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

How to update machine type of gcp instnace using nodejs client library

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

    
    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

有什么解决办法吗?是否有更新机器类型的代码示例?

如果您只想更新实例的机器类型,您应该直接使用专门用于此目的的 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 实例上更改,如记录 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