Revision 4f9d1a69 src/com/rackspace/cloud/loadbalancer/api/client/LoadBalancerManager.java

b/src/com/rackspace/cloud/loadbalancer/api/client/LoadBalancerManager.java
5 5

  
6 6
import java.io.IOException;
7 7
import java.io.StringReader;
8
import java.io.UnsupportedEncodingException;
8 9
import java.util.ArrayList;
9 10

  
10 11
import javax.xml.parsers.FactoryConfigurationError;
......
14 15

  
15 16
import org.apache.http.HttpResponse;
16 17
import org.apache.http.client.ClientProtocolException;
18
import org.apache.http.client.methods.HttpDelete;
17 19
import org.apache.http.client.methods.HttpGet;
20
import org.apache.http.client.methods.HttpPost;
21
import org.apache.http.client.methods.HttpPut;
22
import org.apache.http.entity.StringEntity;
18 23
import org.apache.http.impl.client.BasicResponseHandler;
24
import org.apache.http.protocol.RequestExpectContinue;
19 25
import org.xml.sax.InputSource;
20 26
import org.xml.sax.SAXException;
21 27
import org.xml.sax.XMLReader;
......
28 34
import com.rackspace.cloud.loadbalancer.api.parsers.LoadBalancersXmlParser;
29 35
import com.rackspace.cloud.loadbalancers.api.client.http.LoadBalancersException;
30 36
import com.rackspace.cloud.servers.api.client.Account;
37
import com.rackspace.cloud.servers.api.client.CloudServersException;
38
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
31 39

  
32 40
public class LoadBalancerManager extends EntityManager {
33 41
	private Context context;
......
37 45
	}
38 46

  
39 47
	public LoadBalancer getLoadBalancerById(long id) throws LoadBalancersException {
48
		LoadBalancer loadBalancer = null;
49
		//First try DFW
50
		try{
51
			loadBalancer = getLoadBalancerById(id, Account.getAccount().getLoadBalancerDFWUrl());
52
			loadBalancer.setRegion("DFW");
53
		} catch(LoadBalancersException lbe){
54
			//Didn't work
55

  
56
		}
57

  
58
		//Then try ORD
59
		if(loadBalancer == null){
60
			try{
61
				loadBalancer = getLoadBalancerById(id, Account.getAccount().getLoadBalancerORDUrl());
62
				loadBalancer.setRegion("ORD");
63
			}
64
			catch(LoadBalancersException lbe){
65
				throw lbe;
66
			}
67
		}
68
		return loadBalancer;
69
	}
70

  
71
	private LoadBalancer getLoadBalancerById(long id, String url) throws LoadBalancersException {
40 72
		//TODO:grab from ord and combine list
41 73
		CustomHttpClient httpclient = new CustomHttpClient(context);
42 74
		//TODO: check for uk or us
43
		HttpGet get = new HttpGet(Account.getAccount().getLoadBalancerDFWUrl() + Account.getAccount().getAccountId() + "/loadbalancers/" + id);
75
		HttpGet get = new HttpGet(url + Account.getAccount().getAccountId() + "/loadbalancers/" + id);
44 76
		LoadBalancer loadBalancer = new LoadBalancer();
45 77

  
46 78
		get.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
......
88 120
			cse.setMessage(e.getLocalizedMessage());
89 121
			throw cse;
90 122
		}
91

  
123
		if(loadBalancer != null){
124
			Log.d("info", "the name is " + loadBalancer.getName());
125
		}
126
		else{
127
			Log.d("info", "the lb is null");
128
		}
92 129
		return loadBalancer;
93 130
	}
94
	
