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
25 import functest.utils.functest_logger as ft_logger
26 import functest.utils.openstack_utils as os_utils
27 from functest.utils.constants import CONST
29 logger = ft_logger.Logger("openstack_snapshot").getLogger()
32 OS_SNAPSHOT_FILE = CONST.openstack_snapshot_file
36 logger.info("-------------------------------------------")
39 def get_instances(nova_client):
40 logger.debug("Getting instances...")
42 instances = os_utils.get_instances(nova_client)
43 if not (instances is None or len(instances) == 0):
44 for instance in instances:
45 dic_instances.update({getattr(instance, 'id'): getattr(instance,
47 return {'instances': dic_instances}
50 def get_images(nova_client):
51 logger.debug("Getting images...")
53 images = os_utils.get_images(nova_client)
54 if not (images is None or len(images) == 0):
56 dic_images.update({getattr(image, 'id'): getattr(image, 'name')})
57 return {'images': dic_images}
60 def get_volumes(cinder_client):
61 logger.debug("Getting volumes...")
63 volumes = os_utils.get_volumes(cinder_client)
64 if volumes is not None:
65 for volume in volumes:
66 dic_volumes.update({volume.id: volume.name})
67 return {'volumes': dic_volumes}
70 def get_networks(neutron_client):
71 logger.debug("Getting networks")
73 networks = os_utils.get_network_list(neutron_client)
74 if networks is not None:
75 for network in networks:
76 dic_networks.update({network['id']: network['name']})
77 return {'networks': dic_networks}
80 def get_routers(neutron_client):
81 logger.debug("Getting routers")
83 routers = os_utils.get_router_list(neutron_client)
84 if routers is not None:
85 for router in routers:
86 dic_routers.update({router['id']: router['name']})
87 return {'routers': dic_routers}
90 def get_security_groups(neutron_client):
91 logger.debug("Getting Security groups...")
93 secgroups = os_utils.get_security_groups(neutron_client)
94 if not (secgroups is None or len(secgroups) == 0):
95 for secgroup in secgroups:
96 dic_secgroups.update({secgroup['id']: secgroup['name']})
97 return {'secgroups': dic_secgroups}
100 def get_floatinips(nova_client):
101 logger.debug("Getting Floating IPs...")
103 floatingips = os_utils.get_floating_ips(nova_client)
104 if not (floatingips is None or len(floatingips) == 0):
105 for floatingip in floatingips:
106 dic_floatingips.update({floatingip.id: floatingip.ip})
107 return {'floatingips': dic_floatingips}
110 def get_users(keystone_client):
111 logger.debug("Getting users...")
113 users = os_utils.get_users(keystone_client)
114 if not (users is None or len(users) == 0):
116 dic_users.update({getattr(user, 'id'): getattr(user, 'name')})
117 return {'users': dic_users}
120 def get_tenants(keystone_client):
121 logger.debug("Getting tenants...")
123 tenants = os_utils.get_tenants(keystone_client)
124 if not (tenants is None or len(tenants) == 0):
125 for tenant in tenants:
126 dic_tenants.update({getattr(tenant, 'id'):
127 getattr(tenant, 'name')})
128 return {'tenants': dic_tenants}
132 logger.info("Generating OpenStack snapshot...")
134 nova_client = os_utils.get_nova_client()
135 neutron_client = os_utils.get_neutron_client()
136 keystone_client = os_utils.get_keystone_client()
137 cinder_client = os_utils.get_cinder_client()
139 if not os_utils.check_credentials():
140 logger.error("Please source the openrc credentials and run the" +
145 snapshot.update(get_instances(nova_client))
146 snapshot.update(get_images(nova_client))
147 snapshot.update(get_volumes(cinder_client))
148 snapshot.update(get_networks(neutron_client))
149 snapshot.update(get_routers(neutron_client))
150 snapshot.update(get_security_groups(neutron_client))
151 snapshot.update(get_floatinips(nova_client))
152 snapshot.update(get_users(keystone_client))
153 snapshot.update(get_tenants(keystone_client))
155 with open(OS_SNAPSHOT_FILE, 'w+') as yaml_file:
156 yaml_file.write(yaml.safe_dump(snapshot, default_flow_style=False))
158 logger.debug("Openstack Snapshot found in the deployment:\n%s"
160 logger.debug("NOTE: These objects will NOT be deleted after " +
164 if __name__ == '__main__':