Python googleapiclient API to get default project and zone

382 views
Skip to first unread message

Nathan Sowatskey

unread,
May 20, 2017, 10:31:33 AM5/20/17
to gce-discussion
Hi

Is there an Python API that will return the equivalent of these CLI commands please?

gcloud config get-value core/project


gcloud config get-value compute/zone


Many thanks


Nathan

Carlos (Cloud Platform Support)

unread,
May 23, 2017, 4:43:22 PM5/23/17
to gce-discussion
Hi Nathan,

It does seems that gcloud settings are stored locally and therefore access through the python library is limited.

The configuration files that gcloud uses are stored in $HOME/.config/gcloud/configurations


Nathan Sowatskey

unread,
May 25, 2017, 9:43:09 AM5/25/17
to Carlos (Cloud Platform Support), gce-discussion
Carlos

Many thanks for following up.

In some guides for the Python SDK it is stated that the SDK will use the local settings for the project. It makes sense that it should do so, but I can't see how.

As matters stand now. It seems that one has to create environment variables with the zone and project, and then pass those values as command line arguments to Python script.

Since the zone and project are already configured in the local SDK properties, creating environment variables, that are decoupled from the local settings, is quite likely to introduce errors. For example, one changes one's default project, but one forgets to change the environment variable.

Since this is so obviously cumbersome and fragile, it must be wrong.

I expect to hear from one of your colleagues about the environment variables bug that I raised for templates. I shall discuss this with them also.

Regards

Nathan
> --
> © 2017 Google Inc. 1600 Amphitheatre Parkway, Mountain View, CA 94043
>
> Email preferences: You received this email because you signed up for the Google Compute Engine Discussion Google Group (gce-dis...@googlegroups.com) to participate in discussions with other members of the Google Compute Engine community and the Google Compute Engine Team.
> ---
> You received this message because you are subscribed to a topic in the Google Groups "gce-discussion" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/gce-discussion/B9nW41gqSWM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to gce-discussio...@googlegroups.com.
> To post to this group, send email to gce-dis...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/gce-discussion/2534b9c8-ea31-4853-b46b-174b9e4f38d4%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Nathan Sowatskey

unread,
May 25, 2017, 9:46:06 AM5/25/17
to gce-discussion
Carlos

Many thanks for following up.

As an example of what can be done, see the deployment manager template below.

In the generate_config.py file I am using "context.env['project’]”. 

That means, in the context of the template mechanism, that the project (but not the zone) is available from the environment.

Note that the zone may be added as noted in this bug:

https://issuetracker.google.com/issues/38464927

So, this is an example of the kind of thing that is required, but in a general sense, not just in this specific case of templates.

Regards

Nathan

Content of dev.yaml is below:

imports:
- path: generate_config.py

resources:
- name: application
 type: generate_config.py
 properties:
   zone: europe-west1-d

Content of generate_config.py below.

#!/usr/bin/env python

COMPUTE_URL_BASE = 'https://www.googleapis.com/compute/v1/'

def generate_config(context):

 resources = [{
     'name': context.env['name'],
     'type': 'compute.v1.instance',
     'properties': {
         'zone': context.properties['zone'],
         'machineType': ''.join([COMPUTE_URL_BASE, 
                                 'projects/', 
                                 context.env['project'],
                                 '/zones/',
                                 context.properties['zone'],
                                 '/machineTypes/n1-standard-1']),
         'disks': [{
             'deviceName': 'boot',
             'type': 'PERSISTENT',
             'boot': True,
             'autoDelete': True,
             'initializeParams': {
                 'sourceImage': ''.join([COMPUTE_URL_BASE, 
                                         'projects/',
                                         context.env['project'],
                                         '/global/images/jre-10gb-debian-jessie'])
             }
         }],
         'networkInterfaces': [{
             'network': ''.join([COMPUTE_URL_BASE, 
                                 'projects/',
                                 context.env['project'],
                                 '/global/networks/default']),
             'accessConfigs': [{
                 'name': 'External NAT',
                 'type': 'ONE_TO_ONE_NAT'
             }]
         }]
     }
 }]
 return {'resources': resources}


Carlos (Cloud Platform Support)

unread,
May 25, 2017, 12:07:36 PM5/25/17
to gce-dis...@googlegroups.com
Hi Nathan,

I can see your point. Apparently the pre-set environment variables that can be used are limited. My colleague has reached the backend team on this tracker and additional feedback will be provided. Apparently there are some other ways to parse the ¨zone¨ variable to the deployment. 

