4 # Generates a list of the current Openstack objects in the deployment:
9 # - Neutron networks, subnets and ports
14 # jose.lausuch@ericsson.com
17 # All rights reserved. This program and the accompanying materials
18 # are made available under the terms of the Apache License, Version 2.0
19 # which accompanies this distribution, and is available at
20 # http://www.apache.org/licenses/LICENSE-2.0
26 from novaclient import client as novaclient
27 from neutronclient.v2_0 import client as neutronclient
28 from keystoneclient.v2_0 import client as keystoneclient
29 from cinderclient import client as cinderclient
31 import functest.utils.openstack_utils as os_utils
32 import functest.utils.functest_logger as ft_logger
34 """ logging configuration """
35 logger = ft_logger.Logger("generate_defaults").getLogger()
37 REPO_PATH = os.environ['repos_dir'] + '/functest/'
38 if not os.path.exists(REPO_PATH):
39 logger.error("Functest repository directory not found '%s'" % REPO_PATH)
43 DEFAULTS_FILE = '/home/opnfv/functest/conf/os_defaults.yaml'
47 logger.info("-------------------------------------------")
50 def get_instances(nova_client):
51 logger.debug("Getting instances...")
53 instances = os_utils.get_instances(nova_client)
54 if not (instances is None or len(instances) == 0):
55 for instance in instances:
56 dic_instances.update({getattr(instance, 'id'): getattr(instance,
58 return {'instances': dic_instances}
61 def get_images(nova_client):
62 logger.debug("Getting images...")
64 images = os_utils.get_images(nova_client)
65 if not (images is None or len(images) == 0):
67 dic_images.update({getattr(image, 'id'): getattr(image, 'name')})
68 return {'images': dic_images}
71 def get_volumes(cinder_client):
72 logger.debug("Getting volumes...")
74 volumes = os_utils.get_volumes(cinder_client)
75 if volumes is not None:
76 for volume in volumes:
77 dic_volumes.update({volume.id: volume.display_name})
78 return {'volumes': dic_volumes}
81 def get_networks(neutron_client):
82 logger.debug("Getting networks")
84 networks = os_utils.get_network_list(neutron_client)
85 if networks is not None:
86 for network in networks:
87 dic_networks.update({network['id']: network['name']})
88 return {'networks': dic_networks}
91 def get_routers(neutron_client):
92 logger.debug("Getting routers")
94 routers = os_utils.get_router_list(neutron_client)
95 if routers is not None:
96 for router in routers:
97 dic_routers.update({router['id']: router['name']})
98 return {'routers': dic_routers}
101 def get_security_groups(neutron_client):
102 logger.debug("Getting Security groups...")
104 secgroups = os_utils.get_security_groups(neutron_client)
105 if not (secgroups is None or len(secgroups) == 0):
106 for secgroup in secgroups:
107 dic_secgroups.update({secgroup['id']: secgroup['name']})
108 return {'secgroups': dic_secgroups}
111 def get_floatinips(nova_client):
112 logger.debug("Getting Floating IPs...")
114 floatingips = os_utils.get_floating_ips(nova_client)
115 if not (floatingips is None or len(floatingips) == 0):
116 for floatingip in floatingips:
117 dic_floatingips.update({floatingip.id: floatingip.ip})
118 return {'floatingips': dic_floatingips}
121 def get_users(keystone_client):
122 logger.debug("Getting users...")
124 users = os_utils.get_users(keystone_client)
125 if not (users is None or len(users) == 0):
127 dic_users.update({getattr(user, 'id'): getattr(user, 'name')})
128 return {'users': dic_users}
131 def get_tenants(keystone_client):
132 logger.debug("Getting users...")
134 tenants = os_utils.get_tenants(keystone_client)
135 if not (tenants is None or len(tenants) == 0):
136 for tenant in tenants:
137 dic_tenants.update({getattr(tenant, 'id'):
138 getattr(tenant, 'name')})
139 return {'tenants': dic_tenants}
143 creds_nova = os_utils.get_credentials("nova")
144 nova_client = novaclient.Client('2', **creds_nova)
146 creds_neutron = os_utils.get_credentials("neutron")
147 neutron_client = neutronclient.Client(**creds_neutron)
149 creds_keystone = os_utils.get_credentials("keystone")
150 keystone_client = keystoneclient.Client(**creds_keystone)
152 creds_cinder = os_utils.get_credentials("cinder")
153 cinder_client = cinderclient.Client('1', creds_cinder['username'],
154 creds_cinder['api_key'],
155 creds_cinder['project_id'],
156 creds_cinder['auth_url'],
157 service_type="volume")
159 if not os_utils.check_credentials():
160 logger.error("Please source the openrc credentials and run the" +
165 defaults.update(get_instances(nova_client))
166 defaults.update(get_images(nova_client))
167 defaults.update(get_volumes(cinder_client))
168 defaults.update(get_networks(neutron_client))
169 defaults.update(get_routers(neutron_client))
170 defaults.update(get_security_groups(neutron_client))
171 defaults.update(get_floatinips(nova_client))
172 defaults.update(get_users(keystone_client))
173 defaults.update(get_tenants(keystone_client))
175 with open(DEFAULTS_FILE, 'w+') as yaml_file:
176 yaml_file.write(yaml.safe_dump(defaults, default_flow_style=False))
178 logger.info("Openstack Defaults found in the deployment:\n%s"
180 logger.debug("NOTE: These objects will NOT be deleted after " +
181 "running the tests.")
184 if __name__ == '__main__':