Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ViewLoadBalancerActivity.java @ 232548ba

History | View | Annotate | Download (12.8 kB)

1
/**
2
 * 
3
 */
4
package com.rackspacecloud.android;
5

    
6
import java.util.ArrayList;
7
import java.util.Collections;
8

    
9
import org.apache.http.HttpResponse;
10

    
11
import android.app.Activity;
12
import android.app.AlertDialog;
13
import android.app.Dialog;
14
import android.content.DialogInterface;
15
import android.content.Intent;
16
import android.graphics.Color;
17
import android.os.AsyncTask;
18
import android.os.Bundle;
19
import android.util.Log;
20
import android.util.TypedValue;
21
import android.view.Menu;
22
import android.view.MenuInflater;
23
import android.view.MenuItem;
24
import android.view.View;
25
import android.view.View.OnClickListener;
26
import android.widget.Button;
27
import android.widget.LinearLayout;
28
import android.widget.TextView;
29

    
30
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer;
31
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager;
32
import com.rackspace.cloud.loadbalancer.api.client.Node;
33
import com.rackspace.cloud.loadbalancer.api.client.VirtualIp;
34
import com.rackspace.cloud.loadbalancer.api.client.http.LoadBalancersException;
35
import com.rackspace.cloud.servers.api.client.CloudServersException;
36
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
37

    
38
public class ViewLoadBalancerActivity extends CloudActivity {
39

    
40
        private static final int EDIT_LOAD_BALANCER_CODE = 184;
41
        private static final int EDIT_NODES_CODE = 185;
42

    
43
        private LoadBalancer loadBalancer;
44
        private PollLoadBalancerTask pollLoadBalancerTask;
45

    
46
        @Override
47
        public void onCreate(Bundle savedInstanceState) {
48
                super.onCreate(savedInstanceState);
49
                loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer");
50
                Log.d("info", "the status is " + loadBalancer.getStatus());
51
                setContentView(R.layout.view_loadbalancer);
52
                restoreState(savedInstanceState);
53
        }
54

    
55
        @Override
56
        protected void onSaveInstanceState(Bundle outState) {
57
                super.onSaveInstanceState(outState);
58
                outState.putSerializable("loadBalancer", loadBalancer);
59
        }
60

    
61
        protected void restoreState(Bundle state) {
62
                super.restoreState(state);
63

    
64
                if (state != null && state.containsKey("loadBalancer") && state.getSerializable("loadBalancer") != null) {
65
                        loadBalancer = (LoadBalancer) state.getSerializable("loadBalancer");
66
                        loadLoadBalancerData();
67
                        if(!loadBalancer.getStatus().contains("DELETE")){
68
                                setUpButtons();
69
                        } else {
70
                                setUpBadButtons();
71
                        }
72
                }
73
                else{
74
                        new LoadLoadBalancerTask().execute((Void[]) null);
75
                }
76
        }
77

    
78
        private void setUpButtons(){
79
                Button editLoadBalancer = (Button)findViewById(R.id.edit_loadbalancer_button);
80
                editLoadBalancer.setOnClickListener(new OnClickListener() {
81

    
82
                        @Override
83
                        public void onClick(View v) {
84
                                Intent editLoadBalancerIntent = new Intent(getContext(), EditLoadBalancerActivity.class);
85
                                editLoadBalancerIntent.putExtra("loadBalancer", loadBalancer);
86
                                startActivityForResult(editLoadBalancerIntent, EDIT_LOAD_BALANCER_CODE);
87
                        }
88

    
89
                });
90

    
91
                Button deleteLoadBalancer = (Button)findViewById(R.id.delete_loadbalancer_button);
92
                deleteLoadBalancer.setOnClickListener(new OnClickListener() {
93

    
94
                        @Override
95
                        public void onClick(View v) {
96
                                showDialog(R.id.view_server_delete_button);
97
                        }
98

    
99
                });
100

    
101
                Button editNodes = (Button)findViewById(R.id.edit_nodes_button);
102
                editNodes.setOnClickListener(new OnClickListener() {
103

    
104
                        @Override
105
                        public void onClick(View v) {
106
                                Intent editLoadBalancerIntent = new Intent(getContext(), EditNodesActivity.class);
107
                                editLoadBalancerIntent.putExtra("nodes", loadBalancer.getNodes());
108
                                editLoadBalancerIntent.putExtra("loadBalancer", loadBalancer);
109
                                startActivityForResult(editLoadBalancerIntent, EDIT_NODES_CODE);
110
                        }
111
                });
112
        }
113
        
114
        private void setUpBadButtons(){
115
                Button editLoadBalancer = (Button)findViewById(R.id.edit_loadbalancer_button);
116
                editLoadBalancer.setOnClickListener(new OnClickListener() {
117

    
118
                        @Override
119
                        public void onClick(View v) {
120
                                showAlert(loadBalancer.getStatus(), "The Load Balancer cannot currently be updated");
121
                        }
122

    
123
                });
124

    
125
                Button deleteLoadBalancer = (Button)findViewById(R.id.delete_loadbalancer_button);
126
                deleteLoadBalancer.setOnClickListener(new OnClickListener() {
127

    
128
                        @Override
129
                        public void onClick(View v) {
130
                                showAlert(loadBalancer.getStatus(), "The Load Balancer cannot currently be deleted");
131
                        }
132

    
133
                });
134

    
135
                Button editNodes = (Button)findViewById(R.id.edit_nodes_button);
136
                editNodes.setOnClickListener(new OnClickListener() {
137

    
138
                        @Override
139
                        public void onClick(View v) {
140
                                showAlert(loadBalancer.getStatus(), "The nodes cannot currently be edited");
141
                        }
142
                });
143

    
144

    
145
        }
146

    
147
        @Override
148
        protected Dialog onCreateDialog(int id) {
149
                switch (id) {
150
                case R.id.view_server_delete_button:
151
                        return new AlertDialog.Builder(ViewLoadBalancerActivity.this)
152
                        .setIcon(R.drawable.alert_dialog_icon)
153
                        .setTitle("Delete Load Balancer")
154
                        .setMessage("Are you sure you want to delete the load balancer?")
155
                        .setPositiveButton("Delete", new DialogInterface.OnClickListener() {
156
                                public void onClick(DialogInterface dialog, int whichButton) {
157
                                        // User clicked OK so do some stuff
158
                                        new DeleteLoadBalancerTask().execute((Void[]) null);
159
                                }
160
                        })
161
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
162
                                public void onClick(DialogInterface dialog, int whichButton) {
163
                                        // User clicked Cancel so do some stuff
164
                                }
165
                        })
166
                        .create();
167
                }
