Skip to content

put_resource_policy

put_resource_policy(
    resource_arn: ResourcePolicyResourceArn,
    policy: ResourcePolicy,
    *,
    config_overrides: Optional[ContextClientConfig] = None,
    expected_revision_id: Optional[RevisionId] = None,
) -> PutResourcePolicyResponse

Attaches a resource-based policy to an agent context, or replaces the one it has. The policy controls which other accounts can call operations on the agent context. Returns the policy's revision ID.

Parameters:

  • resource_arn (ResourcePolicyResourceArn) –

    ARN of the agent context to attach the policy to.

  • policy (ResourcePolicy) –

    The resource-based policy document.

  • expected_revision_id (Optional[RevisionId], default: None ) –

    The revision that you last read. The write succeeds only if it matches the stored revision.

Raises:

  • AccessDeniedException –

    You don't have sufficient access to perform this operation. Verify that you have permission to perform the operation on the resource.

  • InternalServerException –

    The request failed because of an internal error. Retry the request.

  • ThrottlingException –

    The request was denied because it exceeded a request rate quota. Wait, then retry the request.

  • ValidationException –

    The input fails to satisfy the constraints of the operation. Correct the input before you retry the request.

  • ConflictException –

    The request conflicts with the current state of the resource. Check the resource's current state before you retry the request.

  • ResourceNotFoundException –

    The specified resource was not found. Check that the ID is correct and that the resource exists in your account and Region.

  • UnknownServiceError –

    The service returned an error code this client does not model.

Examples:

Attach a resource-based policy to an agent context The following example lets account 444455556666 also get an agent context, in addition to searching and traversing it, replacing the policy at the revision that GetResourcePolicy returned.

>>> client.put_resource_policy(resource_arn='arn:aws:context:us-east-1:111122223333:agent-context/ac-3f9c2e7a1b8d4c56a0e4f7b29d1c8e53', policy='{"Version":"2012-10-17","Statement":[{"Sid":"AllowAnalyticsAccountRead","Effect":"Allow","Principal":{"AWS":"arn:aws:iam::444455556666:root"},"Action":["context:GetAgentContext","context:SearchAgentContext","context:TraverseAgentContext"],"Resource":"arn:aws:context:us-east-1:111122223333:agent-context/ac-3f9c2e7a1b8d4c56a0e4f7b29d1c8e53"}]}', expected_revision_id='3c7e1a9f-2b4d-4e6a-8f0c-5d9b1e3a7c2f')
Put a policy with a stale revision ID, which fails with ConflictException
The following example shows the ConflictException returned when expectedRevisionId doesn't match the stored revision because another writer changed the policy first.
>>> client.put_resource_policy(resource_arn='arn:aws:context:us-east-1:111122223333:agent-context/ac-3f9c2e7a1b8d4c56a0e4f7b29d1c8e53', policy='{"Version":"2012-10-17","Statement":[{"Sid":"AllowAnalyticsAccountRead","Effect":"Allow","Principal":{"AWS":"arn:aws:iam::444455556666:root"},"Action":["context:SearchAgentContext","context:TraverseAgentContext"],"Resource":"arn:aws:context:us-east-1:111122223333:agent-context/ac-3f9c2e7a1b8d4c56a0e4f7b29d1c8e53"}]}', expected_revision_id='3c7e1a9f-2b4d-4e6a-8f0c-5d9b1e3a7c2f')
Source code in docs_src/capo_context/_services/context.py
def put_resource_policy(
    self,
    resource_arn: "capo_context.types.resource_policy_resource_arn.ResourcePolicyResourceArn",
    policy: "capo_context.types.resource_policy.ResourcePolicy",
    *,
    config_overrides: Optional[ContextClientConfig] = None,
    expected_revision_id: Optional[
        "capo_context.types.revision_id.RevisionId"
    ] = None,
) -> "capo_context.types.put_resource_policy_response.PutResourcePolicyResponse":
    """<p>Attaches a resource-based policy to an agent context, or replaces the one it has. The policy controls which other accounts can call operations on the agent context. Returns the policy's revision ID.</p>

    Args:
        resource_arn: <p>ARN of the agent context to attach the policy to.</p>
        policy: <p>The resource-based policy document.</p>
        expected_revision_id: <p>The revision that you last read. The write succeeds only if it matches the stored revision.</p>

    Raises:
        capo_context.errors.access_denied_exception.AccessDeniedException: <p>You don't have sufficient access to perform this operation. Verify that you have permission to perform the operation on the resource.</p>
        capo_context.errors.internal_server_exception.InternalServerException: <p>The request failed because of an internal error. Retry the request.</p>
        capo_context.errors.throttling_exception.ThrottlingException: <p>The request was denied because it exceeded a request rate quota. Wait, then retry the request.</p>
        capo_context.errors.validation_exception.ValidationException: <p>The input fails to satisfy the constraints of the operation. Correct the input before you retry the request.</p>
        capo_context.errors.conflict_exception.ConflictException: <p>The request conflicts with the current state of the resource. Check the resource's current state before you retry the request.</p>
        capo_context.errors.resource_not_found_exception.ResourceNotFoundException: <p>The specified resource was not found. Check that the ID is correct and that the resource exists in your account and Region.</p>
        capo_context.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        Attach a resource-based policy to an agent context
        The following example lets account 444455556666 also get an agent context, in addition to searching and traversing it, replacing the policy at the revision that GetResourcePolicy returned.

        >>> client.put_resource_policy(resource_arn='arn:aws:context:us-east-1:111122223333:agent-context/ac-3f9c2e7a1b8d4c56a0e4f7b29d1c8e53', policy='{"Version":"2012-10-17","Statement":[{"Sid":"AllowAnalyticsAccountRead","Effect":"Allow","Principal":{"AWS":"arn:aws:iam::444455556666:root"},"Action":["context:GetAgentContext","context:SearchAgentContext","context:TraverseAgentContext"],"Resource":"arn:aws:context:us-east-1:111122223333:agent-context/ac-3f9c2e7a1b8d4c56a0e4f7b29d1c8e53"}]}', expected_revision_id='3c7e1a9f-2b4d-4e6a-8f0c-5d9b1e3a7c2f')
        Put a policy with a stale revision ID, which fails with ConflictException
        The following example shows the ConflictException returned when expectedRevisionId doesn't match the stored revision because another writer changed the policy first.

        >>> client.put_resource_policy(resource_arn='arn:aws:context:us-east-1:111122223333:agent-context/ac-3f9c2e7a1b8d4c56a0e4f7b29d1c8e53', policy='{"Version":"2012-10-17","Statement":[{"Sid":"AllowAnalyticsAccountRead","Effect":"Allow","Principal":{"AWS":"arn:aws:iam::444455556666:root"},"Action":["context:SearchAgentContext","context:TraverseAgentContext"],"Resource":"arn:aws:context:us-east-1:111122223333:agent-context/ac-3f9c2e7a1b8d4c56a0e4f7b29d1c8e53"}]}', expected_revision_id='3c7e1a9f-2b4d-4e6a-8f0c-5d9b1e3a7c2f')
    """

    def _handler(
        req: "OperationRequest[capo_context.types.put_resource_policy_request.PutResourcePolicyRequest]",
    ) -> OperationResponse[
        "capo_context.types.put_resource_policy_response.PutResourcePolicyResponse"
    ]:
        import capo_context._operations.context.put_resource_policy

        output, http_response = (
            capo_context._operations.context.put_resource_policy.put_resource_policy(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("PutResourcePolicy")
    )
    input_: capo_context.types.put_resource_policy_request.PutResourcePolicyRequest = {
        "resource_arn": resource_arn,
        "policy": policy,
    }
    if expected_revision_id is not None:
        input_["expected_revision_id"] = expected_revision_id

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output