adding error handling to gswauth-cleanup-tokens tool

added input validation for a couple of options and
error handling in case a non-existing account name
is provided

Change-Id: I6d703d584552fc7b7574f34e79ed25a2982b6d5e
Signed-off-by: Thiago da Silva <thiago@redhat.com>
Reviewed-on: http://review.gluster.org/6767
Reviewed-by: Prashanth Pai <ppai@redhat.com>
Tested-by: Prashanth Pai <ppai@redhat.com>
Reviewed-by: pushpesh sharma <psharma@redhat.com>
Tested-by: pushpesh sharma <psharma@redhat.com>
Reviewed-by: Chetan Risbud <crisbud@redhat.com>
This commit is contained in:
Thiago da Silva 2014-01-23 13:43:28 -05:00 committed by Chetan Risbud
parent ec2c548af9
commit 3ee9a70402
2 changed files with 27 additions and 10 deletions

View File

@ -60,19 +60,39 @@ if __name__ == '__main__':
parser.parse_args(['-h']) parser.parse_args(['-h'])
if options.admin_key is None: if options.admin_key is None:
parser.parse_args(['-h']) parser.parse_args(['-h'])
options.admin_url = options.admin_url.rstrip('/') options.admin_url = options.admin_url.rstrip('/')
if not options.admin_url.endswith('/v1.0'): if not options.admin_url.endswith('/v1.0'):
options.admin_url += '/v1.0' options.admin_url += '/v1.0'
options.admin_user = '.super_admin:.super_admin' options.admin_user = '.super_admin:.super_admin'
options.token_life = timedelta(0, float(options.token_life))
options.sleep = float(options.sleep) try:
options.token_life = timedelta(0, float(options.token_life))
options.sleep = float(options.sleep)
except ValueError:
parser.parse_args(['-h'])
conn = Connection(options.admin_url, options.admin_user, options.admin_key) conn = Connection(options.admin_url, options.admin_user, options.admin_key)
if options.purge_account: if options.purge_account:
marker = None marker = None
while True: while True:
if options.verbose: if options.verbose:
print 'GET %s?marker=%s' % (options.purge_account, marker) print 'GET %s?marker=%s' % (options.purge_account, marker)
objs = conn.get_container(options.purge_account, marker=marker)[1] try:
objs = conn.get_container(options.purge_account,
marker=marker)[1]
except ClientException, e:
if e.http_status == 404:
exit('Account %s not found.' % (options.purge_account))
elif e.http_status == 401:
exit('Cleanup tokens failed: 401 Unauthorized: ' \
'Invalid user/key provided')
else:
exit('Purging %s failed with status '
'code %d' % (options.purge_account, e.http_status))
except socket.error, (errno, msg):
exit('Token clean-up failed: %s. ' \
'Check that the admin_url is valid' % msg)
if objs: if objs:
marker = objs[-1]['name'] marker = objs[-1]['name']
else: else:
@ -112,7 +132,8 @@ if __name__ == '__main__':
exit('Container %s not found. gswauth-prep needs to be ' exit('Container %s not found. gswauth-prep needs to be '
'rerun' % (container)) 'rerun' % (container))
elif e.http_status == 401: elif e.http_status == 401:
exit('Cleanup tokens failed: 401 Unauthorized: Invalid user/key provided') exit('Cleanup tokens failed: 401 Unauthorized: ' \
'Invalid user/key provided')
else: else:
exit('Object listing on container %s failed with status ' exit('Object listing on container %s failed with status '
'code %d' % (container, e.http_status)) 'code %d' % (container, e.http_status))

View File

@ -766,15 +766,11 @@ class TestCleanUPToken(unittest.TestCase):
#https://bugs.launchpad.net/gluster-swift/+bug/1271550 #https://bugs.launchpad.net/gluster-swift/+bug/1271550
#cleanup token with token-life option non numeric value #cleanup token with token-life option non numeric value
(status,output)=Utils.cleanToken(option='token-life', value='notanumaric') (status,output)=Utils.cleanToken(option='token-life', value='notanumaric')
self.assertNotEqual(status, 0, 'clean up success with token-life option token-life non numeric value'+output) self.assertEqual('Usage:' in output, True, 'clean up success with token-life option non numeric value'+output)
self.assertEqual('ValueError' in output,True, 'clean up \
success with token-life option non numeric value: '+output)
#cleanup token with sleep option non numeric value #cleanup token with sleep option non numeric value
(status,output)=Utils.cleanToken(option='sleep', value='notanumeric') (status,output)=Utils.cleanToken(option='sleep', value='notanumeric')
self.assertNotEqual(status, 0, 'clean up failed with sleep option non numeric value'+output) self.assertEqual('Usage:' in output, True, 'clean up success with sleep option non numeric value'+output)
self.assertEqual('ValueError' in output,True, 'clean up \
success with token-life option non numeric value: '+output)
def testSetAccountService(self): def testSetAccountService(self):
self.setTestAccUserEnv() self.setTestAccUserEnv()