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
+