131

  
95 132
	public ArrayList<LoadBalancer> createList() throws LoadBalancersException{
96 133
		ArrayList<LoadBalancer> loadBalancers = createSublist(Account.getAccount().getLoadBalancerORDUrl());
97
		loadBalancers.addAll(createSublist(Account.getAccount().getLoadBalancerDFWUrl()));
134
		for(LoadBalancer loadBalancer: loadBalancers){
135
			loadBalancer.setRegion("ORD");
136
		}
137
		ArrayList<LoadBalancer> DFWloadBalancers = createSublist(Account.getAccount().getLoadBalancerDFWUrl());
138
		for(LoadBalancer loadBalancer: DFWloadBalancers){
139
			loadBalancer.setRegion("DFW");
140
		}
141
		loadBalancers.addAll(DFWloadBalancers);
98 142
		return loadBalancers;
99 143
	}
100 144

  
......
152 196
		}
153 197
		return loadBalancers;
154 198
	}
199

  
200
	public HttpBundle create(LoadBalancer entity, String regionUrl) throws CloudServersException {
201
		HttpResponse resp = null;
202
		CustomHttpClient httpclient = new CustomHttpClient(context);
203

  
204
		HttpPost post = new HttpPost(regionUrl + Account.getAccount().getAccountId() + "/loadbalancers");
205
		post.addHeader("Content-Type", "application/xml");
206

  
207
		StringEntity tmp = null;
208
		try {
209
			tmp = new StringEntity(entity.toDetailedXML());
210
		} catch (UnsupportedEncodingException e) {
211
			CloudServersException cse = new CloudServersException();
212
			cse.setMessage(e.getLocalizedMessage());
213
			throw cse;
214
		}
215

  
216
		Log.d("info", entity.toDetailedXML());
217

  
218
		post.setEntity(tmp);
219

  
220
		post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
221
		httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
222

  
223
		HttpBundle bundle = new HttpBundle();
224
		bundle.setCurlRequest(post);
225
		try {
226
			resp = httpclient.execute(post);
227
			bundle.setHttpResponse(resp);
228
		} catch (ClientProtocolException e) {
229
			CloudServersException cse = new CloudServersException();
230
			cse.setMessage(e.getLocalizedMessage());
231
			throw cse;
232
		} catch (IOException e) {
233
			CloudServersException cse = new CloudServersException();
234
			cse.setMessage(e.getLocalizedMessage());
235
			throw cse;
236
		} catch (FactoryConfigurationError e) {
237
			CloudServersException cse = new CloudServersException();
238
			cse.setMessage(e.getLocalizedMessage());
239
			throw cse;
240
		}
241
		return bundle;
242
	}
243

  
244
	public HttpBundle delete(LoadBalancer loadBalancer) throws CloudServersException {
245
		HttpResponse resp = null;
246
		CustomHttpClient httpclient = new CustomHttpClient(context);
247

  
248
		HttpDelete delete = new HttpDelete(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() 
249
				+ "/loadbalancers/" + loadBalancer.getId());				
250
		delete.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
251
		delete.addHeader("Content-Type", "application/xml");
252
		httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
253

  
254
		HttpBundle bundle = new HttpBundle();
255
		bundle.setCurlRequest(delete);
256

  
257
		try {			
258
			resp = httpclient.execute(delete);
259
			bundle.setHttpResponse(resp);
260
		} catch (ClientProtocolException e) {
261
			CloudServersException cse = new CloudServersException();
262
			cse.setMessage(e.getLocalizedMessage());
263
			throw cse;
264
		} catch (IOException e) {
265
			CloudServersException cse = new CloudServersException();
266
			cse.setMessage(e.getLocalizedMessage());
267
			throw cse;
268
		} catch (FactoryConfigurationError e) {
269
			CloudServersException cse = new CloudServersException();
270
			cse.setMessage(e.getLocalizedMessage());
271
			throw cse;
272
		}	
273
		return bundle;
274
	}
