Reads:37765Replies:0
Use OpenApi to manage your cloud server ECS elastically
Besides the ECS console on which you can manage your cloud server ECS on Alibaba Cloud and create resources, OpenApi is also provided for you to implement resource management and customized development. With the help of OpenApi, you can manage and deploy your cloud server more flexibly.
OpenApi is packed in the SDK, so it can be integrated into your existing system. In this document, we take the development of Python as an example to show you how to manage your cloud server using OpenApi. You can master the development of cloud services following the instructions in this document, even if you may have no Python development experience. For the development and management in other languages, you can contact us by leaving a message. Install ECS Python SDK First you should ensure that the Python Runtime is ready. The version of Python used in the document is 2.7+. pip install aliyun-python-sdk-ecs If you don't have permissions, use sudo and continue. sudo pip install aliyun-python-sdk-ecs The version of the SDK is 2.1.2. If your SDK version is older, please upgrade it. Hello Aliyun ECS First you should create a file hello_ecs_api.py. In order to use the SDK, you should instantiate the AcsClient object, and input your Accesskey and Accesskey Secret here. Your Access Key ID and Access Key Secret are the secret keys for access to Alibaba Cloud API. The full permissions of your account can be accessed with the keys, so you should keep them private. from aliyunsdkcore import client from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest from aliyunsdkecs.request.v20140526.DescribeRegionsRequest import DescribeRegionsRequest clt = client.AcsClient('Your Access Key Id', 'Your Access Key Secrect', 'cn-beijing') You can develop the first application after instantiation. Check the list of regions supported by your account. def hello_aliyun_regions(): request = DescribeRegionsRequest() response = _send_request(request) region_list = response.get('Regions').get('Region') assert response is not None assert region_list is not None result = map(_print_region_id, region_list) logging.info("region list: %s", result) def _print_region_id(item): region_id = item.get("RegionId") return region_id 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) hello_aliyun_regions() Input python hello_ecs_api.py in the command line, and you will get the list of regions supported. The output is as follows [u'cn-shenzhen', u'ap-southeast-1', u'cn-qingdao', u'cn-beijing', u'cn-shanghai', u'us-east-1', u'cn-hongkong', u'me-east-1', u'ap-southeast-2', u'cn-hangzhou', u'eu-central-1', u'ap-northeast-1', u'us-west-1'] Query the list of ECS instances under the current region Querying the instance list is very similar to that of region list, and all you need to do is to replace the parameter using DescribeInstancesRequest. def list_instances(): request = DescribeInstancesRequest() response = _send_request(request) if response is not None: instance_list = response.get('Instances').get('Instance') result = map(_print_instance_id, instance_list) logging.info("current region include instance %s", result) def _print_instance_id(item): instance_id = item.get('InstanceId'); return instance_id The output result is as follows current region include instance [u'i-****', u'i-****''] Next step All the 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 check import json import logging from aliyunsdkcore import client from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest from aliyunsdkecs.request.v20140526.DescribeRegionsRequest import DescribeRegionsRequest # configuration the log output formatter, if you want to save the output to file, # append ",filename='ecs_invoke.log'" after datefmt. 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') # sample api to list aliyun open api. def hello_aliyun_regions(): request = DescribeRegionsRequest() response = _send_request(request) if response is not None: region_list = response.get('Regions').get('Region') assert response is not None assert region_list is not None result = map(_print_region_id, region_list) logging.info("region list: %s", result) # output the instance owned in current region. def list_instances(): request = DescribeInstancesRequest() response = _send_request(request) if response is not None: instance_list = response.get('Instances').get('Instance') result = map(_print_instance_id, instance_list) logging.info("current region include instance %s", result) def _print_instance_id(item): instance_id = item.get('InstanceId'); return instance_id def _print_region_id(item): region_id = item.get("RegionId") return region_id # send open api request 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("Hello Aliyun OpenApi!") hello_aliyun_regions() list_instances() |
|