Reputation: 73
I am trying to use python lib azure.mgmt.resourcegraph to receive list all cosmosdb accounts in my tenant.
Here is code:
from azure.mgmt.subscription import SubscriptionClient
from msrestazure.azure_active_directory import ServicePrincipalCredentials
from azure.mgmt.resourcegraph import ResourceGraphClient
credentials = ServicePrincipalCredentials(client_id, secret, tenant=tenant_id)
rgraph_object = ResourceGraphClient(credentials)
test = rgraph_object.resources(query="resources | where type == 'microsoft.documentdb/databaseaccounts'")
Error: AttributeError: 'str' object has no attribute 'get'
Could anybody help with that?
Upvotes: 0
Views: 1330
Reputation: 73
Well, I founded the reason. In general - method resources uses specific type "QueryRequest"
So, correct code is:
from azure.mgmt.subscription import SubscriptionClient
from msrestazure.azure_active_directory import ServicePrincipalCredentials
from azure.mgmt.resourcegraph import ResourceGraphClient
from azure.mgmt.resourcegraph.models import QueryRequest
credentials = ServicePrincipalCredentials(client_id, secret, tenant=tenant_id)
sub_object = SubscriptionClient(credentials)
rgraph_object = ResourceGraphClient(credentials)
subs = [sub.as_dict() for sub in sub_object.subscriptions.list()]
subs_list = []
for sub in subs:
subs_list.append(sub.get('subscription_id'))
request = QueryRequest(subscriptions=subs_list, query="resources | where type == 'microsoft.documentdb/databaseaccounts'")
test = rgraph_object.resources(request)
Upvotes: 1
Reputation: 72171
this:
"resources | where type == 'microsoft.documentdb/databaseaccounts""
should be this:
"resources | where type == 'microsoft.documentdb/databaseaccounts'"
Upvotes: 1