开发者社区> 问答> 正文

ECS 实例续费


除了通过 ECS控制台售卖页 进行云服务器续费外,阿里云还支持直接通过 API 进行续费查询和续费管理。
本文主要涉及如下关键功能:

  • 按照过期时间查询云服务器
  • 续费实例
  • 查询云服务器自动续费时间
  • 设置云服务器自动续费时间

对于包年包月的云服务器,生命周期非常重要。如果云服务器资源不能按时续费,将可能导致服务器被锁定甚至被释放,从而影响业务持续性。API 帮助您及时了解和检查资源的到期时间,并完成续费充值功能。
本篇需关注如下 API:

查询指定范围内到期的云服务器


查询实例列表的 API,通过过滤参数,您可以查询一定时间范围内到期的实例信息。通过设置过滤参数 ExpiredStartTime 和 ExpiredEndTime(时间参数 按照 ISO8601 标准表示,并需要使用 UTC 时间。 格式为:yyyy-MM-ddTHH:mmZ。) ,可以方便地查询该时间范围内到期的实例列表。如果需要通过安全组进行过滤,只需加上安全组 ID 即可。
  1. INSTANCE_EXPIRED_START_TIME_IN_UTC_STRING = '2017-01-22T00:00Z'
  2. INSTANCE_EXPIRE_END_TIME_IN_UTC_STRING = '2017-01-28T00:00Z'
  3. def describe_need_renew_instance(page_size=100, page_number=1, instance_id=None,
  4.                                  check_need_renew=True, security_group_id=None):
  5.     request = DescribeInstancesRequest()
  6.     if check_need_renew is True:
  7.         request.set_Filter3Key("ExpiredStartTime")
  8.         request.set_Filter3Value(INSTANCE_EXPIRED_START_TIME_IN_UTC_STRING)
  9.         request.set_Filter4Key("ExpiredEndTime")
  10.         request.set_Filter4Value(INSTANCE_EXPIRE_END_TIME_IN_UTC_STRING)
  11.     if instance_id is not None:
  12.         request.set_InstanceIds(json.dumps([instance_id]))
  13.     if security_group_id:
  14.         request.set_SecurityGroupId(security_group_id)
  15.     request.set_PageNumber(page_number)
  16.     request.set_PageSize(page_size)
  17.     return _send_request(request)



续费云服务器



续费实例只支持包年包月的服务器类型,不支持按量付费的服务器,同时要求用户必须支持账号的余额支付或信用支付。执行 API 的时候将执行同步的扣费和订单生成。因此,执行 API 的时候必须保证您的账号有足够的资金支持自动扣费。
  1. def _renew_instance_action(instance_id, period='1'):
  2.     request = RenewInstanceRequest()
  3.     request.set_Period(period)
  4.     request.set_InstanceId(instance_id)
  5.     response = _send_request(request)
  6.     logging.info('renew %s ready, output is %s ', instance_id, response)

续费实例将会自动完成扣费。在完成续费后,您可以根据InstanceId查询实例的资源到期时间。由于 API 为异步任务,查询资源到期时间可能需要延迟 10 秒才会变化。


开启云服务器自动续费



为了减少您的资源到期维护成本,针对包年包月的 ECS 实例,阿里云还推出了 自动续费功能。 自动续费扣款日为服务器到期前第 9 天的 08:00:00。如果前一日执行自动扣费失败,将会继续下一日定时执行,直到完成扣费或者 9 天后到期资源锁定。您只需要保证自己的账号余额或者信用额度充足即可。

查询自动续费设置


您可以通过 OpenAPI 来查询和设置自动续费。该 API 仅支持包年包月的实例,按量付费的实例执行将会报错。查询实例的自动续费状态支持一次最多查询 100 个包年包月的实例,多个实例 ID 以逗号连接。
DescribeInstanceAutoRenewAttribut 的入参为实例 ID.
  • InstanceId:支持最多查询 100 个包年包月的实例,多个实例 ID 以逗号连接。
