子网
获取Endpoint
在确认您使用SDK时配置的Endpoint时,可先阅读开发人员指南中关于VPC服务域名的部分,理解Endpoint相关的概念。 百度智能云目前开放了多区域支持,请参考区域选择说明中网络产品VPC的部分。
注意: 子网 API支持HTTP和HTTPS两种调用方式。为了提升数据的安全性,建议通过HTTPS调用
获取AK/SK
要使用百度智能云子网,您需要拥有一个有效的 AK(Access Key ID)和SK(Secret Access Key)用来进行签名认证。AK/SK是由系统分配给用户的,均为字符串,用于标识用户,为访问服务做签名验证。 可以通过如下步骤获得并了解您的AK/SK信息:
新建SubnetClient
SubnetClient是Subnet服务的客户端,为开发者与Subnet服务进行交互提供了一系列的方法。 新建SubnetClient时,需要先使用Endpoint、AK、SK对BceClientConfigurationl类型的config实例进行配置,再使用config实例对SubnetClient进行配置,具体配置方法如下:
static final String ENDPOINT = ""
static final String AK = "";
static final String SK = "";
SubnetClientConfiguration config = new SubnetClientConfiguration();
config.setCredentials(new DefaultBceCredentials(AK, SK));
config.setEndpoint(ENDPOINT);
SubnetClient subnetClient = new SubnetClient(config);
创建子网
函数定义如下:
/**
*
* @param name The name of subnet that will be created.
* @param vpcId The id of vpc which this subnet belong.
* @param cidr The CIDR of this subnet.
* @param zoneName
* the name of available zone which the subnet belong
* through listZones, we can get all available zone info at current region
* ee.g. "cn-gz-a" "cn-gz-b"
* @return
*/
public CreateSubnetResponse createSubnet(String name, String vpcId, String cidr, String zoneName) {
......
}
/**
* Create a subnet 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 subnet.
* @return List of subnetId newly created
* @throws BceClientException
*/
public CreateSubnetResponse createSubnet(CreateSubnetRequest request)
throws BceClientException {
......
}
注意:
第一个接口只支持少量常用参数
第二个接口则可以支持较多参数,但需要创建request实例
参数说明如下:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
name | String | 是 | 子网名称,不能取值“default”,长度不超过65个字符,可由数字、字符、下划线组成 |
vpcId | String | 是 | 子网所属vpc的id |
cidr | String | 是 | 子网的cidr |
zoneName | String | 是 | 可用区名称 |
subnetType | String | 否 | 子网类型,“BCC”、"BBC" |
description | String | 否 | 子网描述,不超过200字符 |
使用示例如下:
subnetClient.createSubnet("test_subnet_2", "vpc-e8ff5i875svs", "192.168.0.0/20", "cn-bj-a");
列举子网
函数定义如下:
/**
* Return a list of subnets owned by the authenticated user.
*
* @return The response containing a list of subnets owned by the authenticated user.
*/
public ListSubnetsResponse listSubnets() {
......
}
/**
* Return a list of subnet owned by the authenticated user.
*
* @param request The request containing all options for listing own's subnet.
* @return The response containing a list of subnets owned by the authenticated user.
*/
public ListSubnetsResponse listSubnets(ListSubnetsRequest request) {
......
}
注意:
第一个接口只支持少量常用参数
第二个接口则可以支持较多参数,但需要创建request实例
参数说明如下:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
marker | String | 否 | 批量获取列表的查询的起始位置,是一个由系统生成的字符串 |
maxKeys | Integer | 否 | 每页包含的最大数量,最大数量通常不超过1000,缺省值为1000 |
vpcId | String | 否 | 所属vpc的id |
zoneName | String | 否 | 所属可用区的名称 |
subnetType | String | 否 | 子网类型,"BCC"、"BBC" |
使用示例如下:
subnetClient.listSubnets();
查询子网
函数定义如下:
/**
* Get the detail information of specified subnet.
*
* @param subnetId The id of the subnet.
* @return A subnet detail model for the subnetId.
*/
public GetSubnetResponse getSubnet(String subnetId) {
/**
* Get the detail information of specified subnet.
*
* @param getSubnetRequest The request containing all options for getting the subnet info.
* @return A subnet detail model for the subnetId.
*/
public GetSubnetResponse getSubnet(GetSubnetRequest getSubnetRequest) {
注意:
第一个接口只支持少量常用参数
第二个接口则可以支持较多参数,但需要创建request实例
参数说明如下:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
subnetId | String | 是 | 待查询子网的id |
使用示例如下:
subnetClient.getSubnet("sbn-kvrxdch0rwaf");
更新子网
函数定义如下:
/**
* Modifying the special attribute to new value of the subnet owned by the user.
* @param subnetId The id of the subnet
* @param name The name of the subnet after modifying
*/
public void modifySubnetAttributes(String subnetId, String name) {
......
}
/**
* Modifying the special attribute to new value of the subnet owned by the user.
*
* @param modifySubnetAttributesRequest The request containing all options for modifying own's subnet.
*/
public void modifySubnetAttributes(ModifySubnetAttributesRequest modifySubnetAttributesRequest) {
......
}
注意:
第一个接口只支持少量常用参数
第二个接口则可以支持较多参数,但需要创建request实例
参数说明如下:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
subnetId | String | 是 | 待更新子网的id |
name | String | 是 | 子网名称,不能取值“default”,长度不超过65个字符,可由数字、字符、下划线组成 |
description | String | 否 | 子网描述,不超过200字符 |
使用示例如下:
subnetClient.modifySubnetAttributes("sbn-eg6ixcdpa3j2", "subnet_modify");
注意: 只允许更新子网的name和description字段,cidr、zoneName和subnetType字段不能更新
删除子网
函数定义如下:
/**
* Delete the specified subnet owned by the user.
*
* @param subnetId The id of the subnet to delete.
*/
public void deleteSubnet(String subnetId) {
......
}
/**
* Delete the specified subnet owned by the user.
*
* @param deleteSubnetRequest the request containing all options for deleting own's subnet.
*/
public void deleteSubnet(DeleteSubnetRequest deleteSubnetRequest) {
......
}
注意:
第一个接口只支持少量常用参数
第二个接口则可以支持较多参数,但需要创建request实例
参数说明如下:
参数名称 | 类型 | 是否必需 | 描述 |
---|---|---|---|
subnetId | String | 是 | 待删除子网的id |
使用示例如下:
subnetClient.deleteSubnet("sbn-kvrxdch0rwaf");