這篇文章帶給大家的內容是關於怎麼直接透過 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() descbe_request = DescribeInstanceAutoRenewAttributeRequest() desbe_request().re_stail_stail_re_m)d quest=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。 = 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 進行續費查詢和續費管理的詳細內容。更多資訊請關注PHP中文網其他相關文章!