Revision 403bb53b src/com/rackspacecloud/android/AddLoadBalancerActivity.java
b/src/com/rackspacecloud/android/AddLoadBalancerActivity.java | ||
---|---|---|
9 | 9 |
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager; |
10 | 10 |
import com.rackspace.cloud.loadbalancer.api.client.Node; |
11 | 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; |
|
12 | 14 |
import com.rackspace.cloud.servers.api.client.CloudServersException; |
13 | 15 |
import com.rackspace.cloud.servers.api.client.http.HttpBundle; |
14 | 16 |
|
... | ... | |
16 | 18 |
import android.content.Intent; |
17 | 19 |
import android.os.AsyncTask; |
18 | 20 |
import android.os.Bundle; |
21 |
import android.text.Editable; |
|
22 |
import android.text.TextWatcher; |
|
23 |
import android.util.Log; |
|
19 | 24 |
import android.view.View; |
20 | 25 |
import android.view.View.OnClickListener; |
21 | 26 |
import android.widget.AdapterView; |
... | ... | |
24 | 29 |
import android.widget.Button; |
25 | 30 |
import android.widget.EditText; |
26 | 31 |
import android.widget.Spinner; |
32 |
import android.widget.TableRow;; |
|
27 | 33 |
|
28 | 34 |
public class AddLoadBalancerActivity extends CloudActivity implements OnItemSelectedListener { |
29 | 35 |
|
30 | 36 |
//TODO Shared Virtual IP (not in API though?) |
31 |
private static final String[] VIP_TYPES = {"Public", "ServiceNet"}; |
|
32 |
private static final String[] REGIONS = {"ORD", "DFW"}; |
|
37 |
private static final String[] VIP_TYPES = {"Public", "ServiceNet", "Shared"}; |
|
33 | 38 |
|
34 | 39 |
private static final int ADD_NODES_ACTIVITY_CODE = 165; |
40 |
private static final int SHARED_VIP_ACTIVITY_CODE = 235; |
|
35 | 41 |
|
36 | 42 |
private Protocol[] protocols; |
43 |
private Protocol selectedProtocol; |
|
37 | 44 |
private Algorithm[] algorithms; |
45 |
private Algorithm selectedAlgorithm; |
|
46 |
private VirtualIp selectedVip; |
|
38 | 47 |
private Spinner protocolSpinner; |
39 | 48 |
private Spinner algorithmSpinner; |
40 | 49 |
private Spinner vipSpinner; |
41 | 50 |
private Spinner regionSpinner; |
42 |
private Protocol selectedProtocol; |
|
43 |
private Algorithm selectedAlgorithm; |
|
51 |
private Button selectVipButton; |
|
52 |
private Button selectNodesButton; |
|
53 |
private EditText portEditText; |
|
44 | 54 |
private String selectedVipType; |
45 | 55 |
private String selectedRegion; |
46 | 56 |
private String selectedName; |
... | ... | |
65 | 75 |
else{ |
66 | 76 |
nodes = new ArrayList<Node>(); |
67 | 77 |
} |
68 |
|
|
78 |
setUpButtons(); |
|
79 |
setupText(); |
|
69 | 80 |
loadProtocolSpinner(); |
70 | 81 |
loadAlgorithmSpinner(); |
71 | 82 |
loadVipSpinner(); |
72 | 83 |
loadRegionSpinner(); |
73 |
setUpButtons(); |
|
74 | 84 |
} |
75 | 85 |
|
76 | 86 |
@Override |
... | ... | |
89 | 99 |
} |
90 | 100 |
else if (parent == vipSpinner){ |
91 | 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 |
} |
|
92 | 107 |
} |
93 | 108 |
else if (parent == regionSpinner){ |
94 |
selectedRegion = REGIONS[position]; |
|
109 |
selectedRegion = Account.getAccount().getLoadBalancerRegions()[position]; |
|
110 |
updateVipIndicatorLight(); |
|
95 | 111 |
} |
96 | 112 |
} |
97 | 113 |
|
98 | 114 |
public void onNothingSelected(AdapterView<?> parent) { |
99 | 115 |
|
100 | 116 |
} |
101 |
|
|
117 |
|
|
102 | 118 |
private void setUpButtons(){ |
103 |
Button addNodes = (Button) findViewById(R.id.add_nodes_button); |
|
104 |
addNodes.setOnClickListener(new OnClickListener() { |
|
105 |
|
|
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() { |
|
106 | 135 |
@Override |
107 | 136 |
public void onClick(View v) { |
108 | 137 |
Intent viewIntent = new Intent(getApplicationContext(), AddNodesActivity.class); |
... | ... | |
117 | 146 |
@Override |
118 | 147 |
public void onClick(View v) { |
119 | 148 |
selectedName = ((EditText)findViewById(R.id.edit_lb_name_text)).getText().toString(); |
120 |
selectedPort = ((EditText)findViewById(R.id.edit_port_text)).getText().toString(); |
|
149 |
//selectedPort = ((EditText)findViewById(R.id.edit_port_text)).getText().toString();
|
|
121 | 150 |
if(!validName()){ |
122 | 151 |
showAlert("Error", "Load balancer name cannot be blank."); |
123 |
} |
|
124 |
else if(!validPort()){ |
|
152 |
} else if(!validPort()){ |
|
125 | 153 |
showAlert("Error", "Must have a protocol port number that is between 1 and 65535."); |
126 |
} |
|
127 |
else{ |
|
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 { |
|
128 | 159 |
loadBalancer = new LoadBalancer(); |
129 | 160 |
loadBalancer.setName(selectedName); |
130 | 161 |
loadBalancer.setProtocol(selectedProtocol.getName()); |
... | ... | |
132 | 163 |
loadBalancer.setVirtualIpType(selectedVipType); |
133 | 164 |
loadBalancer.setAlgorithm(selectedAlgorithm.getName()); |
134 | 165 |
loadBalancer.setNodes(nodes); |
166 |
if(selectedVip != null){ |
|
167 |
ArrayList<VirtualIp> vips = new ArrayList<VirtualIp>(); |
|
168 |
vips.add(selectedVip); |
|
169 |
loadBalancer.setVirtualIps(vips); |
|
170 |
} |
|
135 | 171 |
new AddLoadBalancerTask().execute(); |
136 | 172 |
} |
137 | 173 |
} |
138 | 174 |
}); |
139 | 175 |
} |
140 |
|
|
176 |
|
|
141 | 177 |
private boolean validName(){ |
142 | 178 |
return !selectedName.equals(""); |
143 | 179 |
} |
... | ... | |
145 | 181 |
private boolean validPort(){ |
146 | 182 |
return !selectedPort.equals("") && Integer.valueOf(selectedPort) > 0 && Integer.valueOf(selectedPort) < 65536; |
147 | 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 |
} |
|
148 | 230 |
|
149 | 231 |
private void loadRegionSpinner() { |
150 | 232 |
regionSpinner = (Spinner) findViewById(R.id.edit_region_spinner); |
151 | 233 |
regionSpinner.setOnItemSelectedListener(this); |
152 | 234 |
|
153 |
ArrayAdapter<String> regionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, REGIONS); |
|
235 |
ArrayAdapter<String> regionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, |
|
236 |
Account.getAccount().getLoadBalancerRegions()); |
|
154 | 237 |
regionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); |
155 | 238 |
regionSpinner.setAdapter(regionAdapter); |
156 | 239 |
} |
... | ... | |
196 | 279 |
algorithmSpinner.setAdapter(algorithmAdapter); |
197 | 280 |
} |
198 | 281 |
|
199 |
@SuppressWarnings("unchecked") |
|
200 |
@Override |
|
201 |
protected void onActivityResult (int requestCode, int resultCode, Intent data){ |
|
202 |
if(requestCode == ADD_NODES_ACTIVITY_CODE && resultCode == RESULT_OK){ |
|
203 |
//set node list |
|
204 |
nodes = ((ArrayList<Node>)data.getSerializableExtra("nodes")); |
|
205 |
} |
|
206 |
else if(requestCode == ADD_NODES_ACTIVITY_CODE && resultCode == RESULT_CANCELED){ |
|
207 |
//don't change list |
|
208 |
|
|
209 |
} |
|
210 |
} |
|
211 |
|
|
212 | 282 |
private class AddLoadBalancerTask extends AsyncTask<Void, Void, HttpBundle> { |
213 | 283 |
private CloudServersException exception; |
214 | 284 |
|
215 | 285 |
protected void onPreExecute(){ |
216 | 286 |
showDialog(); |
217 | 287 |
} |
218 |
|
|
288 |
|
|
219 | 289 |
@Override |
220 | 290 |
protected HttpBundle doInBackground(Void... arg0) { |
221 | 291 |
HttpBundle bundle = null; |
... | ... | |
251 | 321 |
finish(); |
252 | 322 |
} |
253 | 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 |
} |
|
254 | 341 |
} |
Also available in: Unified diff