我正在尝试使用API和Python添加一个新标签。我有下面的代码,无论我如何修改它,我总是会出错
我在这里使用了指南: https://cloud.google.com/compute/docs/reference/rest/v1/instances/setLabels
instances_list=list_instances(compute, "terraform-313709", "europe-west3-a")
#Getting the current labelFingerprint
labelFingerprint_value=instances_list[0]['labelFingerprint']
#Setting the labels
instances_set_labels_request_body = {
"labels": {
"key": "value"
},
"LabelFingerprint": labelFingerprint_value
}
request = service.instances().setLabels(project="terraform-313709", zone="europe-west3-a", instance="terraform-instance", body=instances_set_labels_request_body)
print(request)
response = request.execute()
我得到的全部错误是:
googleapiclient.errors.HttpError: <HttpError 412 when requesting https://compute.googleapis.com/compute/v1/projects/terraform-313709/zones/europe-west3-a/instances/terraform-instance/setLabels?alt=json
returned "Labels fingerprint either invalid or resource labels have changed". Details: "[{'message': 'Labels fingerprint either invalid or resource labels have changed', 'domain': 'global', 'reason': 'conditionNotMet', 'location': 'If-Match', 'locationType': 'header'}]">
我做错了什么
多谢各位
正如错误消息中所述,指纹不匹配,因此无法更新标签,这正是为什么首先要使用指纹-以确保您正在更新您认为正在更新的内容。我同意John的观点,您正在更新的实例很可能与您从中获取指纹的实例不同。尝试打印实例列表[0],查看“名称”是否与“地形实例”匹配
我尝试了这个小代码(如果它们的名称以“gke”开头,那么它会更新所有的节点,使其具有“goog gke node”的标签),看起来一切都很好:
这将产生以下输出:
相关问题 更多 >
编程相关推荐