我应该使用哪个python库在我的私人PC上控制或启动或删除Google云平台上的实例?
答案 0 :(得分:0)
您可能还想签出libcloud。
答案 1 :(得分:0)
我认为最简单的方法是使用Google Compute Engine Python API客户端库。 You can see a sample with examples here.
中看到有关实例的功能的完整列表。您可以看到,您可以这样做:
import googleapiclient.discovery
compute = googleapiclient.discovery.build('compute', 'v1')
listInstance = compute.instances().list(project=project, zone=zone).execute()
stopInstance = compute.instances().stop(project=project, zone=zone, instance=instance_id).execute()
startInstance = compute.instances().start(project=project, zone=zone, instance=instance_id).execute()
deleteInstance = compute.instances().delete(project=project, zone=zone, instance=instance_id).execute()
请勿将参数名称“实例”与路径参数“ resourceId”的所选名称混淆。您可以在页面示例的右侧或底部看到带有真实参数名称的示例。
如果您更喜欢使用POST / PUT方法,还可以在Python中直接调用REST API(see example)。
您可能还想使用OAuth。如您在所提供的链接的示例中所看到的,它类似于:
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
service = discovery.build('compute', 'v1', credentials=credentials)
# Project ID for this request.
project = 'my-project' # TODO: Update placeholder value.
# The name of the zone for this request.
zone = 'my-zone' # TODO: Update placeholder value.
# Name of the instance resource to start.
instance = 'my-instance' # TODO: Update placeholder value.
request = service.instances().start(project=project, zone=zone, instance=instance)
response = request.execute()