我们可以使用GCP云函数来启动和停止GCP实例,但是我需要使用云函数和调度器来执行GCP实例的计划挂起和恢复。
从GCP文档中,我了解到我们可以开始和停止使用https://github.com/GoogleCloudPlatform/nodejs-docs-samples/tree/master/functions/scheduleinstance下面可用的云功能。
是否有相同的节点JS或其他语言Pcgks可用于挂起和恢复GCP实例?
如果不能,我们可以创建自己的挂起/恢复。
当我尝试一个错误时,我得到了"TypeError: compute.zone(.).vm(.).resume不是一个函数“
编辑,谢谢克里斯和纪尧姆,在浏览了你的链接后,我编辑了我的代码,下面是我的index.js文件。由于某种原因,当我执行gcloud函数部署resumeInstancePubSub触发器主题简历实例运行时nodejs10允许未经身份验证。
我总是在提供的模块中没有定义函数'resumeInstancePubSub1‘。resumeInstancePubSub1 2020-09-04 10:57:00.333您指定了要执行的正确目标函数吗?
我以前没有使用过Node或JS,我期待类似的东西来启动/停止文档,我可以很容易地使用下面的git 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);
});
}发布于 2020-09-03 11:46:54
发布于 2021-12-14 11:18:43
https://stackoverflow.com/questions/63722888
复制相似问题