driftctl/pkg/middlewares/route53_records.go

66 lines
1.8 KiB
Go
Raw Normal View History

package middlewares
import (
"github.com/sirupsen/logrus"
2021-12-06 13:29:39 +00:00
"github.com/snyk/driftctl/pkg/resource"
"github.com/snyk/driftctl/pkg/resource/aws"
)
// Remote NS and SAO records from remote state if not managed by IAC
type Route53DefaultZoneRecordSanitizer struct{}
func NewRoute53DefaultZoneRecordSanitizer() Route53DefaultZoneRecordSanitizer {
return Route53DefaultZoneRecordSanitizer{}
}
2021-08-09 14:03:04 +00:00
func (m Route53DefaultZoneRecordSanitizer) Execute(remoteResources, resourcesFromState *[]*resource.Resource) error {
2021-08-09 14:03:04 +00:00
newRemoteResources := make([]*resource.Resource, 0)
// We iterate on remote resource and adding them to a new slice except for default records
// added by aws in the zone at creation
for _, remoteResource := range *remoteResources {
existInState := false
// Ignore all resources other than route53 records
if remoteResource.ResourceType() != aws.AwsRoute53RecordResourceType {
newRemoteResources = append(newRemoteResources, remoteResource)
continue
}
2021-08-09 14:03:04 +00:00
if !isDefaultRecord(remoteResource) {
newRemoteResources = append(newRemoteResources, remoteResource)
continue
}
for _, stateResource := range *resourcesFromState {
2021-08-09 14:03:04 +00:00
if remoteResource.Equal(stateResource) {
existInState = true
break
}
}
if existInState {
newRemoteResources = append(newRemoteResources, remoteResource)
}
if !existInState {
logrus.WithFields(logrus.Fields{
"id": remoteResource.ResourceId(),
"type": remoteResource.ResourceType(),
}).Debug("Ignoring default unmanaged record")
}
}
*remoteResources = newRemoteResources
return nil
}
// Return true if the record is considered as default one added by aws
2021-08-09 14:03:04 +00:00
func isDefaultRecord(record *resource.Resource) bool {
2021-05-17 15:03:10 +00:00
ty, _ := record.Attrs.Get("type")
return ty == "NS" || ty == "SOA"
}