Change logic for boot-sources import

  * Maas have 'default' switch, in case no boot-source
    configured => switch forcelly to default one.
    Our old flow was:
    delete all sources=> add mirror one => ran import.
    In rare cases, during time 'delete <=> add new' maas engine
    might add default one mirror, which broke mirror logic.
    So, we would try to delete all 'boot_sources_delete_all_others'
    exactly after source add.
  * Misc: add maasng() simple wrapper.

Closes-Bug: PROD-21614 (PROD:21614)

Change-Id: I8c783b5ec12ac046f41453678586a4eebcfcb96b
diff --git a/_modules/maasng.py b/_modules/maasng.py
index 5189597..1c82912 100644
--- a/_modules/maasng.py
+++ b/_modules/maasng.py
@@ -1438,13 +1438,14 @@
     """
     Delete all boot-sources, except defined in 'except_urls' list.
     """
-    result = {}
+    result = {'changes': {}}
     maas_boot_sources = get_boot_source()
     if 0 in [len(except_urls), len(maas_boot_sources)]:
         result['result'] = None
-        result[
-            "comment"] = "Exclude or maas sources for delete empty. No changes goinng to be."
+        result["comment"] = "'except_urls' or 'maas boot-sources' for " \
+                            "delete empty. No changes goinng to be."
         return result
+    # FIXME: fix 'changes' accumulator
     for url in maas_boot_sources.keys():
         if url not in except_urls:
             LOG.info("Removing boot-source:{}".format(url))
@@ -1623,7 +1624,7 @@
     # at least simple retry ;(
     json_res = False
     poll_time = 5
