adding tabular output to swauth-list

swauth-list now shows lists of accounts, users and groups
in a tabular format (default). JSON and plain-text options
are still available

Change-Id: I897009f5f6c05aa4c14e306bb6ad35234e993fcd
Signed-off-by: Thiago da Silva <thiago@redhat.com>
Reviewed-on: http://review.gluster.org/6347
Reviewed-by: Luis Pabon <lpabon@redhat.com>
Tested-by: Luis Pabon <lpabon@redhat.com>
This commit is contained in:
Thiago da Silva 2013-11-25 12:18:03 -05:00 committed by Luis Pabon
parent 3796a1c757
commit ba6171a2df
4 changed files with 34 additions and 7 deletions

View File

@ -26,6 +26,7 @@ from sys import argv, exit
from swift.common.bufferedhttp import http_connect_raw as http_connect from swift.common.bufferedhttp import http_connect_raw as http_connect
from swift.common.utils import urlparse from swift.common.utils import urlparse
from prettytable import PrettyTable
if __name__ == '__main__': if __name__ == '__main__':
gettext.install('swauth', unicode=1) gettext.install('swauth', unicode=1)
@ -34,11 +35,11 @@ Usage: %prog [options] [account] [user]
If [account] and [user] are omitted, a list of accounts will be output. If [account] and [user] are omitted, a list of accounts will be output.
If [account] is included but not [user], an account's information will be If [account] is included but not [user], a list of users within the account
output, including a list of users within the account. will be output.
If [account] and [user] are included, the user's information will be output, If [account] and [user] are included, a list of groups the user belongs to
including a list of groups the user belongs to. will be ouptput.
If the [user] is '.groups', the active groups for the account will be listed. If the [user] is '.groups', the active groups for the account will be listed.
'''.strip()) '''.strip())
@ -46,6 +47,10 @@ If the [user] is '.groups', the active groups for the account will be listed.
action='store_true', default=False, help='Changes the output from ' action='store_true', default=False, help='Changes the output from '
'JSON to plain text. This will cause an account to list only the ' 'JSON to plain text. This will cause an account to list only the '
'users and a user to list only the groups.') 'users and a user to list only the groups.')
parser.add_option('-j', '--json', dest='json_format',
action='store_true', default=False, help='Output in JSON format. '
'This will print all information about given account or user, '
'including stored password.')
parser.add_option('-A', '--admin-url', dest='admin_url', parser.add_option('-A', '--admin-url', dest='admin_url',
default='http://127.0.0.1:8080/auth/', help='The URL to the auth ' default='http://127.0.0.1:8080/auth/', help='The URL to the auth '
'subsystem (default: http://127.0.0.1:8080/auth/') 'subsystem (default: http://127.0.0.1:8080/auth/')
@ -82,5 +87,12 @@ If the [user] is '.groups', the active groups for the account will be listed.
info = json.loads(body) info = json.loads(body)
for group in info[['accounts', 'users', 'groups'][len(args)]]: for group in info[['accounts', 'users', 'groups'][len(args)]]:
print group['name'] print group['name']
else: elif options.json_format:
print body print body
else:
info = json.loads(body)
h = ['accounts', 'users', 'groups'][len(args)]
table = PrettyTable([h.title()])
for group in info[h]:
table.add_row([group['name']])
print table

View File

@ -23,6 +23,7 @@ BuildRequires: python-setuptools
Requires : memcached Requires : memcached
Requires : openssl Requires : openssl
Requires : python Requires : python
Requires : python-prettytable
Requires : openstack-swift = 1.10.0 Requires : openstack-swift = 1.10.0
Requires : openstack-swift-account = 1.10.0 Requires : openstack-swift-account = 1.10.0
Requires : openstack-swift-container = 1.10.0 Requires : openstack-swift-container = 1.10.0

View File

@ -33,6 +33,10 @@ class Utils:
def deleteAccount(self,account_name,authurl='http://127.0.0.1:8080/auth/',user=config['admin_user'],key=config['admin_key']): def deleteAccount(self,account_name,authurl='http://127.0.0.1:8080/auth/',user=config['admin_user'],key=config['admin_key']):
return commands.getstatusoutput('swauth-delete-account %s -A %s -U %s -K %s' % (account_name,authurl, user, key)) return commands.getstatusoutput('swauth-delete-account %s -A %s -U %s -K %s' % (account_name,authurl, user, key))
@classmethod
def listAccounts(self,authurl='http://127.0.0.1:8080/auth/',user=config['admin_user'],key=config['admin_key']):
return commands.getstatusoutput('swauth-list -A %s -U %s -K %s' % (authurl, user, key))
@classmethod @classmethod
def swauthPrep(self,authurl='http://127.0.0.1:8080/auth/',user=config['admin_user'],key=config['admin_key']): def swauthPrep(self,authurl='http://127.0.0.1:8080/auth/',user=config['admin_user'],key=config['admin_key']):
return commands.getstatusoutput('swauth-prep -A %s -U %s -K %s' % (authurl, user, key)) return commands.getstatusoutput('swauth-prep -A %s -U %s -K %s' % (authurl, user, key))
@ -56,8 +60,8 @@ class Utils:
@classmethod @classmethod
def cleanAll(self): def cleanAll(self):
#TODO:It's a dirty hack,any suggestions? #TODO:It's a dirty hack,any suggestions?
commands.getstatusoutput('rm -rf '+os.path.join(config['devices'], config['gsmetadata_volume'], '*')) commands.getstatusoutput('sudo rm -rf '+os.path.join(config['devices'], config['gsmetadata_volume'], '*'))
commands.getstatusoutput('rm -rf '+os.path.join(config['devices'], config['gsmetadata_volume'], '.*')) return commands.getstatusoutput('sudo rm -rf '+os.path.join(config['devices'], config['gsmetadata_volume'], '.*'))
class TestSwauthPrep(unittest.TestCase): class TestSwauthPrep(unittest.TestCase):
@ -158,6 +162,15 @@ class TestAccount(unittest.TestCase):
self.assertNotEqual(status, 0, 'account deletion failed for accountdoesnotexist'+output) self.assertNotEqual(status, 0, 'account deletion failed for accountdoesnotexist'+output)
#TODO:more cases #TODO:more cases
def testListAcounts(self):
(status,output)=Utils.addAccount('test')
self.assertEqual(status, 0, 'account creation failed'+output)
(status,output)=Utils.listAccounts()
self.assertEqual(output,
'+----------+\n| Accounts |\n+----------+\n| test |\n+----------+',
'swauth-list failed:\n%s' % output)
class TestUser(unittest.TestCase): class TestUser(unittest.TestCase):

View File

@ -11,3 +11,4 @@ nosehtmloutput
sphinx>=1.1.2 sphinx>=1.1.2
mock>=0.8.0 mock>=0.8.0
python-keystoneclient python-keystoneclient
prettytable