diff options
| author | Jenkins <jenkins@review.openstack.org> | 2013-02-19 17:20:57 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2013-02-19 17:20:57 +0000 |
| commit | ee4cb431859046c7be93e09168961d2af3328ef7 (patch) | |
| tree | 9c074ee5990fcb698af70babe1f458b36e2cb18d /quantumclient/quantum | |
| parent | 49b20ec0fb186d21831da8e6d9653636f83dd39d (diff) | |
| parent | 73b93725fee0383c2b0ac73a309dc95063f12d48 (diff) | |
| download | python-neutronclient-ee4cb431859046c7be93e09168961d2af3328ef7.tar.gz | |
Merge "Allow known options after unknown ones in list and update command"
Diffstat (limited to 'quantumclient/quantum')
| -rw-r--r-- | quantumclient/quantum/v2_0/__init__.py | 59 | ||||
| -rw-r--r-- | quantumclient/quantum/v2_0/network.py | 6 | ||||
| -rw-r--r-- | quantumclient/quantum/v2_0/port.py | 7 |
3 files changed, 41 insertions, 31 deletions
diff --git a/quantumclient/quantum/v2_0/__init__.py b/quantumclient/quantum/v2_0/__init__.py index dde2a69..cba5ca1 100644 --- a/quantumclient/quantum/v2_0/__init__.py +++ b/quantumclient/quantum/v2_0/__init__.py @@ -98,6 +98,18 @@ def add_extra_argument(parser, name, _help): '[--key2 [type=int|bool|...] value ...]') +def is_number(s): + try: + float(s) # for int, long and float + except ValueError: + try: + complex(s) # for complex + except ValueError: + return False + + return True + + def parse_args_to_dict(values_specs): '''It is used to analyze the extra command options to command. @@ -156,7 +168,8 @@ def parse_args_to_dict(values_specs): _list_flag = True continue if not _item.startswith('--'): - if not current_item or '=' in current_item: + if (not current_item or '=' in current_item or + _item.startswith('-') and not is_number(_item)): raise exceptions.CommandError( "Invalid values_specs %s" % ' '.join(values_specs)) _value_number += 1 @@ -318,8 +331,6 @@ class UpdateCommand(QuantumCommand): parser.add_argument( 'id', metavar=self.resource, help='ID or name of %s to update' % self.resource) - add_extra_argument(parser, 'value_specs', - 'new values for the %s' % self.resource) self.add_known_arguments(parser) return parser @@ -327,11 +338,14 @@ class UpdateCommand(QuantumCommand): self.log.debug('run(%s)' % parsed_args) quantum_client = self.get_client() quantum_client.format = parsed_args.request_format - value_specs = parsed_args.value_specs - dict_args = self.args2body(parsed_args).get(self.resource, {}) - dict_specs = parse_args_to_dict(value_specs) - body = {self.resource: dict(dict_args.items() + - dict_specs.items())} + _extra_values = parse_args_to_dict(self.values_specs) + _merge_args(self, parsed_args, _extra_values, + self.values_specs) + body = self.args2body(parsed_args) + if self.resource in body: + body[self.resource].update(_extra_values) + else: + body[self.resource] = _extra_values if not body[self.resource]: raise exceptions.CommandError( "Must specify new values to update %s" % self.resource) @@ -400,30 +414,29 @@ class ListCommand(QuantumCommand, lister.Lister): def get_parser(self, prog_name): parser = super(ListCommand, self).get_parser(prog_name) add_show_list_common_argument(parser) - add_extra_argument(parser, 'filter_specs', 'filters options') return parser - def retrieve_list(self, parsed_args): - """Retrieve a list of resources from Quantum server""" - quantum_client = self.get_client() - search_opts = parse_args_to_dict(parsed_args.filter_specs) - self.log.debug('search options: %s', search_opts) - quantum_client.format = parsed_args.request_format + def args2search_opts(self, parsed_args): + search_opts = {} fields = parsed_args.fields - extra_fields = search_opts.get('fields', []) - if extra_fields: - if isinstance(extra_fields, list): - fields.extend(extra_fields) - else: - fields.append(extra_fields) - if fields: + if parsed_args.fields: search_opts.update({'fields': fields}) if parsed_args.show_details: search_opts.update({'verbose': 'True'}) + return search_opts + + def retrieve_list(self, parsed_args): + """Retrieve a list of resources from Quantum server""" + quantum_client = self.get_client() + quantum_client.format = parsed_args.request_format + _extra_values = parse_args_to_dict(self.values_specs) + _merge_args(self, parsed_args, _extra_values, + self.values_specs) + search_opts = self.args2search_opts(parsed_args) + search_opts.update(_extra_values) obj_lister = getattr(quantum_client, "list_%ss" % self.resource) data = obj_lister(**search_opts) - collection = self.resource + "s" return data.get(collection, []) diff --git a/quantumclient/quantum/v2_0/network.py b/quantumclient/quantum/v2_0/network.py index b714ae5..dfc0171 100644 --- a/quantumclient/quantum/v2_0/network.py +++ b/quantumclient/quantum/v2_0/network.py @@ -60,9 +60,9 @@ class ListExternalNetwork(ListNetwork): log = logging.getLogger(__name__ + '.ListExternalNetwork') def retrieve_list(self, parsed_args): - if '--' not in parsed_args.filter_specs: - parsed_args.filter_specs.append('--') - parsed_args.filter_specs.append('--router:external=True') + external = '--router:external=True' + if external not in self.values_specs: + self.values_specs.append('--router:external=True') return super(ListExternalNetwork, self).retrieve_list(parsed_args) diff --git a/quantumclient/quantum/v2_0/port.py b/quantumclient/quantum/v2_0/port.py index 76468b7..753df0e 100644 --- a/quantumclient/quantum/v2_0/port.py +++ b/quantumclient/quantum/v2_0/port.py @@ -52,13 +52,10 @@ class ListRouterPort(ListCommand): list_columns = ['id', 'name', 'mac_address', 'fixed_ips'] def get_parser(self, prog_name): - parser = super(ListCommand, self).get_parser(prog_name) - quantumv20.add_show_list_common_argument(parser) + parser = super(ListRouterPort, self).get_parser(prog_name) parser.add_argument( 'id', metavar='router', help='ID or name of router to look up') - quantumv20.add_extra_argument(parser, 'filter_specs', - 'filters options') return parser def get_data(self, parsed_args): @@ -66,7 +63,7 @@ class ListRouterPort(ListCommand): quantum_client.format = parsed_args.request_format _id = quantumv20.find_resourceid_by_name_or_id( quantum_client, 'router', parsed_args.id) - parsed_args.filter_specs.append('--device_id=%s' % _id) + self.values_specs.append('--device_id=%s' % _id) return super(ListRouterPort, self).get_data(parsed_args) |
