From 59ecb3541c8897d799736380bd47a291cb944bd5 Mon Sep 17 00:00:00 2001 From: William Beuil Date: Mon, 17 May 2021 13:24:58 +0200 Subject: [PATCH] Refacto aws_internet_gateway --- .../test/internet_gateway/result.golden.json | 17 +++-- pkg/middlewares/aws_default_igw_route.go | 2 +- pkg/middlewares/aws_default_igw_route_test.go | 37 ++++++---- .../aws_default_internet_gateway.go | 7 +- .../aws_default_internet_gateway_test.go | 73 ++++++++++++------- pkg/resource/aws/aws_internet_gateway_test.go | 2 +- pkg/resource/resource.go | 2 +- 7 files changed, 89 insertions(+), 51 deletions(-) diff --git a/pkg/iac/terraform/state/test/internet_gateway/result.golden.json b/pkg/iac/terraform/state/test/internet_gateway/result.golden.json index 45f8e398..616ceb48 100755 --- a/pkg/iac/terraform/state/test/internet_gateway/result.golden.json +++ b/pkg/iac/terraform/state/test/internet_gateway/result.golden.json @@ -1,12 +1,15 @@ [ { - "Arn": "arn:aws:ec2:eu-west-3:047081014315:internet-gateway/igw-0184eb41aadc62d1c", "Id": "igw-0184eb41aadc62d1c", - "OwnerId": "047081014315", - "Tags": { - "Name": "main" - }, - "VpcId": "vpc-0f3ac2b7909b6bedd", - "CtyVal": {} + "Type": "aws_internet_gateway", + "Attrs": { + "arn": "arn:aws:ec2:eu-west-3:047081014315:internet-gateway/igw-0184eb41aadc62d1c", + "id": "igw-0184eb41aadc62d1c", + "owner_id": "047081014315", + "tags": { + "Name": "main" + }, + "vpc_id": "vpc-0f3ac2b7909b6bedd" + } } ] \ No newline at end of file diff --git a/pkg/middlewares/aws_default_igw_route.go b/pkg/middlewares/aws_default_igw_route.go index d3ba89b3..b75d8c6a 100644 --- a/pkg/middlewares/aws_default_igw_route.go +++ b/pkg/middlewares/aws_default_igw_route.go @@ -63,7 +63,7 @@ func (m AwsDefaultInternetGatewayRoute) Execute(remoteResources, resourcesFromSt func isDefaultInternetGatewayRoute(route *resource.AbstractResource, remoteResources *[]resource.Resource) bool { for _, remoteResource := range *remoteResources { if remoteResource.TerraformType() == aws.AwsInternetGatewayResourceType && - isDefaultInternetGateway(remoteResource.(*aws.AwsInternetGateway), remoteResources) { + isDefaultInternetGateway(remoteResource.(*resource.AbstractResource), remoteResources) { gtwId, gtwIdExist := route.Attrs.Get("gateway_id") destCIDRBlock, destCIDRBlockExist := route.Attrs.Get("destination_cidr_block") return gtwIdExist && destCIDRBlockExist && gtwId == remoteResource.TerraformId() && destCIDRBlock == "0.0.0.0/0" diff --git a/pkg/middlewares/aws_default_igw_route_test.go b/pkg/middlewares/aws_default_igw_route_test.go index 90812d7b..e0359548 100644 --- a/pkg/middlewares/aws_default_igw_route_test.go +++ b/pkg/middlewares/aws_default_igw_route_test.go @@ -4,7 +4,6 @@ import ( "strings" "testing" - awssdk "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awsutil" "github.com/cloudskiff/driftctl/pkg/resource" "github.com/cloudskiff/driftctl/pkg/resource/aws" @@ -24,9 +23,12 @@ func TestAwsDefaultInternetGatewayRoute_Execute(t *testing.T) { &aws.AwsDefaultVpc{ Id: "default-vpc", }, - &aws.AwsInternetGateway{ - Id: "default-igw", - VpcId: awssdk.String("default-vpc"), + &resource.AbstractResource{ + Id: "default-igw", + Type: aws.AwsInternetGatewayResourceType, + Attrs: &resource.Attributes{ + "vpc_id": "default-vpc", + }, }, &resource.AbstractResource{ Id: "default-route-table", @@ -66,9 +68,12 @@ func TestAwsDefaultInternetGatewayRoute_Execute(t *testing.T) { &aws.AwsDefaultVpc{ Id: "default-vpc", }, - &aws.AwsInternetGateway{ - Id: "default-igw", - VpcId: awssdk.String("default-vpc"), + &resource.AbstractResource{ + Id: "default-igw", + Type: aws.AwsInternetGatewayResourceType, + Attrs: &resource.Attributes{ + "vpc_id": "default-vpc", + }, }, &resource.AbstractResource{ Id: "default-route-table", @@ -101,9 +106,12 @@ func TestAwsDefaultInternetGatewayRoute_Execute(t *testing.T) { &aws.AwsDefaultVpc{ Id: "default-vpc", }, - &aws.AwsInternetGateway{ - Id: "default-igw", - VpcId: awssdk.String("default-vpc"), + &resource.AbstractResource{ + Id: "default-igw", + Type: aws.AwsInternetGatewayResourceType, + Attrs: &resource.Attributes{ + "vpc_id": "default-vpc", + }, }, &resource.AbstractResource{ Id: "default-route-table", @@ -153,9 +161,12 @@ func TestAwsDefaultInternetGatewayRoute_Execute(t *testing.T) { &aws.AwsDefaultVpc{ Id: "default-vpc", }, - &aws.AwsInternetGateway{ - Id: "default-igw", - VpcId: awssdk.String("default-vpc"), + &resource.AbstractResource{ + Id: "default-igw", + Type: aws.AwsInternetGatewayResourceType, + Attrs: &resource.Attributes{ + "vpc_id": "default-vpc", + }, }, &resource.AbstractResource{ Id: "default-route-table", diff --git a/pkg/middlewares/aws_default_internet_gateway.go b/pkg/middlewares/aws_default_internet_gateway.go index d3ce9ed4..09648d6d 100644 --- a/pkg/middlewares/aws_default_internet_gateway.go +++ b/pkg/middlewares/aws_default_internet_gateway.go @@ -24,7 +24,7 @@ func (m AwsDefaultInternetGateway) Execute(remoteResources, resourcesFromState * continue } - internetGateway, _ := remoteResource.(*aws.AwsInternetGateway) + internetGateway, _ := remoteResource.(*resource.AbstractResource) // Ignore all non-default internet gateways if !isDefaultInternetGateway(internetGateway, remoteResources) { newRemoteResources = append(newRemoteResources, remoteResource) @@ -59,10 +59,11 @@ func (m AwsDefaultInternetGateway) Execute(remoteResources, resourcesFromState * } // Return true if the internet gateway is the default one (e.g. attached to the default vpc) -func isDefaultInternetGateway(internetGateway *aws.AwsInternetGateway, remoteResources *[]resource.Resource) bool { +func isDefaultInternetGateway(internetGateway *resource.AbstractResource, remoteResources *[]resource.Resource) bool { for _, remoteResource := range *remoteResources { if remoteResource.TerraformType() == aws.AwsDefaultVpcResourceType { - return *internetGateway.VpcId == remoteResource.TerraformId() + vpcId, exist := internetGateway.Attrs.Get("vpc_id") + return exist && vpcId == remoteResource.TerraformId() } } return false diff --git a/pkg/middlewares/aws_default_internet_gateway_test.go b/pkg/middlewares/aws_default_internet_gateway_test.go index b6a5bda1..28fa190f 100644 --- a/pkg/middlewares/aws_default_internet_gateway_test.go +++ b/pkg/middlewares/aws_default_internet_gateway_test.go @@ -4,7 +4,6 @@ import ( "strings" "testing" - awssdk "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awsutil" "github.com/cloudskiff/driftctl/pkg/resource" "github.com/cloudskiff/driftctl/pkg/resource/aws" @@ -31,19 +30,28 @@ func TestAwsDefaultInternetGateway_Execute(t *testing.T) { &aws.AwsVpc{ Id: "dummy-vpc", }, - &aws.AwsInternetGateway{ - Id: "default-igw", - VpcId: awssdk.String("default-vpc"), + &resource.AbstractResource{ + Id: "default-igw", + Type: aws.AwsInternetGatewayResourceType, + Attrs: &resource.Attributes{ + "vpc_id": "default-vpc", + }, }, - &aws.AwsInternetGateway{ - Id: "dummy-igw", - VpcId: awssdk.String("dummy-vpc"), + &resource.AbstractResource{ + Id: "dummy-igw", + Type: aws.AwsInternetGatewayResourceType, + Attrs: &resource.Attributes{ + "vpc_id": "dummy-vpc", + }, }, }, []resource.Resource{ - &aws.AwsInternetGateway{ - Id: "default-igw", - VpcId: awssdk.String("default-vpc"), + &resource.AbstractResource{ + Id: "default-igw", + Type: aws.AwsInternetGatewayResourceType, + Attrs: &resource.Attributes{ + "vpc_id": "default-vpc", + }, }, }, []resource.Resource{ @@ -56,13 +64,19 @@ func TestAwsDefaultInternetGateway_Execute(t *testing.T) { &aws.AwsVpc{ Id: "dummy-vpc", }, - &aws.AwsInternetGateway{ - Id: "default-igw", - VpcId: awssdk.String("default-vpc"), + &resource.AbstractResource{ + Id: "default-igw", + Type: aws.AwsInternetGatewayResourceType, + Attrs: &resource.Attributes{ + "vpc_id": "default-vpc", + }, }, - &aws.AwsInternetGateway{ - Id: "dummy-igw", - VpcId: awssdk.String("dummy-vpc"), + &resource.AbstractResource{ + Id: "dummy-igw", + Type: aws.AwsInternetGatewayResourceType, + Attrs: &resource.Attributes{ + "vpc_id": "dummy-vpc", + }, }, }, }, @@ -78,13 +92,19 @@ func TestAwsDefaultInternetGateway_Execute(t *testing.T) { &aws.AwsVpc{ Id: "dummy-vpc", }, - &aws.AwsInternetGateway{ - Id: "default-igw", - VpcId: awssdk.String("default-vpc"), + &resource.AbstractResource{ + Id: "default-igw", + Type: aws.AwsInternetGatewayResourceType, + Attrs: &resource.Attributes{ + "vpc_id": "default-vpc", + }, }, - &aws.AwsInternetGateway{ - Id: "dummy-igw", - VpcId: awssdk.String("dummy-vpc"), + &resource.AbstractResource{ + Id: "dummy-igw", + Type: aws.AwsInternetGatewayResourceType, + Attrs: &resource.Attributes{ + "vpc_id": "dummy-vpc", + }, }, }, []resource.Resource{}, @@ -98,9 +118,12 @@ func TestAwsDefaultInternetGateway_Execute(t *testing.T) { &aws.AwsVpc{ Id: "dummy-vpc", }, - &aws.AwsInternetGateway{ - Id: "dummy-igw", - VpcId: awssdk.String("dummy-vpc"), + &resource.AbstractResource{ + Id: "dummy-igw", + Type: aws.AwsInternetGatewayResourceType, + Attrs: &resource.Attributes{ + "vpc_id": "dummy-vpc", + }, }, }, }, diff --git a/pkg/resource/aws/aws_internet_gateway_test.go b/pkg/resource/aws/aws_internet_gateway_test.go index 03cf206f..71500de4 100644 --- a/pkg/resource/aws/aws_internet_gateway_test.go +++ b/pkg/resource/aws/aws_internet_gateway_test.go @@ -22,7 +22,7 @@ func TestAcc_AwsInternetGateway(t *testing.T) { t.Fatal(err) } result.AssertInfrastructureIsInSync() - result.Equal(1, result.Summary().TotalManaged) + result.AssertManagedCount(1) }, }, }, diff --git a/pkg/resource/resource.go b/pkg/resource/resource.go index 329bec60..45520d87 100644 --- a/pkg/resource/resource.go +++ b/pkg/resource/resource.go @@ -43,7 +43,7 @@ var refactoredResources = []string{ "aws_iam_user_policy", "aws_iam_user_policy_attachment", "aws_instance", - // "aws_internet_gateway", + "aws_internet_gateway", "aws_key_pair", // "aws_kms_alias", // "aws_kms_key",