Skip to content

Google Cloud Platform (GCP) Utilities

drizm_commons.google.testing

TestStorageBucket

A class that exposes a simple API for working with GCS buckets in tests.

Examples:

from drizm_commons.google import TestStorageBucket
from google.oauth2 import service_account

credentials = service_account.Credentials.from_service_account_file(
    "path/to/svc.json"
)
test_bucket = TestStorageBucket(
    project_id="your-project-id",
    credentials=credentials
)
test_bucket.create()

# ... do whatever you need to test ...

test_bucket.destroy()

autogenerate_bucket_name() staticmethod

Automatically generates a generic bucket name, in case none has been provided by the user.

create(self, obtain_existing=False)

Create and obtain a testing bucket.

If a bucket already exists under this name, you can pass 'obtain_existing = True' to retrieve it.

If no parameters are provided, this method will attempt to create a new bucket with the given name.

destroy(self)

Delete the testing bucket and all items in it.

As per the limitations of the Python GCS API, this will only work on buckets with 256 blobs or less.

drizm_commons.google.token

force_obtain_id_token(credentials)

Can be used to obtain an OIDC-Token for authenticating to GoogleCloud services and some Google APIs.

This is effectively manually forcing the equivalent of credentials.refresh().

Examples:

from drizm_commons.google import force_obtain_id_token
from google.oauth2 import service_account


auth = service_account.IDTokenCredentials.from_service_account_file(
    "/path/to/svc.json",
    target_audience="https://example.com/"
)
token = force_obtain_id_token(auth)

Returns:

Type Description
str

Returns a Google OpenID-Connect access token as a string.