弹性网卡
初始化
确认Endpoint
在确认您使用SDK时配置的Endpoint时,可先阅读开发人员指南中关于VPC服务域名的部分,理解Endpoint相关的概念。百度云目前开放了多区域支持,请参考区域选择说明。
目前支持“华北-北京”、“华南-广州”、“华东-苏州”、“香港”、“金融华中-武汉”和“华北-保定”六个区域。对应信息为:
| 访问区域 | 对应Endpoint | 协议 | 
|---|---|---|
| BJ | bcc.bj.baidubce.com | HTTP and HTTPS | 
| GZ | bcc.gz.baidubce.com | HTTP and HTTPS | 
| SU | bcc.su.baidubce.com | HTTP and HTTPS | 
| HKG | bcc.hkg.baidubce.com | HTTP and HTTPS | 
| FWH | bcc.fwh.baidubce.com | HTTP and HTTPS | 
| BD | bcc.bd.baidubce.com | HTTP and HTTPS | 
获取密钥
要使用百度云路由,您需要拥有一个有效的AK(Access Key ID)和SK(Secret Access Key)用来进行签名认证。AK/SK是由系统分配给用户的,均为字符串,用于标识用户,为访问Route做签名验证。
可以通过如下步骤获得并了解您的AK/SK信息:
创建弹性网卡
- 可指定IPv4和IPv6内网IP,指定的内网IP必须在弹性网卡所在子网内,而且不能被占用。
- 指定的IPv4 IP中只能有一个是主IP,其他必须是辅助IP。
- 指定IPv6 IP时,需弹性网卡所在子网已分配IPv6网段,目前IPv6 IP均为辅助IP。
- privateIpAddress为””表示自动分配IP地址。
- 创建弹性网卡同时可以绑定已有普通安全组或企业安全组(二选一),普通安全组所属VPC必须与网卡所属的VPC一致。
函数声明
1type PrivateIp struct {
2    PublicIpAddress  string `json:"publicIpAddress"`
3    Primary          bool   `json:"primary"`
4    PrivateIpAddress string `json:"privateIpAddress"`
5}
6type CreateEniArgs struct {
7    ClientToken                string      `json:"-"`
8    Name                       string      `json:"name"`
9    SubnetId                   string      `json:"subnetId"`
10    InstanceId                 string      `json:"instanceId,omitempty"`
11    SecurityGroupIds           []string    `json:"securityGroupIds"`
12    EnterpriseSecurityGroupIds []string    `json:"enterpriseSecurityGroupIds"`
13    PrivateIpSet               []PrivateIp `json:"privateIpSet"`
14    Ipv6PrivateIpSet           []PrivateIp `json:"ipv6PrivateIpSet,omitempty"`
15    Description                string      `json:"description,omitempty"`
16}
17type CreateEniResult struct {
18    EniId string `json:"eniId"`
19}
20func (c *Client) CreateEni(args *CreateEniArgs) (*CreateEniResult, error) 参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/pkknfara0
返回值
操作成功:
1{
2    "eniId":"eni-2dn7v328tegp"
3}操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_create_eni.go
更新弹性网卡
更新弹性网卡名称和描述。
- 名称为大小写字母、数字以及-_/.特殊字符,必须以字母开头,长度1-65
- 名称和描述二者不能同时为空
函数声明
1type UpdateEniArgs struct {
2    EniId       string `json:"-"`
3    ClientToken string `json:"-"`
4    Name        string `json:"name"`
5    Description string `json:"description"`
6}
7func (c *Client) UpdateEni(args *UpdateEniArgs) error参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/Zkknfh5h8
返回值
操作成功:
1无特殊返回参数操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_update_eni.go
删除弹性网卡
- 已挂载、挂载中、卸载中的弹性网卡不能删除。
- 删除之后弹性网卡上所有内网IP都将被退还。
函数声明
1type DeleteEniArgs struct {
2    EniId       string
3    ClientToken string
4}
5func (c *Client) DeleteEni(args *DeleteEniArgs) error参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/ikknfinx7
返回值
操作成功:
1无特殊返回参数操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_delete_eni.go
查询弹性网卡列表
函数声明
1type ListEniArgs struct {
2    VpcId            string
3    InstanceId       string
4    Name             string
5    Marker           string
6    MaxKeys          int
7    PrivateIpAddress []string `json:"privateIpAddress,omitempty"`
8}
9type ListEniResult struct {
10    Eni         []Eni  `json:"enis"`
11    Marker      string `json:"marker"`
12    IsTruncated bool   `json:"isTruncated"`
13    NextMarker  string `json:"nextMarker"`
14    MaxKeys     int    `json:"maxKeys"`
15}
16func (c *Client) ListEni(args *ListEniArgs) (*ListEniResult, error)参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/Okknfjt6o
返回值
操作成功:
1{
2    "enis":[
3    {
4        "eniId":"eni-tnj00he350fh",
5        "name":"eni-1",
6        "zoneName":"cn-bj-d",
7        "description":"",
8        "instanceId":"",
9        "macAddress":"fa:20:20:2f:c6:30",
10        "vpcId":"vpc-jm7h2j497ut7",
11        "subnetId":"sbn-d63m7t0bbwt5",
12        "status":"available",
13        "privateIpSet":[
14        {
15            "publicIpAddress":"",
16            "primary":true,
17            "privateIpAddress":"10.0.1.115"
18        },
19        {
20            "publicIpAddress":"",
21            "primary":false,
22            "privateIpAddress":"10.0.1.6"
23        }],
24        "ipv6PrivateIpSet":[],
25        "securityGroupIds":null,
26        "enterpriseSecurityGroupIds":null,
27        "createdTime":"2023-12-06T06:32:46Z"
28    },
29    {
30        "eniId":"eni-2dn7v328tegp",
31        "name":"eni-1",
32        "zoneName":"cn-bj-d",
33        "description":"",
34        "instanceId":"",
35        "macAddress":"fa:20:20:30:08:08",
36        "vpcId":"vpc-jm7h2j497ut7",
37        "subnetId":"sbn-d63m7t0bbwt5",
38        "status":"available",
39        "privateIpSet":[
40        {
41            "publicIpAddress":"",
42            "primary":true,
43            "privateIpAddress":"10.0.1.116"
44        },
45        {
46            "publicIpAddress":"",
47            "primary":false,
48            "privateIpAddress":"10.0.1.7"
49        }],
50        "ipv6PrivateIpSet":[],
51        "securityGroupIds":null,
52        "enterpriseSecurityGroupIds":null,
53        "createdTime":"2023-12-06T07:07:10Z"
54    }],
55    "marker":"eni-tnj00he350fh",
56    "isTruncated":true,
57    "nextMarker":"eni-ziqsx63b3nfq",
58    "maxKeys":2
59}操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_list_eni.go
增加弹性网卡内网IP
- isIpv6为true时,表示新增IPv6内网IP,弹性网卡所在子网需分配IPv6网段。
- 如果privateIpAddress为""表示自动分配。
- 若privateIpAddress不为空,则必须为该子网内的IP地址。
- 新增的内网IP都是辅助IP。
函数声明
1type EniPrivateIpArgs struct {
2    EniId            string `json:"-"`
3    ClientToken      string `json:"-"`
4    IsIpv6           bool   `json:"isIpv6,omitempty"`
5    PrivateIpAddress string `json:"privateIpAddress"`
6}
7type AddPrivateIpResult struct {
8    PrivateIpAddress string `json:"privateIpAddress"`
9}
10func (c *Client) AddPrivateIp(args *EniPrivateIpArgs) (*AddPrivateIpResult, error)参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/Hkknfl4q3
返回值
操作成功:
1{
2    "privateIpAddress":"10.0.1.108"
3}操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_add_private_ip.go
删除弹性网卡内网IP
- 主IP不能删除,仅可删除辅助IP。
- 可指定IPv4或IPv6内网IP删除。
函数声明
1type EniPrivateIpArgs struct {
2    EniId            string `json:"-"`
3    ClientToken      string `json:"-"`
4    IsIpv6           bool   `json:"isIpv6,omitempty"`
5    PrivateIpAddress string `json:"privateIpAddress"`
6}
7func (c *Client) DeletePrivateIp(args *EniPrivateIpArgs) error参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/jkknfm534
返回值
操作成功:
1无特殊返回参数操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_delete_private_ip.go
查询指定的弹性网卡
函数声明
1type Eni struct {
2    EniId                      string      `json:"eniId"`
3    Name                       string      `json:"name"`
4    ZoneName                   string      `json:"zoneName"`
5    Description                string      `json:"description"`
6    InstanceId                 string      `json:"instanceId"`
7    MacAddress                 string      `json:"macAddress"`
8    VpcId                      string      `json:"vpcId"`
9    SubnetId                   string      `json:"subnetId"`
10    Status                     string      `json:"status"`
11    PrivateIpSet               []PrivateIp `json:"privateIpSet"`
12    Ipv6PrivateIpSet           []PrivateIp `json:"ipv6PrivateIpSet"`
13    SecurityGroupIds           []string    `json:"securityGroupIds"`
14    EnterpriseSecurityGroupIds []string    `json:"enterpriseSecurityGroupIds"`
15    CreatedTime                string      `json:"createdTime"`
16}
17func (c *Client) GetEniDetail(eniId string) (*Eni, error)参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/6kknfn5m8
返回值
操作成功:
1{
2    "eniId":"eni-477g9akswgjv",
3    "name":"GO_SDK_TEST_UPDATE",
4    "zoneName":"cn-bj-d",
5    "description":"go sdk update",
6    "instanceId":"",
7    "macAddress":"fa:20:20:2f:b2:ae",
8    "vpcId":"vpc-jm7h2j497ut7",
9    "subnetId":"sbn-d63m7t0bbwt5",
10    "status":"available",
11    "privateIpSet":[
12    {
13        "publicIpAddress":"",
14        "primary":true,
15        "privateIpAddress":"10.0.1.100"
16    }],
17    "ipv6PrivateIpSet":[
18    {
19        "publicIpAddress":"",
20        "primary":false,
21        "privateIpAddress":"240c:4081:8005:5e04::2"
22    }],
23    "securityGroupIds":[],
24    "enterpriseSecurityGroupIds":["esg-1atxb1iqd1e2"],
25    "createdTime":"2023-11-24T11:39:54Z"
26}操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_get_eni_detail.go
弹性网卡挂载云主机
- 一个云主机可以绑定多个弹性网卡,但只能绑定一个主网卡。
- 一个弹性网卡只能同时绑定一个云主机。
- 只有运行中或者已关机状态的云主机才能绑定弹性网卡。
- 弹性网卡和绑定的云主机必须是在同一私有网络内,且两者所在子网的可用区相同。
函数声明
1type EniInstance struct {
2    EniId       string `json:"-"`
3    InstanceId  string `json:"instanceId"`
4    ClientToken string `json:"-"`
5}
6func (c *Client) AttachEniInstance(args *EniInstance) error参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/qkknfp44w
返回值
操作成功:
1无特殊返回参数操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_attach_eni_instance.go
弹性网卡卸载云主机
- 只有已挂载状态的弹性网卡可以解绑云主机。
函数声明
1type EniInstance struct {
2    EniId       string `json:"-"`
3    InstanceId  string `json:"instanceId"`
4    ClientToken string `json:"-"`
5}
6func (c *Client) DetachEniInstance(args *EniInstance) error参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/jkknfq87r
返回值
操作成功:
1无特殊返回参数操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_detach_eni_instance.go
弹性网卡绑定EIP
- 挂载中和卸载中的网卡不能绑定EIP。
- NAT专属子网中的网卡不能绑定EIP。
函数声明
1type BindEniPublicIpArgs struct {
2    EniId            string `json:"-"`
3    ClientToken      string `json:"-"`
4    PrivateIpAddress string `json:"privateIpAddress"`
5    PublicIpAddress  string `json:"publicIpAddress"`
6}
7func (c *Client) BindEniPublicIp(args *BindEniPublicIpArgs) error参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/vkknfr857
返回值
操作成功:
1无特殊返回参数操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_bind_eni_public_ip.go
弹性网卡解绑EIP
- 挂载中和卸载中的网卡不能解绑EIP。
函数声明
1type UnBindEniPublicIpArgs struct {
2    EniId           string `json:"-"`
3    ClientToken     string `json:"-"`
4    PublicIpAddress string `json:"publicIpAddress"`
5}
6func (c *Client) UnBindEniPublicIp(args *UnBindEniPublicIpArgs) error参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/Hkknfs7ee
返回值
操作成功:
1无特殊返回参数操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_unbind_public_ip.go
弹性网卡更新普通安全组
- 弹性网卡至少要有一个安全组(普通安全组或企业安全组)。
- 该操作是弹性网卡最终加入的普通安全组,并会移出已有的普通安全组或企业安全组。
函数声明
1type UpdateEniSecurityGroupArgs struct {
2    EniId            string   `json:"-"`
3    ClientToken      string   `json:"-"`
4    SecurityGroupIds []string `json:"securityGroupIds"`
5}
6func (c *Client) UpdateEniSecurityGroup(args *UpdateEniSecurityGroupArgs) error 参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/Nkknft2l0
返回值
操作成功:
1无特殊返回参数操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_update_eni_sg.go
弹性网卡更新企业安全组
- 弹性网卡至少要有一个安全组(普通安全组或企业安全组)。
- 该操作是弹性网卡最终加入的企业安全组,并会移出已有的普通安全组或企业安全组。
函数声明
1type UpdateEniEnterpriseSecurityGroupArgs struct {
2    EniId                      string   `json:"-"`
3    ClientToken                string   `json:"-"`
4    EnterpriseSecurityGroupIds []string `json:"enterpriseSecurityGroupIds"`
5}
6func (c *Client) UpdateEniEnterpriseSecurityGroup(args *UpdateEniEnterpriseSecurityGroupArgs) error参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/Jl5kp9uyw
返回值
操作成功:
1无特殊返回参数操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_update_eni_esg.go
批量删除弹性网卡内网IP
- 主IP不能删除,仅可删除辅助IP。
- 可指定IPv4或IPv6内网IP删除。
函数声明
1type EniBatchPrivateIpArgs struct {
2    EniId                 string   `json:"-"`
3    ClientToken           string   `json:"-"`
4    IsIpv6                bool     `json:"isIpv6,omitempty"`
5    PrivateIpAddresses    []string `json:"privateIpAddresses"`
6    PrivateIpAddressCount int      `json:"privateIpAddressCount,omitempty"`
7}
8func (c *Client) BatchDeletePrivateIp(args *EniBatchPrivateIpArgs) error参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/Akw4yd4ht
返回值
操作成功:
1无特殊返回参数操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_batch_delete_private_ip.go
批量增加弹性网卡内网IP
- 新增的内网IP都是辅助IP。
- isIpv6为true时,表示新增IPv6内网IP,弹性网卡所在子网需分配IPv6网段。
函数声明
1type EniBatchPrivateIpArgs struct {
2    EniId                 string   `json:"-"`
3    ClientToken           string   `json:"-"`
4    IsIpv6                bool     `json:"isIpv6,omitempty"`
5    PrivateIpAddresses    []string `json:"privateIpAddresses"`
6    PrivateIpAddressCount int      `json:"privateIpAddressCount,omitempty"`
7}
8type BatchAddPrivateIpResult struct {
9    PrivateIpAddresses []string `json:"privateIpAddresses"`
10}
11func (c *Client) BatchAddPrivateIp(args *EniBatchPrivateIpArgs) (*BatchAddPrivateIpResult, error)参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/9kw4xi713
返回值
操作成功:
1{
2    "privateIpAddresses": ["10.0.1.201","10.0.1.202"]
3}操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_batch_add_private_ip.go
查询弹性网卡状态
函数声明
1type EniStatusInfo struct {
2    Status string `json:"status"`
3}
4func (c *Client) GetEniStatus(eniId string) (*EniStatusInfo, error)
5 参数含义
请参考OpenAPI文档:https://cloud.baidu.com/doc/VPC/s/alk0j86xp
返回值
操作成功:
1{
2    "status": "available"
3}操作失败:
抛出异常,异常列表参考:https://cloud.baidu.com/doc/VPC/s/sjwvyuhe7#弹性网卡业务错误码
代码示例
具体代码示例参考:example_get_eni_status.go
