Skip to content

Commit

Permalink
Allow security group rules to have their own group as a source
Browse files Browse the repository at this point in the history
Fixes bug 1012457.

Change-Id: I8f9dc6904ba4b19302551fae7455fdf41b50442e
  • Loading branch information
jpichon committed Jul 18, 2012
1 parent b282751 commit ac39a8e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
Expand Up @@ -97,8 +97,7 @@ def __init__(self, *args, **kwargs):
current_group_id = initials.get('security_group_id', 0)
security_groups = initials.get('security_group_list', [])
security_groups_choices = [("", "CIDR")] # default choice is CIDR
group_choices = [s for s in security_groups
if str(s[0]) != current_group_id]
group_choices = [s for s in security_groups]
if len(group_choices): # add group choice if available
security_groups_choices.append(('Security Group', group_choices))
self.fields['source_group'].choices = security_groups_choices
Expand Down
Expand Up @@ -179,6 +179,34 @@ def test_edit_rules_add_rule_cidr_and_source_group(self):
msg = 'Either CIDR or Source Group may be specified, but not both.'
self.assertFormErrors(res, count=1, message=msg)

def test_edit_rules_add_rule_self_as_source_group(self):
sec_group = self.security_groups.first()
sec_group_list = self.security_groups.list()
rule = self.security_group_rules.get(id=3)

self.mox.StubOutWithMock(api, 'security_group_rule_create')
self.mox.StubOutWithMock(api, 'security_group_list')
api.security_group_rule_create(IsA(http.HttpRequest),
sec_group.id,
rule.ip_protocol,
int(rule.from_port),
int(rule.to_port),
None,
u'%s' % sec_group.id).AndReturn(rule)
api.security_group_list(
IsA(http.HttpRequest)).AndReturn(sec_group_list)
self.mox.ReplayAll()

formData = {'method': 'AddRule',
'security_group_id': sec_group.id,
'from_port': rule.from_port,
'to_port': rule.to_port,
'ip_protocol': rule.ip_protocol,
'cidr': '0.0.0.0/0',
'source_group': sec_group.id}
res = self.client.post(self.edit_url, formData)
self.assertRedirectsNoFollow(res, INDEX_URL)

def test_edit_rules_invalid_port_range(self):
sec_group = self.security_groups.first()
sec_group_list = self.security_groups.list()
Expand Down
12 changes: 12 additions & 0 deletions horizon/tests/test_data/nova_data.py
Expand Up @@ -229,12 +229,24 @@ def data(TEST):
'to_port': u"5",
'parent_group_id': 1,
'ip_range': {'cidr': u"0.0.0.0/32"}}

group_rule = {'id': 3,
'ip_protocol': u"tcp",
'from_port': u"80",
'to_port': u"80",
'parent_group_id': 1,
'source_group_id': 1}

rule_obj = rules.SecurityGroupRule(rules.SecurityGroupRuleManager(None),
rule)
rule_obj2 = rules.SecurityGroupRule(rules.SecurityGroupRuleManager(None),
icmp_rule)
rule_obj3 = rules.SecurityGroupRule(rules.SecurityGroupRuleManager(None),
group_rule)

TEST.security_group_rules.add(rule_obj)
TEST.security_group_rules.add(rule_obj2)
TEST.security_group_rules.add(rule_obj3)

sec_group_1.rules = [rule_obj]
sec_group_2.rules = [rule_obj]
Expand Down

0 comments on commit ac39a8e

Please sign in to comment.