我正在尝试获取给定容器引擎集群的实例组。我可以成功获取容器集群对象,它有一个名为instanceGroupUrls
的属性,它是一个字符串列表,其中每个字符串都是实例组的完整URL:
In [14]: gke_api = discovery.build('container', 'v1', credentials=credentials)
In [15]: cluster = gke_api.projects().zones().clusters().get(
projectId=project_id, zone=zone, clusterId=cluster_id).execute()
In [16]: cluster['instanceGroupUrls']
Out[16]: ['https://www.googleapis.com/compute/v1/projects/{project-id}/zones/{zone-id}/instanceGroupManagers/{instanceGroupID}']
我的问题是:如果我只拥有资源的完整URL,我如何获得实例组?现在,我可以解析该URL,然后使用api发现的事情instanceGroups.get(project='foo', instanceGroup='bar')
,但这看起来非常hacky并且容易破损。
API客户端是否有办法提供完整的URL?我在IPython中做了很多谷歌搜索和探索,但找不到多少。我得到的最接近的是HttpRequest
类(http://google.github.io/google-api-python-client/docs/epy/googleapiclient.http.HttpRequest-class.html),但我不知道该为args传递什么。
任何帮助表示赞赏, 亚伦
答案 0 :(得分:0)
TL; DR - container
API的发现与compute
API的发现无关,他们都无法使用URL了解资源的类型。
httplib2.Http
对象向URL发送请求。compute
API的发现灵活地访问资源,就像您提到的那样,您必须从网址中提取zone
和instanceGroupName
字段并传递它到instanceGroups.get()
。顺便说一下,这并不是很棘手,因为大多数Google Cloud API都不太可能改变URL的资源格式。 instance group API doc is here。如果您希望只查看实例组资源并在不进行任何修改的情况下访问其属性,则可以使用httplib2
中经过身份验证的Http对象来获取与该URL对应的资源。
from googleapiclient import discovery
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
import json
import pprint
# BEGIN CONFIG
PRIVATE_KEY_JSON = '/path/to/service_account_credentials.json'
API_SCOPES = ['https://www.googleapis.com/auth/cloud-platform']
PROJECT_NAME = 'some-arbitrary-project'
ZONE_NAME = 'us-central1-a'
# END CONFIG
# Create a ServiceAccountCredentials object by reading the credentials from
# your JSON file. If you're using a different mechanism to load credentials
# like Application Default Credentials, substitute building the
# credentials object accordingly. I've added steps in the very
# end for using Application Default Credentials.
credentials = ServiceAccountCredentials.from_json_keyfile_name(
PRIVATE_KEY_JSON, scopes=API_SCOPES)
# Create an authorized http object which can be used for making the
# API requests. We will use this later.
http_auth = credentials.authorize(Http())
# Build the Cloud Container Engine API object which you will be using for
# invoking the corresponding APIs using the authorized Http object
# you created earlier
gke_api = discovery.build('container', 'v1', credentials=credentials)
# List all clusters for the specified project and zone
cluster_list_response = gke_api.projects().zones().clusters().list(
projectId=PROJECT_NAME, zone=ZONE_NAME).execute()
# Pick a cluster of interest
cluster = cluster_list_response['clusters'][0]
# Pick the instance group of interest
instance_group_url = cluster['instanceGroupUrls'][0]
print 'Instance Group URL: ' + instance_group_url
# Use the authorized Http object to make the request to fetch
# the instance group resource info.
response, content = http_auth.request(instance_group_url)
# Convert the json body into a dict for easy access.
instance_group = json.loads(content)
# Display the instance group using pprint.
print 'Instance Group Info: '
pprint.PrettyPrinter(indent=2).pprint(instance_group)
Instance Group URL: https://www.googleapis.com/compute/v1/projects/some-arbitrary-project/zones/us-central1-a/instanceGroupManagers/gke-cluster-foo-some-group
Instance Group Info:
{ u'baseInstanceName': u'gke-cluster-foo-default-pool-abcdef',
u'creationTimestamp': u'2017-07-07T10:11:12.678-07:00',
u'currentActions': { u'abandoning': 0,
u'creating': 0,
u'creatingWithoutRetries': 0,
u'deleting': 0,
u'none': 3,
u'recreating': 0,
u'refreshing': 0,
u'restarting': 0},
u'fingerprint': u'AABBCCDDEFG=',
u'id': u'12345678901234',
u'instanceGroup': u'https://www.googleapis.com/compute/v1/projects/some-arbitrary-project/zones/us-central1-a/instanceGroups/gke-cluster-foo-default-pool-some-group',
u'instanceTemplate': u'https://www.googleapis.com/compute/v1/projects/some-arbitrary-project/global/instanceTemplates/gke-cluster-foo-default-pool-abcdef',
u'kind': u'compute#instanceGroupManager',
u'name': u'gke-cluster-foo-default-pool-some-group',
u'selfLink': u'https://www.googleapis.com/compute/v1/projects/some-arbitrary-project/zones/us-central1-a/instanceGroupManagers/gke-cluster-foo-default-pool-some-grp',
u'targetSize': 3,
u'zone': u'https://www.googleapis.com/compute/v1/projects/some-arbitrary-project/zones/us-central1-a'}
如果您使用应用程序默认凭据,则需要对上面的示例进行以下修改,而不是服务帐户,您需要在其中构建凭证对象,其余代码保持不变:
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
以下是代码段(您可能需要与我上面发布的代码段结合使用),以便从URL中提取实例组名称和区域名称组件。无需提取项目名称,因为它与您拥有Google容器引擎群集的项目相同。输出与上面相同。
import urlparse
# Extract the path component of the URL, and using that extract instance group
# name and zone name.
instance_group_url_components = urlparse.urlsplit(
instance_group_url).path.split('/')
instance_group_name = instance_group_url_components[-1]
zone_name = instance_group_url_components[-3]
compute_api = discovery.build('compute', 'v1', credentials=credentials)
instance_group_info = compute_api.instanceGroups().get(
project=PROJECT_NAME, zone=zone_name,
instanceGroup=instance_group_name).execute()
print 'Instance Group Info: '
pprint.PrettyPrinter(indent=2).pprint(instance_group)
Instance Group URL: https://www.googleapis.com/compute/v1/projects/some-arbitrary-project/zones/us-central1-a/instanceGroupManagers/gke-cluster-foo-some-group
Instance Group Info:
{ u'baseInstanceName': u'gke-cluster-foo-default-pool-abcdef',
u'creationTimestamp': u'2017-07-07T10:11:12.678-07:00',
u'currentActions': { u'abandoning': 0,
u'creating': 0,
u'creatingWithoutRetries': 0,
u'deleting': 0,
u'none': 3,
u'recreating': 0,
u'refreshing': 0,
u'restarting': 0},
u'fingerprint': u'AABBCCDDEFG=',
u'id': u'12345678901234',
u'instanceGroup': u'https://www.googleapis.com/compute/v1/projects/some-arbitrary-project/zones/us-central1-a/instanceGroups/gke-cluster-foo-default-pool-some-group',
u'instanceTemplate': u'https://www.googleapis.com/compute/v1/projects/some-arbitrary-project/global/instanceTemplates/gke-cluster-foo-default-pool-abcdef',
u'kind': u'compute#instanceGroupManager',
u'name': u'gke-cluster-foo-default-pool-some-group',
u'selfLink': u'https://www.googleapis.com/compute/v1/projects/some-arbitrary-project/zones/us-central1-a/instanceGroupManagers/gke-cluster-foo-default-pool-some-grp',
u'targetSize': 3,
u'zone': u'https://www.googleapis.com/compute/v1/projects/some-arbitrary-project/zones/us-central1-a'}