google.cloud.forseti.common.gcp_api.compute module

Wrapper for Compute API client.

class ComputeClient(global_configs, **kwargs)[source]

Bases: object

Compute Client.

ESTIMATED_API_COMPLETION_IN_SEC = 7
delete_firewall_rule(project_id, rule, uuid=None, blocking=False, retry_count=0, timeout=0)[source]

Delete a firewall rule.

Parameters:
  • project_id (str) – The project id.
  • rule (dict) – The firewall rule dict to delete.
  • uuid (str) – An optional UUID to identify this request. If the same request is resent to the API, it will ignore the additional requests. If uuid is not set, one will be generated for the request.
  • blocking (bool) – If true, don’t return until the async operation completes on the backend or timeout seconds pass.
  • retry_count (int) – If greater than 0, retry on operation timeout.
  • timeout (float) – If greater than 0 and blocking is True, then raise an exception if timeout seconds pass before the operation completes.
Returns:

Global Operation status and info. https://cloud.google.com/compute/docs/reference/latest/globalOperations/get

Return type:

dict

Raises:

OperationTimeoutError – Raised if the operation times out.

get_backend_services(project_id)[source]

Get the backend services for a project.

Parameters:project_id (str) – The project id.
Returns:A list of backend services for this project.
Return type:list
get_disks(project_id, zone=None)[source]

Return the list of all disks in the project.

Parameters:
  • project_id (str) – The project id.
  • zone (str) – An optional zone to query, if not provided then all disks in all zones are returned.
Returns:

A list of disk resources for this project.

Return type:

list

get_firewall_quota(project_id)[source]

Calls get_quota to request the firewall quota :param project_id: The project id. :type project_id: str

Returns:The quota of a requested metric in a dict.
Return type:dict

An example return value:

{
“metric”: “FIREWALLS”, “limit”: 100.0, “usage”: 9.0

}

Raises:KeyError – Metric was not a firewall resource.
get_firewall_rules(project_id)[source]

Get the firewall rules for a given project id.

Parameters:project_id (str) – The project id.
Returns:A list of firewall rules for this project id.
Return type:list
get_forwarding_rules(project_id, region=None)[source]

Get the forwarding rules for a project.

If no region name is specified, use aggregatedList() to query for forwarding rules in all regions.

Parameters:
  • project_id (str) – The project id.
  • region (str) – The region name.
Returns:

A list of forwarding rules for this project.

Return type:

list

get_global_operation(project_id, operation_id)[source]

Get the Operations Status :param project_id: The project id. :type project_id: str :param operation_id: The operation id. :type operation_id: str

Returns:

Global Operation status and info. https://cloud.google.com/compute/docs/reference/latest/globalOperations/get

Return type:

dict

Raises:
  • ApiNotEnabledError – Returns if the api is not enabled.
  • ApiExecutionError – Returns if the api is not executable.
get_image(project_id, image_name)[source]

Get an image from a project.

Parameters:
  • project_id (str) – The project id.
  • image_name (str) – The image name to get.
Returns:

A Compute Image resource dict. https://cloud.google.com/compute/docs/reference/latest/images

Return type:

dict

get_images(project_id)[source]

Get all images created in a project.

Parameters:project_id (str) – The project id.
Returns:A list of images for this project.
Return type:list
get_instance_group_instances(project_id, instance_group_name, region=None, zone=None)[source]

Get the instance groups for a project.

One and only one of zone (for zonal instance groups) and region (for regional instance groups) must be specified.

Parameters:
  • project_id (str) – The project id.
  • instance_group_name (str) – The instance group’s name.
  • region (str) – The regional instance group’s region.
  • zone (str) – The zonal instance group’s zone.
Returns:

instance URLs for this instance group.

Return type:

list

Raises:

ValueError – invalid combination of parameters

get_instance_group_managers(project_id)[source]

Get the instance group managers for a project.

Parameters:project_id (str) – The project id.
Returns:A list of instance group managers for this project.
Return type:list
get_instance_groups(project_id, include_instance_urls=True)[source]

