Skip to content

Commit

Permalink
Merge pull request #1553 from ATGE/issues/1550-loadbal-l7policies
Browse files Browse the repository at this point in the history
Issues/1550 loadbal l7policies
  • Loading branch information
allmightyspiff committed Oct 15, 2021
2 parents b364ea6 + 26f2052 commit 98feac7
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 0 deletions.
55 changes: 55 additions & 0 deletions SoftLayer/CLI/loadbal/layer7_policy_list.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""List Layer7 policies"""
import click

import SoftLayer
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting


@click.command()
@click.option('--protocol-id', '-p',
required=False,
type=int,
help="Front-end Protocol identifier")
@environment.pass_env
def policies(env, protocol_id):
"""List policies of the front-end protocol (listener)."""
mgr = SoftLayer.LoadBalancerManager(env.client)

if protocol_id:
l7policies = mgr.get_l7policies(protocol_id)
table = generate_l7policies_table(l7policies, protocol_id)
else:
l7policies = mgr.get_all_l7policies()
table = l7policies_table(l7policies)
env.fout(table)


def generate_l7policies_table(l7policies, identifier):
"""Takes a list of Layer7 policies and makes a table"""
table = formatting.Table([
'Id', 'UUID', 'Name', 'Action', 'Redirect', 'Priority', 'Create Date'
], title=f"Layer7 policies - protocol ID {identifier}")

table.align['Name'] = 'l'
table.align['Action'] = 'l'
table.align['Redirect'] = 'l'
for l7policy in sorted(l7policies, key=lambda data: data.get('priority')):
table.add_row([
l7policy.get('id'),
l7policy.get('uuid'),
l7policy.get('name'),
l7policy.get('action'),
l7policy.get('redirectL7PoolId') or l7policy.get('redirectUrl') or formatting.blank(),
l7policy.get('priority'),
l7policy.get('createDate'),
])
return table


def l7policies_table(listeners):
"""Takes a dict of (protocols: policies list) and makes a list of tables"""
tables = []
for listener_id, list_policy in listeners.items():
tables.append(generate_l7policies_table(list_policy, listener_id))
return tables
1 change: 1 addition & 0 deletions SoftLayer/CLI/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@
('loadbal:health', 'SoftLayer.CLI.loadbal.health:cli'),
('loadbal:member-add', 'SoftLayer.CLI.loadbal.members:add'),
('loadbal:member-del', 'SoftLayer.CLI.loadbal.members:remove'),
('loadbal:l7policies', 'SoftLayer.CLI.loadbal.layer7_policy_list:policies'),
('loadbal:pool-add', 'SoftLayer.CLI.loadbal.pools:add'),
('loadbal:pool-edit', 'SoftLayer.CLI.loadbal.pools:edit'),
('loadbal:pool-del', 'SoftLayer.CLI.loadbal.pools:delete'),
Expand Down
22 changes: 22 additions & 0 deletions SoftLayer/fixtures/SoftLayer_Network_LBaaS_Listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,25 @@
'name': 'ams01',
'statusId': 2
}}

getL7Policies = [
{'action': 'REJECT',
'createDate': '2021-09-08T15:08:35-06:00',
'id': 123456,
'modifyDate': None,
'name': 'test-reject',
'priority': 2,
'redirectL7PoolId': None,
'uuid': '123mock-1234-43c9-b659-12345678mock'
},
{'action': 'REDIRECT_HTTPS',
'createDate': '2021-09-08T15:03:53-06:00',
'id': 432922,
'modifyDate': None,
'name': 'test-policy-https-1',
'priority': 0,
'redirectL7PoolId': None,
'redirectUrl': 'url-test-uuid-mock-1234565',
'uuid': 'test-uuid-mock-1234565'
}
]
27 changes: 27 additions & 0 deletions SoftLayer/managers/load_balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,33 @@ def add_lb_listener(self, identifier, listener):
return self.client.call('SoftLayer_Network_LBaaS_Listener', 'updateLoadBalancerProtocols',
identifier, [listener])

def get_l7policies(self, identifier):
"""Gets Layer7 policies from a listener
:param identifier: id
"""

return self.client.call('SoftLayer_Network_LBaaS_Listener', 'getL7Policies', id=identifier)

def get_all_l7policies(self):
"""Gets all Layer7 policies
:returns: Dictionary of (protocol_id: policies list).
"""

mask = 'mask[listeners[l7Policies]]'
lbaas = self.get_lbaas(mask=mask)
listeners = []
for load_bal in lbaas:
listeners.extend(load_bal.get('listeners'))
policies = {}
for protocol in listeners:
if protocol.get('l7Policies'):
listener_id = protocol.get('id')
l7policies = protocol.get('l7Policies')
policies[listener_id] = l7policies
return policies

def add_lb_l7_pool(self, identifier, pool, members, health, session):
"""Creates a new l7 pool for a LBaaS instance
Expand Down
3 changes: 3 additions & 0 deletions docs/cli/loadbal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ LBaaS Commands
.. click:: SoftLayer.CLI.loadbal.pools:l7pool_del
:prog: loadbal l7pool-del
:show-nested:
.. click:: SoftLayer.CLI.loadbal.layer7_policy_list:policies
:prog: loadbal l7policies
:show-nested:
.. click:: SoftLayer.CLI.loadbal.order:order
:prog: loadbal order
:show-nested:
Expand Down
10 changes: 10 additions & 0 deletions tests/CLI/modules/loadbal_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ def test_lb_member_del(self, click):
self.assert_no_fail(result)
click.secho.assert_called_with(output, fg='green')

def test_lb_l7policies_list(self):
command = 'loadbal l7policies'
result = self.run_command(command.split(' '))
self.assert_no_fail(result)

def test_lb_l7policies_protocol_list(self):
command = 'loadbal l7policies -p 123456'
result = self.run_command(command.split(' '))
self.assert_no_fail(result)

@mock.patch('SoftLayer.CLI.loadbal.health.click')
def test_lb_health_manage(self, click):
lb_id = '1111111'
Expand Down
10 changes: 10 additions & 0 deletions tests/managers/loadbal_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ def test_add_lb_listener(self):
self.assert_called_with('SoftLayer_Network_LBaaS_Listener', 'updateLoadBalancerProtocols',
args=(uuid, [listener]))

def test_get_l7policies(self):
my_id = 1111111
self.lb_mgr.get_l7policies(my_id)
self.assert_called_with('SoftLayer_Network_LBaaS_Listener', 'getL7Policies', identifier=my_id)

def test_get_all_l7policies(self):
policies = self.lb_mgr.get_all_l7policies()
self.assert_called_with('SoftLayer_Network_LBaaS_LoadBalancer', 'getAllObjects')
self.assertIsInstance(policies, dict)

def test_add_lb_l7_pool(self):
uuid = 'aa-bb-cc'
pool = {'id': 1}
Expand Down

0 comments on commit 98feac7

Please sign in to comment.