Start execution of a 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


    Execution of the policy will start execution of its enabled children policies.

  3. [Optional] Fetch the resources as described in Fetching a list of all resources, then define the resource_id variable and assign it with the ID of a resource. As an example, the ID of the first resource will be taken:

    >>> resource_id = resources[0]['id']
    >>> resource_id


    If resources are not specified in the request, the policy will be executed on all resources to which it is applied.

  4. Define a variable named policy_exec_data, and then assign an object containing the following JSON parameters to this variable:

    >>> policy_exec_data = {
    ...     'state': 'running',
    ...     'policy_id': policy_id,
    ...     'context_ids': [resource_id]
    ... }

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

  5. Convert the policy_exec_data object to a JSON text:

    >>> policy_exec_data = json.dumps(policy_exec_data, indent=4)
  6. Send a PUT request with the JSON text to the /policy_management/v4/applications/run endpoint:

    >>> response = requests.put(
    ...     f'{base_url}/policy_management/v4/applications/run',
    ...     headers={'Content-Type': 'application/json', **auth},
    ...     data=policy_exec_data,
    ... )
  7. Check the status code of the response:

    >>> response.status_code

    Status code 202 means that the request was successful. If you received error code 422 and RunNowRespBody contains a message related to application status or licensing, make sure that the provided policy is enabled and the resource has correct service quota assigned.

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