Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

API manages or customizes how to develop ECS

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about API management or customization how to develop ECS, many people may not know much about it. In order to make you understand better, the editor summed up the following for you. I hope you can get something according to this article.

Flexible management of ECS instances

Get the AK key of the RAM sub-account

To manage ECS instances with API, you need API keys (AccessKey ID and AccessKey Secret) that have access to ECS resources. To ensure the security of the cloud service, you need to create a RAM user who can access ECS resources, obtain the user's AccessKey key, and use this RAM user and API to manage the ECS instance.

Here are the steps to obtain the AccessKey key of the RAM user:

Create a RAM user and get the AccessKey key.

Directly authorize RAM users, and grant RAM users permission to manage CVM services (ECS).

Install ECS Python SDK

First of all, make sure that you already have the Runtime for Python, and the version of Python used in this article is 2.7 +.

Pip install aliyun-python-sdk-ecs

If prompted that you do not have permissions, switch sudo to continue.

Sudo pip install aliyun-python-sdk-ecs

The version of SDK used in this article is 2.1.2.

Hello Alibaba Cloud

Create the file hello_ecs_api.py. To use SDK, first instantiate the AcsClient object, where you need the AccessKey ID and AccessKey Secret of the RAM user.

AccessKey ID and AccessKey Secret are keys for RAM users to access Aliyun ECS service API. They have full permissions on this account. Please keep them safe.

From aliyunsdkcore import clientfrom aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequestfrom aliyunsdkecs.request.v20140526.DescribeRegionsRequest import DescribeRegionsRequestclt = client.AcsClient ('Your Access Key Id',' Your Access Key Secrect', 'cn-beijing')

The first application can be developed after the instantiation is completed. Query the list of regions supported by the current account. For more information, please see query the list of available regions.

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_iddef _ 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 ()

Running python hello_ecs_api.py on the command line will get a list of currently supported Region. A similar output is as follows:

Ubiquitous cnkoshenzhenfang, upright apcopyright southeastMurray 1century, upright cncopyright qingdaoqi, upright cnMube beijingling, upright cnkoshanghaifang, upright eastfuge, upright cnMube hongkongfang, upright MeimeMueastMui 1, upright apatheastMutual 2, utiliscnhangzhoufang, upright CPULING, upright CPUTHAYLING, ubiquitous APFESTUSTUSE 1']

Query the list of ECS instances under the current Region

Querying the instance list is very similar to querying the Region list. You can replace the input parameter object with DescribeInstancesRequest. For more query parameters, refer to the query instance list.

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 is as follows:

Current region include instance [ubiquitous murmur / mustard, ubiquitous / mustard]

For more API reference ECS API overview, you can try to make a list of query disks and replace the parameters of the instance with DescribeDisksRequest.

Complete code example

An example of the complete code for the above operation is shown below.

# 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.DescribeInstancesRequest import DescribeInstancesRequestfrom 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) slots, datefmt='%a,% d% b% Y% HV% MRV% 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_iddef _ print_region_id (item): region_id = item.get ("RegionId") return region_id# send open api requestdef _ 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 () after reading the above, do you have any further understanding of how API management or customization develops ECS? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report