更新时间:2025-08-19 GMT+08:00

创建辅助弹性网卡

功能介绍

辅助弹性网卡是一种基于弹性网卡的衍生资源,用户可以通过创建辅助弹性网卡,使单个云服务器实例挂载更多网卡,实现灵活、高可用的网络方案配置。

调用方法

请参见如何调用API

URI

POST /v3/{project_id}/vpc/sub-network-interfaces

表1 路径参数

参数

是否必选

参数类型

描述

project_id

String

参数解释

辅助弹性网卡所属的项目ID。获取方式请参见获取项目ID

取值范围

不涉及。

请求参数

表2 请求Body参数

参数

是否必选

参数类型

描述

dry_run

Boolean

参数解释

是否只预检此次请求。

约束限制

不涉及。

取值范围

  • true:发送检查请求,不会创建辅助弹性网卡。检查项包括是否填写了必需参数、请求格式、业务限制。如果检查不通过,则返回对应错误。如果检查通过,则返回响应码202。

  • false:发送正常请求,并直接创建辅助弹性网卡。

默认取值

false

sub_network_interface

CreateSubNetworkInterfaceOption object

参数解释

创建辅助弹性网卡的请求体。

约束限制

不涉及。

取值范围

不涉及。

默认取值

不涉及。

表3 CreateSubNetworkInterfaceOption

参数

是否必选

参数类型

描述

virsubnet_id

String

参数解释

辅助弹性网卡所在的虚拟子网ID。

约束限制

带“-”的标准UUID格式。

取值范围

不涉及。

默认取值

不涉及。

vlan_id

String

参数解释

辅助弹性网卡的VLAN ID。

约束限制

同一个宿主弹性网卡下,辅助弹性网卡的VLAN ID不能重复。

取值范围

1-4094

默认取值

不涉及。

parent_id

String

参数解释

辅助弹性网卡所挂载的弹性网卡的ID。

约束限制

必须是实际存在的弹性网卡ID。

取值范围

不涉及。

默认取值

不涉及。

description

String

参数解释

辅助弹性网卡的描述信息。

约束限制

0-255个字符,不能包含“<”和“>”。

取值范围

不涉及。

默认取值

不涉及。

ipv6_enable

Boolean

参数解释

辅助弹性网卡是否开启IPv6能力。

约束限制

不涉及。

取值范围

  • true:开启IPv6。

  • false:关闭IPv6。

默认取值

false

private_ip_address

String

参数解释

辅助弹性网卡的私有IPv4地址。

约束限制

必须在虚拟子网的网段内,请求时此参数不填则在虚拟子网网段内随机分配IP地址。

取值范围

不涉及。

默认取值

不涉及。

ipv6_ip_address

String

参数解释

辅助弹性网卡的私有IPv6地址。

约束限制

必须在虚拟子网的网段内,请求时此参数不填则在虚拟子网网段内随机分配IP地址。

取值范围

不涉及。

默认取值

不涉及。

security_groups

Array of strings

参数解释

辅助弹性网卡关联的安全组的ID列表。例如:"security_groups": ["a0608cbf-d047-4f54-8b28-cd7b59853fff"]。

约束限制

如果请求时不指定此参数,辅助弹性网卡创建后会自动关联默认安全组。

取值范围

不涉及。

默认取值

不涉及。

project_id

String

参数解释

辅助弹性网卡所属的项目ID。

约束限制

此参数只能指定为当前租户的租户ID,不指定时默认是当前租户的项目ID。

取值范围

不涉及。

默认取值

不涉及。

allowed_address_pairs

Array of AllowedAddressPair objects

参数解释

辅助弹性网卡的IP/Mac对列表,详情请参见“AllowedAddressPair”对象表。

约束限制

  • 如果allowed_address_pairs配置地址池较大的IP网段(掩码小于24位),建议为该网卡配置一个单独的安全组。

  • 硬件SDN环境不支持ip_address属性配置为CIDR格式。

取值范围

不涉及。

默认取值

不涉及。

表4 AllowedAddressPair

参数

是否必选

参数类型

描述

ip_address

String

参数解释

IP地址。

约束限制

如果allowed_address_pairs配置地址池较大的IP网段(掩码小于24位),建议为该网卡配置一个单独的安全组。

取值范围

单个IP地址:例如192.168.21.25。

IP网段:例如192.168.21.0/24。

默认取值

不涉及。

