Skip to content

Commit 9a07660

Browse files
author
Brian Flores
committed
Update detail command and add unit tests
1 parent 9c55550 commit 9a07660

4 files changed

Lines changed: 251 additions & 17 deletions

File tree

SoftLayer/CLI/firewall/detail.py

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,67 @@
1212

1313
@click.command(cls=SoftLayer.CLI.command.SLCommand, )
1414
@click.argument('identifier')
15+
@click.option('--credentials', type=click.BOOL,
16+
help="Display FortiGate username and FortiGate password to multi vlans.")
1517
@environment.pass_env
16-
def cli(env, identifier):
18+
def cli(env, identifier, credentials):
1719
"""Detail firewall."""
1820

1921
mgr = SoftLayer.FirewallManager(env.client)
2022

2123
firewall_type, firewall_id = firewall.parse_id(identifier)
22-
_firewall = mgr.get_instance(firewall_id)
2324

24-
table = formatting.KeyValueTable(['name', 'value'])
25-
table.align['name'] = 'r'
26-
table.align['value'] = 'l'
25+
if firewall_type == 'vs' or firewall_type == 'server' or firewall_type == 'vlan' or firewall_type == 'multiVlan':
2726

28-
table.add_row(['id', _firewall.get('id')])
29-
table.add_row(['primaryIpAddress', _firewall.get('primaryIpAddress')])
30-
table.add_row(['datacenter', utils.lookup(_firewall, 'datacenter', 'longName')])
31-
table.add_row(['networkVlan', utils.lookup(_firewall, 'networkVlan', 'name')])
32-
table.add_row(['networkVlaniD', utils.lookup(_firewall, 'networkVlan', 'id')])
27+
if firewall_type == 'vlan':
28+
_firewall = mgr.get_instance(firewall_id)
29+
30+
table = formatting.KeyValueTable(['name', 'value'])
31+
table.align['name'] = 'r'
32+
table.align['value'] = 'l'
33+
34+
table.add_row(['id', _firewall.get('id')])
35+
table.add_row(['primaryIpAddress', _firewall.get('primaryIpAddress')])
36+
table.add_row(['datacenter', utils.lookup(_firewall, 'datacenter', 'longName')])
37+
table.add_row(['networkVlan', utils.lookup(_firewall, 'networkVlan', 'name')])
38+
table.add_row(['networkVlaniD', utils.lookup(_firewall, 'networkVlan', 'id')])
39+
40+
rules = mgr.get_dedicated_fwl_rules(firewall_id)
41+
table.add_row(['rules', get_rules_table(rules)])
42+
43+
if firewall_type == 'multiVlan':
44+
_firewall = mgr.get_instance(firewall_id)
45+
46+
table = formatting.KeyValueTable(['name', 'value'])
47+
table.align['name'] = 'r'
48+
table.align['value'] = 'l'
49+
50+
table.add_row(['name', utils.lookup(_firewall, 'networkGateway', 'name')])
51+
table.add_row(['datacenter', utils.lookup(_firewall, 'datacenter', 'longName')])
52+
table.add_row(['public ip', utils.lookup(_firewall, 'networkGateway', 'publicIpAddress', 'ipAddress')])
53+
table.add_row(['private ip', utils.lookup(_firewall, 'networkGateway', 'privateIpAddress', 'ipAddress')])
54+
table.add_row(['public ipv6', utils.lookup(_firewall, 'networkGateway', 'publicIpv6Address', 'ipAddress')])
55+
table.add_row(['public vlan', utils.lookup(_firewall, 'networkGateway', 'publicVlan', 'vlanNumber')])
56+
table.add_row(['private vlan', utils.lookup(_firewall, 'networkGateway', 'privateVlan', 'vlanNumber')])
57+
table.add_row(['type', _firewall.get('firewallType')])
58+
59+
if credentials:
60+
table.add_row(['FortiGate username', utils.lookup(_firewall, 'managementCredentials', 'username')])
61+
table.add_row(['FortiGate password', utils.lookup(_firewall, 'managementCredentials', 'password')])
62+
63+
rules = mgr.get_dedicated_fwl_rules(firewall_id)
64+
table.add_row(['rules', get_rules_table(rules)])
65+
66+
if firewall_type == 'vs' or firewall_type == 'server':
67+
rules = mgr.get_standard_fwl_rules(firewall_id)
68+
table = get_rules_table(rules)
69+
70+
env.fout(table)
3371

34-
if firewall_type == 'vlan':
35-
rules = mgr.get_dedicated_fwl_rules(firewall_id)
3672
else:
37-
rules = mgr.get_standard_fwl_rules(firewall_id)
38-
table.add_row(['rules', get_rules_table(rules)])
39-
env.fout(table)
73+
click.secho('Invalid firewall type %s: firewall type should be either vlan, multiVlan, vs or server.'
74+
% firewall_type, fg='red')
75+
return
4076

4177

4278
def get_rules_table(rules):

