'Delete excess backups associated to an instance.
Instances are allowed a fixed number of backups (the rotation number);
this method deletes the oldest backups that exceed the rotation
threshold.
:param context: security context
:param instance: Instance dict
:param backup_type: a user-defined type, like "daily" or "weekly" etc.
:param rotation: int representing how many backups to keep around;
None if rotation shouldn\'t be used (as in the case of snapshots)'

@wrap_instance_fault
def _rotate_backups(self, context, instance, backup_type, rotation):

    filters = {'property-image_type': 'backup', 'property-backup_type': backup_type, 'property-instance_uuid': instance.uuid}
    images = self.image_api.get_all(context, filters=filters, sort_key='created_at', sort_dir='desc')
    num_images = len(images)
    LOG.debug('Found    %(num_images)d    images    (rotation:    %(rotation)d)', {'num_images': num_images, 'rotation': rotation}, instance=instance)
    if (num_images > rotation):
        excess = (len(images) - rotation)
        LOG.debug('Rotating    out    %d    backups', excess, instance=instance)
        for i in range(excess):
            image = images.pop()
            image_id = image['id']
            LOG.debug('Deleting    image    %s', image_id, instance=instance)
            try:
                self.image_api.delete(context, image_id)
            except exception.ImageNotFound:
                LOG.info('Failed    to    find    image    %(image_id)s    to    delete', {'image_id': image_id}, instance=instance)
