-2

Trying to filter RDS DB name using key i['DBSnapshotIdentifier'].

Sample DB names: db-test-709-au50-2020-10-17 or db-test-uk10-2020-10-17.

        print('Deleting all DB Snapshots older than %s' % retentionDate)
        
        for i in response['DBSnapshots']:
            print(i['DBSnapshotIdentifier']) # gives: db-test-709-au50-2020-10-17
            if (i['SnapshotCreateTime'] < retentionDate) and (i['DBSnapshotIdentifier'] == "*-au50*" or i['DBSnapshotIdentifier'] == "*-uk10*"):
                print ('Deleting snapshot %s' % i['DBSnapshotIdentifier'])

Trying to figure out how to filter using the above if statement. any clue how to implement the filter? TIA!

print(i) gives:

{u'MasterUsername': 'root', u'LicenseModel': 'postgresql-license', u'InstanceCreateTime': datetime.datetime(2017, 5, 23, 7, 52, 47, 355000, tzinfo=tzlocal()), u'Engine': 'postgres', u'VpcId': 'vpc-64b09', u'DBSnapshotIdentifier': 'db-test-709-au50-2020-10-17', u'AllocatedStorage': 5, u'Status': 'available', u'PercentProgress': 100, u'DBSnapshotArn': 'arn:aws:rds:eu-west-1:754878741835:snapshot:db-test-709-au50-2020-10-17', u'EngineVersion': '9.5.4', u'ProcessorFeatures': [], u'OptionGroupName': 'default:postgres-9-5', u'SnapshotCreateTime': datetime.datetime(2017, 5, 23, 8, 9, 24, 683000, tzinfo=tzlocal()), u'AvailabilityZone': 'eu-west-1a', u'StorageType': 'standard', u'Encrypted': False, u'IAMDatabaseAuthenticationEnabled': False, u'DbiResourceId': 'db-KQBQVZRNHHGHHJKIY3NZONZX5E', u'SnapshotType': 'manual', u'Port': 5432, u'DBInstanceIdentifier': 'db-test-709-au50'}
tripleee
  • 175,061
  • 34
  • 275
  • 318
james
  • 132
  • 12
  • use [re.search](https://docs.python.org/3/library/re.html#re.search). check also [regular-expression](https://docs.python.org/3/library/re.html#regular-expression-syntax) – napuzba Oct 27 '20 at 06:37
  • I rolled back your recent edit since it (a) turned this into a duplicate and (b) invalidated the answer somebody just posted. – tripleee Oct 27 '20 at 06:55
  • 1
    You don't need regex for this; `if "-au50" in i['DBSnapshotIdentifier']` – tripleee Oct 27 '20 at 06:55

1 Answers1

0

You may try this approach here:

import re
import functools 

def identifierFilter(identifier, patterns):
  return functools.reduce(lambda a,b : a or b, 
                          map(lambda pattern: pattern in identifier, patterns))

def snapshotFilter(ele, params):
  return (ele['SnapshotCreateTime'] < params["retentionDate"]) 
         and identifierFilter(ele["DBSnapshotIdentifier"], params["dbIdentifierPattern"])

snapshotParams = {
                   "retentionDate": retentionData, 
                   "dbIdentifierPattern": ["-au50-", "-uk10-"]
                 }

snapshotsToDelete = filter(lambda ele: snapshotFilter(ele, snapshotParams), response['DBSnapshots'])        

print('Deleting all DB Snapshots older than %s' % retentionDate)

for snapshot in snapshotsToDelete:
  print ('Deleting snapshot %s' % snapshot['DBSnapshotIdentifier'])

# Bonus: Write tests for the above!
bitsapien
  • 1,753
  • 12
  • 24