driftctl/pkg/middlewares/aws_nat_gateway_eip_assoc.go

59 lines
1.9 KiB
Go
Raw Normal View History

2021-01-20 16:28:56 +00:00
package middlewares
import (
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/sirupsen/logrus"
)
type AwsNatGatewayEipAssoc struct{}
func NewAwsNatGatewayEipAssoc() AwsNatGatewayEipAssoc {
return AwsNatGatewayEipAssoc{}
}
// When creating a nat gateway, we associate an EIP to the gateway
// 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
2021-08-09 14:03:04 +00:00
func (a AwsNatGatewayEipAssoc) Execute(remoteResources, resourcesFromState *[]*resource.Resource) error {
newRemoteResources := make([]*resource.Resource, 0, len(*remoteResources))
2021-01-20 16:28:56 +00:00
for _, remoteResource := range *remoteResources {
// Ignore all resources other than aws_eip_association
if remoteResource.ResourceType() != aws.AwsEipAssociationResourceType {
2021-01-20 16:28:56 +00:00
newRemoteResources = append(newRemoteResources, remoteResource)
continue
}
isAssociatedToNatGateway := false
// Search for a nat gateway associated with our EIP
for _, res := range *remoteResources {
if res.ResourceType() == aws.AwsNatGatewayResourceType {
2021-08-09 14:03:04 +00:00
allocationId, allocationIdExist := res.Attrs.Get("allocation_id")
eipAssocAllocId, eipAssocAllocIdExist := remoteResource.Attrs.Get("allocation_id")
2021-05-17 14:21:45 +00:00
if allocationIdExist && eipAssocAllocIdExist &&
allocationId == eipAssocAllocId {
2021-01-20 16:28:56 +00:00
isAssociatedToNatGateway = true
break
}
}
}
if isAssociatedToNatGateway {
logrus.WithFields(logrus.Fields{
"id": remoteResource.ResourceId(),
"type": remoteResource.ResourceType(),
2021-01-20 16:28:56 +00:00
}).Debug("Ignoring aws_eip_association as it is associated to a nat gateway")
continue
}
2021-08-09 14:03:04 +00:00
newRemoteResources = append(newRemoteResources, remoteResource)
2021-01-20 16:28:56 +00:00
}
*remoteResources = newRemoteResources
return nil
}