3dc6f80c72b73772907084e2b75fc2bc3105420a
[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 logging
24 import yaml
25
26 import functest.utils.openstack_utils as os_utils
27 from functest.utils.constants import CONST
28
29 logger = logging.getLogger(__name__)
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(glance_client):
51     logger.debug("Getting images...")
52     dic_images = {}
53     images = os_utils.get_images(glance_client)
54     if images is None:
55         return -1
56     dic_images.update({image.id: image.name for image in images})
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_floatingips(neutron_client):
101     logger.debug("Getting Floating IPs...")
102     dic_floatingips = {}
103     floatingips = os_utils.get_floating_ips(neutron_client)
104     if not (floatingips is None or len(floatingips) == 0):
105         for floatingip in floatingips:
106             dic_floatingips.update({floatingip['id']:
107                                     floatingip['floating_ip_address']})
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     logging.basicConfig()
134     logger.info("Generating OpenStack snapshot...")
135
136     nova_client = os_utils.get_nova_client()
137     neutron_client = os_utils.get_neutron_client()
138     keystone_client = os_utils.get_keystone_client()
139     cinder_client = os_utils.get_cinder_client()
140     glance_client = os_utils.get_glance_client()
141
142     if not os_utils.check_credentials():
143         logger.error("Please source the openrc credentials and run the" +
144                      "script again.")
145         return -1
146
147     snapshot = {}
148     snapshot.update(get_instances(nova_client))
149     snapshot.update(get_images(glance_client))
150     snapshot.update(get_volumes(cinder_client))
151     snapshot.update(get_networks(neutron_client))
152     snapshot.update(get_routers(neutron_client))
153     snapshot.update(get_security_groups(neutron_client))
154     snapshot.update(get_floatingips(neutron_client))
155     snapshot.update(get_users(keystone_client))
156     snapshot.update(get_tenants(keystone_client))
157
158     with open(OS_SNAPSHOT_FILE, 'w+') as yaml_file:
159         yaml_file.write(yaml.safe_dump(snapshot, default_flow_style=False))
160         yaml_file.seek(0)
161         logger.debug("Openstack Snapshot found in the deployment:\n%s"
162                      % yaml_file.read())
163         logger.debug("NOTE: These objects will NOT be deleted after " +
164                      "running the test.")
165     return 0