class SnapshotSkipAmiSnapshots(Filter): """Filter to remove snapshots of AMIs from results This filter is 'true' by default. :example: .. code-block: yaml policies: - name: delete-stale-snapshots resource: ebs-snapshots filters: - type: age days: 28 op: ge - skip-ami-snapshots: true """ schema = type_schema('skip-ami-snapshots', value={'type': 'boolean'}) permissions = AMI.get_permissions() def validate(self): if not isinstance(self.data.get('value', True), bool): raise FilterValidationError( "invalid config: expected boolean value") return self def process(self, snapshots, event=None): resources = _filter_ami_snapshots(self, snapshots) return resources
def _filter_ami_snapshots(self, snapshots): if not self.data.get('value', True): return snapshots #try using cache first to get a listing of all AMI snapshots and compares resources to the list #This will populate the cache. ami_manager = AMI(self.manager.ctx, {}) amis = ami_manager.resources() ami_snaps = [] for i in amis: for dev in i.get('BlockDeviceMappings'): if 'Ebs' in dev and 'SnapshotId' in dev['Ebs']: ami_snaps.append(dev['Ebs']['SnapshotId']) matches = [] for snap in snapshots: if snap['SnapshotId'] not in ami_snaps: matches.append(snap) return matches
def initialize(self, asgs): from c7n.resources.ami import AMI super(ImageAgeFilter, self).initialize(asgs) image_ids = set() for cfg in self.configs.values(): image_ids.add(cfg['ImageId']) results = AMI(self.manager.ctx, {}).resources() self.images = {i['ImageId']: i for i in results}
def get_images(self): from c7n.resources.ami import AMI manager = AMI(self.manager.ctx, {}) images = set() # Verify image snapshot validity, i've been told by a TAM this # is a possibility, but haven't seen evidence of it, since # snapshots are strongly ref'd by amis, but its negible cost # to verify. for a in manager.resources(): found = True for bd in a.get('BlockDeviceMappings', ()): if 'Ebs' not in bd or 'SnapshotId' not in bd['Ebs']: continue if bd['Ebs']['SnapshotId'] not in self.snapshots: found = False break if found: images.add(a['ImageId']) return images
def get_images(self): from c7n.resources.ami import AMI manager = AMI(self.manager.ctx, {}) return set([i['ImageId'] for i in manager.resources()])
def get_permissions(self): return AMI(self.manager.ctx, {}).get_permissions()