Updated on 2022-09-23 GMT+08:00

Attaching a NIC to an ECS

Scenarios

If an ECS requires multiple NICs, you can call the API for creating NICs and attach them to the ECS.

A NIC can be attached by setting the nics parameter during ECS creation or after the ECS is created. This section describes how to attach a NIC to a created ECS.

Involved APIs

Attaching a NIC involves the following APIs:

Procedure

  1. Create a NIC.

    1. Create a network.
      • API

        URI format: POST /v1/{project_id}/vpcs

        For details, see Creating a VPC.

      • Example request

        POST https://{Endpoint}/v1/{project_id}/vpcs

        Obtain {endpoint} from the administrator.

        Body:

        {
            "vpc": {
                "name": "vpc",
                "description": "test",
                "cidr": "192.168.0.0/16"
            }
        }
      • Example response
        {
        	"vpc": {
        		"id": "99d9d709-8478-4b46-9f3f-2206b1023fd3",
        		"name": "vpc",
        		"description": "test",
        		"cidr": "192.168.0.0/16",
        		"status": "CREATING",
        		"routes": []
        	}
        }
    2. Record the vpc ID in the response.
    3. Create a subnet.
      • API

        URI format: POST /v2.0/subnets

        For details, see Creating a Subnet.

      • Example request

        POST: https://{endpoint}/v2.0/subnets

        Obtain {endpoint} from the administrator.

        Body:

        {
            "subnet": {
                "name": "testsubnet",
                "enable_dhcp": true,
                "network_id": "c4a3019d-1ac0-4cfb-a838-2342eb992e6b",
                "tenant_id": "74610f3a5ad941998e91f076297ecf27",
                "dns_nameservers": [
                    "8.8.8.8",
                    "8.8.8.7"
                ],
                "allocation_pools": [
                    {
                        "start": "10.0.10.2",
                        "end": "10.0.10.254"
                    }
                ],
                "host_routes": [],
                "ip_version": 4,
                "gateway_ip": "10.0.10.1",
                "cidr": "10.0.10.0/24"
            }
        }
      • Example response
        {
          "subnet": {
            "name": "testsubnet",
            "cidr": "10.0.10.0/24",
            "id": "877b5567-e8c6-4a0d-aabf-0f13da225fe5",
            "enable_dhcp": true,
            "network_id": "c4a3019d-1ac0-4cfb-a838-2342eb992e6b",
            "tenant_id": "74610f3a5ad941998e91f076297ecf27",
            "dns_nameservers": [
              "8.8.8.8",
              "8.8.8.7"
            ],
            "allocation_pools": [
              {
                "start": "10.0.10.2",
                "end": "10.0.10.254"
              }
            ],
            "host_routes": [],
            "ip_version": 4,
            "gateway_ip": "10.0.10.1"
          }
        }
    4. Record the subnet ID in the response.
    5. Create a port.
      • API

        URI format: POST /v2.0/ports

        For details, see Creating a Port.

      • Example request

        POST: https://{endpoint}/v2.0/ports

        Obtain {endpoint} from the administrator.

        Body:

        {
            "port": {
                "admin_state_up": true, 
        
                "fixed_ips": [
                    {
                        "subnet_id": "877b5567-e8c6-4a0d-aabf-0f13da225fe5"
                    }
                ], 
                "name": "test", 
                "network_id": "c4a3019d-1ac0-4cfb-a838-2342eb992e6b",
                "tenant_id": "74610f3a5ad941998e91f076297ecf27"
            }
        }
      • Example response
        {
          "port": {
            "id": "7bf1c36f-e7f8-478a-be3d-674b486abbc4",
            "name": "test",
            "status": "DOWN",
            "admin_state_up": true,
            "fixed_ips": [
              {
                "subnet_id": "877b5567-e8c6-4a0d-aabf-0f13da225fe5",
                "ip_address": "10.0.10.233"
              }
            ],
            "mac_address": "fa:16:3e:db:91:f6",
            "network_id": "c4a3019d-1ac0-4cfb-a838-2342eb992e6b",
            "tenant_id": "74610f3a5ad941998e91f076297ecf27",
            "device_id": "",
            "device_owner": "",
            "security_groups": [
              "93031677-2895-4b83-855a-637e309aa9e6"
            ],
            "extra_dhcp_opts": [],
            "allowed_address_pairs": [],
            "binding:vnic_type": "normal",
            "binding:vif_details": {},
            "binding:profile": {}
          }
        }
    6. Record the port ID in the response.

  2. Attach the NIC to the ECS.

    • API

      URI format: POST /v2.1/{tenant_id}/servers/{server_id}/os-interface

      For details, see Adding a NIC to an ECS.

    • Example request

      POST: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/servers/9f4d9281-95e7-4915-a126-1ee597101e2e/os-interface

      Obtain {endpoint} from the administrator.

      Body:

      {
          "interfaceAttachment": {
              "port_id": "7bf1c36f-e7f8-478a-be3d-674b486abbc4"
          }
      }
    • Example response
      {
        "interfaceAttachment": {
          "port_state": "ACTIVE",
          "fixed_ips": [
            {
              "subnet_id": "877b5567-e8c6-4a0d-aabf-0f13da225fe5",
              "ip_address": "10.0.10.233"
            }
          ],
          "port_id": "7bf1c36f-e7f8-478a-be3d-674b486abbc4",
          "net_id": "c4a3019d-1ac0-4cfb-a838-2342eb992e6b",
          "mac_addr": "fa:16:3e:db:91:f6"
        }
      }

  3. Verify the NIC attachment.

    • API

      URI format: GET /v2.1/{tenant_id}/servers/{server_id}/os-interface

      For details, see Querying ECS NICs.

    • Example request

      GET: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/servers/9f4d9281-95e7-4915-a126-1ee597101e2e/os-interface

      Obtain {endpoint} from the administrator.

    • Example response
      {
        "interfaceAttachments": [
          {
            "port_state": "ACTIVE",
            "fixed_ips": [
              {
                "subnet_id": "46712fe4-25bd-4eae-874b-a528abfb76be",
                "ip_address": "192.168.0.50"
              }
            ],
            "port_id": "dd706739-b696-40be-a9f4-477ce478cb18",
            "net_id": "17251a8f-a671-4d7c-85d9-af5415962994",
            "mac_addr": "fa:16:3e:a5:e0:3c"
          },
      {
            "port_state": "ACTIVE",
            "fixed_ips": [
              {
                "subnet_id": "877b5567-e8c6-4a0d-aabf-0f13da225fe5",
                "ip_address": "10.0.10.233"
              }
            ],
            "port_id": "7bf1c36f-e7f8-478a-be3d-674b486abbc4",
            "net_id": "c4a3019d-1ac0-4cfb-a838-2342eb992e6b",
            "mac_addr": "fa:16:3e:db:91:f6"
          }
        ]
      }