Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / ViewLoadBalancerActivity.java @ 1b82ddb3

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
                setContentView(R.layout.view_loadbalancer);
51
                restoreState(savedInstanceState);
52
        }
53

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

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

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

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

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

    
88
                });
89

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

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

    
98
                });
99

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

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

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

    
122
                });
123

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

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

    
132
                });
133

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

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

    
143

    
144
        }
145

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

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

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

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

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

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

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

    
196

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

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

    
202
                        loadVirutalIpData();
203
                }
204
        }
205

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

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

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

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

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

    
235
                loadNodeData();
236
        }
237

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
338
                private LoadBalancersException exception;
339
                private String loadBalancerId;
340

    
341
                protected void onPreExecute() {
342
                        Log.d("info", "load called");
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
                        Log.d("info", "in post");
366
                        hideDialog();
367
                        if (exception != null) {
368
                                showAlert("Error", exception.getMessage());
369
                        }
370
                        loadBalancer = result;
371

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

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

    
383
                private CloudServersException exception;
384

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

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

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

    
425

    
426
}