1

我正在尝试使用 NodeJS 来列出可用于 Google Compute Engine 帐户的所有图像。

我尝试使用 Node 以 JSON 格式获得与此命令行相同的结果:

gcutil listimages --project=google
  1. 我从这段代码开始https://github.com/google/google-api-nodejs-client
  2. 我也研究了这个 todo 示例:https ://developers.google.com/datastore/docs/getstarted/start_nodejs/

但是现在,我被卡住了,我无法真正将示例与文档链接起来(请参阅代码中的链接,因为如果信誉 < 到 10,我不能粘贴超过 2 个链接 :-)

这是我开始的代码。你能告诉我怎么做吗

/*
 Retrieves the list of image resources available to the specified project.

 command line: gcutil listimages --project=google
 REST API: https://developers.google.com/compute/docs/reference/latest/images/list
 */

"use strict";

var googleapis = require('googleapis');
var authclient = new googleapis.OAuth2Client();
var compute = new googleapis.auth.Compute();

var projectName = process.argv[2] || 'google';

var gceService;

var usage = 'usage: listimages.js <project name>';

compute.authorize(function (err, result) {
    console.assert(!err, err);
    googleapis.discover('compute', 'v1beta15')
        .withAuthClient(compute)
        .execute(function (err, client) {
            console.assert(!err, err);

            //here I want to instantiate a Google Compute Engine service
            //this does not work (obviously)
            gceService = client.instanciateGoogleComputeEngineService();

            console.log('an instance of a GCE service is made !');
        });
});

// the following line isn't part of the code, I just try to help you understand my needs :-)
console.log(gceService.listimages(projectName);

预先感谢您的帮助

4

3 回答 3

0

尽管 Google Compute Engine 图像存储在 Google Cloud Service 中,但您确实想使用 Compute Engine API。查看https://developers.google.com/compute/docs/api/javascript-guide上的示例代码

具体https://developers.google.com/apis-explorer/#p/compute/v1beta16/compute.images.list

当我发送这个GET命令

GET https://www.googleapis.com/compute/v1beta16/projects/rti-latency/global/images?key={YOUR_API_KEY}

我看到了结果

200 OK


{
 "kind": "compute#imageList",
 "selfLink": "https://content.googleapis.com/compute/v1beta16/projects/<project-id>/global/images",
 "id": "projects/<project-id>/global/images",
 "items": [
  {

   "kind": "compute#image",
   "selfLink": "https://content.googleapis.com/compute/v1beta16/projects/<project-id>/global/images/rti-5-debian-7-20140110",
   "id": "17737537448393276537",
   "creationTimestamp": "2014-01-09T14:25:56.614-08:00",
   "name": "rti-5-debian-7-20140110",
   "description": "",
   "sourceType": "RAW",
   "rawDisk": {
    "containerType": "TAR",
    "source": ""
   },
   "status": "READY",
   "archiveSizeBytes": "2278217814"
  }
 ]
}

你需要这样的东西

function listImages() {
       var request = gapi.client.compute.images.list({
         'project': DEFAULT_PROJECT,
         'zone': DEFAULT_ZONE
       });
       executeRequest(request, 'listImages');
     }

 function executeRequest(request, apiRequestName) {
       request.execute(function (resp) {
         newWindow = window.open(apiRequestName, '', 'width=600, height=600, scrollbars=yes');
         newWindow.document.write('<h1>' + apiRequestName + '</h1> <br />'
           + '<pre>' + JSON.stringify(resp.result, null, ' ') + '</pre>');
       });
     }
于 2014-01-15T05:32:02.060 回答
0

一个你有compute变量设置,你可以使用以下 api 来查询 API:

compute.request(opts, function(err, result) {
   callback(err, result);
});

whereopts应该包括uri,method和 form data

于 2014-04-12T15:06:23.213 回答
0

您可以使用stephenplusplus/gce-images NPM 模块:

var gceImages = require("gce-images");
var images = gceImages({ keyFile: "..." });

images.getAll(function (err, images) {
    // images contains a JSON with all the available images
});

有关更多详细信息,请参阅内容。

于 2015-11-10T12:49:01.073 回答