Fetching a usage report from tenant reports

  1. Authenticate to the cloud platform via the Python shell.

    The following variables should be available now:

    >>> base_url  # the base URL of the API
    'https://eu2-cloud.acronis.com/api/2'
    >>> auth  # the 'Authorization' header value with the access token
    {'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6ImMwMD...'}
    >>> tenant_id  # the UUID of the tenant to which the token provides access
    'ede9f834-70b3-476c-83d9-736f9f8c7dae'
    
  2. Send a GET request to the /tenants/{tenant_id}/reports endpoint:

    >>> response = requests.get(f'{base_url}/tenants/{tenant_id}/reports', headers=auth)
    
  3. Check the status code of the response:

    >>> response.status_code
    200
    

    Status code 200 means that the request was successful.

    A different status code means that an error has occurred. For the details, refer to “Status and error codes”.

    Also, the response body contains the items array with the report UUIDs formatted as a JSON text. When converted to an object, it will look as follows:

    >>> pprint.pprint(response.json())
    {'items': ['aa4f8923-8950-4804-8827-c6d78388e5b6',
               '4eb7b320-48b4-4552-9bf8-f7482538da23']}
    
  4. Store the array of the report UUIDs in a variable:

    >>> report_ids = response.json()['items']
    
  5. Iterate over the report_ids array, send GET requests to the /reports/{report_id} endpoint and store the response results in an array:

    >>> results = []
    >>> for report_id in report_ids:
    ...     response = requests.get(f'{base_url}/reports/{report_id}', headers=auth)
    ...     results.append(response)
    
  6. If all requests were successful, the results array will look as follows:

    >>> pprint.pprint(results)
    [{'generation_date': '2017-05-08',
      'id': 'a0eab795-2edd-4a47-8eb8-01efa775dc09',
      'parameters': {'formats': ['csv'],
                     'kind': 'usage_summary',
                     'level': 'accounts',
                     'period': {'end': '2017-05-07', 'start': '2017-05-01'},
                     'tenant_id': 'ede9f834-70b3-476c-83d9-736f9f8c7dae'},
      'recipients': ['ca451758-b48a-45d9-9ae6-8f1059f67619',
                     'ab00e527-219b-481b-b01b-311b5be0ed3c'],
      'result_action': 'send',
      'schedule': {'enabled': True, 'type': 'once'},
      'version': 2},
     {'generation_date': '2017-05-08',
      'id': '20dffb36-d77b-45c6-b2fa-39276e98d5fc',
      'parameters': {'formats': ['csv', 'html'],
                     'kind': 'usage_summary',
                     'level': 'accounts',
                     'period': {'end': '2017-05-07', 'start': '2017-05-01'},
                     'tenant_id': 'ede9f834-70b3-476c-83d9-736f9f8c7dae'},
      'recipients': ['ca451758-b48a-45d9-9ae6-8f1059f67619',
                     'ab00e527-219b-481b-b01b-311b5be0ed3c'],
      'result_action': 'send',
      'schedule': {'enabled': True, 'type': 'once'},
      'version': 1}]
    
  7. Choose a report and store its UUID in a variable. As an example, the UUID of the last report will be taken:

    >>> report_id = results[-1]['id']
    >>> report_id
    '20dffb36-d77b-45c6-b2fa-39276e98d5fc'
    
  8. [Optional] Store the revision number of the report, in case if you need to delete it:

    >>> version = results[-1]['version']
    >>> version
    1