mac_address

String

参数解释

MAC地址。

约束限制

不涉及。

取值范围

不涉及。

默认取值

不涉及。

响应参数

状态码:201

表5 响应Body参数

参数

参数类型

描述

request_id

String

参数解释

请求ID。

取值范围

不涉及。

sub_network_interface

SubNetworkInterface object

参数解释

创建辅助弹性网卡的响应体。

取值范围

不涉及。

表6 SubNetworkInterface

参数

参数类型

描述

id

String

参数解释

辅助弹性网卡的资源ID。辅助弹性网卡创建成功后,会生成一个辅助弹性网卡 ID,是辅助弹性网卡对应的唯一标识。

取值范围

带“-”的标准UUID格式。

virsubnet_id

String

参数解释

辅助弹性网卡所在的虚拟子网ID。

取值范围

带“-”的标准UUID格式。

private_ip_address

String

参数解释

辅助弹性网卡的私有IPv4地址。

取值范围

不涉及。

ipv6_ip_address

String

参数解释

辅助弹性网卡的私有IPv6地址。

取值范围

不涉及。

mac_address

String

参数解释

辅助弹性网卡的MAC地址。

取值范围

合法的MAC地址,系统随机分配。

parent_device_id

String

参数解释

辅助弹性网卡的宿主网卡所属的设备ID。

取值范围

带“-”的标准UUID格式。

parent_id

String

参数解释

辅助弹性网卡所挂载的弹性网卡的ID。

取值范围

带“-”的标准UUID格式。

description

String

参数解释

辅助弹性网卡的描述信息。

取值范围

0-255个字符,不能包含“<”和“>”。

vpc_id

String

参数解释

辅助弹性网卡所属VPC的ID。

取值范围

带“-”的标准UUID格式。

vlan_id

Integer

参数解释

辅助弹性网卡的VLAN ID。

取值范围

1-4094

security_groups

Array of strings

参数解释

辅助弹性网卡关联的安全组的ID列表。例如:"security_groups": ["a0608cbf-d047-4f54-8b28-cd7b59853fff"]。

取值范围

如果请求时不指定此参数,辅助弹性网卡创建后会自动关联默认安全组。

tags

Array of ResponseTag objects

参数解释

辅助弹性网卡的标签信息,包括标签键和标签值,可用来分类和标识资源。详情请参见Tag对象。

取值范围

不涉及。

project_id

String

参数解释

辅助弹性网卡所属的项目ID。

取值范围

不涉及。

created_at

String

参数解释

辅助弹性网卡的创建时间。

取值范围

UTC时间格式,yyyy-MM-ddTHH:mm:ssZ。

allowed_address_pairs

Array of AllowedAddressPair objects

参数解释

辅助弹性网卡的IP/Mac对列表,详情请参见“AllowedAddressPair”对象表。

取值范围

不涉及。

state

String

参数解释

辅助弹性网卡的状态。

取值范围

  • NORMAL:表示辅助弹性网卡已挂载在弹性网卡上。

  • UNBOUND:表示辅助弹性网卡未挂载在弹性网卡上。

instance_id

String

参数解释

辅助弹性网卡所属的云服务实例ID,例如RDS实例ID。

取值范围

不涉及。

instance_type

String

参数解释

辅助弹性网卡所属的云服务实例类型,例如“RDS”。

取值范围

不涉及。

scope

String

参数解释

辅助弹性网卡所在站点的公网出口信息。

取值范围

  • center:默认值,表示作用域为中心。

  • 某个AZ ID:表示作用域为具体的AZ。

security_enabled

Boolean

参数解释

辅助弹性网卡安全使能标记,如果不使能则安全组不生效。

取值范围

不涉及。

表7 ResponseTag

参数

参数类型

描述

key

String

参数解释

标签键。

取值范围

  • 最大长度128个unicode字符, key不能为空。

  • 同一资源的key不能重复。

  • 可以包含的字符范围:

    • 英文字母

    • 数字

    • 特殊字符:下划线(_)、中划线(-)

    • 中文字符

value

String

参数解释

标签值。

取值范围

  • 每个值最大长度255个unicode字符,value可以为空。

  • 可以包含的字符范围:

    • 英文字母

    • 数字

    • 特殊字符:下划线(_)、中划线(-)、点(.)

    • 中文字符

表8 AllowedAddressPair

参数

参数类型

描述

ip_address

String

参数解释

