From b7677be573bcd3558d30f3b58e8a75b7b35f40e7 Mon Sep 17 00:00:00 2001 From: tengqm Date: Tue, 20 Jan 2015 21:45:14 +0800 Subject: [PATCH] Revised profile list implementation --- senlinclient/v1/client.py | 33 +++++++++++++++--------------- senlinclient/v1/models.py | 2 ++ senlinclient/v1/shell.py | 42 ++++++++++++++++++++++++++++++--------- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/senlinclient/v1/client.py b/senlinclient/v1/client.py index f1315d85..5608fa9a 100644 --- a/senlinclient/v1/client.py +++ b/senlinclient/v1/client.py @@ -14,6 +14,7 @@ import inspect import json import uuid +from openstack import exceptions as exc from openstack.identity import identity_service from openstack.network.v2 import thin as thins from openstack import transport as trans @@ -25,11 +26,7 @@ class Client(object): self.auth = session.authenticator def get_options(self, options): - try: - iddy = uuid.UUID(str(options)) - return {'id': iddy} - except ValueError: - return json.loads(options) + return json.loads(options) def transport(self, opts): '''Create a transport given some options. @@ -59,19 +56,22 @@ class Client(object): return xport def list(self, cls, options=None): - objs = cls.list(self.session, path_args=options) - return objs + try: + result = cls.list(self.session, path_args=options) + return result + except exc.HttpException as ex: + print(ex) + return None def list_short(self, cls, options=None): - path_args = None - if options: - path_args = self.get_options(options) - objs = cls.list_short(self.session, path_args=path_args) - return objs + try: + return cls.list_short(self.session, path_args=None, **options) + except exc.HttpException as ex: + print(ex) + return None - def create(self, cls, options): - #kwargs = self.get_options(options) - obj = cls.new(**options) + def create(self, cls, params): + obj = cls.new(**params) return obj.create(self.session) def get(self, cls, options=None): @@ -94,8 +94,7 @@ class Client(object): obj.update(self.session) def delete(self, cls, options): - kwargs = self.get_options(options) - obj = cls.new(**kwargs) + obj = cls.new(**options) obj.delete(self.session) def head(self, cls, options): diff --git a/senlinclient/v1/models.py b/senlinclient/v1/models.py index 32dc622f..415dfd7c 100644 --- a/senlinclient/v1/models.py +++ b/senlinclient/v1/models.py @@ -97,6 +97,8 @@ class Profile(resource.Resource): spec = resource.prop('spec', type=dict) permission = resource.prop('permission') tags = resource.prop('tags', type=dict) + created_time = resource.prop('created_time') + deleted_time = resource.prop('deleted_time') class PolicyType(resource.Resource): diff --git a/senlinclient/v1/shell.py b/senlinclient/v1/shell.py index 8759ea42..14863f43 100644 --- a/senlinclient/v1/shell.py +++ b/senlinclient/v1/shell.py @@ -10,6 +10,7 @@ # License for the specific language governing permissions and limitations # under the License. +import json import logging from oslo_serialization import jsonutils @@ -86,15 +87,18 @@ def do_profile_type_template(sc, args): help=_('Only return profiles that appear after the given ID.')) def do_profile_list(sc, args=None): '''List profiles that meet the criteria.''' - queries = {} fields = ['id', 'name', 'type', 'permission', 'created_time'] - if args: - queries = {'limit': args.limit, - 'marker': args.marker, - 'show_deleted': args.show_deleted} + queries = {'show_deleted': False} + if args.limit: + queries['limit'] = args.limit + if args.marker: + queries['marker'] = args.marker + if args.show_deleted: + queries['show_deleted'] = str(bool(args.show_deleted)) - profiles = sc.list(models.Profile, queries) - utils.print_list(profiles, fields, sortby_index=1) + profiles = sc.list_short(models.Profile, queries) + if profiles: + utils.print_list(profiles, fields, sortby_index=1) @utils.arg('-t', '--profile-type', metavar='', @@ -123,8 +127,28 @@ def do_profile_create(sc, args): 'tags': utils.format_parameters(args.tags), } - sc.create(models.Profile, params) - do_profile_list(sc) + profile = sc.create(models.Profile, params) + if profile: + print("Profile ID: %s" % profile.id) + + +@utils.arg('id', metavar='', nargs='+', + help=_('Name or ID of profile(s) to delete.')) +def do_profile_delete(sc, args): + '''Delete profile(s).''' + failure_count = 0 + + for cid in args.id: + try: + query = {'id': cid} + sc.delete(models.Profile, query) + except exc.HTTPNotFound as ex: + failure_count += 1 + print(ex) + if failure_count == len(args.id): + msg = _('Failed to delete any of the specified profile(s).') + raise exc.CommandError(msg) + print('Profile deleted: %s' % args.id) #### POLICY TYPES