Juan Hernandez has uploaded a new change for review.

Change subject: cli: Support show by alias
......................................................................

cli: Support show by alias

Some collections, in particular the disks collection, don't support
retrieving elements by name, but by alias. This patch adds support for
this type of collections to the "show" command.

Change-Id: Ibf922ecf4d3f34a2c200ec7c145f18c15a8efd71
Bug-Url: https://bugzilla.redhat.com/1091725
Signed-off-by: Juan Hernandez <juan.hernan...@redhat.com>
(cherry picked from commit 2f145a0292cf1d639481102530fc964cdb8fdf5a)
---
M src/ovirtcli/command/command.py
1 file changed, 41 insertions(+), 21 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine-cli refs/changes/13/32413/1

diff --git a/src/ovirtcli/command/command.py b/src/ovirtcli/command/command.py
index bdedc47..43acd74 100644
--- a/src/ovirtcli/command/command.py
+++ b/src/ovirtcli/command/command.py
@@ -319,10 +319,6 @@
         """Return an object by id or name."""
         self.check_connection()
         connection = self.context.connection
-        name, kwargs = self._get_query_params(opts, query_arg='--name')
-
-        candidate = typ if typ is not None and isinstance(typ, type('')) \
-                        else type(typ).__name__.lower()
 
         if base:
             options = self.get_options(method='get', resource=base,
@@ -336,25 +332,11 @@
 
         self.validate_options(opts, options)
 
-        if name and 'name' not in options:
-            self.error(Messages.Error.NO_NAME)
-        if kwargs and kwargs.has_key('id') and 'id' not in options:
-            self.error(Messages.Error.NO_ID % 'show')
+        candidate = typ if typ is not None and isinstance(typ, type('')) \
+                        else type(typ).__name__.lower()
 
         if hasattr(base, candidate + 's'):
             coll = getattr(base, candidate + 's')
-            if coll is not None:
-                if name:
-                    return self.__get_by_name(coll, name, kwargs)
-                if kwargs and kwargs.has_key('id'):
-                    id, kwargs = self._get_query_params(opts, query_arg='--id')
-                    return self.__get_by_id(coll, str(id), kwargs)
-                else:
-                    identifier = self.__produce_identifier(obj_id)
-                    if identifier:
-                        return self.__get_by_id(coll, str(obj_id), kwargs)
-                    else:
-                        return self.__get_by_name(coll, obj_id, kwargs)
         else:
             err_str = Messages.Error.NO_SUCH_TYPE_OR_ARS_NOT_VALID
             if context_variants:
@@ -362,15 +344,53 @@
                                       % str(context_variants))
             self.error(err_str % candidate)
 
-        return None
+        if obj_id is not None:
+            obj_uuid = self.__produce_identifier(obj_id)
+            _, kwargs = self._get_query_params(opts)
+            if obj_uuid is not None and 'id' in options:
+                return self.__get_by_id(coll, obj_id, kwargs)
+            if 'name' in options:
+                return self.__get_by_name(coll, obj_id, kwargs)
+            if 'alias' in options:
+                return self.__get_by_alias(coll, obj_id, kwargs)
+            return None
+
+        if 'id' in options:
+            obj_id, kwargs = self._get_query_params(opts, query_arg='--id')
+            if obj_id is not None:
+                return self.__get_by_id(coll, obj_id, kwargs)
+
+        if 'name' in options:
+            obj_id, kwargs = self._get_query_params(opts, query_arg='--name')
+            if obj_id is not None:
+                return self.__get_by_name(coll, obj_id, kwargs)
+
+        if 'alias' in options:
+            obj_id, kwargs = self._get_query_params(opts, query_arg='--alias')
+            if obj_id is not None:
+                return self.__get_by_alias(coll, obj_id, kwargs)
+
+        self.error(Messages.Error.NO_ID % 'show')
+
+    def __get_by_alias(self, coll, alias, kwargs):
+        if 'alias' in kwargs:
+            del kwargs['alias']
+        if kwargs:
+            return coll.get(alias=alias, **kwargs)
+        else:
+            return coll.get(alias=alias)
 
     def __get_by_name(self, coll, name, kwargs):
+        if 'name' in kwargs:
+            del kwargs['name']
         if kwargs:
             return coll.get(name=name, **kwargs)
         else:
             return coll.get(name=name)
 
     def __get_by_id(self, coll, id, kwargs):
+        if 'id' in kwargs:
+            del kwargs['id']
         if kwargs:
             return coll.get(id=id, **kwargs)
         else:


-- 
To view, visit http://gerrit.ovirt.org/32413
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibf922ecf4d3f34a2c200ec7c145f18c15a8efd71
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine-cli
Gerrit-Branch: cli_3.5
Gerrit-Owner: Juan Hernandez <juan.hernan...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to