X-Git-Url: https://gerrit.opnfv.org/gerrit/gitweb?a=blobdiff_plain;f=utils%2Fopenstack_utils.py;h=ff9b54a58a3e1e36e5a5df89ed681f805d9bce00;hb=4a803db9fa32c42559306da22beaab437d664fdc;hp=a0de87310d8419181a84e2ee57c0d1d47b515daf;hpb=76a691ab6bba6bf0eca9e04ebb5632647ccf8ba6;p=functest.git diff --git a/utils/openstack_utils.py b/utils/openstack_utils.py old mode 100644 new mode 100755 index a0de87310..ff9b54a58 --- a/utils/openstack_utils.py +++ b/utils/openstack_utils.py @@ -70,6 +70,14 @@ def get_credentials(service): "auth_url": os.environ.get("OS_AUTH_URL"), tenant: os.environ.get("OS_TENANT_NAME") }) + if os.getenv('OS_ENDPOINT_TYPE') is not None: + creds.update({ + "endpoint_type": os.environ.get("OS_ENDPOINT_TYPE") + }) + if os.getenv('OS_REGION_NAME') is not None: + creds.update({ + "region_name": os.environ.get("OS_REGION_NAME") + }) cacert = os.environ.get("OS_CACERT") if cacert is not None: # each openstack client uses differnt kwargs for this @@ -94,6 +102,24 @@ def source_credentials(rc_file): return env +def get_credentials_for_rally(): + creds = get_credentials("keystone") + admin_keys = ['username', 'tenant_name', 'password'] + endpoint_types = [('internalURL', 'internal'), + ('publicURL', 'public'), ('adminURL', 'admin')] + if 'endpoint_type' in creds.keys(): + for k, v in endpoint_types: + if creds['endpoint_type'] == k: + creds['endpoint_type'] = v + rally_conf = {"type": "ExistingCloud", "admin": {}} + for key in creds: + if key in admin_keys: + rally_conf['admin'][key] = creds[key] + else: + rally_conf[key] = creds[key] + return rally_conf + + # ********************************************* # CLIENTS # ********************************************* @@ -109,11 +135,10 @@ def get_nova_client(): def get_cinder_client(): creds_cinder = get_credentials("cinder") - return cinderclient.Client('2', creds_cinder['username'], - creds_cinder['api_key'], - creds_cinder['project_id'], - creds_cinder['auth_url'], - service_type="volume") + creds_cinder.update({ + "service_type": "volume" + }) + return cinderclient.Client('2', **creds_cinder) def get_neutron_client(): @@ -123,8 +148,12 @@ def get_neutron_client(): def get_glance_client(): keystone_client = get_keystone_client() + glance_endpoint_type = 'publicURL' + os_endpoint_type = os.getenv('OS_ENDPOINT_TYPE') + if os_endpoint_type is not None: + glance_endpoint_type = os_endpoint_type glance_endpoint = keystone_client.service_catalog.url_for( - service_type='image', endpoint_type='publicURL') + service_type='image', endpoint_type=glance_endpoint_type) return glanceclient.Client(1, glance_endpoint, token=keystone_client.auth_token) @@ -146,9 +175,8 @@ def get_instance_status(nova_client, instance): try: instance = nova_client.servers.get(instance.id) return instance.status - except: - # logger.error("Error [get_instance_status(nova_client, '%s')]:" % - # str(instance)), e + except Exception, e: + logger.error("Error [get_instance_status(nova_client)]: %s" % e) return None @@ -230,7 +258,7 @@ def create_instance(flavor_name, flavors = nova_client.flavors.list() logger.error("Error: Flavor '%s' not found. Available flavors are: " "\n%s" % (flavor_name, flavors)) - return -1 + return None if fixed_ip is not None: nics = {"net-id": network_id, "v4-fixed-ip": fixed_ip} else: @@ -408,14 +436,14 @@ def get_external_net(neutron_client): for network in neutron_client.list_networks()['networks']: if network['router:external']: return network['name'] - return False + return None def get_external_net_id(neutron_client): for network in neutron_client.list_networks()['networks']: if network['router:external']: return network['id'] - return False + return None def check_neutron_net(neutron_client, net_name): @@ -436,7 +464,7 @@ def create_neutron_net(neutron_client, name): except Exception, e: logger.error("Error [create_neutron_net(neutron_client, '%s')]: %s" % (name, e)) - return False + return None def create_neutron_subnet(neutron_client, name, cidr, net_id): @@ -448,7 +476,7 @@ def create_neutron_subnet(neutron_client, name, cidr, net_id): except Exception, e: logger.error("Error [create_neutron_subnet(neutron_client, '%s', " "'%s', '%s')]: %s" % (name, cidr, net_id, e)) - return False + return None def create_neutron_router(neutron_client, name): @@ -459,7 +487,7 @@ def create_neutron_router(neutron_client, name): except Exception, e: logger.error("Error [create_neutron_router(neutron_client, '%s')]: %s" % (name, e)) - return False + return None def create_neutron_port(neutron_client, name, network_id, ip): @@ -475,7 +503,7 @@ def create_neutron_port(neutron_client, name, network_id, ip): except Exception, e: logger.error("Error [create_neutron_port(neutron_client, '%s', '%s', " "'%s')]: %s" % (name, network_id, ip, e)) - return False + return None def update_neutron_net(neutron_client, network_id, shared=False): @@ -500,7 +528,7 @@ def update_neutron_port(neutron_client, port_id, device_owner): except Exception, e: logger.error("Error [update_neutron_port(neutron_client, '%s', '%s')]:" " %s" % (port_id, device_owner, e)) - return False + return None def add_interface_router(neutron_client, router_id, subnet_id): @@ -588,8 +616,7 @@ def remove_gateway_router(neutron_client, router_id): return False -def create_network_full(logger, - neutron_client, +def create_network_full(neutron_client, net_name, subnet_name, router_name, @@ -615,26 +642,26 @@ def create_network_full(logger, subnet_id = create_neutron_subnet(neutron_client, subnet_name, cidr, network_id) if not subnet_id: - return False + return None logger.debug("Subnet '%s' created successfully" % subnet_id) logger.debug('Creating Router...') router_id = create_neutron_router(neutron_client, router_name) if not router_id: - return False + return None logger.debug("Router '%s' created successfully" % router_id) logger.debug('Adding router to subnet...') if not add_interface_router(neutron_client, router_id, subnet_id): - return False + return None logger.debug("Interface added successfully.") logger.debug('Adding gateway to router...') if not add_gateway_router(neutron_client, router_id): - return False + return None logger.debug("Gateway added successfully.") @@ -644,6 +671,28 @@ def create_network_full(logger, return network_dic +def create_shared_network_full(net_name, subnt_name, router_name, subnet_cidr): + neutron_client = get_neutron_client() + + network_dic = create_network_full(neutron_client, + net_name, + subnt_name, + router_name, + subnet_cidr) + if network_dic: + if not update_neutron_net(neutron_client, + network_dic['net_id'], + shared=True): + logger.error("Failed to update network %s..." % net_name) + return None + else: + logger.debug("Network '%s' is available..." % net_name) + else: + logger.error("Network %s creation failed" % net_name) + return None + return network_dic + + def create_bgpvpn(neutron_client, **kwargs): # route_distinguishers # route_targets @@ -656,6 +705,11 @@ def create_network_association(neutron_client, bgpvpn_id, neutron_network_id): return neutron_client.create_network_association(bgpvpn_id, json_body) +def create_router_association(neutron_client, bgpvpn_id, router_id): + json_body = {"router_association": {"router_id": router_id}} + return neutron_client.create_router_association(bgpvpn_id, json_body) + + def update_bgpvpn(neutron_client, bgpvpn_id, **kwargs): json_body = {"bgpvpn": kwargs} return neutron_client.update_bgpvpn(bgpvpn_id, json_body) @@ -698,7 +752,7 @@ def create_security_group(neutron_client, sg_name, sg_description): except Exception, e: logger.error("Error [create_security_group(neutron_client, '%s', " "'%s')]: %s" % (sg_name, sg_description, e)) - return False + return None def create_secgroup_rule(neutron_client, sg_id, direction, protocol, @@ -736,7 +790,7 @@ def create_secgroup_rule(neutron_client, sg_id, direction, protocol, return False -def create_security_group_full(logger, neutron_client, +def create_security_group_full(neutron_client, sg_name, sg_description): sg_id = get_security_group_id(neutron_client, sg_name) if sg_id != '': @@ -748,7 +802,7 @@ def create_security_group_full(logger, neutron_client, sg_description) if not SECGROUP: logger.error("Failed to create the security group...") - return False + return None sg_id = SECGROUP['id'] @@ -760,19 +814,19 @@ def create_security_group_full(logger, neutron_client, if not create_secgroup_rule(neutron_client, sg_id, 'ingress', 'icmp'): logger.error("Failed to create the security group rule...") - return False + return None logger.debug("Adding SSH rules in security group '%s'..." % sg_name) if not create_secgroup_rule( neutron_client, sg_id, 'ingress', 'tcp', '22', '22'): logger.error("Failed to create the security group rule...") - return False + return None if not create_secgroup_rule( neutron_client, sg_id, 'egress', 'tcp', '22', '22'): logger.error("Failed to create the security group rule...") - return False + return None return sg_id @@ -835,10 +889,10 @@ def get_image_id(glance_client, image_name): def create_glance_image(glance_client, image_name, file_path, disk="qcow2", - container="bare", public=True, logger=None): + container="bare", public=True): if not os.path.isfile(file_path): logger.error("Error: file %s does not exist." % file_path) - return False + return None try: image_id = get_image_id(glance_client, image_name) if image_id != '': @@ -859,7 +913,27 @@ def create_glance_image(glance_client, image_name, file_path, disk="qcow2", except Exception, e: logger.error("Error [create_glance_image(glance_client, '%s', '%s', " "'%s')]: %s" % (image_name, file_path, str(public), e)) - return False + return None + + +def get_or_create_image(name, path, format): + image_exists = False + glance_client = get_glance_client() + + image_id = get_image_id(glance_client, name) + if image_id != '': + logger.info("Using existing image '%s'..." % name) + image_exists = True + else: + logger.info("Creating image '%s' from '%s'..." % (name, path)) + image_id = create_glance_image(glance_client, name, path, format) + if not image_id: + logger.error("Failed to create a Glance image...") + else: + logger.debug("Image '%s' with ID=%s created successfully." + % (name, image_id)) + + return image_exists, image_id def delete_glance_image(nova_client, image_id): @@ -1008,9 +1082,9 @@ def create_tenant(keystone_client, tenant_name, tenant_description): enabled=True) return tenant.id except Exception, e: - logger.error("Error [create_tenant(cinder_client, '%s', '%s')]: %s" + logger.error("Error [create_tenant(keystone_client, '%s', '%s')]: %s" % (tenant_name, tenant_description, e)) - return False + return None def create_user(keystone_client, user_name, user_password, @@ -1024,7 +1098,7 @@ def create_user(keystone_client, user_name, user_password, logger.error("Error [create_user(keystone_client, '%s', '%s', '%s'" "'%s')]: %s" % (user_name, user_password, user_email, tenant_id, e)) - return False + return None def add_role_user(keystone_client, user_id, role_id, tenant_id):