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