Verify and convert labels in option parser.

Verify labels, convert label list to label map, and raise errors
if necessary earlier. This allows label verification without cloning
skylab_inventory.

BUG=chromium:840039
TEST='atest host --skylab' with '-l'
CQ-DEPEND=CL:1063165

Change-Id: I7a3d2495e297bcf83d581f438d855ef9a7662c7d
Reviewed-on: https://chromium-review.googlesource.com/1063172
Tested-by: Ningning Xia <nxia@chromium.org>
Commit-Queue: Ningning Xia <nxia@chromium.org>
Reviewed-by: Ningning Xia <nxia@chromium.org>
diff --git a/cli/host.py b/cli/host.py
index a5642e1..6afe415 100644
--- a/cli/host.py
+++ b/cli/host.py
@@ -184,6 +184,7 @@
 
         self.locked = options.locked
         self.unlocked = options.unlocked
+        self.label_map = None
 
         if self.skylab:
             if options.user or options.acl or options.status:
@@ -193,6 +194,9 @@
             if self.full_output and self.hostnames_only:
                 self.invalid_syntax('--full-output is conflicted with '
                                     '--hostnames-only.')
+
+            if self.labels:
+                self.label_map = device.convert_to_label_map(self.labels)
         else:
             if options.full_output:
                 self.invalid_syntax('--full_output is only supported with '
@@ -212,7 +216,7 @@
             lab,
             'duts',
             self.environment,
-            labels=self.labels,
+            label_map=self.label_map,
             hostnames=self.hosts,
             locked=self.locked,
             unlocked=self.unlocked)
@@ -482,13 +486,17 @@
 
         self._parse_lock_options(options)
 
-        if (self.skylab and
-            (options.protection or options.acls or options.alist or
-             options.platform)):
+        self.label_map = None
+        if self.skylab:
             # TODO(nxia): drop these flags when all hosts are migrated to skylab
-            self.invalid_syntax(
-                    '--protection, --acls, --alist or --platform is not '
-                    'supported with --skylab.')
+            if (options.protection or options.acls or options.alist or
+                options.platform):
+                self.invalid_syntax(
+                        '--protection, --acls, --alist or --platform is not '
+                        'supported with --skylab.')
+
+            if self.labels:
+                self.label_map = device.convert_to_label_map(self.labels)
 
         if options.protection:
             self.data['protection'] = options.protection
@@ -658,7 +666,7 @@
                         unlock_lock_id=self.unlock_lock_id,
                         attributes=self.attributes,
                         remove_labels=self.remove_labels,
-                        labels=self.labels)
+                        label_map=self.label_map)
                 successes.append(hostname)
             except device.SkylabDeviceActionError as e:
                 print('Cannot modify host %s: %s' % (hostname, e))