Esempio n. 1
0
class BackupRetentionPolicyBaseFilter(Filter):

    schema = type_schema('backup-retention-policy',
                         **{'op': {
                             'enum': list(scalar_ops.keys())
                         }})

    def __init__(self,
                 operations_property,
                 retention_limit,
                 data,
                 manager=None):
        super(BackupRetentionPolicyBaseFilter, self).__init__(data, manager)
        self.operations_property = operations_property
        self.retention_limit = retention_limit

    @abc.abstractmethod
    def get_retention_from_backup_policy(self, retention_policy):
        raise NotImplementedError()

    def process(self, resources, event=None):
        resources, exceptions = ThreadHelper.execute_in_parallel(
            resources=resources,
            event=event,
            execution_method=self._process_resource_set,
            executor_factory=self.executor_factory,
            log=log)
        if exceptions:
            raise exceptions[0]
        return resources

    def _process_resource_set(self, resources, event):
        client = self.manager.get_client()
        get_operation = getattr(client, self.operations_property).get
        matched_resources = []

        for resource in resources:
            match = self._process_resource(resource, get_operation)
            if match:
                matched_resources.append(resource)
        return matched_resources

    def _process_resource(self, resource, get_operation):
        retention_policy = BackupRetentionPolicyHelper.get_backup_retention_policy(
            resource, get_operation, self.operations_property)
        if retention_policy is None:
            return self._perform_op(0, self.retention_limit)
        retention = self.get_retention_from_backup_policy(retention_policy)
        return retention is not None and self._perform_op(
            retention, self.retention_limit)

    def _perform_op(self, a, b):
        op = scalar_ops.get(self.data.get('op', 'eq'))
        return op(a, b)
Esempio n. 2
0
class BackupRetentionPolicyFilter(Filter):

    schema = type_schema(
        'backup-retention-policy',
        **{
            'op': {'enum': list(scalar_ops.keys())}
        }
    )

    def __init__(self, operations_property, retention_limit, data, manager=None):
        super(BackupRetentionPolicyFilter, self).__init__(data, manager)
        self.operations_property = operations_property
        self.retention_limit = retention_limit

    @abc.abstractmethod
    def get_retention_from_backup_policy(self, retention_policy):
        raise NotImplementedError()

    def process(self, resources, event=None):
        resources, exceptions = ThreadHelper.execute_in_parallel(
            resources=resources,
            event=event,
            execution_method=self._process_resource_set,
            executor_factory=self.executor_factory,
            log=log
        )
        if exceptions:
            raise exceptions[0]
        return resources

    def _process_resource_set(self, resources, event):
        client = self.manager.get_client()
        get_operation = getattr(client, self.operations_property).get
        matched_resources = []

        for resource in resources:
            match = self._process_resource(resource, get_operation)
            if match:
                matched_resources.append(resource)
        return matched_resources

    def _process_resource(self, resource, get_operation):
        retention_policy = self._get_backup_retention_policy(resource, get_operation)
        resource['c7n:{}'.format(self.operations_property)] = retention_policy.as_dict()
        if retention_policy is None:
            return self._perform_op(0, self.retention_limit)
        retention = self.get_retention_from_backup_policy(retention_policy)
        return retention is not None and self._perform_op(retention, self.retention_limit)

    def _get_backup_retention_policy(self, resource, get_operation):
        server_id = resource[self.manager.resource_type.parent_key]
        resource_group_name = resource.get('resourceGroup')
        if resource_group_name is None:
            resource_group_name = ResourceIdParser.get_resource_group(server_id)
        database_name = resource['name']
        server_name = ResourceIdParser.get_resource_name(server_id)

        try:
            response = get_operation(resource_group_name, server_name, database_name)
        except CloudError as e:
            if e.status_code == 404:
                response = None
            else:
                log.error("Unable to get backup retention policy. "
                "(resourceGroup: {}, sqlserver: {}, sqldatabase: {})".format(
                    resource_group_name, server_name, database_name))
                raise e

        return response

    def _perform_op(self, a, b):
        op = scalar_ops.get(self.data.get('op', 'eq'))
        return op(a, b)