我有一个要求重新启动我的计算机器,这是运行在谷歌云平台上使用云功能。
是否有任何使用云函数的方法,尽管计算有rest来访问它,但我不太确定它是否能在云函数上工作?
发布于 2018-01-15 15:04:47
是的,您可以使用云函数重置Google计算实例。
您可以按照此链接中提供的步骤创建云函数,index.js和package.json应该如下所示:
index.js
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
{
"name": "googleapis",
"version": "24.0.0",
"dependencies": {"googleapis":"^24.0.0"}
}在创建云函数时会发现的“”字段中,您需要输入正在使用的函数的名称,在这种情况下,需要“重置”
请注意,您需要在我提供的index.js代码中更改项目、区域和实例的值。
https://stackoverflow.com/questions/42992200
复制相似问题