如何使用nodejs客户端库更新gcp实例的机器类型

发布于 2025-01-12 11:48:57 字数 1187 浏览 0 评论 0原文

我有一个处理 gcp 实例操作的节点服务器。我正在尝试更新现有正在运行的实例的机器类型。我不想更新任何其他属性,例如磁盘大小或任何内容。

    
    const computeClient = new Compute.InstancesClient({
      projectId: "project",
      keyFilename: "keyfile",
    });
        let resource = {
          instance: "testinstance",
          instanceResource: {
            machineType : "zones/us-central1-a/machineTypes/e2-standard-4",
            name: "testinstance"
          },
          project: "project",
          zone : "us-central1-a"
        }
        const resp1 = await computeClient.update(resource);

当我尝试运行上面的代码时,出现此错误

    Stacktrace:
    ==================== 
    Error: Invalid value for field 'resource.disks': ''. No disks are specified.
        at Function.parseHttpError (////node_modules/google-gax/build/src/googleError.js:49:37)
        at decodeResponse (///node_modules/google-gax/build/src/fallbackRest.js:72:49)
        at ////node_modules/google-gax/build/src/fallbackServiceStub.js:90:42
        at processTicksAndRejections (node:internal/process/task_queues:96:5)

    node version : v16.14.0 
    @google-cloud/compute version : 3.1.2

有解决方案吗?有更新机器类型的代码示例吗?

I have a node server which handles gcp instance operations. I am trying to update the machine type of a existing running instance. I don't want to update any other properties like disk size or anything.

    
    const computeClient = new Compute.InstancesClient({
      projectId: "project",
      keyFilename: "keyfile",
    });
        let resource = {
          instance: "testinstance",
          instanceResource: {
            machineType : "zones/us-central1-a/machineTypes/e2-standard-4",
            name: "testinstance"
          },
          project: "project",
          zone : "us-central1-a"
        }
        const resp1 = await computeClient.update(resource);

When I try to run above code this error occurs

    Stacktrace:
    ==================== 
    Error: Invalid value for field 'resource.disks': ''. No disks are specified.
        at Function.parseHttpError (////node_modules/google-gax/build/src/googleError.js:49:37)
        at decodeResponse (///node_modules/google-gax/build/src/fallbackRest.js:72:49)
        at ////node_modules/google-gax/build/src/fallbackServiceStub.js:90:42
        at processTicksAndRejections (node:internal/process/task_queues:96:5)

    node version : v16.14.0 
    @google-cloud/compute version : 3.1.2

Any solution ? Any code sample to update machine type ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌路终见情 2025-01-19 11:48:57

如果您只想更新实例的机器类型,您应该直接使用专门用于此目的的 setMachineType 方法。请参阅下面的示例:

// Imports the Compute library
const {InstancesClient} = require('@google-cloud/compute').v1;

// Instantiates a client
const computeClient = new InstancesClient();
const instance = "instance-name";
const instancesSetMachineTypeRequestResource = {machineType: "zones/us-central1-a/machineTypes/n1-standard-1"}
const project = "project-id";
const zone = "us-central1-a";

async function callSetMachineType() {
  // Construct request
  const request = {
    instance,
    instancesSetMachineTypeRequestResource,
    project,
    zone,
  };

  // Run request
  const response = await computeClient.setMachineType(request);
  console.log(response);
}

callSetMachineType();

请注意,机器类型只能在 TERMINATED 实例上更改,如文档此处。在更新机器类型之前,您需要首先确保实例已停止或在代码中停止它。有关可用方法的更多详细信息,请参见此处

If you only want to update your instance's machine type you should use the setMachineType method directly which is meant for this specifically. See example below:

// Imports the Compute library
const {InstancesClient} = require('@google-cloud/compute').v1;

// Instantiates a client
const computeClient = new InstancesClient();
const instance = "instance-name";
const instancesSetMachineTypeRequestResource = {machineType: "zones/us-central1-a/machineTypes/n1-standard-1"}
const project = "project-id";
const zone = "us-central1-a";

async function callSetMachineType() {
  // Construct request
  const request = {
    instance,
    instancesSetMachineTypeRequestResource,
    project,
    zone,
  };

  // Run request
  const response = await computeClient.setMachineType(request);
  console.log(response);
}

callSetMachineType();

Note that machine type can only be changed on a TERMINATED instance as documented here. You'll need to first ensure the instance is stopped or stop it in your code prior to updating machine type. More details on available methods here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文