Skip to main content

Google Cloud Compute API client library

Project description

stable pypi versions

Compute Engine API: Create and runs virtual machines on Google Cloud Platform.

Quick Start

In order to use this library, you first need to go through the following steps:

  1. Select or create a Cloud Platform project.

  2. Enable billing for your project.

  3. Enable the Compute Engine API.

  4. Setup Authentication.

Installation

Install this library in a virtualenv using pip. virtualenv is a tool to create isolated Python environments. The basic problem it addresses is one of dependencies and versions, and indirectly permissions.

With virtualenv, it’s possible to install this library without needing system install permissions, and without clashing with the installed system dependencies.

Mac/Linux

pip install virtualenv
virtualenv <your-env>
source <your-env>/bin/activate
<your-env>/bin/pip install google-cloud-compute

Windows

pip install virtualenv
virtualenv <your-env>
<your-env>\Scripts\activate
<your-env>\Scripts\pip.exe install google-cloud-compute

Authentication and Authorization

This client library supports authentication via Google Application Default Credentials , or by providing a JSON key file for a Service Account. See examples below.

Google Application Default Credentials (ADC) is the recommended way to authorize and authenticate clients. Here is an example of a client using ADC to authenticate:

from google.cloud import compute_v1

networks_client = compute_v1.NetworksClient()
for network in networks_client.list(project='YOUR_PROJECT'):
    print(network)

You can use a file with credentials to authenticate and authorize, such as a JSON key file associated with a Google service account. You can create service account keys and download them using the Google Cloud Console. For more information, see Creating and managing Service Account keys.

The library used to create the credentials objects is google-auth. This example uses the Networks Client, but the same steps apply to the other clients in this package. Example:

from google.oauth2 import service_account
from google.cloud import compute_v1

credentials = service_account.Credentials.from_service_account_file(
    '/path/to/key.json')

networks_client = compute_v1.NetworksClient(credentials=credentials)
for network in networks_client.list(project='YOUR_PROJECT'):
    print(network)

When you don’t want to store secrets on disk, you can create credentials from in-memory JSON and use the from_service_account_info method. You can also limit the use of your credentials only to specified scopes. For more information about OAuth 2.0 scopes for Google APIs, see Scopes documentation page. Example:

import json

from google.oauth2 import service_account
from google.cloud import compute_v1

json_acct_info = json.loads(function_to_get_json_creds())
credentials = service_account.Credentials.from_service_account_info(
    json_acct_info)

scoped_credentials = credentials.with_scopes(
    ['https://www.googleapis.com/auth/cloud-platform'])

networks_client = compute_v1.NetworksClient(credentials=scoped_credentials)
for network in networks_client.list(project='YOUR_PROJECT'):
    print(network)

Long Running Operations

Long-Running Operations (LROs), like the many insert() operations, can be handled using the ExtendedOperation object that is returned when the LRO is started.

You can wait for the completion of an operation using its result() method. This method accepts a timeout argument, specifying how long you want your process to wait for completion of the operation (in seconds). When the call to result() times out, the operation is not automatically cancelled. At any time, you can check whether the operation is complete by using its done() method.

A sample method to handle LROs featuring error and warning reporting can be found in the Python code samples repository: GoogleCloudPlatform/python-docs-samples

Code Samples

You can find useful code samples that will demonstrate the usage of this library on the Google Cloud samples page.

PyCharm/JetBrains IDEs

This library has now grown in size past the JetBrains default size limit of ~2.5Mb. As a result, code completion in JetBrains products can fail to work with the classes from our library. To fix this, you need to update the idea.max.intellisense.filesize setting in custom properties (Help -> Edit custom properties…). Just add the line idea.max.intellisense.filesize = 10000 to change this limit to ~10Mb.

Next Steps

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

google-cloud-compute-1.11.0.tar.gz (2.8 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

google_cloud_compute-1.11.0-py2.py3-none-any.whl (2.2 MB view details)

Uploaded Python 2Python 3

File details

Details for the file google-cloud-compute-1.11.0.tar.gz.

File metadata

  • Download URL: google-cloud-compute-1.11.0.tar.gz
  • Upload date:
  • Size: 2.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for google-cloud-compute-1.11.0.tar.gz
Algorithm Hash digest
SHA256 d1d05a4b3ec6f830bbdcc779a740e963a80d324a4fd6ef41a7c248a07cf96489
MD5 a5abf4f6e038225fd974fddc64a7dbd0
BLAKE2b-256 6f243f83ebbc47fa472e983bf4d78a16c3ea900953783e46a07134f711a4b42d

See more details on using hashes here.

File details

Details for the file google_cloud_compute-1.11.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for google_cloud_compute-1.11.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 fafe7024d8992537cea8533ffc1e478375f1cbc8de39fa00e75b98730bc65ff9
MD5 3039a833d2e34dd3c262f943cde9c244
BLAKE2b-256 212292577034e5010471afc6eac2c4df333458a063f28cca811a9c9770b54051

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page