def get_existing_backups(self, backup_tag_prefix: str) -> List[BackupResource]: """ Collect existing backups on system of given type, marked with given tag """ local_region = boto3.session.Session().region_name marker_tag = f"{backup_tag_prefix}:{BackupResource.BACKUP_MARKER_TAG}" response = self.redshift_client.describe_cluster_snapshots( SnapshotType='manual', TagKeys=[marker_tag], TagValues=SHELVERY_DO_BACKUP_TAGS) snapshots = response['Snapshots'] backups = [] for snap in snapshots: cluster_id = snap['ClusterIdentifier'] d_tags = BackupResource.dict_from_boto3_tags(snap['Tags']) create_time = snap['ClusterCreateTime'] redshift_entity = EntityResource(cluster_id, local_region, create_time, d_tags) backup_id = f"arn:aws:redshift:{local_region}:{snap['OwnerAccount']}" backup_id = f"{backup_id}:snapshot:{snap['ClusterIdentifier']}/{snap['SnapshotIdentifier']}" backup_resource = BackupResource.construct(backup_tag_prefix, backup_id, d_tags) backup_resource.entity_resource = redshift_entity backup_resource.entity_id = redshift_entity.resource_id backups.append(backup_resource) return backups
def get_entities_to_backup(self, tag_name: str) -> List[EntityResource]: """Get all instances that contain `tag_name` as a tag.""" clusters = self.collect_clusters(tag_name) # TODO: To get the cluster's creation time, we need to query the "events" with the # cluster ID. entities = [] for cluster in clusters: if cluster['ClusterStatus'] != 'available': self.logger.info( f"Skipping cluster '{cluster['ClusterIdentifier']}' as its status is '{cluster['ClusterStatus']}'." ) continue d_tags = BackupResource.dict_from_boto3_tags(cluster['Tags']) entity = EntityResource( resource_id=cluster['ClusterIdentifier'], resource_region=self.region, date_created=f"{datetime.datetime.utcnow():%Y-%m-%d %H:%M:%S}", tags=d_tags) entities.append(entity) return entities
def get_backup_resource(self, backup_region: str, backup_id: str) -> BackupResource: """ Get Backup Resource within region, identified by its backup_id """ redshift_client = AwsHelper.boto3_client( 'redshift', region_name=backup_region, arn=self.role_arn, external_id=self.role_external_id) snapshot_id = backup_id.split(":")[-1].split("/")[1] snapshots = redshift_client.describe_cluster_snapshots( SnapshotIdentifier=snapshot_id) snapshot = snapshots['Snapshots'][0] d_tags = BackupResource.dict_from_boto3_tags(snapshot['Tags']) return BackupResource.construct(d_tags['shelvery:tag_name'], backup_id, d_tags)