google 云函数计算引擎 python api 未按预期关闭调用停止实例
google cloud function Compute engine python api not shutting down instance on call to stop as expected
我在 python 3.9 中使用 google 云函数
我的意图是从 http 触发器触发此云功能以停止计算引擎实例。我的云函数的代码如下(入口点stop_server)
# python 3.9 code for cloud function body
from googleapiclient import discovery
def stop_server (request):
"""
Responds to HTTP Get request stops server specified inget param instancename=<name>
Returns status
"""
service = discovery.build('compute', 'v1')
# Project ID for this request.
project = 'my-gcp-project'
# The name of the zone for this request.
zone = 'us-east1-b'
# determine instance to stop from http params
if request.args and 'instancename' in request.args:
instance = request.args.get('instancename')
status = service.instances().stop(project=project, zone=zone, instance=instance)
return f'instance: {instance} - stopped --> status = {status}'
else: # no instance specified
return f'specify instance in get param ?instancename="name"'
“requirements.txt”如下
# Function dependencies
# package>=version
google-api-core
google-api-python-client
这段代码在 gcp 中部署得很好,我可以生成一个“触发器 url”,如下所示
https://us-east1-myproject.cloudfunctions.net/stop-specified-vm?instancename=name_of_instance
当我 运行 代码通过触发器 URL - 代码执行正常并且 returns http 响应中的状态消息如下
instance: my-gcp-instance - stopped --> status =
但是指定的实例没有按预期停止
所以为了调试,我需要找到停止的文档 instances.stop api - 我在 运行 时间检查了“服务对象”的类型,方法是将其类型打印为关注
enter code here
service = discovery.build('compute', 'v1')
stype=(type(service))
print(f'type of service = {stype}')
在日志中显示服务对象的类型为“googleapiclient.discovery.Resource”
我想这是一个基础 class 对象,它根据您传递给构建的服务参数保存适当特定类型的对象。
所以在我的例子中,因为服务是计算引擎——当我调用时,它绑定到什么
服务 = discovery.build('compute', 'v1')
在哪里可以找到相关文档 api
还有其他调试技巧吗?
请参阅下面 John Hanley 的回复 - 正如他正确指出的那样,我错过了对通过调用 service.instances().stop
返回的(请求)对象调用 execute()
所以基本上 - 我需要了解这里使用的范例。 service.instances().stop returns 一个对象,它是请求的 restful 服务的客户端(在本例中为停止)。然后我必须调用“执行”来实际调用该服务:-)
再次在下面发布完整的工作代码
# python 3.9 code for cloud function body
from googleapiclient import discovery
def stop_server (request):
"""
Responds to HTTP Get request stops server specified inget param instancename=<name>
Returns status
"""
service = discovery.build('compute', 'v1')
# Project ID for this request.
project = 'my-gcp-project'
# The name of the zone for this request.
zone = 'us-east1-b'
# determine instance to stop from http params
if request.args and 'instancename' in request.args:
instance = request.args.get('instancename')
request= service.instances().stop(project=project, zone=zone, instance=instance)
response = request.execute() # returning full dictionary object for caller to examine
rstr=str(response)
return f'instance: {instance} - stop requested --> response = {rstr}'
else: # no instance specified
return f'specify instance in get param ?instancename="name"'
APIstop()returns一个操作资源而不是字符串。您必须轮询操作资源以获取停止操作的状态。这包括成功和失败。
注意:您的代码不会在设置请求后调用 execute()。
request = service.instances().stop(project=project, zone=zone, instance=instance)
response = request.execute()
由于您正在编写新代码,我建议您不要使用当前的 Python 库。
而是切换到仍在开发中的当前库。
我在 python 3.9 中使用 google 云函数 我的意图是从 http 触发器触发此云功能以停止计算引擎实例。我的云函数的代码如下(入口点stop_server)
# python 3.9 code for cloud function body
from googleapiclient import discovery
def stop_server (request):
"""
Responds to HTTP Get request stops server specified inget param instancename=<name>
Returns status
"""
service = discovery.build('compute', 'v1')
# Project ID for this request.
project = 'my-gcp-project'
# The name of the zone for this request.
zone = 'us-east1-b'
# determine instance to stop from http params
if request.args and 'instancename' in request.args:
instance = request.args.get('instancename')
status = service.instances().stop(project=project, zone=zone, instance=instance)
return f'instance: {instance} - stopped --> status = {status}'
else: # no instance specified
return f'specify instance in get param ?instancename="name"'
“requirements.txt”如下
# Function dependencies
# package>=version
google-api-core
google-api-python-client
这段代码在 gcp 中部署得很好,我可以生成一个“触发器 url”,如下所示
https://us-east1-myproject.cloudfunctions.net/stop-specified-vm?instancename=name_of_instance
当我 运行 代码通过触发器 URL - 代码执行正常并且 returns http 响应中的状态消息如下
instance: my-gcp-instance - stopped --> status =
但是指定的实例没有按预期停止
所以为了调试,我需要找到停止的文档 instances.stop api - 我在 运行 时间检查了“服务对象”的类型,方法是将其类型打印为关注
enter code here
service = discovery.build('compute', 'v1')
stype=(type(service))
print(f'type of service = {stype}')
在日志中显示服务对象的类型为“googleapiclient.discovery.Resource” 我想这是一个基础 class 对象,它根据您传递给构建的服务参数保存适当特定类型的对象。 所以在我的例子中,因为服务是计算引擎——当我调用时,它绑定到什么 服务 = discovery.build('compute', 'v1')
在哪里可以找到相关文档 api
还有其他调试技巧吗?
请参阅下面 John Hanley 的回复 - 正如他正确指出的那样,我错过了对通过调用 service.instances().stop
返回的(请求)对象调用 execute()所以基本上 - 我需要了解这里使用的范例。 service.instances().stop returns 一个对象,它是请求的 restful 服务的客户端(在本例中为停止)。然后我必须调用“执行”来实际调用该服务:-)
再次在下面发布完整的工作代码
# python 3.9 code for cloud function body
from googleapiclient import discovery
def stop_server (request):
"""
Responds to HTTP Get request stops server specified inget param instancename=<name>
Returns status
"""
service = discovery.build('compute', 'v1')
# Project ID for this request.
project = 'my-gcp-project'
# The name of the zone for this request.
zone = 'us-east1-b'
# determine instance to stop from http params
if request.args and 'instancename' in request.args:
instance = request.args.get('instancename')
request= service.instances().stop(project=project, zone=zone, instance=instance)
response = request.execute() # returning full dictionary object for caller to examine
rstr=str(response)
return f'instance: {instance} - stop requested --> response = {rstr}'
else: # no instance specified
return f'specify instance in get param ?instancename="name"'
APIstop()returns一个操作资源而不是字符串。您必须轮询操作资源以获取停止操作的状态。这包括成功和失败。
注意:您的代码不会在设置请求后调用 execute()。
request = service.instances().stop(project=project, zone=zone, instance=instance)
response = request.execute()
由于您正在编写新代码,我建议您不要使用当前的 Python 库。
而是切换到仍在开发中的当前库。