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
24 import functest.utils.functest_logger as ft_logger
25 import functest.utils.functest_utils as ft_utils
26 import functest.utils.openstack_utils as os_utils
30 logger = ft_logger.Logger("openstack_snapshot").getLogger()
32 REPO_PATH = os.environ['repos_dir'] + '/functest/'
33 if not os.path.exists(REPO_PATH):
34 logger.error("Functest repository directory not found '%s'" % REPO_PATH)
38 OS_SNAPSHOT_FILE = ft_utils.get_parameter_from_yaml(
39 "general.openstack.snapshot_file")
43 logger.info("-------------------------------------------")
46 def get_instances(nova_client):
47 logger.debug("Getting instances...")
49 instances = os_utils.get_instances(nova_client)
50 if not (instances is None or len(instances) == 0):
51 for instance in instances:
52 dic_instances.update({getattr(instance, 'id'): getattr(instance,
54 return {'instances': dic_instances}
57 def get_images(nova_client):
58 logger.debug("Getting images...")
60 images = os_utils.get_images(nova_client)
61 if not (images is None or len(images) == 0):
63 dic_images.update({getattr(image, 'id'): getattr(image, 'name')})
64 return {'images': dic_images}
67 def get_volumes(cinder_client):
68 logger.debug("Getting volumes...")
70 volumes = os_utils.get_volumes(cinder_client)
71 if volumes is not None:
72 for volume in volumes:
73 dic_volumes.update({volume.id: volume.display_name})
74 return {'volumes': dic_volumes}
77 def get_networks(neutron_client):
78 logger.debug("Getting networks")
80 networks = os_utils.get_network_list(neutron_client)
81 if networks is not None:
82 for network in networks:
83 dic_networks.update({network['id']: network['name']})
84 return {'networks': dic_networks}
87 def get_routers(neutron_client):
88 logger.debug("Getting routers")
90 routers = os_utils.get_router_list(neutron_client)
91 if routers is not None:
92 for router in routers:
93 dic_routers.update({router['id']: router['name']})
94 return {'routers': dic_routers}
97 def get_security_groups(neutron_client):
98 logger.debug("Getting Security groups...")
100 secgroups = os_utils.get_security_groups(neutron_client)
101 if not (secgroups is None or len(secgroups) == 0):
102 for secgroup in secgroups:
103 dic_secgroups.update({secgroup['id']: secgroup['name']})
104 return {'secgroups': dic_secgroups}
107 def get_floatinips(nova_client):
108 logger.debug("Getting Floating IPs...")
110 floatingips = os_utils.get_floating_ips(nova_client)
111 if not (floatingips is None or len(floatingips) == 0):
112 for floatingip in floatingips:
113 dic_floatingips.update({floatingip.id: floatingip.ip})
114 return {'floatingips': dic_floatingips}
117 def get_users(keystone_client):
118 logger.debug("Getting users...")
120 users = os_utils.get_users(keystone_client)
121 if not (users is None or len(users) == 0):
123 dic_users.update({getattr(user, 'id'): getattr(user, 'name')})
124 return {'users': dic_users}
127 def get_tenants(keystone_client):
128 logger.debug("Getting tenants...")
130 tenants = os_utils.get_tenants(keystone_client)
131 if not (tenants is None or len(tenants) == 0):
132 for tenant in tenants:
133 dic_tenants.update({getattr(tenant, 'id'):
134 getattr(tenant, 'name')})
135 return {'tenants': dic_tenants}
139 logger.info("Generating OpenStack snapshot...")
141 nova_client = os_utils.get_nova_client()
142 neutron_client = os_utils.get_neutron_client()
143 keystone_client = os_utils.get_keystone_client()
144 cinder_client = os_utils.get_cinder_client()
146 if not os_utils.check_credentials():
147 logger.error("Please source the openrc credentials and run the" +
152 snapshot.update(get_instances(nova_client))
153 snapshot.update(get_images(nova_client))
154 snapshot.update(get_volumes(cinder_client))
155 snapshot.update(get_networks(neutron_client))
156 snapshot.update(get_routers(neutron_client))
157 snapshot.update(get_security_groups(neutron_client))
158 snapshot.update(get_floatinips(nova_client))
159 snapshot.update(get_users(keystone_client))
160 snapshot.update(get_tenants(keystone_client))
162 with open(OS_SNAPSHOT_FILE, 'w+') as yaml_file:
163 yaml_file.write(yaml.safe_dump(snapshot, default_flow_style=False))
165 logger.debug("Openstack Snapshot found in the deployment:\n%s"
167 logger.debug("NOTE: These objects will NOT be deleted after " +
171 if __name__ == '__main__':