IP地址。

约束限制

如果allowed_address_pairs配置地址池较大的IP网段(掩码小于24位),建议为该网卡配置一个单独的安全组。

取值范围

单个IP地址:例如192.168.21.25。

IP网段:例如192.168.21.0/24。

默认取值

不涉及。

mac_address

String

参数解释

MAC地址。

约束限制

不涉及。

取值范围

不涉及。

默认取值

不涉及。

请求示例

创建辅助弹性网卡,虚拟子网id为08278e6c-61ca-46c1-9fc3-0d4f6c12f193,宿主弹性网卡id为637748df-2986-4350-8303-95d259580fb3,关联安全组6727c950-9f01-47a2-a7aa-7d3686c4c95b。

POST https://{Endpoint}/v3/8c6fb137a48a428aaf9a0229dca4edb3/vpc/sub-network-interfaces

{
  "sub_network_interface" : {
    "virsubnet_id" : "08278e6c-61ca-46c1-9fc3-0d4f6c12f193",
    "parent_id" : "637748df-2986-4350-8303-95d259580fb3",
    "security_groups" : [ "6727c950-9f01-47a2-a7aa-7d3686c4c95b" ]
  }
}

响应示例

状态码:201

POST操作正常返回,更多状态码请参见状态码

{
  "sub_network_interface" : {
    "id" : "1f5fca65-6a75-47c8-942b-db1bbb81a22a",
    "project_id" : "8c6fb137a48a428aaf9a0229dca4edb3",
    "virsubnet_id" : "08278e6c-61ca-46c1-9fc3-0d4f6c12f193",
    "private_ip_address" : "192.168.0.219",
    "ipv6_ip_address" : "2407:c080:1200:2038:53a4:ad1c:ef34:f4ef",
    "mac_address" : "fa:16:3e:26:7e:59",
    "parent_device_id" : "285b2e45-2968-4f4c-8992-54e0d4c8da1d",
    "security_enabled" : true,
    "security_groups" : [ "6727c950-9f01-47a2-a7aa-7d3686c4c95b" ],
    "allowed_address_pairs" : [ ],
    "vpc_id" : "c10e1e02-9db2-4bd0-89ef-52536952627e",
    "instance_id" : "",
    "instance_type" : "",
    "description" : "",
    "parent_id" : "637748df-2986-4350-8303-95d259580fb3",
    "vlan_id" : 551,
    "state" : "NORMAL",
    "tags" : [ ],
    "scope" : "center",
    "created_at" : "2025-02-10T08:17:16Z"
  },
  "request_id" : "f0893730b622bf32beea6fa5b387a5dc"
}

SDK代码示例

SDK代码示例如下。

创建辅助弹性网卡,虚拟子网id为08278e6c-61ca-46c1-9fc3-0d4f6c12f193,宿主弹性网卡id为637748df-2986-4350-8303-95d259580fb3,关联安全组6727c950-9f01-47a2-a7aa-7d3686c4c95b。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package com.huaweicloud.sdk.test;

import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.vpc.v3.region.VpcRegion;
import com.huaweicloud.sdk.vpc.v3.*;
import com.huaweicloud.sdk.vpc.v3.model.*;

import java.util.List;
import java.util.ArrayList;

public class CreateSubNetworkInterfaceSolution {

    public static void main(String[] args) {
        // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
        // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
        String ak = System.getenv("CLOUD_SDK_AK");
        String sk = System.getenv("CLOUD_SDK_SK");
        String projectId = "{project_id}";

        ICredential auth = new BasicCredentials()
                .withProjectId(projectId)
                .withAk(ak)
                .withSk(sk);

        VpcClient client = VpcClient.newBuilder()
                .withCredential(auth)
                .withRegion(VpcRegion.valueOf("<YOUR REGION>"))
                .build();
        CreateSubNetworkInterfaceRequest request = new CreateSubNetworkInterfaceRequest();
        CreateSubNetworkInterfaceRequestBody body = new CreateSubNetworkInterfaceRequestBody();
        List<String> listSubNetworkInterfaceSecurityGroups = new ArrayList<>();
        listSubNetworkInterfaceSecurityGroups.add("6727c950-9f01-47a2-a7aa-7d3686c4c95b");
        CreateSubNetworkInterfaceOption subNetworkInterfacebody = new CreateSubNetworkInterfaceOption();
        subNetworkInterfacebody.withVirsubnetId("08278e6c-61ca-46c1-9fc3-0d4f6c12f193")
            .withParentId("637748df-2986-4350-8303-95d259580fb3")
            .withSecurityGroups(listSubNetworkInterfaceSecurityGroups);
        body.withSubNetworkInterface(subNetworkInterfacebody);
        request.withBody(body);
        try {
            CreateSubNetworkInterfaceResponse response = client.createSubNetworkInterface(request);
            System.out.println(response.toString());
        } catch (ConnectionException e) {
            e.printStackTrace();
        } catch (RequestTimeoutException e) {
            e.printStackTrace();
        } catch (ServiceResponseException e) {
            e.printStackTrace();
            System.out.println(e.getHttpStatusCode());
            System.out.println(e.getRequestId());
            System.out.println(e.getErrorCode());
            System.out.println(e.getErrorMsg());
        }
    }
}

