boto_rds: ensure that a RDS instance is deleted completely

This commit is contained in:
Quan Tong Anh 2015-11-11 16:32:25 +07:00
parent e71c93b49c
commit 30d9397380
2 changed files with 65 additions and 7 deletions

View File

@ -49,7 +49,7 @@ from __future__ import absolute_import
# Import Python libs
import logging
from salt.exceptions import SaltInvocationError
from time import sleep
from time import time, sleep
log = logging.getLogger(__name__)
@ -475,7 +475,8 @@ def get_endpoint(name, tags=None, region=None, key=None, keyid=None,
def delete(name, skip_final_snapshot=None, final_db_snapshot_identifier=None,
region=None, key=None, keyid=None, profile=None):
region=None, key=None, keyid=None, profile=None,
wait_for_deletion=True, timeout=180):
'''
Delete an RDS instance.
@ -484,6 +485,9 @@ def delete(name, skip_final_snapshot=None, final_db_snapshot_identifier=None,
salt myminion boto_rds.delete myrds skip_final_snapshot=True \
region=us-east-1
'''
if timeout == 180 and not skip_final_snapshot:
timeout = 420
conn = _get_conn(region=region, key=key, keyid=keyid, profile=profile)
if not skip_final_snapshot or final_db_snapshot_identifier:
@ -493,9 +497,21 @@ def delete(name, skip_final_snapshot=None, final_db_snapshot_identifier=None,
try:
conn.delete_db_instance(name, skip_final_snapshot,
final_db_snapshot_identifier)
msg = 'Deleted RDS instance {0}.'.format(name)
log.info(msg)
return True
if not wait_for_deletion:
log.info('Deleted RDS instance {0}.'.format(name))
return True
start_time = time()
while True:
if not __salt__['boto_rds.exists'](name=name, region=region,
key=key, keyid=keyid,
profile=profile):
log.info('Deleted RDS instance {0} completely.'.format(name))
return True
if time() - start_time > timeout:
raise SaltInvocationError('RDS instance {0} has not been '
'deleted completely after {1} '
'seconds'.format(name, timeout))
sleep(10)
except boto.exception.BotoServerError as e:
log.debug(e)
msg = 'Failed to delete RDS instance {0}'.format(name)

View File

@ -433,7 +433,48 @@ def subnet_group_present(name, description, subnet_ids=None, subnet_names=None,
def absent(name, skip_final_snapshot=None, final_db_snapshot_identifier=None,
tags=None, region=None, key=None, keyid=None, profile=None):
tags=None, region=None, key=None, keyid=None, profile=None,
wait_for_deletion=True, timeout=180):
'''
Ensure RDS instance is absent.
name
Name of the RDS instance.
skip_final_snapshot
Whether a final db snapshot is created before the instance is deleted.
If True, no snapshot is created.
If False, a snapshot is created before deleting the instance.
final_db_snapshot_identifier
If a final snapshot is requested, this is the identifier used for that
snapshot.
tags
A list of tags.
region
Region to connect to.
key
Secret key to be used.
keyid
Access key to be used.
profile
A dict with region, key and keyid, or a pillar key (string) that
contains a dict with region, key and keyid.
.. _create_dbinstance: http://boto.readthedocs.org/en/latest/ref/rds.html#boto.rds.RDSConnection.create_dbinstance
wait_for_deletion (bool)
Wait for the RDS instance to be deleted completely before finishing
the state.
timeout (in seconds)
The amount of time that can pass before raising an Exception.
'''
ret = {'name': name,
'result': True,
'comment': '',
@ -453,7 +494,8 @@ def absent(name, skip_final_snapshot=None, final_db_snapshot_identifier=None,
return ret
deleted = __salt__['boto_rds.delete'](name, skip_final_snapshot,
final_db_snapshot_identifier,
region, key, keyid, profile)
region, key, keyid, profile,
wait_for_deletion, timeout)
if not deleted:
ret['result'] = False
ret['comment'] = 'Failed to delete {0} RDS.'.format(name)