1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Improved error handling

More tests, particular for failure testing
This commit is contained in:
Will Thames 2014-09-02 22:08:44 +10:00 committed by Matt Clay
parent 25c3c03a11
commit daac0e3a3e

View file

@ -302,6 +302,19 @@ except ImportError:
has_rds2 = False has_rds2 = False
class RDSException(Exception):
def __init__(self, exc):
if hasattr(exc, 'error_message') and exc.error_message:
self.message = exc.error_message
self.code = exc.error_code
elif hasattr(exc, 'body') and 'Error' in exc.body:
self.message = exc.body['Error']['Message']
self.code = exc.body['Error']['Code']
else:
self.message = str(exc)
self.code = 'Unknown Error'
class RDSConnection: class RDSConnection:
def __init__(self, module, region, **aws_connect_params): def __init__(self, module, region, **aws_connect_params):
try: try:
@ -312,49 +325,73 @@ class RDSConnection:
def get_db_instance(self, instancename): def get_db_instance(self, instancename):
try: try:
return RDSDBInstance(self.connection.get_all_dbinstances(instancename)[0]) return RDSDBInstance(self.connection.get_all_dbinstances(instancename)[0])
except boto.exception.BotoServerError,e: except boto.exception.BotoServerError, e:
return None return None
def get_db_snapshot(self, snapshotid): def get_db_snapshot(self, snapshotid):
try: try:
return RDSSnapshot(self.connection.get_all_dbsnapshots(snapshot_id=snapshotid)[0]) return RDSSnapshot(self.connection.get_all_dbsnapshots(snapshot_id=snapshotid)[0])
except boto.exception.BotoServerError,e: except boto.exception.BotoServerError, e:
return None return None
def create_db_instance(self, instance_name, size, instance_class, db_engine, def create_db_instance(self, instance_name, size, instance_class, db_engine,
username, password, **params): username, password, **params):
params['engine'] = db_engine params['engine'] = db_engine
result = self.connection.create_dbinstance(instance_name, size, instance_class, try:
username, password, **params) result = self.connection.create_dbinstance(instance_name, size, instance_class,
return RDSDBInstance(result) username, password, **params)
return RDSDBInstance(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
def create_db_instance_read_replica(self, instance_name, source_instance, **params): def create_db_instance_read_replica(self, instance_name, source_instance, **params):
result = self.connection.createdb_instance_read_replica(instance_name, source_instance, **params) try:
return RDSDBInstance(result) result = self.connection.createdb_instance_read_replica(instance_name, source_instance, **params)
return RDSDBInstance(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
def delete_db_instance(self, instance_name, **params): def delete_db_instance(self, instance_name, **params):
result = self.connection.delete_dbinstance(instance_name, **params) try:
return RDSDBInstance(result) result = self.connection.delete_dbinstance(instance_name, **params)
return RDSDBInstance(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
def delete_db_snapshot(self, snapshot): def delete_db_snapshot(self, snapshot):
result = self.connection.delete_dbsnapshot(snapshot) try:
return RDSSnapshot(result) result = self.connection.delete_dbsnapshot(snapshot)
return RDSSnapshot(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
def modify_db_instance(self, instance_name, **params): def modify_db_instance(self, instance_name, **params):
result = self.connection.modify_dbinstance(instance_name, **params) try:
return RDSDBInstance(result) result = self.connection.modify_dbinstance(instance_name, **params)
return RDSDBInstance(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
def restore_db_instance_from_db_snapshot(self, instance_name, snapshot, instance_type, **params): def restore_db_instance_from_db_snapshot(self, instance_name, snapshot, instance_type, **params):
result = self.connection.restore_dbinstance_from_dbsnapshot(snapshot, instance_name, instance_type, **params) try:
return RDSDBInstance(result) result = self.connection.restore_dbinstance_from_dbsnapshot(snapshot, instance_name, instance_type, **params)
return RDSDBInstance(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
def create_db_snapshot(self, snapshot, instance_name, **params): def create_db_snapshot(self, snapshot, instance_name, **params):
result = self.connection.create_dbsnapshot(snapshot, instance_name) try:
return RDSSnapshot(result) result = self.connection.create_dbsnapshot(snapshot, instance_name)
return RDSSnapshot(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
def promote_read_replica(self, instance_name, **params): def promote_read_replica(self, instance_name, **params):
result = self.connection.promote_read_replica(instance_name, **params) try:
return RDSInstance(result) result = self.connection.promote_read_replica(instance_name, **params)
return RDSInstance(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
class RDS2Connection: class RDS2Connection:
@ -384,37 +421,61 @@ class RDS2Connection:
def create_db_instance(self, instance_name, size, instance_class, db_engine, def create_db_instance(self, instance_name, size, instance_class, db_engine,
username, password, **params): username, password, **params):
result = self.connection.create_db_instance(instance_name, size, instance_class, try:
result = self.connection.create_db_instance(instance_name, size, instance_class,
db_engine, username, password, **params)['CreateDBInstanceResponse']['CreateDBInstanceResult']['DBInstance'] db_engine, username, password, **params)['CreateDBInstanceResponse']['CreateDBInstanceResult']['DBInstance']
return RDS2DBInstance(result) return RDS2DBInstance(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
def create_db_instance_read_replica(self, instance_name, source_instance, **params): def create_db_instance_read_replica(self, instance_name, source_instance, **params):
result = self.connection.create_db_instance_read_replica(instance_name, source_instance, **params)['CreateDBInstanceReadReplicaResponse']['CreateDBInstanceReadReplicaResult']['DBInstance'] try:
return RDS2DBInstance(result) result = self.connection.create_db_instance_read_replica(instance_name, source_instance, **params)['CreateDBInstanceReadReplicaResponse']['CreateDBInstanceReadReplicaResult']['DBInstance']
return RDS2DBInstance(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
def delete_db_instance(self, instance_name, **params): def delete_db_instance(self, instance_name, **params):
result = self.connection.delete_db_instance(instance_name, **params)['DeleteDBInstanceResponse']['DeleteDBInstanceResult']['DBInstance'] try:
return RDS2DBInstance(result) result = self.connection.delete_db_instance(instance_name, **params)['DeleteDBInstanceResponse']['DeleteDBInstanceResult']['DBInstance']
return RDS2DBInstance(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
def delete_db_snapshot(self, snapshot): def delete_db_snapshot(self, snapshot):
result = self.connection.delete_db_snapshot(snapshot)['DeleteDBSnapshotResponse']['DeleteDBSnapshotResult']['DBSnapshot'] try:
return RDS2Snapshot(result) result = self.connection.delete_db_snapshot(snapshot)['DeleteDBSnapshotResponse']['DeleteDBSnapshotResult']['DBSnapshot']
return RDS2Snapshot(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
def modify_db_instance(self, instance_name, **params): def modify_db_instance(self, instance_name, **params):
result = self.connection.modify_db_instance(instance_name, **params)['ModifyDBInstanceResponse']['ModifyDBInstanceResult']['DBInstance'] try:
return RDS2DBInstance(result) result = self.connection.modify_db_instance(instance_name, **params)['ModifyDBInstanceResponse']['ModifyDBInstanceResult']['DBInstance']
return RDS2DBInstance(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
def restore_db_instance_from_db_snapshot(self, instance_name, snapshot, instance_type, **params): def restore_db_instance_from_db_snapshot(self, instance_name, snapshot, instance_type, **params):
result = self.connection.restore_db_instance_from_db_snapshot(instance_name, snapshot, **params)['RestoreDBInstanceFromDBSnapshotResponse']['RestoreDBInstanceFromDBSnapshotResult']['DBInstance'] try:
return RDS2DBInstance(result) result = self.connection.restore_db_instance_from_db_snapshot(instance_name, snapshot, **params)['RestoreDBInstanceFromDBSnapshotResponse']['RestoreDBInstanceFromDBSnapshotResult']['DBInstance']
return RDS2DBInstance(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
def create_db_snapshot(self, snapshot, instance_name, **params): def create_db_snapshot(self, snapshot, instance_name, **params):
result = self.connection.create_db_snapshot(snapshot, instance_name, **params)['CreateDBSnapshotResponse']['CreateDBSnapshotResult']['DBSnapshot'] try:
return RDS2Snapshot(result) result = self.connection.create_db_snapshot(snapshot, instance_name, **params)['CreateDBSnapshotResponse']['CreateDBSnapshotResult']['DBSnapshot']
return RDS2Snapshot(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
def promote_read_replica(self, instance_name, **params): def promote_read_replica(self, instance_name, **params):
result = self.connection.promote_read_replica(instance_name, **params)['PromoteReadReplicaResponse']['PromoteReadReplicaResult']['DBInstance'] try:
return RDS2DBInstance(result) result = self.connection.promote_read_replica(instance_name, **params)['PromoteReadReplicaResponse']['PromoteReadReplicaResult']['DBInstance']
return RDS2DBInstance(result)
except boto.exception.BotoServerError, e:
raise RDSException(e)
class RDSDBInstance: class RDSDBInstance:
@ -587,8 +648,8 @@ def create_db_instance(module, conn):
module.params.get('instance_type'), module.params.get('db_engine'), module.params.get('instance_type'), module.params.get('db_engine'),
module.params.get('username'), module.params.get('password'), **params) module.params.get('username'), module.params.get('password'), **params)
changed = True changed = True
except boto.exception.StandardError, e: except RDSException, e:
module.fail_json(msg=e.error_message) module.fail_json(msg=e.message)
if module.params.get('wait'): if module.params.get('wait'):
resource = await_resource(conn, result, 'available', module) resource = await_resource(conn, result, 'available', module)
@ -614,8 +675,8 @@ def replicate_db_instance(module, conn):
try: try:
result = conn.create_db_instance_read_replica(instance_name, source_instance, **params) result = conn.create_db_instance_read_replica(instance_name, source_instance, **params)
changed = True changed = True
except boto.exception.StandardError, e: except RDSException, e:
module.fail_json(msg=e.error_message) module.fail_json(msg=e.message)
if module.params.get('wait'): if module.params.get('wait'):
resource = await_resource(conn, result, 'available', module) resource = await_resource(conn, result, 'available', module)
@ -650,8 +711,8 @@ def delete_db_instance_or_snapshot(module, conn):
result = conn.delete_db_instance(instance_name, **params) result = conn.delete_db_instance(instance_name, **params)
else: else:
result = conn.delete_db_snapshot(snapshot) result = conn.delete_db_snapshot(snapshot)
except boto.exception.StandardError, e: except RDSException, e:
module.fail_json(msg=e.error_message) module.fail_json(msg=e.message)
# If we're not waiting for a delete to complete then we're all done # If we're not waiting for a delete to complete then we're all done
# so just return # so just return
@ -660,11 +721,11 @@ def delete_db_instance_or_snapshot(module, conn):
try: try:
resource = await_resource(conn, result, 'deleted', module) resource = await_resource(conn, result, 'deleted', module)
module.exit_json(changed=True) module.exit_json(changed=True)
except boto.exception.StandardError, e: except RDSException, e:
if e.error_code == 'DBInstanceNotFound': if e.code == 'DBInstanceNotFound':
module.exit_json(changed=True) module.exit_json(changed=True)
else: else:
module.fail_json(msg=e.error_message) module.fail_json(msg=e.message)
except Exception, e: except Exception, e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
@ -704,8 +765,8 @@ def modify_db_instance(module, conn):
try: try:
result = conn.modify_db_instance(instance_name, **params) result = conn.modify_db_instance(instance_name, **params)
except boto.exception.StandardError, e: except RDSException, e:
module.fail_json(msg=e.error_message) module.fail_json(msg=e.message)
if params.get('apply_immediately'): if params.get('apply_immediately'):
if new_instance_name: if new_instance_name:
# Wait until the new instance name is valid # Wait until the new instance name is valid
@ -753,8 +814,8 @@ def promote_db_instance(module, conn):
else: else:
try: try:
result = conn.promote_read_replica(instance_name, **params) result = conn.promote_read_replica(instance_name, **params)
except boto.exception.StandardError, e: except RDSException, e:
module.fail_json(msg=e.error_message) module.fail_json(msg=e.message)
if module.params.get('wait'): if module.params.get('wait'):
resource = await_resource(conn, result, 'available', module) resource = await_resource(conn, result, 'available', module)
@ -776,8 +837,8 @@ def snapshot_db_instance(module, conn):
try: try:
result = conn.create_db_snapshot(snapshot, instance_name, **params) result = conn.create_db_snapshot(snapshot, instance_name, **params)
changed = True changed = True
except boto.exception.StandardError, e: except RDSException, e:
module.fail_json(msg=e.error_message) module.fail_json(msg=e.message)
if module.params.get('wait'): if module.params.get('wait'):
resource = await_resource(conn, result, 'available', module) resource = await_resource(conn, result, 'available', module)
@ -807,8 +868,8 @@ def restore_db_instance(module, conn):
try: try:
result = conn.restore_db_instance_from_db_snapshot(instance_name, snapshot, instance_type, **params) result = conn.restore_db_instance_from_db_snapshot(instance_name, snapshot, instance_type, **params)
changed = True changed = True
except boto.exception.StandardError, e: except RDSException, e:
module.fail_json(msg=e.error_message) module.fail_json(msg=e.message)
if module.params.get('wait'): if module.params.get('wait'):
resource = await_resource(conn, result, 'available', module) resource = await_resource(conn, result, 'available', module)