Reference

Some helpers that make google-api-python-client a bit nicer to use.

discovery - helpers for API discovery

googleapiclienthelpers.discovery.build_subresource(servicePath, version, **kargs)[source]

Construct a Resource for interacting with an API’s subresources.

Construct a Resource object for interacting with an API. The servicePath is a dot-delimited string. The first element is the serviceName from the Discovery service. Subsequent elements are the names of subresources accessed via the resulting client. The version is the version of the serviceName from the Discovery service.

Some example servicePaths:
iam.roles iam.projects.serviceAccounts.keys cloudresourcemanager cloudresourcemanager.projects
Parameters:
  • servicePath – string, see above description.
  • version – string, the version of the service.
  • kargs – all other arguments are passed on to build
Returns:

A Resource object with methods for interacting with the service.

iam - helpers for IAM bindings

googleapiclienthelpers.iam.add_binding(client, role, member, **kargs)[source]

Generic function to add an IAM binding to any GCP resource.

This function provides a simple, generic way to add a member to a role for any resource. The caller must supply a client that provides getIamPolicy and setIamPolicy methods. It preserves the policy’s etag to prevent concurrent updates.

Parameters:
  • client – Resource, any googleapiclient resource or subresource.
  • role – string, name of the role to modify (e.g., roles/viewer).
  • member – string, the member to add (e.g., user:email@domain.com, serviceAccount:wooo@yea.iam.gserviceaccount.com).
  • **kargs – passed onto client’s getIamPolicy and setIamPolicy. This must include parameter that identifies the resource whose policy will be modified. The parameter varies; check the docs on client.getIamPolicy for details.
Returns:

the result of client.setIamPolicy

googleapiclienthelpers.iam.get_policy(client, **kargs)[source]

Retrieve a policy with GET or POST, as required

Some getIamPolicy methods are called with GET, and must have an empty body. Other getIamPolicy methods are called with POST, and must have a non-empty body equal to {}. This function provides a common interface to both.

Parameters:
  • client – Resource, any googleapiclient resource or subresource.
  • **kargs – passed onto client.getIamPolicy
Returns:

The result of client.getIamPolicy(**kargs).execute()

googleapiclienthelpers.iam.get_role_bindings(policy, role)[source]

Find the given role’s binding from the given IAM policy.

Given an IAM policy and a desired role, return the binding associated with that role. If the role is not present in the policy, return None.

Parameters:
  • policy – list, the result of any getIamPolicy method.
  • role – string, the role to pull from the policy.
Returns:

A list of dictionaries, or None if the role is not present.

googleapiclienthelpers.iam.remove_binding(client, role, member, **kargs)[source]

Generic function to remove an IAM binding from any GCP resource.

This function provides a simple, generic way to remove a member from a role for any resource. The caller must supply a client that provides getIamPolicy and setIamPolicy methods. It preserves the policy’s etag to prevent concurrent updates.

Parameters:
  • client – Resource, any googleapiclient resource or subresource.
  • role – string, name of the role to modify (e.g., roles/viewer).
  • member – string, the member to add (e.g., user:email@domain.com, serviceAccount:wooo@yea.iam.gserviceaccount.com).
  • **kargs – passed onto client’s getIamPolicy and setIamPolicy. This must include parameter that identifies the resource whose policy will be modified. The parameter varies; check the docs on client.getIamPolicy for details.
Returns:

the result of client.setIamPolicy

waiter - generic way to wait for state

class googleapiclienthelpers.waiter.Waiter(func, *args, **kargs)[source]

Wait for a resource to reach a desired state.

This class implements a generic way to wait for an operation to reach a desired state.

To use: create a new instance by providing a callable and the arguments that it requires. Then, call wait() and the Waiter will poll the callable until the desired status is reached.

The callable you pass must have a .execute() method, like the Resource objects returned by googleapiclient.

For example, you might start an instance and wait until it reaches the RUNNING status: >>> instances = build_subresource(‘compute.instances’, ‘v1’) >>> instances.insert(…) >>> waiter = Waiter(instances.get, project=…, zone=…, instance=…) >>> waiter.wait(‘status’, ‘RUNNING’)

wait(field, value, retries=60, interval=2, terminal_values=[])[source]

Wait until an object reached the desired status

When called, the Waiter will invoke the callable provided at construction and capture its return. The Waiter extracts field, and checks if it’s equal to value. If it is, the full response from the callable is returned.

wait() handles three kinds of field parameter:
  • dict-like return where field is a string that specified a key.
  • object-like return where field is a string naming an attribute.
  • any kind of return where field is a one-place callable that extracts the needed data to compare.
Parameters:
  • field – string or callable, the data to extract.
  • value – varies, the value indicating the desired state.
  • retries – int, maximum number of times to poll.
  • interval – float, time to sleep between polls.
  • terminal_values – list<string> of values that indicate an unrecoverable state. If encountered an exception is raised.
exception googleapiclienthelpers.waiter.WaiterException(*args, response=None)[source]