2013-08-17 40 views
0

运行Google Compute Engine运行Google Compute Engine时,在查询图像和内核时,出现意外结果,与gcutil不同。值得注意的是我没有得到任何结果。Google Compute Engine Python API和Web API不返回图像或内核

随着gcutil我得到预期的结果:

 
$ gcutil --project MYPROJECT listimages 

+----------------------------------------------------------------+----------------------------------------------------------------+----------------------------------------------+-------------+--------+ 
|        name        |       description       |     kernel     | deprecation | status | 
+----------------------------------------------------------------+----------------------------------------------------------------+----------------------------------------------+-------------+--------+ 
| projects/centos-cloud/global/images/centos-6-v20130731   | SCSI-enabled CentOS 6; Created Wed, 31 Jul 2013 00:00:00 +0000 | projects/google/global/kernels/gce-v20130603 |    | READY | 
| projects/debian-cloud/global/images/debian-6-squeeze-v20130723 | Debian GNU/Linux 6.0.7 (squeeze) built on 2013-07-23   | projects/google/global/kernels/gce-v20130603 |    | READY | 
| projects/debian-cloud/global/images/debian-7-wheezy-v20130723 | Debian GNU/Linux 7.1 (wheezy) built on 2013-07-23    | projects/google/global/kernels/gce-v20130603 |    | READY | 
+----------------------------------------------------------------+----------------------------------------------------------------+----------------------------------------------+-------------+--------+ 

但是,当我使用web API或像这样的东西了Python API(以下简称,但是从examples基本上直):

def _service(): 
    flow = flow_from_clientsecrets(CLIENT_SECRETS, scope=GCE_SCOPE) 
    storage = Storage(OAUTH2_STORAGE) 
    credentials = storage.get() 
    if credentials is None or credentials.invalid: 
     credentials = run(flow, storage) 
    http = httplib2.Http() 
    auth_http = credentials.authorize(http) 
    gce_service = build('compute', API_VERSION) 
    return gce_service, credentials, auth_http 

if __name__ == "__main__": 
    gce_service, credentials, auth_http = _service() 
    request = gce_service.images().list(project="MYPROJECT", filter=None) 
    response = request.execute(http=auth_http) 
    pprint(response) 

Web和Python API都返回类似这样的内容:

 
{ 
"kind": "compute#imageList", 
"selfLink": "https://www.googleapis.com/compute/v1beta15/projects/MYPROJECT/global/images", 
"id": "projects/MYPROJECt/global/images" 
} 

即缺少itemsone expects in the response

我在这里可能会做错什么?

回答

1

谷歌(和公共)提供的图像和内核存储在不同的项目。这个想法是,它们本身并不是,而是由google(或debian或centos)发布。

您可以使用gcutil listkernels --project=google列出内核。与代码等价的是将MYPROJECT替换为google

对于图像,可以使用用于CentOS镜像的centos-cloud和用于debian镜像的debian-cloud

gcutil自动集成到常用项目中以便于使用。

+0

非常明智,非常感谢。希望我在文档中注意到这一点。你知道在哪里可以找到这个和类似的信息,乔? –

1

谷歌示例应用程序https://developers.google.com/compute/docs/api/python_guide#listinginstances正确列出了图像。 不确定代码是如何工作的。您可以使用一个参数定义函数_service(opts),然后在第13行中使用无参数调用它。gce_service,credentials,auth_http = _service()

请提供工作代码。

+0

谢谢尤里。我通过删除'_service'的'opts'参数来修复代码。您不妨注意,列举实例在这里不是问题;将'gce_service.images'更改为'gce_service.instances'或'gce_service.zones'将产生预期的结果 - 一个实例列表。这个问题与内核和图像有关。 –

+0

Brian,对此抱歉。所以,我在Google的示例中更改了2行 –

+0

我已经在Google示例中更改了2行,并获取了_images_的列表。我在你的代码中用我的实际项目ID替换了表达式project =“MYPROJECT”。运行它我也获得了图像列表。所以看起来问题不在API或Python库中。 –

相关问题