Skip to content

Commit 509ddd4

Browse files
author
Ryan Rossiter
committed
Add tests for securitygroup
1 parent 35f5799 commit 509ddd4

8 files changed

Lines changed: 465 additions & 25 deletions

File tree

SoftLayer/CLI/securitygroup/delete.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import click
55
import SoftLayer
66
from SoftLayer.CLI import environment
7+
from SoftLayer.CLI import exceptions
78

89

910
@click.command()
@@ -12,4 +13,5 @@
1213
def cli(env, securitygroup_id):
1314
"""Deletes the given security group"""
1415
mgr = SoftLayer.NetworkManager(env.client)
15-
mgr.delete_securitygroup(securitygroup_id)
16+
if not mgr.delete_securitygroup(securitygroup_id):
17+
raise exceptions.CLIAbort("Failed to delete security group")

SoftLayer/CLI/securitygroup/detail.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def cli(env, identifier):
4545

4646
vsi_table = formatting.Table(['id', 'hostname', 'interface', 'ipAddress'])
4747

48-
for binding in secgroup.get('networkComponentBindings'):
48+
for binding in secgroup.get('networkComponentBindings', []):
4949
try:
5050
vsi = binding['networkComponent']['guest']
5151
vsi_id = vsi['id']

SoftLayer/CLI/securitygroup/interface.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def interface_list(env, securitygroup_id, sortby):
3030

