Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue - 1616 - run NewAwsNatGatewayEipAssoc after NewAwsNatGatewayEip… #1619

Merged
merged 1 commit into from
Feb 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/driftctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ func (d DriftCTL) Run() (*analyser.Analysis, error) {
middlewares.NewAwsDefaultNetworkACL(),
middlewares.NewAwsDefaultNetworkACLRule(),
middlewares.NewAwsNetworkACLExpander(d.resourceFactory),
middlewares.NewAwsNatGatewayEipAssoc(),
middlewares.NewAwsBucketPolicyExpander(d.resourceFactory),
middlewares.NewAwsSQSQueuePolicyExpander(d.resourceFactory, d.resourceSchemaRepository),
middlewares.NewAwsDefaultSQSQueuePolicy(),
middlewares.NewAwsSNSTopicPolicyExpander(d.resourceFactory, d.resourceSchemaRepository),
middlewares.NewAwsRoleManagedPolicyExpander(d.resourceFactory),
middlewares.NewTagsAllManager(),
middlewares.NewEipAssociationExpander(d.resourceFactory),
middlewares.NewAwsNatGatewayEipAssoc(),
middlewares.NewRDSClusterInstanceExpander(d.resourceFactory),
middlewares.NewAwsApiGatewayDeploymentExpander(d.resourceFactory),
middlewares.NewAwsApiGatewayResourceExpander(d.resourceFactory),
Expand Down
57 changes: 39 additions & 18 deletions pkg/middlewares/aws_nat_gateway_eip_assoc.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ func NewAwsNatGatewayEipAssoc() AwsNatGatewayEipAssoc {
// It implies that driftctl read a aws_eip_association resource from remote
// As we cannot use aws_eip_association in terraform to assign an eip to an aws_nat_gateway
// we should remove this association to ensure we do not output noise in unmanaged resources
func (a AwsNatGatewayEipAssoc) Execute(remoteResources, _ *[]*resource.Resource) error {
func (a AwsNatGatewayEipAssoc) Execute(remoteResources, resourcesFromState *[]*resource.Resource) error {
newRemoteResources := make([]*resource.Resource, 0, len(*remoteResources))
var newResources []*resource.Resource

for _, remoteResource := range *remoteResources {
// Ignore all resources other than aws_eip_association
Expand All @@ -26,33 +27,53 @@ func (a AwsNatGatewayEipAssoc) Execute(remoteResources, _ *[]*resource.Resource)
continue
}

isAssociatedToNatGateway := false

// Search for a nat gateway associated with our EIP
for _, res := range *remoteResources {
if res.ResourceType() == aws.AwsNatGatewayResourceType {
allocationId, allocationIdExist := res.Attrs.Get("allocation_id")
eipAssocAllocId, eipAssocAllocIdExist := remoteResource.Attrs.Get("allocation_id")
if allocationIdExist && eipAssocAllocIdExist &&
allocationId == eipAssocAllocId {
isAssociatedToNatGateway = true
break
}
}
}

if isAssociatedToNatGateway {
if a.isAssociatedToNatGateway(remoteResource, remoteResources) {
logrus.WithFields(logrus.Fields{
"id": remoteResource.ResourceId(),
"type": remoteResource.ResourceType(),
}).Debug("Ignoring aws_eip_association as it is associated to a nat gateway")
}).Debug("Ignoring aws_eip_association from remote resource list as it is associated to a nat gateway")
continue
}

newRemoteResources = append(newRemoteResources, remoteResource)
}

for _, stateResource := range *resourcesFromState {
// Ignore all resources other than aws_eip_association
if stateResource.ResourceType() != aws.AwsEipAssociationResourceType {
newResources = append(newResources, stateResource)
continue
}

if a.isAssociatedToNatGateway(stateResource, remoteResources) {
logrus.WithFields(logrus.Fields{
"id": stateResource.ResourceId(),
"type": stateResource.ResourceType(),
}).Debug("Ignoring aws_eip_association from state resource list as it is associated to a nat gateway")
continue
}

newResources = append(newResources, stateResource)
}

*remoteResources = newRemoteResources
*resourcesFromState = newResources

return nil
}

func (a AwsNatGatewayEipAssoc) isAssociatedToNatGateway(cur *resource.Resource, resourceSet *[]*resource.Resource) bool {
// Search for a nat gateway associated with our EIP
for _, res := range *resourceSet {
if res.ResourceType() == aws.AwsNatGatewayResourceType {
allocationId, allocationIdExist := res.Attrs.Get("allocation_id")
eipAssocAllocId, eipAssocAllocIdExist := cur.Attrs.Get("allocation_id")
if allocationIdExist && eipAssocAllocIdExist &&
allocationId == eipAssocAllocId {
return true
}
}
}

return false
}