Skip to content

Commit 03ef8d5

Browse files
author
anasouma
committed
Merge branch 'Loadbal_Support' of
https://github.com/anasouma/softlayer-python into Loadbal_Support Conflicts: SoftLayer/CLI/environment.py SoftLayer/managers/__init__.py SoftLayer/managers/load_balancer.py
2 parents fdd84e2 + 1c2556d commit 03ef8d5

3 files changed

Lines changed: 72 additions & 67 deletions

File tree

SoftLayer/CLI/modules/loadbal.py

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
group-delete Delete a service group from the load balancer
1313
group-edit Edit the properties of a service group
1414
group-reset Resets all the connections on a service group
15-
health-checks List the different health check values
15+
health-check List the different health check values
1616
list List active load balancers
1717
routing-methods List supported routing methods
1818
routing-types List supported routing types
@@ -29,6 +29,19 @@
2929
from SoftLayer.CLI.helpers import CLIAbort
3030

3131

32+
def get_ids(input_id):
33+
""" Helper package to retrieve the actual IDs
34+
35+
:param input_id: the ID provided by the user
36+
:returns: A list of valid IDs
37+
"""
38+
key_value = input_id.split(':')
39+
if len(key_value) != 2:
40+
raise CLIAbort('Invalid ID %s: ID should be of the form xxx:yyy'
41+
% input_id)
42+
return key_value
43+
44+
3245
def get_local_lbs_table(load_balancers):
3346
""" Helper package to format the local load balancers into a table.
3447
@@ -42,11 +55,7 @@ def get_local_lbs_table(load_balancers):
4255
'Connections/second',
4356
'Type'])
4457

45-
table.align['VIP Address'] = 'r'
46-
table.align['Location'] = 'r'
47-
table.align['Connections/second'] = 'r'
4858
table.align['Connections/second'] = 'r'
49-
table.align['Type'] = 'r'
5059

5160
for load_balancer in load_balancers:
5261
ssl_support = 'Not Supported'
@@ -113,7 +122,9 @@ def get_local_lb_table(load_balancer):
113122

114123
table3 = Table(['Service_ID', 'IP Address', 'Port',
115124
'Health Check', 'Weight', 'Enabled', 'Status'])
125+
service_exist = False
116126
for service in group['services']:
127+
service_exist = True
117128
health_check = service['healthChecks'][0]
118129
table3.add_row([
119130
'%s:%s' % (load_balancer['id'], service['id']),
@@ -125,7 +136,10 @@ def get_local_lb_table(load_balancer):
125136
service['enabled'],
126137
service['status']
127138
])
128-
table.add_row([' Services', table3])
139+
if service_exist:
140+
table.add_row([' Services', table3])
141+
else:
142+
table.add_row([' Services', 'None'])
129143
return table
130144

131145

@@ -147,11 +161,11 @@ def execute(self, args):
147161

148162
class LoadBalancerHealthChecks(CLIRunnable):
149163
"""
150-
usage: sl loadbal health-check-types [options]
164+
usage: sl loadbal health-checks [options]
151165
152166
List load balancer service health check types that can be used
153167
"""
154-
action = 'health-check-types'
168+
action = 'health-checks'
155169

156170
def execute(self, args):
157171
mgr = LoadBalancerManager(self.client)
@@ -222,7 +236,7 @@ def execute(self, args):
222236

223237
input_id = args.get('<identifier>')
224238

225-
key_value = input_id.split(':')
239+
key_value = get_ids(input_id)
226240
loadbal_id = int(key_value[1])
227241

228242
load_balancer = mgr.get_local_lb(loadbal_id)
@@ -234,18 +248,16 @@ class LoadBalancerCancel(CLIRunnable):
234248
usage: sl loadbal cancel <identifier> [options]
235249
236250
Cancels an existing load_balancer
237-
Options:
238-
--really Whether to skip the confirmation prompt
239251
240252
"""
241253
action = 'cancel'
242-
options = ['really']
254+
options = ['confirm']
243255

244256
def execute(self, args):
245257
mgr = LoadBalancerManager(self.client)
246258
input_id = args.get('<identifier>')
247259

248-
key_value = input_id.split(':')
260+
key_value = get_ids(input_id)
249261
loadbal_id = int(key_value[1])
250262

