Adding running checks for neutron.client

The patch adds waiter check for neutron.client in order
to be sure that neutron services are up and running before
execute neutron.client state.

Change-Id: I858c30b2dad81f6b5ee42bda9b3d3379d4265425
Related-PROD: PROD-24685
diff --git a/_modules/neutronng.py b/_modules/neutronng.py
index 6ea50f9..87bc154 100644
--- a/_modules/neutronng.py
+++ b/_modules/neutronng.py
@@ -1,7 +1,9 @@
 # -*- coding: utf-8 -*-
 
 import logging
+import time
 from functools import wraps
+from salt.exceptions import CommandExecutionError
 LOG = logging.getLogger(__name__)
 
 # Import third party libs
@@ -416,3 +418,22 @@
         salt '*' neutronng.list_extensions
     '''
     return neutron_interface.list_extensions(**kwargs)
+
+
+def wait_for_api_ready(profile, retries=1, retry_timeout=10):
+
+    response = {'status': 'up'}
+    for i in range(1, retries+1):
+        try:
+            list_routers(profile=profile)
+        except Exception as e:
+                msg = ("Error: %s "
+                       "Sleeping for %ss. "
+                       "Attempts %s of %s ")
+                LOG.error(msg % (e, retry_timeout, i, retries))
+                time.sleep(retry_timeout)
+                if i == retries:
+                  raise CommandExecutionError(e)
+                continue
+    return response
+
diff --git a/_modules/neutronv2/__init__.py b/_modules/neutronv2/__init__.py
index 85db2b7..e275af6 100644
--- a/_modules/neutronv2/__init__.py
+++ b/_modules/neutronv2/__init__.py
@@ -12,6 +12,7 @@
 from neutronv2 import subnets
 from neutronv2 import agents
 from neutronv2 import routers
+from neutronv2 import common
 
 
 network_get_details = networks.network_get_details
@@ -62,6 +63,8 @@
 
 wait_for_network_services = agents.wait_for_network_services
 
+wait_for_api_ready = common.wait_for_api_ready
+
 __all__ = (
     'network_get_details', 'network_update', 'network_delete', 'network_list',
     'network_create', 'network_bulk_create', 'subnetpool_get_details',
diff --git a/_modules/neutronv2/common.py b/_modules/neutronv2/common.py
index 7b62122..6f3f56c 100644
--- a/_modules/neutronv2/common.py
+++ b/_modules/neutronv2/common.py
@@ -3,6 +3,8 @@
 import os_client_config
 import time
 
+from salt.exceptions import CommandExecutionError
+
 log = logging.getLogger(__name__)
 
 NEUTRON_VERSION_HEADER = 'x-openstack-networking-version'
@@ -108,3 +110,32 @@
             return resp
         return wrapped_f
     return wrap
+
+def wait_for_api_ready(cloud_name, retries=1, retry_timeout=10, **kwargs):
+
+    adapter = _get_raw_client(cloud_name)
+    response = None
+    for i in range(1, retries+1):
+        try:
+          response = getattr(adapter, 'get')(
+              '/', connect_retries=retries,
+              **kwargs)
+        except Exception as e:
+                msg = ("Error: %s "
+                       "Sleeping for %ss. "
+                       "Attempts %s of %s ")
+                log.error(msg % (e, retry_timeout, i, retries))
+                time.sleep(retry_timeout)
+                continue
+        break
+    if not response or not response.content:
+        if e:
+            raise CommandExecutionError(e)
+        else:
+            return {}
+    try:
+        resp = response.json()
+    except ValueError:
+        resp = response.content
+    return resp
+
diff --git a/neutron/client/resources/init.sls b/neutron/client/resources/init.sls
index 5ec7f0d..8920064 100644
--- a/neutron/client/resources/init.sls
+++ b/neutron/client/resources/init.sls
@@ -11,6 +11,13 @@
 
 {%- for identity_name, identity in client.get('server', {}).iteritems() %}
 
+wait_for_neutron_agents_legacy:
+  module.run:
+    - name: neutronng.wait_for_api_ready
+    - retries: 90
+    - retry_timeout: 1
+    - profile: {{ identity_name }}
+
 {%- if identity.security_group is defined %}
 
 {%- for security_group_name, security_group in identity.security_group.iteritems() %}
@@ -25,6 +32,8 @@
     - tenant: {{ security_group.tenant }}
     {%- if identity.endpoint_type is defined %}
     - endpoint_type: {{ identity.endpoint_type }}
+    - require:
+      - neutronng: wait_for_neutron_agents_legacy
     {%- endif %}
 {%- endfor %}
 
@@ -67,6 +76,8 @@
     {%- if network.is_default is defined %}
     - is_default: {{ network.is_default }}
     {%- endif %}
+    - require:
+      - neutronng.wait_for_api_ready
 
 {%- if network.subnet is defined %}
 
diff --git a/neutron/client/resources/v2.sls b/neutron/client/resources/v2.sls
index 3de104d..b5798cd 100644
--- a/neutron/client/resources/v2.sls
+++ b/neutron/client/resources/v2.sls
@@ -3,6 +3,13 @@
 
 {%- for identity_name, identity in client.get('resources', {}).get('v2', {}).iteritems() %}
 
+wait_for_neutron_agents_v2:
+  module.run:
+    - name: neutronv2.wait_for_api_ready
+    - retries: 90
+    - retry_timeout: 1
+    - cloud_name: {{ identity_name }}
+
   {%- if identity.network is defined %}
   {%- for network_name, network in identity.network.iteritems() %}
 
@@ -34,6 +41,8 @@
     {%- if network.is_default is defined %}
     - is_default: {{ network.is_default }}
     {%- endif %}
+    - require:
+      - wait_for_neutron_agents_v2
 
     {%- if network.subnet is defined %}
     {%- for subnet_name, subnet in network.subnet.iteritems() %}
@@ -107,4 +116,4 @@
   {%- endif %}
 
 {%- endfor %}
-{%- endif %}
\ No newline at end of file
+{%- endif %}