d6e7fe0069feedd344101fe43fc04d640b8d161f
[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 #       - Tacker VNFDs and VNFs
13 #       - Tacker SFCs and SFC classifiers
14 #
15 # Author:
16 #    jose.lausuch@ericsson.com
17 #
18 #
19 # All rights reserved. This program and the accompanying materials
20 # are made available under the terms of the Apache License, Version 2.0
21 # which accompanies this distribution, and is available at
22 # http://www.apache.org/licenses/LICENSE-2.0
23 #
24
25 import functest.utils.functest_logger as ft_logger
26 import functest.utils.openstack_utils as os_utils
27 import functest.utils.openstack_tacker as os_tacker
28 import yaml
29 import functest.utils.functest_constants as ft_constants
30
31 logger = ft_logger.Logger("openstack_snapshot").getLogger()
32
33
34 OS_SNAPSHOT_FILE = ft_constants.OPENSTACK_SNAPSHOT_FILE
35
36
37 def separator():
38     logger.info("-------------------------------------------")
39
40
41 def get_instances(nova_client):
42     logger.debug("Getting instances...")
43     dic_instances = {}
44     instances = os_utils.get_instances(nova_client)
45     if not (instances is None or len(instances) == 0):
46         for instance in instances:
47             dic_instances.update({getattr(instance, 'id'): getattr(instance,
48                                                                    'name')})
49     return {'instances': dic_instances}
50
51
52 def get_images(nova_client):
53     logger.debug("Getting images...")
54     dic_images = {}
55     images = os_utils.get_images(nova_client)
56     if not (images is None or len(images) == 0):
57         for image in images:
58             dic_images.update({getattr(image, 'id'): getattr(image, 'name')})
59     return {'images': dic_images}
60
61
62 def get_volumes(cinder_client):
63     logger.debug("Getting volumes...")
64     dic_volumes = {}
65     volumes = os_utils.get_volumes(cinder_client)
66     if volumes is not None:
67         for volume in volumes:
68             dic_volumes.update({volume.id: volume.display_name})
69     return {'volumes': dic_volumes}
70
71
72 def get_networks(neutron_client):
73     logger.debug("Getting networks")
74     dic_networks = {}
75     networks = os_utils.get_network_list(neutron_client)
76     if networks is not None:
77         for network in networks:
78             dic_networks.update({network['id']: network['name']})
79     return {'networks': dic_networks}
80
81
82 def get_routers(neutron_client):
83     logger.debug("Getting routers")
84     dic_routers = {}
85     routers = os_utils.get_router_list(neutron_client)
86     if routers is not None:
87         for router in routers:
88             dic_routers.update({router['id']: router['name']})
89     return {'routers': dic_routers}
90
91
92 def get_security_groups(neutron_client):
93     logger.debug("Getting Security groups...")
94     dic_secgroups = {}
95     secgroups = os_utils.get_security_groups(neutron_client)
96     if not (secgroups is None or len(secgroups) == 0):
97         for secgroup in secgroups:
98             dic_secgroups.update({secgroup['id']: secgroup['name']})
99     return {'secgroups': dic_secgroups}
100
101
102 def get_floatinips(nova_client):
103     logger.debug("Getting Floating IPs...")
104     dic_floatingips = {}
105     floatingips = os_utils.get_floating_ips(nova_client)
106     if not (floatingips is None or len(floatingips) == 0):
107         for floatingip in floatingips:
108             dic_floatingips.update({floatingip.id: floatingip.ip})
109     return {'floatingips': dic_floatingips}
110
111
112 def get_users(keystone_client):
113     logger.debug("Getting users...")
114     dic_users = {}
115     users = os_utils.get_users(keystone_client)
116     if not (users is None or len(users) == 0):
117         for user in users:
118             dic_users.update({getattr(user, 'id'): getattr(user, 'name')})
119     return {'users': dic_users}
120
121
122 def get_tenants(keystone_client):
123     logger.debug("Getting tenants...")
124     dic_tenants = {}
125     tenants = os_utils.get_tenants(keystone_client)
126     if not (tenants is None or len(tenants) == 0):
127         for tenant in tenants:
128             dic_tenants.update({getattr(tenant, 'id'):
129                                 getattr(tenant, 'name')})
130     return {'tenants': dic_tenants}
131
132
133 def get_tacker_vnfds(tacker_client):
134     logger.debug("Getting Tacker VNFDs...")
135     dic_vnfds = {}
136     vnfds = os_tacker.list_vnfds(tacker_client, verbose=True)['vnfds']
137     if not (vnfds is None or len(vnfds) == 0):
138         for vnfd in vnfds:
139             dic_vnfds.update({vnfd['id']:
140                               vnfd['name']})
141     return {'vnfds': dic_vnfds}
142
143
144 def get_tacker_vnfs(tacker_client):
145     logger.debug("Getting Tacker VNFs...")
146     dic_vnfs = {}
147     vnfs = os_tacker.list_vnfs(tacker_client, verbose=True)['vnfs']
148     if not (vnfs is None or len(vnfs) == 0):
149         for vnf in vnfs:
150             dic_vnfs.update({vnf['id']:
151                              vnf['name']})
152     return {'vnfs': dic_vnfs}
153
154
155 def get_tacker_sfcs(tacker_client):
156     logger.debug("Getting Tacker SFCs...")
157     dic_sfcs = {}
158     sfcs = os_tacker.list_sfcs(tacker_client, verbose=True)['sfcs']
159     if not (sfcs is None or len(sfcs) == 0):
160         for sfc in sfcs:
161             dic_sfcs.update({sfc['id']:
162                              sfc['name']})
163     return {'sfcs': dic_sfcs}
164
165
166 def get_tacker_sfc_classifiers(tacker_client):
167     logger.debug("Getting Tacker SFC classifiers...")
168     dic_sfc_clfs = {}
169     sfc_clfs = os_tacker.list_sfc_clasifiers(
170         tacker_client, verbose=True)['sfc_classifiers']
171     if not (sfc_clfs is None or len(sfc_clfs) == 0):
172         for sfc_clf in sfc_clfs:
173             dic_sfc_clfs.update({sfc_clf['id']:
174                                  sfc_clf['name']})
175     return {'sfc_classifiers': dic_sfc_clfs}
176
177
178 def main():
179     logger.info("Generating OpenStack snapshot...")
180
181     nova_client = os_utils.get_nova_client()
182     neutron_client = os_utils.get_neutron_client()
183     keystone_client = os_utils.get_keystone_client()
184     cinder_client = os_utils.get_cinder_client()
185     tacker_client = os_tacker.get_tacker_client()
186
187     if not os_utils.check_credentials():
188         logger.error("Please source the openrc credentials and run the" +
189                      "script again.")
190         exit(-1)
191
192     snapshot = {}
193     snapshot.update(get_instances(nova_client))
194     snapshot.update(get_images(nova_client))
195     snapshot.update(get_volumes(cinder_client))
196     snapshot.update(get_networks(neutron_client))
197     snapshot.update(get_routers(neutron_client))
198     snapshot.update(get_security_groups(neutron_client))
199     snapshot.update(get_floatinips(nova_client))
200     snapshot.update(get_users(keystone_client))
201     snapshot.update(get_tenants(keystone_client))
202     snapshot.update(get_tacker_vnfds(tacker_client))
203     snapshot.update(get_tacker_vnfs(tacker_client))
204     snapshot.update(get_tacker_sfcs(tacker_client))
205     snapshot.update(get_tacker_sfc_classifiers(tacker_client))
206
207     with open(OS_SNAPSHOT_FILE, 'w+') as yaml_file:
208         yaml_file.write(yaml.safe_dump(snapshot, default_flow_style=False))
209         yaml_file.seek(0)
210         logger.debug("Openstack Snapshot found in the deployment:\n%s"
211                      % yaml_file.read())
212         logger.debug("NOTE: These objects will NOT be deleted after " +
213                      "running the test.")
214
215
216 if __name__ == '__main__':
217     main()