Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Looking for an example of using skip_token for pagination #142

Open
HappyException opened this issue Nov 2, 2023 · 1 comment
Open

Looking for an example of using skip_token for pagination #142

HappyException opened this issue Nov 2, 2023 · 1 comment

Comments

@HappyException
Copy link

Based on the example here: Quickstart: Your first Python query - Azure Resource Graph | Microsoft Learn
Is there an example showing how to properly use the QueryRequestOptions skip_token for pagination?

@HappyException
Copy link
Author

HappyException commented Nov 8, 2023

Maybe this:

# Import Azure Resource Graph library
import azure.mgmt.resourcegraph as arg

# Import specific methods and models from other libraries
from azure.mgmt.resource import SubscriptionClient
from azure.identity import AzureCliCredential

# Wrap all the work in a function
def getresources( strQuery ):
    # Get your credentials from Azure CLI (development only!) and get your subscription list
    credential = AzureCliCredential()
    subsClient = SubscriptionClient(credential)
    subsRaw = []
    for sub in subsClient.subscriptions.list():
        subsRaw.append(sub.as_dict())
    subsList = []
    for sub in subsRaw:
        subsList.append(sub.get('subscription_id'))

    # Create Azure Resource Graph client and set options
    argClient = arg.ResourceGraphClient(credential)
    argQueryOptions = arg.models.QueryRequestOptions(result_format="objectArray")

    # Create query
    argQuery = arg.models.QueryRequest(subscriptions=subsList, query=strQuery, options=argQueryOptions)
    # Run query
    argResults = argClient.resources(argQuery)
    argResultsData = argResults.data

    # If data is paginated the previous query will have a skip_token for itteration
    while argResults.skip_token:
        token = argResults.skip_token
        argQueryOptions.skip_token = argResults.skip_token
        query = arg.models.QueryRequest(subscriptions=subsList, query=strQuery, options=argQueryOptions)
        argResults = argClient.resources(query)
        argResultsData.extend(argResults.data)

    # Display total records returned
    print(f"Total Records:  {argResults.total_records}")
    return(argResultsData)
getresources("Resources | project name, type ")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant