How to enforce a custom contraint at project level in GCP

23 Views Asked by At

I am working on a requirement in which i have manually created a custom constraint at organization level, it is not enforced at the organization level.

What I would like to achieve is that i want to simply enforce this constraint on a selected project(not all projects)

from google.cloud import orgpolicy_v2
from google.oauth2 import service_account
from google.cloud.orgpolicy_v2 import types

# Authentication
path_for_cred=""
cred=service_account.Credentials.from_service_account_file(path_for_cred)
project_id="project-id"

def build_policy():

    rule1 = types.PolicySpec.PolicyRule()
    rule1.enforce = True
    #rule1.condition = Exp

    spec = types.PolicySpec()

    spec.rules.append(rule1)


    policy = types.Policy(
        name=f"projects/{project_id}/policies/constraints.disableSerialPortAccess",
        spec = spec
    )

    return policy


def sample_update_policy():
    # Create a client   
    client = orgpolicy_v2.OrgPolicyClient(credentials=cred)

    policy = build_policy()

    # Debug - view created policy
    print(policy)

    # Initialize request argument(s)
    request = orgpolicy_v2.UpdatePolicyRequest(
        policy=policy,
    )

    # Make the request
    response = client.update_policy(request=request)
    
    # Handle the response
    print(response)

sample_update_policy()

I ma getting the below error:

line 849, in _end_unary_response_blocking raise _InactiveRpcError(state) grpc._channel._InactiveRpcError: <_InactiveRpcError of RPC that terminated with: status = StatusCode.NOT_FOUND details = "Requested entity was not found." debug_error_string = "UNKNOWN:Error received from peer ipv6:%5B2404:6800:4007:809::200a%5D:443 {created_time:"2024-02-01T05:30:26.716497763+00:00", grpc_status:5, grpc_message:"Requested entity was not found."}"


0

There are 0 best solutions below