python # check the instances is renew or not def describe_auto_renew(instance_ids, expected_auto_renew=True): describe_request = DescribeInstanceAutoRenewAttributeRequest() describe_request.set_InstanceId(instance_ids) response_detail = _send_request(request=describe_request) failed_instance_ids = '' if response_detail is not None: attributes = response_detail.get('InstanceRenewAttributes').get('InstanceRenewAttribute') if attributes: for item in attributes: auto_renew_status = item.get('AutoRenewEnabled') if auto_renew_status != expected_auto_renew: failed_instance_ids += item.get('InstanceId') + ',' describe_auto_renew('i-1111,i-2222')
返回内容如下:
  1. {"InstanceRenewAttributes":{"InstanceRenewAttribute":[{"Duration":0,"InstanceId":"i-1111","AutoRenewEnabled":false},{"Duration":0,"InstanceId":"i-2222","AutoRenewEnabled":false}]},"RequestId":"71FBB7A5-C793-4A0D-B17E-D6B426EA746A"}


如果设置自动续费,则返回的属性AutoRenewEnabled为 true,否则返回 false。


设置和取消云服务器的自动续费


设置自动续费有三个入参:
  • InstanceId: 支持最多查询100个包年包月的实例,多个实例 ID 以逗号连接。
  • Duration:支持 1、2、3、6、12,单位为月。
  • AutoRenew:true/false, true为开启自动续费,false为取消自动续费。
python def setting_instance_auto_renew(instance_ids, auto_renew = True): logging.info('execute enable auto renew ' + instance_ids) request = ModifyInstanceAutoRenewAttributeRequest(); request.set_Duration(1); request.set_AutoRenew(auto_renew); request.set_InstanceId(instance_ids) _send_request(request)
执行成功返回 Response 如下:python {"RequestId":"7DAC9984-AAB4-43EF-8FC7-7D74C57BE46D"}
续费成功后,您可以再执行一次查询。如果续费成功将返回续费时长以及是否开启自动续费。python {"InstanceRenewAttributes":{"InstanceRenewAttribute":[{"Duration":1,"InstanceId":"i-1111","AutoRenewEnabled":true},{"Duration":1,"InstanceId":"i-2222","AutoRenewEnabled":true}]},"RequestId":"7F4D14B0-D0D2-48C7-B310-B1DF713D4331"}

