首页 > 解决方案 > Google Compute Engine 实例设置标签

问题描述

我正在尝试在 Java 上以编程方式为 VM 实例设置标签。这是我实现它的方式。

private void setLabels(String key, String value) {
    Compute computeService = ComputeClientHelper.getClient();

    InstancesSetLabelsRequest requestBody = new InstancesSetLabelsRequest();
    requestBody.set(key, value);

    try {
        logger.info("Setting status to " + value);

        Compute.Instances.SetLabels request = computeService.instances().setLabels(
                PROJECT,
                ZONE,
                INSTANCE,
                requestBody
        );

        Operation response = request.execute();
    } catch (IOException ex) {
        logger.warn("Something went wrong, couldn't find instance.");
    } catch (NullPointerException ex) {
        // Thrown when ComputeInstance returns null
        logger.warn("Couldn't change status label, authentication required");
    }
}

但在实例日志中,我收到代码 3 的错误(无效参数)。

标签: javagoogle-cloud-platformgcloud

解决方案


例如,在设置标签时,需要将标签指纹添加到 RequestBody,这对我有用。


推荐阅读