1

Hi I am trying to use the Google Cloud Client Library for Python to convert a shell script using the gcloud CLI to launch a container to python. In searching through the documentation (https://googlecloudplatform.github.io/gcloud-python/), I have been unable to find functionality that would allow me to configure the gcloud project. For example, for the command-line gcloud config set project <project-name>, I have not come across equivalent functionality with the Python client. Is anyone aware of this functionality with the client library?

bossylobster
  • 9,993
  • 1
  • 42
  • 61
Tiffany
  • 11
  • 2

3 Answers3

1

I am not sure I understood your question but if you want to set Google CLI project property, you can use subprocess for this and verify with google.auth:

import subprocess
import google.auth


if __name__ == "__main__":
    project_id = 'my-project'
    subprocess.run(['gcloud', 'config', 'set', 'project', project_id, ])
    credentials, project_id = google.auth.default()
    print(project_id)
Harton
  • 106
  • 2
  • 9
0

Actually I didn't get your question right.

It is possible, let me just take the example of gcloud storage. When creating a Client instance you can provide a project name:

class gcloud.storage.client.Client(project=None, credentials=None, http=None)

http://googlecloudplatform.github.io/gcloud-python/stable/storage-client.html

DevLounge
  • 8,313
  • 3
  • 31
  • 44
0

Note that gcloud-python library and gcloud cli tool are not related. It is possible for gcloud-python library (or many other libraries) to use application default credentials set up gcloud cli tool, but any other gcloud settings are not exported.

If you wish to use those settings you can take advantage of --format gcloud-cli flag:

gcloud config list --format=json

which will return json object of all settings which you can parse and use in your script, for example pass project setting to gcloud-python library.

cherba
  • 8,681
  • 3
  • 27
  • 34