• CLI

sdk
  1. Help Center
  2. CLI
  3. Developer Guide
  4. SDK
  5. Python
  6. VPC Python SDK Demo

VPC Python SDK Demo

VPC Service Python OpenStack SDK Demo

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.

  • Router: A router is a logical entity for forwarding packets across internal subnets and translating the IP addresses of the packets on external networks through an appropriate external gateway.
  • Network: A network is an isolated layer-2 network segment, which is similar to a VLAN in the physical network.
  • Subnet: A subnet is an IP address segment consisting of IPv4 or IPv6 addresses with their associated configuration status.

Creating a VPC and Subnet

Python OpenStack SDK allows you to create a subnet. The detailed operations are as follows:

  1. Create a router.
  2. Create a network.
  3. Create a subnet.
  4. Connect the subnet to the router.

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)

Deleting a VPC

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)

External Network

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