168
                return null;
169
        }
170

    
171
        private void loadLoadBalancerData() {
172
                if(loadBalancer != null){
173
                        TextView name = (TextView) findViewById(R.id.view_name);
174
                        name.setText(loadBalancer.getName());
175

    
176
                        TextView id = (TextView) findViewById(R.id.view_lb_id);
177
                        id.setText(loadBalancer.getId());
178

    
179
                        TextView protocol = (TextView) findViewById(R.id.view_protocol);
180
                        protocol.setText(loadBalancer.getProtocol());
181

    
182
                        TextView port = (TextView) findViewById(R.id.view_port);
183
                        port.setText(loadBalancer.getPort());
184

    
185
                        TextView algorithm = (TextView) findViewById(R.id.view_algorithm);
186
                        algorithm.setText(loadBalancer.getAlgorithm());
187

    
188
                        TextView status = (TextView) findViewById(R.id.view_status);
189
                        if (!"ACTIVE".equals(loadBalancer.getStatus())) {
190
                                status.setText(loadBalancer.getStatus());
191
                                pollLoadBalancerTask = new PollLoadBalancerTask();
192
                                pollLoadBalancerTask.execute((Void[]) null);
193
                        } else {
194
                                status.setText(loadBalancer.getStatus());
195
                        }
196

    
197

    
198
                        status.setText(loadBalancer.getStatus());
199

    
200
                        TextView connectionLogging = (TextView) findViewById(R.id.view_islogging);
201
                        connectionLogging.setText(loadBalancer.getIsConnectionLoggingEnabled());
202

    
203
                        loadVirutalIpData();
204
                }
205
        }
