VPC enables you to provision logically isolated, configurable, and manageable virtual networks for ECSs, improving security of cloud resources and simplifying network deployment.
A typical VPC is composed of a router, network, and subnet, as shown in the following figure.
You can create a VPC on the console and obtain the UUID.
Python OpenStack SDK allows you to create a subnet. The detailed operations are as follows:
The following code shows the network creation process. You can modify these configurations as required. After you have created the router, network, and subnet and connected the subnet to the router, a new VPC is displayed on the console.
def create_VPC(conn) : #create a router testRouterName = "PythonSDKVPC" router = conn.network.create_router( name=testRouterName ) #create_network testNetworkName = "PythonSDKNet" network = conn.network.create_network( name=testNetworkName ) #create a subnet testSubnetName = "PythonSDKSubnet" subnet = conn.network.create_subnet( name = testSubnetName, is_dhcp_enabled = True, cidr = "192.168.1.0/24", network_id = network.id ) #connect the subnet to the router, make the subnet connect to the internet. conn.network.add_interface_to_router(router, subnet.id)
Before deleting a VPC, you need to delete ECSs created in the subnet in the VPC, cancel the association between the router and the subnet, and delete the subnet and the network.
After the ECS deletion command is executed, you can delete the network after ensuring that the ECSs are deleted based on the ECS deletion status. You can use the following code to delete the network:
def deleteVPC(conn): conn.network.remove_interface_from_router(router, subnetId) conn.network.delete_subnet(subnetId) conn.network.delete_network(networkdId) conn.network.delete_router(routerId)
An external network is a network with attribute router:external set to true. This network is used to allocate an EIP. After the EIP is bound to an ECS, the ECS can be accessed from the Internet.
An external network is already available and you do not need to create one.
You can use the following code to get the external network:
def get_external_network(self): for network_each in self.conn.network.networks(): if network_each.is_router_external == True : return network_each