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.openstack_utils as os_utils
26 import functest.utils.functest_constants as ft_constants
28 logger = ft_logger.Logger("openstack_snapshot").getLogger()
31 OS_SNAPSHOT_FILE = ft_constants.OPENSTACK_SNAPSHOT_FILE
35 logger.info("-------------------------------------------")
38 def get_instances(nova_client):
39 logger.debug("Getting instances...")
41 instances = os_utils.get_instances(nova_client)
42 if not (instances is None or len(instances) == 0):
43 for instance in instances:
44 dic_instances.update({getattr(instance, 'id'): getattr(instance,
46 return {'instances': dic_instances}
49 def get_images(nova_client):
50 logger.debug("Getting images...")
52 images = os_utils.get_images(nova_client)
53 if not (images is None or len(images) == 0):
55 dic_images.update({getattr(image, 'id'): getattr(image, 'name')})
56 return {'images': dic_images}
59 def get_volumes(cinder_client):
60 logger.debug("Getting volumes...")
62 volumes = os_utils.get_volumes(cinder_client)
63 if volumes is not None:
64 for volume in volumes:
65 dic_volumes.update({volume.id: volume.name})
66 return {'volumes': dic_volumes}
69 def get_networks(neutron_client):
70 logger.debug("Getting networks")
72 networks = os_utils.get_network_list(neutron_client)
73 if networks is not None:
74 for network in networks:
75 dic_networks.update({network['id']: network['name']})
76 return {'networks': dic_networks}
79 def get_routers(neutron_client):
80 logger.debug("Getting routers")
82 routers = os_utils.get_router_list(neutron_client)
83 if routers is not None:
84 for router in routers:
85 dic_routers.update({router['id']: router['name']})
86 return {'routers': dic_routers}
89 def get_security_groups(neutron_client):
90 logger.debug("Getting Security groups...")
92 secgroups = os_utils.get_security_groups(neutron_client)
93 if not (secgroups is None or len(secgroups) == 0):
94 for secgroup in secgroups:
95 dic_secgroups.update({secgroup['id']: secgroup['name']})
96 return {'secgroups': dic_secgroups}
99 def get_floatinips(nova_client):
100 logger.debug("Getting Floating IPs...")
102 floatingips = os_utils.get_floating_ips(nova_client)
103 if not (floatingips is None or len(floatingips) == 0):
104 for floatingip in floatingips:
105 dic_floatingips.update({floatingip.id: floatingip.ip})
106 return {'floatingips': dic_floatingips}
109 def get_users(keystone_client):
110 logger.debug("Getting users...")
112 users = os_utils.get_users(keystone_client)
113 if not (users is None or len(users) == 0):
115 dic_users.update({getattr(user, 'id'): getattr(user, 'name')})
116 return {'users': dic_users}
119 def get_tenants(keystone_client):
120 logger.debug("Getting tenants...")
122 tenants = os_utils.get_tenants(keystone_client)
123 if not (tenants is None or len(tenants) == 0):
124 for tenant in tenants:
125 dic_tenants.update({getattr(tenant, 'id'):
126 getattr(tenant, 'name')})
127 return {'tenants': dic_tenants}
131 logger.info("Generating OpenStack snapshot...")
133 nova_client = os_utils.get_nova_client()
134 neutron_client = os_utils.get_neutron_client()
135 keystone_client = os_utils.get_keystone_client()
136 cinder_client = os_utils.get_cinder_client()
138 if not os_utils.check_credentials():
139 logger.error("Please source the openrc credentials and run the" +
144 snapshot.update(get_instances(nova_client))
145 snapshot.update(get_images(nova_client))
146 snapshot.update(get_volumes(cinder_client))
147 snapshot.update(get_networks(neutron_client))
148 snapshot.update(get_routers(neutron_client))
149 snapshot.update(get_security_groups(neutron_client))
150 snapshot.update(get_floatinips(nova_client))
151 snapshot.update(get_users(keystone_client))
152 # snapshot.update(get_tenants(keystone_client))
154 with open(OS_SNAPSHOT_FILE, 'w+') as yaml_file:
155 yaml_file.write(yaml.safe_dump(snapshot, default_flow_style=False))
157 logger.debug("Openstack Snapshot found in the deployment:\n%s"
159 logger.debug("NOTE: These objects will NOT be deleted after " +
163 if __name__ == '__main__':