206

    
207
        private void loadVirutalIpData() {
208
                int layoutIndex = 0;
209
                LinearLayout layout = (LinearLayout) this.findViewById(R.id.vip_addresses);    
210
                layout.removeAllViews();
211
                ArrayList<VirtualIp> virtualIps = loadBalancer.getVirtualIps();
212
                //maybe null if the lb has been deleted
213
                if(virtualIps != null){
214
                        for (int i = 0; i < virtualIps.size(); i++) {
215
                                TextView tv = new TextView(this.getBaseContext());
216
                                tv.setLayoutParams(((TextView)findViewById(R.id.view_port)).getLayoutParams()); // easy quick styling! :)
217
                                tv.setTypeface(tv.getTypeface(), 1); // 1 == bold
218
                                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
219
                                tv.setTextColor(Color.WHITE);
220
                                tv.setText(virtualIps.get(i).getAddress());
221
                                layout.addView(tv, layoutIndex++);
222
                        }
223
                }
224

    
225
                //TextView vipId = (TextView) findViewById(R.id.view_vip_id);
226
                //vipId.setText(loadBalancer.getVirtualIps().get(0).getId());
227

    
228
                //TextView address = (TextView) findViewById(R.id.view_vip_address);
229

    
230
                //TextView ipVersion = (TextView) findViewById(R.id.view_ipversion);
231
                //ipVersion.setText(loadBalancer.getVirtualIps().get(0).getIpVersion());
232

    
233
                //TextView type = (TextView) findViewById(R.id.view_vip_type);
234
                //type.setText(loadBalancer.getVirtualIps().get(0).getType());
235

    
236
                loadNodeData();
237
        }
238

    
239
        private void loadNodeData() {
240
                int layoutIndex = 0; // public IPs start here
241
                LinearLayout layout = (LinearLayout) this.findViewById(R.id.node_addresses);   
242
                layout.removeAllViews();
243
                ArrayList<Node> nodeIps = loadBalancer.getNodes();
244
                
245
                /*
246
                 * need to sort the addresses because during polling
247
                 * their order can change and the display becomes
248
                 * jumpy
249
                 */
250
                ArrayList<String> addresses = new ArrayList<String>();
251
                for(Node n : nodeIps){
252
                        addresses.add(n.getAddress());
253
                }
254
                
255
                Collections.sort(addresses);
256
                                
257
                //may be null if lb has been deleted
258
                if(nodeIps != null){
259
                        for (int i = 0; i < nodeIps.size(); i++) {
260
                                TextView tv = new TextView(this.getBaseContext());
261
                                tv.setLayoutParams(((TextView)findViewById(R.id.view_port)).getLayoutParams()); // easy quick styling! :)
262
                                tv.setTypeface(tv.getTypeface(), 1); // 1 == bold
263
                                tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
264
                                tv.setTextColor(Color.WHITE);
265
                                tv.setText(addresses.get(i));
266
                                layout.addView(tv, layoutIndex++);
267
                        }
268
                }
269

    
270
                /*
271
                TextView nodeID = (TextView) findViewById(R.id.view_node_id);
272
                nodeID.setText(loadBalancer.getNodes().get(0).getId());
273

274
                TextView address = (TextView) findViewById(R.id.view_node_address);
275
                address.setText(loadBalancer.getNodes().get(0).getAddress());
276

277
                TextView nodePort = (TextView) findViewById(R.id.view_node_port);
278
                nodePort.setText(loadBalancer.getNodes().get(0).getPort());
279

280
                TextView condition = (TextView) findViewById(R.id.view_node_condition);
281
                condition.setText(loadBalancer.getNodes().get(0).getCondition());
282

283
                TextView nodeStatus = (TextView) findViewById(R.id.view_node_status);
284
                nodeStatus.setText(loadBalancer.getNodes().get(0).getStatus());
285
                 */
286
        }
287

    
288
        //setup menu for when menu button is pressed
289
        public boolean onCreateOptionsMenu(Menu menu) {
290
                super.onCreateOptionsMenu(menu);
291
                MenuInflater inflater = getMenuInflater();
292
                inflater.inflate(R.menu.view_loadbalancer_menu, menu);
293
                return true;
294
        } 
295

    
296
        @Override 
297
        public boolean onOptionsItemSelected(MenuItem item) {
298
                switch (item.getItemId()) {
299
                case R.id.refresh_loadbalancer:
300
                        new LoadLoadBalancerTask().execute((Void[]) null);   
301
                        return true;
302
                }        
303
                return false;
304
        } 
