• CLI

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

VPC Java SDK Demo

VPC Service OpenStack4j 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

OpenStack4j 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.

public Network createNetwork() { 
              //Create a router 
              Router router =  os.networking().router().create(Builders.router() 
                            .name("routerName") 
                            .build()); 
              //Create a network 
              Network network = os.networking().network().create(Builders 
                            .network() 
                            .name("networkName") 
                            .adminStateUp(true) 
                            .build()); 
              //Create a subnet 
              Subnet subnet = os.networking().subnet().create(Builders.subnet() 
                            .networkId(network.getId()) 
                            .name("subnetName") 
                            .enableDHCP(true) 
                            .cidr("192.168.0.0/24") 
                            .addDNSNameServer("8.8.8.8") 
                            .gateway("192.168.0.1") 
                            .build()); 
              //Connect the subnet to the router, make the router connect to the internet. 
              RouterInterface routerinf = os.networking().router().attachInterface(router.getId(), AttachInterfaceType.SUBNET, subnet.getId()); 
              return network; 
       }

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. For details about how to delete the network, see the following code:

private void clearNet() { 
os.networking().router().detachInterface(routerID, subnetID, routerinf.getPortId()); 
os.networking().subnet().delete(subnetID); 
os.networking().network().delete(networkID); 
os.networking().router().delete(routerID); 
}

External Network

An external network is a network with attribute router:external set to true. This network is used to allocate elastic IP addresses (EIPs). 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 run the API: GET /v2.0/networks?router:external=True command to query the ID of the network which is used for creating the EIP.