我想创建一个托管的Compute Engine组,其中包含一些启动脚本,其中包括一个创建对象更改通知的订阅的脚本。在these tutorials之后,这些订阅都需要一个“ subscription_name”。
我无法将其设置为静态名称,因为当启动1个以上的VM时,它将导致冲突。
是否可以自动“递增”名称,即“ VM_sub_1”,“ VM_sub_2”,“ VM_sub_3” ...等?
还是将其保留为空白并接受随机生成的名称是避免冲突的唯一方法?
用于创建订阅的代码示例:
def create_push_subscription(project_id, topic_name, subscription_name, endpoint):
"""Create a new push subscription on the given topic."""
# [START pubsub_create_push_subscription]
from google.cloud
import pubsub_v1
project_id = "bucketcfpubsub"
topic_name = "bucketcfpubsub"
subscription_name = "VM_sub_1"
endpoint = "https://bucketcfpubsub.appspot.com/push"
subscriber = pubsub_v1.SubscriberClient()
topic_path = subscriber.topic_path(project_id, topic_name)
subscription_path = subscriber.subscription_path(project_id, subscription_name)
push_config = pubsub_v1.types.PushConfig(push_endpoint = endpoint)
subscription = subscriber.create_subscription(subscription_path, topic_path, push_config)
print('Push subscription created: {}'.format(subscription))
print('Endpoint for subscription is: {}'.format(endpoint))#[END pubsub_create_push_subscription]
尝试获取元数据:
def check_instance():
import time
import requests
METADATA_URL = 'http://metadata.google.internal/computeMetadata/v1/instance/id'
METADATA_HEADERS = {'Metadata-Flavor': 'Google'}
instance_id = requests.get(METADATA_URL, headers=METADATA_HEADERS)
print(instance_id)
check_instance()
返回
响应[200]
意味
成功!值已更改,或者您达到了指定的timeout_sec,请求成功返回。