SoftLayer/fixtures/SoftLayer_Network_Vlan_Firewall.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,133 @@
4949
}
5050
]
5151
},
52+
"firewallType": "fortigate-security-appliance-10gb",
53+
"managementCredentials": {
54+
"createDate": "2022-05-17T13:59:17-06:00",
55+
"id": 74604882,
56+
"modifyDate": "2022-05-17T13:59:17-06:00",
57+
"password": "test1234",
58+
"port": 23,
59+
"softwareId": 67804284,
60+
"username": "myusername"
61+
},
62+
"networkGateway": {
63+
"accountId": 307608,
64+
"groupNumber": 1,
65+
"id": 615448,
66+
"name": "testFirewall",
67+
"networkSpace": "BOTH",
68+
"privateIpAddressId": 188996652,
69+
"privateVlanId": 3228724,
70+
"publicIpAddressId": 188996794,
71+
"publicIpv6AddressId": 188996808,
72+
"publicVlanId": 3228726,
73+
"statusId": 1,
74+
"insideVlans": [],
75+
"members": [
76+
{
77+
"hardwareId": 3222842,
78+
"id": 687820,
79+
"networkGatewayId": 615448,
80+
"priority": 254,
81+
"networkGateway": None
82+
}
83+
],
84+
"privateIpAddress": {
85+
"id": 188996652,
86+
"ipAddress": "10.37.115.70",
87+
"isBroadcast": False,
88+
"isGateway": False,
89+
"isNetwork": False,
90+
"isReserved": True,
91+
"subnetId": 2552734,
92+
"subnet": {
93+
"broadcastAddress": "10.37.115.127",
94+
"cidr": 26,
95+
"gateway": "10.37.115.65",
96+
"id": 2552734,
97+
"isCustomerOwned": False,
98+
"isCustomerRoutable": False,
99+
"modifyDate": "2022-05-17T13:59:16-06:00",
100+
"netmask": "255.255.255.192",
101+
"networkIdentifier": "10.37.115.64",
102+
"networkVlanId": 3228724,
103+
"sortOrder": "1",
104+
"subnetType": "ADDITIONAL_PRIMARY",
105+
"totalIpAddresses": "64",
106+
"usableIpAddressCount": "61",
107+
"version": 4
108+
}
109+
},
110+
"privateVlan": {
111+
"accountId": 307608,
112+
"fullyQualifiedName": "dal13.bcr03.1330",
113+
"id": 3228724,
114+
"modifyDate": "2022-05-17T14:01:14-06:00",
115+
"primarySubnetId": 2625456,
116+
"vlanNumber": 1330
117+
},
118+
"publicIpAddress": {
119+
"id": 188996794,
120+
"ipAddress": "67.228.206.245",
121+
"isBroadcast": False,
122+
"isGateway": False,
123+
"isNetwork": False,
124+
"isReserved": True,
125+
"subnetId": 66444,
126+
"subnet": {
127+
"broadcastAddress": "67.228.206.247",
128+
"cidr": 29,
129+
"gateway": "67.228.206.241",
130+
"id": 66444,
131+
"isCustomerOwned": False,
132+
"isCustomerRoutable": False,
133+
"modifyDate": "2022-05-17T13:59:16-06:00",
134+
"netmask": "255.255.255.248",
135+
"networkIdentifier": "67.228.206.240",
136+
"networkVlanId": 3228726,
137+
"sortOrder": "1",
138+
"subnetType": "ADDITIONAL_PRIMARY",
139+
"totalIpAddresses": "8",
140+
"usableIpAddressCount": "5",
141+
"version": 4
142+
}
143+
},
144+
"publicIpv6Address": {
145+
"id": 188996808,
146+
"ipAddress": "2607:f0d0:2703:0039:0000:0000:0000:0004",
147+
"isBroadcast": False,
148+
"isGateway": False,
149+
"isNetwork": False,
150+
"isReserved": True,
151+
"subnetId": 2547678,
152+
"subnet": {
153+
"broadcastAddress": "",
154+
"cidr": 64,
155+
"gateway": "2607:f0d0:2703:0039:0000:0000:0000:0001",
156+
"id": 2547678,
157+
"isCustomerOwned": False,
158+
"isCustomerRoutable": False,
159+
"modifyDate": "2022-05-17T13:59:16-06:00",
160+
"netmask": "ffff:ffff:ffff:ffff:0000:0000:0000:0000",
161+
"networkIdentifier": "2607:f0d0:2703:0039:0000:0000:0000:0000",
162+
"networkVlanId": 3228726,
163+
"sortOrder": "4",
164+
"subnetType": "PRIMARY_6",
165+
"totalIpAddresses": "18446744073709551616",
166+
"usableIpAddressCount": "18446744073709551614",
167+
"version": 6
168+
}
169+
},
170+
"publicVlan": {
171+
"accountId": 307608,
172+
"fullyQualifiedName": "dal13.fcr03.1255",
173+
"id": 3228726,
174+
"modifyDate": "2022-05-17T14:00:42-06:00",
175+
"primarySubnetId": 2623338,
176+
"vlanNumber": 1255
177+
}
178+
},
52179
"rules": [
53180
{'destinationIpAddress': 'any on server',
54181
'protocol': 'tcp',

SoftLayer/managers/firewall.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,8 @@ def get_instance(self, firewall_id, mask=None):
297297
:param integer firewall_id: the instance ID of the standard firewall
298298
"""
299299
if not mask:
300-
mask = 'mask[datacenter,networkVlan]'
300+
mask = 'mask[firewallType,networkGateway[insideVlans,members,privateIpAddress,publicIpAddress,' \
301+
'publicIpv6Address,privateVlan,publicVlan],datacenter,managementCredentials,networkVlan]'
301302

302303
svc = self.client['Network_Vlan_Firewall']
303304

tests/CLI/modules/firewall_tests.py

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def test_add_server(self, confirm_mock):
5555
self.assert_no_fail(result)
5656
self.assertIn("Firewall is being created!", result.output)
5757

58-
def test_detail(self):
58+
def test_detail_vlan_firewall(self):
5959
result = self.run_command(['firewall', 'detail', 'vlan:1234'])
6060
self.assert_no_fail(result)
6161
json_result = json.loads(result.output)
@@ -88,6 +88,76 @@ def test_detail(self):
8888
'src_ip': '0.0.0.0',
8989
'src_mask': '0.0.0.0'}]})
9090

91+
def test_detail_multi_vlan_firewall(self):
92+
result = self.run_command(['firewall', 'detail', 'multiVlan:1234', '--credentials', 'true'])
93+
self.assert_no_fail(result)
94+
json_result = json.loads(result.output)
95+
self.assertEqual(json_result['rules'][0]['action'], 'permit')
96+
self.assertEqual(json.loads(result.output),
97+
{'name': 'testFirewall',
98+
'datacenter': 'Amsterdam 1',
99+
'public ip': '67.228.206.245',
100+
'private ip': '10.37.115.70',
101+
'public ipv6': '2607:f0d0:2703:0039:0000:0000:0000:0004',
102+
'public vlan': 1255,
103+
'private vlan': 1330,
104+
'type': 'fortigate-security-appliance-10gb',
105+
'FortiGate username': 'myusername',
106+
'FortiGate password': 'test1234',
107+
'rules': [{'#': 1,
108+
'action': 'permit',
109+
'dest': 'any on server:80-80',
110+
'dest_mask': '255.255.255.255',
111+
'protocol': 'tcp',
112+
'src_ip': '0.0.0.0',
113+
'src_mask': '0.0.0.0'},
114+
{'#': 2,
115+
'action': 'permit',
116+
'dest': 'any on server:1-65535',
117+
'dest_mask': '255.255.255.255',
118+
'protocol': 'tmp',
119+
'src_ip': '193.212.1.10',
120+
'src_mask': '255.255.255.255'},
121+
{'#': 3,
122+
'action': 'permit',
123+
'dest': 'any on server:80-800',
124+
'dest_mask': '255.255.255.255',
125+
'protocol': 'tcp',
126+
'src_ip': '0.0.0.0',
127+
'src_mask': '0.0.0.0'}]})
128+
129+
def test_detail_vs_firewall(self):
130+
result = self.run_command(['firewall', 'detail', 'vs:1234'])
131+
self.assert_no_fail(result)
132+
self.assertEqual(json.loads(result.output),
133+
[{'#': 1,
134+
'action': 'permit',
135+
'dest': 'any on server:80-80',
136+
'dest_mask': '255.255.255.255',
137+
'protocol': 'tcp',
138+
'src_ip': '0.0.0.0',
139+
'src_mask': '0.0.0.0'},
140+
{'#': 2,
141+
'action': 'permit',
142+
'dest': 'any on server:1-65535',
143+
'dest_mask': '255.255.255.255',
144+
'protocol': 'tcp',
145+
'src_ip': '193.212.1.10',
146+
'src_mask': '255.255.255.255'},
147+
{'#': 3,
148+
'action': 'permit',
149+
'dest': 'any on server:80-800',
150+
'dest_mask': '255.255.255.255',
151+
'protocol': 'tcp',
152+
'src_ip': '0.0.0.0',
153+
'src_mask': '0.0.0.0'}])
154+
155+
def test_detail_fails(self):
156+
result = self.run_command(['firewall', 'detail', 'abc:1234'])
157+
self.assert_no_fail(result)
158+
expected_output = 'Invalid firewall type abc: firewall type should be either vlan, multiVlan, vs or server.\n'
159+
self.assertEqual(result.output, expected_output)
160+
91161
@mock.patch('SoftLayer.CLI.formatting.confirm')
92162
def test_cancel_firewall(self, confirm_mock):
93163
confirm_mock.return_value = True

0 commit comments

Comments
 (0)