Add ability to ignore deployed machines in wait_for_ready state

Fixes: PROD-33758

Change-Id: I9ada6415ead91fa5e9bc9c0ff7422f61e16ecd03
diff --git a/README.rst b/README.rst
index 0010113..939ad53 100644
--- a/README.rst
+++ b/README.rst
@@ -805,6 +805,21 @@
         - cmd: maas_login_admin
       ...
 
+It is also possible to skip all the machines which are already in
+the "Deployed" state. This is especially useful when you are adding
+new nodes and you don't want to check the already deployed ones.
+This can be enabled by setting in the ``ignore_deployed_machines``
+parameter to ``true`` in reclass.
+
+.. code-block:: bash
+
+    ...
+
+    maas:
+      region:
+        ignore_deployed_machines: true
+      ...
+
 List of available ``req_status`` defined in global variable:
 
 .. code-block:: python
diff --git a/_modules/maas.py b/_modules/maas.py
index cfc0f89..5976d72 100644
--- a/_modules/maas.py
+++ b/_modules/maas.py
@@ -930,8 +930,10 @@
         req_status = kwargs.get("req_status", "Ready")
         to_discover = kwargs.get("machines", None)
         ignore_machines = kwargs.get("ignore_machines", None)
+        ignore_deployed_machines = kwargs.get("ignore_deployed_machines", False)
         attempts = kwargs.get("attempts", 0)
         counter = {}
+        ignored_deployed = []
         if not to_discover:
             try:
                 to_discover = __salt__['config.get']('maas')['region'][
@@ -947,6 +949,9 @@
             for machine in to_discover:
                 for discovered in MachinesStatus.execute()['machines']:
                     if machine == discovered['hostname'] and machine in total:
+                        if ignore_deployed_machines and discovered['status'].lower() == 'deployed':
+                            total.remove(machine)
+                            ignored_deployed.append(machine)
                         if discovered['status'].lower() == req_status.lower():
                             total.remove(machine)
                         elif attempts > 0 and (machine not in counter or counter[machine] < attempts):
@@ -982,8 +987,18 @@
                                 DeployMachines().process()
                                 counter[machine] = 1 if machine not in counter else (counter[machine] + 1)
             if len(total) <= 0:
-                LOG.debug(
-                    "Machines:{} are:{}".format(to_discover, req_status))
+                if len(ignored_deployed) > 0:
+                    for mach in ignored_deployed:
+                        to_discover.remove(mach)
+                    if len(to_discover) > 0:
+                        LOG.debug(
+                        "Machines:{} are:{} and machines:{} were ignored as already deployed.".format(to_discover, req_status, ignored_deployed))
+                    else:
+                        LOG.debug(
+                        "All required machines already exist and were ignored as already deployed:{}".format(ignored_deployed))
+                else:
+                    LOG.debug(
+                        "Machines:{} are:{}".format(to_discover, req_status))
                 return True
             if (timeout - (time.time() - started_at)) <= 0:
                 raise Exception(
diff --git a/maas/machines/wait_for_ready.sls b/maas/machines/wait_for_ready.sls
index 3e8a0f1..dc57e14 100644
--- a/maas/machines/wait_for_ready.sls
+++ b/maas/machines/wait_for_ready.sls
@@ -10,5 +10,6 @@
   - kwargs:
       timeout: {{ region.timeout.ready }}
       attempts: {{ region.timeout.attempts }}
+      ignore_deployed_machines: {{ region.ignore_deployed_machines }}
   - require:
     - cmd: maas_login_admin
diff --git a/maas/map.jinja b/maas/map.jinja
index 9ff5244..350b0e6 100644
--- a/maas/map.jinja
+++ b/maas/map.jinja
@@ -33,6 +33,7 @@
     deployed: 1800
     ready: 900
     attempts: 10
+  ignore_deployed_machines: false
 {%- endload %}
 
 {%- set region = salt['grains.filter_by'](region_defaults, merge=salt['pillar.get']('maas:region', {})) %}
diff --git a/tests/pillar/maas_region.sls b/tests/pillar/maas_region.sls
index e482b61..53e249f 100644
--- a/tests/pillar/maas_region.sls
+++ b/tests/pillar/maas_region.sls
@@ -39,3 +39,4 @@
       deployed: 900
       ready: 900
       attempts: 2
+    ignore_deployed_machines: true