-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathdata.ts
More file actions
130 lines (112 loc) · 3.47 KB
/
data.ts
File metadata and controls
130 lines (112 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import CloudGraph from '@cloudgraph/sdk'
import SSM, { Activation, DescribeActivationsRequest, DescribeActivationsResult } from 'aws-sdk/clients/ssm'
import { AWSError } from 'aws-sdk/lib/error'
import { Config } from 'aws-sdk/lib/config'
import isEmpty from 'lodash/isEmpty'
import groupBy from 'lodash/groupBy'
import awsLoggerText from '../../properties/logger'
import { initTestEndpoint, setAwsRetryOptions } from '../../utils'
import AwsErrorLog from '../../utils/errorLog'
import { API_GATEWAY_CUSTOM_DELAY } from '../../config/constants'
import { AwsTag, TagMap } from '../../types'
import { convertAwsTagsToTagMap } from '../../utils/format'
const lt = { ...awsLoggerText }
const { logger } = CloudGraph
const MAX_ACTIVATIONS = 50
const serviceName = 'ssmActivations'
const errorLog = new AwsErrorLog(serviceName)
const endpoint = initTestEndpoint(serviceName)
const customRetrySettings = setAwsRetryOptions({
baseDelay: API_GATEWAY_CUSTOM_DELAY,
})
/**
* SSM Activation
*/
export const getActivationsForRegion = async (
ssm: SSM
): Promise<Activation[]> =>
new Promise(async resolve => {
const activationList: Activation[] = []
const describeActivationsOpts: DescribeActivationsRequest = {}
const listAllActivations = (token?: string): void => {
describeActivationsOpts.MaxResults = MAX_ACTIVATIONS
if (token) {
describeActivationsOpts.NextToken = token
}
try {
ssm.describeActivations(
describeActivationsOpts,
(err: AWSError, data: DescribeActivationsResult) => {
if (err) {
errorLog.generateAwsErrorLog({
functionName: 'ssm:describeActivations',
err,
})
}
if (isEmpty(data)) {
return resolve([])
}
const { NextToken: nextToken, ActivationList: items = [] } = data || {}
if (isEmpty(items)) {
return resolve([])
}
logger.debug(lt.fetchedSsmActivations(items.length))
activationList.push(...items)
if (nextToken) {
listAllActivations(nextToken)
} else {
resolve(activationList)
}
}
)
} catch (error) {
resolve([])
}
}
listAllActivations()
})
export interface RawAwsSsmActivation extends Omit<Activation, 'Tags'> {
Tags: TagMap
region: string
account
}
export default async ({
regions,
config,
account,
}: {
account: string
regions: string
config: Config
}): Promise<{
[region: string]: RawAwsSsmActivation[]
}> =>
new Promise(async resolve => {
const activationResult: RawAwsSsmActivation[] = []
const regionPromises = regions.split(',').map(region => {
const ssm = new SSM({
...config,
region,
endpoint,
...customRetrySettings,
})
return new Promise<void>(async resolveSsmActivationData => {
// Get SSM Activations
const activations = await getActivationsForRegion(ssm)
if (!isEmpty(activations)) {
activationResult.push(
...activations.map(({Tags, ...activation}) => ({
...activation,
region,
account,
Tags: convertAwsTagsToTagMap(Tags as AwsTag[]),
}))
)
}
resolveSsmActivationData()
})
})
await Promise.all(regionPromises)
errorLog.reset()
resolve(groupBy(activationResult, 'region'))
})