Merge "Remove useless odlreport2db args"
[functest.git] / utils / openstack_snapshot.py
1 #!/usr/bin/env python
2 #
3 # Description:
4 #  Generates a list of the current Openstack objects in the deployment:
5 #       - Nova instances
6 #       - Glance images
7 #       - Cinder volumes
8 #       - Floating IPs
9 #       - Neutron networks, subnets and ports
10 #       - Routers
11 #       - Users and tenants
12 #
13 # Author:
14 #    jose.lausuch@ericsson.com
15 #
16 #
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
21 #
22
23 import os
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
27 import yaml
28
29
30 logger = ft_logger.Logger("openstack_snapshot").getLogger()
31
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)
35     exit(-1)
36
37
38 OS_SNAPSHOT_FILE = ft_utils.get_parameter_from_yaml(
39     "general.openstack.snapshot_file")
40
41
42 def separator():
43     logger.info("-------------------------------------------")
44
45
46 def get_instances(nova_client):
47     logger.debug("Getting instances...")
48     dic_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,
53                                                                    'name')})
54     return {'instances': dic_instances}
55
56
57 def get_images(nova_client):
58     logger.debug("Getting images...")
59     dic_images = {}
60     images = os_utils.get_images(nova_client)
61     if not (images is None or len(images) == 0):
62         for image in images:
63             dic_images.update({getattr(image, 'id'): getattr(image, 'name')})
64     return {'images': dic_images}
65
66
67 def get_volumes(cinder_client):
68     logger.debug("Getting volumes...")
69     dic_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}
75
76
77 def get_networks(neutron_client):
78     logger.debug("Getting networks")
79     dic_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}
85
86
87 def get_routers(neutron_client):
88     logger.debug("Getting routers")
89     dic_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}
95
96
97 def get_security_groups(neutron_client):
98     logger.debug("Getting Security groups...")
99     dic_secgroups = {}
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}
105
106
107 def get_floatinips(nova_client):
108     logger.debug("Getting Floating IPs...")
109     dic_floatingips = {}
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}
115
116
117 def get_users(keystone_client):
118     logger.debug("Getting users...")
119     dic_users = {}
120     users = os_utils.get_users(keystone_client)
121     if not (users is None or len(users) == 0):
122         for user in users:
123             dic_users.update({getattr(user, 'id'): getattr(user, 'name')})
124     return {'users': dic_users}
125
126
127 def get_tenants(keystone_client):
128     logger.debug("Getting tenants...")
129     dic_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}
136
137
138 def main():
139     logger.info("Generating OpenStack snapshot...")
140
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()
145
146     if not os_utils.check_credentials():
147         logger.error("Please source the openrc credentials and run the" +
148                      "script again.")
149         exit(-1)
150
151     snapshot = {}
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))
161
162     with open(OS_SNAPSHOT_FILE, 'w+') as yaml_file:
163         yaml_file.write(yaml.safe_dump(snapshot, default_flow_style=False))
164         yaml_file.seek(0)
165         logger.debug("Openstack Snapshot found in the deployment:\n%s"
166                      % yaml_file.read())
167         logger.debug("NOTE: These objects will NOT be deleted after " +
168                      "running the test.")
169
170
171 if __name__ == '__main__':
172     main()