创建弹性网卡

本文展示了如何通过调用阿里云ECS Java SDK来创建弹性网卡。

注意事项

调用该接口时,请注意:

  • 该接口为同步接口,新创建的弹性网卡为可用(Available)状态,可以直接进行附加实例等操作。

  • 若创建接口返回结果中networkInterfaceId为空,请再次尝试创建弹性网卡。

示例代码

以下示例适用于在指定交换机和安全组下创建弹性网卡。

import com.aliyun.ecs20140526.Client;
import com.aliyun.ecs20140526.models.CreateNetworkInterfaceRequest;
import com.aliyun.ecs20140526.models.CreateNetworkInterfaceResponse;
import com.aliyun.tea.TeaException;

public class CreateNetworkInterfaceExample {

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.ecs20140526.Client createClient(String accessKeyId, String accessKeySecret,
        String endPoint) throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
            // 必填,您的 AccessKey ID
            .setAccessKeyId(accessKeyId)
            // 必填,您的 AccessKey Secret
            .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = endPoint;
        return new com.aliyun.ecs20140526.Client(config);
    }

    public static void main(String[] args) {
        String accessId = "<yourAccessId>";
        String accessKey = "<yourAccessKey>";
        String ecsEndpoint = "<yourEcsEndPoint>";
        String regionId = "<regionId>";
        String vSwitchId = "<yourVSwitchId>";
        String securityGroupId = "<yourSecurityGroupId>";

        try {
            CreateNetworkInterfaceRequest request = new CreateNetworkInterfaceRequest();
            request.setRegionId(regionId);
            request.setVSwitchId(vSwitchId);
            request.setSecurityGroupId(securityGroupId);
            request.setNetworkInterfaceName("test-eni-name");
            request.setDescription("test");
            Client client = CreateNetworkInterfaceExample.createClient(accessId, accessKey, ecsEndpoint);
            CreateNetworkInterfaceResponse response = client.createNetworkInterface(request);
            if ("Available".equals(response.getBody().getStatus())) {
                System.out.println(String.format("Instance.CreateNetworkInterface success, eni id:%s. Request is is %s.%n",
                    response.getBody().getNetworkInterfaceId(), response.getBody().getRequestId()));
            }
        } catch (TeaException te) {
            System.out.println("ErrCode:" + te.getCode());
            System.out.println("ErrMsg:" + te.getMessage());
            System.out.println("RequestId:" + te.getData().get("RequestId"));
        } catch (Exception e) {
            System.out.println("ErrCode:" + e.getMessage());
        }

    }
}

执行结果

{
 "Description" : "testDescription",
 "Status" : "Available",
 "PrivateIpAddress" : "172.17.**.**",
 "ServiceManaged" : false,
 "RequestId" : "473469C7-AA6F-4DC5-B3DB-A3DC0DE3C83E",
 "ResourceGroupId" : "rg-2ze88m67qx5z****",
 "ZoneId" : "cn-hangzhou-e",
 "VSwitchId" : "vsw-bp16usj2p27htro3****",
 "NetworkInterfaceName" : "my-eni-name",
 "MacAddress" : "00:16:3e:12:**:**",
 "NetworkInterfaceId" : "eni-bp14v2sdd3v8htln****",
 "SecurityGroupIds" : {
 "SecurityGroupId" : [ "sg-bp18kz60mefsicfg****" ]
 },
 "Type" : "Secondary",
 "Ipv6Sets" : {
 "Ipv6Set" : [ {
 "Ipv6Address" : "2001:db8:1234:1a00::****" } ]
 },
 "VpcId" : "vpc-bp1j7w3gc1cexjqd****",
 "OwnerId" : "123456****",
 "Tags" : {
 "Tag" : [ {
 "TagKey" : "TestKey",
 "TagValue" : "TestValue" } ]
 },
 "PrivateIpSets" : {
 "PrivateIpSet" : [ {
 "PrivateIpAddress" : "172.17.**.**",
 "Primary" : true } ]
 }
}

相关文档

CreateNetworkInterface

阿里云首页 云服务器 ECS 相关技术圈