-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.js
278 lines (264 loc) · 10.1 KB
/
lib.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import fs from 'fs/promises'
import path from 'path'
import { execa } from 'execa'
import AWS from 'aws-sdk';
AWS.config.update({region: process.env.AWS_DEFAULT_REGION || 'eu-west-1'});
const tfCheck = async (root) => {
const value = await fs.access(path.join(root, '.terraform')).then(() => true).catch(() => false)
if (!value) {
console.error('Terraform is not initialised in this directory. Please run `terraform init` first.')
process.exit(1)
}
}
const tfCheckResources = async (planfile, options) => {
const verbose = options.verbose
const planFilePath = path.resolve(planfile)
const planFileExists = await fs.access(planFilePath).then(() => true).catch(() => false)
const planFileJsonPath = `${planFilePath}.json`
if(!planFileExists) {
console.error('No plan file found. Please create a terraform plan json first.')
process.exit(1)
}
// execute terraform show -json plan.out | jq > plan.json
const show = await execa('terraform', ['show', '-json', planfile])
const plan = JSON.parse(show.stdout)
const toCreate = plan.resource_changes.filter((change) => change.change.actions.includes('create'))
let uncheckableResourceTypes = [];
let terraformCommands = [];
toCreate.forEach(async (object) => {
if(Object.keys(checkableResources).includes(object.type)) {
if(verbose) console.log(`Checking: ${object.type}.${object.name}`)
const command = await checkableResources[object.type](object, options)
console.log(command)
terraformCommands.push(command)
} else {
if(!uncheckableResourceTypes.includes(object.type)) {
uncheckableResourceTypes.push(object.type)
}
}
})
if(uncheckableResourceTypes.length > 0 && verbose) {
console.log('\nThe following resource types are not currently supported:')
uncheckableResourceTypes.forEach((type) => {
console.log(` ${type}`)
})
}
}
const checkableResources = {
aws_iam_role: async (json, options) => {
const verbose = options.verbose
const role = json.change.after
if(role.hasOwnProperty('name')) {
const iam = new AWS.IAM()
const params = {
RoleName: role.name
}
const result = await iam.getRole(params).promise().then(() => true).catch(() => false)
if(result) {
if(verbose) console.log(`\n${role.name} already exists`)
if(options.delete) {
if(verbose) console.log(`\nDeleting ${role.name}`)
await iam.deleteRole(params).promise()
} else {
// Show import commands
console.log(`Policy ${json.change.after.name} already exists`)
console.log(`Run the following to import this resource into your state:`)
if(options.onepassword) {
console.log(`op run --env-file=.env -- terraform import '${json.address}' ${role.name}`)
} else {
console.log(`terraform import '${json.address}' ${role.name}`)
}
}
}
return result
}
},
aws_iam_policy: async (json, options) => {
const verbose = options.verbose
const policy = json.change.after
if(policy.hasOwnProperty('name')) {
const arn = `arn:aws:iam::394974394191:policy/${policy.name}`
const iam = new AWS.IAM()
try {
await iam.getPolicy({ PolicyArn: arn }).promise()
if(options.delete) {
if(verbose) console.log(`\nPolicy exists. Deleting...`)
try {
await iam.deletePolicy({ PolicyArn: arn }).promise()
} catch (error) {
console.log(`Error deleting policy: ${arn}`)
console.error(error);
}
} else {
console.log(`Policy ${policy.name} already exists`)
console.log(`Run the following to import this resource into your state:`)
if(options.onepassword) {
console.log(`op run --env-file=.env -- terraform import '${json.address}' ${arn}`)
} else {
console.log(`terraform import '${json.address}' ${arn}`)
}
}
} catch (error) {
return false
}
}
},
aws_cloudwatch_log_group: async (json, options) => {
const verbose = options.verbose
const logGroup = json.change.after
if(logGroup.hasOwnProperty('name')) {
const logGroupName = logGroup.name
const logs = new AWS.CloudWatchLogs()
try {
await logs.describeLogGroups({ logGroupNamePrefix: logGroupName }).promise()
if(options.delete) {
if(verbose) console.log(`\nLog group exists. Deleting...`)
try {
const output = await logs.deleteLogGroup({ logGroupName }).promise()
if(verbose) console.log(`Deleted log group: ${logGroupName}`)
} catch (error) {
console.log(`Error deleting log group: ${logGroupName}`)
console.error(error);
}
} else {
console.log(`\nLog group ${logGroupName} already exists`)
console.log(`Run the following to import this resource into your state:`)
if(options.onepassword) {
console.log(`op run --env-file=.env -- terraform import '${json.address}' ${logGroupName}`)
} else {
console.log(`terraform import '${json.address}' ${logGroupName}`)
}
}
} catch (error) {
if(verbose) console.error(error)
return false
}
}
},
aws_eks_cluster: async (json, options) => {
const verbose = options.verbose
const cluster = json.change.after
if(cluster.hasOwnProperty('name')) {
const eks = new AWS.EKS()
const clusterName = cluster.name
try {
await eks.describeCluster({ name: clusterName }).promise()
if(options.delete) {
if(verbose) console.log(`\nCluster exists. Deleting...`)
try {
const output = await eks.deleteCluster({ name: clusterName }).promise()
if(verbose) console.log(`Deleted cluster: ${clusterName}`)
} catch (error) {
console.log(`Error deleting cluster: ${clusterName}`)
console.error(error);
}
} else {
console.log(`\nCluster ${clusterName} already exists`)
console.log(`Run the following to import this resource into your state:`)
console.log(`op run --env-file=.env -- terraform import '${json.address}' ${clusterName}`)
}
} catch (error) {
return false
}
}
},
aws_iam_instance_profile: async (json, options) => {
const verbose = options.verbose
const instanceProfile = json.change.after
if(instanceProfile.hasOwnProperty('name')) {
const iam = new AWS.IAM()
const instanceProfileName = instanceProfile.name
try {
await iam.getInstanceProfile({ InstanceProfileName: instanceProfileName }).promise()
if(options.delete) {
if(verbose) console.log(`\nInstance profile exists. Deleting...`)
try {
const output = await iam.deleteInstanceProfile({ InstanceProfileName: instanceProfileName }).promise()
if(verbose) console.log(`Deleted instance profile: ${instanceProfileName}`)
} catch (error) {
console.log(`Error deleting instance profile: ${instanceProfileName}`)
console.error(error);
}
} else {
console.log(`\nInstance profile ${instanceProfileName} already exists`)
console.log(`Run the following to import this resource into your state:`)
if(options.onepassword) {
console.log(`op run --env-file=.env -- terraform import '${json.address}' ${instanceProfileName}`)
} else {
console.log(`terraform import '${json.address}' ${instanceProfileName}`)
}
}
} catch (error) {
return false
}
}
},
aws_ecr_repository: async (json, options) => {
const verbose = options.verbose
const repository = json.change.after
if(repository.hasOwnProperty('name')) {
const ecr = new AWS.ECR()
const repositoryName = repository.name
try {
await ecr.describeRepositories({ repositoryNames: [repositoryName] }).promise()
if(options.delete) {
if(verbose) console.log(`\nRepository exists. Deleting...`)
try {
const output = await ecr.deleteRepository({ repositoryName }).promise()
if(verbose) console.log(`Deleted repository: ${repositoryName}`)
} catch (error) {
console.log(`Error deleting repository: ${repositoryName}`)
console.error(error);
}
} else {
return outputFunction("Repository", repositoryName, json, options)
}
} catch (error) {
return false
}
}
},
aws_ecr_lifecycle_policy: async (json, options) => {
const verbose = options.verbose
const lifecyclePolicy = json.change.after
if(lifecyclePolicy.hasOwnProperty('repository')) {
const ecr = new AWS.ECR()
const repositoryName = lifecyclePolicy.repository
try {
await ecr.getLifecyclePolicy({ repositoryName }).promise()
if(options.delete) {
if(verbose) console.log(`\nLifecycle policy exists. Deleting...`)
try {
const output = await ecr.deleteLifecyclePolicy({ repositoryName }).promise()
if(verbose) console.log(`Deleted lifecycle policy: ${repositoryName}`)
} catch (error) {
console.log(`Error deleting lifecycle policy: ${repositoryName}`)
console.error(error);
}
} else {
return outputFunction('Lifecycle policy', repositoryName, json, options)
}
} catch (error) {
return false
}
}
}
}
const outputFunction = (type, name, json, options) => {
if(options.output) {
console.log(`\n${type} ${name} already exists`)
console.log(`Run the following to import this resource into your state:`)
}
if(options.onepassword) {
const command = `op run --env-file=.env -- terraform import '${json.address}' ${name}`
return command
} else {
const command = `terraform import '${json.address}' ${name}`
return command
}
}
const hclToJson = async (file) => {
const { stdout } = await execa('hcl2json --', [file])
return JSON.parse(stdout)
}
export { tfCheck, tfCheckResources, hclToJson }