Replace heat client calls with openstack sdk 03/62703/10
authorStamatis Katsaounis <mokats@intracom-telecom.com>
Mon, 15 Oct 2018 07:18:27 +0000 (10:18 +0300)
committernikoskarandreas <nick@intracom-telecom.com>
Mon, 15 Oct 2018 11:58:45 +0000 (14:58 +0300)
JIRA: SDNVPN-220

This patch replaces all calls of heat client with openstack sdk.

Change-Id: I75571e8de8c06d144204af787ce240c99faea6d3
Signed-off-by: Stamatis Katsaounis <mokats@intracom-telecom.com>
requirements.txt
sdnvpn/lib/openstack_utils.py
sdnvpn/lib/utils.py
sdnvpn/test/functest/testcase_1bis.py

index 1979004..252b214 100644 (file)
@@ -6,8 +6,6 @@ requests # Apache-2.0
 opnfv
 PyYAML # MIT
 networking-bgpvpn>=7.0.0 # Apache-2.0
-python-cinderclient!=4.0.0 # Apache-2.0
-python-heatclient # Apache-2.0
 python-keystoneclient!=2.1.0 # Apache-2.0
 python-neutronclient # Apache-2.0
 xtesting # Apache-2.0
index 3fa17e6..cfacfa8 100644 (file)
@@ -18,19 +18,17 @@ import urllib
 
 from keystoneauth1 import loading
 from keystoneauth1 import session
-from cinderclient import client as cinderclient
-from heatclient import client as heatclient
 from keystoneclient import client as keystoneclient
 from neutronclient.neutron import client as neutronclient
 from openstack import connection
 from openstack import cloud as os_cloud
+from openstack.exceptions import ResourceNotFound
 
 from functest.utils import env
 
 logger = logging.getLogger(__name__)
 
 DEFAULT_API_VERSION = '2'
-DEFAULT_HEAT_API_VERSION = '1'
 
 
 # *********************************************
@@ -165,20 +163,6 @@ def get_keystone_client(other_creds={}):
                                  interface=os.getenv('OS_INTERFACE', 'admin'))
 
 
-def get_cinder_client_version():
-    api_version = os.getenv('OS_VOLUME_API_VERSION')
-    if api_version is not None:
-        logger.info("OS_VOLUME_API_VERSION is set in env as '%s'",
-                    api_version)
-        return api_version
-    return DEFAULT_API_VERSION
-
-
-def get_cinder_client(other_creds={}):
-    sess = get_session(other_creds)
-    return cinderclient.Client(get_cinder_client_version(), session=sess)
-
-
 def get_neutron_client_version():
     api_version = os.getenv('OS_NETWORK_API_VERSION')
     if api_version is not None:
@@ -193,20 +177,6 @@ def get_neutron_client(other_creds={}):
     return neutronclient.Client(get_neutron_client_version(), session=sess)
 
 
