Merge pull request #516 from cloudskiff/refacto_internet_gtw

Refacto aws_internet_gateway
main
Elie 2021-05-17 18:04:14 +02:00 committed by GitHub
commit 0bd09e25bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 89 additions and 51 deletions

View File

@ -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"
}
}
]

View File

@ -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"

View File

@ -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",

View File

@ -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

View File

@ -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",
},
},
},
},

View File

@ -22,7 +22,7 @@ func TestAcc_AwsInternetGateway(t *testing.T) {
t.Fatal(err)
}
result.AssertInfrastructureIsInSync()
result.Equal(1, result.Summary().TotalManaged)
result.AssertManagedCount(1)
},
},
},

View File

@ -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",