Get the instance groups for a project.

Parameters:
  • project_id (str) – The project id.
  • include_instance_urls (bool) – If true, fetch instance urls for each instance group and include them in the resource dictionary.
Returns:

A list of instance groups for this project.

Return type:

list

get_instance_templates(project_id)[source]

Get the instance templates for a project.

Parameters:project_id (str) – The project id.
Returns:A list of instance templates for this project.
Return type:list
get_instances(project_id, zone=None)[source]

Get the instances for a project.

Parameters:
  • project_id (str) – The project id.
  • zone (str) – The zone to list the instances in.
Returns:

A list of instances for this project.

Return type:

list

get_networks(project_id)[source]

Get the networks list for a given project id.

Parameters:project_id (str) – The project id.
Returns:A list of networks for this project id.
Return type:list
get_project(project_id)[source]

Returns the specified Project resource.

Parameters:project_id (str) – The project id.
Returns:A Compute Project resource dict. https://cloud.google.com/compute/docs/reference/latest/projects/get
Return type:dict
get_quota(project_id, metric)[source]

Returns the quota for any metric

Parameters:
  • project_id (str) – The project id.
  • metric (str) – The metric name of the quota needed.
Returns:

The quota of a requested metric in a dict.

Return type:

dict

An example return value:

{
“metric”: “FIREWALLS”, “limit”: 100.0, “usage”: 9.0

}

Raises:KeyError – Metric was not found in the project.
get_snapshots(project_id)[source]

Return the list of all snapshots in the project.

Parameters:project_id (str) – The project id.
Returns:A list of snapshot resources for this project.
Return type:list
get_subnetworks(project_id, region=None)[source]

Return the list of all subnetworks in the project.

Parameters:
  • project_id (str) – The project id.
  • region (str) – An optional region to query, if not provided then all subnetworks in all regions are returned.
Returns:

A list of subnetwork resources for this project.

Return type:

list

insert_firewall_rule(project_id, rule, uuid=None, blocking=False, retry_count=0, timeout=0)[source]

Insert a firewall rule.

Parameters:
  • project_id (str) – The project id.
  • rule (dict) – The firewall rule dict to insert.
  • uuid (str) – An optional UUID to identify this request. If the same request is resent to the API, it will ignore the additional requests. If uuid is not set, one will be generated for the request.
  • blocking (bool) – If true, don’t return until the async operation completes on the backend or timeout seconds pass.
  • retry_count (int) – If greater than 0, retry on operation timeout.
  • timeout (float) – If greater than 0 and blocking is True, then raise an exception if timeout seconds pass before the operation completes.
Returns:

Global Operation status and info. https://cloud.google.com/compute/docs/reference/latest/globalOperations/get

Return type:

dict

Raises:

OperationTimeoutError – Raised if the operation times out.

is_api_enabled(project_id)[source]

Checks if the Compute API is enabled for the specified project.

Parameters:project_id (str) – The project id.
Returns:True if the API is enabled, else False.
Return type:bool
update_firewall_rule(project_id, rule, uuid=None, blocking=False, retry_count=0, timeout=0)[source]

Update a firewall rule.

Parameters:
  • project_id (str) – The project id.
  • rule (dict) – The firewall rule dict to update.
  • uuid (str) – An optional UUID to identify this request. If the same request is resent to the API, it will ignore the additional requests. If uuid is not set, one will be generated for the request.
  • blocking (bool) – If true, don’t return until the async operation completes on the backend or timeout seconds pass.
  • retry_count (int) – If greater than 0, retry on operation timeout.
  • timeout (float) – If greater than 0 and blocking is True, then raise an exception if timeout seconds pass before the operation completes.
Returns:

Global Operation status and info. https://cloud.google.com/compute/docs/reference/latest/globalOperations/get

Return type:

dict

Raises:

OperationTimeoutError – Raised if the operation times out.

wait_for_completion(project_id, operation, timeout=0, initial_delay=None)[source]

