From 9353d90a656acc45451956b4048f7a5befcf22cd Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Fri, 3 Aug 2018 10:35:12 +0300 Subject: Change: have generic get, list, create, set-secret commands For all kinds of entities. --- qvisqvetool | 54 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 36 insertions(+), 18 deletions(-) diff --git a/qvisqvetool b/qvisqvetool index 90bfd70..731606e 100755 --- a/qvisqvetool +++ b/qvisqvetool @@ -40,6 +40,13 @@ import qvisqve json_content_type = 'application/json' +paths = { + 'user': '/users', + 'client': '/clients', + 'application': '/applications', +} + + class QvisqveTool(cliapp.Application): def add_settings(self): @@ -86,40 +93,51 @@ class QvisqveTool(cliapp.Application): api, token = self.get_api() self.output.write('{}\n'.format(token)) - def cmd_get_user(self, args): + def cmd_get(self, args): api, token = self.get_api() - for username in args: - path = '/users/{}'.format(username) + kind = args[0] + names = args[1:] + path = paths[kind] + for name in names: + path = '{}/{}'.format(path, name) r = api.GET(token, path) + self.output.write('{} {}:\n'.format(kind, name)) json.dump(r.json(), self.output, indent=4) self.output.write('\n') - def cmd_create_user(self, args): - username, password = args - api, token = self.get_api() + def cmd_list(self, args): + for kind in args: + path = paths[kind] + + api, token = self.get_api() + r = api.GET(token, path) + names = r.json()['resources'] + for name in sorted(names): + self.output.write('{} {}\n'.format(kind, name)) - user = { - 'id': username, + def cmd_create(self, args): + kind, name, password = args + + entity = { + 'id': name, } - path = '/users' - api.POST(token, path, json.dumps(user), json_content_type) + path = paths[kind] + + api, token = self.get_api() + api.POST(token, path, json.dumps(entity), json_content_type) secret = { 'secret': password, } - path = '/users/{}/secret'.format(username) - api.PUT(token, path, json.dumps(secret), json_content_type) + secret_path = '{}/{}/secret'.format(path, name) + api.PUT(token, secret_path, json.dumps(secret), json_content_type) - self.output.write('Created {}\n'.format(username)) + entity_path = '{}/{}'.format(path, name) + self.output.write('Created {} {}\n'.format(kind, name)) def cmd_set_secret(self, args): kind, name, secret = args - kinds = { - 'user': 'users', - 'client': 'clients', - 'application': 'applications', - } kind = kinds.get(kind, kind) api, token = self.get_api() -- cgit v1.2.1