|
| 1 | +import { Config } from 'aws-sdk/lib/config' |
| 2 | +import { groupBy, isEmpty } from 'lodash' |
| 3 | +import RDS from 'aws-sdk/clients/rds' |
| 4 | +import { convertToPromise, fetchAllPaginatedData } from '../../utils/fetchUtils' |
| 5 | +import { initTestEndpoint } from '../../utils' |
| 6 | +import ErrorLog from '../../utils/errorLog' |
| 7 | +import { convertAwsTagsToTagMap } from '../../utils/format' |
| 8 | +import { TagMap, AwsTag } from '../../types' |
| 9 | + |
| 10 | +const serviceName = 'rdsClusterSnapshot' |
| 11 | +const endpoint = initTestEndpoint(serviceName) |
| 12 | +const errorLog = new ErrorLog(serviceName) |
| 13 | + |
| 14 | +export interface RawAwsRdsClusterSnapshot extends RDS.DBClusterSnapshot { |
| 15 | + region: string |
| 16 | + attributes: RDS.DBClusterSnapshotAttributeList |
| 17 | + Tags: TagMap |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * RdsClusterSnapshot |
| 22 | + */ |
| 23 | + |
| 24 | +export default async ({ |
| 25 | + regions, |
| 26 | + config, |
| 27 | +}: { |
| 28 | + regions: string |
| 29 | + config: Config |
| 30 | +}): Promise<{ [region: string]: RawAwsRdsClusterSnapshot[] }> => { |
| 31 | + const result: RawAwsRdsClusterSnapshot[] = [] |
| 32 | + |
| 33 | + const activeRegions = regions.split(',') |
| 34 | + |
| 35 | + for (const region of activeRegions) { |
| 36 | + const client = new RDS({ ...config, region, endpoint }) |
| 37 | + let rdsClusterSnapshotData: RDS.DBClusterSnapshot[] |
| 38 | + try { |
| 39 | + rdsClusterSnapshotData = await fetchAllPaginatedData({ |
| 40 | + getResourcesFn: convertToPromise({ |
| 41 | + sdkContext: client, |
| 42 | + fnName: 'describeDBClusterSnapshots', |
| 43 | + }), |
| 44 | + accessor: '', |
| 45 | + }) |
| 46 | + } catch (err) { |
| 47 | + errorLog.generateAwsErrorLog({ functionName: 'describeDBClusterSnapshots', err }) |
| 48 | + } |
| 49 | + |
| 50 | + if (!isEmpty(rdsClusterSnapshotData)) { |
| 51 | + for (const snapshot of rdsClusterSnapshotData) { |
| 52 | + let snapshotAttributes: RDS.DBClusterSnapshotAttributeList |
| 53 | + try { |
| 54 | + const attributeResponse = await client |
| 55 | + .describeDBClusterSnapshotAttributes({ |
| 56 | + DBClusterSnapshotIdentifier: snapshot.DBClusterSnapshotIdentifier, |
| 57 | + }) |
| 58 | + .promise() |
| 59 | + snapshotAttributes = |
| 60 | + attributeResponse?.DBClusterSnapshotAttributesResult?.DBClusterSnapshotAttributes |
| 61 | + } catch (err) { |
| 62 | + errorLog.generateAwsErrorLog({ functionName: 'describeDBClusterSnapshotAttributes', err }) |
| 63 | + } |
| 64 | + result.push({ ...snapshot, Tags: convertAwsTagsToTagMap(snapshot?.TagList as AwsTag[]), attributes: snapshotAttributes, region }) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + errorLog.reset() |
| 70 | + return groupBy(result, 'region') |
| 71 | +} |
0 commit comments