Skip to content

Commit

Permalink
remove vpc gateway attachment when vpcid changes
Browse files Browse the repository at this point in the history
  • Loading branch information
laardee committed Oct 3, 2018
1 parent 6da8a8e commit ba6be71
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
8 changes: 7 additions & 1 deletion registry/aws-vpcgatewayattachment/src/index.js
@@ -1,5 +1,5 @@
const AWS = require('aws-sdk')
const { equals, pick } = require('ramda')
const { equals, isEmpty, pick } = require('ramda')

const ec2 = new AWS.EC2({
region: process.env.AWS_DEFAULT_REGION || 'us-east-1'
Expand All @@ -16,6 +16,12 @@ const deploy = async (inputs, context) => {
if (compareStateAndInputs(state, inputs, ['internetGatewayId', 'vpcId'])) {
return { internetGatewayId: state.internetGatewayId }
}

// when vpcId or internetGatewayId changes gateway attachment need to be replaced
if (!isEmpty(state) && !compareStateAndInputs(state, inputs, ['internetGatewayId', 'vpcId'])) {
await remove(inputs, context)
}

context.log('Creating Internet Gateway Attachment')
await ec2
.attachInternetGateway({
Expand Down
42 changes: 42 additions & 0 deletions registry/aws-vpcgatewayattachment/src/index.test.js
Expand Up @@ -54,6 +54,48 @@ describe('#aws-vpcgatewayattachment', () => {
expect(contextMock.saveState).toHaveBeenCalledTimes(1)
})

it('should update the VPC gateway attachment when IGW id changes', async () => {
const contextMock = {
state: {
internetGatewayId: 'igw-abbaabba',
vpcId: 'vpc-abbaabba'
},
log: () => {},
saveState: jest.fn()
}
const inputs = {
internetGatewayId: 'igw-abbabaab',
vpcId: 'vpc-abbaabba'
}
const { internetGatewayId } = await awsVpcgatewayattachmentComponent.deploy(inputs, contextMock)

expect(internetGatewayId).toBe('igw-abbabaab')
expect(AWS.mocks.attachInternetGatewayMock).toHaveBeenCalledTimes(1)
expect(AWS.mocks.detachInternetGatewayMock).toHaveBeenCalledTimes(1)
expect(contextMock.saveState).toHaveBeenCalledTimes(2)
})

it('should update the VPC gateway attachment when VPC id changes', async () => {
const contextMock = {
state: {
internetGatewayId: 'igw-abbaabba',
vpcId: 'vpc-abbaabba'
},
log: () => {},
saveState: jest.fn()
}
const inputs = {
internetGatewayId: 'igw-abbaabba',
vpcId: 'vpc-abbabaab'
}
const { internetGatewayId } = await awsVpcgatewayattachmentComponent.deploy(inputs, contextMock)

expect(internetGatewayId).toBe('igw-abbaabba')
expect(AWS.mocks.attachInternetGatewayMock).toHaveBeenCalledTimes(1)
expect(AWS.mocks.detachInternetGatewayMock).toHaveBeenCalledTimes(1)
expect(contextMock.saveState).toHaveBeenCalledTimes(2)
})

it('should remove the VPC gateway attachment', async () => {
const contextMock = {
state: {
Expand Down

0 comments on commit ba6be71

Please sign in to comment.