-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
James Zhou
committed
Mar 3, 2023
1 parent
46a41e7
commit 376551a
Showing
16 changed files
with
438 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import isEmpty from 'lodash/isEmpty' | ||
|
||
import EC2, { | ||
Volume, | ||
Snapshot, | ||
TagList, | ||
} from 'aws-sdk/clients/ec2' | ||
|
||
import { ServiceConnection } from '@cloudgraph/sdk' | ||
|
||
import services from '../../enums/services' | ||
|
||
|
||
/** | ||
* EBS | ||
*/ | ||
|
||
export default ({ | ||
service: volume, | ||
data, | ||
region, | ||
account, | ||
}: { | ||
account: string | ||
data: { name: string; data: { [property: string]: any[] } }[] | ||
service: Volume & { | ||
region: string | ||
Tags?: TagList | ||
} | ||
region: string | ||
}): { [key: string]: ServiceConnection[] } => { | ||
const connections: ServiceConnection[] = [] | ||
|
||
const { | ||
VolumeId: id, | ||
SnapshotId: snapshotId, | ||
Tags: tags, | ||
} = volume | ||
|
||
/** | ||
* Find EBS Snapshot | ||
* related to this EBS Volume | ||
*/ | ||
const ebsSnapshots: { | ||
name: string | ||
data: { [property: string]: Snapshot[] } | ||
} = data.find(({ name }) => name === services.ebsSnapshot) | ||
|
||
if (ebsSnapshots?.data?.[region]) { | ||
const snapshotInRegion: Snapshot[] = ebsSnapshots.data[region].filter( | ||
({ SnapshotId }: Snapshot) => SnapshotId === snapshotId | ||
) | ||
|
||
if (!isEmpty(snapshotInRegion)) { | ||
for (const sh of snapshotInRegion) { | ||
connections.push({ | ||
id: sh.SnapshotId, | ||
resourceType: services.ebsSnapshot, | ||
relation: 'child', | ||
field: 'ebsSnapshots', | ||
}) | ||
} | ||
} | ||
} | ||
|
||
const ebsResult = { | ||
[id]: connections, | ||
} | ||
return ebsResult | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import EC2, { | ||
DescribeSnapshotsResult, | ||
DescribeSnapshotsRequest, | ||
Snapshot, | ||
CreateVolumePermission, | ||
DescribeSnapshotAttributeResult, | ||
} from 'aws-sdk/clients/ec2' | ||
import { Config } from 'aws-sdk/lib/config' | ||
import { AWSError } from 'aws-sdk/lib/error' | ||
|
||
import groupBy from 'lodash/groupBy' | ||
import isEmpty from 'lodash/isEmpty' | ||
|
||
import CloudGraph from '@cloudgraph/sdk' | ||
|
||
import { AwsTag, TagMap } from '../../types' | ||
|
||
import { initTestEndpoint } from '../../utils' | ||
import AwsErrorLog from '../../utils/errorLog' | ||
import { convertAwsTagsToTagMap } from '../../utils/format' | ||
import awsLoggerText from '../../properties/logger' | ||
|
||
/** | ||
* EBS Snapshot | ||
*/ | ||
|
||
const lt = { ...awsLoggerText } | ||
const { logger } = CloudGraph | ||
const serviceName = 'EBS' | ||
const errorLog = new AwsErrorLog(serviceName) | ||
const endpoint = initTestEndpoint(serviceName) | ||
|
||
export interface RawAwsEBSSnapshot extends Omit<Snapshot, 'Tags'> { | ||
region: string | ||
Permissions?: CreateVolumePermission[] | ||
Tags?: TagMap | ||
} | ||
|
||
const listEbsSnapshotAttribute = async ({ | ||
ec2, | ||
snapshotId, | ||
}: { | ||
ec2: EC2 | ||
snapshotId: string | ||
}): Promise<CreateVolumePermission[]> => | ||
new Promise(resolve => | ||
ec2.describeSnapshotAttribute( | ||
{ | ||
Attribute: 'createVolumePermission', | ||
SnapshotId: snapshotId, | ||
}, | ||
(err: AWSError, data: DescribeSnapshotAttributeResult) => { | ||
if (err) { | ||
errorLog.generateAwsErrorLog({ | ||
functionName: 'ec2:describeSnapshotAttribute', | ||
err, | ||
}) | ||
} | ||
|
||
if (isEmpty(data)) { | ||
return resolve([]) | ||
} | ||
|
||
return resolve(data?.CreateVolumePermissions) | ||
} | ||
) | ||
) | ||
|
||
const listEbsSnapshots = async ({ | ||
ec2, | ||
region, | ||
nextToken: NextToken = '', | ||
ebsSnapshotData, | ||
resolveRegion, | ||
}: { | ||
ec2: EC2 | ||
region: string | ||
nextToken?: string | ||
ebsSnapshotData: RawAwsEBSSnapshot[] | ||
resolveRegion: () => void | ||
}): Promise<void> => { | ||
let args: DescribeSnapshotsRequest = { | ||
OwnerIds: ['self'] | ||
} | ||
|
||
if (NextToken) { | ||
args = { ...args, NextToken } | ||
} | ||
|
||
ec2.describeSnapshots( | ||
args, | ||
async (err: AWSError, data: DescribeSnapshotsResult) => { | ||
if (err) { | ||
errorLog.generateAwsErrorLog({ | ||
functionName: 'ec2:describeSnapshots', | ||
err, | ||
}) | ||
} | ||
|
||
/** | ||
* No EBS snapshot data for this region | ||
*/ | ||
if (isEmpty(data)) { | ||
return resolveRegion() | ||
} | ||
|
||
const { NextToken: nextToken, Snapshots: snapshots } = data || {} | ||
logger.debug(lt.fetchedEbsSnapshots(snapshots.length)) | ||
|
||
/** | ||
* No EBS Snapshot Found | ||
*/ | ||
|
||
if (isEmpty(snapshots)) { | ||
return resolveRegion() | ||
} | ||
|
||
/** | ||
* Check to see if there are more | ||
*/ | ||
|
||
if (nextToken) { | ||
listEbsSnapshots({ region, nextToken, ec2, ebsSnapshotData, resolveRegion }) | ||
} | ||
|
||
const ebsVolumes = [] | ||
|
||
for (const { Tags, SnapshotId, ...snapshot } of snapshots) { | ||
let snapshotAttributes: CreateVolumePermission[] = [] | ||
if (SnapshotId) { | ||
snapshotAttributes = await listEbsSnapshotAttribute({ | ||
ec2, | ||
snapshotId: SnapshotId, | ||
}) | ||
} | ||
ebsVolumes.push({ | ||
...snapshot, | ||
region, | ||
SnapshotId, | ||
Permissions: snapshotAttributes, | ||
Tags: convertAwsTagsToTagMap(Tags as AwsTag[]), | ||
}) | ||
} | ||
|
||
ebsSnapshotData.push(...ebsVolumes) | ||
|
||
/** | ||
* If this is the last page of data then return the instances | ||
*/ | ||
|
||
if (!nextToken) { | ||
resolveRegion() | ||
} | ||
} | ||
) | ||
} | ||
|
||
export default async ({ | ||
regions, | ||
config, | ||
}: { | ||
regions: string | ||
config: Config | ||
}): Promise<{ | ||
[region: string]: Snapshot & { region: string }[] | ||
}> => | ||
new Promise(async resolve => { | ||
const ebsSnapshotData: RawAwsEBSSnapshot[] = [] | ||
const volumePromises: Promise<void>[] = [] | ||
|
||
// Get all the EBS data for each region with its snapshots | ||
for (const region of regions.split(',')) { | ||
const ec2 = new EC2({ ...config, region, endpoint }) | ||
|
||
volumePromises.push( | ||
new Promise<void>(resolveRegion => | ||
listEbsSnapshots({ ec2, region, ebsSnapshotData, resolveRegion }) | ||
) | ||
) | ||
} | ||
|
||
logger.debug(lt.fetchingEbsSnapshotData) | ||
// Fetch EBS volumes | ||
await Promise.all(volumePromises) | ||
|
||
errorLog.reset() | ||
|
||
resolve(groupBy(ebsSnapshotData, 'region')) | ||
}) |
Oops, something went wrong.