| 1 |
# Copyright 2016 Cisco Systems |
| 2 |
# All Rights Reserved |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
# |
| 16 |
import sys |
| 17 |
|
| 18 |
from neutronclient._i18n import _ |
| 19 |
from neutronclient.neutron import v2_0 as neutronV20 |
| 20 |
|
| 21 |
|
| 22 |
class Purge(neutronV20.NeutronCommand): |
| 23 |
"""Delete all resources that belong to a given tenant.""" |
| 24 |
|
| 25 |
def _pluralize(self, string): |
| 26 |
return string + 's' |
| 27 |
|
| 28 |
def _get_resources(self, neutron_client, resource_types, tenant_id): |
| 29 |
resources = [] |
| 30 |
for resource_type in resource_types: |
| 31 |
resources.append([]) |
| 32 |
resource_type_plural = self._pluralize(resource_type) |
| 33 |
opts = {'fields': ['id', 'tenant_id']} |
| 34 |
if resource_type_plural == 'ports': |
| 35 |
opts['fields'].append('device_id') |
| 36 |
opts['fields'].append('device_owner') |
| 37 |
function = getattr(neutron_client, 'list_%s' % |
| 38 |
resource_type_plural) |
| 39 |
if callable(function): |
| 40 |
returned_resources = function(**opts).get(resource_type_plural, |
| 41 |
[]) |
| 42 |
for resource in returned_resources: |
| 43 |
if resource['tenant_id'] == tenant_id: |
| 44 |
index = resource_types.index(resource_type) |
| 45 |
resources[index].append(resource) |
| 46 |
self.total_resources += 1 |
| 47 |
return resources |
| 48 |
|
| 49 |
def _delete_resource(self, neutron_client, resource_type, resource): |
| 50 |
resource_id = resource['id'] |
| 51 |
if resource_type == 'port': |
| 52 |
router_interface_owners = ['network:router_interface', |
| 53 |
'network:router_interface_distributed'] |
| 54 |
if resource.get('device_owner', '') in router_interface_owners: |
| 55 |
body = {'port_id': resource_id} |
| 56 |
neutron_client.remove_interface_router(resource['device_id'], |
| 57 |
body) |
| 58 |
return |
| 59 |
function = getattr(neutron_client, 'delete_%s' % resource_type) |
| 60 |
if callable(function): |
| 61 |
function(resource_id) |
| 62 |
|
| 63 |
def _purge_resources(self, neutron_client, resource_types, |
| 64 |
tenant_resources): |
| 65 |
deleted = {} |
| 66 |
failed = {} |
| 67 |
failures = False |
| 68 |
for resources in tenant_resources: |
| 69 |
index = tenant_resources.index(resources) |
| 70 |
resource_type = resource_types[index] |
| 71 |
failed[resource_type] = 0 |
| 72 |
deleted[resource_type] = 0 |
| 73 |
for resource in resources: |
| 74 |
try: |
| 75 |
self._delete_resource(neutron_client, resource_type, |
| 76 |
resource) |
| 77 |
deleted[resource_type] += 1 |
| 78 |
self.deleted_resources += 1 |
| 79 |
except Exception: |
| 80 |
failures = True |
| 81 |
failed[resource_type] += 1 |
| 82 |
self.total_resources -= 1 |
| 83 |
percent_complete = 100 |
| 84 |
if self.total_resources > 0: |
| 85 |
percent_complete = (self.deleted_resources / |
| 86 |
float(self.total_resources)) * 100 |
| 87 |
sys.stdout.write("\rPurging resources: %d%% complete." % |
| 88 |
percent_complete) |
| 89 |
sys.stdout.flush() |
| 90 |
return (deleted, failed, failures) |
| 91 |
|
| 92 |
def _build_message(self, deleted, failed, failures): |
| 93 |
msg = '' |
| 94 |
deleted_msg = [] |
| 95 |
for resource, value in deleted.items(): |
| 96 |
if value: |
| 97 |
if not msg: |
| 98 |
msg = 'Deleted' |
| 99 |
if not value == 1: |
| 100 |
resource = self._pluralize(resource) |
| 101 |
deleted_msg.append(" %d %s" % (value, resource)) |
| 102 |
if deleted_msg: |
| 103 |
msg += ','.join(deleted_msg) |
| 104 |
|
| 105 |
failed_msg = [] |
| 106 |
if failures: |
| 107 |
if msg: |
| 108 |
msg += '. ' |
| 109 |
msg += 'The following resources could not be deleted:' |
| 110 |
for resource, value in failed.items(): |
| 111 |
if value: |
| 112 |
if not value == 1: |
| 113 |
resource = self._pluralize(resource) |
| 114 |
failed_msg.append(" %d %s" % (value, resource)) |
| 115 |
msg += ','.join(failed_msg) |
| 116 |
|
| 117 |
if msg: |
| 118 |
msg += '.' |
| 119 |
else: |
| 120 |
msg = _('Tenant has no supported resources.') |
| 121 |
|
| 122 |
return msg |
| 123 |
|
| 124 |
def get_parser(self, prog_name): |
| 125 |
parser = super(Purge, self).get_parser(prog_name) |
| 126 |
parser.add_argument( |
| 127 |
'tenant', metavar='TENANT', |
| 128 |
help=_('ID of Tenant owning the resources to be deleted.')) |
| 129 |
return parser |
| 130 |
|
| 131 |
def take_action(self, parsed_args): |
| 132 |
neutron_client = self.get_client() |
| 133 |
|
| 134 |
self.any_failures = False |
| 135 |
|
| 136 |
# A list of the types of resources supported in the order in which |
| 137 |
# they should be deleted. |
| 138 |
resource_types = ['floatingip', 'port', 'router', |
| 139 |
'network', 'security_group'] |
| 140 |
|
| 141 |
deleted = {} |
| 142 |
failed = {} |
| 143 |
self.total_resources = 0 |
| 144 |
self.deleted_resources = 0 |
| 145 |
resources = self._get_resources(neutron_client, resource_types, |
| 146 |
parsed_args.tenant) |
| 147 |
deleted, failed, failures = self._purge_resources(neutron_client, |
| 148 |
resource_types, |
| 149 |
resources) |
| 150 |
print('\n%s' % self._build_message(deleted, failed, failures)) |