X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=functest%2Futils%2Fopenstack_utils.py;h=e7cdfc8625837fbcbfc4139b503acbb29ae0fe2d;hb=7c5786afbd8b63c7ca71961abc52a2477ea03824;hp=8bd950528393a9cfe059d34670b41f108bbd1c48;hpb=794493f95dc235ada697f9fd7f6d983b48521b4d;p=functest.git diff --git a/functest/utils/openstack_utils.py b/functest/utils/openstack_utils.py index 8bd950528..e7cdfc862 100644 --- a/functest/utils/openstack_utils.py +++ b/functest/utils/openstack_utils.py @@ -82,7 +82,8 @@ def get_env_cred_dict(): 'OS_PROJECT_NAME': 'project_name', 'OS_ENDPOINT_TYPE': 'endpoint_type', 'OS_REGION_NAME': 'region_name', - 'OS_CACERT': 'https_cacert' + 'OS_CACERT': 'https_cacert', + 'OS_INSECURE': 'https_insecure' } return env_cred_dict @@ -150,10 +151,12 @@ def get_credentials_for_rally(): cred_key = env_cred_dict.get('OS_REGION_NAME') rally_conf[cred_key] = region_name - cacert = os.getenv('OS_CACERT') - if cacert is not None: - cred_key = env_cred_dict.get('OS_CACERT') - rally_conf[cred_key] = cacert + cred_key = env_cred_dict.get('OS_CACERT') + rally_conf[cred_key] = os.getenv('OS_CACERT', '') + + insecure_key = env_cred_dict.get('OS_INSECURE') + rally_conf[insecure_key] = os.getenv('OS_INSECURE', '').lower() == 'true' + return rally_conf @@ -181,14 +184,10 @@ def get_endpoint(service_type, endpoint_type='publicURL'): def get_session(other_creds={}): auth = get_session_auth(other_creds) - cacert = os.getenv('OS_CACERT') - if cacert is not None: - if not os.path.isfile(cacert): - raise Exception("The 'OS_CACERT' environment" - "variable is set to %s but the file" - "does not exist.", cacert) - - return session.Session(auth=auth, verify=cacert) + https_cacert = os.getenv('OS_CACERT', '') + https_insecure = os.getenv('OS_INSECURE', '').lower() == 'true' + return session.Session(auth=auth, + verify=(https_cacert or not https_insecure)) # ********************************************* @@ -279,6 +278,22 @@ def get_heat_client(other_creds={}): return heatclient.Client(get_heat_client_version(), session=sess) +def download_and_add_image_on_glance(glance, image_name, image_url, data_dir): + dest_path = data_dir + if not os.path.exists(dest_path): + os.makedirs(dest_path) + file_name = image_url.rsplit('/')[-1] + if not ft_utils.download_url(image_url, dest_path): + return False + + image = create_glance_image( + glance, image_name, dest_path + file_name) + if not image: + return False + + return image + + # ********************************************* # NOVA # ********************************************* @@ -410,12 +425,12 @@ def get_or_create_flavor(flavor_name, ram, disk, vcpus, public=True): return flavor_exists, flavor_id -def get_floating_ips(nova_client): +def get_floating_ips(neutron_client): try: - floating_ips = nova_client.floating_ips.list() - return floating_ips + floating_ips = neutron_client.list_floatingips() + return floating_ips['floatingips'] except Exception as e: - logger.error("Error [get_floating_ips(nova_client)]: %s" % e) + logger.error("Error [get_floating_ips(neutron_client)]: %s" % e) return None @@ -578,12 +593,12 @@ def delete_instance(nova_client, instance_id): return False -def delete_floating_ip(nova_client, floatingip_id): +def delete_floating_ip(neutron_client, floatingip_id): try: - nova_client.floating_ips.delete(floatingip_id) + neutron_client.delete_floatingip(floatingip_id) return True except Exception as e: - logger.error("Error [delete_floating_ip(nova_client, '%s')]: %s" + logger.error("Error [delete_floating_ip(neutron_client, '%s')]: %s" % (floatingip_id, e)) return False @@ -1081,10 +1096,10 @@ def check_security_group_rules(neutron_client, sg_id, direction, protocol, try: security_rules = get_security_group_rules(neutron_client, sg_id) security_rules = [rule for rule in security_rules - if (rule["direction"].lower() == direction - and rule["protocol"].lower() == protocol - and rule["port_range_min"] == port_min - and rule["port_range_max"] == port_max)] + if (rule["direction"].lower() == direction and + rule["protocol"].lower() == protocol and + rule["port_range_min"] == port_min and + rule["port_range_max"] == port_max)] if len(security_rules) == 0: return True else: @@ -1176,9 +1191,9 @@ def delete_security_group(neutron_client, secgroup_id): # ********************************************* # GLANCE # ********************************************* -def get_images(nova_client): +def get_images(glance_client): try: - images = nova_client.images.list() + images = glance_client.images.list() return images except Exception as e: logger.error("Error [get_images]: %s" % e) @@ -1242,12 +1257,12 @@ def get_or_create_image(name, path, format): return image_exists, image_id -def delete_glance_image(nova_client, image_id): +def delete_glance_image(glance_client, image_id): try: - nova_client.images.delete(image_id) + glance_client.images.delete(image_id) return True except Exception as e: - logger.error("Error [delete_glance_image(nova_client, '%s')]: %s" + logger.error("Error [delete_glance_image(glance_client, '%s')]: %s" % (image_id, e)) return False @@ -1412,6 +1427,32 @@ def get_or_create_tenant(keystone_client, tenant_name, tenant_description): return tenant_id +def get_or_create_tenant_for_vnf(keystone_client, tenant_name, + tenant_description): + """Get or Create a Tenant + + Args: + keystone_client: keystone client reference + tenant_name: the name of the tenant + tenant_description: the description of the tenant + + return False if tenant retrieved though get + return True if tenant created + raise Exception if error during processing + """ + try: + tenant_id = get_tenant_id(keystone_client, tenant_name) + if not tenant_id: + tenant_id = create_tenant(keystone_client, tenant_name, + tenant_description) + return True + else: + return False + except: + raise Exception("Impossible to create a Tenant for the VNF {}".format( + tenant_name)) + + def create_user(keystone_client, user_name, user_password, user_email, tenant_id): try: @@ -1444,6 +1485,32 @@ def get_or_create_user(keystone_client, user_name, user_password, return user_id +def get_or_create_user_for_vnf(keystone_client, vnf_ref): + """Get or Create user for VNF + + Args: + keystone_client: keystone client reference + vnf_ref: VNF reference used as user name & password, tenant name + + return False if user retrieved through get + return True if user created + raise Exception if error during processing + """ + try: + user_id = get_user_id(keystone_client, vnf_ref) + tenant_id = get_tenant_id(keystone_client, vnf_ref) + if not user_id: + user_id = create_user(keystone_client, vnf_ref, vnf_ref, + "", tenant_id) + return True + else: + return False + add_role_user(keystone_client, user_id, 'admin', vnf_ref) + except: + raise Exception("Impossible to create a user for the VNF {}".format( + vnf_ref)) + + def add_role_user(keystone_client, user_id, role_id, tenant_id): try: if is_keystone_v3():