275

  
276
	public HttpBundle update(LoadBalancer loadBalancer, String name, String algorithm, String protocol, String port) throws CloudServersException {
277
		HttpResponse resp = null;
278
		CustomHttpClient httpclient = new CustomHttpClient(context);
279

  
280
		HttpPut put = new HttpPut(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" + loadBalancer.getId());				
281

  
282
		put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
283
		put.addHeader("Content-Type", "application/xml");
284

  
285
		String xml = "<loadBalancer xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" " + 
286
		"name=\"" + name + "\" " + 
287
		"algorithm=\"" + algorithm.toUpperCase() + "\" " + 
288
		"protocol=\"" + protocol.toUpperCase() + "\" " + 
289
		"port=\"" + port + "\" />";
290

  
291
		StringEntity tmp = null;
292
		try {
293
			tmp = new StringEntity(xml);
294
		} catch (UnsupportedEncodingException e) {
295
			CloudServersException cse = new CloudServersException();
296
			cse.setMessage(e.getLocalizedMessage());
297
			throw cse;
298
		}
299

  
300
		put.setEntity(tmp);
301
		httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
302

  
303
		HttpBundle bundle = new HttpBundle();
304
		bundle.setCurlRequest(put);
305

  
306
		try {			
307
			resp = httpclient.execute(put);
308
			bundle.setHttpResponse(resp);
309
		} catch (ClientProtocolException e) {
310
			CloudServersException cse = new CloudServersException();
311
			cse.setMessage(e.getLocalizedMessage());
312
			throw cse;
313
		} catch (IOException e) {
314
			CloudServersException cse = new CloudServersException();
315
			cse.setMessage(e.getLocalizedMessage());
316
			throw cse;
317
		} catch (FactoryConfigurationError e) {
318
			CloudServersException cse = new CloudServersException();
319
			cse.setMessage(e.getLocalizedMessage());
320
			throw cse;
321
		}	
322
		return bundle;
323
	}
324
	
325
	public HttpBundle addNodes(LoadBalancer loadBalancer, ArrayList<Node> nodes) throws CloudServersException {
326
		HttpResponse resp = null;
327
		CustomHttpClient httpclient = new CustomHttpClient(context);
328

  
329
		HttpPost post = new HttpPost(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" + loadBalancer.getId() + "/nodes");				
330

  
331
		post.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
332
		post.addHeader("Content-Type", "application/xml");
333

  
334
		String xml = "<nodes xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\"> ";
335
		for(int i = 0;i < nodes.size(); i++){
336
			Node node = nodes.get(i);
337
			if(node.getWeight() == null){
338
				xml += "<node address=\"" + node.getAddress() + "\" port=\"" + node.getPort() + "\" condition=\"" + node.getCondition() + "\"/>";
339
			}
340
			else{
341
				xml += "<node address=\"" + node.getAddress() + "\" port=\"" + node.getPort() + "\" condition=\"" + node.getCondition() + "\" weight=\"" + node.getWeight() + "\"/>";
342
			}
343
			xml += " </nodes>";
344
		}
345
		
346
		Log.d("info", xml);
347

  
348
		StringEntity tmp = null;
349
		try {
350
			tmp = new StringEntity(xml);
351
		} catch (UnsupportedEncodingException e) {
352
			CloudServersException cse = new CloudServersException();
353
			cse.setMessage(e.getLocalizedMessage());
354
			throw cse;
355
		}
356

  
357
		post.setEntity(tmp);
358
		httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
359

  
360
		HttpBundle bundle = new HttpBundle();
361
		bundle.setCurlRequest(post);
362

  
363
		try {			
364
			resp = httpclient.execute(post);
365
			bundle.setHttpResponse(resp);
366
		} catch (ClientProtocolException e) {
367
			CloudServersException cse = new CloudServersException();
368
			cse.setMessage(e.getLocalizedMessage());
369
			throw cse;
370
		} catch (IOException e) {
371
			CloudServersException cse = new CloudServersException();
372
			cse.setMessage(e.getLocalizedMessage());
373
			throw cse;
374
		} catch (FactoryConfigurationError e) {
375
			CloudServersException cse = new CloudServersException();
376
			cse.setMessage(e.getLocalizedMessage());
377
			throw cse;
378
		}	
379
		return bundle;
380
	}
381

  
382
	public HttpBundle modifyNode(LoadBalancer loadBalancer, Node node, String condition, String weight) throws CloudServersException {
383
		HttpResponse resp = null;
384
		CustomHttpClient httpclient = new CustomHttpClient(context);
385

  
386
		HttpPut put = new HttpPut(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" + loadBalancer.getId() + "/nodes/" + node.getId());				
387

  
388
		put.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
389
		put.addHeader("Content-Type", "application/xml");
390

  
391
		String xml;
392
		//different request body if the nodes have weight
393
		if(weight != null){
394
			xml = "<node xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" condition=\"" + condition.toUpperCase() + "\" weight=\"" + weight + "\"" + "/>";
395
			Log.d("info", "went to first");
396
		}
397
		else{
398
			xml = "<node xmlns=\"http://docs.openstack.org/loadbalancers/api/v1.0\" condition=\"" + condition.toUpperCase() + "\"/>";
399
			Log.d("info", "went to second");
400
		}
401

  
402
		StringEntity tmp = null;
403
		try {
404
			tmp = new StringEntity(xml);
405
		} catch (UnsupportedEncodingException e) {
406
			CloudServersException cse = new CloudServersException();
407
			cse.setMessage(e.getLocalizedMessage());
408
			throw cse;
409
		}
410

  
411
		Log.d("info", xml);
412

  
413
		put.setEntity(tmp);
414
		httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
415

  
416
		HttpBundle bundle = new HttpBundle();
417
		bundle.setCurlRequest(put);
418

  
419
		try {			
420
			resp = httpclient.execute(put);
421
			bundle.setHttpResponse(resp);
422
		} catch (ClientProtocolException e) {
423
			CloudServersException cse = new CloudServersException();
424
			cse.setMessage(e.getLocalizedMessage());
425
			throw cse;
426
		} catch (IOException e) {
427
			CloudServersException cse = new CloudServersException();
428
			cse.setMessage(e.getLocalizedMessage());
429
			throw cse;
430
		} catch (FactoryConfigurationError e) {
431
			CloudServersException cse = new CloudServersException();
432
			cse.setMessage(e.getLocalizedMessage());
433
			throw cse;
434
		}	
435
		return bundle;
436
	}
437

  
438
	public HttpBundle removeNode(LoadBalancer loadBalancer, Node node) throws CloudServersException {
439
		HttpResponse resp = null;
440
		CustomHttpClient httpclient = new CustomHttpClient(context);
441

  
442
		HttpDelete delete = new HttpDelete(LoadBalancer.getRegionUrl(loadBalancer.getRegion()) + Account.getAccount().getAccountId() + "/loadbalancers/" + loadBalancer.getId() + "/nodes/" + node.getId());				
443

  
444
		delete.addHeader("X-Auth-Token", Account.getAccount().getAuthToken());
445
		
446
		httpclient.removeRequestInterceptorByClass(RequestExpectContinue.class);
447

  
448
		HttpBundle bundle = new HttpBundle();
449
		bundle.setCurlRequest(delete);
450

  
451
		try {			
452
			resp = httpclient.execute(delete);
453
			bundle.setHttpResponse(resp);
454
		} catch (ClientProtocolException e) {
455
			CloudServersException cse = new CloudServersException();
456
			cse.setMessage(e.getLocalizedMessage());
457
			throw cse;
458
		} catch (IOException e) {
459
			CloudServersException cse = new CloudServersException();
460
			cse.setMessage(e.getLocalizedMessage());
461
			throw cse;
462
		} catch (FactoryConfigurationError e) {
463
			CloudServersException cse = new CloudServersException();
464
			cse.setMessage(e.getLocalizedMessage());
465
			throw cse;
466
		}	
467
		return bundle;
468
	}
155 469
}

Also available in: Unified diff