From 43ca093c0fb909482d116dfa87200b325370e6d6 Mon Sep 17 00:00:00 2001 From: Aaron Walker Date: Fri, 24 Feb 2023 16:33:49 +0000 Subject: [PATCH] allows custom routes to use a ref --- .github/workflows/rspec.yaml | 23 ++------------ spec/custom_routes_spec.rb | 18 +++++++++++ tests/custom_routes.test.yaml | 7 +++++ vpc-v2.cfndsl.rb | 57 +++++++++++++++++++++-------------- 4 files changed, 63 insertions(+), 42 deletions(-) diff --git a/.github/workflows/rspec.yaml b/.github/workflows/rspec.yaml index b1ee9a7..d0a2ef8 100644 --- a/.github/workflows/rspec.yaml +++ b/.github/workflows/rspec.yaml @@ -3,23 +3,6 @@ name: cftest on: [push, pull_request] jobs: - test: - name: test - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v2 - - name: set up ruby 2.7 - uses: actions/setup-ruby@v1 - with: - ruby-version: 2.7.x - - name: install gems - run: gem install cfhighlander rspec - - name: set cfndsl spec - run: cfndsl -u - - name: cftest - run: rspec - env: - AWS_ACCESS_KEY: ${{ secrets.AWS_ACCESS_KEY }} - AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - AWS_REGION: ap-southeast-2 \ No newline at end of file + rspec: + uses: theonestack/shared-workflows/.github/workflows/rspec.yaml@main + secrets: inherit \ No newline at end of file diff --git a/spec/custom_routes_spec.rb b/spec/custom_routes_spec.rb index 100e177..fde305f 100644 --- a/spec/custom_routes_spec.rb +++ b/spec/custom_routes_spec.rb @@ -47,4 +47,22 @@ end + context 'Resource CustomRoute05' do + + let(:properties) { template["Resources"]["CustomRoute05"]["Properties"] } + + it 'has property RouteTableId' do + expect(properties["RouteTableId"]).to eq({"Ref"=>"RouteTablePrivate0"}) + end + + it 'has property DestinationCidrBlock' do + expect(properties["DestinationCidrBlock"]).to eq("192.168.65.0/22") + end + + it 'has property TransitGatewayId' do + expect(properties["TransitGatewayId"]).to eq({"Ref" => "TransitGateway"}) + end + + end + end diff --git a/tests/custom_routes.test.yaml b/tests/custom_routes.test.yaml index 7b5b48c..f218c11 100644 --- a/tests/custom_routes.test.yaml +++ b/tests/custom_routes.test.yaml @@ -3,6 +3,9 @@ test_metadata: name: custom_routes description: custom routes in code +test_parameters: + TransitGateway: tgw-0a9c82d1928fce121 + tags: Application: MyApp CreatedBy: theonestack @@ -15,3 +18,7 @@ custom_routes: 10.9.0.0/16: tgw-0a9c82d1928fce121 10.10.0.0/16: tgw-0a9c82d1928fce121 192.168.64.0/22: tgw-0a9c82d1928fce121 + 192.168.65.0/22: + type: tgw + value: + Ref: TransitGateway diff --git a/vpc-v2.cfndsl.rb b/vpc-v2.cfndsl.rb index 6188786..1a31083 100644 --- a/vpc-v2.cfndsl.rb +++ b/vpc-v2.cfndsl.rb @@ -112,7 +112,14 @@ ## if custom_routes.length > 0 custom_routes.each_with_index do |(key,value),index| - routeType = value.split('-').first + + if value.is_a?(String) + routeType = value.split('-').first + routeValue = value + else + routeType = value['type'] + routeValue = value['value'] + end EC2_Route("CustomRoutePublic#{index}") { DependsOn ['AttachGateway'] @@ -120,25 +127,25 @@ DestinationCidrBlock key case routeType when "tgw" - TransitGatewayId value + TransitGatewayId routeValue when "eigw" - EgressOnlyInternetGatewayId value + EgressOnlyInternetGatewayId routeValue when "vpce" - VpcEndpointId value + VpcEndpointId routeValue when "vgw" - GatewayId value + GatewayId routeValue when "igw" - GatewayId value + GatewayId routeValue when "nat" - NatGatewayId value + NatGatewayId routeValue when "i" - InstanceId value + InstanceId routeValue when "eni" - NetworkInterfaceId value + NetworkInterfaceId routeValue when "pcx" - VpcPeeringConnectionId value + VpcPeeringConnectionId routeValue when "lgw" - LocalGatewayId value + LocalGatewayId routeValue end } @@ -364,32 +371,38 @@ ## if custom_routes.length > 0 custom_routes.each_with_index do |(key,value),index| - routeType = value.split('-').first + if value.is_a?(String) + routeType = value.split('-').first + routeValue = value + else + routeType = value['type'] + routeValue = value['value'] + end EC2_Route("CustomRoute#{az}#{index}") { RouteTableId Ref("RouteTablePrivate#{az}") DestinationCidrBlock key case routeType when "tgw" - TransitGatewayId value + TransitGatewayId routeValue when "eigw" - EgressOnlyInternetGatewayId value + EgressOnlyInternetGatewayId routeValue when "vpce" - VpcEndpointId value + VpcEndpointId routeValue when "vgw" - GatewayId value + GatewayId routeValue when "igw" - GatewayId value + GatewayId routeValue when "nat" - NatGatewayId value + NatGatewayId routeValue when "i" - InstanceId value + InstanceId routeValue when "eni" - NetworkInterfaceId value + NetworkInterfaceId routeValue when "pcx" - VpcPeeringConnectionId value + VpcPeeringConnectionId routeValue when "lgw" - LocalGatewayId value + LocalGatewayId routeValue end }