-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/sqs_to_another_account
- Loading branch information
Showing
9 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
const _ = require("lodash"); | ||
const { ClearResult } = require("./utils"); | ||
const retry = require("async-retry"); | ||
const async = require("async"); | ||
|
||
const regions = [ | ||
"us-east-1", | ||
"us-east-2", | ||
"us-west-1", | ||
"us-west-2", | ||
"ap-south-1", | ||
"ap-northeast-1", | ||
"ap-northeast-2", | ||
"ap-southeast-1", | ||
"ap-southeast-2", | ||
"ca-central-1", | ||
"eu-central-1", | ||
"eu-west-1", | ||
"eu-west-2", | ||
"eu-west-3", | ||
"eu-north-1", | ||
"sa-east-1" | ||
]; | ||
|
||
const deleteNatGateway = async (natGatewayId, region, retryOpts, AWS) => { | ||
const EC2 = new AWS.EC2({ region }); | ||
await retry(async bail => { | ||
try { | ||
await EC2.deleteNatGateway({ | ||
NatGatewayId: natGatewayId | ||
}).promise(); | ||
} catch (e) { | ||
if (e.code !== "Throttling") { | ||
bail(e); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
}, retryOpts); | ||
}; | ||
|
||
const deleteAllNatGateways = async ( | ||
AWS, | ||
retryOpts = { | ||
retries: 3, | ||
minTimeout: 1000 | ||
} | ||
) => { | ||
const allNatGatewaysPromises = regions.map(region => | ||
getAllNatGatewaysInRegion(region, AWS) | ||
); | ||
const allNatGateways = await Promise.all(allNatGatewaysPromises); | ||
const results = []; | ||
const asyncQueue = async.queue(async x => { | ||
try { | ||
await deleteNatGateway(x.natGatewayId, x.region, retryOpts, AWS); | ||
process.stdout.write(".".green); | ||
results.push(ClearResult.getSuccess(x.natGatewayId, x.region)); | ||
} catch (e) { | ||
process.stdout.write("F".red); | ||
results.push(ClearResult.getFailed(x.natGatewayId, x.region, e)); | ||
} | ||
}, 10); | ||
|
||
_.flatten(allNatGateways).forEach(natGateway => { | ||
asyncQueue.push(natGateway); | ||
}); | ||
|
||
await asyncQueue.drain(); | ||
return results; | ||
}; | ||
|
||
const getAllNatGatewaysInRegion = async (region, AWS) => { | ||
const EC2 = new AWS.EC2({ region }); | ||
|
||
let natGateways = []; | ||
let response = {}; | ||
do { | ||
const params = response.NextToken ? { NextToken: response.NextToken } : {}; | ||
response = await EC2.describeNatGateways(params).promise(); | ||
natGateways = natGateways.concat( | ||
response.NatGateways.map(val => { | ||
return { | ||
natGatewayId: val.NatGatewayId, | ||
vpcId: val.VpcId, | ||
region | ||
}; | ||
}) | ||
); | ||
} while (response.NextToken); | ||
|
||
return natGateways; | ||
}; | ||
|
||
const getAllNatGatewaysCount = async AWS => { | ||
const allNatGatewaysPromises = regions.map(region => | ||
getAllNatGatewaysInRegion(region, AWS) | ||
); | ||
const allNatGateways = await Promise.all(allNatGatewaysPromises); | ||
|
||
return _.flatten(allNatGateways).length; | ||
}; | ||
|
||
module.exports = { | ||
deleteAllNatGateways, | ||
getAllNatGatewaysCount | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const { expect } = require("@oclif/test"); | ||
const { getAWSSDK } = require("../../src/lib/aws"); | ||
const { deleteAllNatGateways, getAllNatGatewaysCount } = require("../../src/lib/nat"); | ||
const chaiAsPromised = require("chai-as-promised"); | ||
const chai = require("chai"); | ||
require("colors"); | ||
const { success, fail, getPromiseResponse } = require("../test-utils/jest-mocks"); // Required for avoid fail on console printing | ||
|
||
jest.spyOn(global.console, "log"); | ||
global.console.log.mockImplementation(() => {}); | ||
chai.use(chaiAsPromised); | ||
|
||
describe("delete all NAT Gateways", () => { | ||
let AWS = null; | ||
|
||
beforeEach(() => { | ||
AWS = getAWSSDK(); | ||
|
||
AWS.EC2.prototype.describeNatGateways = getPromiseResponse({ | ||
NatGateways: [ | ||
{ | ||
NatGatewayId: "natgwid", | ||
VpcId: "vpcid" | ||
} | ||
] | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it("Delete all NAT Gateways successfully", async function() { | ||
AWS.EC2.prototype.deleteNatGateway = success; | ||
|
||
const result = await deleteAllNatGateways(AWS); | ||
|
||
expect(result.length).to.equal(16); | ||
result.forEach(val => { | ||
expect(val.status).to.equal("success"); | ||
}); | ||
}); | ||
|
||
it("Fail Deleting all NAT Gateways", async function() { | ||
AWS.EC2.prototype.deleteNatGateway = fail; | ||
|
||
const result = await deleteAllNatGateways(AWS, { retries: 1, minTimeout: 2 }); | ||
|
||
expect(result.length).to.equal(16); | ||
result.forEach(val => { | ||
expect(val.status).to.equal("fail"); | ||
}); | ||
}); | ||
|
||
it("Count number of NAT Gateways", async function() { | ||
const count = await getAllNatGatewaysCount(AWS); | ||
|
||
expect(count).to.equal(16); | ||
}); | ||
}); |