2020-12-15 10:07:03 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Default VPC should not be shown as unmanaged as they are present by default
|
2021-01-07 16:53:17 +00:00
|
|
|
// This middleware ignores default VPC from unmanaged resources if they are not managed by IaC
|
2020-12-15 10:07:03 +00:00
|
|
|
type AwsDefaultVPC struct{}
|
|
|
|
|
|
|
|
func NewAwsDefaultVPC() AwsDefaultVPC {
|
|
|
|
return AwsDefaultVPC{}
|
|
|
|
}
|
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
func (m AwsDefaultVPC) Execute(remoteResources, resourcesFromState *[]*resource.Resource) error {
|
2020-12-15 10:07:03 +00:00
|
|
|
|
2021-08-09 14:03:04 +00:00
|
|
|
newRemoteResources := make([]*resource.Resource, 0)
|
2020-12-15 10:07:03 +00:00
|
|
|
|
|
|
|
for _, remoteResource := range *remoteResources {
|
|
|
|
existInState := false
|
|
|
|
|
|
|
|
// Ignore all resources other than default VPC
|
2021-08-18 13:58:28 +00:00
|
|
|
if remoteResource.ResourceType() != aws.AwsDefaultVpcResourceType {
|
2020-12-15 10:07:03 +00:00
|
|
|
newRemoteResources = append(newRemoteResources, remoteResource)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, stateResource := range *resourcesFromState {
|
2021-08-09 14:03:04 +00:00
|
|
|
if remoteResource.Equal(stateResource) {
|
2020-12-15 10:07:03 +00:00
|
|
|
existInState = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if existInState {
|
|
|
|
newRemoteResources = append(newRemoteResources, remoteResource)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !existInState {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
2021-08-18 13:58:28 +00:00
|
|
|
"id": remoteResource.ResourceId(),
|
|
|
|
"type": remoteResource.ResourceType(),
|
2020-12-15 10:07:03 +00:00
|
|
|
}).Debug("Ignoring default VPC as it is not managed by IaC")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
*remoteResources = newRemoteResources
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|