示例#1
0
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
示例#2
0
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
示例#3
0
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
示例#4
0
 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}
示例#5
0
 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
示例#6
0
 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
示例#7
0
 def get_images(self):
     from c7n.resources.ami import AMI
     manager = AMI(self.manager.ctx, {})
     return set([i['ImageId'] for i in manager.resources()])
示例#8
0
 def get_images(self):
     from c7n.resources.ami import AMI
     manager = AMI(self.manager.ctx, {})
     return set([i['ImageId'] for i in manager.resources()])
示例#9
0
 def get_permissions(self):
     return AMI(self.manager.ctx, {}).get_permissions()