251263
if args['--really'] or confirm("This action will cancel a load "
@@ -261,18 +273,16 @@ class LoadBalancerServiceDelete(CLIRunnable):
261273
usage: sl loadbal service-delete <identifier> [options]
262274
263275
Deletes an existing load_balancer service
264-
Options:
265-
--really Whether to skip the confirmation prompt
266276
267277
"""
268278
action = 'service-delete'
269-
options = ['really']
279+
options = ['confirm']
270280

271281
def execute(self, args):
272282
mgr = LoadBalancerManager(self.client)
273283
input_id = args.get('<identifier>')
274284

275-
key_value = input_id.split(':')
285+
key_value = get_ids(input_id)
276286
service_id = int(key_value[1])
277287

278288
if args['--really'] or confirm("This action will cancel a service "
@@ -288,18 +298,16 @@ class LoadBalancerServiceToggle(CLIRunnable):
288298
usage: sl loadbal service-toggle <identifier> [options]
289299
290300
Toggle the status of an existing load_balancer service
291-
Options:
292-
--really Whether to skip the confirmation prompt
293301
294302
"""
295303
action = 'service-toggle'
296-
options = ['really']
304+
options = ['confirm']
297305

298306
def execute(self, args):
299307
mgr = LoadBalancerManager(self.client)
300308
input_id = args.get('<identifier>')
301309

302-
key_value = input_id.split(':')
310+
key_value = get_ids(input_id)
303311
service_id = int(key_value[1])
304312

305313
if args['--really'] or confirm("This action will toggle the service "
@@ -329,7 +337,7 @@ def execute(self, args):
329337
mgr = LoadBalancerManager(self.client)
330338
input_id = args.get('<identifier>')
331339

332-
key_value = input_id.split(':')
340+
key_value = get_ids(input_id)
333341
loadbal_id = int(key_value[0])
334342
service_id = int(key_value[1])
335343

@@ -339,7 +347,7 @@ def execute(self, args):
339347
return 'At least one property is required to be changed!'
340348

341349
# check if the IP is valid
342-
ip_address_id = 0
350+
ip_address_id = None
343351
if args['--ip']:
344352
ip_address = mgr.get_ip_address(args['--ip'])
345353
if not ip_address:
@@ -350,10 +358,10 @@ def execute(self, args):
350358
mgr.edit_service(loadbal_id,
351359
service_id,
352360
ip_address_id=ip_address_id,
353-
enabled=int(args.get('--enabled') or -1),
354-
port=int(args.get('--port') or -1),
355-
weight=int(args.get('--weight') or -1),
356-
hc_type=int(args.get('--hc_type') or -1))
361+
enabled=args.get('--enabled'),
362+
port=args.get('--port'),
363+
weight=args.get('--weight'),
364+
hc_type=args.get('--hc_type'))
357365
return 'Load balancer service %s is being modified!' % input_id
358366

359367

@@ -377,7 +385,7 @@ def execute(self, args):
377385
mgr = LoadBalancerManager(self.client)
378386
input_id = args.get('<identifier>')
379387

380-
key_value = input_id.split(':')
388+
key_value = get_ids(input_id)
381389
loadbal_id = int(key_value[0])
382390
group_id = int(key_value[1])
383391

@@ -403,18 +411,16 @@ class LoadBalancerServiceGroupDelete(CLIRunnable):
403411
usage: sl loadbal group-delete <identifier> [options]
404412
405413
Deletes an existing load_balancer service group
406-
Options:
407-
--really Whether to skip the confirmation prompt
408414
409415
"""
410416
action = 'group-delete'
411-
options = ['really']
417+
options = ['confirm']
412418

413419
def execute(self, args):
414420
mgr = LoadBalancerManager(self.client)
415421
input_id = args.get('<identifier>')
416422

417-
key_value = input_id.split(':')
423+
key_value = get_ids(input_id)
418424
group_id = int(key_value[1])
419425

420426
if args['--really'] or confirm("This action will cancel a service"
@@ -431,7 +437,7 @@ class LoadBalancerServiceGroupEdit(CLIRunnable):
431437
432438
Edits an existing load_balancer service group
433439
Options:
434-
--allocation=ALLOC Change the allocated % of connections
440+
--allocation=PERC Change the allocated % of connections
435441
--port=PORT Change the port
436442
--routing_type=TYPE Change the port routing type
437443
--routing_method=METHOD Change the routing method
@@ -443,7 +449,7 @@ def execute(self, args):
443449
mgr = LoadBalancerManager(self.client)
444450
input_id = args.get('<identifier>')
445451

446-
key_value = input_id.split(':')
452+
key_value = get_ids(input_id)
447453
loadbal_id = int(key_value[0])
448454
group_id = int(key_value[1])
449455

@@ -457,10 +463,10 @@ def execute(self, args):
457463

458464
mgr.edit_service_group(loadbal_id,
459465
group_id,
460-
allocation=int(args.get('--allocation') or -1),
461-
port=int(args.get('--port') or 0),
462-
routing_type=int(routing_type or 0),
463-
routing_method=int(routing_method or 0))
466+
allocation=args.get('--allocation'),
467+
port=args.get('--port'),
468+
routing_type=routing_type,
469+
routing_method=routing_method)
464470

465471
return 'Load balancer service group %s is being updated!' % input_id
466472

@@ -478,7 +484,7 @@ def execute(self, args):
478484
mgr = LoadBalancerManager(self.client)
479485
input_id = args.get('<identifier>')
480486

481-
key_value = input_id.split(':')
487+
key_value = get_ids(input_id)
482488
loadbal_id = int(key_value[0])
483489
group_id = int(key_value[1])
484490

@@ -488,12 +494,12 @@ def execute(self, args):
488494

489495
class LoadBalancerServiceGroupAdd(CLIRunnable):
490496
"""
491-
usage: sl loadbal group-add <identifier> --allocation=ALLOC --port=PORT \
497+
usage: sl loadbal group-add <identifier> --allocation=PERC --port=PORT \
492498
--routing_type=TYPE --routing_method=METHOD [options]
493499
494500
Adds a new load_balancer service
495501
Required:
496-
--allocation=ALLOC The % of connections that will be allocated
502+
--allocation=PERC The % of connections that will be allocated
497503
--port=PORT The virtual port number for the group
498504
--routing_type=TYPE The routing type for the group
499505
--routing_method=METHOD The routing method for the group
@@ -504,7 +510,7 @@ class LoadBalancerServiceGroupAdd(CLIRunnable):
504510
def execute(self, args):
505511
mgr = LoadBalancerManager(self.client)
506512
input_id = args.get('<identifier>')
507-
key_value = input_id.split(':')
513+
key_value = get_ids(input_id)
508514

509515
loadbal_id = int(key_value[1])
510516

@@ -527,10 +533,9 @@ class LoadBalancerCreate(CLIRunnable):
527533
-d, --datacenter=DC Datacenter shortname (sng01, dal05, ...)
528534
Note: Omitting this value defaults to the first
529535
available datacenter
530-
--really Whether to skip the confirmation prompt
531536
"""
532537
action = 'create'
533-
options = ['really']
538+
options = ['confirm']
534539

535540
def execute(self, args):
536541
mgr = LoadBalancerManager(self.client)

SoftLayer/managers/load_balancer.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ def toggle_service_status(self, service_id):
170170
'LoadBalancer_Service']
171171
return svc.toggleStatus(id=service_id)
172172

173-
def edit_service(self, loadbal_id, service_id, ip_address_id=0, port=-1,
174-
enabled=-1, hc_type=-1, weight=-1):
173+
def edit_service(self, loadbal_id, service_id, ip_address_id=None,
174+
port=None, enabled=None, hc_type=None, weight=None):
175175
""" Edits an existing service properties
176176
:param int loadbal_id: The id of the loadbal where the service resides
177177
:param int service_id: The id of the service to edit
@@ -194,15 +194,16 @@ def edit_service(self, loadbal_id, service_id, ip_address_id=0, port=-1,
194194
**kwargs)
195195
for service in virtual_servers[0]['serviceGroups'][0]['services']:
196196
if service['id'] == service_id:
197-
if enabled != -1:
198-
service['enabled'] = enabled
199-
if port != -1:
200-
service['port'] = port
201-
if weight != -1:
202-
service['groupReferences'][0]['weight'] = weight
203-
if hc_type != -1:
204-
service['healthChecks'][0]['healthCheckTypeId'] = hc_type
205-
if ip_address_id != 0:
197+
if enabled is not None:
198+
service['enabled'] = int(enabled)
199+
if port is not None:
200+
service['port'] = int(port)
201+
if weight is not None:
202+
service['groupReferences'][0]['weight'] = int(weight)
203+
if hc_type is not None:
204+
service['healthChecks'][0]['healthCheckTypeId'] = \
205+
int(hc_type)
206+
if ip_address_id is not None:
206207
service['ipAddressId'] = ip_address_id
207208

208209
template = {'virtualServers': virtual_servers}
@@ -277,8 +278,8 @@ def add_service_group(self, lb_id, allocation=100, port=80,
277278
virtual_servers.append(service_template)
278279
return self.lb_svc.editObject(load_balancer, id=lb_id)
279280

280-
def edit_service_group(self, loadbal_id, group_id, allocation=-1, port=0,
281-
routing_type=0, routing_method=0):
281+
def edit_service_group(self, loadbal_id, group_id, allocation=None,
282+
port=None, routing_type=None, routing_method=None):
282283
""" Edit an existing service group
283284
:param int loadbal_id: The id of the loadbal where the service resides
284285
:param int group_id: The id of the service group
@@ -296,14 +297,14 @@ def edit_service_group(self, loadbal_id, group_id, allocation=-1, port=0,
296297
for virtual_server in virtual_servers:
297298
if virtual_server['id'] == group_id:
298299
service_group = virtual_server['serviceGroups'][0]
299-
if allocation != -1:
300-
virtual_server['allocation'] = allocation
301-
if port != 0:
302-
virtual_server['port'] = port
303-
if routing_type != 0:
304-
service_group['routingTypeId'] = routing_type
305-
if routing_method != 0:
306-
service_group['routingMethodId'] = routing_method
300+
if allocation is not None:
301+
virtual_server['allocation'] = int(allocation)
302+
if port is not None:
303+
virtual_server['port'] = int(port)
304+
if routing_type is not None:
305+
service_group['routingTypeId'] = int(routing_type)
306+
if routing_method is not None:
307+
service_group['routingMethodId'] = int(routing_method)
307308
break
308309
return self.lb_svc.editObject(load_balancer, id=loadbal_id)
309310

setup.cfg

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
verbosity=2
33
detailed-errors=1
44
with-coverage=1
5-
cover-min-percentage=68
65
cover-erase=true
76
cover-package=SoftLayer
87
cover-html=1
98

109
[wheel]
11-
universal=1
10+
universal=1

0 commit comments

Comments
 (0)