Nathan Sowatskey

unread,
May 25, 2017, 1:49:54 PM5/25/17
to Carlos (Cloud Platform Support), gce-discussion
Thanks Carlos.

Just to be clear, what is needed with both Python scripts in general, and with Python deployment manager templates, is a way to access the zone and project variables defined in the profiles in the SDK install.

Having to use different mechanisms with scripts and templates, to get the same variable values, is just asking for trouble :-)

Regards

Nathan

> On 25 May 2017, at 18:07, 'Carlos (Cloud Platform Support)' via gce-discussion <gce-dis...@googlegroups.com> wrote:
>
> Hi Nathan,
>
> I can see your point. Apparently the pre-set of environment variables that can be used is limited. My colleague has reached the backend team on this tracker and additional feedback will be provided. Apparently there are some other ways to parse the ¨zone¨ variable to the deployment.
>
> --
> © 2017 Google Inc. 1600 Amphitheatre Parkway, Mountain View, CA 94043
>
> Email preferences: You received this email because you signed up for the Google Compute Engine Discussion Google Group (gce-dis...@googlegroups.com) to participate in discussions with other members of the Google Compute Engine community and the Google Compute Engine Team.
> ---
> You received this message because you are subscribed to a topic in the Google Groups "gce-discussion" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/gce-discussion/B9nW41gqSWM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to gce-discussio...@googlegroups.com.
> To post to this group, send email to gce-dis...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/gce-discussion/66e74c89-fed3-44c5-b067-3fcb143fd8fc%40googlegroups.com.

Nathan Sowatskey

unread,
May 27, 2017, 4:31:32 AM5/27/17
to gce-discussion, cbo...@google.com
Carlos

I received an update via this bug:


For the sake of completeness and safety, one would have to do this:

export CLOUDSDK_COMPUTE_ZONE=`gcloud info |tr -d '[]' | awk '/zone:/ {print $2}'`
export GCP_PROJ_ID=`gcloud info |tr -d '[]' | awk '/project:/ {print $2}'`
gcloud deployment-manager deployments create <name> --config <template>.yaml --properties=zone=$CLOUDSDK_COMPUTE_ZONE,project=$GCP_PROJ_ID

Using this combination of techniques, the loop is closed such that the values defined in the SDK install properties are used to create the environment variables that are passed in as arguments to the scripts.

This does work, but one is left with the feeling that it is an odd way of going about things.

Regards

Nathan

Nathan Sowatskey

unread,
Jun 4, 2017, 11:39:04 AM6/4/17
to gce-discussion, cbo...@google.com
One can also do this:

gcloud info --format='value(config.project)'
gcloud info --format='value(config.properties.compute.zone)’
> --
> © 2017 Google Inc. 1600 Amphitheatre Parkway, Mountain View, CA 94043
>
> Email preferences: You received this email because you signed up for the Google Compute Engine Discussion Google Group (gce-dis...@googlegroups.com) to participate in discussions with other members of the Google Compute Engine community and the Google Compute Engine Team.
> ---
> You received this message because you are subscribed to a topic in the Google Groups "gce-discussion" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/gce-discussion/B9nW41gqSWM/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to gce-discussio...@googlegroups.com.
> To post to this group, send email to gce-dis...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/gce-discussion/7680dcc9-7898-4043-96a2-eef726be2083%40googlegroups.com.

Mykhail Martsyniuk

unread,
Jan 13, 2021, 11:39:08 AM1/13/21
to gce-discussion
Hi Carlos,
Apparently solution described here: https://issuetracker.google.com/issues/38464927 is no longer works, as it throws an error:
The properties flag should only be used when using a template (--template) or composite type (--composite-type) as your config file.

Is there any other way of passing additional --properties along with --config conf.yml file without converting it to another level jinja template (as described in this thread)?

четверг, 25 мая 2017 г. в 19:07:36 UTC+3, Carlos (Cloud Platform Support):

Lucas (Cloud Platform Support)

unread,
Jan 13, 2021, 2:58:34 PM1/13/21
to gce-discussion
Hello,

As Google Groups is not a place for troubleshooting, you can also file a report on StackOverFlow for further range of support.

This seems to be working as intended, you would need to file a feature request [1] regarding this issue so that the internal team can take a look at it.

Regards

[1] https://cloud.google.com/support/docs/issue-trackers

Reply all
Reply to author
Forward
0 new messages