Applying a protection plan to resources

  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 plans 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 plan. As an example, the ID of the first protection plan will be taken:

    >>> policy_id = protection_plans[0]['id']
    >>> policy_id
  3. 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
  4. Define a variable named application_data, and then assign an object containing the policy_id key with the ID of protection plan, and the context object with the items key containing a list of resource IDs to this variable:

    >>> application_data = {
    ...     'policy_id': policy_id,
    ...     'context': {
    ...         'items': [
    ...             resource_id
    ...         ]
    ...     }
    ... }
  5. Convert the application_data object to a JSON text:

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

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

    >>> response.status_code

    Status code 200 means that the protection plan has been applied to provided resources.

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

    >>> pprint.pprint(response.json())
    {'issues': []}