Create Org Policy constraints with Python

55 Views Asked by At

I am working on a project to create an Org policy using python. The specific policy which I want to create is set the value of the constraint "constraints/iam.serviceAccountKeyExpiryHours" to "24h".

The documents which I am following for this specific task is:

https://cloud.google.com/python/docs/reference/orgpolicy/latest/google.cloud.orgpolicy_v2.types.PolicySpec

https://cloud.google.com/python/docs/reference/orgpolicy/latest/google.cloud.orgpolicy_v2.types.PolicySpec.PolicyRule.StringValues

The code I have written so far is as below:

from google.cloud import orgpolicy_v2
from google.cloud.orgpolicy_v2 import types

def build_policy():
    rule = types.PolicySpec.PolicyRule.StringValues()
    rule.allowed_values = ["24h"]
    spec = types.PolicySpec.PolicyRule()
    spec.values.append(rule)

    policy = types.Policy(
        name="projects/{project_id}/policies/iam.serviceAccountKeyExpiryHours",
        spec = spec
    )

    return policy

def set_organization_policy():
    # Create the orgpolicies client
    client = orgpolicy_v2.OrgPolicyClient()
    policy = build_policy()
    request = orgpolicy_v2.UpdatePolicyRequest(
        policy=policy
    )

    # Make the request
    response = client.create_policy(request=request)

    # Handle the response
    print(response)

However, I am getting error: AttributeError: Unknown field for StringValues: append

Could you please help me with it ?

Thank you

0

There are 0 best solutions below