vpc
获取Endpoint
在确认您使用SDK时配置的Endpoint时,可先阅读开发人员指南中关于VPC服务域名的部分,理解Endpoint相关的概念。 百度智能云目前开放了多区域支持,请参考区域选择说明中网络产品VPC的部分。
注意: VPC API支持HTTP和HTTPS两种调用方式。为了提升数据的安全性,建议通过HTTPS调用
获取AK/SK
要使用百度智能云VPC,您需要拥有一个有效的 AK(Access Key ID)和SK(Secret Access Key)用来进行签名认证。AK/SK是由系统分配给用户的,均为字符串,用于标识用户,为访问服务做签名验证。 可以通过如下步骤获得并了解您的AK/SK信息:
新建VpcClient
VpcClient是VPC服务的客户端,为开发者与VPC服务进行交互提供了一系列的方法。 新建VpcClient时,需要先使用Endpoint、AK、SK对BceClientConfigurationl类型的config实例进行配置,再使用config实例对VpcClient进行配置,具体配置方法如下:
static final String ENDPOINT = "";
static final String AK = "";
static final String SK = "";
VpcClientConfiguration config = new VpcClientConfiguration();
config.setCredentials(new DefaultBceCredentials(AK, SK));
config.setEndpoint(ENDPOINT);
VpcClient vpcClient = new VpcClient(config);
创建VPC
函数定义如下:
/**
* Create a vpc with the specified options.
*
* @param name The name of vpc
* @param cidr The CIDR of vpc
* @return List of vpcId newly created
*/
public CreateVpcResponse createVpc(String name, String cidr) {
......
}
/**
* Create a vpc with the specified options.
* You must fill the field of clientToken,which is especially for keeping idempotent.
*
* @param request The request containing all options for creating a vpc.
* @return List of vpcId newly created
* @throws BceClientException
*/
public CreateVpcResponse createVpc(CreateVpcRequest request)
throws BceClientException {
......
}
注意:
第一个接口只支持少量常用参数
第二个接口可以支持较多参数,但需要创建request实例
参数说明如下:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
name | String | 是 | vpc名称,不能取值“default”,长度不超过65个字符,可由数字、字符、下划线组成 |
cidr | String | 是 | vpc的cidr |
description | String | 否 | vpc描述,不超过200字符 |
使用示例如下:
vpcClient.createVpc("vpc_test_2", "192.168.0.0/16");
列举VPC
函数定义如下:
/**
* Return a list of vpcs owned by the authenticated user.
*
* @return The response containing a list of vpcs owned by the authenticated user.
*/
public ListVpcResponse listVpcs() {
......
}
/**
* Return a list of vpcs owned by the authenticated user.
*
* @param request The request containing all options for listing own's vpc.
* @return The response containing a list of vpcs owned by the authenticated user.
*/
public ListVpcResponse listVpcs(ListVpcRequest request) {
......
}
注意:
第一个接口只支持少量常用参数
第二个接口可以支持较多参数,但需要创建request实例
参数说明如下:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
marker | String | 否 | 批量获取列表的查询的起始位置,是一个由系统生成的字符串 |
maxKeys | Integer | 否 | 每页包含的最大数量,最大数量通常不超过1000,缺省值为1000 |
isDefault | Boolean | 否 | 是否为默认vpc,可选值:True、False;当不填写此参数时返回所有vpc |
使用示例如下:
vpcClient.listVpcs();
查询VPC
函数定义如下:
/**
* Get the detail information of specified vpc.
*
* @param vpcId The id of the network.
* @return A vpc detail model for the vpcId.
*/
public GetVpcResponse getVpc(String vpcId) {
/**
* Get the detail information of specified vpc.
*
* @param getVpcRequest The request containing all options for getting the vpc info.
* @return A vpc detail model for the vpcId.
*/
public GetVpcResponse getVpc(GetVpcRequest getVpcRequest) {
......
}
注意:
第一个接口只支持少量常用参数
第二个接口可以支持较多参数,但需要创建request实例
参数说明如下:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
vpcId | String | 是 | 待查询vpc的id |
使用示例如下:
vpcClient.getVpc("vpc-7tay6jx1uj5r");
更新VPC
函数定义如下:
/**
* Modifying the special attribute to new value of the vpc owned by the user.
* @param name The name of the vpc after modifying
* @param vpcId the id of the vpc
*/
public void modifyInstanceAttributes(String name, String vpcId) {
......
}
/**
* Modifying the special attribute to new value of the vpc owned by the user.
*
* @param modifyVpcAttributesRequest The request containing all options for modifying own's vpc.
*/
public void modifyInstanceAttributes(ModifyVpcAttributesRequest modifyVpcAttributesRequest) {
......
}
注意:
第一个接口只支持少量常用参数
第二个接口可以支持较多参数,但需要创建request实例
参数说明如下:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
name | String | 是 | vpc名称,不能取值“default”,长度不超过65个字符,可由数字、字符、下划线组成 |
vpcId | String | 是 | 待更新vpc的id |
description | String | 否 | vpc描述,不超过200字符 |
使用示例如下:
vpcClient.modifyInstanceAttributes("test_update_2", "vpc-e8ff5i875svs");
注意: 只允许更新vpc的name和description字段,cidr字段不能更新
删除VPC
函数定义如下:
/**
* Delete the specified vpc owned by the user.All resource in the vpc must be deleted before the vpc itself
* can be deleted.
*
* @param vpcId The id of the vpc to delete.
*/
public void deleteVpc(String vpcId) {
......
}
/**
* Delete the specified vpc owned by the user.All resource in the vpc must be deleted before the vpc itself
* can be deleted.
*
* @param deleteVpcRequest The request containing all options for deleting own's vpc.
*/
public void deleteVpc(DeleteVpcRequest deleteVpcRequest) {
......
}
注意:
第一个接口只支持少量常用参数
第二个接口可以支持较多参数,但需要创建request实例
参数说明如下:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
vpcId | String | 是 | 待删除vpc的id |
使用示例如下:
vpcClient.deleteVpc("vpc-2xkpvijkdyp1");
查询VPC内网IP
函数定义如下:
/**
* Return a list of vpc private ip address info which belongs to the corresponding vpc by vpc id.
*
* @param request The request contains vpc id, list of private ip addresses and private ip range to be queried.
*/
public GetVpcPrivateAddressInfoResponse getVpcPrivateIpAddressInfo(GetVpcPrivateIpAddressInfoRequest request) {
......
}
注意:
只能查询已使用的IP信息,当查询未使用的IP时不会报错,但不会出现在返回结果里
privateIpAddresses 和 privateIpRange 二者不能同时为空
当 privateIpAddresses 和 privateIpRange 同时存在时候,以 privateIpRange 为准
参数说明如下:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
vpcId | String | 是 | VPC的id |
privateIpAddresses | List |
否 | 要查询的ip列表 |
privateIpRange | String | 否 | 要查询的ip范围,格式如 "192.168.0.1-192.168.0.5" |
使用示例如下:
GetVpcPrivateIpAddressInfoRequest request = new GetVpcPrivateIpAddressInfoRequest();
request.setVpcId("vpc-cxvqgxipk36r");
request.setPrivateIpAddresses(Arrays.asList("192.168.0.4","192.168.0.57"));
request.setPrivateIpRange("192.168.0.57-192.168.0.60");
vpcClient.getVpcPrivateIpAddressInfo(request);