Google cloud platform GCP云功能用于挂起和恢复GCP实例

Google cloud platform GCP云功能用于挂起和恢复GCP实例,google-cloud-platform,google-cloud-functions,suspend,google-cloud-scheduler,Google Cloud Platform,Google Cloud Functions,Suspend,Google Cloud Scheduler,我们可以使用GCP云函数来启动和停止GCP实例,但我需要使用云函数和调度器处理GCP实例的计划挂起和恢复 从GCP文档中,我了解到我们可以开始和停止使用下面提供的云函数 我们是否有相同的节点JS或其他语言PCGK可用于挂起和恢复GCP实例 如果没有,我们可以创建自己的暂停/恢复 当我试了一个,我得到下面的错误 “TypeError:compute.zone(…).vm(…).resume不是函数 编辑,谢谢Chris和Guillaume,在浏览了你们的链接之后,我编辑了我的代码,下面是我的in

我们可以使用GCP云函数来启动和停止GCP实例,但我需要使用云函数和调度器处理GCP实例的计划挂起和恢复

从GCP文档中,我了解到我们可以开始和停止使用下面提供的云函数

我们是否有相同的节点JS或其他语言PCGK可用于挂起和恢复GCP实例

如果没有,我们可以创建自己的暂停/恢复

当我试了一个,我得到下面的错误 “TypeError:compute.zone(…).vm(…).resume不是函数

编辑,谢谢Chris和Guillaume,在浏览了你们的链接之后,我编辑了我的代码,下面是我的index.js文件。 因为某种原因,当我这样做的时候 gcloud函数部署ResumeInstancePubSubSubsub--触发主题resume实例--运行时节点JS10--允许未经身份验证

我总是很紧张 提供的模块中未定义函数“resumeInstancePubSub1”。 resumeInstancePubSub1 2020-09-04 10:57:00.333是否指定了要执行的正确目标函数

我以前没有在Node JS或JS上工作过,我期待类似于启动/停止文档的东西,我可以使用下面的git repo轻松地工作

我的index.js文件

// BEFORE RUNNING:
// ---------------
// 1. If not already done, enable the Compute Engine API
//    and check the quota for your project at
//    https://console.developers.google.com/apis/api/compute
// 2. This sample uses Application Default Credentials for authentication.
//    If not already done, install the gcloud CLI from
//    https://cloud.google.com/sdk and run
//    `gcloud beta auth application-default login`.
//    For more information, see
//    https://developers.google.com/identity/protocols/application-default-credentials
// 3. Install the Node.js client library by running
//    `npm install googleapis --save`

const {google} = require('googleapis');
var compute = google.compute('beta');

authorize(function(authClient) {
  var request = {
    // Project ID for this request.
    project: 'my-project',  // TODO: Update placeholder value.

    // The name of the zone for this request.
    zone: 'my-zone',  // TODO: Update placeholder value.

    // Name of the instance resource to resume.
    instance: 'my-instance',  // TODO: Update placeholder value.

    resource: {
      // TODO: Add desired properties to the request body.
    },

    auth: authClient,
  };
exports.resumeInstancePubSub = async (event, context, callback) => {
try {
    const payload = _validatePayload(
      JSON.parse(Buffer.from(event.data, 'base64').toString())
    );
    const options = {filter: `labels.${payload.label}`};
    const [vms] = await compute.getVMs(options);
    await Promise.all(
      vms.map(async (instance) => {
        if (payload.zone === instance.zone.id) {
          const [operation] = await compute
            .zone(payload.zone)
            .vm(instance.name)
            .resume();

          // Operation pending
          return operation.promise();
        }
      })
    );
          // Operation complete. Instance successfully started.
    const message = `Successfully started instance(s)`;
    console.log(message);
    callback(null, message);
  } catch (err) {
    console.log(err);
    callback(err);
  }
};
 compute.instances.resume(request, function(err, response) {
    if (err) {
      console.error(err);
      return;
    }

    // TODO: Change code below to process the `response` object:
    console.log(JSON.stringify(response, null, 2));
  });
});

function authorize(callback) {
  google.auth.getClient({
    scopes: ['https://www.googleapis.com/auth/cloud-platform']
  }).then(client => {
    callback(client);
  }).catch(err => {
    console.error('authentication failed: ', err);
  });
}
是api新beta verison的文档。您可以看到,您可以挂起一个实例,如:

compute.instances.suspend(request, function(err, response) {
    if (err) {
      console.error(err);
      return;
    }
您可以用类似的方式恢复实例:

 compute.instances.resume(request, function(err, response) {
    if (err) {
      console.error(err);
      return;
    }

您没有在正确的位置看到。这是新的Beta API:谢谢!我不知道该Beta。编辑我的回答谢谢guillaume和Chris,您的链接非常有用。请记住,这是Beta功能,尚未在客户端库中实现。但是,如果您使用discovery API,您应该能够使用这些。您可以分享您的信息吗当前代码?我们可以尝试修复它!@guillaumeblaquiere我已经在上面添加了我的index.js。谢谢..!上面的错误现在已经解决,一个函数无法访问,因为它在另一个模块中。