
# 弹性云服务器挂载网卡
#### 场景描述
当弹性云服务器需要多个网卡时，可以调用接口创建网卡，然后挂载至弹性云服务器。
您可以在创建云服务器时，通过设置"nics"参数值挂载网卡，也可以在创建云服务器成功后挂载网卡。本节主要介绍云服务器创建成功后挂载网卡的场景。
#### 涉及接口
挂载网卡涉及的接口如下：
- [创建网络]
- [创建子网]
- [创建端口]
- [添加云服务器网卡]
- [查询任务执行状态]
 
#### 操作步骤
1. 创建网卡。 
   1. 创建网络。
      - 接口相关信息 URI格式：POST /v1/{project_id}/vpcs
        详情请参见[创建网络](https://support.huaweicloud.com/api-vpc/vpc_api01_0001.html)。
        
      
      - 请求示例 POST https://{Endpoint}/v1/{project_id}/vpcs
        {endpoint}信息请从[地区和终端节点](https://console.huaweicloud.com/apiexplorer/#/endpoint/ECS)获取。
        Body：
        ```
        {
            "vpc": {
                "name": "vpc",
                "description": "test",
                "cidr": "192.168.0.0/16"
            }
        }
        ```
        
      
      - 响应示例
        ```
        {
        "vpc": {
        "id": "99d9d709-8478-4b46-9f3f-2206b1023fd3",
        "name": "vpc",
        "description": "test",
        "cidr": "192.168.0.0/16",
        "status": "CREATING",
        "routes": []
        }
        }
        ```
        
       
   
   2. 记录响应中"vpc"的ID。
   
   3. 创建子网。
      - 接口相关信息 URI格式：POST /v1/{project_id}/subnets
        详情请参见[创建子网](https://support.huaweicloud.com/api-vpc/vpc_subnet01_0001.html)。
        
      
      - 请求示例 POST https://{endpoint}/v1/{project_id}/subnets
        {endpoint}信息请从[地区和终端节点](https://console.huaweicloud.com/apiexplorer/#/endpoint/ECS)获取。
        Body：
        ```
        {
            "subnet": {
                "name": "subnet",
                "description": "",
                "cidr": "192.168.20.0/24",
                "gateway_ip": "192.168.20.1",
                "ipv6_enable": true,
                "dhcp_enable": true,
                "primary_dns": "114.xx.xx.114",
                "secondary_dns": "114.xx.xx.115",
                "dnsList": [
                    "114.xx.xx.114",
                    "114.xx.xx.115"
                ],
                "availability_zone": "aa-bb-cc",
                "enable_network_address_usage_metrics": false,
                "vpc_id": "3ec3b33f-ac1c-4630-ad1c-7dba1ed79d85",
                "extra_dhcp_opts": [
                    {
                        "opt_value": "10.100.0.33,10.100.0.34",
                        "opt_name": "ntp"
                    },
                    {
                        "opt_value": "24h",
                        "opt_name": "addresstime"
                    },
                    {
                        "opt_value": "2h",
                        "opt_name": "ipv6_addresstime"
                    }
                ]
            }
        }
        ```
        
      
      - 响应示例
        ```
        {
            "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": "UNKNOWN",
                "vpc_id": "3ec3b33f-ac1c-4630-ad1c-7dba1ed79d85",
                "gateway_ip": "192.168.20.1",
                "ipv6_enable": true, 
                "cidr_v6": "2001:db8:a583::/64",
                "gateway_ip_v6": "2001:db8:a583::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",
                "neutron_subnet_id_v6": "e0fa7de1-a6e2-44c9-b052-b9d8cebe93c4",  
                "enable_network_address_usage_metrics": false,
                "available_ip_address_count": 251,
                "extra_dhcp_opts": [
                    {
                        "opt_value": "10.100.0.33,10.100.0.34",
                        "opt_name": "ntp"
                    },
                    {
                        "opt_value": "24h",
                        "opt_name": "addresstime"
                    },
                    {
                        "opt_value": "2h",
                        "opt_name": "ipv6_addresstime"
                    }
                ],
                "tenant_id": "087679f0aa80d32a2f4ec0172f5e902b",
                "created_at": "2022-12-15T02:42:07",
                "updated_at": "2022-12-15T02:42:07"
            }
        }
        ```
        
        
       
   
   4. 记录响应中"subnet"的ID。
   
   5. 创建端口。
      - 接口相关信息 URI格式：POST /v1/{project_id}/ports
        详情请参见[创建端口](https://support.huaweicloud.com/api-vpc/vpc_port01_0006.html)。
        
      
      - 请求示例 POST https://*{endpoint}*/v1/{project_id}/ports
        {endpoint}信息请从[地区和终端节点](https://console.huaweicloud.com/apiexplorer/#/endpoint/ECS)获取。
        Body：
        ```
        {
            "port": {
                "fixed_ips": [
                    {
                        "ip_address": "192.168.0.38",
                        "subnet_id": "06bc2359-d75e-4f96-82f4-313e39c7148c"
                    }
                ],
                "network_id": "28a1c93c-9a5e-4a9f-813b-e495bdef7d34",
                "security_groups": [
                    "f2c5b3fc-b971-4a86-87b9-032586260e3e"
                ]
            }
        }
        ```
        
      
      - 响应示例
        ```
        {
            "port": {
                "id": "d00f9c13-412f-4855-8af3-de5d8c24cd60",
                "name": "test",
                "status": "DOWN",
                "admin_state_up": "true",
                "fixed_ips": [
                    {
                        "subnet_id": "70f2e74b-e660-410a-b754-0ca46744348a",
                        "ip_address": "10.128.1.10"
                    }
                ],
                "dns_name": "",
                "mac_address": "fa:16:3e:d7:f2:6c",
                "network_id": "5b808927-13c9-4e60-a4f4-ed6ffe225167",
                "tenant_id": "43f2d1cca56a40729dcb17212482f34d",
                "device_id": "",
                "device_owner": "",
                "security_groups": [
                    "02b4e8ee-74fa-4a31-802e-5490df11245e"
                ],
                "extra_dhcp_opts": [],
                "allowed_address_pairs": [],
                "binding:vnic_type": "normal",
                "enable_efi": false
            }
        }
        ```
        
        
       
   
   6. 记录响应中"port"的ID。
   
   
   
   
2. 挂载网卡。
   
   - 接口相关信息 URI格式：POST /v1/{project_id}/cloudservers/{server_id}/nics
     详情请参见"[批量添加云服务器网卡 - BatchAddServerNics](https://support.huaweicloud.com/api-ecs/ecs_02_0501.html)"。
     
   
   - 请求示例 POST https://*{endpoint}*/v1/74610f3a5ad941998e91f076297ecf27/cloudservers/9f4d9281-95e7-4915-a126-1ee597101e2e/nics
     {endpoint}信息请从[地区和终端节点](https://console.huaweicloud.com/apiexplorer/#/endpoint/ECS)获取。
     Body：
     ```
     {
         "nics": [
             {
                 "subnet_id": "877b5567-e8c6-4a0d-aabf-0f13da225fe5" 
             }
         ]
     }
     ```
     
   
   - 响应示例
     ```
     {      
         "job_id": "ff80808288d41e1b018990260955686a" 
     }
     ```
     
   
   
   
   
3. 查询任务执行状态。
   
   - 接口相关信息 URI格式：GET /v1/{project_id}/jobs/{job_id}
     详情请参见[查询任务的执行状态 - ShowJob](https://support.huaweicloud.com/api-ecs/ecs_02_0901.html)。
     
   
   - 请求示例 GET https://{endpoint}/v1/74610f3a5ad941998e91f076297ecf27/jobs/ff80808288d41e1b018990260955686a
     {endpoint}信息请从[地区和终端节点](https://console.huaweicloud.com/apiexplorer/#/endpoint/ECS)获取。
     
   
   - 响应示例
     ```
     {
         "job_id": "ff80808288d41e1b018990260955686a",
         "job_type": "addNicsForServer",
         "begin_time": "2025-02-14T08:43:05.181Z",
         "end_time": "2025-02-14T08:43:17.075Z",
         "status": "SUCCESS",
         "error_code": null,
         "fail_reason": null,
         "entities": {
             "sub_jobs_total": 1,
             "sub_jobs": [
                 {
                     "job_id": "ff80808288d41e1b018990260955686a",
                     "job_type": "addNic",
                     "begin_time": "2025-02-14T08:43:06.423Z",
                     "end_time": "2025-02-14T08:43:12.380Z",
                     "status": "SUCCESS",
                     "error_code": null,
                     "fail_reason": null,
                     "entities": {
                         "nic_id": "877b5567-e8c6-4a0d-aabf-0f13da225fe5"
                     }
                 }
             ]
         }
     }
     ```
     
   
   
   
   
 
