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