3131
mask = (
3232
'''networkComponentBindings[
33+
networkComponentId,
3334
networkComponent[
3435
id,
3536
port,
@@ -44,7 +45,7 @@ def interface_list(env, securitygroup_id, sortby):
4445
)
4546

4647
secgroup = mgr.get_securitygroup(securitygroup_id, mask=mask)
47-
for binding in secgroup.get('networkComponentBindings'):
48+
for binding in secgroup.get('networkComponentBindings', []):
4849
interface_id = binding['networkComponentId']
4950
try:
5051
interface = binding['networkComponent']
@@ -89,7 +90,10 @@ def add(env, securitygroup_id, network_component, server, interface):
8990
mgr = SoftLayer.NetworkManager(env.client)
9091
component_id = _get_component_id(env, network_component, server, interface)
9192

92-
mgr.attach_securitygroup_component(securitygroup_id, component_id)
93+
success = mgr.attach_securitygroup_component(securitygroup_id,
94+
component_id)
95+
if not success:
96+
raise exceptions.CLIAbort("Could not attach network component")
9397

9498

9599
@click.command()
@@ -109,7 +113,10 @@ def remove(env, securitygroup_id, network_component, server, interface):
109113
mgr = SoftLayer.NetworkManager(env.client)
110114
component_id = _get_component_id(env, network_component, server, interface)
111115

112-
mgr.detach_securitygroup_component(securitygroup_id, component_id)
116+
success = mgr.detach_securitygroup_component(securitygroup_id,
117+
component_id)
118+
if not success:
119+
raise exceptions.CLIAbort("Could not detach network component")
113120

114121

115122
def _validate_args(network_component, server, interface):

SoftLayer/CLI/securitygroup/rule.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
'remoteIp',
1313
'remoteGroupId',
1414
'direction',
15-
'etherype',
15+
'ethertype',
1616
'portRangeMin',
1717
'portRangeMax',
1818
'protocol']
@@ -71,9 +71,12 @@ def add(env, securitygroup_id, remote_ip, remote_group,
7171
"""Add a security group rule to a security group."""
7272
mgr = SoftLayer.NetworkManager(env.client)
7373

74-
mgr.add_securitygroup_rule(securitygroup_id, remote_ip, remote_group,
75-
direction, ethertype, port_range_max,
76-
port_range_min, protocol)
74+
ret = mgr.add_securitygroup_rule(securitygroup_id, remote_ip, remote_group,
75+
direction, ethertype, port_range_max,
76+
port_range_min, protocol)
77+
78+
if not ret:
79+
raise exceptions.CLIAbort("Failed to add security group rule")
7780

7881

7982
@click.command()
@@ -126,4 +129,5 @@ def edit(env, securitygroup_id, rule_id, remote_ip, remote_group,
126129
def remove(env, securitygroup_id, rule_id):
127130
"""Remove a rule from a security group."""
128131
mgr = SoftLayer.NetworkManager(env.client)
129-
mgr.remove_securitygroup_rule(securitygroup_id, rule_id)
132+
if not mgr.remove_securitygroup_rule(securitygroup_id, rule_id):
133+
raise exceptions.CLIAbort("Failed to remove security group rule")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
getAllObjects = [
2+
{'id': 100,
3+
'name': 'secgroup1',
4+
'description': 'Securitygroup1'},
5+
{'id': 104,
6+
'name': 'secgroup2'},
7+
{'id': 110}
8+
]
9+
10+
getRules = [
11+
{'id': 100,
12+
'direction': 'egress',
13+
'ethertype': 'IPv4'}
14+
]
15+
16+
guest_dict = {'id': 5000,
17+
'hostname': 'test',
18+
'primaryBackendIpAddress': '10.3.4.5',
19+
'primaryIpAddress': '169.23.123.43'}
20+
21+
getObject = {
22+
'id': 100,
23+
'name': 'secgroup1',
24+
'description': 'Securitygroup1',
25+
'networkComponentBindings': [{'networkComponentId': 1000,
26+
'networkComponent': {'id': 1000,
27+
'port': 0,
28+
'guest': guest_dict}},
29+
{'networkComponentId': 1001,
30+
'networkComponent': {'id': 1001,
31+
'port': 1,
32+
'guest': guest_dict}}],
33+
'rules': getRules
34+
}
35+
36+
createObjects = [{'id': 100,
37+
'name': 'secgroup1',
38+
'description': 'Securitygroup1',
39+
'createDate': '2017-05-05T12:44:43-06:00'}]
40+
editObjects = True
41+
deleteObjects = True
42+
addRules = True
43+
editRules = True
44+
removeRules = True
45+
attachNetworkComponents = True
46+
detachNetworkComponents = True

SoftLayer/managers/network.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,15 @@ def add_securitygroup_rule(self, group_id, remote_ip=None,
7979
:param int port_min: The lower port bound to enforce
8080
:param str protocol: The protocol to enforce (icmp, udp, tcp)
8181
"""
82-
rule = {'direction': direction,
83-
'ethertype': ethertype,
84-
'portRangeMax': port_max,
85-
'portRangeMin': port_min,
86-
'protocol': protocol}
82+
rule = {'direction': direction}
83+
if ethertype is not None:
84+
rule['ethertype'] = ethertype
85+
if port_max is not None:
86+
rule['portRangeMax'] = port_max
87+
if port_min is not None:
88+
rule['portRangeMin'] = port_min
89+
if protocol is not None:
90+
rule['protocol'] = protocol
8791
if remote_ip is not None:
8892
rule['remoteIp'] = remote_ip
8993
if remote_group is not None:
@@ -228,23 +232,24 @@ def delete_securitygroup(self, group_id):
228232
:param int group_id: The ID of the security group
229233
"""
230234
delete_dict = {'id': group_id}
231-
self.security_group.deleteObjects([delete_dict])
235+
return self.security_group.deleteObjects([delete_dict])
232236

233237
def detach_securitygroup_component(self, group_id, component_id):
234238
"""Detaches a network component from a security group.
235239
236240
:param int group_id: The ID of the security group
237241
:param int component_id: The ID of the component to detach
238242
"""
239-
self.detach_securitygroup_components(group_id, [component_id])
243+
return self.detach_securitygroup_components(group_id, [component_id])
240244

241245
def detach_securitygroup_components(self, group_id, component_ids):
242246
"""Detaches network components from a security group.
243247
244248
:param int group_id: The ID of the security group
245249
:param list component_ids: The IDs of the network components to detach
246250
"""
247-
self.security_group.detachNetworkComponents(component_ids, id=group_id)
251+
return self.security_group.detachNetworkComponents(component_ids,
252+
id=group_id)
248253

249254
def edit_rwhois(self, abuse_email=None, address1=None, address2=None,
250255
city=None, company_name=None, country=None,
@@ -281,6 +286,7 @@ def edit_securitygroup(self, group_id, name=None, description=None):
281286
:param string name: The name of the security group
282287
:param string description: The description of the security group
283288
"""
289+
successful = False
284290
obj = {}
285291
if name:
286292
obj['name'] = name
@@ -289,9 +295,9 @@ def edit_securitygroup(self, group_id, name=None, description=None):
289295

290296
if obj:
291297
obj['id'] = group_id
292-
self.security_group.editObjects([obj])
298+
successful = self.security_group.editObjects([obj])
293299

294-
return bool(name or description)
300+
return successful
295301

296302
def edit_securitygroup_rule(self, group_id, rule_id, remote_ip=None,
297303
remote_group=None, direction=None,
@@ -310,6 +316,7 @@ def edit_securitygroup_rule(self, group_id, rule_id, remote_ip=None,
310316
:param str port_range_min: The lower port bound to enforce
311317
:param str protocol: The protocol to enforce (icmp, udp, tcp)
312318
"""
319+
successful = False
313320
obj = {}
314321
if remote_ip:
315322
obj['remoteIp'] = remote_ip
@@ -328,10 +335,9 @@ def edit_securitygroup_rule(self, group_id, rule_id, remote_ip=None,
328335

329336
if obj:
330337
obj['id'] = rule_id
331-
self.security_group.editRules([obj], id=group_id)
338+
successful = self.security_group.editRules([obj], id=group_id)
332339

333-
return bool(remote_ip or remote_group or direction or ethertype
334-
or port_range_max or port_range_min or protocol)
340+
return successful
335341

336342
def ip_lookup(self, ip_address):
337343
"""Looks up an IP address and returns network information about it.
@@ -524,15 +530,15 @@ def remove_securitygroup_rule(self, group_id, rule_id):
524530
:param int group_id: The ID of the security group
525531
:param int rule_id: The ID of the rule to remove
526532
"""
527-
self.remove_securitygroup_rules(group_id, [rule_id])
533+
return self.remove_securitygroup_rules(group_id, [rule_id])
528534

529535
def remove_securitygroup_rules(self, group_id, rules):
530536
"""Remove rules from a security group.
531537
532538
:param int group_id: The ID of the security group
533539
:param list rules: The list of IDs to remove
534540
"""
535-
self.security_group.removeRules(rules, id=group_id)
541+
return self.security_group.removeRules(rules, id=group_id)
536542

537543
def resolve_global_ip_ids(self, identifier):
538544
"""Resolve global ip ids."""

0 commit comments

Comments
 (0)