driftctl/pkg/middlewares/aws_instance_eip.go

57 lines
1.6 KiB
Go
Raw Normal View History

package middlewares
import (
"github.com/sirupsen/logrus"
"github.com/snyk/driftctl/enumeration/resource"
"github.com/snyk/driftctl/enumeration/resource/aws"
)
type AwsInstanceEIP struct{}
2021-08-09 14:03:04 +00:00
func (a AwsInstanceEIP) Execute(remoteResources, resourcesFromState *[]*resource.Resource) error {
for _, remoteResource := range *remoteResources {
// Ignore all resources other than aws_instance
if remoteResource.ResourceType() != aws.AwsInstanceResourceType {
continue
}
2021-08-09 14:03:04 +00:00
if a.hasEIP(remoteResource, resourcesFromState) {
logrus.WithFields(logrus.Fields{
"instance": remoteResource.ResourceId(),
}).Debug("Ignore instance public ip and dns as it has an eip attached")
2021-08-09 14:03:04 +00:00
a.ignorePublicIpAndDns(remoteResource, remoteResources, resourcesFromState)
}
}
return nil
}
2021-08-09 14:03:04 +00:00
func (a AwsInstanceEIP) hasEIP(instance *resource.Resource, resources *[]*resource.Resource) bool {
2021-04-29 10:32:02 +00:00
for _, res := range *resources {
if res.ResourceType() == aws.AwsEipResourceType {
if (*res.Attrs)["instance"] == instance.ResourceId() {
2021-04-29 10:32:02 +00:00
return true
}
}
if res.ResourceType() == aws.AwsEipAssociationResourceType {
if (*res.Attrs)["instance_id"] == instance.ResourceId() {
2021-04-29 10:32:02 +00:00
return true
}
}
}
return false
}
2021-08-09 14:03:04 +00:00
func (a AwsInstanceEIP) ignorePublicIpAndDns(instance *resource.Resource, resourcesSet ...*[]*resource.Resource) {
2021-04-29 10:32:02 +00:00
for _, resources := range resourcesSet {
for _, res := range *resources {
if res.ResourceType() == instance.ResourceType() &&
res.ResourceId() == instance.ResourceId() {
2021-08-09 14:03:04 +00:00
res.Attrs.SafeDelete([]string{"public_dns"})
res.Attrs.SafeDelete([]string{"public_ip"})
2021-04-29 10:32:02 +00:00
}
}
}
}