Move exceptions into a separate module
Change-Id: I92e2d79962d9fe87a2ae5a5bf7876706c5c9c8db
This commit is contained in:
committed by
Christian Berendt
parent
d3d8d6a096
commit
0ae31c5d3a
@@ -49,26 +49,7 @@ import requests
|
|||||||
from swiftclient import client as swift_client
|
from swiftclient import client as swift_client
|
||||||
|
|
||||||
from ospurge import constants
|
from ospurge import constants
|
||||||
|
from ospurge import exceptions
|
||||||
|
|
||||||
class ResourceNotEnabled(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class EndpointNotFound(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class InvalidEndpoint(Exception):
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class NoSuchProject(Exception):
|
|
||||||
ERROR_CODE = constants.NO_SUCH_PROJECT_ERROR_CODE
|
|
||||||
|
|
||||||
|
|
||||||
class DeletionFailed(Exception):
|
|
||||||
ERROR_CODE = constants.DELETION_FAILED_ERROR_CODE
|
|
||||||
|
|
||||||
|
|
||||||
# Decorators
|
# Decorators
|
||||||
@@ -94,7 +75,7 @@ def retry(service_name):
|
|||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
if n == constants.RETRIES:
|
if n == constants.RETRIES:
|
||||||
raise DeletionFailed(service_name)
|
raise exceptions.DeletionFailed(service_name)
|
||||||
n += 1
|
n += 1
|
||||||
logging.info("* Deletion failed - "
|
logging.info("* Deletion failed - "
|
||||||
"Retrying in {} seconds - "
|
"Retrying in {} seconds - "
|
||||||
@@ -151,7 +132,7 @@ class Session(object):
|
|||||||
return self.catalog[service_type][0][self.endpoint_type]
|
return self.catalog[service_type][0][self.endpoint_type]
|
||||||
except (KeyError, IndexError):
|
except (KeyError, IndexError):
|
||||||
# Endpoint could not be found
|
# Endpoint could not be found
|
||||||
raise EndpointNotFound(service_type)
|
raise exceptions.EndpointNotFound(service_type)
|
||||||
|
|
||||||
|
|
||||||
class Resources(object):
|
class Resources(object):
|
||||||
@@ -421,7 +402,7 @@ class NeutronSecgroups(NeutronResources):
|
|||||||
return filter(secgroup_filter, sgs)
|
return filter(secgroup_filter, sgs)
|
||||||
except neutronclient.common.exceptions.NeutronClientException as err:
|
except neutronclient.common.exceptions.NeutronClientException as err:
|
||||||
if getattr(err, "status_code", None) == 404:
|
if getattr(err, "status_code", None) == 404:
|
||||||
raise ResourceNotEnabled
|
raise exceptions.ResourceNotEnabled
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def delete(self, secgroup):
|
def delete(self, secgroup):
|
||||||
@@ -710,7 +691,7 @@ class KeystoneManager(object):
|
|||||||
project_id = filter(
|
project_id = filter(
|
||||||
lambda x: x.name == project_name_or_id, tenants)[0].id
|
lambda x: x.name == project_name_or_id, tenants)[0].id
|
||||||
except IndexError:
|
except IndexError:
|
||||||
raise NoSuchProject(project_name_or_id)
|
raise exceptions.NoSuchProject(project_name_or_id)
|
||||||
|
|
||||||
if not self.tenant_info:
|
if not self.tenant_info:
|
||||||
self.tenant_info = self.client.tenants.get(project_id)
|
self.tenant_info = self.client.tenants.get(project_id)
|
||||||
@@ -767,13 +748,13 @@ def perform_on_project(admin_name, password, project, auth_url,
|
|||||||
res_actions = {'purge': resources.purge,
|
res_actions = {'purge': resources.purge,
|
||||||
'dump': resources.dump}
|
'dump': resources.dump}
|
||||||
res_actions[action]()
|
res_actions[action]()
|
||||||
except (EndpointNotFound,
|
except (exceptions.EndpointNotFound,
|
||||||
keystoneclient.openstack.common.apiclient.exceptions.EndpointNotFound,
|
keystoneclient.openstack.common.apiclient.exceptions.EndpointNotFound,
|
||||||
neutronclient.common.exceptions.EndpointNotFound,
|
neutronclient.common.exceptions.EndpointNotFound,
|
||||||
cinderclient.exceptions.EndpointNotFound,
|
cinderclient.exceptions.EndpointNotFound,
|
||||||
novaclient.exceptions.EndpointNotFound,
|
novaclient.exceptions.EndpointNotFound,
|
||||||
heatclient.openstack.common.apiclient.exceptions.EndpointNotFound,
|
heatclient.openstack.common.apiclient.exceptions.EndpointNotFound,
|
||||||
ResourceNotEnabled):
|
exceptions.ResourceNotEnabled):
|
||||||
# If service is not in Keystone's services catalog, ignoring it
|
# If service is not in Keystone's services catalog, ignoring it
|
||||||
pass
|
pass
|
||||||
except requests.exceptions.MissingSchema as e:
|
except requests.exceptions.MissingSchema as e:
|
||||||
@@ -783,7 +764,7 @@ def perform_on_project(admin_name, password, project, auth_url,
|
|||||||
except (ceilometerclient.exc.InvalidEndpoint, glanceclient.exc.InvalidEndpoint) as e:
|
except (ceilometerclient.exc.InvalidEndpoint, glanceclient.exc.InvalidEndpoint) as e:
|
||||||
logging.warning(
|
logging.warning(
|
||||||
"Unable to connect to {} endpoint : {}".format(rc, e.message))
|
"Unable to connect to {} endpoint : {}".format(rc, e.message))
|
||||||
error = InvalidEndpoint(rc)
|
error = exceptions.InvalidEndpoint(rc)
|
||||||
except (neutronclient.common.exceptions.NeutronClientException):
|
except (neutronclient.common.exceptions.NeutronClientException):
|
||||||
# If service is not configured, ignoring it
|
# If service is not configured, ignoring it
|
||||||
pass
|
pass
|
||||||
@@ -914,9 +895,9 @@ def main():
|
|||||||
except api_exceptions.Forbidden as exc:
|
except api_exceptions.Forbidden as exc:
|
||||||
print("Not authorized: {}".format(str(exc)))
|
print("Not authorized: {}".format(str(exc)))
|
||||||
sys.exit(constants.NOT_AUTHORIZED_ERROR_CODE)
|
sys.exit(constants.NOT_AUTHORIZED_ERROR_CODE)
|
||||||
except NoSuchProject as exc:
|
except exceptions.NoSuchProject as exc:
|
||||||
print("Project {} doesn't exist".format(str(exc)))
|
print("Project {} doesn't exist".format(str(exc)))
|
||||||
sys.exit(NoSuchProject.ERROR_CODE)
|
sys.exit(constants.NO_SUCH_PROJECT_ERROR_CODE)
|
||||||
|
|
||||||
# Proper cleanup
|
# Proper cleanup
|
||||||
try:
|
try:
|
||||||
@@ -927,10 +908,10 @@ def main():
|
|||||||
except requests.exceptions.ConnectionError as exc:
|
except requests.exceptions.ConnectionError as exc:
|
||||||
print("Connection error: {}".format(str(exc)))
|
print("Connection error: {}".format(str(exc)))
|
||||||
sys.exit(constants.CONNECTION_ERROR_CODE)
|
sys.exit(constants.CONNECTION_ERROR_CODE)
|
||||||
except (DeletionFailed, InvalidEndpoint) as exc:
|
except (exceptions.DeletionFailed, exceptions.InvalidEndpoint) as exc:
|
||||||
print("Deletion of {} failed".format(str(exc)))
|
print("Deletion of {} failed".format(str(exc)))
|
||||||
print("*Warning* Some resources may not have been cleaned up")
|
print("*Warning* Some resources may not have been cleaned up")
|
||||||
sys.exit(DeletionFailed.ERROR_CODE)
|
sys.exit(constants.DELETION_FAILED_ERROR_CODE)
|
||||||
|
|
||||||
if (not args.dry_run) and (not args.dont_delete_project) and (not args.own_project):
|
if (not args.dry_run) and (not args.dont_delete_project) and (not args.own_project):
|
||||||
keystone_manager.delete_project(cleanup_project_id)
|
keystone_manager.delete_project(cleanup_project_id)
|
||||||
|
|||||||
39
ospurge/exceptions.py
Normal file
39
ospurge/exceptions.py
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
# This software is released under the MIT License.
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in all
|
||||||
|
# copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
|
||||||
|
|
||||||
|
class ResourceNotEnabled(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class EndpointNotFound(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidEndpoint(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class NoSuchProject(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class DeletionFailed(Exception):
|
||||||
|
pass
|
||||||
Reference in New Issue
Block a user