Add support for all-tenants search opt to secgroup-list

Related to bug 1046054.

Once I6157e408 lands, we will be respecting the all-tenants search option
in the security groups retrieval API. Hence the CLI should be capable of
adding this option when appropriate.

Change-Id: If1217a5ffc7316e4661326c38da9b2956424ab05
This commit is contained in:
Eoghan Glynn 2012-09-14 11:31:30 +00:00
parent 40cb8ef833
commit fc2d622bac
3 changed files with 42 additions and 6 deletions

View File

@ -17,6 +17,8 @@
Security group interface (1.1 extension).
"""
import urllib
from novaclient import base
@ -61,10 +63,17 @@ class SecurityGroupManager(base.ManagerWithFind):
return self._get('/os-security-groups/%s' % group_id,
'security_group')
def list(self):
def list(self, search_opts=None):
"""
Get a list of all security_groups
:rtype: list of :class:`SecurityGroup`
"""
return self._list("/os-security-groups", "security_groups")
search_opts = search_opts or {}
qparams = dict((k, v) for (k, v) in search_opts.iteritems() if v)
query_string = '?%s' % urllib.urlencode(qparams) if qparams else ''
return self._list('/os-security-groups%s' % query_string,
'security_groups')

View File

@ -1524,9 +1524,24 @@ def do_secgroup_delete(cs, args):
cs.security_groups.delete(_get_secgroup(cs, args.secgroup))
@utils.arg('--all-tenants',
dest='all_tenants',
metavar='<0|1>',
nargs='?',
type=int,
const=1,
default=0,
help='Display information from all tenants (Admin only).')
@utils.arg('--all_tenants',
nargs='?',
type=int,
const=1,
help=argparse.SUPPRESS)
def do_secgroup_list(cs, args):
"""List security groups for the current tenant."""
_print_secgroups(cs.security_groups.list())
all_tenants = int(os.environ.get("ALL_TENANTS", args.all_tenants))
search_opts = {'all_tenants': all_tenants}
_print_secgroups(cs.security_groups.list(search_opts=search_opts))
@utils.arg('secgroup', metavar='<secgroup>', help='Name of security group.')

View File

@ -7,12 +7,24 @@ cs = fakes.FakeClient()
class SecurityGroupsTest(utils.TestCase):
def test_list_security_groups(self):
sgs = cs.security_groups.list()
cs.assert_called('GET', '/os-security-groups')
def _do_test_list_security_groups(self, search_opts, path):
sgs = cs.security_groups.list(search_opts=search_opts)
cs.assert_called('GET', path)
for sg in sgs:
self.assertTrue(isinstance(sg, security_groups.SecurityGroup))
def test_list_security_groups_all_tenants_on(self):
self._do_test_list_security_groups(
None, '/os-security-groups')
def test_list_security_groups_all_tenants_on(self):
self._do_test_list_security_groups(
{'all_tenants': 1}, '/os-security-groups?all_tenants=1')
def test_list_security_groups_all_tenants_off(self):
self._do_test_list_security_groups(
{'all_tenants': 0}, '/os-security-groups')
def test_get_security_groups(self):
sg = cs.security_groups.get(1)
cs.assert_called('GET', '/os-security-groups/1')