首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用云函数访问虚拟机

使用云函数访问虚拟机
EN

Stack Overflow用户
提问于 2017-03-24 05:42:12
回答 1查看 741关注 0票数 2

我有一个要求重新启动我的计算机器,这是运行在谷歌云平台上使用云功能。

是否有任何使用云函数的方法,尽管计算有rest来访问它,但我不太确定它是否能在云函数上工作?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-15 15:04:47

是的,您可以使用云函数重置Google计算实例。

您可以按照此链接中提供的步骤创建云函数,index.js和package.json应该如下所示:

index.js

代码语言:javascript
复制
exports.resetting = function resetting() {
        var google = require('googleapis');
        var compute = google.compute('beta');

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

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

        // Name of the instance scoping this request.
        instance: 'YOUR-INSTANCE-NAME',  // TODO: Update placeholder value.

        auth: authClient,
      };

      compute.instances.reset(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.getApplicationDefault(function(err, authClient) {
        if (err) {
          console.error('authentication failed: ', err);
          return;
        }
        if (authClient.createScopedRequired && authClient.createScopedRequired()) {
          var scopes = ['https://www.googleapis.com/auth/cloud-platform'];
          authClient = authClient.createScoped(scopes);
        }
        callback(authClient);
      });
    };
}       

package.json

代码语言:javascript
复制
{
  "name": "googleapis",
  "version": "24.0.0",
  "dependencies": {"googleapis":"^24.0.0"}
}

在创建云函数时会发现的“”字段中,您需要输入正在使用的函数的名称,在这种情况下,需要“重置”

请注意,您需要在我提供的index.js代码中更改项目、区域和实例的值。

https://cloud.google.com/functions/docs/quickstart-console

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42992200

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档