1 import java.util.UUID;
2
3 import org.json.JSONArray;
4 import org.json.JSONException;
5 import org.json.JSONObject;
6
7 public class VPCUtil{
8
9 private String username;
10
11 private String password;
12
13 private String token;
14
15 private String projectId;
16
17 private long overDate;
18
19 public IAMUtil(){
20 }
21
22 public IAMUtil(String username, String password){
23 this.username = username;
24 this.password = password;
25 TokenAndProject tap = new IAMUtil().getToken(username,password);
26 this.overDate = System.currentTimeMillis() + 23*60*60*1000;
27 this.token = tap.getToken();
28 this.projectId = tap.getProjectId();
29 }
30
31 /**
32 *
33 * @Title createVPC
34 * @Description 创建虚拟私有云
35 * @param idx
36 * @return vpc_id 虚拟私有云的ID
37 */
38 public String createVPC(int idx, String name_prefix) {
39 this.checkAccount();
40 JSONObject resjo = null;
41 String res = RequestUtil.requestsPost("https://vpc.cn-south-1.myhuaweicloud.com/v1/" + projectId + "/vpcs","{\"vpc\": {\"name\": \"" + name_prefix + "-" + (1+idx) + "\",\"cidr\": \"192.168.0.0/16\"}}",token);
42 String vpc_id = "";
43 try {
44 resjo = new JSONObject(res);
45 vpc_id = resjo.getJSONObject("vpc").getString("id");
46 } catch (JSONException e) {
47 System.out.println(res);
48 e.printStackTrace();
49 }finally {
50 }
51 return vpc_id;
52 }
53
54 /**
55 *
56 * @Title createVPC
57 * @Description 创建虚拟私有云的子网
58 * @param subNo 192.168.x.y 里面的x
59 * @return vpc_id 虚拟私有云的ID
60 */
61 public NetWorkIds createSubnet(int idx, String vpc_id, int subNo, String name_prefix) {
62 this.checkAccount();
63 JSONObject resjo = null;
64 String res = RequestUtil.requestsPost("https://vpc.cn-south-1.myhuaweicloud.com/v1/" + projectId + "/subnets",
65 "{\"subnet\": {\"name\": \"" + name_prefix + "-" + (subNo == 10 ? "manager" : "service") + "-" + (1+idx) + "\",\"cidr\": \"192.168." + subNo + ".0/24\",\"gateway_ip\": \"192.168." + subNo + ".254\",\"vpc_id\": \"" + vpc_id + "\"}}",token);
66 String netId = "";
67 String subnetId = "";
68 try {
69 resjo = new JSONObject(res);
70 netId = resjo.getJSONObject("subnet").getString("id");
71 subnetId = resjo.getJSONObject("subnet").getString("neutron_subnet_id");
72 } catch (JSONException e) {
73 System.out.println(res);
74 e.printStackTrace();
75 }finally {
76 }
77 return new NetWorkIds(netId,subnetId);
78 }
79
80 public NetWorkIds createSubnetFilter(int idx, String vpc_id, int subNo, String name_prefix) {
81 this.checkAccount();
82 NetWorkIds nids = createSubnet(idx, vpc_id, subNo, name_prefix);
83 while(true) {
84 String portsres = getPorts(nids.getNetId(), true);
85 JSONArray ports = null;
86 JSONArray fixed_ips = null;
87 try {
88 ports = new JSONArray(portsres);
89 if(ports.length() <= 0)continue;
90 fixed_ips = ports.getJSONObject(0).getJSONArray("fixed_ips");
91 if(fixed_ips.length() <= 0)continue;
92 String ip_address = fixed_ips.getJSONObject(0).getString("ip_address");
93 int ip4 = Integer.parseInt(ip_address.split("\\.")[3]);
94 System.out.println("I get the address : " + ip_address);
95 if((ip4 >= 1 && ip4 <= 4) || (ip4 >= 100 && ip4 <= 103)){
96 nids = new VPCUtil().createSubnet(token, projectId, idx, vpc_id, subNo, name_prefix);
97 }else {
98 break;
99 }
100 } catch (JSONException e) {
101 System.out.println(portsres);
102 e.printStackTrace();
103 }
104 }
105
106 return nids;
107 }
108
109 public String createVirtualIP(String net_id, String subnet_id, String ip) {
110 this.checkAccount();
111 String res = RequestUtil.requestsPost("https://vpc.cn-south-1.myhuaweicloud.com/v2.0/ports",
112 "{\"port\": {\"network_id\": \"" + net_id + "\",\"device_owner\": \"neutron:VIP_PORT\",\"name\": \"" + UUID.randomUUID().toString() + "\",\"fixed_ips\": [{\"subnet_id\": \"" + subnet_id + "\", \"ip_address\": \"" + ip + "\"}]}}", token);
113 String new_port_id = "";
114 try {
115 new_port_id = new JSONObject(res).getJSONObject("port").getString("id");
116 } catch (JSONException e) {
117 System.out.println(res);
118 e.printStackTrace();
119 }
120 return new_port_id;
121 }
122
123 public void createVirtualIPport(String port_id, String ip1, String ip2) {
124 this.checkAccount();
125 RequestUtil.requestsPut("https://vpc.cn-south-1.myhuaweicloud.com/v2.0/ports/" + port_id,
126 "{\"port\": {\"allowed_address_pairs\": [{\"ip_address\":\"" + ip1 + "\"},{\"ip_address\":\"" + ip2 + "\"}]}}", token);
127 }
128
129 /**
130 *
131 * @Title getVPCs
132 * @Description 获取虚拟私有云列表
133 * @return vpcsInJSON 虚拟私有云列表的JOSN格式字符串
134 */
135 public String getVPCs() {
136 this.checkAccount();
137 String res = RequestUtil.requestsGet("https://vpc.cn-south-1.myhuaweicloud.com/v1/" + projectId + "/vpcs", token);
138 try {
139 JSONObject resjo = new JSONObject(res);
140 JSONArray vpcs = resjo.getJSONArray("vpcs");
141 res = vpcs.toString();
142 } catch (JSONException e) {
143 System.out.println(res);
144 e.printStackTrace();
145 }
146 return res;
147 }
148
149 public String getSubnets(String vpc_id) {
150 this.checkAccount();
151 String res = RequestUtil.requestsGet("https://vpc.cn-south-1.myhuaweicloud.com/v1/" + projectId + "/subnets?project_id" + projectId + (vpc_id == null ? "" : "&vpc_id=" + vpc_id), token);
152 try {
153 JSONObject resjo = new JSONObject(res);
154 JSONArray vpcs = resjo.getJSONArray("subnets");
155 res = vpcs.toString();
156 } catch (JSONException e) {
157 System.out.println(res);
158 e.printStackTrace();
159 }
160 return res;
161 }
162
163 public String getPorts(String network_id) {
164 this.checkAccount();
165 return getPorts(network_id, false);
166 }
167
168 public String getPorts(String network_id, boolean isDHCP) {
169 this.checkAccount();
170 String res = RequestUtil.requestsGet("https://vpc.cn-south-1.myhuaweicloud.com/v2.0/ports?project_id=" + projectId + (network_id == null ? "" : "&network_id=" + network_id) + (isDHCP ? "&device_owner=network:dhcp" : ""), token);
171 try {
172 JSONObject resjo = new JSONObject(res);
173 JSONArray vpcs = resjo.getJSONArray("ports");
174 res = vpcs.toString();
175 } catch (JSONException e) {
176 System.out.println(res);
177 e.printStackTrace();
178 }
179 return res;
180 }
181
182 public String deletePort(String token, String port_id) {
183 this.checkAccount();
184 System.out.println("deletePort start " + port_id);
185 String res = RequestUtil.requestsDelete("https://vpc.cn-south-1.myhuaweicloud.com/v2.0/ports/" + port_id, token);
186 System.out.println(res);
187 System.out.println("deletePort end");
188 return res;
189 }
190
191 public String deleteSubnet(String vpc_id, String network_id) {
192 this.checkAccount();
193 System.out.println("deleteSubnet start " + network_id);
194 String res = RequestUtil.requestsDelete("https://vpc.cn-south-1.myhuaweicloud.com/v1/" + projectId + "/vpcs/"+ vpc_id +"/subnets/" + network_id, token);
195 System.out.println(res);
196 System.out.println("deleteSubnet end");
197 return res;
198 }
199
200 public String deleteVPC(String vpc_id) {
201 this.checkAccount();
202 String res = RequestUtil.requestsDelete("https://vpc.cn-south-1.myhuaweicloud.com/v1/" + projectId + "/vpcs/" + vpc_id, token);
203 System.out.println(res);
204 return res;
205 }
206
207 private void checkAccount(){
208 if(tap == null || System.currentTimeMillis() > overDate) {
209 tap = new IAMUtil().getToken(this.username,this.password);
210 this.token = tap.getToken();
211 this.projectId = tap.getProjectId();
212 this.overDate = System.currentTimeMillis() + 23*60*60*1000;
213 }
214 }
215
216 }
217
218 class NetWorkIds{
219 String netId;
220 String subnetId;
221 public NetWorkIds(String netId, String subnetId) {
222 super();
223 this.netId = netId;
224 this.subnetId = subnetId;
225 }
226
227 public String getNetId() {return netId;}
228 public String getSubNetId() {return subnetId;}
229 }