2013-08-18 3 views
1

Я пытаюсь использовать NodeJS, чтобы перечислить все изображения, доступные для учетной записи Google Compute Engine.как перечислить изображения, хранящиеся в Google Engine с помощью Node?

Я стараюсь, чтобы получить тот же результат, что и этой командной строке в формате JSON с помощью Node:

gcutil listimages --project=google 
  1. Я начал с этого куска кода https://github.com/google/google-api-nodejs-client
  2. я смотрю в этот TODO»образца также: https://developers.google.com/datastore/docs/getstarted/start_nodejs/

но сейчас я застрял, и я не могу связать образцы с документацией (см. Ссылку в коде, поскольку я не могу вставить больше, чем 2 ссылки, если репутация < до 10 :-)

Вот код, который я начинаю. Не могли бы вы сказать мне, как

/* 
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); 

заранее спасибо за вашу помощь

ответ

0

Хотя изображения Google Compute Engine хранятся в Google Cloud Service, вы действительно хотите использовать API Compute Engine. Посмотрите на образец кода на 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>'); 
     }); 
    } 
0

Один у вас есть переменные настройки compute, вы можете использовать следующие API для запроса API:

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

где opts должен включать uri, method и форму data.

0

Вы можете использовать модуль stephenplusplus/gce-images НПМ:

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

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

См this для более подробной информации.