The command
gcloud interactive
allows you to use
gcloud
in interactive
Python mode. All
gcloud
commands and command groups are available in interactive
mode.
To start interactive mode, run the following command.
$ gcloud interactive Google Cloud SDK interactive Python mode. To use this mode in a Python script, add the following directory to your PYTHONPATH. /usr/local/google-cloud-sdk/lib Visit https://developers.google.com/cloud/sdk/interactive for more information. >>> from googlecloudsdk.gcloud.gcloud import gcloud >>>
gcloud interactive
will put you in a Python REPL, where you can write Python code
against a Python module that mimics the command line. For instance, to run the command
gcloud auth login
from interactive mode, execute the following Python code.
>>> gcloud.auth.login() Your browser has been opened to visit: https://accounts.google.com/o/oauth2/auth?<snip> Created new window in existing browser session. <oauth2client.client.OAuth2Credentials object at 0x7f7f1803ab50>
Basic interaction
The functions that correspond to the CLI invocations also return actual Python objects, rather than printing data to be scraped.
>>> gcloud.auth.list() auth_info(active_account='[email protected]', accounts=[u'[email protected]']) >>> result = gcloud.auth.list() >>> print result.active_account [email protected] >>> print result.accounts[0] [email protected]
As a result, it's much more straightforward to do
gcloud
scripting using the Python
mode, rather than using bash scripts and scraping the output. Not only is Python a more flexible
programming language than bash, but the objects returned by the CLI invocations are much more
stable. Specifically, a small change in the look-and-feel of a command's output will not affect
the object returned in Python mode.
Passing command-line arguments in Python mode
The parameters that can be passed to the Python versions of
gcloud
commands
correspond to the command-line arguments that are passed to the CLI. For instance, the command
gcloud config set
has the following help message.
$ gcloud config set -h Usage: gcloud config set [optional flags] PROPERTY VALUE Set the value for an option, so that Cloud SDK tools can use them as configuration. optional flags: --global-only Set the option in the global properties file. --help Display detailed help. -h Print a summary help and exit. positional arguments: PROPERTY The property to be set. VALUE The value to be set.
The function
gcloud.config.set
has two required parameters and one optional
parameter. Each one must be provided as a named parameter, eg
foo(x=y)
instead of
foo(y)
. Their order does not matter. The special options
-h
and
--help
are not available in Python mode.
To manipulate the config properties in Python mode, the following instructions can be run.
>>> gcloud.config.list() {'core': {'project': 'original_project', 'account': '[email protected]', 'disable_usage_reporting': 'false'}} >>> gcloud.config.set(property='project', value='new_project') >>> gcloud.config.list() {'core': {'project': 'new_project', 'account': '[email protected]', 'disable_usage_reporting': 'false'}}
Scripting against
gcloud
To write a Python script that has access to the
gcloud
Python object, add the
google-cloud-sdk/lib
directory to your
PYTHONPATH
add the
following import statement to your
.py
script.
from googlecloudsdk.gcloud.gcloud import gcloud
Then, your script can manipulate the
gcloud
object exactly like you can while in
the
gcloud interactive
Python shell.