In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to directly through the API renewal query and renewal management, has a certain reference value, friends in need can refer to. I hope you will learn a lot after reading this article. Next, let the editor take you to learn about it.
Renewal of ECS instance
This paper mainly deals with the following key functions:
Query CVM by expiration time
Renew an instance
Query the automatic renewal time of CVM
Set the automatic renewal time of the CVM
For prepaid CVMs, the life cycle is very important. If the CVM resources cannot be renewed on time, it may cause the server to be locked or even released, thus affecting business continuity. API helps you to know and check the expiration time of resources in time, and complete the renewal and recharge function.
In this article, you should pay attention to the following API:
Query the list of instances
Renew an instance
Query the expired CVMs within the specified range
Query the API of the instance list. By filtering parameters, you can query the information of instances that expire within a certain time range. By setting the filter parameters ExpiredStartTime and ExpiredEndTime (the time parameters are expressed according to the ISO8601 standard, and UTC time is required. The format is yyyy-MM-ddTHH:mmZ. You can easily query the list of instances that expire within that time range If you need to filter through the security group, just add the security group 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)
Renew the CVM
Renewal instances only support prepaid servers, but do not support postpaid servers. Users are required to support balance payment or credit payment of the account. Simultaneous deductions and order generation will be performed when the API is executed. Therefore, when performing API, you must ensure that your account has sufficient funds to support automatic deduction.
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)
The fee will be deducted automatically for renewal of the instance. After the renewal, you can query the resource expiration time of the instance based on InstanceId. Because API is an asynchronous task, the expiration time of the query resource may need to be delayed by 10 seconds.
Enable automatic renewal of CVM
In order to reduce the maintenance cost of your resources when they expire, Aliyun has also launched an automatic renewal feature for prepaid ECS instances. The automatic renewal deduction date is 08:00:00 on the ninth day before the expiration of the server. If the automatic deduction fails the previous day, it will continue to be executed on the next day until the deduction is completed or the resource lock expires 9 days later. You only need to make sure that your account balance or credit line is sufficient.
Query auto renewal settings
You can query and set automatic renewal through OpenAPI. This API only supports prepaid instances. The execution of postpaid instances will result in an error. To query the automatic renewal status of instances, you can query up to 100 prepaid instances at a time, and multiple instances ID are connected with commas.
The input parameter of DescribeInstanceAutoRenewAttribut is instance ID.
InstanceId: a maximum of 100 prepaid instances can be queried. Multiple instances ID are connected by commas.
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 (' iMui 1111, mai Mui 2222')
The returned content is as follows:
{"InstanceRenewAttributes": {"InstanceRenewAttribute": [{"Duration": 0, "InstanceId": "false 1111", "AutoRenewEnabled": false}, {"Duration": 0, "InstanceId": "Imuri 2222", "AutoRenewEnabled": false}]}, "RequestId": "71FBB7A5-C793-4A0D-B17E-D6B426EA746A"}
If auto renewal is set, the returned property AutoRenewEnabled is true, otherwise false is returned.
Set and cancel automatic renewal of CVM
There are three input parameters for automatic renewal:
InstanceId: a maximum of 100 prepaid instances can be queried. Multiple instances ID are connected by commas.
Duration: 1, 2, 3, 6, 12 are supported (in months).
AutoRenew:true/false, true enable auto renewal, false cancel auto renewal.
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)
The Response returned for successful execution is as follows:
Python {"RequestId": "7DAC9984-AAB4-43EF-8FC7-7D74C57BE46D"}
After the renewal is successful, you can execute the query again. If the renewal is successful, you will return the renewal period and whether to enable automatic renewal.
Python {"InstanceRenewAttributes": {"InstanceRenewAttribute": [{"Duration": 1, "InstanceId": "AutoRenewEnabled 1111", "AutoRenewEnabled": true}, {"Duration": 1, "InstanceId": "Imura 2222", "AutoRenewEnabled": true}]}, "RequestId": "7F4D14B0-D0D2-48C7-B310-B1DF713D4331"}
The complete code is as follows:
# 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 checkimport jsonimport loggingfrom aliyunsdkcore import clientfrom aliyunsdkecs.request.v20140526.DescribeInstanceAutoRenewAttributeRequest import\ DescribeInstanceAutoRenewAttributeRequestfrom aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequestfrom aliyunsdkecs.request.v20140526.ModifyInstanceAutoRenewAttributeRequest import\ ModifyInstanceAutoRenewAttributeRequestfrom aliyunsdkecs.request.v20140526.RenewInstanceRequest import RenewInstanceRequestlogging.basicConfig (level=logging.INFO, format='% (asctime) s% (filename) s [line:% (lineno) d]% (levelname) s% (message) slots, datefmt='%a % d% b% Y% HVA% MVA% 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 notdef 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# renew an instance for one month 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! ") # query whether there are any instances that need to be renewed within the specified time range. Describe_need_renew_instance () # Renewal an instance, directly execute the deduction renew_instance ('iMur1111') # query the status of auto renewal of the instance # describe_instance_auto_renew_setting (' iMube 1111memiray2222') # set the instance auto renewal # setting_instance_auto_renew ('iMuth1111Lime2222') Thank you for reading this article carefully I hope the editor will share how to query and manage the renewal directly through API. At the same time, I also hope that you will support us, pay attention to the industry information channel, and find out if you encounter problems. Detailed solutions are waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.