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 import functest.utils.functest_logger as ft_logger
24 import functest.utils.functest_utils as ft_utils
25 import functest.utils.openstack_utils as os_utils
29 logger = ft_logger.Logger("openstack_snapshot").getLogger()
32 OS_SNAPSHOT_FILE = ft_utils.get_parameter_from_yaml(
33 "general.openstack.snapshot_file")
37 logger.info("-------------------------------------------")
40 def get_instances(nova_client):
41 logger.debug("Getting instances...")
43 instances = os_utils.get_instances(nova_client)
44 if not (instances is None or len(instances) == 0):
45 for instance in instances:
46 dic_instances.update({getattr(instance, 'id'): getattr(instance,
48 return {'instances': dic_instances}
51 def get_images(nova_client):
52 logger.debug("Getting images...")
54 images = os_utils.get_images(nova_client)
55 if not (images is None or len(images) == 0):
57 dic_images.update({getattr(image, 'id'): getattr(image, 'name')})
58 return {'images': dic_images}
61 def get_volumes(cinder_client):
62 logger.debug("Getting volumes...")
64 volumes = os_utils.get_volumes(cinder_client)
65 if volumes is not None:
66 for volume in volumes:
67 dic_volumes.update({volume.id: volume.display_name})
68 return {'volumes': dic_volumes}
71 def get_networks(neutron_client):
72 logger.debug("Getting networks")
74 networks = os_utils.get_network_list(neutron_client)
75 if networks is not None:
76 for network in networks:
77 dic_networks.update({network['id']: network['name']})
78 return {'networks': dic_networks}
81 def get_routers(neutron_client):
82 logger.debug("Getting routers")
84 routers = os_utils.get_router_list(neutron_client)
85 if routers is not None:
86 for router in routers:
87 dic_routers.update({router['id']: router['name']})
88 return {'routers': dic_routers}
91 def get_security_groups(neutron_client):
92 logger.debug("Getting Security groups...")
94 secgroups = os_utils.get_security_groups(neutron_client)
95 if not (secgroups is None or len(secgroups) == 0):
96 for secgroup in secgroups:
97 dic_secgroups.update({secgroup['id']: secgroup['name']})
98 return {'secgroups': dic_secgroups}
101 def get_floatinips(nova_client):
102 logger.debug("Getting Floating IPs...")
104 floatingips = os_utils.get_floating_ips(nova_client)
105 if not (floatingips is None or len(floatingips) == 0):
106 for floatingip in floatingips:
107 dic_floatingips.update({floatingip.id: floatingip.ip})
108 return {'floatingips': dic_floatingips}
111 def get_users(keystone_client):
112 logger.debug("Getting users...")
114 users = os_utils.get_users(keystone_client)
115 if not (users is None or len(users) == 0):
117 dic_users.update({getattr(user, 'id'): getattr(user, 'name')})
118 return {'users': dic_users}
121 def get_tenants(keystone_client):
122 logger.debug("Getting tenants...")
124 tenants = os_utils.get_tenants(keystone_client)
125 if not (tenants is None or len(tenants) == 0):
126 for tenant in tenants:
127 dic_tenants.update({getattr(tenant, 'id'):
128 getattr(tenant, 'name')})
129 return {'tenants': dic_tenants}
133 logger.info("Generating OpenStack snapshot...")
135 nova_client = os_utils.get_nova_client()
136 neutron_client = os_utils.get_neutron_client()
137 keystone_client = os_utils.get_keystone_client()
138 cinder_client = os_utils.get_cinder_client()
140 if not os_utils.check_credentials():
141 logger.error("Please source the openrc credentials and run the" +
146 snapshot.update(get_instances(nova_client))
147 snapshot.update(get_images(nova_client))
148 snapshot.update(get_volumes(cinder_client))
149 snapshot.update(get_networks(neutron_client))
150 snapshot.update(get_routers(neutron_client))
151 snapshot.update(get_security_groups(neutron_client))
152 snapshot.update(get_floatinips(nova_client))
153 snapshot.update(get_users(keystone_client))
154 snapshot.update(get_tenants(keystone_client))
156 with open(OS_SNAPSHOT_FILE, 'w+') as yaml_file:
157 yaml_file.write(yaml.safe_dump(snapshot, default_flow_style=False))
159 logger.debug("Openstack Snapshot found in the deployment:\n%s"
161 logger.debug("NOTE: These objects will NOT be deleted after " +
165 if __name__ == '__main__':