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
23 """ logging configuration """
27 from cinderclient import client as cinderclient
28 import functest.utils.functest_logger as ft_logger
29 import functest.utils.functest_utils as ft_utils
30 import functest.utils.openstack_utils as os_utils
31 from keystoneclient.v2_0 import client as keystoneclient
32 from neutronclient.v2_0 import client as neutronclient
33 from novaclient import client as novaclient
37 logger = ft_logger.Logger("openstack_snapshot").getLogger()
39 REPO_PATH = os.environ['repos_dir'] + '/functest/'
40 if not os.path.exists(REPO_PATH):
41 logger.error("Functest repository directory not found '%s'" % REPO_PATH)
45 OS_SNAPSHOT_FILE = ft_utils.get_parameter_from_yaml(
46 "general.openstack.snapshot_file")
50 logger.info("-------------------------------------------")
53 def get_instances(nova_client):
54 logger.debug("Getting instances...")
56 instances = os_utils.get_instances(nova_client)
57 if not (instances is None or len(instances) == 0):
58 for instance in instances:
59 dic_instances.update({getattr(instance, 'id'): getattr(instance,
61 return {'instances': dic_instances}
64 def get_images(nova_client):
65 logger.debug("Getting images...")
67 images = os_utils.get_images(nova_client)
68 if not (images is None or len(images) == 0):
70 dic_images.update({getattr(image, 'id'): getattr(image, 'name')})
71 return {'images': dic_images}
74 def get_volumes(cinder_client):
75 logger.debug("Getting volumes...")
77 volumes = os_utils.get_volumes(cinder_client)
78 if volumes is not None:
79 for volume in volumes:
80 dic_volumes.update({volume.id: volume.display_name})
81 return {'volumes': dic_volumes}
84 def get_networks(neutron_client):
85 logger.debug("Getting networks")
87 networks = os_utils.get_network_list(neutron_client)
88 if networks is not None:
89 for network in networks:
90 dic_networks.update({network['id']: network['name']})
91 return {'networks': dic_networks}
94 def get_routers(neutron_client):
95 logger.debug("Getting routers")
97 routers = os_utils.get_router_list(neutron_client)
98 if routers is not None:
99 for router in routers:
100 dic_routers.update({router['id']: router['name']})
101 return {'routers': dic_routers}
104 def get_security_groups(neutron_client):
105 logger.debug("Getting Security groups...")
107 secgroups = os_utils.get_security_groups(neutron_client)
108 if not (secgroups is None or len(secgroups) == 0):
109 for secgroup in secgroups:
110 dic_secgroups.update({secgroup['id']: secgroup['name']})
111 return {'secgroups': dic_secgroups}
114 def get_floatinips(nova_client):
115 logger.debug("Getting Floating IPs...")
117 floatingips = os_utils.get_floating_ips(nova_client)
118 if not (floatingips is None or len(floatingips) == 0):
119 for floatingip in floatingips:
120 dic_floatingips.update({floatingip.id: floatingip.ip})
121 return {'floatingips': dic_floatingips}
124 def get_users(keystone_client):
125 logger.debug("Getting users...")
127 users = os_utils.get_users(keystone_client)
128 if not (users is None or len(users) == 0):
130 dic_users.update({getattr(user, 'id'): getattr(user, 'name')})
131 return {'users': dic_users}
134 def get_tenants(keystone_client):
135 logger.debug("Getting tenants...")
137 tenants = os_utils.get_tenants(keystone_client)
138 if not (tenants is None or len(tenants) == 0):
139 for tenant in tenants:
140 dic_tenants.update({getattr(tenant, 'id'):
141 getattr(tenant, 'name')})
142 return {'tenants': dic_tenants}
146 creds_nova = os_utils.get_credentials("nova")
147 nova_client = novaclient.Client('2', **creds_nova)
149 creds_neutron = os_utils.get_credentials("neutron")
150 neutron_client = neutronclient.Client(**creds_neutron)
152 creds_keystone = os_utils.get_credentials("keystone")
153 keystone_client = keystoneclient.Client(**creds_keystone)
155 creds_cinder = os_utils.get_credentials("cinder")
156 cinder_client = cinderclient.Client('1', creds_cinder['username'],
157 creds_cinder['api_key'],
158 creds_cinder['project_id'],
159 creds_cinder['auth_url'],
160 service_type="volume")
162 if not os_utils.check_credentials():
163 logger.error("Please source the openrc credentials and run the" +
168 snapshot.update(get_instances(nova_client))
169 snapshot.update(get_images(nova_client))
170 snapshot.update(get_volumes(cinder_client))
171 snapshot.update(get_networks(neutron_client))
172 snapshot.update(get_routers(neutron_client))
173 snapshot.update(get_security_groups(neutron_client))
174 snapshot.update(get_floatinips(nova_client))
175 snapshot.update(get_users(keystone_client))
176 snapshot.update(get_tenants(keystone_client))
178 with open(OS_SNAPSHOT_FILE, 'w+') as yaml_file:
179 yaml_file.write(yaml.safe_dump(snapshot, default_flow_style=False))
181 logger.debug("Openstack Snapshot found in the deployment:\n%s"
183 logger.debug("NOTE: These objects will NOT be deleted after " +
187 if __name__ == '__main__':