Juan Hernandez has uploaded a new change for review. Change subject: cli: Add "parent" prefix to parent identifier options ......................................................................
cli: Add "parent" prefix to parent identifier options Currently the way to specify the identifier of a parent entity is to use the "--*-identifier" or "--*-name" options. These option, specially the "name" option, may conflict with other options used to populate the request. For example, when adding a storage domain the "--host-name" option is used to indicate what host name should perform the operation, but the CLI assumes that it is used to specify the parent host, which doesn't make sense. This patch changes the CLI so that it will require the "parent" prefix in the option used to specify the parent name. The option used to specify the parent identifier will also support the "parent" prefix, but it will still work without it, for backwards compatibility. Change-Id: I4150389b6c87cc178865757f99a2e09e272c530f Bug-Url: https://bugzilla.redhat.com/1213393 Signed-off-by: Juan Hernandez <[email protected]> --- M src/ovirtcli/command/action.py M src/ovirtcli/command/add.py M src/ovirtcli/command/command.py M src/ovirtcli/command/list.py M src/ovirtcli/command/remove.py M src/ovirtcli/command/show.py M src/ovirtcli/command/update.py M src/ovirtcli/utils/autocompletionhelper.py 8 files changed, 82 insertions(+), 50 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/54/40054/1 diff --git a/src/ovirtcli/command/action.py b/src/ovirtcli/command/action.py index fa1d18f..dabd02b 100644 --- a/src/ovirtcli/command/action.py +++ b/src/ovirtcli/command/action.py @@ -68,13 +68,13 @@ - This help will list all available attribute options for specific subresource - * format - help action resource resource_name --subresource-identifier mysubresource - * example - help action nic bond0 --host-identifier myhost + * format - help action resource resource_name --parent-subresource-identifier mysubresource + * example - help action nic bond0 --parent-host-identifier myhost - This help will display all available attribute options for specific action on given subresource - * format - help action action_name resource resource_name --subresource-identifier mysubresource - * example - help action attach nic bond0 --host-identifier myhost + * format - help action action_name resource resource_name --parent-subresource-identifier mysubresource + * example - help action attach nic bond0 --parent-host-identifier myhost == Available types == @@ -138,13 +138,13 @@ Some objects can exist inside other objects. For example, a disk can exist in the content of a virtual machine. In this case, one or more - object identifier options need to be provided to identify the - containing object. + parent object identifier options need to be provided to identify the + parent object. - An object identifier is an option of the form '--<type>-identifier - <id>' or '--<type>-name <name>'. This would identify an object with - type <type> and id <id> or name <name>. See the examples section below - for a few examples. + An parent object identifier is an option of the form + '--parent-<type>-identifier <id>' or '--parent-<type>-name <name>'. This + would identify a parent object with type <type> and id <id> or name + <name>. See the examples section below for a few examples. == Attribute Options == @@ -161,7 +161,7 @@ - This example detaches a host nic with id 'mynic' from host 'myhost': - $ action nic mynic detach --host-identifier myhost + $ action nic mynic detach --parent-host-name myhost == Return values == diff --git a/src/ovirtcli/command/add.py b/src/ovirtcli/command/add.py index 0f49f48..44e0052 100644 --- a/src/ovirtcli/command/add.py +++ b/src/ovirtcli/command/add.py @@ -45,8 +45,8 @@ - This help will list all available attribute options for given subresource creation - * format - help add subresource_name --resource-identifier xxx - * example - help add disk --vm-identifier myvm + * format - help add subresource_name --parent-resource-identifier xxx + * example - help add disk --parent-vm-identifier myvm == Available Types == @@ -61,10 +61,10 @@ object identifier options need to be provided to identify the containing object. - An object identifier is an option of the form '--<type>-identifier - <id>' or '--<type>-name <name>'. This would identify an object with - type <type> and id <id> or name <name>. See the examples section below - for a few examples. + An object identifier is an option of the form + '--parent-<type>-identifier <id>' or '--parent-<type>-name <name>'. This + would identify an object with type <type> and id <id> or name <name>. + See the examples section below for a few examples. == Attribute Options == @@ -91,7 +91,7 @@ - This example create vm nic: - $ add nic --vm-identifier cli_vm3 --network-name engine --name test + $ add nic --parent-vm-name cli_vm3 --network-name engine --name test == Return Values == diff --git a/src/ovirtcli/command/command.py b/src/ovirtcli/command/command.py index 444d053..3c256fb 100644 --- a/src/ovirtcli/command/command.py +++ b/src/ovirtcli/command/command.py @@ -30,6 +30,39 @@ from ovirtsdk.xml import params +# These are the regular expressions used to check if an option should be +# used to find a parent entity. The preferred way is using the "parent" +# prefix, as it avoid potential conflicts with other options, but we +# need to support the "identifier" suffix without the "parent" prefix +# for backwards compatibility. +PARENT_ID_OPTION_EXPRESSIONS = [ + re.compile(r"^--parent-(?P<type>.+)-(identifier|name)$"), + re.compile(r"^--(?P<type>.+)-identifier$"), +] + +def _is_parent_id_option(option): + """ + Checks if the given option name is a reference to a parent + entity. + """ + for parent_id_option_expression in PARENT_ID_OPTION_EXPRESSIONS: + if parent_id_option_expression.match(option): + return True + return False + +def _get_parent_id_type(option): + """ + Extracts the name of the type from an option that is a reference to + a parent entity. For example, if the option is "--parent-host-name" + this method will return "host". + """ + for parent_id_option_expression in PARENT_ID_OPTION_EXPRESSIONS: + match = parent_id_option_expression.match(option) + if match is not None: + return match.group("type") + return None + + class OvirtCommand(Command): """Base class for oVirt commands.""" @@ -41,8 +74,7 @@ def resolve_base(self, options): """ - Resolves a base object from a set of - '--type-(identifier|name) value' options. + Resolves a base object from a set of parent identifier options. """ collection_candidate = self.check_connection() @@ -51,7 +83,7 @@ parnet_candidates = [ key for key in options.keys() - if re.match(r"--(.+)-(identifier|name)$", key) + if _is_parent_id_option(key) ] parnet_candidates_permutations = list(itertools.permutations(parnet_candidates)) @@ -61,7 +93,7 @@ key = item val = options[key] parnet_candidate_locator += 1 - typename = re.sub(r"--(.+)-(identifier|name)$", r"\1", key) + typename = _get_parent_id_type(key) coll = TypeHelper.to_plural(typename) if not (TypeHelper.isKnownType(typename) or TypeHelper.isKnownType(coll)): @@ -215,9 +247,9 @@ """Updates object properties with values from `options'.""" for key in options.keys(): + if _is_parent_id_option(key): continue prop = key.replace('--', '') val = options[key] - if not prop.endswith('-id') and prop.endswith('-identifier'): continue if type(val) == types.ListType: for item in val: self.__do_set_data(obj=obj, prop=prop, fq_prop=key, val=item) @@ -260,7 +292,7 @@ self.error(Messages.Error.INVALID_KWARGS_CONTENT) mopts = {} for k, v in opts.iteritems(): - if k != query_arg and k != kwargs_arg and not re.search(r"-(identifier|name)$", k): + if k != query_arg and k != kwargs_arg and not _is_parent_id_option(k): mopts[k if not k.startswith('--') else k[2:]] = v kw.update(mopts) @@ -317,7 +349,7 @@ if opt.startswith('--'): opt_item = opt[2:] else: opt_item = opt - if opt_item not in options and not re.search(r"-(identifier|name)$", opt_item): + if opt_item not in options and not _is_parent_id_option(opt_item): self.error(Messages.Error.NO_SUCH_OPTION % opt) def get_object(self, typ, obj_id, base=None, opts={}, context_variants=[]): diff --git a/src/ovirtcli/command/list.py b/src/ovirtcli/command/list.py index c408dc9..cd2b822 100644 --- a/src/ovirtcli/command/list.py +++ b/src/ovirtcli/command/list.py @@ -51,8 +51,8 @@ - This help will list all available attribute options for listing subcollection of given types - * format - help list subtypes --parent-identifier - * example - help list disks --vm-identifier myvm + * format - help list subtypes --parent-type-identifier + * example - help list disks --parent-vm-name myvm == Available Types == @@ -98,7 +98,7 @@ - This example list all disks by vm_id in virtual machine 'myvm': - $ list disks --vm-identifier myvm + $ list disks --parent-vm-name myvm - This example list all vms having memory size of 1073741824 using client side filtering (this kind of filtering is useful on non queryable collections @@ -110,7 +110,7 @@ - This example retrieves vm disk with name 'Disk 3' using client side filtering as oVirt dialect is not available on vm disks collection. - $ list disks --vm-identifier myvm --kwargs "name=Disk 3" + $ list disks --parent-vm-name myvm --kwargs "name=Disk 3" == Return values == diff --git a/src/ovirtcli/command/remove.py b/src/ovirtcli/command/remove.py index af7b49a..613270f 100644 --- a/src/ovirtcli/command/remove.py +++ b/src/ovirtcli/command/remove.py @@ -51,8 +51,8 @@ - This help will list all available attribute options for given subresource removal - * format - help remove subtype --resource-identifier - * example - help remove disk --vm-identifier iscsi_desktop + * format - help remove subtype --parent-resource-identifier + * example - help remove disk --parent-vm-name iscsi_desktop == Available Types == @@ -67,10 +67,10 @@ object identifier options need to be provided to identify the containing object. - An object identifier is an option of the form '--<type>-identifier - <id>' or '--<type>-name <name>'. This would identify an object with - type <type> and id <id> or name <name>. See the examples section below - for a few examples. + An object identifier is an option of the form + '--parent-<type>-identifier <id>' or '--parent-<type>-name <name>'. This + would identify an object with type <type> and id <id> or name <name>. + See the examples section below for a few examples. == Examples == @@ -80,11 +80,11 @@ - This example removes the disk "disk0" from the virtual machine named "myvm" - $ remove disk disk0 --vm-name myvm + $ remove disk disk0 --parent-vm-name myvm - This example removes the storagedomain "mydomain" using host named "myhost" - $ remove storagedomain mydomain --host-id myhost + $ remove storagedomain mydomain --parent-host-name myhost == Return values == diff --git a/src/ovirtcli/command/show.py b/src/ovirtcli/command/show.py index a089fef..a2d3e5f 100644 --- a/src/ovirtcli/command/show.py +++ b/src/ovirtcli/command/show.py @@ -65,8 +65,8 @@ object identifier options need to be provided to identify the containing object. - An object identifier is an option of the form '--<type>-identifier - <id>' or '--<type>-name <name>'. This would identify an object with + An object identifier is an option of the form '--parent-<type>-identifier + <id>' or '--parent-<type>-name <name>'. This would identify an object with type <type> and id <id> or name <name>. See the examples section below for a few examples. @@ -80,12 +80,12 @@ virtual machine with identifier "a71ff45c-d2d7-44d7-98e6-be06f5a82016": - $ show disk disk1 --vm-identifier a71ff45c-d2d7-44d7-98e6-be06f5a82016 + $ show disk disk1 --parent-vm-identifier a71ff45c-d2d7-44d7-98e6-be06f5a82016 - This example shows information about the nic named 'nic1' of the virtual machine with name "myvm": - $ show nic nic1 --vm-name myvm + $ show nic nic1 --parent-vm-name myvm == Return values == @@ -129,8 +129,8 @@ # Raise an error if object identifier xxx is not specified #855750 # e.g: # show vm xxx - # show disk xxx --vm-identifier yyy - # show disk xxx --vm-name zzz + # show disk xxx --parent-vm-identifier yyy + # show disk xxx --parent-vm-name zzz if len(args) < 2 and ( len(opts) == 0 or ( diff --git a/src/ovirtcli/command/update.py b/src/ovirtcli/command/update.py index 22df7ab..9968401 100644 --- a/src/ovirtcli/command/update.py +++ b/src/ovirtcli/command/update.py @@ -65,10 +65,10 @@ object identifier options need to be provided to identify the containing object. - An object identifier is an option of the form '--<type>-identifier - <id>' or '--<type>-name <name>'. This would identify an object with - type <type> and id <id> or name <name>. See the examples section below - for a few examples. + An object identifier is an option of the form + '--parent-<type>-identifier <id>' or '--parent-<type>-name <name>'. This + would identify an object with type <type> and id <id> or name <name>. + See the examples section below for a few examples. == Attribute Options == @@ -88,7 +88,7 @@ - This example updates a virtual machine disk with name "mydisk", setting "bootable" to true. - $ update disk "mydisk" --vm-identifier "myvm" --bootable true + $ update disk "mydisk" --parent-vm-name "myvm" --bootable true == Return Values == diff --git a/src/ovirtcli/utils/autocompletionhelper.py b/src/ovirtcli/utils/autocompletionhelper.py index af16034..e8dbb5d 100644 --- a/src/ovirtcli/utils/autocompletionhelper.py +++ b/src/ovirtcli/utils/autocompletionhelper.py @@ -58,11 +58,11 @@ parameter is the name of the object, and the result is a list containing the options that can be used to specify that object as a parent. For example, if the object name is 'vm' then the result - will be a list containing 'vm-identifier' and 'vm-name'. + will be a list containing 'parent-vm-identifier' and 'parent-vm-name'. """ if obj == 'None': return [] - return ['%s-identifier' % obj, '%s-name' % obj] + return ['parent-%s-identifier' % obj, 'parent-%s-name' % obj] @staticmethod def _get_verb_replecations(container, text): -- To view, visit https://gerrit.ovirt.org/40054 To unsubscribe, visit https://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I4150389b6c87cc178865757f99a2e09e272c530f Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine-cli Gerrit-Branch: master Gerrit-Owner: Juan Hernandez <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
