Tracking the execution progress of the policy

  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
    >>> auth  # the 'Authorization' header value with the access token
    {'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6ImMwMD...'}
  2. Fetch the protection policies as described in Fetching a list of policies and protection plans, then define the policy_id variable and assign it with the ID of a protection policy. As an example, the ID of the first policy will be taken:

    >>> policy_id = policies[0]['id']
    >>> policy_id
  3. [Optional] To get a unique result, fetch the resources as described in Fetching a list of all resources, then define the agent_id variable and assign it with the ID of the agent of a resource. As an example, the agent ID of the first resource will be taken:

    >>> agent_id = resources[0]['agent_id']
    >>> agent_id
  4. Define a variable named filters, and then assign an object containing the ID of the policy in the policy_id key and ID of the agent in the agent_id key to this variable:

    >>> filters = {
    ...     'policy_id': policy_id,
    ...     'agent_id': agent_id
    ... }

    For the list of available query string parameters, refer to the API reference.

  5. Send a GET request to the /policy_management/v4/applications endpoint:

    >>> response = requests.get(
    ...     f'{base_url}/policy_management/v4/applications',
    ...     headers=auth,
    ...     params=filters,
    ... )
  6. Check the status code of the response:

    >>> response.status_code

    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 an object with the items key containing an array with application objects formatted as a JSON text. When converted to an object, it will look as follows:

    >>> pprint.pprint(response.json())
    {'items': [{...
                'agent_id': '23effcf6-2798-4631-9a52-5785bf3af657',
                'context': {'id': '5c350066-2ba6-4eeb-aa91-1213dd35f033',
                            'type': 'resource.machine'},
                'context_tenant_id': '17',
                'id': 'e8987b4d-d191-4751-baae-1aae6d4f36c1',
                'last_activity': '',
                'last_runtime': '2020-10-08T15:04:43.2648116Z',
                'next_activity': '',
                'next_run_time': '2020-10-12T12:10:00Z',
                'origin_contexts': ['5c350066-2ba6-4eeb-aa91-1213dd35f033'],
                'policy': {'id': '33965f81-7293-45d4-9f13-dc4281d7bdfd',
                           'name': '',
                           'type': ''},
                'running': {'progress': '0', 'state': 'running'},
                'status': 'running',
                'tenant_id': '16'},
  7. Iterate over the items array and check the status in the status field and the value of the progress field in the running object of the application(s):

    >>> for application in applications:
    ...     print(f'Status of the {application['policy']['type'] policy is {application['status']}}')
    ...     if application['status'] == 'running':
    ...         print(f'Current progress is {application['running']['progress']}%')