Add test ability to instances_mapped_to_cell

Related-issue: https://mirantis.jira.com/browse/PROD-26861
Change-Id: I5a4898390379f2f99a7c37fc300cb555519774ab
(cherry picked from commit 8a7631f8418fbbebcd68937b1372a99597ad4b5a)
diff --git a/_states/novav21.py b/_states/novav21.py
index b9f9dd0..7b6452b 100644
--- a/_states/novav21.py
+++ b/_states/novav21.py
@@ -361,6 +361,43 @@
     return ret
 
 
+def instances_mapped_to_cell(name, timeout=60, runas='nova'):
+    """Ensure that all instances in the cell are mapped
+
+    :param name: cell name.
+    :param timeout: amount of time in seconds mapping process should finish in.
+    :param runas: username to run the shell commands under.
+    """
+    test = __opts__.get('test', False)
+    cell_uuid = __salt__['cmd.shell'](
+        "nova-manage cell_v2 list_cells 2>/dev/null | "
+        "awk '/%s/ {print $4}'" % name, runas=runas)
+    result = {'name': name, 'changes': {}, 'result': False}
+    if not cell_uuid:
+        result['comment'] = (
+            'Failed to map all instances in cell {0}, it does not exist'
+            .format(name))
+        return result
+    start_time = time.time()
+    if not test:
+        while True:
+            rc = __salt__['cmd.retcode'](
+                'nova-manage cell_v2 map_instances --cell_uuid %s' % cell_uuid,
+                runas=runas)
+            if rc == 0 or time.time() - start_time > timeout:
+                break
+        if rc != 0:
+            result['comment'] = (
+                'Failed to map all instances in cell {0} in {1} seconds'
+                .format(name, timeout))
+            return result
+    result['comment'] = 'All instances mapped in cell {0}'.format(name)
+    if test:
+        result['comment'] = 'TEST: {}'.format(result['comment'])
+    result['result'] = True
+    return result
+
+
 def _db_version_update(db, version, human_readable_resource_name):
     existing_version = __salt__['cmd.shell'](
         'nova-manage %s version 2>/dev/null' % db)