Wait for the operation to complete.

Parameters:
  • project_id (str) – The project id.
  • operation (dict) – The global operation response from an API call.
  • timeout (float) – The maximum time to wait for the operation to complete.
  • initial_delay (float) – The time to wait before first checking if the API has completed.
Returns:

Global Operation status and info. https://cloud.google.com/compute/docs/reference/latest/globalOperations/get

Return type:

dict

Raises:

OperationTimeoutError – Raised if the operation times out.

class ComputeRepositoryClient(quota_max_calls=None, quota_period=100.0, use_rate_limiter=True, read_only=False)[source]

Bases: google.cloud.forseti.common.gcp_api._base_repository.BaseRepositoryClient

Compute API Respository.

backend_services

Returns a _ComputeBackendServicesRepository instance.

disks

Returns a _ComputeDisksRepository instance.

firewalls

Returns a _ComputeFirewallsRepository instance.

forwarding_rules

Returns a _ComputeForwardingRulesRepository instance.

global_operations

Returns a _ComputeGlobalOperationsRepository instance.

images

Returns a _ComputeImagesRepository instance.

instance_group_managers

Returns a _ComputeInstanceGroupManagersRepository instance.

instance_groups

Returns a _ComputeInstanceGroupsRepository instance.

instance_templates

Returns a _ComputeInstanceTemplatesRepository instance.

instances

Returns a _ComputeInstancesRepository instance.

networks

Returns a _ComputeNetworksRepository instance.

projects

Returns a _ComputeProjectsRepository instance.

region_instance_groups

Returns a _ComputeRegionInstanceGroupsRepository instance.

snapshots

Returns a _ComputeSnapshotsRepository instance.

subnetworks

Returns a _ComputeSubnetworksRepository instance.

class _ComputeBackendServicesRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.AggregatedListQueryMixin, google.cloud.forseti.common.gcp_api.repository_mixins.ListQueryMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Backend Services repository.

class _ComputeDisksRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.AggregatedListQueryMixin, google.cloud.forseti.common.gcp_api.repository_mixins.ListQueryMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Disks repository.

list(resource, zone, **kwargs)[source]

List disks by zone.

Parameters:
  • resource (str) – The project to query resources for.
  • zone (str) – The zone of the instance group to query.
  • **kwargs (dict) – Additional args to pass through to the base method.
Returns:

An iterator over each page of results from the API.

Return type:

iterator

class _ComputeFirewallsRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.ListQueryMixin, google.cloud.forseti.common.gcp_api.repository_mixins.InsertResourceMixin, google.cloud.forseti.common.gcp_api.repository_mixins.UpdateResourceMixin, google.cloud.forseti.common.gcp_api.repository_mixins.DeleteResourceMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Firewall Rules repository.

class _ComputeForwardingRulesRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.AggregatedListQueryMixin, google.cloud.forseti.common.gcp_api.repository_mixins.ListQueryMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Forwarding Rules repository.

list(resource, region, **kwargs)[source]

List instances by zone.

Parameters:
  • resource (str) – The project to query resources for.
  • region (str) – The region of the forwarding rules to query.
  • **kwargs (dict) – Additional args to pass through to the base method.
Returns:

An iterator over each page of results from the API.

Return type:

iterator

class _ComputeGlobalOperationsRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.GetQueryMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Global Operations repository.

class _ComputeImagesRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.GetQueryMixin, google.cloud.forseti.common.gcp_api.repository_mixins.ListQueryMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Images repository.

class _ComputeInstanceGroupManagersRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.AggregatedListQueryMixin, google.cloud.forseti.common.gcp_api.repository_mixins.ListQueryMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Instance Group Managers repository.

class _ComputeInstanceGroupsRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.AggregatedListQueryMixin, google.cloud.forseti.common.gcp_api.repository_mixins.ListQueryMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Instance Groups repository.

list_instances(resource, instance_group, zone, **kwargs)[source]

List instances for instance group.

