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