本文展示了如何通过调用阿里云ECS Java SDK来创建弹性网卡。
注意事项
调用该接口时,请注意:
该接口为同步接口,新创建的弹性网卡为可用(Available)状态,可以直接进行附加实例等操作。
若创建接口返回结果中networkInterfaceId为空,您可以使用相同ClientToken再次尝试创建弹性网卡。
示例代码
以下示例适用于在指定交换机和安全组下创建弹性网卡。
说明
当前示例基于阿里云SDK V2.0版本编写。
import java.util.UUID;
import com.aliyun.ecs20140526.Client;
import com.aliyun.ecs20140526.models.CreateNetworkInterfaceRequest;
import com.aliyun.ecs20140526.models.CreateNetworkInterfaceResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
public class CreateNetworkInterfaceExample {
/**
* 使用AK&SK初始化账号Client。
*
* 本示例通过从环境变量中读取AccessKey,来实现API访问的身份验证。请根据你的生产环境要求适当调整。
* 避免AK&SK等关键信息在代码中明文存储是云上安全红线!
*
* @return Client
* @throws Exception
*/
public static Client createClient() throws Exception {
Config config = new Config()
.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"))
.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
config.endpoint = "<yourEcsEndPoint>";
return new Client(config);
}
public static String CreateNetworkInterface(Client client, String regionId, String vSwitchId,
String securityGroupId, String name, String description) {
try {
CreateNetworkInterfaceRequest request = new CreateNetworkInterfaceRequest();
request.setRegionId(regionId);
request.setVSwitchId(vSwitchId);
request.setSecurityGroupId(securityGroupId);
request.setNetworkInterfaceName(name);
request.setDescription(description);
request.setClientToken(UUID.randomUUID().toString());
//用来设置个性化的策略
RuntimeOptions runtime = new RuntimeOptions();
CreateNetworkInterfaceResponse response = client.createNetworkInterfaceWithOptions(request, runtime);
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()));
return response.getBody().getNetworkInterfaceId();
}
} 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());
}
return null;
}
public static void main(String[] args) throws Exception {
String regionId = "<regionId>";
String vSwitchId = "<yourVSwitchId>";
String securityGroupId = "<yourSecurityGroupId>";
Client client = createClient();
CreateNetworkInterface(client, regionId, vSwitchId, securityGroupId, "test-eni", "description");
}
}
执行结果
{
"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 } ]
}
}
相关文档
反馈
- 本页导读 (1)
文档反馈