def process(self, clusters): skip = self.data.get('skip-snapshot', False) client = local_session( self.manager.session_factory).client('elasticache') clusters_to_delete = [] replication_groups_to_delete = set() for cluster in clusters: if cluster.get('ReplicationGroupId', ''): replication_groups_to_delete.add(cluster['ReplicationGroupId']) else: clusters_to_delete.append(cluster) for cluster in clusters_to_delete: params = {'CacheClusterId': cluster['CacheClusterId']} if _cluster_eligible_for_snapshot(cluster) and not skip: params['FinalSnapshotIdentifier'] = snapshot_identifier( 'Final', cluster['CacheClusterId']) client.delete_cache_cluster(**params) self.log.info('Deleted ElastiCache cluster: %s', cluster['CacheClusterId']) for replication_group in replication_groups_to_delete: params = { 'ReplicationGroupId': replication_group, 'RetainPrimaryCluster': False } if not skip: params['FinalSnapshotIdentifier'] = snapshot_identifier( 'Final', replication_group) client.delete_replication_group(**params) self.log.info('Deleted ElastiCache replication group: %s', replication_group)
def process_cluster_snapshot(self, cluster): c = local_session(self.manager.session_factory).client('redshift') c.create_cluster_snapshot( SnapshotIdentifier=snapshot_identifier( 'Backup', cluster['ClusterIdentifier']), ClusterIdentifier=cluster['ClusterIdentifier'])
def process(self, dbs): skip = self.data.get('skip-snapshot', False) # Concurrency feels like overkill here. client = local_session(self.manager.session_factory).client('rds') for db in dbs: params = dict( DBInstanceIdentifier=db['DBInstanceIdentifier']) if skip or not _db_instance_eligible_for_final_snapshot(db): params['SkipFinalSnapshot'] = True else: params['FinalDBSnapshotIdentifier'] = snapshot_identifier( 'Final', db['DBInstanceIdentifier']) if self.data.get('copy-restore-info', False): self.copy_restore_info(client, db) if not db['CopyTagsToSnapshot']: client.modify_db_instance( DBInstanceIdentifier=db['DBInstanceIdentifier'], CopyTagsToSnapshot=True) self.log.info( "Deleting rds: %s snapshot: %s", db['DBInstanceIdentifier'], params.get('FinalDBSnapshotIdentifier', False)) try: client.delete_db_instance(**params) except ClientError as e: if e.response['Error']['Code'] == "InvalidDBInstanceState": continue raise return dbs
def process(self, resources): resources = self.filter_table_state( resources, self.valid_status) if not len(resources): return c = local_session(self.manager.session_factory).client('dynamodb') futures = {} prefix = self.data.get('prefix', 'Backup') with self.executor_factory(max_workers=2) as w: for t in resources: futures[w.submit( c.create_backup, BackupName=snapshot_identifier( prefix, t['TableName']), TableName=t['TableName'])] = t for f in as_completed(futures): t = futures[f] if f.exception(): self.manager.log.warning( "Could not complete DynamoDB backup table:%s", t) arn = f.result()['BackupDetails']['BackupArn'] t['c7n:BackupArn'] = arn
def process_cluster_snapshot(self, cluster): c = local_session(self.manager.session_factory).client('elasticache') c.create_snapshot( SnapshotName=snapshot_identifier( 'Backup', cluster['CacheClusterId']), CacheClusterId=cluster['CacheClusterId'])
def process_cluster_snapshot(self, client, cluster): cluster_tags = cluster.get('Tags') client.create_cluster_snapshot( SnapshotIdentifier=snapshot_identifier( 'Backup', cluster['ClusterIdentifier']), ClusterIdentifier=cluster['ClusterIdentifier'], Tags=cluster_tags)
def process(self, clusters): skip = self.data.get('skip-snapshot', False) delete_instances = self.data.get('delete-instances', True) client = local_session(self.manager.session_factory).client('rds') for cluster in clusters: if delete_instances: for instance in cluster.get('DBClusterMembers', []): client.delete_db_instance( DBInstanceIdentifier=instance['DBInstanceIdentifier'], SkipFinalSnapshot=True) self.log.info('Deleted RDS instance: %s', instance['DBInstanceIdentifier']) params = {'DBClusterIdentifier': cluster['DBClusterIdentifier']} if skip: params['SkipFinalSnapshot'] = True else: params['FinalDBSnapshotIdentifier'] = snapshot_identifier( 'Final', cluster['DBClusterIdentifier']) try: client.delete_db_cluster(**params) except ClientError as e: if e.response['Error']['Code'] == 'InvalidDBClusterStateFault': self.log.info('RDS cluster in invalid state: %s', cluster['DBClusterIdentifier']) continue raise self.log.info('Deleted RDS cluster: %s', cluster['DBClusterIdentifier'])
def process(self, clusters): skip = self.data.get('skip-snapshot', False) delete_instances = self.data.get('delete-instances', True) client = local_session(self.manager.session_factory).client('rds') for cluster in clusters: if delete_instances: for instance in cluster.get('DBClusterMembers', []): client.delete_db_instance( DBInstanceIdentifier=instance['DBInstanceIdentifier'], SkipFinalSnapshot=True) self.log.info( 'Deleted RDS instance: %s', instance['DBInstanceIdentifier']) params = {'DBClusterIdentifier': cluster['DBClusterIdentifier']} if skip: params['SkipFinalSnapshot'] = True else: params['FinalDBSnapshotIdentifier'] = snapshot_identifier( 'Final', cluster['DBClusterIdentifier']) try: client.delete_db_cluster(**params) except ClientError as e: if e.response['Error']['Code'] == 'InvalidDBClusterStateFault': self.log.info( 'RDS cluster in invalid state: %s', cluster['DBClusterIdentifier']) continue raise self.log.info( 'Deleted RDS cluster: %s', cluster['DBClusterIdentifier'])
def process_cluster_snapshot(self, cluster): c = local_session(self.manager.session_factory).client('rds') c.create_db_cluster_snapshot( DBClusterSnapshotIdentifier=snapshot_identifier( 'Backup', cluster['DBClusterIdentifier']), DBClusterIdentifier=cluster['DBClusterIdentifier'])
def process(self, clusters): skip = self.data.get('skip-snapshot', False) delete_instances = self.data.get('delete-instances', True) client = local_session(self.manager.session_factory).client('rds') for cluster in clusters: if delete_instances: for instance in cluster.get('DBClusterMembers', []): client.delete_db_instance( DBInstanceIdentifier=instance['DBInstanceIdentifier'], SkipFinalSnapshot=True) self.log.info('Deleted RDS instance: %s', instance['DBInstanceIdentifier']) params = {'DBClusterIdentifier': cluster['DBClusterIdentifier']} if skip: params['SkipFinalSnapshot'] = True else: params['FinalDBSnapshotIdentifier'] = snapshot_identifier( 'Final', cluster['DBClusterIdentifier']) _run_cluster_method(client.delete_db_cluster, params, (client.exceptions.DBClusterNotFoundFault, client.exceptions.ResourceNotFoundFault), client.exceptions.InvalidDBClusterStateFault)
def process(self, clusters): skip = self.data.get('skip-snapshot', False) delete_instances = self.data.get('delete-instances', True) client = local_session(self.manager.session_factory).client('rds') for cluster in clusters: if delete_instances: for instance in cluster.get('DBClusterMembers', []): client.delete_db_instance( DBInstanceIdentifier=instance['DBInstanceIdentifier'], SkipFinalSnapshot=True) self.log.info( 'Deleted RDS instance: %s', instance['DBInstanceIdentifier']) params = {'DBClusterIdentifier': cluster['DBClusterIdentifier']} if skip: params['SkipFinalSnapshot'] = True else: params['FinalDBSnapshotIdentifier'] = snapshot_identifier( 'Final', cluster['DBClusterIdentifier']) _run_cluster_method( client.delete_db_cluster, params, (client.exceptions.DBClusterNotFoundFault, client.exceptions.ResourceNotFoundFault), client.exceptions.InvalidDBClusterStateFault)
def process(self, resources): resources = self.filter_resources( resources, 'TableStatus', self.valid_status) if not len(resources): return c = local_session(self.manager.session_factory).client('dynamodb') futures = {} prefix = self.data.get('prefix', 'Backup') with self.executor_factory(max_workers=2) as w: for t in resources: futures[w.submit( c.create_backup, BackupName=snapshot_identifier( prefix, t['TableName']), TableName=t['TableName'])] = t for f in as_completed(futures): t = futures[f] if f.exception(): self.manager.log.warning( "Could not complete DynamoDB backup table:%s", t) arn = f.result()['BackupDetails']['BackupArn'] t['c7n:BackupArn'] = arn
def process_rds_snapshot(self, resource): if not _db_instance_eligible_for_backup(resource): return c = local_session(self.manager.session_factory).client('rds') c.create_db_snapshot( DBSnapshotIdentifier=snapshot_identifier( 'Backup', resource['DBInstanceIdentifier']), DBInstanceIdentifier=resource['DBInstanceIdentifier'])
def process_db_set(self, db_set): c = local_session(self.manager.session_factory).client('redshift') for db in db_set: params = {'ClusterIdentifier': db['ClusterIdentifier']} if self.skip: params['SkipFinalClusterSnapshot'] = True else: params['FinalClusterSnapshotIdentifier'] = snapshot_identifier( 'Final', db['ClusterIdentifier']) c.delete_cluster(**params)
def process(self, clusters): client = local_session(self.manager.session_factory).client('rds') for cluster in clusters: _run_cluster_method( client.create_db_cluster_snapshot, dict(DBClusterSnapshotIdentifier=snapshot_identifier( 'Backup', cluster['DBClusterIdentifier']), DBClusterIdentifier=cluster['DBClusterIdentifier']), (client.exceptions.DBClusterNotFoundFault, client.exceptions.ResourceNotFoundFault), client.exceptions.InvalidDBClusterStateFault)
def process(self, clusters): client = local_session(self.manager.session_factory).client('rds') for cluster in clusters: _run_cluster_method( client.create_db_cluster_snapshot, dict( DBClusterSnapshotIdentifier=snapshot_identifier( 'Backup', cluster['DBClusterIdentifier']), DBClusterIdentifier=cluster['DBClusterIdentifier']), (client.exceptions.DBClusterNotFoundFault, client.exceptions.ResourceNotFoundFault), client.exceptions.InvalidDBClusterStateFault)
def process_db_set(self, db_set): skip = self.data.get('skip-snapshot', False) c = local_session(self.manager.session_factory).client('redshift') for db in db_set: params = {'ClusterIdentifier': db['ClusterIdentifier']} if skip: params['SkipFinalClusterSnapshot'] = True else: params['FinalClusterSnapshotIdentifier'] = snapshot_identifier( 'Final', db['ClusterIdentifier']) c.delete_cluster(**params)
def process(self, clusters): skip = self.data.get('skip-snapshot', False) client = local_session( self.manager.session_factory).client('elasticache') clusters_to_delete = [] replication_groups_to_delete = set() for cluster in clusters: if cluster.get('ReplicationGroupId', ''): replication_groups_to_delete.add(cluster['ReplicationGroupId']) else: clusters_to_delete.append(cluster) # added if statement to handle differences in parameters if snapshot is skipped for cluster in clusters_to_delete: params = {'CacheClusterId': cluster['CacheClusterId']} if _cluster_eligible_for_snapshot(cluster) and not skip: params['FinalSnapshotIdentifier'] = snapshot_identifier( 'Final', cluster['CacheClusterId']) self.log.debug( "Taking final snapshot of %s", cluster['CacheClusterId']) else: self.log.debug( "Skipping final snapshot of %s", cluster['CacheClusterId']) client.delete_cache_cluster(**params) self.log.info( 'Deleted ElastiCache cluster: %s', cluster['CacheClusterId']) for replication_group in replication_groups_to_delete: params = {'ReplicationGroupId': replication_group, 'RetainPrimaryCluster': False} if not skip: params['FinalSnapshotIdentifier'] = snapshot_identifier( 'Final', replication_group) client.delete_replication_group(**params) self.log.info( 'Deleted ElastiCache replication group: %s', replication_group)
def process_db_set(self, db_set): skip = self.data.get('skip-snapshot', False) c = local_session(self.manager.session_factory).client('redshift') for db in db_set: params = {'ClusterIdentifier': db['ClusterIdentifier']} if skip: params['SkipFinalClusterSnapshot'] = True else: params['FinalClusterSnapshotIdentifier'] = snapshot_identifier( 'Final', db['ClusterIdentifier']) try: c.delete_cluster(**params) except ClientError as e: if e.response['Error']['Code'] == "InvalidClusterState": self.log.warning( "Cannot delete cluster when not in 'Available' state: %s" %db['ClusterIdentifier']) continue raise
def process_db_set(self, db_set): skip = self.data.get('skip-snapshot', False) c = local_session(self.manager.session_factory).client('redshift') for db in db_set: params = {'ClusterIdentifier': db['ClusterIdentifier']} if skip: params['SkipFinalClusterSnapshot'] = True else: params['FinalClusterSnapshotIdentifier'] = snapshot_identifier( 'Final', db['ClusterIdentifier']) try: c.delete_cluster(**params) except ClientError as e: if e.response['Error']['Code'] == "InvalidClusterState": self.log.warning( "Cannot delete cluster when not 'Available' state: %s", db['ClusterIdentifier']) continue raise
def process(self, resources): self.skip = self.data.get('skip-snapshot', False) client = local_session(self.manager.session_factory).client('rds') for cluster in resources: params = {'DBClusterIdentifier': cluster['DBClusterIdentifier']} if self.skip: params['SkipFinalSnapshot'] = True else: params['FinalDBSnapshotIdentifier'] = snapshot_identifier( 'Final', cluster['DBClusterIdentifier']) try: client.delete_db_cluster(**params) except ClientError as e: if e.response['Error']['Code'] in ['InvalidDBClusterStateFault']: continue raise self.log.info('Deleted RDS cluster: %s' % cluster['DBClusterIdentifier'])
def process(self, resources): self.skip = self.data.get('skip-snapshot', False) # Concurrency feels like over kill here. client = local_session(self.manager.session_factory).client('rds') for rdb in resources: params = dict( DBInstanceIdentifier=rdb['DBInstanceIdentifier']) if self.skip: params['SkipFinalSnapshot'] = True else: params['FinalDBSnapshotIdentifier'] = snapshot_identifier( 'Final', rdb['DBInstanceIdentifier']) try: client.delete_db_instance(**params) except ClientError as e: if e.response['Error']['Code'] == "InvalidDBInstanceState": continue raise self.log.info("Deleted rds: %s" % rdb['DBInstanceIdentifier'])
def process(self, dbs): skip = self.data.get('skip-snapshot', False) # Concurrency feels like overkill here. client = local_session(self.manager.session_factory).client('rds') for db in dbs: params = dict( DBInstanceIdentifier=db['DBInstanceIdentifier']) if skip or not _db_instance_eligible_for_final_snapshot(db): params['SkipFinalSnapshot'] = True else: params['FinalDBSnapshotIdentifier'] = snapshot_identifier( 'Final', db['DBInstanceIdentifier']) self.log.info( "Deleting rds: %s snapshot: %s", db['DBInstanceIdentifier'], params.get('FinalDBSnapshotIdentifier', False)) try: client.delete_db_instance(**params) except ClientError as e: if e.response['Error']['Code'] == "InvalidDBInstanceState": continue raise return dbs
def test_snapshot_identifier(self): identifier = utils.snapshot_identifier("bkup", "abcdef") # e.g. bkup-2016-07-27-abcdef self.assertEqual(len(identifier), 28)
def test_snapshot_identifier(self): identifier = utils.snapshot_identifier('bkup', 'abcdef') # e.g. bkup-2016-07-27-abcdef self.assertEqual(len(identifier), 22)
def process_cluster_snapshot(self, client, cluster): client.create_snapshot(SnapshotName=snapshot_identifier( 'Backup', cluster['CacheClusterId']), CacheClusterId=cluster['CacheClusterId'])
def process_cluster_snapshot(self, client, cluster): client.create_snapshot( SnapshotName=snapshot_identifier( 'Backup', cluster['CacheClusterId']), CacheClusterId=cluster['CacheClusterId'])