java - 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 的错误(无效参数)。
解决方案
例如,在设置标签时,需要将标签指纹添加到 RequestBody,这对我有用。
推荐阅读
- mysql - RDS - 超过 1205 锁定等待超时尝试重新启动事务
- regex - 如何在 linux shell 中使用 sed 选择内容
- c - 我尝试创建以相反顺序显示其给定参数的代码,但为什么它的代码告诉我分段错误?
- sql - TSQL Outer JOIN:SELECT 附近的语法不正确
- mysql - 表DB自动填充
- geoip - Matomo 中的 GeoIP,之前的 Piwik 地图东京区
- objective-c - 如何在 LZip 的 NSTask 参数中传递源和目标路径?
- php - 如何在 php 中调用表 AVG()
- python - 监控文件夹中添加的 .jpg,使用 CUPS 自动打印,将相同的 .jpg 移动到另一个文件夹
- javascript - 为什么我的幻灯片在我的 React 应用程序中不起作用?