def populate_volume_information(self, backups): volume_ids = [] volumes = {} local_region = boto3.session.Session().region_name # create list of all volume ids for backup in backups: if backup.entity_id not in volume_ids: volume_ids.append(backup.entity_id) # populate map volumeid->volume if present for volume_id in volume_ids: try: volume = self.ec2client.describe_volumes( VolumeIds=[volume_id])['Volumes'][0] d_tags = dict( map(lambda t: (t['Key'], t['Value']), volume['Tags'])) volumes[volume_id] = EntityResource(volume_id, local_region, volume['CreateTime'], d_tags) except ClientError as e: if 'InvalidVolume.NotFound' in str(e): volumes[volume_id] = EntityResource.empty() volumes[volume_id].resource_id = volume_id else: raise e # add info to backup resource objects for backup in backups: if backup.entity_id in volumes: backup.entity_resource = volumes[backup.entity_id]
def populate_snap_entity_resource(self, all_snapshots): cluster_ids = [] for snap in all_snapshots: if snap['DBClusterIdentifier'] not in cluster_ids: cluster_ids.append(snap['DBClusterIdentifier']) entities = {} rds_client = boto3.client('rds') local_region = boto3.session.Session().region_name for cluster_id in cluster_ids: try: rds_instance = rds_client.describe_db_clusters(DBClusterIdentifier=cluster_id)['DBClusters'][0] tags = rds_client.list_tags_for_resource(ResourceName=rds_instance['DBClusterArn'])['TagList'] d_tags = dict(map(lambda t: (t['Key'], t['Value']), tags)) rds_entity = EntityResource(cluster_id, local_region, rds_instance['ClusterCreateTime'], d_tags) entities[cluster_id] = rds_entity except ClientError as e: if 'DBClusterNotFoundFault' in str(type(e)): entities[cluster_id] = EntityResource.empty() entities[cluster_id].resource_id = cluster_id else: raise e for snap in all_snapshots: if snap['DBClusterIdentifier'] in entities: snap['EntityResource'] = entities[snap['DBClusterIdentifier']]
def populate_snap_entity_resource(self, all_snapshots): cluster_ids = [] for snap in all_snapshots: if snap['DBClusterIdentifier'] not in cluster_ids: cluster_ids.append(snap['DBClusterIdentifier']) entities = {} rds_client = AwsHelper.boto3_client('rds', arn=self.role_arn, external_id=self.role_external_id) local_region = boto3.session.Session().region_name for cluster_id in cluster_ids: try: self.logger.info( f"Collecting tags from DB cluster {cluster_id} ...") rds_instance = rds_client.describe_db_clusters( DBClusterIdentifier=cluster_id)['DBClusters'][0] tags = rds_instance['TagList'] d_tags = dict(map(lambda t: (t['Key'], t['Value']), tags)) rds_entity = EntityResource(cluster_id, local_region, rds_instance['ClusterCreateTime'], d_tags) entities[cluster_id] = rds_entity except ClientError as e: if 'DBClusterNotFoundFault' in str(type(e)): entities[cluster_id] = EntityResource.empty() entities[cluster_id].resource_id = cluster_id else: raise e for snap in all_snapshots: if snap['DBClusterIdentifier'] in entities: snap['EntityResource'] = entities[snap['DBClusterIdentifier']]
def populate_snap_entity_resource(self, all_snapshots): instance_ids = [] for snap in all_snapshots: if snap['DBInstanceIdentifier'] not in instance_ids: instance_ids.append(snap['DBInstanceIdentifier']) entities = {} rds_client = AwsHelper.boto3_client('rds', arn=self.role_arn, external_id=self.role_external_id) local_region = boto3.session.Session().region_name for instance_id in instance_ids: try: rds_instance = rds_client.describe_db_instances( DBInstanceIdentifier=instance_id)['DBInstances'][0] tags = rds_client.list_tags_for_resource( ResourceName=rds_instance['DBInstanceArn'])['TagList'] d_tags = dict(map(lambda t: (t['Key'], t['Value']), tags)) d_tags = dict(map(lambda t: (t['Key'], t['Value']), tags)) rds_entity = EntityResource(instance_id, local_region, rds_instance['InstanceCreateTime'], d_tags) entities[instance_id] = rds_entity except ClientError as e: if 'DBInstanceNotFoundFault' in str(type(e)): entities[instance_id] = EntityResource.empty() entities[instance_id].resource_id = instance_id else: raise e for snap in all_snapshots: if snap['DBInstanceIdentifier'] in entities: snap['EntityResource'] = entities[snap['DBInstanceIdentifier']]