我正在尝试为一台现有的机器添加启动脚本,当我从Google的测试器('Try this API')中添加启动脚本时,它可以工作,客户端似乎没有任何变化……下面是代码(只是请求的一个例子)+我得到的响应(看起来不错)和发送请求后机器的数据。
我的要求是:
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
from pprint import pprint
jsonPath = "myJSON.json"
credentials = GoogleCredentials.from_stream(jsonPath)
# Define gcp service
service = discovery.build('compute', 'v1', credentials=credentials)
#Request Body
bodyData = {"fingerprint": "***","items": [{"key": "startup-script","value": "#! /bin/bash\n\nservice start sshguard"}]}
#setMeta Data Request
instance = service.instances().setMetadata(project="<PROJECT>", zone="europe-west1-b", instance="<ID>", body=bodyData)
#Execute request
response = instance.execute()
# Get instacne details
instanceget = service.instances().get(project="<PROJECT>", zone="europe-west1-b", instance="<ID>").execute()
#Print response + New Metadata
pprint(response)
print("'New Metadata:", instanceget['metadata'])我得到的回应
{'id': '6099825023953066427',
'insertTime': '2020-03-16T04:47:32.880-07:00',
'kind': 'compute#operation',
'name': 'operation-84359252330-5a0f7626e861e-cf743913-4f05cd',
'operationType': 'setMetadata',
'progress': 0,
'selfLink': 'https://www.googleapis.com/compute/v1/projects/<Project>/zones/europe-west1-b/operations/operation-1584359252330-5a0f7626e861e-cf743913-4f05cd31',
'startTime': '2020-03-16T04:47:32.899-07:00',
'status': 'RUNNING',
'targetId': '<ID>',
'targetLink': 'https://www.googleapis.com/compute/v1/projects/<Project>/zones/europe-west1-b/instances/<ID>',
'user': 'pubsub-aws@<Project>.iam.gserviceaccount.com',
'zone': 'https://www.googleapis.com/compute/v1/projects/<Project>/zones/europe-west1-b'}
'New Metadata: {'fingerprint': '***', 'kind': 'compute#metadata'}如您所见,启动脚本从未添加到元数据中...我想可能是json格式的问题吧?也许请求正文需要编码或其他什么?将感谢您的帮助!提前谢谢。
发布于 2020-03-23 22:31:54
解决了。给它‘所有者’权限,它就能工作了。意味着我有错误的权限。谢谢大家!
发布于 2020-03-18 06:43:20
选项1
setMetadata调用可能失败。查看云控制台日志,查看操作结果。
选项2
setMetadata不会立即应用更改,它可能需要很短的时间。因为您的代码会立即获取实例,所以setMetadata更改可能会在您获取实例之后应用。
如果您的脚本需要检查更改是否已应用,它应该获取操作对象,直到它不再是RUNNING。
https://stackoverflow.com/questions/60707826
复制相似问题