root / src / com / rackspacecloud / android / AddLoadBalancerActivity.java @ 403bb53b
History | View | Annotate | Download (11.8 kB)
1 |
package com.rackspacecloud.android; |
---|---|
2 |
|
3 |
import java.util.ArrayList; |
4 |
|
5 |
import org.apache.http.HttpResponse; |
6 |
|
7 |
import com.rackspace.cloud.loadbalancer.api.client.Algorithm; |
8 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer; |
9 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager; |
10 |
import com.rackspace.cloud.loadbalancer.api.client.Node; |
11 |
import com.rackspace.cloud.loadbalancer.api.client.Protocol; |
12 |
import com.rackspace.cloud.loadbalancer.api.client.VirtualIp; |
13 |
import com.rackspace.cloud.servers.api.client.Account; |
14 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
15 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
16 |
|
17 |
import android.app.Activity; |
18 |
import android.content.Intent; |
19 |
import android.os.AsyncTask; |
20 |
import android.os.Bundle; |
21 |
import android.text.Editable; |
22 |
import android.text.TextWatcher; |
23 |
import android.util.Log; |
24 |
import android.view.View; |
25 |
import android.view.View.OnClickListener; |
26 |
import android.widget.AdapterView; |
27 |
import android.widget.AdapterView.OnItemSelectedListener; |
28 |
import android.widget.ArrayAdapter; |
29 |
import android.widget.Button; |
30 |
import android.widget.EditText; |
31 |
import android.widget.Spinner; |
32 |
import android.widget.TableRow;; |
33 |
|
34 |
public class AddLoadBalancerActivity extends CloudActivity implements OnItemSelectedListener { |
35 |
|
36 |
//TODO Shared Virtual IP (not in API though?)
|
37 |
private static final String[] VIP_TYPES = {"Public", "ServiceNet", "Shared"}; |
38 |
|
39 |
private static final int ADD_NODES_ACTIVITY_CODE = 165; |
40 |
private static final int SHARED_VIP_ACTIVITY_CODE = 235; |
41 |
|
42 |
private Protocol[] protocols; |
43 |
private Protocol selectedProtocol;
|
44 |
private Algorithm[] algorithms; |
45 |
private Algorithm selectedAlgorithm;
|
46 |
private VirtualIp selectedVip;
|
47 |
private Spinner protocolSpinner;
|
48 |
private Spinner algorithmSpinner;
|
49 |
private Spinner vipSpinner;
|
50 |
private Spinner regionSpinner;
|
51 |
private Button selectVipButton; |
52 |
private Button selectNodesButton; |
53 |
private EditText portEditText;
|
54 |
private String selectedVipType; |
55 |
private String selectedRegion; |
56 |
private String selectedName; |
57 |
private String selectedPort; |
58 |
private ArrayList<Node> nodes; |
59 |
private LoadBalancer loadBalancer;
|
60 |
|
61 |
@Override
|
62 |
public void onCreate(Bundle savedInstanceState) { |
63 |
super.onCreate(savedInstanceState);
|
64 |
setContentView(R.layout.view_add_loadbalancer); |
65 |
restoreState(savedInstanceState); |
66 |
} |
67 |
|
68 |
@SuppressWarnings("unchecked") |
69 |
protected void restoreState(Bundle state) { |
70 |
super.restoreState(state);
|
71 |
|
72 |
if(state != null && state.containsKey("nodes")){ |
73 |
nodes = (ArrayList<Node>) state.getSerializable("nodes"); |
74 |
} |
75 |
else{
|
76 |
nodes = new ArrayList<Node>(); |
77 |
} |
78 |
setUpButtons(); |
79 |
setupText(); |
80 |
loadProtocolSpinner(); |
81 |
loadAlgorithmSpinner(); |
82 |
loadVipSpinner(); |
83 |
loadRegionSpinner(); |
84 |
} |
85 |
|
86 |
@Override
|
87 |
protected void onSaveInstanceState(Bundle outState) { |
88 |
super.onSaveInstanceState(outState);
|
89 |
outState.putSerializable("nodes", nodes);
|
90 |
} |
91 |
|
92 |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { |
93 |
if (parent == protocolSpinner) {
|
94 |
selectedProtocol = protocols[position]; |
95 |
//portText.setText(protocols[position].getDefaultPort());
|
96 |
} |
97 |
else if (parent == algorithmSpinner){ |
98 |
selectedAlgorithm = algorithms[position]; |
99 |
} |
100 |
else if (parent == vipSpinner){ |
101 |
selectedVipType = VIP_TYPES[position]; |
102 |
if(VIP_TYPES[position].equals("Shared")){ |
103 |
((TableRow) findViewById(R.id.select_vip_layout)).setVisibility(View.VISIBLE);
|
104 |
} else {
|
105 |
((TableRow) findViewById(R.id.select_vip_layout)).setVisibility(View.GONE);
|
106 |
} |
107 |
} |
108 |
else if (parent == regionSpinner){ |
109 |
selectedRegion = Account.getAccount().getLoadBalancerRegions()[position]; |
110 |
updateVipIndicatorLight(); |
111 |
} |
112 |
} |
113 |
|
114 |
public void onNothingSelected(AdapterView<?> parent) { |
115 |
|
116 |
} |
117 |
|
118 |
private void setUpButtons(){ |
119 |
selectVipButton = (Button) findViewById(R.id.selected_shared_vip);
|
120 |
updateVipIndicatorLight(); |
121 |
selectVipButton.setOnClickListener(new OnClickListener() {
|
122 |
@Override
|
123 |
public void onClick(View v) { |
124 |
Intent viewIntent = new Intent(getApplicationContext(), SharedVipActivity.class);
|
125 |
viewIntent.putExtra("loadBalancerPort", ((EditText)findViewById(R.id.edit_port_text)).getText().toString());
|
126 |
viewIntent.putExtra("loadBalancerRegion", selectedRegion);
|
127 |
startActivityForResult(viewIntent, SHARED_VIP_ACTIVITY_CODE); |
128 |
} |
129 |
}); |
130 |
((TableRow) findViewById(R.id.select_vip_layout)).setVisibility(View.GONE);
|
131 |
|
132 |
selectNodesButton = (Button) findViewById(R.id.add_nodes_button);
|
133 |
updateNodesIndicatorLight(); |
134 |
selectNodesButton.setOnClickListener(new OnClickListener() {
|
135 |
@Override
|
136 |
public void onClick(View v) { |
137 |
Intent viewIntent = new Intent(getApplicationContext(), AddNodesActivity.class);
|
138 |
viewIntent.putExtra("nodes", nodes);
|
139 |
startActivityForResult(viewIntent, ADD_NODES_ACTIVITY_CODE); |
140 |
} |
141 |
}); |
142 |
|
143 |
Button addLoadBalancer = (Button) findViewById(R.id.add_lb_button); |
144 |
addLoadBalancer.setOnClickListener(new OnClickListener() {
|
145 |
|
146 |
@Override
|
147 |
public void onClick(View v) { |
148 |
selectedName = ((EditText)findViewById(R.id.edit_lb_name_text)).getText().toString(); |
149 |
//selectedPort = ((EditText)findViewById(R.id.edit_port_text)).getText().toString();
|
150 |
if(!validName()){
|
151 |
showAlert("Error", "Load balancer name cannot be blank."); |
152 |
} else if(!validPort()){ |
153 |
showAlert("Error", "Must have a protocol port number that is between 1 and 65535."); |
154 |
} else if(!validVip()){ |
155 |
showAlert("Error", "Please select a valid Virtual IP."); |
156 |
} else if(!validNodes()){ |
157 |
showAlert("Error", "You must select at least one enabled cloud server or add and enable at least one external node."); |
158 |
} else {
|
159 |
loadBalancer = new LoadBalancer();
|
160 |
loadBalancer.setName(selectedName); |
161 |
loadBalancer.setProtocol(selectedProtocol.getName()); |
162 |
loadBalancer.setPort(selectedPort); |
163 |
loadBalancer.setVirtualIpType(selectedVipType); |
164 |
loadBalancer.setAlgorithm(selectedAlgorithm.getName()); |
165 |
loadBalancer.setNodes(nodes); |
166 |
if(selectedVip != null){ |
167 |
ArrayList<VirtualIp> vips = new ArrayList<VirtualIp>(); |
168 |
vips.add(selectedVip); |
169 |
loadBalancer.setVirtualIps(vips); |
170 |
} |
171 |
new AddLoadBalancerTask().execute();
|
172 |
} |
173 |
} |
174 |
}); |
175 |
} |
176 |
|
177 |
private boolean validName(){ |
178 |
return !selectedName.equals(""); |
179 |
} |
180 |
|
181 |
private boolean validPort(){ |
182 |
return !selectedPort.equals("") && Integer.valueOf(selectedPort) > 0 && Integer.valueOf(selectedPort) < 65536; |
183 |
} |
184 |
|
185 |
private boolean validNodes(){ |
186 |
return nodes != null && nodes.size() > 0; |
187 |
} |
188 |
|
189 |
private void updateNodesIndicatorLight(){ |
190 |
if(validNodes()){
|
191 |
selectNodesButton.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0); |
192 |
} else {
|
193 |
selectNodesButton.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.ic_notification_overlay,0); |
194 |
} |
195 |
} |
196 |
|
197 |
private boolean validVip(){ |
198 |
return selectedVip != null && !selectedVip.getLoadBalancer().getPort().equals(selectedPort) |
199 |
&& selectedVip.getLoadBalancer().getRegion().equals(selectedRegion); |
200 |
} |
201 |
|
202 |
private void updateVipIndicatorLight(){ |
203 |
if(validVip()){
|
204 |
selectVipButton.setCompoundDrawablesWithIntrinsicBounds(0,0,0,0); |
205 |
} else {
|
206 |
selectVipButton.setCompoundDrawablesWithIntrinsicBounds(0,0,R.drawable.ic_notification_overlay,0); |
207 |
} |
208 |
} |
209 |
|
210 |
private void setupText(){ |
211 |
portEditText = (EditText)findViewById(R.id.edit_port_text); |
212 |
portEditText.addTextChangedListener(new TextWatcher() {
|
213 |
|
214 |
@Override
|
215 |
public void onTextChanged(CharSequence s, int start, int before, int count) { |
216 |
selectedPort = s.toString(); |
217 |
updateVipIndicatorLight(); |
218 |
} |
219 |
@Override
|
220 |
public void beforeTextChanged(CharSequence s, int start, int count, |
221 |
int after) {
|
222 |
// do nothing
|
223 |
} |
224 |
@Override
|
225 |
public void afterTextChanged(Editable s) { |
226 |
// do nothing
|
227 |
} |
228 |
}); |
229 |
} |
230 |
|
231 |
private void loadRegionSpinner() { |
232 |
regionSpinner = (Spinner) findViewById(R.id.edit_region_spinner); |
233 |
regionSpinner.setOnItemSelectedListener(this);
|
234 |
|
235 |
ArrayAdapter<String> regionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, |
236 |
Account.getAccount().getLoadBalancerRegions()); |
237 |
regionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
238 |
regionSpinner.setAdapter(regionAdapter); |
239 |
} |
240 |
|
241 |
private void loadVipSpinner() { |
242 |
vipSpinner = (Spinner) findViewById(R.id.edit_vip_spinner); |
243 |
vipSpinner.setOnItemSelectedListener(this);
|
244 |
|
245 |
ArrayAdapter<String> vipAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, VIP_TYPES); |
246 |
vipAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
247 |
vipSpinner.setAdapter(vipAdapter); |
248 |
} |
249 |
|
250 |
private void loadProtocolSpinner() { |
251 |
protocolSpinner = (Spinner) findViewById(R.id.edit_protocol_spinner); |
252 |
protocolSpinner.setOnItemSelectedListener(this);
|
253 |
String protocolNames[] = new String[Protocol.getProtocols().size()]; |
254 |
protocols = new Protocol[Protocol.getProtocols().size()];
|
255 |
|
256 |
for(int i = 0; i < Protocol.getProtocols().size(); i++){ |
257 |
protocols[i] = Protocol.getProtocols().get(i); |
258 |
protocolNames[i] = Protocol.getProtocols().get(i).getName(); |
259 |
} |
260 |
|
261 |
ArrayAdapter<String> protocolAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, protocolNames); |
262 |
protocolAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
263 |
protocolSpinner.setAdapter(protocolAdapter); |
264 |
} |
265 |
|
266 |
private void loadAlgorithmSpinner() { |
267 |
algorithmSpinner = (Spinner) findViewById(R.id.edit_algorithm_spinner); |
268 |
algorithmSpinner.setOnItemSelectedListener(this);
|
269 |
String algorithmNames[] = new String[Algorithm.getAlgorithms().size()]; |
270 |
algorithms = new Algorithm[Algorithm.getAlgorithms().size()];
|
271 |
|
272 |
for(int i = 0; i < Algorithm.getAlgorithms().size(); i++){ |
273 |
algorithms[i] = Algorithm.getAlgorithms().get(i); |
274 |
algorithmNames[i] = Algorithm.getAlgorithms().get(i).getName(); |
275 |
} |
276 |
|
277 |
ArrayAdapter<String> algorithmAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, algorithmNames); |
278 |
algorithmAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
279 |
algorithmSpinner.setAdapter(algorithmAdapter); |
280 |
} |
281 |
|
282 |
private class AddLoadBalancerTask extends AsyncTask<Void, Void, HttpBundle> { |
283 |
private CloudServersException exception;
|
284 |
|
285 |
protected void onPreExecute(){ |
286 |
showDialog(); |
287 |
} |
288 |
|
289 |
@Override
|
290 |
protected HttpBundle doInBackground(Void... arg0) { |
291 |
HttpBundle bundle = null;
|
292 |
try {
|
293 |
bundle = (new LoadBalancerManager(getContext())).create(loadBalancer, LoadBalancer.getRegionUrl(selectedRegion));
|
294 |
} catch (CloudServersException e) {
|
295 |
exception = e; |
296 |
} |
297 |
return bundle;
|
298 |
} |
299 |
|
300 |
@Override
|
301 |
protected void onPostExecute(HttpBundle bundle) { |
302 |
hideDialog(); |
303 |
HttpResponse response = bundle.getResponse(); |
304 |
if (response != null) { |
305 |
int statusCode = response.getStatusLine().getStatusCode();
|
306 |
if (statusCode == 202) { |
307 |
setResult(Activity.RESULT_OK); |
308 |
finish(); |
309 |
} else {
|
310 |
CloudServersException cse = parseCloudServersException(response); |
311 |
if ("".equals(cse.getMessage())) { |
312 |
showError("There was a problem creating your load balancer.", bundle);
|
313 |
} else {
|
314 |
//if container with same name already exists
|
315 |
showError("There was a problem creating your load balancer: " + cse.getMessage() + "\n See details for more information", bundle); |
316 |
} |
317 |
} |
318 |
} else if (exception != null) { |
319 |
showError("There was a problem creating your container: " + exception.getMessage()+"\n See details for more information.", bundle); |
320 |
} |
321 |
finish(); |
322 |
} |
323 |
} |
324 |
|
325 |
@SuppressWarnings("unchecked") |
326 |
@Override
|
327 |
protected void onActivityResult (int requestCode, int resultCode, Intent data){ |
328 |
if(requestCode == ADD_NODES_ACTIVITY_CODE && resultCode == RESULT_OK){
|
329 |
//set node list
|
330 |
nodes = ((ArrayList<Node>)data.getSerializableExtra("nodes")); |
331 |
updateNodesIndicatorLight(); |
332 |
} |
333 |
else if(requestCode == ADD_NODES_ACTIVITY_CODE && resultCode == RESULT_CANCELED){ |
334 |
//don't change list
|
335 |
} |
336 |
else if(requestCode == SHARED_VIP_ACTIVITY_CODE && resultCode == RESULT_OK){ |
337 |
selectedVip = (VirtualIp)data.getSerializableExtra("selectedVip");
|
338 |
updateVipIndicatorLight(); |
339 |
} |
340 |
} |
341 |
} |