Skip to content

Commit

Permalink
feat: add support to define destination cidr
Browse files Browse the repository at this point in the history
  • Loading branch information
limcross committed Aug 7, 2023
1 parent 4eeac38 commit 9f5a457
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ The `@static-ip` pragma allows you to configure the network configuration.
- The `ips` entry allow you to define number of ips
- The `private-subnets` entry allow you to define each private subnet cidr
- The `public-subnets` entry allow you to define each public subnet cidr
- The `destination-cidr` entry allow to define destination cidr for public route table
* The default value is `0.0.0.0/0`

#### Example

Expand Down
10 changes: 9 additions & 1 deletion src/_get-static-ip-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = (staticIp) => {
let privateSubnets = []
let publicSubnets = []
let ips = 1
let destinationCidr = '0.0.0.0/0'

if (staticIp) {
const privateIndex = staticIp.findIndex((param) => param['private-subnets'])
Expand Down Expand Up @@ -36,11 +37,18 @@ module.exports = (staticIp) => {
throw ReferenceError(`Invalid static ip params: Number of ips (${ips}) is greater than the number of public subnets (${publicSubnets.length})`)
}
}

const destinationCidrIndex = staticIp.findIndex((param) => Array.isArray(param) && param[0] == 'destination-cidr')
if (destinationCidrIndex >= 0) {
destinationCidr = staticIp[destinationCidrIndex][1]
validateCidr(destinationCidr)
}
}

return {
privateSubnets,
publicSubnets,
ips
ips,
destinationCidr
}
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
const staticIp = arc['static-ip']
if (!staticIp) return cfn

const { privateSubnets, publicSubnets, ips } = getStaticIpOptions(staticIp)
const { privateSubnets, publicSubnets, ips, destinationCidr } = getStaticIpOptions(staticIp)

cfn.Resources['VPC'] = {
Type: 'AWS::EC2::VPC',
Expand Down Expand Up @@ -52,7 +52,7 @@ module.exports = {
Type: 'AWS::EC2::Route',
Properties: {
RouteTableId: { Ref: 'PublicRouteTable' },
DestinationCidrBlock: '0.0.0.0/0',
DestinationCidrBlock: destinationCidr,
GatewayId: { Ref: 'InternetGateway' }
},
DependsOn: 'AttachGateway'
Expand Down
39 changes: 39 additions & 0 deletions test/unit/src/_get-static-ip-options-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,45 @@ public-subnets
t.deepEqual(publicSubnets, expectedPublicSubnets, 'Got correct public subnets')
})

test('Specify destination cidr', async (t) => {
t.plan(1)
let rawArc = `
@app
app
@static-ip
private-subnets
10.0.1.0/24
10.0.2.0/24
public-subnets
10.0.3.0/24
10.0.4.0/24
destination-cidr 1.0.10.0/24
`
let { inv } = await _inventory({ rawArc })
let arc = inv._project.arc
let { destinationCidr } = getStaticIpOptions(arc['static-ip'])
t.equal(destinationCidr, '1.0.10.0/24', 'Got correct destination cidr')
})

test('Get default destination cidr', async (t) => {
t.plan(1)
let rawArc = `
@app
app
@static-ip
private-subnets
10.0.1.0/24
10.0.2.0/24
public-subnets
10.0.3.0/24
10.0.4.0/24
`
let { inv } = await _inventory({ rawArc })
let arc = inv._project.arc
let { destinationCidr } = getStaticIpOptions(arc['static-ip'])
t.equal(destinationCidr, '0.0.0.0/0', 'Got correct default destination cidr')
})

test('Custom number of ips', async (t) => {
t.plan(1)
let rawArc = `
Expand Down

0 comments on commit 9f5a457

Please sign in to comment.