Skip to content

Commit 98feac7

Browse files
Merge pull request softlayer#1553 from ATGE/issues/1550-loadbal-l7policies
Issues/1550 loadbal l7policies
2 parents b364ea6 + 26f2052 commit 98feac7

File tree

7 files changed

+128
-0
lines changed

7 files changed

+128
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""List Layer7 policies"""
2+
import click
3+
4+
import SoftLayer
5+
from SoftLayer.CLI import environment
6+
from SoftLayer.CLI import formatting
7+
8+
9+
@click.command()
10+
@click.option('--protocol-id', '-p',
11+
required=False,
12+
type=int,
13+
help="Front-end Protocol identifier")
14+
@environment.pass_env
15+
def policies(env, protocol_id):
16+
"""List policies of the front-end protocol (listener)."""
17+
mgr = SoftLayer.LoadBalancerManager(env.client)
18+
19+
if protocol_id:
20+
l7policies = mgr.get_l7policies(protocol_id)
21+
table = generate_l7policies_table(l7policies, protocol_id)
22+
else:
23+
l7policies = mgr.get_all_l7policies()
24+
table = l7policies_table(l7policies)
25+
env.fout(table)
26+
27+
28+
def generate_l7policies_table(l7policies, identifier):
29+
"""Takes a list of Layer7 policies and makes a table"""
30+
table = formatting.Table([
31+
'Id', 'UUID', 'Name', 'Action', 'Redirect', 'Priority', 'Create Date'
32+
], title=f"Layer7 policies - protocol ID {identifier}")
33+
34+
table.align['Name'] = 'l'
35+
table.align['Action'] = 'l'
36+
table.align['Redirect'] = 'l'
37+
for l7policy in sorted(l7policies, key=lambda data: data.get('priority')):
38+
table.add_row([
39+
l7policy.get('id'),
40+
l7policy.get('uuid'),
41+
l7policy.get('name'),
42+
l7policy.get('action'),
43+
l7policy.get('redirectL7PoolId') or l7policy.get('redirectUrl') or formatting.blank(),
44+
l7policy.get('priority'),
45+
l7policy.get('createDate'),
46+
])
47+
return table
48+
49+
50+
def l7policies_table(listeners):
51+
"""Takes a dict of (protocols: policies list) and makes a list of tables"""
52+
tables = []
53+
for listener_id, list_policy in listeners.items():
54+
tables.append(generate_l7policies_table(list_policy, listener_id))
55+
return tables

SoftLayer/CLI/routes.py

+1
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@
204204
('loadbal:health', 'SoftLayer.CLI.loadbal.health:cli'),
205205
('loadbal:member-add', 'SoftLayer.CLI.loadbal.members:add'),
206206
('loadbal:member-del', 'SoftLayer.CLI.loadbal.members:remove'),
207+
('loadbal:l7policies', 'SoftLayer.CLI.loadbal.layer7_policy_list:policies'),
207208
('loadbal:pool-add', 'SoftLayer.CLI.loadbal.pools:add'),
208209
('loadbal:pool-edit', 'SoftLayer.CLI.loadbal.pools:edit'),
209210
('loadbal:pool-del', 'SoftLayer.CLI.loadbal.pools:delete'),

SoftLayer/fixtures/SoftLayer_Network_LBaaS_Listener.py

+22
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,25 @@
4141
'name': 'ams01',
4242
'statusId': 2
4343
}}
44+
45+
getL7Policies = [
46+
{'action': 'REJECT',
47+
'createDate': '2021-09-08T15:08:35-06:00',
48+
'id': 123456,
49+
'modifyDate': None,
50+
'name': 'test-reject',
51+
'priority': 2,
52+
'redirectL7PoolId': None,
53+
'uuid': '123mock-1234-43c9-b659-12345678mock'
54+
},
55+
{'action': 'REDIRECT_HTTPS',
56+
'createDate': '2021-09-08T15:03:53-06:00',
57+
'id': 432922,
58+
'modifyDate': None,
59+
'name': 'test-policy-https-1',
60+
'priority': 0,
61+
'redirectL7PoolId': None,
62+
'redirectUrl': 'url-test-uuid-mock-1234565',
63+
'uuid': 'test-uuid-mock-1234565'
64+
}
65+
]

SoftLayer/managers/load_balancer.py

+27
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,33 @@ def add_lb_listener(self, identifier, listener):
160160
return self.client.call('SoftLayer_Network_LBaaS_Listener', 'updateLoadBalancerProtocols',
161161
identifier, [listener])
162162

163+
def get_l7policies(self, identifier):
164+
"""Gets Layer7 policies from a listener
165+
166+
:param identifier: id
167+
"""
168+
169+
return self.client.call('SoftLayer_Network_LBaaS_Listener', 'getL7Policies', id=identifier)
170+
171+
def get_all_l7policies(self):
172+
"""Gets all Layer7 policies
173+
174+
:returns: Dictionary of (protocol_id: policies list).
175+
"""
176+
177+
mask = 'mask[listeners[l7Policies]]'
178+
lbaas = self.get_lbaas(mask=mask)
179+
listeners = []
180+
for load_bal in lbaas:
181+
listeners.extend(load_bal.get('listeners'))
182+
policies = {}
183+
for protocol in listeners:
184+
if protocol.get('l7Policies'):
185+
listener_id = protocol.get('id')
186+
l7policies = protocol.get('l7Policies')
187+
policies[listener_id] = l7policies
188+
return policies
189+
163190
def add_lb_l7_pool(self, identifier, pool, members, health, session):
164191
"""Creates a new l7 pool for a LBaaS instance
165192

