blob: b494db6347c318ac3d7bb8ec6bbc9ab11527de4f [file] [log] [blame]
David Kranzb9d97502013-05-01 15:55:04 -04001#!/usr/bin/env python
2
David Kranzb9d97502013-05-01 15:55:04 -04003# Copyright 2013 Quanta Research Cambridge, Inc.
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17from tempest import clients
Marc Kodererb714de52013-08-08 09:21:46 +020018from tempest.openstack.common import log as logging
19
20LOG = logging.getLogger(__name__)
David Kranzb9d97502013-05-01 15:55:04 -040021
22
Marc Kodererb714de52013-08-08 09:21:46 +020023def cleanup():
David Kranzb9d97502013-05-01 15:55:04 -040024 admin_manager = clients.AdminManager()
25
26 _, body = admin_manager.servers_client.list_servers({"all_tenants": True})
Marc Kodererb714de52013-08-08 09:21:46 +020027 LOG.info("Cleanup::remove %s servers" % len(body['servers']))
David Kranzb9d97502013-05-01 15:55:04 -040028 for s in body['servers']:
29 try:
30 admin_manager.servers_client.delete_server(s['id'])
31 except Exception:
32 pass
33
34 for s in body['servers']:
35 try:
36 admin_manager.servers_client.wait_for_server_termination(s['id'])
37 except Exception:
38 pass
39
40 _, keypairs = admin_manager.keypairs_client.list_keypairs()
Marc Kodererb714de52013-08-08 09:21:46 +020041 LOG.info("Cleanup::remove %s keypairs" % len(keypairs))
David Kranzb9d97502013-05-01 15:55:04 -040042 for k in keypairs:
43 try:
44 admin_manager.keypairs_client.delete_keypair(k['name'])
45 except Exception:
46 pass
47
Attila Fazekas9f03f8b2014-02-24 17:41:08 +010048 secgrp_client = admin_manager.security_groups_client
49 _, secgrp = secgrp_client.list_security_groups({"all_tenants": True})
50 secgrp_del = [grp for grp in secgrp if grp['name'] != 'default']
51 LOG.info("Cleanup::remove %s Security Group" % len(secgrp_del))
52 for g in secgrp_del:
53 try:
54 secgrp_client.delete_security_group(g['id'])
55 except Exception:
56 pass
57
David Kranzb9d97502013-05-01 15:55:04 -040058 _, floating_ips = admin_manager.floating_ips_client.list_floating_ips()
Marc Kodererb714de52013-08-08 09:21:46 +020059 LOG.info("Cleanup::remove %s floating ips" % len(floating_ips))
David Kranzb9d97502013-05-01 15:55:04 -040060 for f in floating_ips:
61 try:
62 admin_manager.floating_ips_client.delete_floating_ip(f['id'])
63 except Exception:
64 pass
65
David Kranzb7afa922014-12-30 10:56:26 -050066 users = admin_manager.identity_client.get_users()
Marc Kodererb714de52013-08-08 09:21:46 +020067 LOG.info("Cleanup::remove %s users" % len(users))
David Kranzb9d97502013-05-01 15:55:04 -040068 for user in users:
69 if user['name'].startswith("stress_user"):
70 admin_manager.identity_client.delete_user(user['id'])
71
David Kranzb7afa922014-12-30 10:56:26 -050072 tenants = admin_manager.identity_client.list_tenants()
Marc Kodererb714de52013-08-08 09:21:46 +020073 LOG.info("Cleanup::remove %s tenants" % len(tenants))
David Kranzb9d97502013-05-01 15:55:04 -040074 for tenant in tenants:
75 if tenant['name'].startswith("stress_tenant"):
76 admin_manager.identity_client.delete_tenant(tenant['id'])
Giulio Fidentebbb69e72013-06-18 16:06:24 +020077
Walter A. Boring IVb725e622013-07-11 17:21:33 -070078 # We have to delete snapshots first or
79 # volume deletion may block
80
81 _, snaps = admin_manager.snapshots_client.\
82 list_snapshots({"all_tenants": True})
Marc Kodererb714de52013-08-08 09:21:46 +020083 LOG.info("Cleanup::remove %s snapshots" % len(snaps))
Walter A. Boring IVb725e622013-07-11 17:21:33 -070084 for v in snaps:
85 try:
86 admin_manager.snapshots_client.\
87 wait_for_snapshot_status(v['id'], 'available')
88 admin_manager.snapshots_client.delete_snapshot(v['id'])
89 except Exception:
90 pass
91
92 for v in snaps:
93 try:
94 admin_manager.snapshots_client.wait_for_resource_deletion(v['id'])
95 except Exception:
96 pass
97
Giulio Fidentebbb69e72013-06-18 16:06:24 +020098 _, vols = admin_manager.volumes_client.list_volumes({"all_tenants": True})
Marc Kodererb714de52013-08-08 09:21:46 +020099 LOG.info("Cleanup::remove %s volumes" % len(vols))
Giulio Fidentebbb69e72013-06-18 16:06:24 +0200100 for v in vols:
101 try:
Walter A. Boring IVb725e622013-07-11 17:21:33 -0700102 admin_manager.volumes_client.\
103 wait_for_volume_status(v['id'], 'available')
Giulio Fidentebbb69e72013-06-18 16:06:24 +0200104 admin_manager.volumes_client.delete_volume(v['id'])
105 except Exception:
106 pass
107
108 for v in vols:
109 try:
110 admin_manager.volumes_client.wait_for_resource_deletion(v['id'])
111 except Exception:
112 pass