Merge "Restore Kingbird functest"
[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 yaml
24
25 import functest.utils.functest_logger as ft_logger
26 import functest.utils.openstack_utils as os_utils
27 from functest.utils.constants import CONST
28
29 logger = ft_logger.Logger("openstack_snapshot").getLogger()
30
31
32 OS_SNAPSHOT_FILE = CONST.openstack_snapshot_file
33
34
35 def separator():
36     logger.info("-------------------------------------------")
37
38
39 def get_instances(nova_client):
40     logger.debug("Getting instances...")
41     dic_instances = {}
42     instances = os_utils.get_instances(nova_client)
43     if not (instances is None or len(instances) == 0):
44         for instance in instances:
45             dic_instances.update({getattr(instance, 'id'): getattr(instance,
46                                                                    'name')})
47     return {'instances': dic_instances}
48
49
50 def get_images(nova_client):
51     logger.debug("Getting images...")
52     dic_images = {}
53     images = os_utils.get_images(nova_client)
54     if not (images is None or len(images) == 0):
55         for image in images:
56             dic_images.update({getattr(image, 'id'): getattr(image, 'name')})
57     return {'images': dic_images}
58
59
60 def get_volumes(cinder_client):
61     logger.debug("Getting volumes...")
62     dic_volumes = {}
63     volumes = os_utils.get_volumes(cinder_client)
64     if volumes is not None:
65         for volume in volumes:
66             dic_volumes.update({volume.id: volume.name})
67     return {'volumes': dic_volumes}
68
69
70 def get_networks(neutron_client):
71     logger.debug("Getting networks")
72     dic_networks = {}
73     networks = os_utils.get_network_list(neutron_client)
74     if networks is not None:
75         for network in networks:
76             dic_networks.update({network['id']: network['name']})
77     return {'networks': dic_networks}
78
79
80 def get_routers(neutron_client):
81     logger.debug("Getting routers")
82     dic_routers = {}
83     routers = os_utils.get_router_list(neutron_client)
84     if routers is not None:
85         for router in routers:
86             dic_routers.update({router['id']: router['name']})
87     return {'routers': dic_routers}
88
89
90 def get_security_groups(neutron_client):
91     logger.debug("Getting Security groups...")
92     dic_secgroups = {}
93     secgroups = os_utils.get_security_groups(neutron_client)
94     if not (secgroups is None or len(secgroups) == 0):
95         for secgroup in secgroups:
96             dic_secgroups.update({secgroup['id']: secgroup['name']})
97     return {'secgroups': dic_secgroups}
98
99
100 def get_floatinips(nova_client):
101     logger.debug("Getting Floating IPs...")
102     dic_floatingips = {}
103     floatingips = os_utils.get_floating_ips(nova_client)
104     if not (floatingips is None or len(floatingips) == 0):
105         for floatingip in floatingips:
106             dic_floatingips.update({floatingip.id: floatingip.ip})
107     return {'floatingips': dic_floatingips}
108
109
110 def get_users(keystone_client):
111     logger.debug("Getting users...")
112     dic_users = {}
113     users = os_utils.get_users(keystone_client)
114     if not (users is None or len(users) == 0):
115         for user in users:
116             dic_users.update({getattr(user, 'id'): getattr(user, 'name')})
117     return {'users': dic_users}
118
119
120 def get_tenants(keystone_client):
121     logger.debug("Getting tenants...")
122     dic_tenants = {}
123     tenants = os_utils.get_tenants(keystone_client)
124     if not (tenants is None or len(tenants) == 0):
125         for tenant in tenants:
126             dic_tenants.update({getattr(tenant, 'id'):
127                                 getattr(tenant, 'name')})
128     return {'tenants': dic_tenants}
129
130
131 def main():
132     logger.info("Generating OpenStack snapshot...")
133
134     nova_client = os_utils.get_nova_client()
135     neutron_client = os_utils.get_neutron_client()
136     keystone_client = os_utils.get_keystone_client()
137     cinder_client = os_utils.get_cinder_client()
138
139     if not os_utils.check_credentials():
140         logger.error("Please source the openrc credentials and run the" +
141                      "script again.")
142         exit(-1)
143
144     snapshot = {}
145     snapshot.update(get_instances(nova_client))
146     snapshot.update(get_images(nova_client))
147     snapshot.update(get_volumes(cinder_client))
148     snapshot.update(get_networks(neutron_client))
149     snapshot.update(get_routers(neutron_client))
150     snapshot.update(get_security_groups(neutron_client))
151     snapshot.update(get_floatinips(nova_client))
152     snapshot.update(get_users(keystone_client))
153     snapshot.update(get_tenants(keystone_client))
154
155     with open(OS_SNAPSHOT_FILE, 'w+') as yaml_file:
156         yaml_file.write(yaml.safe_dump(snapshot, default_flow_style=False))
157         yaml_file.seek(0)
158         logger.debug("Openstack Snapshot found in the deployment:\n%s"
159                      % yaml_file.read())
160         logger.debug("NOTE: These objects will NOT be deleted after " +
161                      "running the test.")
162
163
164 if __name__ == '__main__':
165     main()