305

    
306
        @Override
307
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
308
                super.onActivityResult(requestCode, resultCode, data);
309

    
310
                if (resultCode == RESULT_OK) {
311
                        new LoadLoadBalancerTask().execute((Void[]) null);   
312
                }
313
        }
314

    
315
        // HTTP request tasks
316
        private class PollLoadBalancerTask extends AsyncTask<Void, Void, LoadBalancer> {
317

    
318
                @Override
319
                protected LoadBalancer doInBackground(Void... arg0) {
320
                        try {
321
                                loadBalancer = (new LoadBalancerManager(getContext())).getLoadBalancerById(Integer.parseInt(loadBalancer.getId()));
322
                        } catch (NumberFormatException e) {
323
                                // we're polling, so need to show exceptions
324
                        } catch (LoadBalancersException e) {
325
                                // we're polling, so need to show exceptions
326
                        }
327
                        return loadBalancer;
328
                }
329

    
330
                @Override
331
                protected void onPostExecute(LoadBalancer result) {
332
                        loadBalancer = result;
333
                        loadLoadBalancerData();
334
                }
335
        }
336

    
337
        private class LoadLoadBalancerTask extends AsyncTask<Void, Void, LoadBalancer> {
338

    
339
                private LoadBalancersException exception;
340
                private String loadBalancerId;
341

    
342
                protected void onPreExecute() {
343
                        loadBalancerId = loadBalancer.getId();
344
                        /*
345
                         * set to null, so if config change occurs
346
                         * it will be reloaded in onCreate()
347
                         */
348
                        loadBalancer = null;
349
                        showDialog();
350
                }
351

    
352
                @Override
353
                protected LoadBalancer doInBackground(Void... arg0) {
354
                        LoadBalancer result = null;
355
                        try {
356
                                result = (new LoadBalancerManager(getContext())).getLoadBalancerById(Integer.parseInt(loadBalancerId));
357
                        } catch (LoadBalancersException e) {
358
                                exception = e;
359
                        }
360
                        return result;
361
                }
362

    
363
                @Override
364
                protected void onPostExecute(LoadBalancer result) {
365
                        hideDialog();
366
                        if (exception != null) {
367
                                showAlert("Error", exception.getMessage());
368
                        }
369
                        loadBalancer = result;
370

    
371
                        if(!loadBalancer.getStatus().contains("DELETE")){
372
                                setUpButtons();
373
                        } else {
374
                                setUpBadButtons();
375
                        }
376
                        loadLoadBalancerData();
377
                }
378
        } 
379

    
380
        public class DeleteLoadBalancerTask extends AsyncTask<Void, Void, HttpBundle> {
381

    
382
                private CloudServersException exception;
383

    
384
                @Override
385
                protected void onPreExecute(){
386
                        showDialog();
387
                }
388

    
389
                @Override
390
                protected HttpBundle doInBackground(Void... arg0) {
391
                        HttpBundle bundle = null;
392
                        try {
393
                                bundle = (new LoadBalancerManager(getContext())).delete(loadBalancer);
394
                        } catch (CloudServersException e) {
395
                                exception = e;
396
                        }
397
                        return bundle;
398
                }
399

    
400
                @Override
401
                protected void onPostExecute(HttpBundle bundle) {
402
                        hideDialog();
403
                        HttpResponse response = bundle.getResponse();
404
                        if (response != null) {
405
                                int statusCode = response.getStatusLine().getStatusCode();
406
                                if (statusCode == 202) {
407
                                        //showToast("Delete successful");
408
                                        setResult(Activity.RESULT_OK);
409
                                        finish();
410
                                } else {
411
                                        CloudServersException cse = parseCloudServersException(response);
412
                                        if ("".equals(cse.getMessage())) {
413
                                                showError("There was a problem deleting your load balancer.", bundle);
414
                                        } else {
415
                                                showError("There was a problem deleting your load balancer: " + cse.getMessage(), bundle);
416
                                        }
417
                                }
418
                        } else if (exception != null) {
419
                                showError("There was a problem deleting your load balancer: " + exception.getMessage(), bundle);                                
420
                        }                        
421
                }
422
        }
423

    
424

    
425
}