diff --git a/karborclient/v1/protectables.py b/karborclient/v1/protectables.py index d0d8463..55fc2c2 100644 --- a/karborclient/v1/protectables.py +++ b/karborclient/v1/protectables.py @@ -77,14 +77,27 @@ class ProtectableManager(base.ManagerWithFind): sort_dir=sort_dir, sort=sort) return self._list(url, response_key='instances', obj_class=Instances) - def get_instance(self, type, id, session_id=None): + def get_instance(self, type, id, search_opts=None, session_id=None): if session_id: headers = {'X-Configuration-Session': session_id} else: headers = {} - url = "/protectables/{protectable_type}/" \ - "instances/{protectable_id}".format(protectable_type=type, - protectable_id=id) + + if search_opts is None: + search_opts = {} + query_params = {} + for key, val in search_opts.items(): + if val: + query_params[key] = val + query_string = "" + if query_params: + params = sorted(query_params.items(), key=lambda x: x[0]) + query_string = "?%s" % parse.urlencode(params) + + url = ("/protectables/{protectable_type}/instances/" + "{protectable_id}{query_string}").format( + protectable_type=type, protectable_id=id, + query_string=query_string) return self._get(url, response_key="instance", headers=headers) def _build_instances_list_url(self, protectable_type, diff --git a/karborclient/v1/shell.py b/karborclient/v1/shell.py index 6376ec2..24ab0be 100644 --- a/karborclient/v1/shell.py +++ b/karborclient/v1/shell.py @@ -417,16 +417,28 @@ def do_protectable_show(cs, args): utils.print_dict(protectable.to_dict()) -@utils.arg('protectable_id', - metavar='', - help='Protectable instance id.') @utils.arg('protectable_type', metavar='', help='Protectable type.') +@utils.arg('protectable_id', + metavar='', + help='Protectable instance id.') +@utils.arg('--parameters', + type=str, + nargs='*', + metavar='', + default=None, + help='Show a instance by parameters key and value pair. ' + 'Default=None.') def do_protectable_show_instance(cs, args): """Shows instance details.""" + search_opts = { + 'parameters': (_extract_instances_parameters(args) + if args.parameters else None), + } instance = cs.protectables.get_instance(args.protectable_type, - args.protectable_id) + args.protectable_id, + search_opts=search_opts) utils.print_dict(instance.to_dict()) @@ -462,11 +474,20 @@ def do_protectable_show_instance(cs, args): 'form of [:]. ' 'Valid keys: %s. ' 'Default=None.') % ', '.join(base.SORT_KEY_VALUES))) +@utils.arg('--parameters', + type=str, + nargs='*', + metavar='', + default=None, + help='List instances by parameters key and value pair. ' + 'Default=None.') def do_protectable_list_instances(cs, args): """Lists all protectable instances.""" search_opts = { 'type': args.type, + 'parameters': (_extract_instances_parameters(args) + if args.parameters else None), } if args.sort and (args.sort_key or args.sort_dir): @@ -490,6 +511,19 @@ def do_protectable_list_instances(cs, args): sortby_index=sortby_index) +def _extract_instances_parameters(args): + parameters = {} + for parameter in args.parameters: + if '=' in parameter: + (key, value) = parameter.split('=', 1) + else: + key = parameter + value = None + + parameters[key] = value + return parameters + + @utils.arg('provider_id', metavar='', help='Id of provider.')