docs/cli/loadbal.rst

+3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ LBaaS Commands
4040
.. click:: SoftLayer.CLI.loadbal.pools:l7pool_del
4141
:prog: loadbal l7pool-del
4242
:show-nested:
43+
.. click:: SoftLayer.CLI.loadbal.layer7_policy_list:policies
44+
:prog: loadbal l7policies
45+
:show-nested:
4346
.. click:: SoftLayer.CLI.loadbal.order:order
4447
:prog: loadbal order
4548
:show-nested:

tests/CLI/modules/loadbal_tests.py

+10
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,16 @@ def test_lb_member_del(self, click):
181181
self.assert_no_fail(result)
182182
click.secho.assert_called_with(output, fg='green')
183183

184+
def test_lb_l7policies_list(self):
185+
command = 'loadbal l7policies'
186+
result = self.run_command(command.split(' '))
187+
self.assert_no_fail(result)
188+
189+
def test_lb_l7policies_protocol_list(self):
190+
command = 'loadbal l7policies -p 123456'
191+
result = self.run_command(command.split(' '))
192+
self.assert_no_fail(result)
193+
184194
@mock.patch('SoftLayer.CLI.loadbal.health.click')
185195
def test_lb_health_manage(self, click):
186196
lb_id = '1111111'

tests/managers/loadbal_tests.py

+10
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,16 @@ def test_add_lb_listener(self):
110110
self.assert_called_with('SoftLayer_Network_LBaaS_Listener', 'updateLoadBalancerProtocols',
111111
args=(uuid, [listener]))
112112

113+
def test_get_l7policies(self):
114+
my_id = 1111111
115+
self.lb_mgr.get_l7policies(my_id)
116+
self.assert_called_with('SoftLayer_Network_LBaaS_Listener', 'getL7Policies', identifier=my_id)
117+
118+
def test_get_all_l7policies(self):
119+
policies = self.lb_mgr.get_all_l7policies()
120+
self.assert_called_with('SoftLayer_Network_LBaaS_LoadBalancer', 'getAllObjects')
121+
self.assertIsInstance(policies, dict)
122+
113123
def test_add_lb_l7_pool(self):
114124
uuid = 'aa-bb-cc'
115125
pool = {'id': 1}

0 commit comments

Comments
 (0)