您好,登錄后才能下訂單哦!
這篇文章主要介紹了如何直接通過 API 進行續費查詢和續費管理,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。
ECS 實例續費
本文主要涉及如下關鍵功能:
按照過期時間查詢云服務器
續費實例
查詢云服務器自動續費時間
設置云服務器自動續費時間
對于包年包月的云服務器,生命周期非常重要。如果云服務器資源不能按時續費,將可能導致服務器被鎖定甚至被釋放,從而影響業務持續性。API 幫助您及時了解和檢查資源的到期時間,并完成續費充值功能。
本篇需關注如下 API:
查詢實例列表
續費實例
查詢指定范圍內到期的云服務器
查詢實例列表的 API,通過過濾參數,您可以查詢一定時間范圍內到期的實例信息。通過設置過濾參數 ExpiredStartTime 和 ExpiredEndTime(時間參數 按照 ISO8601 標準表示,并需要使用 UTC 時間。 格式為:yyyy-MM-ddTHH:mmZ。) ,可以方便地查詢該時間范圍內到期的實例列表。如果需要通過安全組進行過濾,只需加上安全組 ID 即可。
INSTANCE_EXPIRED_START_TIME_IN_UTC_STRING = '2017-01-22T00:00Z' INSTANCE_EXPIRE_END_TIME_IN_UTC_STRING = '2017-01-28T00:00Z' def describe_need_renew_instance(page_size=100, page_number=1, instance_id=None, check_need_renew=True, security_group_id=None): request = DescribeInstancesRequest() if check_need_renew is True: request.set_Filter3Key("ExpiredStartTime") request.set_Filter3Value(INSTANCE_EXPIRED_START_TIME_IN_UTC_STRING) request.set_Filter4Key("ExpiredEndTime") request.set_Filter4Value(INSTANCE_EXPIRE_END_TIME_IN_UTC_STRING) if instance_id is not None: request.set_InstanceIds(json.dumps([instance_id])) if security_group_id: request.set_SecurityGroupId(security_group_id) request.set_PageNumber(page_number) request.set_PageSize(page_size) return _send_request(request)
續費云服務器
續費實例只支持包年包月的服務器類型,不支持按量付費的服務器,同時要求用戶必須支持賬號的余額支付或信用支付。執行 API 的時候將執行同步的扣費和訂單生成。因此,執行 API 的時候必須保證您的賬號有足夠的資金支持自動扣費。
def _renew_instance_action(instance_id, period='1'): request = RenewInstanceRequest() request.set_Period(period) request.set_InstanceId(instance_id) response = _send_request(request) 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')
返回內容如下:
{"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"}
完整的代碼如下:
# coding=utf-8 # if the python sdk is not install using 'sudo pip install aliyun-python-sdk-ecs' # if the python sdk is install using 'sudo pip install --upgrade aliyun-python-sdk-ecs' # make sure the sdk version is 2.1.2, you can use command 'pip show aliyun-python-sdk-ecs' to check import json import logging from aliyunsdkcore import client from aliyunsdkecs.request.v20140526.DescribeInstanceAutoRenewAttributeRequest import \ DescribeInstanceAutoRenewAttributeRequest from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest from aliyunsdkecs.request.v20140526.ModifyInstanceAutoRenewAttributeRequest import \ ModifyInstanceAutoRenewAttributeRequest from aliyunsdkecs.request.v20140526.RenewInstanceRequest import RenewInstanceRequest logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%a, %d %b %Y %H:%M:%S') clt = client.AcsClient('Your Access Key Id', 'Your Access Key Secrect', 'cn-beijing') # data format in UTC, only support passed the value for minute, seconds is not support. INSTANCE_EXPIRED_START_TIME_IN_UTC_STRING = '2017-01-22T00:00Z' INSTANCE_EXPIRE_END_TIME_IN_UTC_STRING = '2017-01-28T00:00Z' def renew_job(page_size=100, page_number=1, check_need_renew=True, security_group_id=None): response = describe_need_renew_instance(page_size=page_size, page_number=page_number, check_need_renew=check_need_renew, security_group_id=security_group_id) response_list = response.get('Instances').get('Instance') logging.info("%s instances need to renew", str(response.get('TotalCount'))) if response_list > 0: instance_ids = '' for item in response_list: instance_id = item.get('InstanceId') instance_ids += instance_id + ',' renew_instance(instance_id=instance_id) logging.info("%s execute renew action ready", instance_ids) def describe_need_renew_instance(page_size=100, page_number=1, instance_id=None, check_need_renew=True, security_group_id=None): request = DescribeInstancesRequest() if check_need_renew is True: request.set_Filter3Key("ExpiredStartTime") request.set_Filter3Value(INSTANCE_EXPIRED_START_TIME_IN_UTC_STRING) request.set_Filter4Key("ExpiredEndTime") request.set_Filter4Value(INSTANCE_EXPIRE_END_TIME_IN_UTC_STRING) if instance_id is not None: request.set_InstanceIds(json.dumps([instance_id])) if security_group_id: request.set_SecurityGroupId(security_group_id) request.set_PageNumber(page_number) request.set_PageSize(page_size) return _send_request(request) # check the instances is renew or not def describe_instance_auto_renew_setting(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') + ',' if len(failed_instance_ids) > 0: logging.error("instance %s auto renew not match expect %s.", failed_instance_ids, expected_auto_renew) 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) describe_instance_auto_renew_setting(instance_ids, auto_renew) # if using the instance id can be found means the instance is not renew successfully. def check_instance_need_renew(instance_id): response = describe_need_renew_instance(instance_id=instance_id) if response is not None: return response.get('TotalCount') == 1 return False # 續費一個實例一個月 def renew_instance(instance_id, period='1'): need_renew = check_instance_need_renew(instance_id) if need_renew: _renew_instance_action(instance_id=instance_id, period=period) # describe_need_renew_instance(instance_id=instance_id, check_need_renew=False) def _renew_instance_action(instance_id, period='1'): request = RenewInstanceRequest() request.set_Period(period) request.set_InstanceId(instance_id) response = _send_request(request) logging.info('renew %s ready, output is %s ', instance_id, response) def _send_request(request): request.set_accept_format('json') try: response_str = clt.do_action(request) logging.info(response_str) response_detail = json.loads(response_str) return response_detail except Exception as e: logging.error(e) if __name__ == '__main__': logging.info("Renew ECS Instance by OpenApi!") # 查詢在指定的時間范圍內是否有需要續費的實例。 describe_need_renew_instance() # 續費一個實例, 直接執行扣費 renew_instance('i-1111') # 查詢實例自動續費的狀態 # describe_instance_auto_renew_setting('i-1111,i-2222') # 設置實例自動續費 # setting_instance_auto_renew('i-1111,i-2222')
感謝你能夠認真閱讀完這篇文章,希望小編分享如何直接通過 API 進行續費查詢和續費管理內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。