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 # Check that the env vars exists:
60 envvars = ('OS_USERNAME', 'OS_PASSWORD', 'OS_AUTH_URL', 'OS_TENANT_NAME')
61 for envvar in envvars:
62 if os.getenv(envvar) is None:
63 raise MissingEnvVar(envvar)
65 # Unfortunately, each of the OpenStack client will request slightly
66 # different entries in their credentials dict.
67 if service.lower() in ("nova", "cinder"):
72 tenant = "tenant_name"
74 # The most common way to pass these info to the script is to do it through
75 # environment variables.
77 "username": os.environ.get("OS_USERNAME"),
78 password: os.environ.get("OS_PASSWORD"),
79 "auth_url": os.environ.get("OS_AUTH_URL"),
80 tenant: os.environ.get("OS_TENANT_NAME")
82 if os.getenv('OS_ENDPOINT_TYPE') is not None:
84 "endpoint_type": os.environ.get("OS_ENDPOINT_TYPE")
86 if os.getenv('OS_REGION_NAME') is not None:
88 "region_name": os.environ.get("OS_REGION_NAME")
90 cacert = os.environ.get("OS_CACERT")
91 if cacert is not None:
92 # each openstack client uses differnt kwargs for this
93 creds.update({"cacert": cacert,
95 "https_ca_cert": cacert,
96 "https_cacert": cacert,
98 creds.update({"insecure": "True", "https_insecure": "True"})
99 if not os.path.isfile(cacert):
100 logger.info("WARNING: The 'OS_CACERT' environment variable is "
101 "set to %s but the file does not exist." % cacert)
105 def source_credentials(rc_file):
106 pipe = subprocess.Popen(". %s; env" % rc_file, stdout=subprocess.PIPE,
108 output = pipe.communicate()[0]
109 env = dict((line.split("=", 1) for line in output.splitlines()))
110 os.environ.update(env)
114 def get_credentials_for_rally():
115 creds = get_credentials("keystone")
116 admin_keys = ['username', 'tenant_name', 'password']
117 endpoint_types = [('internalURL', 'internal'),
118 ('publicURL', 'public'), ('adminURL', 'admin')]
119 if 'endpoint_type' in creds.keys():
120 for k, v in endpoint_types:
121 if creds['endpoint_type'] == k:
122 creds['endpoint_type'] = v
123 rally_conf = {"type": "ExistingCloud", "admin": {}}
125 if key in admin_keys:
126 rally_conf['admin'][key] = creds[key]
128 rally_conf[key] = creds[key]
132 # *********************************************
134 # *********************************************
135 def get_keystone_client():
136 creds_keystone = get_credentials("keystone")
137 return keystoneclient.Client(**creds_keystone)
140 def get_nova_client():
141 creds_nova = get_credentials("nova")
142 return novaclient.Client('2', **creds_nova)
145 def get_cinder_client():
146 creds_cinder = get_credentials("cinder")
147 creds_cinder.update({
148 "service_type": "volume"
150 return cinderclient.Client('2', **creds_cinder)
153 def get_neutron_client():
154 creds_neutron = get_credentials("neutron")
155 return neutronclient.Client(**creds_neutron)
158 def get_glance_client():
159 keystone_client = get_keystone_client()
160 glance_endpoint_type = 'publicURL'
161 os_endpoint_type = os.getenv('OS_ENDPOINT_TYPE')
162 if os_endpoint_type is not None:
163 glance_endpoint_type = os_endpoint_type
164 glance_endpoint = keystone_client.service_catalog.url_for(
165 service_type='image', endpoint_type=glance_endpoint_type)
166 return glanceclient.Client(1, glance_endpoint,
167 token=keystone_client.auth_token)
170 # *********************************************
172 # *********************************************
173 def get_instances(nova_client):
175 instances = nova_client.servers.list(search_opts={'all_tenants': 1})
178 logger.error("Error [get_instances(nova_client)]: %s" % e)
182 def get_instance_status(nova_client, instance):
184 instance = nova_client.servers.get(instance.id)
185 return instance.status
187 logger.error("Error [get_instance_status(nova_client)]: %s" % e)
191 def get_instance_by_name(nova_client, instance_name):
193 instance = nova_client.servers.find(name=instance_name)
196 logger.error("Error [get_instance_by_name(nova_client, '%s')]: %s"
197 % (instance_name, e))
201 def get_flavor_id(nova_client, flavor_name):
202 flavors = nova_client.flavors.list(detailed=True)
205 if f.name == flavor_name:
211 def get_flavor_id_by_ram_range(nova_client, min_ram, max_ram):
212 flavors = nova_client.flavors.list(detailed=True)
215 if min_ram <= f.ram and f.ram <= max_ram:
221 def get_aggregates(nova_client):
223 aggregates = nova_client.aggregates.list()
226 logger.error("Error [get_aggregates(nova_client)]: %s" % e)
230 def get_aggregate_id(nova_client, aggregate_name):
232 aggregates = get_aggregates(nova_client)
233 _id = [ag.id for ag in aggregates if ag.name == aggregate_name][0]
236 logger.error("Error [get_aggregate_id(nova_client, %s)]:"
237 " %s" % (aggregate_name, e))
241 def get_availability_zones(nova_client):
243 availability_zones = nova_client.availability_zones.list()
244 return availability_zones
246 logger.error("Error [get_availability_zones(nova_client)]: %s" % e)
250 def get_availability_zone_names(nova_client):
252 az_names = [az.zoneName for az in get_availability_zones(nova_client)]
255 logger.error("Error [get_availability_zone_names(nova_client)]:"
260 def create_flavor(nova_client, flavor_name, ram, disk, vcpus, public=True):
262 flavor = nova_client.flavors.create(
263 flavor_name, ram, vcpus, disk, is_public=public)
265 extra_specs = ft_utils.get_functest_config(
266 'general.flavor_extra_specs')
267 flavor.set_keys(extra_specs)
269 # flavor extra specs are not configured, therefore skip the update
273 logger.error("Error [create_flavor(nova_client, '%s', '%s', '%s', "
274 "'%s')]: %s" % (flavor_name, ram, disk, vcpus, e))
279 def get_or_create_flavor(flavor_name, ram, disk, vcpus, public=True):
280 flavor_exists = False
281 nova_client = get_nova_client()
283 flavor_id = get_flavor_id(nova_client, flavor_name)
285 logger.info("Using existing flavor '%s'..." % flavor_name)
288 logger.info("Creating flavor '%s' with '%s' RAM, '%s' disk size, "
289 "'%s' vcpus..." % (flavor_name, ram, disk, vcpus))
290 flavor_id = create_flavor(
291 nova_client, flavor_name, ram, disk, vcpus, public=public)
293 logger.error("Failed to create flavor '%s'..." % (flavor_name))
295 logger.debug("Flavor '%s' with ID=%s created successfully."
296 % (flavor_name, flavor_id))
298 return flavor_exists, flavor_id
301 def get_floating_ips(nova_client):
303 floating_ips = nova_client.floating_ips.list()
306 logger.error("Error [get_floating_ips(nova_client)]: %s" % e)
310 def get_hypervisors(nova_client):
313 hypervisors = nova_client.hypervisors.list()
314 for hypervisor in hypervisors:
315 if hypervisor.state == "up":
316 nodes.append(hypervisor.hypervisor_hostname)
319 logger.error("Error [get_hypervisors(nova_client)]: %s" % e)
323 def create_aggregate(nova_client, aggregate_name, av_zone):
325 nova_client.aggregates.create(aggregate_name, av_zone)
328 logger.error("Error [create_aggregate(nova_client, %s, %s)]: %s"
329 % (aggregate_name, av_zone, e))
333 def add_host_to_aggregate(nova_client, aggregate_name, compute_host):
335 aggregate_id = get_aggregate_id(nova_client, aggregate_name)
336 nova_client.aggregates.add_host(aggregate_id, compute_host)
339 logger.error("Error [add_host_to_aggregate(nova_client, %s, %s)]: %s"
340 % (aggregate_name, compute_host, e))
344 def create_aggregate_with_host(
345 nova_client, aggregate_name, av_zone, compute_host):
347 create_aggregate(nova_client, aggregate_name, av_zone)
348 add_host_to_aggregate(nova_client, aggregate_name, compute_host)
351 logger.error("Error [create_aggregate_with_host("
352 "nova_client, %s, %s, %s)]: %s"
353 % (aggregate_name, av_zone, compute_host, e))
357 def create_instance(flavor_name,
360 instance_name="functest-vm",
366 nova_client = get_nova_client()
368 flavor = nova_client.flavors.find(name=flavor_name)
370 flavors = nova_client.flavors.list()
371 logger.error("Error: Flavor '%s' not found. Available flavors are: "
372 "\n%s" % (flavor_name, flavors))
374 if fixed_ip is not None:
375 nics = {"net-id": network_id, "v4-fixed-ip": fixed_ip}
377 nics = {"net-id": network_id}
379 instance = nova_client.servers.create(
384 availability_zone=av_zone,
388 instance = nova_client.servers.create(
393 config_drive=confdrive,
395 availability_zone=av_zone,
401 def create_instance_and_wait_for_active(flavor_name,
411 VM_BOOT_TIMEOUT = 180
412 nova_client = get_nova_client()
413 instance = create_instance(flavor_name,
422 count = VM_BOOT_TIMEOUT / SLEEP
423 for n in range(count, -1, -1):
424 status = get_instance_status(nova_client, instance)
425 if status.lower() == "active":
427 elif status.lower() == "error":
428 logger.error("The instance %s went to ERROR status."
432 logger.error("Timeout booting the instance %s." % instance_name)
436 def create_floating_ip(neutron_client):
437 extnet_id = get_external_net_id(neutron_client)
438 props = {'floating_network_id': extnet_id}
440 ip_json = neutron_client.create_floatingip({'floatingip': props})
441 fip_addr = ip_json['floatingip']['floating_ip_address']
442 fip_id = ip_json['floatingip']['id']
444 logger.error("Error [create_floating_ip(neutron_client)]: %s" % e)
446 return {'fip_addr': fip_addr, 'fip_id': fip_id}
449 def add_floating_ip(nova_client, server_id, floatingip_id):
451 nova_client.servers.add_floating_ip(server_id, floatingip_id)
454 logger.error("Error [add_floating_ip(nova_client, '%s', '%s')]: %s"
455 % (server_id, floatingip_id, e))
459 def delete_instance(nova_client, instance_id):
461 nova_client.servers.force_delete(instance_id)
464 logger.error("Error [delete_instance(nova_client, '%s')]: %s"
469 def delete_floating_ip(nova_client, floatingip_id):
471 nova_client.floating_ips.delete(floatingip_id)
474 logger.error("Error [delete_floating_ip(nova_client, '%s')]: %s"
475 % (floatingip_id, e))
479 def remove_host_from_aggregate(nova_client, aggregate_name, compute_host):
481 aggregate_id = get_aggregate_id(nova_client, aggregate_name)
482 nova_client.aggregates.remove_host(aggregate_id, compute_host)
485 logger.error("Error [remove_host_from_aggregate(nova_client, %s, %s)]:"
486 " %s" % (aggregate_name, compute_host, e))
490 def remove_hosts_from_aggregate(nova_client, aggregate_name):
491 aggregate_id = get_aggregate_id(nova_client, aggregate_name)
492 hosts = nova_client.aggregates.get(aggregate_id).hosts
494 all(remove_host_from_aggregate(nova_client, aggregate_name, host)
498 def delete_aggregate(nova_client, aggregate_name):
500 remove_hosts_from_aggregate(nova_client, aggregate_name)
501 nova_client.aggregates.delete(aggregate_name)
504 logger.error("Error [delete_aggregate(nova_client, %s)]: %s"
505 % (aggregate_name, e))
509 # *********************************************
511 # *********************************************
512 def get_network_list(neutron_client):
513 network_list = neutron_client.list_networks()['networks']
514 if len(network_list) == 0:
520 def get_router_list(neutron_client):
521 router_list = neutron_client.list_routers()['routers']
522 if len(router_list) == 0:
528 def get_port_list(neutron_client):
529 port_list = neutron_client.list_ports()['ports']
530 if len(port_list) == 0:
536 def get_network_id(neutron_client, network_name):
537 networks = neutron_client.list_networks()['networks']
540 if n['name'] == network_name:
546 def get_subnet_id(neutron_client, subnet_name):
547 subnets = neutron_client.list_subnets()['subnets']
550 if s['name'] == subnet_name:
556 def get_router_id(neutron_client, router_name):
557 routers = neutron_client.list_routers()['routers']
560 if r['name'] == router_name:
566 def get_private_net(neutron_client):
567 # Checks if there is an existing shared private network
568 networks = neutron_client.list_networks()['networks']
569 if len(networks) == 0:
572 if (net['router:external'] is False) and (net['shared'] is True):
577 def get_external_net(neutron_client):
578 for network in neutron_client.list_networks()['networks']:
579 if network['router:external']:
580 return network['name']
584 def get_external_net_id(neutron_client):
585 for network in neutron_client.list_networks()['networks']:
586 if network['router:external']:
591 def check_neutron_net(neutron_client, net_name):
592 for network in neutron_client.list_networks()['networks']:
593 if network['name'] == net_name:
594 for subnet in network['subnets']:
599 def create_neutron_net(neutron_client, name):
600 json_body = {'network': {'name': name,
601 'admin_state_up': True}}
603 network = neutron_client.create_network(body=json_body)
604 network_dict = network['network']
605 return network_dict['id']
607 logger.error("Error [create_neutron_net(neutron_client, '%s')]: %s"
612 def create_neutron_subnet(neutron_client, name, cidr, net_id):
613 json_body = {'subnets': [{'name': name, 'cidr': cidr,
614 'ip_version': 4, 'network_id': net_id}]}
616 subnet = neutron_client.create_subnet(body=json_body)
617 return subnet['subnets'][0]['id']
619 logger.error("Error [create_neutron_subnet(neutron_client, '%s', "
620 "'%s', '%s')]: %s" % (name, cidr, net_id, e))
624 def create_neutron_router(neutron_client, name):
625 json_body = {'router': {'name': name, 'admin_state_up': True}}
627 router = neutron_client.create_router(json_body)
628 return router['router']['id']
630 logger.error("Error [create_neutron_router(neutron_client, '%s')]: %s"
635 def create_neutron_port(neutron_client, name, network_id, ip):
636 json_body = {'port': {
637 'admin_state_up': True,
639 'network_id': network_id,
640 'fixed_ips': [{"ip_address": ip}]
643 port = neutron_client.create_port(body=json_body)
644 return port['port']['id']
646 logger.error("Error [create_neutron_port(neutron_client, '%s', '%s', "
647 "'%s')]: %s" % (name, network_id, ip, e))
651 def update_neutron_net(neutron_client, network_id, shared=False):
652 json_body = {'network': {'shared': shared}}
654 neutron_client.update_network(network_id, body=json_body)
657 logger.error("Error [update_neutron_net(neutron_client, '%s', '%s')]: "
658 "%s" % (network_id, str(shared), e))
662 def update_neutron_port(neutron_client, port_id, device_owner):
663 json_body = {'port': {
664 'device_owner': device_owner,
667 port = neutron_client.update_port(port=port_id,
669 return port['port']['id']
671 logger.error("Error [update_neutron_port(neutron_client, '%s', '%s')]:"
672 " %s" % (port_id, device_owner, e))
676 def add_interface_router(neutron_client, router_id, subnet_id):
677 json_body = {"subnet_id": subnet_id}
679 neutron_client.add_interface_router(router=router_id, body=json_body)
682 logger.error("Error [add_interface_router(neutron_client, '%s', "
683 "'%s')]: %s" % (router_id, subnet_id, e))
687 def add_gateway_router(neutron_client, router_id):
688 ext_net_id = get_external_net_id(neutron_client)
689 router_dict = {'network_id': ext_net_id}
691 neutron_client.add_gateway_router(router_id, router_dict)
694 logger.error("Error [add_gateway_router(neutron_client, '%s')]: %s"
699 def delete_neutron_net(neutron_client, network_id):
701 neutron_client.delete_network(network_id)
704 logger.error("Error [delete_neutron_net(neutron_client, '%s')]: %s"
709 def delete_neutron_subnet(neutron_client, subnet_id):
711 neutron_client.delete_subnet(subnet_id)
714 logger.error("Error [delete_neutron_subnet(neutron_client, '%s')]: %s"
719 def delete_neutron_router(neutron_client, router_id):
721 neutron_client.delete_router(router=router_id)
724 logger.error("Error [delete_neutron_router(neutron_client, '%s')]: %s"
729 def delete_neutron_port(neutron_client, port_id):
731 neutron_client.delete_port(port_id)
734 logger.error("Error [delete_neutron_port(neutron_client, '%s')]: %s"
739 def remove_interface_router(neutron_client, router_id, subnet_id):
740 json_body = {"subnet_id": subnet_id}
742 neutron_client.remove_interface_router(router=router_id,
746 logger.error("Error [remove_interface_router(neutron_client, '%s', "
747 "'%s')]: %s" % (router_id, subnet_id, e))
751 def remove_gateway_router(neutron_client, router_id):
753 neutron_client.remove_gateway_router(router_id)
756 logger.error("Error [remove_gateway_router(neutron_client, '%s')]: %s"
761 def create_network_full(neutron_client,
767 # Check if the network already exists
768 network_id = get_network_id(neutron_client, net_name)
769 subnet_id = get_subnet_id(neutron_client, subnet_name)
770 router_id = get_router_id(neutron_client, router_name)
772 if network_id != '' and subnet_id != '' and router_id != '':
773 logger.info("A network with name '%s' already exists..." % net_name)
775 neutron_client.format = 'json'
776 logger.info('Creating neutron network %s...' % net_name)
777 network_id = create_neutron_net(neutron_client, net_name)
782 logger.debug("Network '%s' created successfully" % network_id)
783 logger.debug('Creating Subnet....')
784 subnet_id = create_neutron_subnet(neutron_client, subnet_name,
789 logger.debug("Subnet '%s' created successfully" % subnet_id)
790 logger.debug('Creating Router...')
791 router_id = create_neutron_router(neutron_client, router_name)
796 logger.debug("Router '%s' created successfully" % router_id)
797 logger.debug('Adding router to subnet...')
799 if not add_interface_router(neutron_client, router_id, subnet_id):
802 logger.debug("Interface added successfully.")
804 logger.debug('Adding gateway to router...')
805 if not add_gateway_router(neutron_client, router_id):
808 logger.debug("Gateway added successfully.")
810 network_dic = {'net_id': network_id,
811 'subnet_id': subnet_id,
812 'router_id': router_id}
816 def create_shared_network_full(net_name, subnt_name, router_name, subnet_cidr):
817 neutron_client = get_neutron_client()
819 network_dic = create_network_full(neutron_client,
825 if not update_neutron_net(neutron_client,
826 network_dic['net_id'],
828 logger.error("Failed to update network %s..." % net_name)
831 logger.debug("Network '%s' is available..." % net_name)
833 logger.error("Network %s creation failed" % net_name)
838 def create_bgpvpn(neutron_client, **kwargs):
839 # route_distinguishers
841 json_body = {"bgpvpn": kwargs}
842 return neutron_client.create_bgpvpn(json_body)
845 def create_network_association(neutron_client, bgpvpn_id, neutron_network_id):
846 json_body = {"network_association": {"network_id": neutron_network_id}}
847 return neutron_client.create_network_association(bgpvpn_id, json_body)
850 def create_router_association(neutron_client, bgpvpn_id, router_id):
851 json_body = {"router_association": {"router_id": router_id}}
852 return neutron_client.create_router_association(bgpvpn_id, json_body)
855 def update_bgpvpn(neutron_client, bgpvpn_id, **kwargs):
856 json_body = {"bgpvpn": kwargs}
857 return neutron_client.update_bgpvpn(bgpvpn_id, json_body)
860 def delete_bgpvpn(neutron_client, bgpvpn_id):
861 return neutron_client.delete_bgpvpn(bgpvpn_id)
864 def get_bgpvpn(neutron_client, bgpvpn_id):
865 return neutron_client.show_bgpvpn(bgpvpn_id)
868 def get_bgpvpn_routers(neutron_client, bgpvpn_id):
869 return get_bgpvpn(neutron_client, bgpvpn_id)['bgpvpn']['routers']
872 def get_bgpvpn_networks(neutron_client, bgpvpn_id):
873 return get_bgpvpn(neutron_client, bgpvpn_id)['bgpvpn']['networks']
875 # *********************************************
877 # *********************************************
880 def get_security_groups(neutron_client):
882 security_groups = neutron_client.list_security_groups()[
884 return security_groups
886 logger.error("Error [get_security_groups(neutron_client)]: %s" % e)
890 def get_security_group_id(neutron_client, sg_name):
891 security_groups = get_security_groups(neutron_client)
893 for sg in security_groups:
894 if sg['name'] == sg_name:
900 def create_security_group(neutron_client, sg_name, sg_description):
901 json_body = {'security_group': {'name': sg_name,
902 'description': sg_description}}
904 secgroup = neutron_client.create_security_group(json_body)
905 return secgroup['security_group']
907 logger.error("Error [create_security_group(neutron_client, '%s', "
908 "'%s')]: %s" % (sg_name, sg_description, e))
912 def create_secgroup_rule(neutron_client, sg_id, direction, protocol,
913 port_range_min=None, port_range_max=None):
914 if port_range_min is None and port_range_max is None:
915 json_body = {'security_group_rule': {'direction': direction,
916 'security_group_id': sg_id,
917 'protocol': protocol}}
918 elif port_range_min is not None and port_range_max is not None:
919 json_body = {'security_group_rule': {'direction': direction,
920 'security_group_id': sg_id,
921 'port_range_min': port_range_min,
922 'port_range_max': port_range_max,
923 'protocol': protocol}}
925 logger.error("Error [create_secgroup_rule(neutron_client, '%s', '%s', "
926 "'%s', '%s', '%s', '%s')]:" % (neutron_client,
931 " Invalid values for port_range_min, port_range_max")
934 neutron_client.create_security_group_rule(json_body)
937 logger.error("Error [create_secgroup_rule(neutron_client, '%s', '%s', "
938 "'%s', '%s', '%s', '%s')]: %s" % (neutron_client,
947 def create_security_group_full(neutron_client,
948 sg_name, sg_description):
949 sg_id = get_security_group_id(neutron_client, sg_name)
951 logger.info("Using existing security group '%s'..." % sg_name)
953 logger.info("Creating security group '%s'..." % sg_name)
954 SECGROUP = create_security_group(neutron_client,
958 logger.error("Failed to create the security group...")
961 sg_id = SECGROUP['id']
963 logger.debug("Security group '%s' with ID=%s created successfully."
964 % (SECGROUP['name'], sg_id))
966 logger.debug("Adding ICMP rules in security group '%s'..."
968 if not create_secgroup_rule(neutron_client, sg_id,
970 logger.error("Failed to create the security group rule...")
973 logger.debug("Adding SSH rules in security group '%s'..."
975 if not create_secgroup_rule(
976 neutron_client, sg_id, 'ingress', 'tcp', '22', '22'):
977 logger.error("Failed to create the security group rule...")
980 if not create_secgroup_rule(
981 neutron_client, sg_id, 'egress', 'tcp', '22', '22'):
982 logger.error("Failed to create the security group rule...")
987 def add_secgroup_to_instance(nova_client, instance_id, secgroup_id):
989 nova_client.servers.add_security_group(instance_id, secgroup_id)
992 logger.error("Error [add_secgroup_to_instance(nova_client, '%s', "
993 "'%s')]: %s" % (instance_id, secgroup_id, e))
997 def update_sg_quota(neutron_client, tenant_id, sg_quota, sg_rule_quota):
998 json_body = {"quota": {
999 "security_group": sg_quota,
1000 "security_group_rule": sg_rule_quota
1004 neutron_client.update_quota(tenant_id=tenant_id,
1007 except Exception, e:
1008 logger.error("Error [update_sg_quota(neutron_client, '%s', '%s', "
1009 "'%s')]: %s" % (tenant_id, sg_quota, sg_rule_quota, e))
1013 def delete_security_group(neutron_client, secgroup_id):
1015 neutron_client.delete_security_group(secgroup_id)
1017 except Exception, e:
1018 logger.error("Error [delete_security_group(neutron_client, '%s')]: %s"
1023 # *********************************************
1025 # *********************************************
1026 def get_images(nova_client):
1028 images = nova_client.images.list()
1030 except Exception, e:
1031 logger.error("Error [get_images]: %s" % e)
1035 def get_image_id(glance_client, image_name):
1036 images = glance_client.images.list()
1039 if i.name == image_name:
1045 def create_glance_image(glance_client, image_name, file_path, disk="qcow2",
1046 container="bare", public=True):
1047 if not os.path.isfile(file_path):
1048 logger.error("Error: file %s does not exist." % file_path)
1051 image_id = get_image_id(glance_client, image_name)
1054 logger.info("Image %s already exists." % image_name)
1057 logger.info("Creating image '%s' from '%s'..." % (image_name,
1060 properties = ft_utils.get_functest_config(
1061 'general.image_properties')
1063 # image properties are not configured
1064 # therefore don't add any properties
1066 with open(file_path) as fimage:
1067 image = glance_client.images.create(name=image_name,
1070 container_format=container,
1071 properties=properties,
1075 except Exception, e:
1076 logger.error("Error [create_glance_image(glance_client, '%s', '%s', "
1077 "'%s')]: %s" % (image_name, file_path, str(public), e))
1081 def get_or_create_image(name, path, format):
1082 image_exists = False
1083 glance_client = get_glance_client()
1085 image_id = get_image_id(glance_client, name)
1087 logger.info("Using existing image '%s'..." % name)
1090 logger.info("Creating image '%s' from '%s'..." % (name, path))
1091 image_id = create_glance_image(glance_client, name, path, format)
1093 logger.error("Failed to create a Glance image...")
1095 logger.debug("Image '%s' with ID=%s created successfully."
1098 return image_exists, image_id
1101 def delete_glance_image(nova_client, image_id):
1103 nova_client.images.delete(image_id)
1105 except Exception, e:
1106 logger.error("Error [delete_glance_image(nova_client, '%s')]: %s"
1111 # *********************************************
1113 # *********************************************
1114 def get_volumes(cinder_client):
1116 volumes = cinder_client.volumes.list(search_opts={'all_tenants': 1})
1118 except Exception, e:
1119 logger.error("Error [get_volumes(cinder_client)]: %s" % e)
1123 def list_volume_types(cinder_client, public=True, private=True):
1125 volume_types = cinder_client.volume_types.list()
1127 volume_types = [vt for vt in volume_types if not vt.is_public]
1129 volume_types = [vt for vt in volume_types if vt.is_public]
1131 except Exception, e:
1132 logger.error("Error [list_volume_types(cinder_client)]: %s" % e)
1136 def create_volume_type(cinder_client, name):
1138 volume_type = cinder_client.volume_types.create(name)
1140 except Exception, e:
1141 logger.error("Error [create_volume_type(cinder_client, '%s')]: %s"
1146 def update_cinder_quota(cinder_client, tenant_id, vols_quota,
1147 snapshots_quota, gigabytes_quota):
1148 quotas_values = {"volumes": vols_quota,
1149 "snapshots": snapshots_quota,
1150 "gigabytes": gigabytes_quota}
1153 cinder_client.quotas.update(tenant_id, **quotas_values)
1155 except Exception, e:
1156 logger.error("Error [update_cinder_quota(cinder_client, '%s', '%s', "
1157 "'%s' '%s')]: %s" % (tenant_id, vols_quota,
1158 snapshots_quota, gigabytes_quota, e))
1162 def delete_volume(cinder_client, volume_id, forced=False):
1166 cinder_client.volumes.detach(volume_id)
1168 logger.error(sys.exc_info()[0])
1169 cinder_client.volumes.force_delete(volume_id)
1171 cinder_client.volumes.delete(volume_id)
1173 except Exception, e:
1174 logger.error("Error [delete_volume(cinder_client, '%s', '%s')]: %s"
1175 % (volume_id, str(forced), e))
1179 def delete_volume_type(cinder_client, volume_type):
1181 cinder_client.volume_types.delete(volume_type)
1183 except Exception, e:
1184 logger.error("Error [delete_volume_type(cinder_client, '%s')]: %s"
1189 # *********************************************
1191 # *********************************************
1192 def get_tenants(keystone_client):
1194 tenants = keystone_client.tenants.list()
1196 except Exception, e:
1197 logger.error("Error [get_tenants(keystone_client)]: %s" % e)
1201 def get_users(keystone_client):
1203 users = keystone_client.users.list()
1205 except Exception, e:
1206 logger.error("Error [get_users(keystone_client)]: %s" % e)
1210 def get_tenant_id(keystone_client, tenant_name):
1211 tenants = keystone_client.tenants.list()
1214 if t.name == tenant_name:
1220 def get_user_id(keystone_client, user_name):
1221 users = keystone_client.users.list()
1224 if u.name == user_name:
1230 def get_role_id(keystone_client, role_name):
1231 roles = keystone_client.roles.list()
1234 if r.name == role_name:
1240 def create_tenant(keystone_client, tenant_name, tenant_description):
1242 tenant = keystone_client.tenants.create(tenant_name,
1246 except Exception, e:
1247 logger.error("Error [create_tenant(keystone_client, '%s', '%s')]: %s"
1248 % (tenant_name, tenant_description, e))
1252 def create_user(keystone_client, user_name, user_password,
1253 user_email, tenant_id):
1255 user = keystone_client.users.create(user_name, user_password,
1256 user_email, tenant_id,
1259 except Exception, e:
1260 logger.error("Error [create_user(keystone_client, '%s', '%s', '%s'"
1261 "'%s')]: %s" % (user_name, user_password,
1262 user_email, tenant_id, e))
1266 def add_role_user(keystone_client, user_id, role_id, tenant_id):
1268 keystone_client.roles.add_user_role(user_id, role_id, tenant_id)
1270 except Exception, e:
1271 logger.error("Error [add_role_user(keystone_client, '%s', '%s'"
1272 "'%s')]: %s " % (user_id, role_id, tenant_id, e))
1276 def delete_tenant(keystone_client, tenant_id):
1278 keystone_client.tenants.delete(tenant_id)
1280 except Exception, e:
1281 logger.error("Error [delete_tenant(keystone_client, '%s')]: %s"
1286 def delete_user(keystone_client, user_id):
1288 keystone_client.users.delete(user_id)
1290 except Exception, e:
1291 logger.error("Error [delete_user(keystone_client, '%s')]: %s"