-    for i in range(0, 5):
+    for i in range(0, 10):
         try:
             json_res = json.loads(
                 maas.post(u'api/2.0/boot-sources/{0}/selections/'.format(bs_id), None,
@@ -1632,8 +1633,8 @@
             m = inst.readlines()
             LOG.warning("boot_source_selections "
                         "catch error during processing. Most-probably, "
-                        "streams not imported yet.\nSleep:{}s"
-                        "Retry:{}/5".format(poll_time, i))
+                        "streams data not imported yet.\nSleep:{}s "
+                        "Retry:{}/10".format(poll_time, i))
             LOG.warning("Message:{0}".format(m))
             time.sleep(poll_time)
             continue
diff --git a/_states/maasng.py b/_states/maasng.py
index 0275465..22c68fc 100644
--- a/_states/maasng.py
+++ b/_states/maasng.py
@@ -24,6 +24,23 @@
     return 'maasng'
 
 
+def maasng(funcname, *args, **kwargs):
+    """
+    Simple wrapper, for __salt__ maasng
+    :param funcname:
+    :param args:
+    :param kwargs:
+    :return:
+    """
+    return __salt__['maasng.{}'.format(funcname)](*args, **kwargs)
+
+
+def merge2dicts(d1, d2):
+    z = d1.copy()
+    z.update(d2)
+    return z
+
+
 def disk_layout_present(hostname, layout_type, root_size=None, root_device=None,
                         volume_group=None, volume_name=None, volume_size=None,
                         disk={}, **kwargs):
@@ -391,7 +408,9 @@
     return ret
 
 
-def boot_source_present(url, keyring_file='', keyring_data=''):
+def boot_source_present(url, keyring_file='', keyring_data='',
+                        delete_undefined_sources=False,
+                        delete_undefined_sources_except_urls=[]):
     """
     Process maas boot-sources: link to maas-ephemeral repo
 
@@ -399,6 +418,7 @@
     :param url:               The URL of the BootSource.
     :param keyring_file:      The path to the keyring file for this BootSource.
     :param keyring_data:      The GPG keyring for this BootSource, base64-encoded data.
+    :param delete_undefined_sources:  Delete all boot-sources, except defined in reclass
     """
     ret = {'name': url,
            'changes': {},
@@ -408,16 +428,20 @@
     if __opts__['test']:
         ret['result'] = None
         ret['comment'] = 'boot-source {0} will be updated'.format(url)
-
-    maas_boot_sources = __salt__['maasng.get_boot_source']()
-    # TODO imlpement check and update for keyrings!
+    maas_boot_sources = maasng('get_boot_source')
+    # TODO implement check and update for keyrings!
     if url in maas_boot_sources.keys():
         ret["result"] = True
         ret["comment"] = 'boot-source {0} alredy exist'.format(url)
-        return ret
-    ret["changes"] = __salt__['maasng.create_boot_source'](url,
-                                                           keyring_filename=keyring_file,
-                                                           keyring_data=keyring_data)
+    else:
+        ret["changes"] = maasng('create_boot_source', url,
+                                keyring_filename=keyring_file,
+                                keyring_data=keyring_data)
+    if delete_undefined_sources:
+        ret["changes"] = merge2dicts(ret.get('changes', {}),
+                                     maasng('boot_sources_delete_all_others',
+                                            except_urls=delete_undefined_sources_except_urls))
+        # Re-import data
     return ret
 
 
@@ -447,7 +471,7 @@
         ret['comment'] = 'boot-source {0}' \
                          'selection will be updated'.format(bs_url)
 
-    maas_boot_sources = __salt__['maasng.get_boot_source']()
+    maas_boot_sources = maasng('get_boot_source')
     if bs_url not in maas_boot_sources.keys():
         ret["result"] = False
         ret["comment"] = 'Requested boot-source' \
@@ -455,13 +479,11 @@
                          'to proceed selection for it'.format(bs_url)
         return ret
 
-    ret = __salt__['maasng.create_boot_source_selections'](bs_url,
-                                                           os,
-                                                           release,
-                                                           arches=arches,
-                                                           subarches=subarches,
-                                                           labels=labels,
-                                                           wait=wait)
+    ret = maasng('create_boot_source_selections', bs_url, os, release,
+                 arches=arches,
+                 subarches=subarches,
+                 labels=labels,
+                 wait=wait)
     return ret
 
 
diff --git a/maas/region.sls b/maas/region.sls
index 1226b5d..fd54fb1 100644
--- a/maas/region.sls
+++ b/maas/region.sls
@@ -197,9 +197,6 @@
   - wait: True
   - require:
     - cmd: maas_login_admin
-  {% if region.get('boot_sources_delete_all_others', False)  %}
-    - module: region_boot_sources_delete_all_others
-  {%- endif %}
   - require_in:
     - module: maas_wait_for_racks_import_done
   {%- if grains.get('kitchen-test') %}
@@ -215,20 +212,6 @@
   - onlyif: /bin/false
   {%- endif %}
 
-{##}
-{% if region.get('boot_sources_delete_all_others', False)  %}
-  {# Collect exclude list, all other - will be removed #}
-  {% set exclude_list=[] %}
-  {%- for _, bs in region.boot_sources.iteritems() %} {% if bs.url is defined %} {% do exclude_list.append(bs.url) %} {% endif %} {%- endfor %}
-region_boot_sources_delete_all_others:
-  module.run:
-  - name: maasng.boot_sources_delete_all_others
-  - except_urls: {{ exclude_list }}
-  - require:
-    - cmd: maas_login_admin
-{%- endif %}
-
-{##}
 {% if region.get('boot_sources', False)  %}
   {%- for b_name, b_source in region.boot_sources.iteritems() %}
 maas_region_boot_source_{{ b_name }}:
@@ -240,6 +223,13 @@
   {%- if b_source.keyring_file is defined %}
     - keyring_file: {{ b_source.keyring_file }}
   {%- endif %}
+  {% if region.get('boot_sources_delete_all_others', False)  %}
+    {# Collect exclude list, all other - will be removed #}
+    {% set exclude_list=[] %}
+    {%- for _, bs in region.boot_sources.iteritems() %} {% if bs.url is defined %} {% do exclude_list.append(bs.url) %} {% endif %} {%- endfor %}
+    - delete_undefined_sources: True
+    - delete_undefined_sources_except_urls: {{ exclude_list }}
+  {%- endif %}
     - require:
       - cmd: maas_login_admin
   {%- endfor %}