Skip to content

Commit c835307

Browse files
committed
feat: add missing cloudwatch services
1 parent f8895e0 commit c835307

15 files changed

Lines changed: 485 additions & 37 deletions

File tree

src/enums/serviceAliases.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ export default {
1515
[services.cloudFormationStackSet]: 'cloudFormationStackSets',
1616
[services.cloudfront]: 'cloudfrontDistributions',
1717
[services.cloudwatchLog]: 'cloudwatchLogs',
18+
[services.cloudwatchDashboard]: 'cloudwatchDashboards',
19+
[services.cloudwatchEventRule]: 'cloudwatchEventRules',
1820
[services.codebuild]: 'codebuilds',
1921
[services.configurationRecorder]: 'configurationRecorders',
2022
[services.dmsReplicationInstance]: 'dmsReplicationInstances',

src/enums/serviceMap.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import CloudTrail from '../services/cloudtrail'
2020
import CloudFormationStack from '../services/cloudFormationStack'
2121
import CloudFormationStackSet from '../services/cloudFormationStackSet'
2222
import CloudWatch from '../services/cloudwatch'
23+
import CloudWatchDashboard from '../services/cloudwatchDashboards'
24+
import CloudWatchEventRule from '../services/cloudwatchEventRules'
2325
import CognitoIdentityPool from '../services/cognitoIdentityPool'
2426
import CognitoUserPool from '../services/cognitoUserPool'
2527
import DynamoDB from '../services/dynamodb'
@@ -131,6 +133,8 @@ export default {
131133
[services.cloudFormationStack]: CloudFormationStack,
132134
[services.cloudFormationStackSet]: CloudFormationStackSet,
133135
[services.cloudwatch]: CloudWatch,
136+
[services.cloudwatchDashboard]: CloudWatchDashboard,
137+
[services.cloudwatchEventRule]: CloudWatchEventRule,
134138
[services.cloudwatchLog]: CloudWatchLog,
135139
[services.codebuild]: CodeBuild,
136140
[services.cognitoIdentityPool]: CognitoIdentityPool,

src/enums/services.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export default {
1717
cloudfront: 'cloudfront',
1818
cloudtrail: 'cloudtrail',
1919
cloudwatch: 'cloudwatch',
20+
cloudwatchDashboard: 'cloudwatchDashboard',
21+
cloudwatchEventRule: 'cloudwatchEventRule',
2022
cloudwatchLog: 'cloudwatchLog',
2123
codebuild: 'codebuild',
2224
cognitoIdentityPool: 'cognitoIdentityPool',

src/properties/logger.ts

Lines changed: 52 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ export default {
9494
`Found ${num} CloudWatch metric alarms to add for ${namespace}`,
9595
gettingCloudwatchAlarms: 'Fetching CloudWatch alarms...',
9696
gettingCloudwatchAlarmTags: 'Fetching tags for each CloudWatch alarm...',
97+
gettingCloudwatchDashboards: 'Fetching CloudWatch dashboards...',
98+
gettingCloudwatchRules: 'Fetching CloudWatch events rules...',
99+
gettingCloudwatchRuleTargets: 'Fetching CloudWatch events rule targets...',
97100
/**
98101
* Kinesis Data Streams
99102
*/
@@ -257,10 +260,11 @@ export default {
257260
/**
258261
* EBS Snapshot
259262
*/
260-
fetchingEbsSnapshotData: 'Fetching EBS Snapshot data for this AWS account via the AWS SDK...',
261-
doneFetchingEbsSnapshotData: '✅ Done fetching EBS Snapshot Data ✅',
262-
fetchedEbsSnapshots: (num: number): string => `Fetched ${num} EBS Snapshots`,
263-
lookingForEbsSnapshot: 'Looking for EBS Snapshots...',
263+
fetchingEbsSnapshotData:
264+
'Fetching EBS Snapshot data for this AWS account via the AWS SDK...',
265+
doneFetchingEbsSnapshotData: '✅ Done fetching EBS Snapshot Data ✅',
266+
fetchedEbsSnapshots: (num: number): string => `Fetched ${num} EBS Snapshots`,
267+
lookingForEbsSnapshot: 'Looking for EBS Snapshots...',
264268
/**
265269
* EC2
266270
*/
@@ -274,8 +278,7 @@ export default {
274278
fetchedNetworkInterfaces: (num: number): string =>
275279
`Fetched ${num} Network Interfaces`,
276280
lookingForNetworkInterfaces: 'Gathering Network Interfaces to add...',
277-
foundKeyPair: (id: string): string =>
278-
`Found Key Pair ${id} for instance`,
281+
foundKeyPair: (id: string): string => `Found Key Pair ${id} for instance`,
279282
doneFetchingEc2Data: '✅ Done fetching EC2 Instance Data ✅',
280283
/**
281284
* RDS
@@ -486,23 +489,25 @@ export default {
486489
`Adding Transit Gateway ${name} to the VPC`,
487490
addingTransitGatewayToRegion: ({ name }): string =>
488491
`Transit Gateway not attached to a VPC, adding Transit Gateway ${name} to the region level`,
489-
fetchedTransitGateways: (num: number): string => `Found ${num} Transit Gateways`,
492+
fetchedTransitGateways: (num: number): string =>
493+
`Found ${num} Transit Gateways`,
490494
/**
491495
* Transit Gateway Attachment
492496
*/
493-
fetchedTransitGatewayAttachments: (num: number): string => `Found ${num} Transit Gateway Attachments`,
494-
/**
497+
fetchedTransitGatewayAttachments: (num: number): string =>
498+
`Found ${num} Transit Gateway Attachments`,
499+
/**
495500
* Transit Gateway Route Tables
496501
*/
497-
fetchedTransitGatewayRouteTables: (num: number): string => `Found ${num} Transit Gateway Route Tables`,
502+
fetchedTransitGatewayRouteTables: (num: number): string =>
503+
`Found ${num} Transit Gateway Route Tables`,
498504
/**
499505
* VPN Gateway
500506
*/
501507
lookingForVpnGateway: 'Looking for Vpn Gateways to add...',
502508
addingVpnGatewayToVpc: (num: number): string =>
503509
`Adding ${num} Vpn Gateways to the VPC`,
504-
fetchedVpnGateways: (num: number): string =>
505-
`Fetched ${num} Vpn Gateways`,
510+
fetchedVpnGateways: (num: number): string => `Fetched ${num} Vpn Gateways`,
506511
/**
507512
* Customer Gateway
508513
*/
@@ -659,57 +664,67 @@ export default {
659664
* NACL
660665
*/
661666
fetchedNacls: (num: number): string => `Fetched ${num} NACLs`,
662-
/**
667+
/**
663668
* Customer Gateway
664669
*/
665-
fetchedCustomerGateways: (num: number): string => `Fetched ${num} Customer Gateways`,
670+
fetchedCustomerGateways: (num: number): string =>
671+
`Fetched ${num} Customer Gateways`,
666672
/**
667673
* CloudWatch Logs
668674
*/
669-
lookingForCloudwatchLogGroups: 'Looking for CloudWatch Log groups to add to Region...',
670-
fetchingCloudwatchLogGroupsData:
671-
'Fetching CloudWatch Log groups for this AWS account via the AWS SDK...',
672-
doneFetchingCloudwatchLogGroupsData: (num: number): string =>
673-
`🕒 Done fetching CloudWatch Log groups in ${num} 🕘`,
674-
foundMoreCloudwatchLogGroups: (num: number): string =>
675-
`Found another ${num} CloudWatch Log groups in this region...`,
676-
gettingCloudwatchLogGroups: 'Fetching CloudWatch Log groups...',
677-
foundMoreCloudwatchMetricFilters: (num: number): string =>
678-
`Found another ${num} CloudWatch Metric filters in this region...`,
675+
lookingForCloudwatchLogGroups:
676+
'Looking for CloudWatch Log groups to add to Region...',
677+
fetchingCloudwatchLogGroupsData:
678+
'Fetching CloudWatch Log groups for this AWS account via the AWS SDK...',
679+
doneFetchingCloudwatchLogGroupsData: (num: number): string =>
680+
`🕒 Done fetching CloudWatch Log groups in ${num} 🕘`,
681+
foundMoreCloudwatchLogGroups: (num: number): string =>
682+
`Found another ${num} CloudWatch Log groups in this region...`,
683+
gettingCloudwatchLogGroups: 'Fetching CloudWatch Log groups...',
684+
foundMoreCloudwatchMetricFilters: (num: number): string =>
685+
`Found another ${num} CloudWatch Metric filters in this region...`,
679686
/**
680687
* Configuration Recorders
681688
*/
682-
fetchedConfigurationRecorders: (num: number): string => `Fetched ${num} Configuration Recorders`,
689+
fetchedConfigurationRecorders: (num: number): string =>
690+
`Fetched ${num} Configuration Recorders`,
683691
/**
684692
* Configuration Recorder Status
685693
*/
686-
fetchedConfigurationRecorderStatus: (num: number): string => `Fetched ${num} Configuration Recorder Status`,
687-
/**
694+
fetchedConfigurationRecorderStatus: (num: number): string =>
695+
`Fetched ${num} Configuration Recorder Status`,
696+
/**
688697
* Vpc Endpoints
689698
*/
690-
fetchedVpcEndpoints: (num: number): string => `Fetched ${num} Vpc Endpoints`,
699+
fetchedVpcEndpoints: (num: number): string => `Fetched ${num} Vpc Endpoints`,
691700
/**
692701
* Access Analyzers
693702
*/
694-
fetchedaccessAnalyzers: (num: number): string => `Found ${num} Access Analyzers`,
703+
fetchedaccessAnalyzers: (num: number): string =>
704+
`Found ${num} Access Analyzers`,
695705
/**
696706
* Managed Prefix Lists
697707
*/
698-
fetchedManagedPrefixLists: (num: number): string => `Found ${num} Managed Prefix Lists`,
699-
fetchedManagedPrefixListEntries: (num: number): string => `Found ${num} Managed Prefix List Entries`,
708+
fetchedManagedPrefixLists: (num: number): string =>
709+
`Found ${num} Managed Prefix Lists`,
710+
fetchedManagedPrefixListEntries: (num: number): string =>
711+
`Found ${num} Managed Prefix List Entries`,
700712
/**
701713
* Vpc Peering Connections
702714
*/
703-
fetchedVpcPeeringConnections: (num: number): string => `Found ${num} Vpc Peering Connections`,
715+
fetchedVpcPeeringConnections: (num: number): string =>
716+
`Found ${num} Vpc Peering Connections`,
704717
/**
705718
* Security Hub
706719
*/
707-
securityHubNotFound: (region: string): string => `Security Hub not found/disabled for region: ${region}`,
708-
fetchedSecurityHub: (region: string): string => `Security Hub found/enabled for region: ${region}`,
709-
fetchingSecurityHub: 'Fetching Security Hub data for this AWS account via the AWS SDK...',
720+
securityHubNotFound: (region: string): string =>
721+
`Security Hub not found/disabled for region: ${region}`,
722+
fetchedSecurityHub: (region: string): string =>
723+
`Security Hub found/enabled for region: ${region}`,
724+
fetchingSecurityHub:
725+
'Fetching Security Hub data for this AWS account via the AWS SDK...',
710726
/**
711727
* Msk
712728
*/
713-
fetchedMskClusters: (num: number): string =>
714-
`Fetched ${num} Msk clusters`,
729+
fetchedMskClusters: (num: number): string => `Fetched ${num} Msk clusters`,
715730
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import CloudGraph from '@cloudgraph/sdk'
2+
import groupBy from 'lodash/groupBy'
3+
import isEmpty from 'lodash/isEmpty'
4+
5+
import CloudWatch, {
6+
DashboardEntries,
7+
ListDashboardsOutput,
8+
ListDashboardsInput,
9+
DashboardEntry,
10+
} from 'aws-sdk/clients/cloudwatch'
11+
import { Config } from 'aws-sdk/lib/config'
12+
import { AWSError } from 'aws-sdk/lib/error'
13+
14+
import awsLoggerText from '../../properties/logger'
15+
import { initTestEndpoint } from '../../utils'
16+
import AwsErrorLog from '../../utils/errorLog'
17+
18+
/**
19+
* Cloudwatch Dashboard
20+
*/
21+
const lt = { ...awsLoggerText }
22+
const { logger } = CloudGraph
23+
const serviceName = 'CloudwatchDashboard'
24+
const errorLog = new AwsErrorLog(serviceName)
25+
const endpoint = initTestEndpoint(serviceName)
26+
27+
export interface RawAwsCloudwatchDashboard extends DashboardEntry {
28+
region: string
29+
}
30+
31+
const listDashboardsForRegion = async ({
32+
cloudwatch,
33+
resolveRegion,
34+
}: {
35+
cloudwatch: CloudWatch
36+
resolveRegion: () => void
37+
}): Promise<DashboardEntries> =>
38+
new Promise<DashboardEntries>(resolve => {
39+
const dashboardList: DashboardEntries = []
40+
const listDashboardOpts: ListDashboardsInput = {}
41+
const listAllDashboard = (token?: string): void => {
42+
if (token) {
43+
listDashboardOpts.NextToken = token
44+
}
45+
try {
46+
cloudwatch.listDashboards(
47+
listDashboardOpts,
48+
(err: AWSError, data: ListDashboardsOutput) => {
49+
const { NextToken: nextToken, DashboardEntries: entries } =
50+
data || {}
51+
if (err) {
52+
errorLog.generateAwsErrorLog({
53+
functionName: 'cloudwatch:listDashboards',
54+
err,
55+
})
56+
}
57+
/**
58+
* No dashboard for this region
59+
*/
60+
if (isEmpty(data)) {
61+
return resolveRegion()
62+
}
63+
64+
dashboardList.push(...entries)
65+
66+
if (nextToken) {
67+
listAllDashboard(nextToken)
68+
} else {
69+
resolve(dashboardList)
70+
}
71+
}
72+
)
73+
} catch (error) {
74+
resolve([])
75+
}
76+
}
77+
listAllDashboard()
78+
})
79+
80+
export default async ({
81+
regions,
82+
config,
83+
}: {
84+
regions: string
85+
config: Config
86+
}): Promise<{ [property: string]: RawAwsCloudwatchDashboard[] }> =>
87+
new Promise(async resolve => {
88+
const cloudwatchData: RawAwsCloudwatchDashboard[] = []
89+
const regionPromises = []
90+
91+
// get all dashboard for all regions
92+
regions.split(',').forEach(region => {
93+
const cloudwatch = new CloudWatch({
94+
...config,
95+
region,
96+
endpoint,
97+
})
98+
const regionPromise = new Promise<void>(async resolveRegion => {
99+
const dashboards = await listDashboardsForRegion({
100+
cloudwatch,
101+
resolveRegion,
102+
})
103+
cloudwatchData.push(
104+
...dashboards.map(dashboard => ({ ...dashboard, region }))
105+
)
106+
resolveRegion()
107+
})
108+
regionPromises.push(regionPromise)
109+
})
110+
111+
logger.debug(lt.gettingCloudwatchDashboards)
112+
await Promise.all(regionPromises)
113+
errorLog.reset()
114+
115+
resolve(groupBy(cloudwatchData, 'region'))
116+
})
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { AwsCloudwatchDashboard } from '../../types/generated'
2+
import { RawAwsCloudwatchDashboard } from './data'
3+
4+
/**
5+
* CloudWatch Dashboard
6+
*/
7+
export default ({
8+
service,
9+
account,
10+
region,
11+
}: {
12+
service: RawAwsCloudwatchDashboard
13+
account: string
14+
region: string
15+
}): AwsCloudwatchDashboard => {
16+
const { DashboardName: name, DashboardArn: arn } = service
17+
return {
18+
id: name,
19+
accountId: account,
20+
arn,
21+
region,
22+
}
23+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Service } from '@cloudgraph/sdk'
2+
import BaseService from '../base'
3+
import format from './format'
4+
import getData from './data'
5+
import mutation from './mutation'
6+
7+
export default class CloudWatchDashboard
8+
extends BaseService
9+
implements Service
10+
{
11+
format = format.bind(this)
12+
13+
getData = getData.bind(this)
14+
15+
mutation = mutation
16+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export default `mutation($input: [AddawsCloudwatchDashboardInput!]!) {
2+
addawsCloudwatchDashboard(input: $input, upsert: true) {
3+
numUids
4+
}
5+
}`
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
type awsCloudwatchDashboard implements awsBaseService @key(fields: "arn")

0 commit comments

Comments
 (0)