-def get_heat_client_version():
-    api_version = os.getenv('OS_ORCHESTRATION_API_VERSION')
-    if api_version is not None:
-        logger.info("OS_ORCHESTRATION_API_VERSION is set in env as '%s'",
-                    api_version)
-        return api_version
-    return DEFAULT_HEAT_API_VERSION
-
-
-def get_heat_client(other_creds={}):
-    sess = get_session(other_creds)
-    return heatclient.Client(get_heat_client_version(), session=sess)
-
-
 def download_url(url, dest_path):
     """
     Download a file to a destination path given a URL
@@ -1434,50 +1404,52 @@ def delete_user(keystone_client, user_id):
 # *********************************************
 #   HEAT
 # *********************************************
-def get_resource(heat_client, stack_id, resource):
+def get_resource(conn, stack_id, resource):
     try:
-        resources = heat_client.resources.get(stack_id, resource)
-        return resources
+        resource = conn.orchestration.resources(stack_id, id=resource).next()
+        return resource
     except Exception as e:
-        logger.error("Error [get_resource]: %s" % e)
+        logger.error("Error [get_resource(orchestration)]: %s" % e)
         return None
 
 
-def create_stack(heat_client, **kwargs):
+def create_stack(conn, **kwargs):
     try:
-        stack = heat_client.stacks.create(**kwargs)
-        stack_id = stack['stack']['id']
+        stack = conn.orchestration.create_stack(**kwargs)
+        stack_id = stack.id
         if stack_id is None:
             logger.error("Stack create start failed")
             raise SystemError("Stack create start failed")
         return stack_id
     except Exception as e:
-        logger.error("Error [create_stack]: %s" % e)
+        logger.error("Error [create_stack(orchestration)]: %s" % e)
         return None
 
 
-def delete_stack(heat_client, stack_id):
+def delete_stack(conn, stack_id):
     try:
-        heat_client.stacks.delete(stack_id)
+        conn.orchestration.delete_stack(stack_id)
         return True
     except Exception as e:
-        logger.error("Error [delete_stack]: %s" % e)
+        logger.error("Error [delete_stack(orchestration)]: %s" % e)
         return False
 
 
-def list_stack(heat_client, **kwargs):
+def list_stacks(conn, **kwargs):
     try:
-        result = heat_client.stacks.list(**kwargs)
+        result = conn.orchestration.stacks(**kwargs)
         return result
     except Exception as e:
-        logger.error("Error [list_stack]: %s" % e)
+        logger.error("Error [list_stack(orchestration)]: %s" % e)
         return None
 
 
-def get_output(heat_client, stack_id, output_key):
+def get_output(conn, stack_id, output_key):
     try:
-        output = heat_client.stacks.output_show(stack_id, output_key)
-        return output
-    except Exception as e:
-        logger.error("Error [get_output]: %s" % e)
-        return None
+        stack = conn.orchestration.get_stack(stack_id)
+        for output in stack.outputs:
+            if output['output_key'] == output_key:
+                return output
+    except ResourceNotFound as e:
+        logger.error("Error [get_output(orchestration)]: %s" % e)
+    return None
index 135501c..a4bb959 100644 (file)
@@ -14,9 +14,8 @@ import time
 import requests
 import re
 import subprocess
-import yaml
 from concurrent.futures import ThreadPoolExecutor
-from openstack.exceptions import ResourceNotFound
+from openstack.exceptions import ResourceNotFound, NotFoundException
 from requests.auth import HTTPBasicAuth
 
 from opnfv.deployment.factory import Factory as DeploymentFactory
@@ -990,7 +989,7 @@ def is_fib_entry_present_on_odl(controllers, ip_prefix, vrf_id):
     return False
 
 
-def wait_stack_for_status(heat_client, stack_id, stack_status, limit=12):
+def wait_stack_for_status(conn, stack_id, stack_status, limit=12):
     """ Waits to reach specified stack status. To be used with
     CREATE_COMPLETE and UPDATE_COMPLETE.
     Will try a specific number of attempts at 10sec intervals
@@ -1005,13 +1004,12 @@ def wait_stack_for_status(heat_client, stack_id, stack_status, limit=12):
     stack_create_complete = False
     attempts = 0
     while attempts < limit:
-        kwargs = {
-            "filters": {
-                "id": stack_id
-            }
-        }
-        stack_st = os_utils.list_stack(
-                            heat_client, **kwargs).next().stack_status
+        try:
+            stack_st = conn.orchestration.get_stack(stack_id).status
+        except NotFoundException:
+            logger.error("Stack create failed")
+            raise SystemError("Stack create failed")
+            return False
         if stack_st == stack_status:
             stack_create_complete = True
             break
@@ -1027,7 +1025,7 @@ def wait_stack_for_status(heat_client, stack_id, stack_status, limit=12):
     return True
 
 
-def delete_stack_and_wait(heat_client, stack_id, limit=12):
+def delete_stack_and_wait(conn, stack_id, limit=12):
     """ Starts and waits for completion of delete stack
 
     Will try a specific number of attempts at 10sec intervals
@@ -1038,7 +1036,7 @@ def delete_stack_and_wait(heat_client, stack_id, limit=12):
     """
     delete_started = False
     if stack_id is not None:
-        delete_started = os_utils.delete_stack(heat_client, stack_id)
+        delete_started = os_utils.delete_stack(conn, stack_id)
 
     if delete_started is True:
         logger.debug("Stack delete succesfully started")
@@ -1048,20 +1046,14 @@ def delete_stack_and_wait(heat_client, stack_id, limit=12):
     stack_delete_complete = False
     attempts = 0
     while attempts < limit:
-        kwargs = {
-            "filters": {
-                "id": stack_id
-            }
-        }
         try:
-            stack_st = os_utils.list_stack(
-                                heat_client, **kwargs).next().stack_status
+            stack_st = conn.orchestration.get_stack(stack_id).status
             if stack_st == 'DELETE_COMPLETE':
                 stack_delete_complete = True
                 break
             attempts += 1
             time.sleep(10)
-        except StopIteration:
+        except NotFoundException:
             stack_delete_complete = True
             break
 
