Skip to content

Commit

Permalink
feat: Added snapshots for dbinstances
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Franceschi committed May 30, 2023
1 parent 7c3e5c0 commit 1377e9a
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 4 deletions.
26 changes: 26 additions & 0 deletions src/services/rdsDbInstance/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import RDS, {
DBInstance,
DescribeDBInstancesMessage,
DBInstanceMessage,
DBSnapshotMessage,
DBSnapshot,
} from 'aws-sdk/clients/rds'
import { AWSError } from 'aws-sdk/lib/error'
import groupBy from 'lodash/groupBy'
Expand Down Expand Up @@ -57,6 +59,28 @@ const listDBInstancesForRegion = async (rds: RDS): Promise<DBInstance[]> =>
listAllDBInstances()
})

const getInstanceSnapshots = async (rds: RDS, arn: string): Promise<DBSnapshot[]> =>
new Promise(resolve => {
try {
rds.describeDBSnapshots(
{ DBInstanceIdentifier: arn },
(err: AWSError, data: DBSnapshotMessage) => {
if (err) {
errorLog.generateAwsErrorLog({
functionName: 'rds:describeDBSnapshots',
err,
})
return resolve([])
}
const { DBSnapshots: snapshots = [] } = data || {}
resolve(snapshots)
}
)
} catch (error) {
resolve([])
}
})

