Skip to content

Commit c36e75d

Browse files
Zuulopenstack-gerrit
authored andcommitted
Merge "Add 'security_group' type support to network rbac commands"
2 parents 6868499 + be7a758 commit c36e75d

File tree

5 files changed

+89
-6
lines changed

5 files changed

+89
-6
lines changed

doc/source/cli/command-objects/network-rbac.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Create network RBAC policy
2626
2727
.. option:: --type <type>
2828
29-
Type of the object that RBAC policy affects ("qos_policy" or "network") (required)
29+
Type of the object that RBAC policy affects ("security_group", "qos_policy" or "network") (required)
3030
3131
.. option:: --action <action>
3232
@@ -90,7 +90,7 @@ List network RBAC policies
9090
9191
.. option:: --type <type>
9292
93-
List network RBAC policies according to given object type ("qos_policy" or "network")
93+
List network RBAC policies according to given object type ("security_group", "qos_policy" or "network")
9494
9595
.. option:: --action <action>
9696

openstackclient/network/v2/network_rbac.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def _get_attrs(client_manager, parsed_args):
4848
object_id = network_client.find_qos_policy(
4949
parsed_args.rbac_object,
5050
ignore_missing=False).id
51+
if parsed_args.type == 'security_group':
52+
object_id = network_client.find_security_group(
53+
parsed_args.rbac_object,
54+
ignore_missing=False).id
5155
attrs['object_id'] = object_id
5256

5357
identity_client = client_manager.identity
@@ -87,9 +91,9 @@ def get_parser(self, prog_name):
8791
'--type',
8892
metavar="<type>",
8993
required=True,
90-
choices=['qos_policy', 'network'],
94+
choices=['security_group', 'qos_policy', 'network'],
9195
help=_('Type of the object that RBAC policy '
92-
'affects ("qos_policy" or "network")')
96+
'affects ("security_group", "qos_policy" or "network")')
9397
)
9498
parser.add_argument(
9599
'--action',
@@ -178,9 +182,10 @@ def get_parser(self, prog_name):
178182
parser.add_argument(
179183
'--type',
180184
metavar='<type>',
181-
choices=['qos_policy', 'network'],
185+
choices=['security_group', 'qos_policy', 'network'],
182186
help=_('List network RBAC policies according to '
183-
'given object type ("qos_policy" or "network")')
187+
'given object type ("security_group", "qos_policy" '
188+
'or "network")')
184189
)
185190
parser.add_argument(
186191
'--action',

openstackclient/tests/unit/network/v2/fakes.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,39 @@ def get_qos_policies(qos_policies=None, count=2):
968968
return mock.Mock(side_effect=qos_policies)
969969

970970

971+
class FakeNetworkSecGroup(object):
972+
"""Fake one security group."""
973+
974+
@staticmethod
975+
def create_one_security_group(attrs=None):
976+
"""Create a fake security group.
977+
978+
:param Dictionary attrs:
979+
A dictionary with all attributes
980+
:return:
981+
A FakeResource object with name, id, etc.
982+
"""
983+
attrs = attrs or {}
984+
sg_id = attrs.get('id') or 'security-group-id-' + uuid.uuid4().hex
985+
986+
# Set default attributes.
987+
security_group_attrs = {
988+
'name': 'security-group-name-' + uuid.uuid4().hex,
989+
'id': sg_id,
990+
'tenant_id': 'project-id-' + uuid.uuid4().hex,
991+
'description': 'security-group-description-' + uuid.uuid4().hex
992+
}
993+
994+
security_group = fakes.FakeResource(
995+
info=copy.deepcopy(security_group_attrs),
996+
loaded=True)
997+
998+
# Set attributes with special mapping in OpenStack SDK.
999+
security_group.project_id = security_group_attrs['tenant_id']
1000+
1001+
return security_group
1002+
1003+
9711004
class FakeNetworkQosRule(object):
9721005
"""Fake one or more Network QoS rules."""
9731006

openstackclient/tests/unit/network/v2/test_network_rbac.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class TestCreateNetworkRBAC(TestNetworkRBAC):
3737

3838
network_object = network_fakes.FakeNetwork.create_one_network()
3939
qos_object = network_fakes.FakeNetworkQosPolicy.create_one_qos_policy()
40+
sg_object = network_fakes.FakeNetworkSecGroup.create_one_security_group()
4041
project = identity_fakes_v3.FakeProject.create_one_project()
4142
rbac_policy = network_fakes.FakeNetworkRBAC.create_one_network_rbac(
4243
attrs={'tenant_id': project.id,
@@ -74,6 +75,8 @@ def setUp(self):
7475
return_value=self.network_object)
7576
self.network.find_qos_policy = mock.Mock(
7677
return_value=self.qos_object)
78+
self.network.find_security_group = mock.Mock(
79+
return_value=self.sg_object)
7780
self.projects_mock.get.return_value = self.project
7881

7982
def test_network_rbac_create_no_type(self):
@@ -258,6 +261,43 @@ def test_network_rbac_create_qos_object(self):
258261
self.assertEqual(self.columns, columns)
259262
self.assertEqual(self.data, list(data))
260263

264+
def test_network_rbac_create_security_group_object(self):
265+
self.rbac_policy.object_type = 'security_group'
266+
self.rbac_policy.object_id = self.sg_object.id
267+
arglist = [
268+
'--type', 'security_group',
269+
'--action', self.rbac_policy.action,
270+
'--target-project', self.rbac_policy.target_tenant,
271+
self.sg_object.name,
272+
]
273+
verifylist = [
274+
('type', 'security_group'),
275+
('action', self.rbac_policy.action),
276+
('target_project', self.rbac_policy.target_tenant),
277+
('rbac_object', self.sg_object.name),
278+
]
279+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
280+
281+
# DisplayCommandBase.take_action() returns two tuples
282+
columns, data = self.cmd.take_action(parsed_args)
283+
284+
self.network.create_rbac_policy.assert_called_with(**{
285+
'object_id': self.sg_object.id,
286+
'object_type': 'security_group',
287+
'action': self.rbac_policy.action,
288+
'target_tenant': self.rbac_policy.target_tenant,
289+
})
290+
self.data = [
291+
self.rbac_policy.action,
292+
self.rbac_policy.id,
293+
self.sg_object.id,
294+
'security_group',
295+
self.rbac_policy.tenant_id,
296+
self.rbac_policy.target_tenant,
297+
]
298+
self.assertEqual(self.columns, columns)
299+
self.assertEqual(self.data, list(data))
300+
261301

262302
class TestDeleteNetworkRBAC(TestNetworkRBAC):
263303

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
features:
2+
- |
3+
Add ``security_group`` as a valid ``--type`` value for the
4+
``network rbac create`` and ``network rbac list`` commands.
5+

0 commit comments

Comments
 (0)