3 # jose.lausuch@ericsson.com
4 # valentin.boucher@orange.com
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
17 from cinderclient import client as cinderclient
18 import functest.utils.functest_logger as ft_logger
19 import functest.utils.functest_utils as ft_utils
20 from glanceclient import client as glanceclient
21 from keystoneclient.v2_0 import client as keystoneclient
22 from neutronclient.v2_0 import client as neutronclient
23 from novaclient import client as novaclient
25 logger = ft_logger.Logger("openstack_utils").getLogger()
28 # *********************************************
30 # *********************************************
31 class MissingEnvVar(Exception):
33 def __init__(self, var):
37 return str.format("Please set the mandatory env var: {}", self.var)
40 def check_credentials():
42 Check if the OpenStack credentials (openrc) are sourced
44 env_vars = ['OS_AUTH_URL', 'OS_USERNAME', 'OS_PASSWORD', 'OS_TENANT_NAME']
45 return all(map(lambda v: v in os.environ and os.environ[v], env_vars))
48 def get_credentials(service):
49 """Returns a creds dictionary filled with the following keys:
51 * password/api_key (depending on the service)
52 * tenant_name/project_id (depending on the service)
54 :param service: a string indicating the name of the service
55 requesting the credentials.
59 keystone_api_version = os.getenv('OS_IDENTITY_API_VERSION')
60 if (keystone_api_version is None or
61 keystone_api_version == '2'):
63 tenant_env = 'OS_TENANT_NAME'
64 tenant = 'tenant_name'
67 tenant_env = 'OS_PROJECT_NAME'
68 tenant = 'project_name'
70 # Check that the env vars exists:
71 envvars = ('OS_USERNAME', 'OS_PASSWORD', 'OS_AUTH_URL', tenant_env)
72 for envvar in envvars:
73 if os.getenv(envvar) is None:
74 raise MissingEnvVar(envvar)
76 # Unfortunately, each of the OpenStack client will request slightly
77 # different entries in their credentials dict.
78 if service.lower() in ("nova", "cinder"):
84 # The most common way to pass these info to the script is to do it through
85 # environment variables.
87 "username": os.environ.get("OS_USERNAME"),
88 password: os.environ.get("OS_PASSWORD"),
89 "auth_url": os.environ.get("OS_AUTH_URL"),
90 tenant: os.environ.get(tenant_env)
93 if os.getenv('OS_USER_DOMAIN_NAME') is not None:
95 "user_domain_name": os.getenv('OS_USER_DOMAIN_NAME')
97 if os.getenv('OS_PROJECT_DOMAIN_NAME') is not None:
99 "project_domain_name": os.getenv('OS_PROJECT_DOMAIN_NAME')
102 if os.getenv('OS_ENDPOINT_TYPE') is not None:
104 "endpoint_type": os.environ.get("OS_ENDPOINT_TYPE")
106 if os.getenv('OS_REGION_NAME') is not None:
108 "region_name": os.environ.get("OS_REGION_NAME")
110 cacert = os.environ.get("OS_CACERT")
111 if cacert is not None:
112 # each openstack client uses differnt kwargs for this
113 creds.update({"cacert": cacert,
115 "https_ca_cert": cacert,
116 "https_cacert": cacert,
118 creds.update({"insecure": "True", "https_insecure": "True"})
119 if not os.path.isfile(cacert):
120 logger.info("WARNING: The 'OS_CACERT' environment variable is "
121 "set to %s but the file does not exist." % cacert)
125 def source_credentials(rc_file):
126 pipe = subprocess.Popen(". %s; env" % rc_file, stdout=subprocess.PIPE,
128 output = pipe.communicate()[0]
129 env = dict((line.split("=", 1) for line in output.splitlines()))
130 os.environ.update(env)
134 def get_credentials_for_rally():
135 creds = get_credentials("keystone")
136 keystone_api_version = os.getenv('OS_IDENTITY_API_VERSION')
137 if (keystone_api_version is None or
138 keystone_api_version == '2'):
139 admin_keys = ['username', 'tenant_name', 'password']
141 admin_keys = ['username', 'password', 'user_domain_name',
142 'project_name', 'project_domain_name']
144 endpoint_types = [('internalURL', 'internal'),
145 ('publicURL', 'public'), ('adminURL', 'admin')]
146 if 'endpoint_type' in creds.keys():
147 for k, v in endpoint_types:
148 if creds['endpoint_type'] == k:
149 creds['endpoint_type'] = v
150 rally_conf = {"type": "ExistingCloud", "admin": {}}
152 if key in admin_keys:
153 rally_conf['admin'][key] = creds[key]
155 rally_conf[key] = creds[key]
159 # *********************************************
161 # *********************************************
162 def get_keystone_client():
163 creds_keystone = get_credentials("keystone")
164 return keystoneclient.Client(**creds_keystone)
167 def get_nova_client():
168 creds_nova = get_credentials("nova")
169 return novaclient.Client('2', **creds_nova)
172 def get_cinder_client():
173 creds_cinder = get_credentials("cinder")
174 creds_cinder.update({
175 "service_type": "volume"
177 return cinderclient.Client('2', **creds_cinder)
180 def get_neutron_client():
181 creds_neutron = get_credentials("neutron")
182 return neutronclient.Client(**creds_neutron)
185 def get_glance_client():
186 keystone_client = get_keystone_client()
187 glance_endpoint_type = 'publicURL'
188 os_endpoint_type = os.getenv('OS_ENDPOINT_TYPE')
189 if os_endpoint_type is not None:
190 glance_endpoint_type = os_endpoint_type
191 glance_endpoint = keystone_client.service_catalog.url_for(
192 service_type='image', endpoint_type=glance_endpoint_type)
193 return glanceclient.Client(1, glance_endpoint,
194 token=keystone_client.auth_token)
197 # *********************************************
199 # *********************************************
200 def get_instances(nova_client):
202 instances = nova_client.servers.list(search_opts={'all_tenants': 1})
205 logger.error("Error [get_instances(nova_client)]: %s" % e)
209 def get_instance_status(nova_client, instance):
211 instance = nova_client.servers.get(instance.id)
212 return instance.status
214 logger.error("Error [get_instance_status(nova_client)]: %s" % e)
218 def get_instance_by_name(nova_client, instance_name):
220 instance = nova_client.servers.find(name=instance_name)
223 logger.error("Error [get_instance_by_name(nova_client, '%s')]: %s"
224 % (instance_name, e))
228 def get_flavor_id(nova_client, flavor_name):
229 flavors = nova_client.flavors.list(detailed=True)
232 if f.name == flavor_name:
238 def get_flavor_id_by_ram_range(nova_client, min_ram, max_ram):
239 flavors = nova_client.flavors.list(detailed=True)
242 if min_ram <= f.ram and f.ram <= max_ram:
248 def get_aggregates(nova_client):
250 aggregates = nova_client.aggregates.list()
253 logger.error("Error [get_aggregates(nova_client)]: %s" % e)
257 def get_aggregate_id(nova_client, aggregate_name):
259 aggregates = get_aggregates(nova_client)
260 _id = [ag.id for ag in aggregates if ag.name == aggregate_name][0]
263 logger.error("Error [get_aggregate_id(nova_client, %s)]:"
264 " %s" % (aggregate_name, e))
268 def get_availability_zones(nova_client):
270 availability_zones = nova_client.availability_zones.list()
271 return availability_zones
273 logger.error("Error [get_availability_zones(nova_client)]: %s" % e)
277 def get_availability_zone_names(nova_client):
279 az_names = [az.zoneName for az in get_availability_zones(nova_client)]
282 logger.error("Error [get_availability_zone_names(nova_client)]:"
287 def create_flavor(nova_client, flavor_name, ram, disk, vcpus, public=True):
289 flavor = nova_client.flavors.create(
290 flavor_name, ram, vcpus, disk, is_public=public)
292 extra_specs = ft_utils.get_functest_config(
293 'general.flavor_extra_specs')
294 flavor.set_keys(extra_specs)
296 # flavor extra specs are not configured, therefore skip the update
300 logger.error("Error [create_flavor(nova_client, '%s', '%s', '%s', "
301 "'%s')]: %s" % (flavor_name, ram, disk, vcpus, e))
306 def get_or_create_flavor(flavor_name, ram, disk, vcpus, public=True):
307 flavor_exists = False
308 nova_client = get_nova_client()
310 flavor_id = get_flavor_id(nova_client, flavor_name)
312 logger.info("Using existing flavor '%s'..." % flavor_name)
315 logger.info("Creating flavor '%s' with '%s' RAM, '%s' disk size, "
316 "'%s' vcpus..." % (flavor_name, ram, disk, vcpus))
317 flavor_id = create_flavor(
318 nova_client, flavor_name, ram, disk, vcpus, public=public)
320 logger.error("Failed to create flavor '%s'..." % (flavor_name))
322 logger.debug("Flavor '%s' with ID=%s created successfully."
323 % (flavor_name, flavor_id))
325 return flavor_exists, flavor_id
328 def get_floating_ips(nova_client):
330 floating_ips = nova_client.floating_ips.list()
333 logger.error("Error [get_floating_ips(nova_client)]: %s" % e)
337 def get_hypervisors(nova_client):
340 hypervisors = nova_client.hypervisors.list()
341 for hypervisor in hypervisors:
342 if hypervisor.state == "up":
343 nodes.append(hypervisor.hypervisor_hostname)
346 logger.error("Error [get_hypervisors(nova_client)]: %s" % e)
350 def create_aggregate(nova_client, aggregate_name, av_zone):
352 nova_client.aggregates.create(aggregate_name, av_zone)
355 logger.error("Error [create_aggregate(nova_client, %s, %s)]: %s"
356 % (aggregate_name, av_zone, e))
360 def add_host_to_aggregate(nova_client, aggregate_name, compute_host):
362 aggregate_id = get_aggregate_id(nova_client, aggregate_name)
363 nova_client.aggregates.add_host(aggregate_id, compute_host)
366 logger.error("Error [add_host_to_aggregate(nova_client, %s, %s)]: %s"
367 % (aggregate_name, compute_host, e))
371 def create_aggregate_with_host(
372 nova_client, aggregate_name, av_zone, compute_host):
374 create_aggregate(nova_client, aggregate_name, av_zone)
375 add_host_to_aggregate(nova_client, aggregate_name, compute_host)
378 logger.error("Error [create_aggregate_with_host("
379 "nova_client, %s, %s, %s)]: %s"
380 % (aggregate_name, av_zone, compute_host, e))
384 def create_instance(flavor_name,
387 instance_name="functest-vm",
393 nova_client = get_nova_client()
395 flavor = nova_client.flavors.find(name=flavor_name)
397 flavors = nova_client.flavors.list()
398 logger.error("Error: Flavor '%s' not found. Available flavors are: "
399 "\n%s" % (flavor_name, flavors))
401 if fixed_ip is not None:
402 nics = {"net-id": network_id, "v4-fixed-ip": fixed_ip}
404 nics = {"net-id": network_id}
406 instance = nova_client.servers.create(
411 availability_zone=av_zone,
415 instance = nova_client.servers.create(
420 config_drive=confdrive,
422 availability_zone=av_zone,
428 def create_instance_and_wait_for_active(flavor_name,
438 VM_BOOT_TIMEOUT = 180
439 nova_client = get_nova_client()
440 instance = create_instance(flavor_name,
449 count = VM_BOOT_TIMEOUT / SLEEP
450 for n in range(count, -1, -1):
451 status = get_instance_status(nova_client, instance)
452 if status.lower() == "active":
454 elif status.lower() == "error":
455 logger.error("The instance %s went to ERROR status."
459 logger.error("Timeout booting the instance %s." % instance_name)
463 def create_floating_ip(neutron_client):
464 extnet_id = get_external_net_id(neutron_client)
465 props = {'floating_network_id': extnet_id}
467 ip_json = neutron_client.create_floatingip({'floatingip': props})
468 fip_addr = ip_json['floatingip']['floating_ip_address']
469 fip_id = ip_json['floatingip']['id']
471 logger.error("Error [create_floating_ip(neutron_client)]: %s" % e)
473 return {'fip_addr': fip_addr, 'fip_id': fip_id}
476 def add_floating_ip(nova_client, server_id, floatingip_addr):
478 nova_client.servers.add_floating_ip(server_id, floatingip_addr)
481 logger.error("Error [add_floating_ip(nova_client, '%s', '%s')]: %s"
482 % (server_id, floatingip_addr, e))
486 def delete_instance(nova_client, instance_id):
488 nova_client.servers.force_delete(instance_id)
491 logger.error("Error [delete_instance(nova_client, '%s')]: %s"
496 def delete_floating_ip(nova_client, floatingip_id):
498 nova_client.floating_ips.delete(floatingip_id)
501 logger.error("Error [delete_floating_ip(nova_client, '%s')]: %s"
502 % (floatingip_id, e))
506 def remove_host_from_aggregate(nova_client, aggregate_name, compute_host):
508 aggregate_id = get_aggregate_id(nova_client, aggregate_name)
509 nova_client.aggregates.remove_host(aggregate_id, compute_host)
512 logger.error("Error [remove_host_from_aggregate(nova_client, %s, %s)]:"
513 " %s" % (aggregate_name, compute_host, e))
517 def remove_hosts_from_aggregate(nova_client, aggregate_name):
518 aggregate_id = get_aggregate_id(nova_client, aggregate_name)
519 hosts = nova_client.aggregates.get(aggregate_id).hosts
521 all(remove_host_from_aggregate(nova_client, aggregate_name, host)
525 def delete_aggregate(nova_client, aggregate_name):
527 remove_hosts_from_aggregate(nova_client, aggregate_name)
528 nova_client.aggregates.delete(aggregate_name)
531 logger.error("Error [delete_aggregate(nova_client, %s)]: %s"
532 % (aggregate_name, e))
536 # *********************************************
538 # *********************************************
539 def get_network_list(neutron_client):
540 network_list = neutron_client.list_networks()['networks']
541 if len(network_list) == 0:
547 def get_router_list(neutron_client):
548 router_list = neutron_client.list_routers()['routers']
549 if len(router_list) == 0:
555 def get_port_list(neutron_client):
556 port_list = neutron_client.list_ports()['ports']
557 if len(port_list) == 0:
563 def get_network_id(neutron_client, network_name):
564 networks = neutron_client.list_networks()['networks']
567 if n['name'] == network_name:
573 def get_subnet_id(neutron_client, subnet_name):
574 subnets = neutron_client.list_subnets()['subnets']
577 if s['name'] == subnet_name:
583 def get_router_id(neutron_client, router_name):
584 routers = neutron_client.list_routers()['routers']
587 if r['name'] == router_name:
593 def get_private_net(neutron_client):
594 # Checks if there is an existing shared private network
595 networks = neutron_client.list_networks()['networks']
596 if len(networks) == 0:
599 if (net['router:external'] is False) and (net['shared'] is True):
604 def get_external_net(neutron_client):
605 for network in neutron_client.list_networks()['networks']:
606 if network['router:external']:
607 return network['name']
611 def get_external_net_id(neutron_client):
612 for network in neutron_client.list_networks()['networks']:
613 if network['router:external']:
618 def check_neutron_net(neutron_client, net_name):
619 for network in neutron_client.list_networks()['networks']:
620 if network['name'] == net_name:
621 for subnet in network['subnets']:
626 def create_neutron_net(neutron_client, name):
627 json_body = {'network': {'name': name,
628 'admin_state_up': True}}
630 network = neutron_client.create_network(body=json_body)
631 network_dict = network['network']
632 return network_dict['id']
634 logger.error("Error [create_neutron_net(neutron_client, '%s')]: %s"
639 def create_neutron_subnet(neutron_client, name, cidr, net_id):
640 json_body = {'subnets': [{'name': name, 'cidr': cidr,
641 'ip_version': 4, 'network_id': net_id}]}
643 subnet = neutron_client.create_subnet(body=json_body)
644 return subnet['subnets'][0]['id']
646 logger.error("Error [create_neutron_subnet(neutron_client, '%s', "
647 "'%s', '%s')]: %s" % (name, cidr, net_id, e))
651 def create_neutron_router(neutron_client, name):
652 json_body = {'router': {'name': name, 'admin_state_up': True}}
654 router = neutron_client.create_router(json_body)
655 return router['router']['id']
657 logger.error("Error [create_neutron_router(neutron_client, '%s')]: %s"
662 def create_neutron_port(neutron_client, name, network_id, ip):
663 json_body = {'port': {
664 'admin_state_up': True,
666 'network_id': network_id,
667 'fixed_ips': [{"ip_address": ip}]
670 port = neutron_client.create_port(body=json_body)
671 return port['port']['id']
673 logger.error("Error [create_neutron_port(neutron_client, '%s', '%s', "
674 "'%s')]: %s" % (name, network_id, ip, e))
678 def update_neutron_net(neutron_client, network_id, shared=False):
679 json_body = {'network': {'shared': shared}}
681 neutron_client.update_network(network_id, body=json_body)
684 logger.error("Error [update_neutron_net(neutron_client, '%s', '%s')]: "
685 "%s" % (network_id, str(shared), e))
689 def update_neutron_port(neutron_client, port_id, device_owner):
690 json_body = {'port': {
691 'device_owner': device_owner,
694 port = neutron_client.update_port(port=port_id,
696 return port['port']['id']
698 logger.error("Error [update_neutron_port(neutron_client, '%s', '%s')]:"
699 " %s" % (port_id, device_owner, e))
703 def add_interface_router(neutron_client, router_id, subnet_id):
704 json_body = {"subnet_id": subnet_id}
706 neutron_client.add_interface_router(router=router_id, body=json_body)
709 logger.error("Error [add_interface_router(neutron_client, '%s', "
710 "'%s')]: %s" % (router_id, subnet_id, e))
714 def add_gateway_router(neutron_client, router_id):
715 ext_net_id = get_external_net_id(neutron_client)
716 router_dict = {'network_id': ext_net_id}
718 neutron_client.add_gateway_router(router_id, router_dict)
721 logger.error("Error [add_gateway_router(neutron_client, '%s')]: %s"
726 def delete_neutron_net(neutron_client, network_id):
728 neutron_client.delete_network(network_id)
731 logger.error("Error [delete_neutron_net(neutron_client, '%s')]: %s"
736 def delete_neutron_subnet(neutron_client, subnet_id):
738 neutron_client.delete_subnet(subnet_id)
741 logger.error("Error [delete_neutron_subnet(neutron_client, '%s')]: %s"
746 def delete_neutron_router(neutron_client, router_id):
748 neutron_client.delete_router(router=router_id)
751 logger.error("Error [delete_neutron_router(neutron_client, '%s')]: %s"
756 def delete_neutron_port(neutron_client, port_id):
758 neutron_client.delete_port(port_id)
761 logger.error("Error [delete_neutron_port(neutron_client, '%s')]: %s"
766 def remove_interface_router(neutron_client, router_id, subnet_id):
767 json_body = {"subnet_id": subnet_id}
769 neutron_client.remove_interface_router(router=router_id,
773 logger.error("Error [remove_interface_router(neutron_client, '%s', "
774 "'%s')]: %s" % (router_id, subnet_id, e))
778 def remove_gateway_router(neutron_client, router_id):
780 neutron_client.remove_gateway_router(router_id)
783 logger.error("Error [remove_gateway_router(neutron_client, '%s')]: %s"
788 def create_network_full(neutron_client,
794 # Check if the network already exists
795 network_id = get_network_id(neutron_client, net_name)
796 subnet_id = get_subnet_id(neutron_client, subnet_name)
797 router_id = get_router_id(neutron_client, router_name)
799 if network_id != '' and subnet_id != '' and router_id != '':
800 logger.info("A network with name '%s' already exists..." % net_name)
802 neutron_client.format = 'json'
803 logger.info('Creating neutron network %s...' % net_name)
804 network_id = create_neutron_net(neutron_client, net_name)
809 logger.debug("Network '%s' created successfully" % network_id)
810 logger.debug('Creating Subnet....')
811 subnet_id = create_neutron_subnet(neutron_client, subnet_name,
816 logger.debug("Subnet '%s' created successfully" % subnet_id)
817 logger.debug('Creating Router...')
818 router_id = create_neutron_router(neutron_client, router_name)
823 logger.debug("Router '%s' created successfully" % router_id)
824 logger.debug('Adding router to subnet...')
826 if not add_interface_router(neutron_client, router_id, subnet_id):
829 logger.debug("Interface added successfully.")
831 logger.debug('Adding gateway to router...')
832 if not add_gateway_router(neutron_client, router_id):
835 logger.debug("Gateway added successfully.")
837 network_dic = {'net_id': network_id,
838 'subnet_id': subnet_id,
839 'router_id': router_id}
843 def create_shared_network_full(net_name, subnt_name, router_name, subnet_cidr):
844 neutron_client = get_neutron_client()
846 network_dic = create_network_full(neutron_client,
852 if not update_neutron_net(neutron_client,
853 network_dic['net_id'],
855 logger.error("Failed to update network %s..." % net_name)
858 logger.debug("Network '%s' is available..." % net_name)
860 logger.error("Network %s creation failed" % net_name)
865 def create_bgpvpn(neutron_client, **kwargs):
866 # route_distinguishers
868 json_body = {"bgpvpn": kwargs}
869 return neutron_client.create_bgpvpn(json_body)
872 def create_network_association(neutron_client, bgpvpn_id, neutron_network_id):
873 json_body = {"network_association": {"network_id": neutron_network_id}}
874 return neutron_client.create_network_association(bgpvpn_id, json_body)
877 def create_router_association(neutron_client, bgpvpn_id, router_id):
878 json_body = {"router_association": {"router_id": router_id}}
879 return neutron_client.create_router_association(bgpvpn_id, json_body)
882 def update_bgpvpn(neutron_client, bgpvpn_id, **kwargs):
883 json_body = {"bgpvpn": kwargs}
884 return neutron_client.update_bgpvpn(bgpvpn_id, json_body)
887 def delete_bgpvpn(neutron_client, bgpvpn_id):
888 return neutron_client.delete_bgpvpn(bgpvpn_id)
891 def get_bgpvpn(neutron_client, bgpvpn_id):
892 return neutron_client.show_bgpvpn(bgpvpn_id)
895 def get_bgpvpn_routers(neutron_client, bgpvpn_id):
896 return get_bgpvpn(neutron_client, bgpvpn_id)['bgpvpn']['routers']
899 def get_bgpvpn_networks(neutron_client, bgpvpn_id):
900 return get_bgpvpn(neutron_client, bgpvpn_id)['bgpvpn']['networks']
902 # *********************************************
904 # *********************************************
907 def get_security_groups(neutron_client):
909 security_groups = neutron_client.list_security_groups()[
911 return security_groups
913 logger.error("Error [get_security_groups(neutron_client)]: %s" % e)
917 def get_security_group_id(neutron_client, sg_name):
918 security_groups = get_security_groups(neutron_client)
920 for sg in security_groups:
921 if sg['name'] == sg_name:
927 def create_security_group(neutron_client, sg_name, sg_description):
928 json_body = {'security_group': {'name': sg_name,
929 'description': sg_description}}
931 secgroup = neutron_client.create_security_group(json_body)
932 return secgroup['security_group']
934 logger.error("Error [create_security_group(neutron_client, '%s', "
935 "'%s')]: %s" % (sg_name, sg_description, e))
939 def create_secgroup_rule(neutron_client, sg_id, direction, protocol,
940 port_range_min=None, port_range_max=None):
941 if port_range_min is None and port_range_max is None:
942 json_body = {'security_group_rule': {'direction': direction,
943 'security_group_id': sg_id,
944 'protocol': protocol}}
945 elif port_range_min is not None and port_range_max is not None:
946 json_body = {'security_group_rule': {'direction': direction,
947 'security_group_id': sg_id,
948 'port_range_min': port_range_min,
949 'port_range_max': port_range_max,
950 'protocol': protocol}}
952 logger.error("Error [create_secgroup_rule(neutron_client, '%s', '%s', "
953 "'%s', '%s', '%s', '%s')]:" % (neutron_client,
958 " Invalid values for port_range_min, port_range_max")
961 neutron_client.create_security_group_rule(json_body)
964 logger.error("Error [create_secgroup_rule(neutron_client, '%s', '%s', "
965 "'%s', '%s', '%s', '%s')]: %s" % (neutron_client,
974 def create_security_group_full(neutron_client,
975 sg_name, sg_description):
976 sg_id = get_security_group_id(neutron_client, sg_name)
978 logger.info("Using existing security group '%s'..." % sg_name)
980 logger.info("Creating security group '%s'..." % sg_name)
981 SECGROUP = create_security_group(neutron_client,
985 logger.error("Failed to create the security group...")
988 sg_id = SECGROUP['id']
990 logger.debug("Security group '%s' with ID=%s created successfully."
991 % (SECGROUP['name'], sg_id))
993 logger.debug("Adding ICMP rules in security group '%s'..."
995 if not create_secgroup_rule(neutron_client, sg_id,
997 logger.error("Failed to create the security group rule...")
1000 logger.debug("Adding SSH rules in security group '%s'..."
1002 if not create_secgroup_rule(
1003 neutron_client, sg_id, 'ingress', 'tcp', '22', '22'):
1004 logger.error("Failed to create the security group rule...")
1007 if not create_secgroup_rule(
1008 neutron_client, sg_id, 'egress', 'tcp', '22', '22'):
1009 logger.error("Failed to create the security group rule...")
1014 def add_secgroup_to_instance(nova_client, instance_id, secgroup_id):
1016 nova_client.servers.add_security_group(instance_id, secgroup_id)
1018 except Exception, e:
1019 logger.error("Error [add_secgroup_to_instance(nova_client, '%s', "
1020 "'%s')]: %s" % (instance_id, secgroup_id, e))
1024 def update_sg_quota(neutron_client, tenant_id, sg_quota, sg_rule_quota):
1025 json_body = {"quota": {
1026 "security_group": sg_quota,
1027 "security_group_rule": sg_rule_quota
1031 neutron_client.update_quota(tenant_id=tenant_id,
1034 except Exception, e:
1035 logger.error("Error [update_sg_quota(neutron_client, '%s', '%s', "
1036 "'%s')]: %s" % (tenant_id, sg_quota, sg_rule_quota, e))
1040 def delete_security_group(neutron_client, secgroup_id):
1042 neutron_client.delete_security_group(secgroup_id)
1044 except Exception, e:
1045 logger.error("Error [delete_security_group(neutron_client, '%s')]: %s"
1050 # *********************************************
1052 # *********************************************
1053 def get_images(nova_client):
1055 images = nova_client.images.list()
1057 except Exception, e:
1058 logger.error("Error [get_images]: %s" % e)
1062 def get_image_id(glance_client, image_name):
1063 images = glance_client.images.list()
1066 if i.name == image_name:
1072 def create_glance_image(glance_client, image_name, file_path, disk="qcow2",
1073 container="bare", public=True):
1074 if not os.path.isfile(file_path):
1075 logger.error("Error: file %s does not exist." % file_path)
1078 image_id = get_image_id(glance_client, image_name)
1081 logger.info("Image %s already exists." % image_name)
1084 logger.info("Creating image '%s' from '%s'..." % (image_name,
1087 properties = ft_utils.get_functest_config(
1088 'general.image_properties')
1090 # image properties are not configured
1091 # therefore don't add any properties
1093 with open(file_path) as fimage:
1094 image = glance_client.images.create(name=image_name,
1097 container_format=container,
1098 properties=properties,
1102 except Exception, e:
1103 logger.error("Error [create_glance_image(glance_client, '%s', '%s', "
1104 "'%s')]: %s" % (image_name, file_path, str(public), e))
1108 def get_or_create_image(name, path, format):
1109 image_exists = False
1110 glance_client = get_glance_client()
1112 image_id = get_image_id(glance_client, name)
1114 logger.info("Using existing image '%s'..." % name)
1117 logger.info("Creating image '%s' from '%s'..." % (name, path))
1118 image_id = create_glance_image(glance_client, name, path, format)
1120 logger.error("Failed to create a Glance image...")
1122 logger.debug("Image '%s' with ID=%s created successfully."
1125 return image_exists, image_id
1128 def delete_glance_image(nova_client, image_id):
1130 nova_client.images.delete(image_id)
1132 except Exception, e:
1133 logger.error("Error [delete_glance_image(nova_client, '%s')]: %s"
1138 # *********************************************
1140 # *********************************************
1141 def get_volumes(cinder_client):
1143 volumes = cinder_client.volumes.list(search_opts={'all_tenants': 1})
1145 except Exception, e:
1146 logger.error("Error [get_volumes(cinder_client)]: %s" % e)
1150 def list_volume_types(cinder_client, public=True, private=True):
1152 volume_types = cinder_client.volume_types.list()
1154 volume_types = [vt for vt in volume_types if not vt.is_public]
1156 volume_types = [vt for vt in volume_types if vt.is_public]
1158 except Exception, e:
1159 logger.error("Error [list_volume_types(cinder_client)]: %s" % e)
1163 def create_volume_type(cinder_client, name):
1165 volume_type = cinder_client.volume_types.create(name)
1167 except Exception, e:
1168 logger.error("Error [create_volume_type(cinder_client, '%s')]: %s"
1173 def update_cinder_quota(cinder_client, tenant_id, vols_quota,
1174 snapshots_quota, gigabytes_quota):
1175 quotas_values = {"volumes": vols_quota,
1176 "snapshots": snapshots_quota,
1177 "gigabytes": gigabytes_quota}
1180 cinder_client.quotas.update(tenant_id, **quotas_values)
1182 except Exception, e:
1183 logger.error("Error [update_cinder_quota(cinder_client, '%s', '%s', "
1184 "'%s' '%s')]: %s" % (tenant_id, vols_quota,
1185 snapshots_quota, gigabytes_quota, e))
1189 def delete_volume(cinder_client, volume_id, forced=False):
1193 cinder_client.volumes.detach(volume_id)
1195 logger.error(sys.exc_info()[0])
1196 cinder_client.volumes.force_delete(volume_id)
1198 cinder_client.volumes.delete(volume_id)
1200 except Exception, e:
1201 logger.error("Error [delete_volume(cinder_client, '%s', '%s')]: %s"
1202 % (volume_id, str(forced), e))
1206 def delete_volume_type(cinder_client, volume_type):
1208 cinder_client.volume_types.delete(volume_type)
1210 except Exception, e:
1211 logger.error("Error [delete_volume_type(cinder_client, '%s')]: %s"
1216 # *********************************************
1218 # *********************************************
1219 def get_tenants(keystone_client):
1221 tenants = keystone_client.tenants.list()
1223 except Exception, e:
1224 logger.error("Error [get_tenants(keystone_client)]: %s" % e)
1228 def get_users(keystone_client):
1230 users = keystone_client.users.list()
1232 except Exception, e:
1233 logger.error("Error [get_users(keystone_client)]: %s" % e)
1237 def get_tenant_id(keystone_client, tenant_name):
1238 tenants = keystone_client.tenants.list()
1241 if t.name == tenant_name:
1247 def get_user_id(keystone_client, user_name):
1248 users = keystone_client.users.list()
1251 if u.name == user_name:
1257 def get_role_id(keystone_client, role_name):
1258 roles = keystone_client.roles.list()
1261 if r.name == role_name:
1267 def create_tenant(keystone_client, tenant_name, tenant_description):
1269 tenant = keystone_client.tenants.create(tenant_name,
1273 except Exception, e:
1274 logger.error("Error [create_tenant(keystone_client, '%s', '%s')]: %s"
1275 % (tenant_name, tenant_description, e))
1279 def create_user(keystone_client, user_name, user_password,
1280 user_email, tenant_id):
1282 user = keystone_client.users.create(user_name, user_password,
1283 user_email, tenant_id,
1286 except Exception, e:
1287 logger.error("Error [create_user(keystone_client, '%s', '%s', '%s'"
1288 "'%s')]: %s" % (user_name, user_password,
1289 user_email, tenant_id, e))
1293 def add_role_user(keystone_client, user_id, role_id, tenant_id):
1295 keystone_client.roles.add_user_role(user_id, role_id, tenant_id)
1297 except Exception, e:
1298 logger.error("Error [add_role_user(keystone_client, '%s', '%s'"
1299 "'%s')]: %s " % (user_id, role_id, tenant_id, e))
1303 def delete_tenant(keystone_client, tenant_id):
1305 keystone_client.tenants.delete(tenant_id)
1307 except Exception, e:
1308 logger.error("Error [delete_tenant(keystone_client, '%s')]: %s"
1313 def delete_user(keystone_client, user_id):
1315 keystone_client.users.delete(user_id)
1317 except Exception, e:
1318 logger.error("Error [delete_user(keystone_client, '%s')]: %s"