创建辅助弹性网卡,虚拟子网id为08278e6c-61ca-46c1-9fc3-0d4f6c12f193,宿主弹性网卡id为637748df-2986-4350-8303-95d259580fb3,关联安全组6727c950-9f01-47a2-a7aa-7d3686c4c95b。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# coding: utf-8

import os
from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkvpc.v3.region.vpc_region import VpcRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkvpc.v3 import *

if __name__ == "__main__":
    # The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    # In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak = os.environ["CLOUD_SDK_AK"]
    sk = os.environ["CLOUD_SDK_SK"]
    projectId = "{project_id}"

    credentials = BasicCredentials(ak, sk, projectId)

    client = VpcClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(VpcRegion.value_of("<YOUR REGION>")) \
        .build()

    try:
        request = CreateSubNetworkInterfaceRequest()
        listSecurityGroupsSubNetworkInterface = [
            "6727c950-9f01-47a2-a7aa-7d3686c4c95b"
        ]
        subNetworkInterfacebody = CreateSubNetworkInterfaceOption(
            virsubnet_id="08278e6c-61ca-46c1-9fc3-0d4f6c12f193",
            parent_id="637748df-2986-4350-8303-95d259580fb3",
            security_groups=listSecurityGroupsSubNetworkInterface
        )
        request.body = CreateSubNetworkInterfaceRequestBody(
            sub_network_interface=subNetworkInterfacebody
        )
        response = client.create_sub_network_interface(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

创建辅助弹性网卡,虚拟子网id为08278e6c-61ca-46c1-9fc3-0d4f6c12f193,宿主弹性网卡id为637748df-2986-4350-8303-95d259580fb3,关联安全组6727c950-9f01-47a2-a7aa-7d3686c4c95b。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main

import (
	"fmt"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
    vpc "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v3"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v3/model"
    region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/vpc/v3/region"
)

func main() {
    // The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
    // In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
    ak := os.Getenv("CLOUD_SDK_AK")
    sk := os.Getenv("CLOUD_SDK_SK")
    projectId := "{project_id}"

    auth := basic.NewCredentialsBuilder().
        WithAk(ak).
        WithSk(sk).
        WithProjectId(projectId).
        Build()

    client := vpc.NewVpcClient(
        vpc.VpcClientBuilder().
            WithRegion(region.ValueOf("<YOUR REGION>")).
            WithCredential(auth).
            Build())

    request := &model.CreateSubNetworkInterfaceRequest{}
	var listSecurityGroupsSubNetworkInterface = []string{
        "6727c950-9f01-47a2-a7aa-7d3686c4c95b",
    }
	subNetworkInterfacebody := &model.CreateSubNetworkInterfaceOption{
		VirsubnetId: "08278e6c-61ca-46c1-9fc3-0d4f6c12f193",
		ParentId: "637748df-2986-4350-8303-95d259580fb3",
		SecurityGroups: &listSecurityGroupsSubNetworkInterface,
	}
	request.Body = &model.CreateSubNetworkInterfaceRequestBody{
		SubNetworkInterface: subNetworkInterfacebody,
	}
	response, err := client.CreateSubNetworkInterface(request)
	if err == nil {
        fmt.Printf("%+v\n", response)
    } else {
        fmt.Println(err)
    }
}

更多编程语言的SDK代码示例,请参见API Explorer的代码示例页签,可生成自动对应的SDK代码示例。

状态码

状态码

描述

201

POST操作正常返回,更多状态码请参见状态码

错误码

请参见错误码