Skip to content

Instantly share code, notes, and snippets.

@pharaujo
Last active June 11, 2019 14:16
Show Gist options
  • Select an option

  • Save pharaujo/43c110e68cf381dca9b731bd0715b7e9 to your computer and use it in GitHub Desktop.

Select an option

Save pharaujo/43c110e68cf381dca9b731bd0715b7e9 to your computer and use it in GitHub Desktop.

Revisions

  1. pharaujo revised this gist Jun 11, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion 1.log
    Original file line number Diff line number Diff line change
    @@ -49,4 +49,4 @@ Traceback (most recent call last):
    query_response = self.arg.resources(query_request)
    File ".../.env/lib/python2.7/site-packages/azure/mgmt/resourcegraph/resource_graph_client.py", line 123, in resources
    raise models.ErrorResponseException(self._deserialize, response)
    azure.mgmt.resourcegraph.models.error_response.ErrorResponseException: Operation returned an invalid
    azure.mgmt.resourcegraph.models.error_response.ErrorResponseException: Operation returned an invalid status code ''
  2. pharaujo created this gist Jun 7, 2019.
    52 changes: 52 additions & 0 deletions 1.log
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    ...
    DEBUG:msrest.http_logger:Request URL: 'https://management.azure.com/providers/Microsoft.ResourceGraph/resources?api-version=2019-04-01'
    DEBUG:msrest.http_logger:Request method: 'POST'
    DEBUG:msrest.http_logger:Request headers:
    DEBUG:msrest.http_logger: 'accept-language': 'en-US'
    DEBUG:msrest.http_logger: 'Content-Type': 'application/json; charset=utf-8'
    DEBUG:msrest.http_logger: 'x-ms-client-request-id': '<REDACTED>'
    DEBUG:msrest.http_logger: 'Accept': 'application/json'
    DEBUG:msrest.http_logger: 'Content-Length': '451'
    DEBUG:msrest.http_logger: 'User-Agent': 'python/2.7.16 (Darwin-18.6.0-x86_64-i386-64bit) msrest/0.6.6 msrest_azure/0.6.0 azure-mgmt-resourcegraph/1.0.0 Azure-SDK-For-Python'
    DEBUG:msrest.http_logger:Request body:
    DEBUG:msrest.http_logger:{"query": "where type =~ 'Microsoft.Network/networkInterfaces'| project name, id, properties.virtualMachine, properties.macAddress, properties.ipConfigur
    ations", "options": {"$skipToken": "<REDACTED>"}, "subscriptions": ["<REDACTED>"]}
    DEBUG:msrest.universal_http:Configuring redirects: allow=True, max=30
    DEBUG:msrest.universal_http:Configuring request: timeout=100, verify=True, cert=None
    DEBUG:msrest.universal_http:Configuring proxies: ''
    DEBUG:msrest.universal_http:Evaluate proxies against ENV settings: True
    DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): management.azure.com:443
    DEBUG:urllib3.connectionpool:https://management.azure.com:443 "POST /providers/Microsoft.ResourceGraph/resources?api-version=2019-04-01 HTTP/1.1" 429 2
    DEBUG:msrest.http_logger:Response status: 429
    DEBUG:msrest.http_logger:Response headers:
    DEBUG:msrest.http_logger: 'Cache-Control': 'no-cache'
    DEBUG:msrest.http_logger: 'Pragma': 'no-cache'
    DEBUG:msrest.http_logger: 'Content-Length': '2'
    DEBUG:msrest.http_logger: 'Content-Type': 'application/json; charset=utf-8'
    DEBUG:msrest.http_logger: 'Expires': '-1'
    DEBUG:msrest.http_logger: 'Strict-Transport-Security': 'max-age=31536000; includeSubDomains'
    DEBUG:msrest.http_logger: 'x-ms-user-quota-resets-after': '00:00:05'
    DEBUG:msrest.http_logger: 'x-ms-user-quota-remaining': '0'
    DEBUG:msrest.http_logger: 'x-ms-ratelimit-remaining-tenant-resource-requests': '0'
    DEBUG:msrest.http_logger: 'Server': 'Microsoft-HTTPAPI/2.0'
    DEBUG:msrest.http_logger: 'x-ms-ratelimit-remaining-tenant-reads': '11991'
    DEBUG:msrest.http_logger: 'x-ms-request-id': '656e909a-1aea-400a-8a19-108060883623'
    DEBUG:msrest.http_logger: 'x-ms-correlation-request-id': '656e909a-1aea-400a-8a19-108060883623'
    DEBUG:msrest.http_logger: 'x-ms-routing-request-id': 'FRANCESOUTH:20190606T143814Z:656e909a-1aea-400a-8a19-108060883623'
    DEBUG:msrest.http_logger: 'X-Content-Type-Options': 'nosniff'
    DEBUG:msrest.http_logger: 'Date': 'Thu, 06 Jun 2019 14:38:14 GMT'
    DEBUG:msrest.http_logger: 'Connection': 'close'
    DEBUG:msrest.http_logger:Response content:
    DEBUG:msrest.http_logger:""
    DEBUG:msrest.exceptions:Unable to deserialize to object: type, AttributeError: 'unicode' object has no attribute 'get'
    DEBUG:msrest.exceptions:Operation returned an invalid status code ''
    Traceback (most recent call last):
    File "./arg.py", line 57, in <module>
    arg.instances()
    File "./arg.py", line 42, in instances
    nics = self._resources('where type =~ \'Microsoft.Network/'
    File "./arg.py", line 34, in _resources
    query_response = self.arg.resources(query_request)
    File ".../.env/lib/python2.7/site-packages/azure/mgmt/resourcegraph/resource_graph_client.py", line 123, in resources
    raise models.ErrorResponseException(self._deserialize, response)
    azure.mgmt.resourcegraph.models.error_response.ErrorResponseException: Operation returned an invalid
    60 changes: 60 additions & 0 deletions arg.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    #!/usr/bin/env python

    # Run this in parallel to hit the rate limit
    # $ time (for i in {1..10}; do (python ./arg.py 2>${i}.log)& done ; wait)

    import logging
    import sys

    from azure.common.credentials import ServicePrincipalCredentials
    from azure.mgmt.resourcegraph import ResourceGraphClient
    from azure.mgmt.resourcegraph.models import QueryRequest
    from azure.mgmt.resourcegraph.models import QueryRequestOptions

    import config

    logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)


    class ARG(object):

    def __init__(self, subscription_id, credentials):
    self.subscription_id = subscription_id
    self.arg = ResourceGraphClient(credentials=credentials)
    self.arg.config.enable_http_logger = True

    def _resources(self, query):

    query_request = QueryRequest(
    query=query,
    subscriptions=[self.subscription_id]
    )
    query_response = self.arg.resources(query_request)

    while query_response.skip_token:
    token = query_response.skip_token
    query_request.options = QueryRequestOptions(skip_token=token)
    query_response = self.arg.resources(query_request)

    def instances(self):
    vms = self._resources('where type =~ '
    '\'Microsoft.Compute/virtualMachines\' | '
    'project name, id, location, tags, '
    'properties.storageProfile.osDisk.osType,'
    'properties.availabilitySet.id')
    nics = self._resources('where type =~ \'Microsoft.Network/'
    'networkInterfaces\'| '
    'project name, '
    'id, '
    'properties.virtualMachine, '
    'properties.macAddress, '
    'properties.ipConfigurations')

    cred = ServicePrincipalCredentials(
    tenant=config.TENANT_ID,
    client_id=config.CLIENT_ID,
    secret=config.SECRET)

    arg = ARG(subscription_id=config.SUBSCRIPTION_ID,
    credentials=cred)
    arg.instances()
    4 changes: 4 additions & 0 deletions config.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    TENANT_ID = ""
    CLIENT_ID = ""
    SECRET = ""
    SUBSCRIPTION_ID = ""
    25 changes: 25 additions & 0 deletions pip_freeze.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    adal==1.2.1
    asn1crypto==0.24.0
    azure-common==1.1.21
    azure-mgmt-nspkg==3.0.2
    azure-mgmt-resourcegraph==1.0.0
    azure-nspkg==3.0.2
    certifi==2019.3.9
    cffi==1.12.3
    chardet==3.0.4
    cryptography==2.7
    enum34==1.1.6
    idna==2.8
    ipaddress==1.0.22
    isodate==0.6.0
    msrest==0.6.6
    msrestazure==0.6.0
    oauthlib==3.0.1
    pycparser==2.19
    PyJWT==1.7.1
    python-dateutil==2.8.0
    requests==2.22.0
    requests-oauthlib==1.2.0
    six==1.12.0
    typing==3.6.6
    urllib3==1.25.3