完整的代码如下:

  1. #  coding=utf-8
  2. # if the python sdk is not install using 'sudo pip install aliyun-python-sdk-ecs'
  3. # if the python sdk is install using 'sudo pip install --upgrade aliyun-python-sdk-ecs'
  4. # make sure the sdk version is 2.1.2, you can use command 'pip show aliyun-python-sdk-ecs' to check
  5. import json
  6. import logging
  7. from aliyunsdkcore import client
  8. from aliyunsdkecs.request.v20140526.DescribeInstanceAutoRenewAttributeRequest import \
  9.     DescribeInstanceAutoRenewAttributeRequest
  10. from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest
  11. from aliyunsdkecs.request.v20140526.ModifyInstanceAutoRenewAttributeRequest import \
  12.     ModifyInstanceAutoRenewAttributeRequest
  13. from aliyunsdkecs.request.v20140526.RenewInstanceRequest import RenewInstanceRequest
  14. logging.basicConfig(level=logging.INFO,
  15.                     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
  16.                     datefmt='%a, %d %b %Y %H:%M:%S')
  17. clt = client.AcsClient('Your Access Key Id', 'Your Access Key Secrect', 'cn-beijing')
  18. # data format in UTC, only support passed the value for minute, seconds is not support.
  19. INSTANCE_EXPIRED_START_TIME_IN_UTC_STRING = '2017-01-22T00:00Z'
  20. INSTANCE_EXPIRE_END_TIME_IN_UTC_STRING = '2017-01-28T00:00Z'
  21. def renew_job(page_size=100, page_number=1, check_need_renew=True, security_group_id=None):
  22.     response = describe_need_renew_instance(page_size=page_size, page_number=page_number,
  23.                                             check_need_renew=check_need_renew,
  24.                                             security_group_id=security_group_id)
  25.     response_list = response.get('Instances').get('Instance')
  26.     logging.info("%s instances need to renew", str(response.get('TotalCount')))
  27.     if response_list > 0:
  28.         instance_ids = ''
  29.         for item in response_list:
  30.             instance_id = item.get('InstanceId')
  31.             instance_ids += instance_id + ','
  32.             renew_instance(instance_id=instance_id)
  33.         logging.info("%s execute renew action ready", instance_ids)
  34. def describe_need_renew_instance(page_size=100, page_number=1, instance_id=None,
  35.                                  check_need_renew=True, security_group_id=None):
  36.     request = DescribeInstancesRequest()
  37.     if check_need_renew is True:
  38.         request.set_Filter3Key("ExpiredStartTime")
  39.         request.set_Filter3Value(INSTANCE_EXPIRED_START_TIME_IN_UTC_STRING)
  40.         request.set_Filter4Key("ExpiredEndTime")
  41.         request.set_Filter4Value(INSTANCE_EXPIRE_END_TIME_IN_UTC_STRING)
  42.     if instance_id is not None:
  43.         request.set_InstanceIds(json.dumps([instance_id]))
  44.     if security_group_id:
  45.         request.set_SecurityGroupId(security_group_id)
  46.     request.set_PageNumber(page_number)
  47.     request.set_PageSize(page_size)
  48.     return _send_request(request)
  49. # check the instances is renew or not
  50. def describe_instance_auto_renew_setting(instance_ids, expected_auto_renew=True):
  51.     describe_request = DescribeInstanceAutoRenewAttributeRequest()
  52.     describe_request.set_InstanceId(instance_ids)
  53.     response_detail = _send_request(request=describe_request)
  54.     failed_instance_ids = ''
  55.     if response_detail is not None:
  56.         attributes = response_detail.get('InstanceRenewAttributes').get('InstanceRenewAttribute')
  57.         if attributes:
  58.             for item in attributes:
  59.                 auto_renew_status = item.get('AutoRenewEnabled')
  60.                 if auto_renew_status != expected_auto_renew:
  61.                     failed_instance_ids += item.get('InstanceId') + ','
  62.     if len(failed_instance_ids) > 0:
  63.         logging.error("instance %s auto renew not match expect %s.", failed_instance_ids,
  64.                       expected_auto_renew)
  65. def setting_instance_auto_renew(instance_ids, auto_renew=True):
  66.     logging.info('execute enable auto renew ' + instance_ids)
  67.     request = ModifyInstanceAutoRenewAttributeRequest();
  68.     request.set_Duration(1);
  69.     request.set_AutoRenew(auto_renew);
  70.     request.set_InstanceId(instance_ids)
  71.     _send_request(request)
  72.     describe_instance_auto_renew_setting(instance_ids, auto_renew)
  73. # if using the instance id can be found means the instance is not renew successfully.
  74. def check_instance_need_renew(instance_id):
  75.     response = describe_need_renew_instance(instance_id=instance_id)
  76.     if response is not None:
  77.         return response.get('TotalCount') == 1
  78.     return False
  79. # 续费一个实例一个月
  80. def renew_instance(instance_id, period='1'):
  81.     need_renew = check_instance_need_renew(instance_id)
  82.     if need_renew:
  83.         _renew_instance_action(instance_id=instance_id, period=period)
  84.         # describe_need_renew_instance(instance_id=instance_id, check_need_renew=False)
  85. def _renew_instance_action(instance_id, period='1'):
  86.     request = RenewInstanceRequest()
  87.     request.set_Period(period)
  88.     request.set_InstanceId(instance_id)
  89.     response = _send_request(request)
  90.     logging.info('renew %s ready, output is %s ', instance_id, response)
  91. def _send_request(request):
  92.     request.set_accept_format('json')
  93.     try:
  94.         response_str = clt.do_action(request)
  95.         logging.info(response_str)
  96.         response_detail = json.loads(response_str)
  97.         return response_detail
  98.     except Exception as e:
  99.         logging.error(e)
  100. if __name__ == '__main__':
  101.     logging.info("Renew ECS Instance by OpenApi!")
  102.     # 查询在指定的时间范围内是否有需要续费的实例。
  103.     describe_need_renew_instance()
  104.     # 续费一个实例, 直接执行扣费
  105.     renew_instance('i-1111')
  106.     # 查询实例自动续费的状态
  107.     # describe_instance_auto_renew_setting('i-1111,i-2222')
  108.     # 设置实例自动续费
  109.     # setting_instance_auto_renew('i-1111,i-2222')

如您想了解 ECS 中 API 的其它操作,请参考 ECS中的API操作

展开
收起
chenchuan 2018-08-31 22:13:18 1064 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
如何运维千台以上游戏云服务器 立即下载
网站/服务器取证 实践与挑战 立即下载
ECS快储存加密技术 立即下载