Extracted all global parameters into functest_constants.py
[functest.git] / functest / 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 functest.utils.functest_logger as ft_logger
24 import functest.utils.openstack_utils as os_utils
25 import yaml
26 import functest.utils.functest_constants as ft_constants
27
28 logger = ft_logger.Logger("openstack_snapshot").getLogger()
29
30
31 OS_SNAPSHOT_FILE = ft_constants.OPENSTACK_SNAPSHOT_FILE
32
33
34 def separator():
35     logger.info("-------------------------------------------")
36
37
38 def get_instances(nova_client):
39     logger.debug("Getting instances...")
40     dic_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,
45                                                                    'name')})
46     return {'instances': dic_instances}
47
48
49 def get_images(nova_client):
50     logger.debug("Getting images...")
51     dic_images = {}
52     images = os_utils.get_images(nova_client)
53     if not (images is None or len(images) == 0):
54         for image in images:
55             dic_images.update({getattr(image, 'id'): getattr(image, 'name')})
56     return {'images': dic_images}
57
58
59 def get_volumes(cinder_client):
60     logger.debug("Getting volumes...")
61     dic_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.display_name})
66     return {'volumes': dic_volumes}
67
68
69 def get_networks(neutron_client):
70     logger.debug("Getting networks")
71     dic_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}
77
78
79 def get_routers(neutron_client):
80     logger.debug("Getting routers")
81     dic_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}
87
88
89 def get_security_groups(neutron_client):
90     logger.debug("Getting Security groups...")
91     dic_secgroups = {}
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}
97
98
99 def get_floatinips(nova_client):
100     logger.debug("Getting Floating IPs...")
101     dic_floatingips = {}
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}
107
108
109 def get_users(keystone_client):
110     logger.debug("Getting users...")
111     dic_users = {}
112     users = os_utils.get_users(keystone_client)
113     if not (users is None or len(users) == 0):
114         for user in users:
115             dic_users.update({getattr(user, 'id'): getattr(user, 'name')})
116     return {'users': dic_users}
117
118
119 def get_tenants(keystone_client):
120     logger.debug("Getting tenants...")
121     dic_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}
128
129
130 def main():
131     logger.info("Generating OpenStack snapshot...")
132
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()
137
138     if not os_utils.check_credentials():
139         logger.error("Please source the openrc credentials and run the" +
140                      "script again.")
141         exit(-1)
142
143     snapshot = {}
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))
153
154     with open(OS_SNAPSHOT_FILE, 'w+') as yaml_file:
155         yaml_file.write(yaml.safe_dump(snapshot, default_flow_style=False))
156         yaml_file.seek(0)
157         logger.debug("Openstack Snapshot found in the deployment:\n%s"
158                      % yaml_file.read())
159         logger.debug("NOTE: These objects will NOT be deleted after " +
160                      "running the test.")
161
162
163 if __name__ == '__main__':
164     main()