Help Center/ Virtual Private Cloud/ API Reference/ Application Examples/ Example 1: Creating a VPC and Subnet for an ECS
Updated on 2022-09-26 GMT+08:00

Example 1: Creating a VPC and Subnet for an ECS

Scenarios

This section describes how to create a VPC and subnet for an ECS by calling APIs.

Prerequisites

If you use a token for authentication, you must obtain the token and add X-Auth-Token to the request header when making an API call. Obtain the token by referring to Authentication.

The token obtained from IAM is valid for only 24 hours. If you want to use a token for authentication, you can cache it to avoid frequent calling.

Procedure

  1. Create a VPC.
    1. Send POST https://VPC endpoint/v1/{project_id}/vpcs. Parameter project_id indicates the project ID.
    2. Add X-Auth-Token to the request header.
    3. Set the following parameters in the request body. Plan the value of the cidr field in advance.
      {
          "vpc": {
              "name": "vpc", //VPC name
              "cidr": "192.168.0.0/16" //Available subnet in the VPC
          }
      }
    4. Check the response message.
      • The request is successful if the following response is displayed. In the response, id indicates the VPC ID.
        {
            "vpc": {
                "id": "b6684a27-b049-407d-90b4-c9551f2390e1",
                "name": "vpc",
                "cidr": "192.168.0.0/16",
                "status": "CREATING",
                "routes": []
            }
        }
      • For details about the error codes displayed if the request fails, see section Error Codes.
  1. View details of the VPC.
    1. Send GET https://VPC endpoint/v1/{project_id}/vpcs/{vpc_id}. Parameter project_id indicates the project ID.
    2. Add X-Auth-Token to the request header.
    3. Check the response message.
      • The request is successful if the following response is displayed. In the response, id indicates the VPC ID.
        {
            "vpc": {
                "id": "b6684a27-b049-407d-90b4-c9551f2390e1",
                "name": "vpc",
                "description": "",
                "cidr": "192.168.0.0/16",
                "status": "OK",
                "enterprise_project_id": "0" ,
                "routes": []
            }
        }
      • For details about the error codes displayed if the request fails, see section Error Codes.
  2. Create a subnet in the VPC.
    1. Send POST https://VPC endpoint/v1/{project_id}/subnets. Parameter project_id indicates the project ID.
    2. Add X-Auth-Token to the request header.
    3. Set the following parameters in the request body. For details about the values of dnsList, see What Are the Private DNS Server Addresses Provided by the DNS Service?
      {
      	"subnet": {
      		"name": "subnet",
      		"description": "",
      		"cidr": "192.168.0.0/24",
      		"gateway_ip": "192.168.0.1",
      		"dhcp_enable": true,
      		"dnsList": ["114.xx.xx.114", "114.xx.xx.115"],
      		"availability_zone": "aa-bb-cc",
      		"vpc_id": "b6684a27-b049-407d-90b4-c9551f2390e1"
      	}
      }
    4. Check the response message.
      • The request is successful if the following response is displayed.
        {
        	"subnet": {
        		"id": "4779ab1c-7c1a-44b1-a02e-93dfc361b32d",
        		"name": "subnet",
        		"description": "",
        		"cidr": "192.168.0.0/24",
        		"dnsList": ["114.xx.xx.114", "1114.xx.xx.115"],
        		"status": "UNKNOWN",
        		"vpc_id": "b6684a27-b049-407d-90b4-c9551f2390e1",
        		"gateway_ip": "192.168.0.1",
        		"dhcp_enable": true,
        		"primary_dns": "114.xx.xx.114",
        		"secondary_dns": "114.xx.xx.115",
        		"availability_zone": "aa-bb-cc",
        		"neutron_network_id": "4779ab1c-7c1a-44b1-a02e-93dfc361b32d",
        		"neutron_subnet_id": "213cb9d-3122-2ac1-1a29-91ffc1231a12",
        		"extra_dhcp_opts": []
        	}
        }
      • For details about the error codes displayed if the request fails, see section Error Codes.
  1. View details of the subnet.
    1. Send GET https://VPC endpoint/v1/{project_id}/subnets/{subnet_id}. Parameter project_id indicates the project ID.
    2. Add X-Auth-Token to the request header.
    3. Check the response message.
      {
      	"subnet": {
      		"id": "4779ab1c-7c1a-44b1-a02e-93dfc361b32d",
      		"name": "subnet",
      		"description": "",
      		"cidr": "192.168.20.0/24",
      		"dnsList": ["114.xx.xx.114", "114.xx.xx.115"],
      		"status": "ACTIVE",
      		"vpc_id": "b6684a27-b049-407d-90b4-c9551f2390e1",
      		"gateway_ip": "192.168.20.1",
      		"ipv6_enable": false,
      		"dhcp_enable": true,
      		"primary_dns": "114.xx.xx.114",
      		"secondary_dns": "114.xx.xx.115",
      		"availability_zone": "aa-bb-cc",
      		"neutron_network_id": "4779ab1c-7c1a-44b1-a02e-93dfc361b32d",
      		"neutron_subnet_id": "213cb9d-3122-2ac1-1a29-91ffc1231a12",
      		"extra_dhcp_opts": []
      	}
      }