const getResourceTags = async (rds: RDS, arn: string): Promise<TagMap> =>
new Promise(resolve => {
try {
Expand All @@ -81,6 +105,7 @@ const getResourceTags = async (rds: RDS, arn: string): Promise<TagMap> =>

export interface RawAwsRdsDbInstance extends DBInstance {
Tags?: TagMap
Snapshots?: DBSnapshot[]
region: string
}

Expand Down Expand Up @@ -123,6 +148,7 @@ export default async ({
const rds = new RDS({ ...config, region, endpoint })
const tagsPromise = new Promise<void>(async resolveTags => {
rdsData[idx].Tags = await getResourceTags(rds, DBInstanceArn)
rdsData[idx].Snapshots = await getInstanceSnapshots(rds, DBInstanceArn)
resolveTags()
})
tagsPromises.push(tagsPromise)
Expand Down
66 changes: 62 additions & 4 deletions src/services/rdsDbInstance/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import { generateUniqueId } from '@cloudgraph/sdk'
import upperFirst from 'lodash/upperFirst'
import { RawAwsRdsDbInstance } from './data'
import {
AwsRdsDbInstance,
AwsRdsDbInstance, AwsRdsDbInstanceGroupOption, AwsRdsDbInstanceParameterGroup, AwsRdsDbInstanceSnapshot,
} from '../../types/generated'
import { formatTagsFromMap } from '../../utils/format'
import { convertAwsTagsToTagMap, formatTagsFromMap } from '../../utils/format'
import { AwsTag } from '../../types'

export default ({
service,
Expand Down Expand Up @@ -44,11 +45,12 @@ export default ({
Endpoint: endpoint,
LicenseModel: licenseModel,
Tags = {},
Snapshots = []
} = service

const subnetGroup = service.DBSubnetGroup?.DBSubnetGroupName || ''

const parameterGroups = service.DBParameterGroups.map(
const parameterGroups: AwsRdsDbInstanceParameterGroup[] = service.DBParameterGroups.map(
(parameter) => {
const { DBParameterGroupName, ParameterApplyStatus } = parameter
return ({
Expand All @@ -63,7 +65,7 @@ export default ({
}
)

const optionsGroups = service.OptionGroupMemberships.map(
const optionsGroups: AwsRdsDbInstanceGroupOption[] = service.OptionGroupMemberships.map(
(option) => {
const { OptionGroupName, Status } = option
return ({
Expand All @@ -79,6 +81,61 @@ export default ({

)

const snapshots: AwsRdsDbInstanceSnapshot[] = Snapshots.map(
(snapshot) => {
const tags = convertAwsTagsToTagMap(snapshot.TagList as AwsTag[])
return ({
id: generateUniqueId({
arn,
...snapshot
}),
dBSnapshotIdentifier: snapshot.DBSnapshotIdentifier,
dBInstanceIdentifier: snapshot.DBInstanceIdentifier,
snapshotCreateTime: snapshot.SnapshotCreateTime?.toISOString(),
engine: snapshot.Engine,
allocatedStorage: snapshot.AllocatedStorage,
status: snapshot.Status,
port: snapshot.Port,
availabilityZone: snapshot.AvailabilityZone,
vpcId: snapshot.VpcId,
instanceCreateTime: snapshot.InstanceCreateTime?.toISOString(),
masterUsername: snapshot.MasterUsername,
engineVersion: snapshot.EngineVersion,
licenseModel: snapshot.LicenseModel,
snapshotType: snapshot.SnapshotType,
iops: snapshot.Iops,
optionGroupName: snapshot.OptionGroupName,
percentProgress: snapshot.PercentProgress,
sourceRegion: snapshot.SourceRegion,
sourceDBSnapshotIdentifier: snapshot.SourceDBSnapshotIdentifier,
storageType: snapshot.StorageType,
tdeCredentialArn: snapshot.TdeCredentialArn,
encrypted: snapshot.Encrypted,
kmsKeyId: snapshot.KmsKeyId,
dBSnapshotArn: snapshot.DBSnapshotArn,
timezone: snapshot.Timezone,
iAMDatabaseAuthenticationEnabled: snapshot.IAMDatabaseAuthenticationEnabled,
processorFeatures: snapshot.ProcessorFeatures?.map(p => ({
id: generateUniqueId({
arn,
...p
}),
name: p.Name,
value: p.Value
})),
dbiResourceId: snapshot.DbiResourceId,
tagList: snapshot.TagList,
originalSnapshotCreateTime: snapshot.OriginalSnapshotCreateTime?.toISOString(),
snapshotDatabaseTime: snapshot.SnapshotDatabaseTime?.toISOString(),
snapshotTarget: snapshot.SnapshotTarget,
storageThroughput: snapshot.StorageThroughput,
tags: formatTagsFromMap(tags)
})
}
)



return {
id: arn,
accountId: account,
Expand Down Expand Up @@ -114,6 +171,7 @@ export default ({
kmsKey,
encrypted,
licenseModel,
snapshots,
tags: formatTagsFromMap(Tags),
}
}
52 changes: 52 additions & 0 deletions src/services/rdsDbInstance/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,57 @@ type awsRdsDbInstanceGroupOption {
description: String @search(by: [hash, regexp])
}

type awsRdsDbInstanceSnapshot {
id: String! @id
groupName: String @search(by: [hash, regexp])
status: String @search(by: [hash, regexp])
description: String @search(by: [hash, regexp])
}


type awsRdsDbInstanceProcessorFeature {
id: String! @id
name: String @search(by: [hash, regexp])
value: String @search(by: [hash, regexp])
}

type awsRdsDbInstanceSnapshot {
id: String! @id
dBSnapshotIdentifier: String @search(by: [hash, regexp])
dBInstanceIdentifier: String @search(by: [hash, regexp])
snapshotCreateTime: DateTime @search(by: [day])
engine: String @search(by: [hash, regexp])
allocatedStorage: Int @search
status: String @search(by: [hash, regexp])
port: Int @search
availabilityZone: String @search(by: [hash, regexp])
vpcId: String @search(by: [hash, regexp])
instanceCreateTime: DateTime @search(by: [day])
masterUsername: String @search(by: [hash, regexp])
engineVersion: String @search(by: [hash, regexp])
licenseModel: String @search(by: [hash, regexp])
snapshotType: String @search(by: [hash, regexp])
iops: Int @search
optionGroupName: String @search(by: [hash, regexp])
percentProgress: Int @search
sourceRegion: String @search(by: [hash, regexp])
sourceDBSnapshotIdentifier: String @search(by: [hash, regexp])
storageType: String @search(by: [hash, regexp])
tdeCredentialArn: String @search(by: [hash, regexp])
encrypted: Boolean @search
kmsKeyId: String @search(by: [hash, regexp])
dBSnapshotArn: String @search(by: [hash, regexp])
timezone: String @search(by: [hash, regexp])
iAMDatabaseAuthenticationEnabled: Boolean @search
processorFeatures: [awsRdsDbInstanceProcessorFeature]
dbiResourceId: String @search(by: [hash, regexp])
tags: [awsRawTag]
originalSnapshotCreateTime: DateTime @search(by: [day])
snapshotDatabaseTime: DateTime @search(by: [day])
snapshotTarget: String @search(by: [hash, regexp])
storageThroughput: Int @search
}


type awsRdsDbInstance implements awsBaseService @key(fields: "arn") {
name: String @search(by: [hash, regexp])
Expand Down Expand Up @@ -45,6 +96,7 @@ type awsRdsDbInstance implements awsBaseService @key(fields: "arn") {
encrypted: Boolean @search
licenseModel: String @search(by: [hash, regexp])
tags: [awsRawTag]
snapshots: [awsRdsDbInstanceSnapshot]
cluster: [awsRdsCluster] @hasInverse(field: instances)
kms: [awsKms] @hasInverse(field: rdsDbInstance)
securityGroups: [awsSecurityGroup] @hasInverse(field: rdsDbInstance)
Expand Down
46 changes: 46 additions & 0 deletions src/types/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4044,6 +4044,7 @@ export type AwsRdsDbInstance = AwsBaseService & {
resourceId?: Maybe<Scalars['String']>;
route53HostedZone?: Maybe<Array<Maybe<AwsRoute53HostedZone>>>;
securityGroups?: Maybe<Array<Maybe<AwsSecurityGroup>>>;
snapshots?: Maybe<Array<Maybe<AwsRdsDbInstanceSnapshot>>>;
status?: Maybe<Scalars['String']>;
storageType?: Maybe<Scalars['String']>;
subnet?: Maybe<Array<Maybe<AwsSubnet>>>;
Expand All @@ -4067,6 +4068,51 @@ export type AwsRdsDbInstanceParameterGroup = {
status?: Maybe<Scalars['String']>;
};

export type AwsRdsDbInstanceProcessorFeature = {
id: Scalars['String'];
name?: Maybe<Scalars['String']>;
value?: Maybe<Scalars['String']>;
};

export type AwsRdsDbInstanceSnapshot = {
allocatedStorage?: Maybe<Scalars['Int']>;
availabilityZone?: Maybe<Scalars['String']>;
dBInstanceIdentifier?: Maybe<Scalars['String']>;
dBSnapshotArn?: Maybe<Scalars['String']>;
dBSnapshotIdentifier?: Maybe<Scalars['String']>;
dbiResourceId?: Maybe<Scalars['String']>;
description?: Maybe<Scalars['String']>;
encrypted?: Maybe<Scalars['Boolean']>;
engine?: Maybe<Scalars['String']>;
engineVersion?: Maybe<Scalars['String']>;
groupName?: Maybe<Scalars['String']>;
iAMDatabaseAuthenticationEnabled?: Maybe<Scalars['Boolean']>;
id: Scalars['String'];
instanceCreateTime?: Maybe<Scalars['DateTime']>;
iops?: Maybe<Scalars['Int']>;
kmsKeyId?: Maybe<Scalars['String']>;
licenseModel?: Maybe<Scalars['String']>;
masterUsername?: Maybe<Scalars['String']>;
optionGroupName?: Maybe<Scalars['String']>;
originalSnapshotCreateTime?: Maybe<Scalars['DateTime']>;
percentProgress?: Maybe<Scalars['Int']>;
port?: Maybe<Scalars['Int']>;
processorFeatures?: Maybe<Array<Maybe<AwsRdsDbInstanceProcessorFeature>>>;
snapshotCreateTime?: Maybe<Scalars['DateTime']>;
snapshotDatabaseTime?: Maybe<Scalars['DateTime']>;
snapshotTarget?: Maybe<Scalars['String']>;
snapshotType?: Maybe<Scalars['String']>;
sourceDBSnapshotIdentifier?: Maybe<Scalars['String']>;
sourceRegion?: Maybe<Scalars['String']>;
status?: Maybe<Scalars['String']>;
storageThroughput?: Maybe<Scalars['Int']>;
storageType?: Maybe<Scalars['String']>;
tags?: Maybe<Array<Maybe<AwsRawTag>>>;
tdeCredentialArn?: Maybe<Scalars['String']>;
timezone?: Maybe<Scalars['String']>;
vpcId?: Maybe<Scalars['String']>;
};

export type AwsRdsDbProxies = AwsBaseService & {
auth?: Maybe<Array<Maybe<AwsRdsDbProxiesUserAuthConfigInfo>>>;
createdDate?: Maybe<Scalars['DateTime']>;
Expand Down

0 comments on commit 1377e9a

Please sign in to comment.