我们可以使用GCP云功能来启动和停止GCP实例,但是我需要使用云功能和调度程序来计划GCP实例的挂起和恢复。
从GCP文档中,我可以开始和停止使用下面提供的云功能 https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/functions/scheduleinstance
我们是否有相同的节点JS或其他语言的Pcgks可用于挂起和恢复GCP实例?
如果不能,我们可以创建自己的暂停/继续操作。
当我尝试一个时,出现以下错误 “ TypeError:compute.zone(...)。vm(...)。resume不是函数
编辑,谢谢Chris和Guillaume,通过您的链接后,我已经编辑了我的代码,下面是我的index.js文件。 由于某些原因,当我这样做 gcloud函数部署resumeInstancePubSub --trigger-topic resume-instance --runtime nodejs10 --allow-未经身份验证
我总是得到 函数“ resumeInstancePubSub1”未在提供的模块中定义。 resumeInstancePubSub1 2020-09-04 10:57:00.333您是否指定了要执行的正确目标函数?
我以前没有从事Node JS或JS的工作,我期待与启动/停止文档类似的东西,可以使用git repo轻松进行工作 https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git
我的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);
});
}