Unable to get iam policy for a project's Artifact Registry in GCP using Python GCloud API

201 Views Asked by At

I'm trying to access the IAM permissions for the artifact registry of one of my projects through artifactregistry_v1 python API fro GCP.

name_val = "projects/" + REGISTRY_NAME
    artifact_registry_client = artifactregistry_v1.ArtifactRegistryClient(
    credentials=credentials
)
    req = iam_policy_pb2.GetIamPolicyRequest(resource=name_val)
    policy = artifact_registry_client.get_iam_policy(request=req)
    print(policy)

When I try this code, I get an error: 400 requests contain an invalid argument for the line request=req.

But if I just pass the resource into the get_iam_policy function, I get an error saying it got an unexpected keyword argument 'resource'.

1

There are 1 best solutions below

0
Robert G On

Based on this documentation on get_iam_policy, when you make the request, the variable for the request argument(s) should be the same when you make the request.

In your sample code, it should look like this:

name_val = "projects/" + REGISTRY_NAME
    artifact_registry_client = artifactregistry_v1.ArtifactRegistryClient(
    credentials=credentials
)
    req = iam_policy_pb2.GetIamPolicyRequest(resource=name_val)
    policy = artifact_registry_client.get_iam_policy(req=req) # the code should look like this
    print(policy)