|
| 1 | +import EC2, { |
| 2 | + DescribeSnapshotsResult, |
| 3 | + DescribeSnapshotsRequest, |
| 4 | + Snapshot, |
| 5 | + CreateVolumePermission, |
| 6 | + DescribeSnapshotAttributeResult, |
| 7 | +} from 'aws-sdk/clients/ec2' |
| 8 | +import { Config } from 'aws-sdk/lib/config' |
| 9 | +import { AWSError } from 'aws-sdk/lib/error' |
| 10 | + |
| 11 | +import groupBy from 'lodash/groupBy' |
| 12 | +import isEmpty from 'lodash/isEmpty' |
| 13 | + |
| 14 | +import CloudGraph from '@cloudgraph/sdk' |
| 15 | + |
| 16 | +import { AwsTag, TagMap } from '../../types' |
| 17 | + |
| 18 | +import { initTestEndpoint } from '../../utils' |
| 19 | +import AwsErrorLog from '../../utils/errorLog' |
| 20 | +import { convertAwsTagsToTagMap } from '../../utils/format' |
| 21 | +import awsLoggerText from '../../properties/logger' |
| 22 | + |
| 23 | +/** |
| 24 | + * EBS Snapshot |
| 25 | + */ |
| 26 | + |
| 27 | +const lt = { ...awsLoggerText } |
| 28 | +const { logger } = CloudGraph |
| 29 | +const serviceName = 'EBS' |
| 30 | +const errorLog = new AwsErrorLog(serviceName) |
| 31 | +const endpoint = initTestEndpoint(serviceName) |
| 32 | + |
| 33 | +export interface RawAwsEBSSnapshot extends Omit<Snapshot, 'Tags'> { |
| 34 | + region: string |
| 35 | + Permissions?: CreateVolumePermission[] |
| 36 | + Tags?: TagMap |
| 37 | +} |
| 38 | + |
| 39 | +const listEbsSnapshotAttribute = async ({ |
| 40 | + ec2, |
| 41 | + snapshotId, |
| 42 | +}: { |
| 43 | + ec2: EC2 |
| 44 | + snapshotId: string |
| 45 | +}): Promise<CreateVolumePermission[]> => |
| 46 | + new Promise(resolve => |
| 47 | + ec2.describeSnapshotAttribute( |
| 48 | + { |
| 49 | + Attribute: 'createVolumePermission', |
| 50 | + SnapshotId: snapshotId, |
| 51 | + }, |
| 52 | + (err: AWSError, data: DescribeSnapshotAttributeResult) => { |
| 53 | + if (err) { |
| 54 | + errorLog.generateAwsErrorLog({ |
| 55 | + functionName: 'ec2:describeSnapshotAttribute', |
| 56 | + err, |
| 57 | + }) |
| 58 | + } |
| 59 | + |
| 60 | + if (isEmpty(data)) { |
| 61 | + return resolve([]) |
| 62 | + } |
| 63 | + |
| 64 | + return resolve(data?.CreateVolumePermissions) |
| 65 | + } |
| 66 | + ) |
| 67 | + ) |
| 68 | + |
| 69 | +const listEbsSnapshots = async ({ |
| 70 | + ec2, |
| 71 | + region, |
| 72 | + nextToken: NextToken = '', |
| 73 | + ebsSnapshotData, |
| 74 | + resolveRegion, |
| 75 | +}: { |
| 76 | + ec2: EC2 |
| 77 | + region: string |
| 78 | + nextToken?: string |
| 79 | + ebsSnapshotData: RawAwsEBSSnapshot[] |
| 80 | + resolveRegion: () => void |
| 81 | +}): Promise<void> => { |
| 82 | + let args: DescribeSnapshotsRequest = { |
| 83 | + OwnerIds: ['self'] |
| 84 | + } |
| 85 | + |
| 86 | + if (NextToken) { |
| 87 | + args = { ...args, NextToken } |
| 88 | + } |
| 89 | + |
| 90 | + ec2.describeSnapshots( |
| 91 | + args, |
| 92 | + async (err: AWSError, data: DescribeSnapshotsResult) => { |
| 93 | + if (err) { |
| 94 | + errorLog.generateAwsErrorLog({ |
| 95 | + functionName: 'ec2:describeSnapshots', |
| 96 | + err, |
| 97 | + }) |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * No EBS snapshot data for this region |
| 102 | + */ |
| 103 | + if (isEmpty(data)) { |
| 104 | + return resolveRegion() |
| 105 | + } |
| 106 | + |
| 107 | + const { NextToken: nextToken, Snapshots: snapshots } = data || {} |
| 108 | + logger.debug(lt.fetchedEbsSnapshots(snapshots.length)) |
| 109 | + |
| 110 | + /** |
| 111 | + * No EBS Snapshot Found |
| 112 | + */ |
| 113 | + |
| 114 | + if (isEmpty(snapshots)) { |
| 115 | + return resolveRegion() |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Check to see if there are more |
| 120 | + */ |
| 121 | + |
| 122 | + if (nextToken) { |
| 123 | + listEbsSnapshots({ region, nextToken, ec2, ebsSnapshotData, resolveRegion }) |
| 124 | + } |
| 125 | + |
| 126 | + const ebsVolumes = [] |
| 127 | + |
| 128 | + for (const { Tags, SnapshotId, ...snapshot } of snapshots) { |
| 129 | + let snapshotAttributes: CreateVolumePermission[] = [] |
| 130 | + if (SnapshotId) { |
| 131 | + snapshotAttributes = await listEbsSnapshotAttribute({ |
| 132 | + ec2, |
| 133 | + snapshotId: SnapshotId, |
| 134 | + }) |
| 135 | + } |
| 136 | + ebsVolumes.push({ |
| 137 | + ...snapshot, |
| 138 | + region, |
| 139 | + SnapshotId, |
| 140 | + Permissions: snapshotAttributes, |
| 141 | + Tags: convertAwsTagsToTagMap(Tags as AwsTag[]), |
| 142 | + }) |
| 143 | + } |
| 144 | + |
| 145 | + ebsSnapshotData.push(...ebsVolumes) |
| 146 | + |
| 147 | + /** |
| 148 | + * If this is the last page of data then return the instances |
| 149 | + */ |
| 150 | + |
| 151 | + if (!nextToken) { |
| 152 | + resolveRegion() |
| 153 | + } |
| 154 | + } |
| 155 | + ) |
| 156 | +} |
| 157 | + |
| 158 | +export default async ({ |
| 159 | + regions, |
| 160 | + config, |
| 161 | +}: { |
| 162 | + regions: string |
| 163 | + config: Config |
| 164 | +}): Promise<{ |
| 165 | + [region: string]: Snapshot & { region: string }[] |
| 166 | +}> => |
| 167 | + new Promise(async resolve => { |
| 168 | + const ebsSnapshotData: RawAwsEBSSnapshot[] = [] |
| 169 | + const volumePromises: Promise<void>[] = [] |
| 170 | + |
| 171 | + // Get all the EBS data for each region with its snapshots |
| 172 | + for (const region of regions.split(',')) { |
| 173 | + const ec2 = new EC2({ ...config, region, endpoint }) |
| 174 | + |
| 175 | + volumePromises.push( |
| 176 | + new Promise<void>(resolveRegion => |
| 177 | + listEbsSnapshots({ ec2, region, ebsSnapshotData, resolveRegion }) |
| 178 | + ) |
| 179 | + ) |
| 180 | + } |
| 181 | + |
| 182 | + logger.debug(lt.fetchingEbsSnapshotData) |
| 183 | + // Fetch EBS volumes |
| 184 | + await Promise.all(volumePromises) |
| 185 | + |
| 186 | + errorLog.reset() |
| 187 | + |
| 188 | + resolve(groupBy(ebsSnapshotData, 'region')) |
| 189 | + }) |
0 commit comments