google-compute-engine - 如何使用 GCE Node.js 客户端从实例模板创建新的 GCE VM 实例?

标签 google-compute-engine google-api-nodejs-client

Google compute engine我可以使用 instance template从模板创建新 VM。使用 GCE 控制台可以正常工作,使用 API 也可以正常工作,也是(URL 参数“sourceInstanceTemplate”)。

如何使用 googleapis/nodejs-compute 从实例模板创建新的 GCE-VM (Node.js GCE SDK)?

最佳答案

google-auth-library-nodejs可用于访问 GCE instances.insert API直接。

以下示例改编自https://github.com/google/google-auth-library-nodejs如果在 GCE 中执行(特别是在 Google Cloud Function 中),则工作正常。

const zone = 'some-zone';
const name = 'a-name';
const sourceInstanceTemplate = `some-template-name`;
createVM(zone, name, sourceInstanceTemplate)
  .then(console.log)
  .catch(console.error);

async function createVM(zone, vmName, templateName) {
  const {auth} = require('google-auth-library');
  const client = await auth.getClient({
    scopes: 'https://www.googleapis.com/auth/cloud-platform'
  });
  const projectId = await auth.getDefaultProjectId();

  const sourceInstanceTemplate = `projects/${projectId}/global/instanceTemplates/${templateName}`;
  const url = `https://www.googleapis.com/compute/v1/projects/${projectId}/zones/${zone}/instances?sourceInstanceTemplate=${sourceInstanceTemplate}`;

  return await client.request({
    url: url,
    method: 'post',
    data: {name: vmName}
  });
}

关于google-compute-engine - 如何使用 GCE Node.js 客户端从实例模板创建新的 GCE VM 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51480750/

相关文章:

python - 如何避免 gcloud compute 警报将 key 存储在缓存中

ssh - 在 Google Compute Engine 上访问 FTP

node.js - 使用 Google Drive API

node.js - 使用 node.js 直接从服务器使用 youtube/google API 上传视频?

node.js - 通过管道将数据传输至字符串 Google Node API

google-compute-engine - GCE HTTPS 负载均衡器 session 关联性

django - 如何在 Google Compute Engine 上托管的 Django 应用程序中使用 Google Cloud Storage?

google-api - node js 是否使用 googleapis 或 google-cloud 库

google-cloud-platform - Google kubernetes 引擎节点池问题

node.js - 如何撤销 Google OAuth 提供的 token ? NodeJS