This topic describes how to use Alibaba Cloud SDK for Python to connect two virtual private clouds (VPCs) that belong to the same region and Alibaba Cloud account.
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
When you create a router interface, take note of the following limits:
- You can create only one pair of interfaces to be connected between two routers.
- You can create at most five interfaces for each router.
- If your Alibaba Cloud account has a router interface with overdue payments, you cannot create new router interfaces.
- Each destination CIDR block of route entries in the same route table must be unique.
Procedure
- In the directory of the downloaded SDK files, open the aliyun-openapi-python-sdk-examples\sdk_examples\examples\ec folder.
- Open the ec_same_account.py file in your text editor. Set the required parameters, save the configurations, and then exit the editor. The code sample in this topic includes the following operations:
- Create an interface for the initiator router.
- Create an interface for the receiver router.
- Modify the configurations of the initiator router interface.
- Modify the configurations of the receiver router interface.
- Query information about the initiator router interface.
- Query information about the receiver router interface.
- Connect the initiator router interface to the receiver router interface.
- Create a route whose next hop is the initiator router interface.
- Create a route whose next hop is the receiver router interface.
- Delete the route whose next hop is the receiver router interface.
- Delete the route whose next hop is the initiator router interface.
- Disable the initiator router interface.
- Disable the receiver router interface.
- Delete the initiator router interface.
- Delete the receiver router interface.
#encoding=utf-8 import sys import json from alibabacloud_credentials.client import Client as CredClient from aliyunsdkcore.acs_exception.exceptions import ServerException, ClientException from aliyunsdkvpc.request.v20160428 import CreateRouterInterfaceRequest from aliyunsdkvpc.request.v20160428 import DeleteRouterInterfaceRequest from aliyunsdkvpc.request.v20160428 import DescribeRouterInterfacesRequest from aliyunsdkvpc.request.v20160428 import ConnectRouterInterfaceRequest from aliyunsdkvpc.request.v20160428 import DeactivateRouterInterfaceRequest from aliyunsdkvpc.request.v20160428 import ModifyRouterInterfaceAttributeRequest from sdk_lib.sdk_route_entry import RouteEntry from sdk_lib.exception import ExceptionHandler from sdk_lib.common_util import CommonUtil from sdk_lib.check_status import CheckStatus from sdk_lib.consts import * # 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>') class RouterInterface(object): def __init__(self, client): self.client = client def create_router_interface(self, params): """ create_router_interface: Create a router interface. """ try: request = CreateRouterInterfaceRequest.CreateRouterInterfaceRequest() # The specification of the router interface. request.set_Spec(params['spec']) # The role of the router interface. request.set_Role(params['role']) # The ID of the router that is associated with the router interface. request.set_RouterId(params['router_id']) # The type of router that is associated with the router interface. request.set_RouterType(params['router_type']) # The ID of the region where the receiver is deployed. request.set_OppositeRegionId(params['opposite_region_id']) # The type of router that is associated with the peer router interface. request.set_OppositeRouterType(params['opposite_router_type']) response = self.client.do_action_with_exception(request) response_json = json.loads(response) # Check whether the router interface is in the Available state. if CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME, self.describe_ri_status, 'Idle', response_json['RouterInterfaceId']): return response_json except ServerException as e: ExceptionHandler.server_exception(e) except ClientException as e: ExceptionHandler.client_exception(e) def connect_router_interface(self, params): """ connect_router_interface: A connection request is sent from the initiator router interface to the receiver router interface. """ try: request = ConnectRouterInterfaceRequest.ConnectRouterInterfaceRequest() # The ID of the initiator router interface that initiates the connection request. request.set_RouterInterfaceId(params['router_interface_id']) response = self.client.do_action_with_exception(request) response_json = json.loads(response) if CheckStatus.check_status(TIME_DEFAULT_OUT * 5, DEFAULT_TIME * 5, self.describe_ri_status, 'Active', params['router_interface_id']): return response_json except ServerException as e: ExceptionHandler.server_exception(e) except ClientException as e: ExceptionHandler.client_exception(e) def deactivate_router_interface(self, params): """ deactivate_router_interface: Disable the router interface. """ try: request = DeactivateRouterInterfaceRequest.DeactivateRouterInterfaceRequest() # The ID of the router interface. request.set_RouterInterfaceId(params['router_interface_id']) response = self.client.do_action_with_exception(request) response_json = json.loads(response) # Check whether the router interface is in the Available state. if CheckStatus.check_status(TIME_DEFAULT_OUT * 5, DEFAULT_TIME * 5, self.describe_ri_status, 'Inactive', params['router_interface_id']): return response_json except ServerException as e: ExceptionHandler.server_exception(e) except ClientException as e: ExceptionHandler.client_exception(e) def modify_router_interface_attribute(self, params): """ modify_router_interface_attribute: Modify the configurations of the router interface. """ try: request = ModifyRouterInterfaceAttributeRequest.ModifyRouterInterfaceAttributeRequest() # The ID of the router interface. request.set_RouterInterfaceId(params['router_interface_id']) # The ID of the peer router interface. request.set_OppositeInterfaceId(params['opposite_interface_id']) # The ID of the peer router. request.set_OppositeRouterId(params['opposite_router_id']) response = self.client.do_action_with_exception(request) response_json = json.loads(response) # Check whether the router interface is in the Available state. if CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME, self.describe_ri_status, 'Idle', params['router_interface_id']): return response_json except ServerException as e: ExceptionHandler.server_exception(e) except ClientException as e: ExceptionHandler.client_exception(e) def describe_router_interface(self, instance_id): """ describe_router_interface: Query the router interface in a specific region. """ try: request = DescribeRouterInterfacesRequest.DescribeRouterInterfacesRequest() # The type of filter condition that is used to query the router interface. request.add_query_param('Filter.1.Key', "RouterInterfaceId") # The ID of the router interface to be queried. request.add_query_param('Filter.1.Value.1', instance_id) response = self.client.do_action_with_exception(request) response_json = json.loads(response) return response_json except ServerException as e: ExceptionHandler.server_exception(e) except ClientException as e: ExceptionHandler.client_exception(e) def describe_ri_status(self, instance_id): """ describe_ri_status: Query the state of the router interface in a specific region. """ response = self.describe_router_interface(instance_id) if len(response['RouterInterfaceSet']['RouterInterfaceType']) == 0: return '' return response['RouterInterfaceSet']['RouterInterfaceType'][0]['Status'] def delete_router_interface(self, params): """ delete_router_interface: Delete the router interface. """ try: request = DeleteRouterInterfaceRequest.DeleteRouterInterfaceRequest() # The ID of the router interface. request.set_RouterInterfaceId(params['instance_id']) response = self.client.do_action_with_exception(request) response_json = json.loads(response) # Check whether the router interface is in the Available state. if CheckStatus.check_status(TIME_DEFAULT_OUT, DEFAULT_TIME * 5, self.describe_ri_status, '', params['instance_id']): return response_json except ServerException as e: ExceptionHandler.server_exception(e) except ClientException as e: ExceptionHandler.client_exception(e) def main(): client = ACS_CLIENT router_interface = RouterInterface(client) route_entry = RouteEntry(client) params = {} params['spec'] = "Large.2" params['role'] = "InitiatingSide" params['router_id'] = ROUTER_ID params['router_type'] = "VRouter" params['opposite_region_id'] = "cn-hangzhou" params['opposite_router_type'] = "VRouter" # Create an initiator router interface. router_interface_json = router_interface.create_router_interface(params) CommonUtil.log("create_router_interface", router_interface_json) # Create a receiver router interface. params['spec'] = "Negative" params['role'] = "AcceptingSide" params['router_id'] = ROUTER_ID2 router_interface_json2 = router_interface.create_router_interface(params) CommonUtil.log("create_router_interface", router_interface_json2) # Modify the information about the initiator router interface. params['router_interface_id'] = router_interface_json['RouterInterfaceId'] params['opposite_interface_id'] = router_interface_json2['RouterInterfaceId'] params['opposite_router_id'] = ROUTER_ID2 modify_ri_json = router_interface.modify_router_interface_attribute(params) CommonUtil.log("modify_router_interface_attribute", modify_ri_json) # Modify the information about the receiver router interface. params['router_interface_id'] = router_interface_json2['RouterInterfaceId'] params['opposite_interface_id'] = router_interface_json['RouterInterfaceId'] params['opposite_router_id'] = ROUTER_ID modify_ri_json2 = router_interface.modify_router_interface_attribute(params) CommonUtil.log("modify_router_interface_attribute", modify_ri_json2) # Query information about the initiator router interface. describe_ri_json = router_interface.describe_router_interface(router_interface_json['RouterInterfaceId']) CommonUtil.log("describe_router_interface", describe_ri_json) # Query information about the receiver router interface. describe_ri_json2 = router_interface.describe_router_interface(router_interface_json2['RouterInterfaceId']) CommonUtil.log("describe_router_interface", describe_ri_json2) # Initiate a connection. params['router_interface_id'] = router_interface_json['RouterInterfaceId'] connect_ri_json = router_interface.connect_router_interface(params) CommonUtil.log("connect_router_interface", connect_ri_json) # Create a route whose next hop is the initiator router interface. params['route_table_id'] = TABLE_ID params['destination_cidr_block'] = "0.0.0.0/0" params['nexthop_type'] = 'RouterInterface' params['nexthop_id'] = router_interface_json['RouterInterfaceId'] route_entry_json = route_entry.create_route_entry(params) CommonUtil.log("create_route_entry", route_entry_json) # Create a route whose next hop is the receiver router interface. params['route_table_id'] = TABLE_ID2 params['destination_cidr_block'] = "0.0.0.0/0" params['nexthop_type'] = 'RouterInterface' params['nexthop_id'] = router_interface_json2['RouterInterfaceId'] route_entry_json2 = route_entry.create_route_entry(params) CommonUtil.log("create_route_entry", route_entry_json2) # Delete the route whose next hop is the receiver router interface. route_entry_json = route_entry.delete_route_entry(params) CommonUtil.log("delete_route_entry", route_entry_json) # Delete the route whose next hop is the initiator router interface. params['route_table_id'] = TABLE_ID params['nexthop_id'] = router_interface_json['RouterInterfaceId'] route_entry_json = route_entry.delete_route_entry(params) CommonUtil.log("delete_route_entry", route_entry_json) # Disable the initiator router interface. params['router_interface_id'] = router_interface_json['RouterInterfaceId'] deactivate_ri_json = router_interface.deactivate_router_interface(params) CommonUtil.log("deactivate_router_interface", deactivate_ri_json) # Disable the receiver router interface. params['router_interface_id'] = router_interface_json2['RouterInterfaceId'] deactivate_ri_json2 = router_interface.deactivate_router_interface(params) CommonUtil.log("deactivate_router_interface", deactivate_ri_json2) # Delete the initiator router interface. params['instance_id'] = router_interface_json['RouterInterfaceId'] router_interface_json = router_interface.delete_router_interface(params) CommonUtil.log("delete_router_interface", router_interface_json) # Delete the receiver router interface. params['instance_id'] = router_interface_json2['RouterInterfaceId'] router_interface_json2 = router_interface.delete_router_interface(params) CommonUtil.log("delete_router_interface", router_interface_json2) if __name__ == '__main__': sys.exit(main())
- Go to the directory where the ec_same_account.py file is stored and run the following command to connect two VPCs that belong to the same region and Alibaba Cloud account:
python ec_same_account.py
Result
The following output is returned:
---------------------------create_router_interface---------------------------
{
"RequestId": "F5493DC1-53B0-4916-874F-87773A54525F",
"RouterInterfaceId": "ri-bp1ujwb6xsw16****"
}
---------------------------create_router_interface---------------------------
{
"RequestId": "E3F5BE99-B2C5-4751-8173-0F590300EE72",
"RouterInterfaceId": "ri-bp1vze2rusg2c****"
}
---------------------------modify_router_interface_attribute---------------------------
{
"RequestId": "8D691507-F31A-41E5-9C72-457EFF1F7727"
}
---------------------------modify_router_interface_attribute---------------------------
{
"RequestId": "2E664208-8F37-4F19-B719-00B4F1AF03B2"
}
---------------------------describe_router_interface---------------------------
{
"TotalCount": 1,
"RouterInterfaceSet": {
"RouterInterfaceType": [
{
"BusinessStatus": "Normal",
"CreationTime": "2019-04-30T06:09:16Z",
"Role": "InitiatingSide",
"OppositeRouterId": "vrt-bp141no9pds2b****",
"Spec": "Large.2",
"Status": "Idle",
"EndTime": "2999-09-08T16:00:00Z",
"OppositeInterfaceSpec": "Negative",
"RouterInterfaceId": "ri-bp1ujwb6xsw16****",
"RouterType": "VRouter",
"OppositeBandwidth": 0,
"OppositeVpcInstanceId": "vpc-bp1v31by9jix2****",
"HasReservationData": false,
"OppositeInterfaceBusinessStatus": "Normal",
"OppositeRouterType": "VRouter",
"OppositeRegionId": "cn-hangzhou",
"VpcInstanceId": "vpc-bp15opprpg0rg****",
"RouterId": "vrt-bp1ltkytn6lgm****",
"CrossBorder": false,
"OppositeInterfaceOwnerId": "",
"Bandwidth": 2048,
"OppositeInterfaceId": "ri-bp1vze2rusg2c****",
"ChargeType": "AfterPay"
}
]
},
"PageNumber": 1,
"RequestId": "89EF0631-0A36-41AD-A586-AF4FFDA6E68B",
"PageSize": 10
}
---------------------------describe_router_interface---------------------------
{
"TotalCount": 1,
"RouterInterfaceSet": {
"RouterInterfaceType": [
{
"Status": "Idle",
"OppositeRegionId": "cn-hangzhou",
"BusinessStatus": "Normal",
"OppositeRouterId": "vrt-bp1ltkytn6lgm****",
"VpcInstanceId": "vpc-bp1v31by9jix2****",
"RouterInterfaceId": "ri-bp1vze2rusg2c****",
"CreationTime": "2019-04-30T06:09:18Z",
"RouterType": "VRouter",
"OppositeInterfaceOwnerId": "",
"RouterId": "vrt-bp141no9pds2b****",
"Bandwidth": 0,
"OppositeInterfaceId": "ri-bp1ujwb6xsw16****",
"EndTime": "2999-09-08T16:00:00Z",
"ChargeType": "AfterPay",
"OppositeVpcInstanceId": "vpc-bp15opprpg0rg****",
"HasReservationData": false,
"CrossBorder": false,
"OppositeInterfaceBusinessStatus": "Normal",
"Spec": "Negative",
"OppositeRouterType": "VRouter",
"Role": "AcceptingSide"
}
]
},
"PageNumber": 1,
"RequestId": "578448D7-9DCF-4703-8337-EF88DDF2C325",
"PageSize": 10
}
---------------------------connect_router_interface---------------------------
{
"RequestId": "A5DBB86B-F6F5-4A53-899E-8CF4FEF510F2"
}
---------------------------create_route_entry---------------------------
{
"RequestId": "70D896FE-986B-48EF-9734-17D6BDC8327A"
}
---------------------------create_route_entry---------------------------
{
"RequestId": "A2233E25-4D6B-4713-A96F-E7CA745973CA"
}
---------------------------delete_route_entry---------------------------
{
"RequestId": "464C62A4-EE65-4414-AF0A-4984AE6B8696"
}
--------------------------delete_route_entry---------------------------
{
"RequestId": "0C11A332-969B-47CA-A683-5BFFECA28B3D"
}
---------------------------deactivate_router_interface---------------------------
{
"RequestId": "018305AD-FB9E-450A-91E8-3830634F5AC2"
}
---------------------------deactivate_router_interface---------------------------
{
"RequestId": "89B03203-9224-4CA3-8679-E0A49029A2D2"
}
---------------------------delete_router_interface---------------------------
{
"RequestId": "FB0424CE-D0C7-438B-A3FA-BCF24EE9CC8A"
}
---------------------------delete_router_interface---------------------------
{
"RequestId": "8A0A0BB6-A69D-461B-A117-14AD6670DECA"
}