A “key” uniquely identifying a GCP resource.

Keys can be converted to and from URLs.

class Key(object_kind, object_path)[source]

Bases: object

Represents a reference to a unique GCP object.

Entities in the GCP API have a composite ‘key’ that can uniquely identify them. For instance, if a GET request for an object requires a project and a name, then (project, name) is a unique key for that object. Or, if the URL to an object has a path containing a project, a zone, and an object ID, then (project, zone, ID) forms a key to the object.


Compare a Key with another object for sorting purposes.

Parameters:other (object) – The object to compare with
Returns:(-1 if self < other, 0 if self == other, 1 if self > other)
Return type:int

Hashcode for the object.

Returns:The hash code
Return type:int

Debugging representation of the object.

Returns:The debug string
Return type:str
classmethod _from_url(object_kind, path_component_map, url, defaults=None)[source]

Constructs a key given a GCP object reference URL.

Inter-object references in GCP APIs use URLs. These URLs are a sequence of key-value pairs describing the path to the resource, e.g.: /projects/foo /global /backendServices/bar

describes a backend service named ‘bar’ with global scope in project ‘foo’, whereas: /projects/foo /zones/us-east1-a /instances/bar

describes a compute instance named ‘bar’ in zone ‘us-east1-a’ of project ‘foo’.

  • object_kind (str) – The kind of resource the key represents
  • path_component_map (dict) –

    Maps components in the URL path to object_path keys. In the backend service example, this would be:

    {‘projects’: ‘project_id’,
    ’backendServices’: ‘name’}

    The special component ‘global’ is ignored. If a component not seen in this map is seen, this function raises an exception. The caller is responsible for checking that all required components were present.

  • url (str) – Resource URL
  • defaults (dict) – If non-None, a dictionary specifying default values for object_path keys.

A Key instance

Return type:



ValueError – If the URL is invalid.


Retrieves a specific element from the path.

Parameters:key (str) – The component to retrieve
Returns:The value
Return type:object