Parameters:
  • resource (str) – The project to query resources for.
  • instance_group (str) – The name of the instance group to query.
  • zone (str) – The zone of the instance group to query.
  • **kwargs (dict) – Additional args to pass through to the base method.
Returns:

An iterator over each page of results from the API.

Return type:

iterator

class _ComputeInstanceTemplatesRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.ListQueryMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Instance Templates repository.

class _ComputeInstancesRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.AggregatedListQueryMixin, google.cloud.forseti.common.gcp_api.repository_mixins.ListQueryMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Instances repository.

list(resource, zone, **kwargs)[source]

List instances by zone.

Parameters:
  • resource (str) – The project to query resources for.
  • zone (str) – The zone of the instance group to query.
  • **kwargs (dict) – Additional args to pass through to the base method.
Returns:

An iterator over each page of results from the API.

Return type:

iterator

class _ComputeNetworksRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.ListQueryMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Networks repository.

class _ComputeProjectsRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.GetQueryMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Projects repository.

class _ComputeRegionInstanceGroupsRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.ListQueryMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Region Instance Groups repository.

list_instances(resource, instance_group, region, **kwargs)[source]

List instances for instance group.

Parameters:
  • resource (str) – The project to query resources for.
  • instance_group (str) – The name of the instance group to query.
  • region (str) – The region of the instance group to query.
  • **kwargs (dict) – Additional args to pass through to the base method.
Returns:

An iterator over each page of results from the API.

Return type:

iterator

class _ComputeSnapshotsRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.ListQueryMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Snapshots repository.

class _ComputeSubnetworksRepository(**kwargs)[source]

Bases: google.cloud.forseti.common.gcp_api.repository_mixins.AggregatedListQueryMixin, google.cloud.forseti.common.gcp_api.repository_mixins.ListQueryMixin, google.cloud.forseti.common.gcp_api._base_repository.GCPRepository

Implementation of Compute Subnetworks repository.

list(resource, region, **kwargs)[source]

List subnetworks by region.

Parameters:
  • resource (str) – The project to query resources for.
  • region (str) – The region of the forwarding rules to query.
  • **kwargs (dict) – Additional args to pass through to the base method.
Returns:

An iterator over each page of results from the API.

Return type:

iterator

_api_not_enabled(error)[source]

Checks if the error is due to the API not being enabled for project.

Parameters:error (Exception) – The error to check.
Returns:
(bool, str) True, API not enabled reason if API is not enabled
or False, ‘’ if there is a different exception.
Return type:tuple
_debug_operation_response_time(project_id, operation)[source]

Log timing details for a running operation if debug logs enabled.

Parameters:
  • project_id (str) – The project id the operation was for.
  • operation (dict) – The last Operation resource returned from the API server.
_flatten_aggregated_list_results(project_id, paged_results, item_key, sort_key='name')[source]

Flatten results and handle exceptions.

Parameters:
  • project_id (str) – The project id the results are for.
  • paged_results (list) – A list of paged API response objects. [{page 1 results}, {page 2 results}, {page 3 results}, …]
  • item_key (str) – The name of the key within the inner “items” lists containing the objects of interest.
  • sort_key (str) – The name of the key to sort the results by before returning.
Returns:

A sorted list of items.

Return type:

list

Raises:
  • ApiNotEnabledError – Raised if the API is not enabled for the project.
  • ApiExecutionError – Raised if there is another error while calling the API method.
_flatten_list_results(project_id, paged_results, item_key)[source]

Flatten results and handle exceptions.

Parameters:
  • project_id (str) – The project id the results are for.
  • paged_results (list) – A list of paged API response objects. [{page 1 results}, {page 2 results}, {page 3 results}, …]
  • item_key (str) – The name of the key within the inner “items” lists containing the objects of interest.
Returns:

A list of items.

Return type:

list

Raises:
  • ApiNotEnabledError – Raised if the API is not enabled for the project.
  • ApiExecutionError – Raised if there is another error while calling the API method.