代码示例
1.创建VPC
Copied!
public static CreateVPCResult createVPC(String projectId, String token, String cidr) {
String url = "https://{endpoint}/v1/" + projectId + "/vpcs";
String body = "{" +
" \"vpc\": {" +
" \"name\":\"cus_vpc_" + System.currentTimeMillis() + "\"," +
" \"description\":\"由伙伴自动创建的VPC\"," +
" \"cidr\":\"" + cidr + "\"" +
" \"enterprise_project_id\":\"" + projectId + "\"" +
" }" +
"}";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.set("X-Auth-Token", token);
HttpEntity<String> httpEntity = new HttpEntity<>(body, httpHeaders);
// 创建VPC资源
ResponseEntity<String> responseEntity = RestTemplateContext.getInstance().postForEntity(url, httpEntity, String.class);
String responseResult = responseEntity.getBody();
log.info("createVPC: " + responseResult);
CreateVPCResult createVPCResult = JsonUtil.jsonToBean(responseResult, CreateVPCResult.class);
return createVPCResult;
}
响应样例
Copied!
{
"vpc":
{
"id": "99d9d709-8478-xxxx-xxxx-2206b1023fd3",
"name": "vpc",
"description": "test",
"cidr": "192.168.0.0/24",
"status": "CREATING",
"enterprise_project_id": "0aad99bc-f5f6-xxxx-xxxx-c598d76b0ed2",
"routes": []
}
}
>接口参考创建VPC
2.创建安全组
Copied!
public static SecurityGroupResult createSecurityGroup(String projectId, String token, CreateVPCResult createVPCResult) {
String url = "https://{endpoint}/v1/" + projectId + "/security-groups";
String body = "{\n" +
" \"security_group\": {\n" +
" \"name\": \"custest_SecurityGroups_" + System.currentTimeMillis() + "\", \n" +
" \"vpc_id\": \"" + createVPCResult.getVpc().getId() + "\"\n" +
" }\n" +
"}";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.set("X-Auth-Token", token);
HttpEntity<String> httpEntity = new HttpEntity<>(body, httpHeaders);
ResponseEntity<String> responseEntity = RestTemplateContext.getInstance().postForEntity(url, httpEntity, String.class);
String result = responseEntity.getBody();
log.info("createSecurityGroup : " + result);
SecurityGroupResult securityGroupResult = JsonUtil.jsonToBean(result, SecurityGroupResult.class);
return securityGroupResult;
}
响应样例
Copied!
{
"security_group": {
"id": "16b6e77a-08fa-xxxx-xxxx-106c048884e6",
"name": "qq",
"description": "",
"vpc_id": "3ec3b33f-ac1c-xxxx-xxxx-7dba1ed79d85",
"enterprise_project_id": "0aad99bc-f5f6-xxxx-xxxx-c598d76b0ed2",
"security_group_rules": [
{
"direction": "egress",
"ethertype": "IPv4",
"id": "369e6499-b2cb-xxxx-xxxx-97e589692c62",
"description": "",
"security_group_id": "16b6e77a-08fa-xxxx-xxxx-106c048884e6"
},
{
"direction": "ingress",
"ethertype": "IPv4",
"id": "0222556c-6556-xxxx-xxxx-9fd5d3c06171",
"description": "",
"remote_group_id": "16b6e77a-08fa-xxxx-xxxx-106c048884e6",
"security_group_id": "16b6e77a-08fa-xxxx-xxxx-106c048884e6"
}
]
}
}
>接口参考创建安全组
3.创建安全组规则
Copied!
public static SecurityGroupRuleResult createSecurityGroupRules(String token, String projectId, SecurityGroupResult securityGroupResult) {
String url = "https://{endpoint}/v1/" + projectId + "/security-group-rules";
String body = "{\n" +
" \"security_group_rule\": {\n" +
" \"direction\": \"ingress\", \n" +
" \"port_range_min\": \"80\", \n" +
" \"ethertype\": \"IPv4\", \n" +
" \"port_range_max\": \"80\", \n" +
" \"protocol\": \"tcp\", \n" +
" \"security_group_id\": \"" + securityGroupResult.getSecurityGroup().getId() + "\"\n" +
" }\n" +
"}";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.set("X-Auth-Token", token);
HttpEntity<String> httpEntity = new HttpEntity<>(body, httpHeaders);
ResponseEntity<String> responseEntity = RestTemplateContext.getInstance().postForEntity(url, httpEntity, String.class);
String responseResult = responseEntity.getBody();
log.info("securityGroupRuleResult:" + responseResult);
SecurityGroupRuleResult securityGroupRuleResult =
JsonUtil.jsonToBean(responseResult, SecurityGroupRuleResult.class);
return securityGroupRuleResult;
}
响应示例
Copied!
{
"security_group_rule": {
"direction": "ingress",
"ethertype": "IPv4",
"id": "2bc0accf-312e-xxxx-xxxx-e4407625eb62",
"description": "",
"port_range_max": 80,
"port_range_min": 80,
"protocol": "tcp",
"remote_group_id": "85cc3048-abc3-xxxx-xxxx-377341426ac5",
"remote_ip_prefix": null,
"security_group_id": "a7734e61-b545-xxxx-xxxx-0189cbd9747a",
"tenant_id": "e4f50856753b4dc6afee5fa6xxxxxxxx"
}
}
>接口参考创建安全组规则
4.创建子网
Copied!
public static CreatSubnetsResult createSubnets(String token, String projectId, CreateVPCResult createVPCResult) {
String url = "https://{endpoint}/v1/" + projectId + "/subnets";
String body = "{\n" +
" \"subnet\": {\n" +
" \"name\": \"cus_subnet_"+System.currentTimeMillis()+"\",\n" +
" \"description\": \"\",\n" +
" \"cidr\": \"192.168.0.0/24\",\n" +
" \"gateway_ip\": \"192.168.0.1\",\n" +
" \"vpc_id\": \"" + createVPCResult.getVpc().getId() + "\"\n" +
" }\n" +
"}";
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.set("X-Auth-Token", token);
HttpEntity<String> httpEntity = new HttpEntity<>(body, httpHeaders);
ResponseEntity<String> responseEntity = RestTemplateContext.getInstance().postForEntity(url, httpEntity, String.class);
String responseResult = responseEntity.getBody();
log.info("createSubnetsResult : " + responseResult);
CreatSubnetsResult creatSubnetsResult = JsonUtil.jsonToBean(responseResult, CreatSubnetsResult.class);
return creatSubnetsResult;
}
响应示例
Copied!
{
"subnet": {
"id": "4779ab1c-7c1a-xxxx-xxxx-93dfc361b32d",
"name": "subnet",
"description": "",
"cidr": "192.168.20.0/24",
"dnsList": [
"114.xx.xx.114",
"1114.xx.xx.115"
],
"status": "UNKNOWN",
"vpc_id": "3ec3b33f-ac1c-xxxx-xxxx-7dba1ed79d85",
"gateway_ip": "192.168.20.1",
"ipv6_enable": true,
"cidr_v6": "xxxx:db8:xxxx::/64",
"gateway_ip_v6": "xxxx:db8:xxxx::1",
"dhcp_enable": true,
"primary_dns": "xxx.xx.xx.114",
"secondary_dns": "xxx.xx.xx.115",
"availability_zone": "aa-bb-cc",
"neutron_network_id": "4779ab1c-7c1a-xxxx-xxxx-93dfc361b32d",
"neutron_subnet_id": "213cb9d-3122-xxxx-xxxx-91ffc1231a12",
"neutron_subnet_id_v6": "e0fa7de1-a6e2-xxxx-xxxx-b9d8cebe93c4",
"extra_dhcp_opts": [
{
"opt_value": "xxx.xxx.0.33,xxx.xxx.0.34",
"opt_name": "ntp"
}
]
}
}
>接口参考创建子网
前提条件
拥有已实名认证的华为云账号和java运行环境
虚拟私有云(Virtual Private Cloud)是用户在华为云上申请的隔离的、私密的虚拟网络环境。用户可以自由配置VPC内的IP地址段、子网、安全组等子服务,也可以申请弹性带宽和弹性公网IP搭建业务系统
本示例展示如何通过接口调用方式创建VPC基础设施
代码示例
1.创建VPC
响应样例
{ "vpc": { "id": "99d9d709-8478-xxxx-xxxx-2206b1023fd3", "name": "vpc", "description": "test", "cidr": "192.168.0.0/24", "status": "CREATING", "enterprise_project_id": "0aad99bc-f5f6-xxxx-xxxx-c598d76b0ed2", "routes": [] } }
>接口参考创建VPC
2.创建安全组
响应样例
{ "security_group": { "id": "16b6e77a-08fa-xxxx-xxxx-106c048884e6", "name": "qq", "description": "", "vpc_id": "3ec3b33f-ac1c-xxxx-xxxx-7dba1ed79d85", "enterprise_project_id": "0aad99bc-f5f6-xxxx-xxxx-c598d76b0ed2", "security_group_rules": [ { "direction": "egress", "ethertype": "IPv4", "id": "369e6499-b2cb-xxxx-xxxx-97e589692c62", "description": "", "security_group_id": "16b6e77a-08fa-xxxx-xxxx-106c048884e6" }, { "direction": "ingress", "ethertype": "IPv4", "id": "0222556c-6556-xxxx-xxxx-9fd5d3c06171", "description": "", "remote_group_id": "16b6e77a-08fa-xxxx-xxxx-106c048884e6", "security_group_id": "16b6e77a-08fa-xxxx-xxxx-106c048884e6" } ] } }
>接口参考创建安全组
3.创建安全组规则
响应示例
{ "security_group_rule": { "direction": "ingress", "ethertype": "IPv4", "id": "2bc0accf-312e-xxxx-xxxx-e4407625eb62", "description": "", "port_range_max": 80, "port_range_min": 80, "protocol": "tcp", "remote_group_id": "85cc3048-abc3-xxxx-xxxx-377341426ac5", "remote_ip_prefix": null, "security_group_id": "a7734e61-b545-xxxx-xxxx-0189cbd9747a", "tenant_id": "e4f50856753b4dc6afee5fa6xxxxxxxx" } }
>接口参考创建安全组规则
4.创建子网
响应示例
{ "subnet": { "id": "4779ab1c-7c1a-xxxx-xxxx-93dfc361b32d", "name": "subnet", "description": "", "cidr": "192.168.20.0/24", "dnsList": [ "114.xx.xx.114", "1114.xx.xx.115" ], "status": "UNKNOWN", "vpc_id": "3ec3b33f-ac1c-xxxx-xxxx-7dba1ed79d85", "gateway_ip": "192.168.20.1", "ipv6_enable": true, "cidr_v6": "xxxx:db8:xxxx::/64", "gateway_ip_v6": "xxxx:db8:xxxx::1", "dhcp_enable": true, "primary_dns": "xxx.xx.xx.114", "secondary_dns": "xxx.xx.xx.115", "availability_zone": "aa-bb-cc", "neutron_network_id": "4779ab1c-7c1a-xxxx-xxxx-93dfc361b32d", "neutron_subnet_id": "213cb9d-3122-xxxx-xxxx-91ffc1231a12", "neutron_subnet_id_v6": "e0fa7de1-a6e2-xxxx-xxxx-b9d8cebe93c4", "extra_dhcp_opts": [ { "opt_value": "xxx.xxx.0.33,xxx.xxx.0.34", "opt_name": "ntp" } ] } }
>接口参考创建子网