【问题标题】:Google Compute Engine Instance setting labelsGoogle Compute Engine 实例设置标签
【发布时间】:2023-11-23 14:15:02
【问题描述】:

我正在尝试在 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 的错误(无效参数)。

【问题讨论】:

  • 由于它返回无效参数,很高兴看到您遇到的错误消息。尽管如此,在不查看错误的情况下我能想到的最佳解决方案是使用以下documentation 仔细检查传递给 setLabels 的参数。希望对你有帮助

标签: java google-cloud-platform gcloud


【解决方案1】:

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

【讨论】:

    最近更新 更多