This topic describes how to use Alibaba Cloud SDK for Python to create and delete virtual private clouds (VPCs) and vSwitches.
Prerequisites
Before you use Alibaba Cloud SDK for Python, make sure that the following requirements are met:
- An Alibaba Cloud account and the AccessKey pair are obtained. You can create and view your AccessKey pair on the Security Management page of the Alibaba Cloud Management Console.
- Make sure that Alibaba Cloud SDK for Python is installed. For more information, see aliyun-python-sdk-vpc 3.0.25.
- The VPC Python example library is downloaded. Go to the directory where the setup.py file is stored, and run the following command to initialize the environment:
python setup.py install
Background information
The sample code in this topic includes the following operations:- Create a VPC in the China (Zhangjiakou) region.
- Create a vSwitch in the VPC.
- Delete the VPC.
- Delete the vSwitch.
Procedure
- Open the aliyun-openapi-python-sdk-examples\sdk_examples\examples\vpc folder in the downloaded SDK directory.
- Open the vpc_quick_start.py file in your text editor. Set the required parameters, save the configurations, and then exit the editor. The following sample code is displayed:
#encoding=utf-8 import sys import json import time from alibabacloud_credentials.client import Client as CredClient from aliyunsdkcore.acs_exception.exceptions import ServerException, ClientException from aliyunsdkvpc.request.v20160428 import CreateVpcRequest from aliyunsdkvpc.request.v20160428 import CreateVSwitchRequest from aliyunsdkvpc.request.v20160428 import DeleteVSwitchRequest from aliyunsdkvpc.request.v20160428 import DeleteVpcRequest from aliyunsdkvpc.request.v20160428 import DescribeVSwitchAttributesRequest from aliyunsdkvpc.request.v20160428 import DescribeVpcAttributeRequest from aliyunsdkcore.client import AcsClient class VpcQuickStart(object): def __init__(self, client): self.client = client self.TIME_DEFAULT_OUT = 15 self.DEFAULT_TIME = 1 def check_status(self,time_default_out, default_time, func, check_status, id): for i in range(time_default_out): time.sleep(default_time) status = func(id) if status == check_status: return True return False def create_vpc(self): try: request = CreateVpcRequest.CreateVpcRequest() response = self.client.do_action_with_exception(request) response_json = json.loads(response) # Check whether the VPC is in the Available state. if self.check_status(self.TIME_DEFAULT_OUT, self.DEFAULT_TIME, self.describe_vpc_status, "Available", response_json['VpcId']): return response_json except ServerException as e: print(e) except ClientException as e: print(e) def delete_vpc(self, params): try: request = DeleteVpcRequest.DeleteVpcRequest() # The ID of the VPC that you want to delete. request.set_VpcId(params['vpc_id']) response = self.client.do_action_with_exception(request) response_json = json.loads(response) return response_json except ServerException as e: print(e) except ClientException as e: print(e) def describe_vpc_attribute(self, vpc_id): try: request = DescribeVpcAttributeRequest.DescribeVpcAttributeRequest() # The ID of the VPC that you want to query. request.set_VpcId(vpc_id) response = self.client.do_action_with_exception(request) response_json = json.loads(response) return response_json except ServerException as e: print(e) except ClientException as e: print(e) def describe_vpc_status(self, vpc_id): response = self.describe_vpc_attribute(vpc_id) return response["Status"] def create_vswitch(self, params): try: request = CreateVSwitchRequest.CreateVSwitchRequest() # The ID of the zone to which the vSwitch belongs. You can call the DescribeZones operation to query zone IDs. request.set_ZoneId(params['zone_id']) # The ID of the VPC to which the vSwitch belongs. request.set_VpcId(params['vpc_id']) # The CIDR block of the vSwitch. request.set_CidrBlock(params['cidr_block']) response = self.client.do_action_with_exception(request) response_json = json.loads(response) # Check whether the vSwitch is in the Available state. if self.check_status(self.TIME_DEFAULT_OUT, self.DEFAULT_TIME, self.describe_vswitch_status, "Available", response_json['VSwitchId']): return response_json except ServerException as e: print(e) except ClientException as e: print(e) def describe_vswitch_attribute(self, vswitch_id): try: request = DescribeVSwitchAttributesRequest.DescribeVSwitchAttributesRequest() request.set_VSwitchId(vswitch_id) response = self.client.do_action_with_exception(request) response_json = json.loads(response) return response_json except ServerException as e: print(e) except ClientException as e: print(e) def describe_vswitch_status(self, vswitch_id): response = self.describe_vswitch_attribute(vswitch_id) return response["Status"] def delete_vswitch(self, params): try: request = DeleteVSwitchRequest.DeleteVSwitchRequest() # The ID of the vSwitch that you want to delete. request.set_VSwitchId(params['vswitch_id']) response = self.client.do_action_with_exception(request) response_json = json.loads(response) # Check whether the vSwitch is deleted. if self.check_status(self.TIME_DEFAULT_OUT, self.DEFAULT_TIME * 5, self.describe_vswitch_status, '', params['vswitch_id']): return response_json except ServerException as e: print(e) except ClientException as e: print(e) if __name__ == "__main__": # Security risks may arise if you use the AccessKey pair of an Alibaba Cloud account because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine O&M. # We do not recommend that you save the AccessKey ID and AccessKey secret in your project code. Otherwise, the AccessKey pair may be leaked and the security of your resources may be compromised. # In this example, the AccessKey pair is obtained by using the Alibaba Cloud Credentials tool to authenticate API access. For more information about how to configure the variables, see https://www.alibabacloud.com/help/alibaba-cloud-sdk-262060/latest/configure-credentials-378659. cred = CredClient() access_key_id = cred.get_access_key_id() access_key_secret = cred.get_access_key_secret() # Create an AcsClient instance. client = AcsClient(access_key_id, access_key_secret, '<your-region-id>') vpc_quick_start = VpcQuickStart(client) params = {} params['zone_id'] = "cn-zhangjiakou-b" params['cidr_block'] = "172.16.0.0/16" # Create a VPC. vpc_json = vpc_quick_start.create_vpc() print("---------------------------create_vpc---------------------------") print(vpc_json) # Create a vSwitch. params['vpc_id'] = vpc_json['VpcId'] vswitch_json = vpc_quick_start.create_vswitch(params) print("---------------------------create_vswitch---------------------------") print(vswitch_json) # Delete the vSwitch. params['vswitch_id'] = vswitch_json['VSwitchId'] vswitch_json = vpc_quick_start.delete_vswitch(params) print("---------------------------delete_vswitch---------------------------") print(vswitch_json) # Delete the VPC. vpc_json = vpc_quick_start.delete_vpc(params) print("---------------------------delete_vpc---------------------------") print(vpc_json)
- Go to the directory of the vpc_quick_start.py file and run the following command to create and delete a VPC and a vSwitch:
python vpc_quick_start.py
Result
The following output is returned:
---------------------------create_vpc---------------------------
{
"ResourceGroupId": "rg-acfmxazmq1h****",
"RouteTableId": "vtb-8vbf9ud7xrcn9mh1q****",
"VRouterId": "vrt-8vb1qjnxcm03nm1hq****",
"VpcId": "vpc-8vb67v4ozd8wf1mhq****",
"RequestId": "5052F988-75CC-46AD-A1A6-0E9E445BD0D5"
}
---------------------------create_vswitch---------------------------
{
"VSwitchId": "vsw-8vbqn2at0kljjmn3a****",
"RequestId": "0BA1ABF7-21CF-4460-9A86-0BB783886E58"
}
---------------------------delete_vswitch---------------------------
{
"RequestId": "D691F04B-A6EE-49A7-A434-4A45DD3AA0B8"
}
---------------------------delete_vpc---------------------------
{
"RequestId": "4570F816-AB8D-45EA-8913-6AE787C1632C"
}