本文操作示例介绍如何使用Alibaba Cloud SDK for Python创建和删除专有网络VPC与交换机VSwitch。

前提条件

在使用Alibaba Cloud SDK for Python前,您需要完成以下准备工作:
  • 使用Alibaba Cloud SDK for Python,您需要一个阿里云账号和访问密钥(AccessKey)。 请在阿里云控制台中的AccessKey管理页面上创建和查看您的AccessKey。
  • 确保您已经安装了Alibaba Cloud SDK for Python,请参见aliyun-python-sdk-vpc 3.0.8
  • 下载阿里云专有网络Python SDK场景示例的VPC Python Example库
    进入 setup.py所在的目录,执行如下命令,完成环境初始化配置。
    1
    python setup.py install

背景信息

本文档中的代码示例包含以下操作:
  1. 在华北3(张家口)地域创建一个VPC。
  2. 在新建的VPC下创建一个VSwitch。
  3. 删除新建的VPC。
  4. 删除新建的VSwitch。

操作步骤

  1. 在下载的SDK目录中,打开aliyun-openapi-python-sdk-examples\sdk_examples\examples\vpc文件夹。
  2. 使用代码编辑工具打开vpc_quick_start.py文件,根据实际情况配置相关参数,保存退出。
    完整代码示例如下:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    #encoding=utf-8
    import sys
    import json
    import time
     
    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)
                # 判断VPC状态是否可用
                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()
                # 要删除的VPC的ID
                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()
                # 要查询的VPC ID
                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()
                # 交换机所属区的ID,您可以通过调用DescribeZones接口获取地域ID
                request.set_ZoneId(params['zone_id'])
                # 交换机所属的VPC ID
                request.set_VpcId(params['vpc_id'])
                # 交换机的网段
                request.set_CidrBlock(params['cidr_block'])
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                # 判断VSwitch状态是否可用
                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()
                # 要删除的交换机的ID
                request.set_VSwitchId(params['vswitch_id'])
                response = self.client.do_action_with_exception(request)
                response_json = json.loads(response)
                # 判断VSwitch是否被删除成功
                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__":
        client = AcsClient('accessKeyId','accessSecret','cn-shanghai',timeout = 35)
        vpc_quick_start = VpcQuickStart(client)
     
        params = {}
        params['zone_id'] = "cn-zhangjiakou-b"
        params['cidr_block'] = "172.16.0.0/16"
     
        # 创建vpc
        vpc_json = vpc_quick_start.create_vpc()
        print("---------------------------create_vpc---------------------------")
        print(vpc_json)
     
        # 创建vswitch
        params['vpc_id'] = vpc_json['VpcId']
        vswitch_json = vpc_quick_start.create_vswitch(params)
        print("---------------------------create_vswitch---------------------------")
        print(vswitch_json)
     
        # 删除vswitch
        params['vswitch_id'] = vswitch_json['VSwitchId']
        vswitch_json = vpc_quick_start.delete_vswitch(params)
        print("---------------------------delete_vswitch---------------------------")
        print(vswitch_json)
     
        # 删除vpc
        vpc_json = vpc_quick_start.delete_vpc(params)
        print("---------------------------delete_vpc---------------------------")
        print(vpc_json)
  3. 进入vpc_quick_start.py所在的目录,执行如下命令,运行创建和删除VPC与VSwitch示例。
    1
    python vpc_quick_start.py

执行结果

系统回显结果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
---------------------------create_vpc---------------------------
{
  "ResourceGroupId": "rg-acfmxazxxxxxxxx",
  "RouteTableId": "vtb-8vbf9ud7xrcn9xxxxxxxx",
  "VRouterId": "vrt-8vb1qjnxcm03nxxxxxxxx",
  "VpcId": "vpc-8vb67v4ozd8wfxxxxxxxx",
  "RequestId": "5052F988-75CC-46AD-A1A6-0E9E445BD0D5"
}
 
---------------------------create_vswitch---------------------------
{
  "VSwitchId": "vsw-8vbqn2at0kljjxxxxxxxx",
  "RequestId": "0BA1ABF7-21CF-4460-9A86-0BB783886E58"
}
 
---------------------------delete_vswitch---------------------------
{
  "RequestId": "D691F04B-A6EE-49A7-A434-4A45DD3AA0B8"
}
 
---------------------------delete_vpc---------------------------
{
  "RequestId": "4570F816-AB8D-45EA-8913-6AE787C1632C"
}