Java Google计算引擎实例设置标签
我试图在Java上以编程方式为VM实例设置标签。下面是我如何实现它的Java Google计算引擎实例设置标签,java,google-cloud-platform,gcloud,Java,Google Cloud Platform,Gcloud,我试图在Java上以编程方式为VM实例设置标签。下面是我如何实现它的 private void setLabels(String key, String value) { Compute computeService = ComputeClientHelper.getClient(); InstancesSetLabelsRequest requestBody = new InstancesSetLabelsRequest(); requestBody.set(key,
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错误(无效参数) 例如,在设置label时,需要将label fingerprint添加到RequestBody,这对我来说很有效。因为它返回的参数无效,所以最好能看到您遇到的错误消息。尽管如此,在不查看错误的情况下,我所能想到的最佳解决方案是使用以下命令对传递到setLabels的参数进行双重检查。希望能有帮助