@@ -1090,12 +1082,10 @@ def get_heat_environment(testcase, common_config):
     param_dict = testcase.heat_parameters
     param_dict['flavor'] = fl
     env_dict = {'parameters': param_dict}
-    environment = yaml.safe_dump(env_dict, default_flow_style=False)
-    return environment
+    return env_dict
 
 
-def get_vms_from_stack_outputs(heat_client, conn,
-                               stack_id, vm_stack_output_keys):
+def get_vms_from_stack_outputs(conn, stack_id, vm_stack_output_keys):
     """ Converts a vm name from a heat stack output to a nova vm object
 
     :param stack_id: the id of the stack to fetch the vms from
@@ -1104,9 +1094,11 @@ def get_vms_from_stack_outputs(heat_client, conn,
     """
     vms = []
     for vmk in vm_stack_output_keys:
-        vm_output = os_utils.get_output(heat_client, stack_id, vmk)
-        vm_name = vm_output['output']['output_value']
-        logger.debug("vm '%s' read from heat output" % vm_name)
-        vm = os_utils.get_instance_by_name(conn, vm_name)
-        vms.append(vm)
+        vm_output = os_utils.get_output(conn, stack_id, vmk)
+        if vm_output is not None:
+            vm_name = vm_output['output_value']
+            logger.debug("vm '%s' read from heat output" % vm_name)
+            vm = os_utils.get_instance_by_name(conn, vm_name)
+            if vm is not None:
+                vms.append(vm)
     return vms
index f33d247..c090182 100644 (file)
@@ -33,7 +33,7 @@ def main():
     results.add_to_summary(2, "STATUS", "SUBTEST")
     results.add_to_summary(0, "=")
 
-    heat_client = os_utils.get_heat_client()
+    conn = os_utils.get_os_connection()
     # neutron client is needed as long as bgpvpn heat module
     # is not yet installed by default in apex (APEX-618)
     neutron_client = os_utils.get_neutron_client()
@@ -60,32 +60,26 @@ def main():
         env = test_utils.get_heat_environment(TESTCASE_CONFIG, COMMON_CONFIG)
         logger.debug("Environment is read: '%s'" % env)
 
-        kwargs = {
-            "stack_name": TESTCASE_CONFIG.stack_name,
-            "template": templ,
-            "environment": env,
-            "parameters": {
-                "image_n": TESTCASE_CONFIG.image_name,
-                "av_zone_1": az_1,
-                "av_zone_2": az_2
-            }
-        }
-        stack_id = os_utils.create_stack(heat_client, **kwargs)
+        env['name'] = TESTCASE_CONFIG.stack_name
+        env['template'] = templ
+        env['parameters']['image_n'] = TESTCASE_CONFIG.image_name
+        env['parameters']['av_zone_1'] = az_1
+        env['parameters']['av_zone_2'] = az_2
+
+        stack_id = os_utils.create_stack(conn, **env)
         if stack_id is None:
             logger.error("Stack create start failed")
             raise SystemError("Stack create start failed")
 
-        test_utils.wait_stack_for_status(heat_client,
-                                         stack_id, 'CREATE_COMPLETE')
+        test_utils.wait_stack_for_status(conn, stack_id, 'CREATE_COMPLETE')
 
-        net_1_output = os_utils.get_output(heat_client, stack_id, 'net_1_o')
-        network_1_id = net_1_output['output']['output_value']
-        net_2_output = os_utils.get_output(heat_client, stack_id, 'net_2_o')
-        network_2_id = net_2_output['output']['output_value']
+        net_1_output = os_utils.get_output(conn, stack_id, 'net_1_o')
+        network_1_id = net_1_output['output_value']
+        net_2_output = os_utils.get_output(conn, stack_id, 'net_2_o')
+        network_2_id = net_2_output['output_value']
 
         vm_stack_output_keys = ['vm1_o', 'vm2_o', 'vm3_o', 'vm4_o', 'vm5_o']
-        vms = test_utils.get_vms_from_stack_outputs(heat_client,
-                                                    conn,
+        vms = test_utils.get_vms_from_stack_outputs(conn,
                                                     stack_id,
                                                     vm_stack_output_keys)
 
@@ -203,7 +197,7 @@ def main():
                                    [], [], [], [])
 
         try:
-            test_utils.delete_stack_and_wait(heat_client, stack_id)
+            test_utils.delete_stack_and_wait(conn, stack_id)
         except Exception as e:
             logger.error(
                    "exception occurred while executing testcase_1bis: %s", e)