Skip to content

Commit 42d9d7a

Browse files
committed
#1550 add loadbal l7policies
1 parent 2f7b75f commit 42d9d7a

File tree

6 files changed

+126
-0
lines changed

6 files changed

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

SoftLayer/CLI/routes.py

Lines changed: 1 addition & 0 deletions
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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,26 @@
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+
]
66+

SoftLayer/managers/load_balancer.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,32 @@ 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+
:returns: Dictionary of (protocol_id: policies list).
174+
"""
175+
176+
mask = 'mask[listeners[l7Policies]]'
177+
lbaas = self.get_lbaas(mask=mask)
178+
listeners = []
179+
for lb in lbaas:
180+
listeners.extend(lb.get('listeners'))
181+
policies = {}
182+
for protocol in listeners:
183+
if protocol.get('l7Policies'):
184+
listener_id = protocol.get('id')
185+
l7policies = protocol.get('l7Policies')
186+
policies[listener_id] = l7policies
187+
return policies
188+
163189
def add_lb_l7_pool(self, identifier, pool, members, health, session):
164190
"""Creates a new l7 pool for a LBaaS instance
165191

tests/CLI/modules/loadbal_tests.py

Lines changed: 10 additions & 0 deletions
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

Lines changed: 10 additions & 0 deletions
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)