4 # Generates a list of the current Openstack objects in the deployment:
9 # - Neutron networks, subnets and ports
12 # - Tacker VNFDs and VNFs
13 # - Tacker SFCs and SFC classifiers
16 # jose.lausuch@ericsson.com
19 # All rights reserved. This program and the accompanying materials
20 # are made available under the terms of the Apache License, Version 2.0
21 # which accompanies this distribution, and is available at
22 # http://www.apache.org/licenses/LICENSE-2.0
25 import functest.utils.functest_logger as ft_logger
26 import functest.utils.openstack_utils as os_utils
27 import functest.utils.openstack_tacker as os_tacker
29 import functest.utils.functest_constants as ft_constants
31 logger = ft_logger.Logger("openstack_snapshot").getLogger()
34 OS_SNAPSHOT_FILE = ft_constants.OPENSTACK_SNAPSHOT_FILE
38 logger.info("-------------------------------------------")
41 def get_instances(nova_client):
42 logger.debug("Getting instances...")
44 instances = os_utils.get_instances(nova_client)
45 if not (instances is None or len(instances) == 0):
46 for instance in instances:
47 dic_instances.update({getattr(instance, 'id'): getattr(instance,
49 return {'instances': dic_instances}
52 def get_images(nova_client):
53 logger.debug("Getting images...")
55 images = os_utils.get_images(nova_client)
56 if not (images is None or len(images) == 0):
58 dic_images.update({getattr(image, 'id'): getattr(image, 'name')})
59 return {'images': dic_images}
62 def get_volumes(cinder_client):
63 logger.debug("Getting volumes...")
65 volumes = os_utils.get_volumes(cinder_client)
66 if volumes is not None:
67 for volume in volumes:
68 dic_volumes.update({volume.id: volume.display_name})
69 return {'volumes': dic_volumes}
72 def get_networks(neutron_client):
73 logger.debug("Getting networks")
75 networks = os_utils.get_network_list(neutron_client)
76 if networks is not None:
77 for network in networks:
78 dic_networks.update({network['id']: network['name']})
79 return {'networks': dic_networks}
82 def get_routers(neutron_client):
83 logger.debug("Getting routers")
85 routers = os_utils.get_router_list(neutron_client)
86 if routers is not None:
87 for router in routers:
88 dic_routers.update({router['id']: router['name']})
89 return {'routers': dic_routers}
92 def get_security_groups(neutron_client):
93 logger.debug("Getting Security groups...")
95 secgroups = os_utils.get_security_groups(neutron_client)
96 if not (secgroups is None or len(secgroups) == 0):
97 for secgroup in secgroups:
98 dic_secgroups.update({secgroup['id']: secgroup['name']})
99 return {'secgroups': dic_secgroups}
102 def get_floatinips(nova_client):
103 logger.debug("Getting Floating IPs...")
105 floatingips = os_utils.get_floating_ips(nova_client)
106 if not (floatingips is None or len(floatingips) == 0):
107 for floatingip in floatingips:
108 dic_floatingips.update({floatingip.id: floatingip.ip})
109 return {'floatingips': dic_floatingips}
112 def get_users(keystone_client):
113 logger.debug("Getting users...")
115 users = os_utils.get_users(keystone_client)
116 if not (users is None or len(users) == 0):
118 dic_users.update({getattr(user, 'id'): getattr(user, 'name')})
119 return {'users': dic_users}
122 def get_tenants(keystone_client):
123 logger.debug("Getting tenants...")
125 tenants = os_utils.get_tenants(keystone_client)
126 if not (tenants is None or len(tenants) == 0):
127 for tenant in tenants:
128 dic_tenants.update({getattr(tenant, 'id'):
129 getattr(tenant, 'name')})
130 return {'tenants': dic_tenants}
133 def get_tacker_vnfds(tacker_client):
134 logger.debug("Getting Tacker VNFDs...")
136 vnfds = os_tacker.list_vnfds(tacker_client, verbose=True)['vnfds']
137 if not (vnfds is None or len(vnfds) == 0):
139 dic_vnfds.update({vnfd['id']:
141 return {'vnfds': dic_vnfds}
144 def get_tacker_vnfs(tacker_client):
145 logger.debug("Getting Tacker VNFs...")
147 vnfs = os_tacker.list_vnfs(tacker_client, verbose=True)['vnfs']
148 if not (vnfs is None or len(vnfs) == 0):
150 dic_vnfs.update({vnf['id']:
152 return {'vnfs': dic_vnfs}
155 def get_tacker_sfcs(tacker_client):
156 logger.debug("Getting Tacker SFCs...")
158 sfcs = os_tacker.list_sfcs(tacker_client, verbose=True)['sfcs']
159 if not (sfcs is None or len(sfcs) == 0):
161 dic_sfcs.update({sfc['id']:
163 return {'sfcs': dic_sfcs}
166 def get_tacker_sfc_classifiers(tacker_client):
167 logger.debug("Getting Tacker SFC classifiers...")
169 sfc_clfs = os_tacker.list_sfc_clasifiers(
170 tacker_client, verbose=True)['sfc_classifiers']
171 if not (sfc_clfs is None or len(sfc_clfs) == 0):
172 for sfc_clf in sfc_clfs:
173 dic_sfc_clfs.update({sfc_clf['id']:
175 return {'sfc_classifiers': dic_sfc_clfs}
179 logger.info("Generating OpenStack snapshot...")
181 nova_client = os_utils.get_nova_client()
182 neutron_client = os_utils.get_neutron_client()
183 keystone_client = os_utils.get_keystone_client()
184 cinder_client = os_utils.get_cinder_client()
185 tacker_client = os_tacker.get_tacker_client()
187 if not os_utils.check_credentials():
188 logger.error("Please source the openrc credentials and run the" +
193 snapshot.update(get_instances(nova_client))
194 snapshot.update(get_images(nova_client))
195 snapshot.update(get_volumes(cinder_client))
196 snapshot.update(get_networks(neutron_client))
197 snapshot.update(get_routers(neutron_client))
198 snapshot.update(get_security_groups(neutron_client))
199 snapshot.update(get_floatinips(nova_client))
200 snapshot.update(get_users(keystone_client))
201 snapshot.update(get_tenants(keystone_client))
202 snapshot.update(get_tacker_vnfds(tacker_client))
203 snapshot.update(get_tacker_vnfs(tacker_client))
204 snapshot.update(get_tacker_sfcs(tacker_client))
205 snapshot.update(get_tacker_sfc_classifiers(tacker_client))
207 with open(OS_SNAPSHOT_FILE, 'w+') as yaml_file:
208 yaml_file.write(yaml.safe_dump(snapshot, default_flow_style=False))
210 logger.debug("Openstack Snapshot found in the deployment:\n%s"
212 logger.debug("NOTE: These objects will NOT be deleted after " +
216 if __name__ == '__main__':