driftctl/pkg/middlewares/s3_bucket_acl.go

50 lines
1.3 KiB
Go
Raw Normal View History

package middlewares
import (
"github.com/sirupsen/logrus"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
)
// Remove grant field on remote resources when acl field != private in state
type S3BucketAcl struct{}
func NewS3BucketAcl() S3BucketAcl {
return S3BucketAcl{}
}
func (m S3BucketAcl) Execute(remoteResources, resourcesFromState *[]resource.Resource) error {
for _, iacResource := range *resourcesFromState {
// Ignore all resources other than s3 buckets
if iacResource.TerraformType() != aws.AwsS3BucketResourceType {
continue
}
2021-04-29 14:36:05 +00:00
decodedIacResource, _ := iacResource.(*resource.AbstractResource)
for _, remoteResource := range *remoteResources {
if resource.IsSameResource(remoteResource, decodedIacResource) {
2021-04-29 14:36:05 +00:00
decodedRemoteResource, _ := remoteResource.(*resource.AbstractResource)
aclAttr, exist := decodedIacResource.Attrs.Get("acl")
if !exist || aclAttr == nil || aclAttr == "" {
break
}
if aclAttr != "private" {
logrus.WithFields(logrus.Fields{
"type": decodedRemoteResource.TerraformType(),
"id": decodedRemoteResource.TerraformId(),
}).Debug("Found a resource to update")
2021-04-29 14:36:05 +00:00
decodedRemoteResource.Attrs.SafeDelete([]string{"grant"})
}
break
}
}
2021-04-29 14:36:05 +00:00
decodedIacResource.Attrs.SafeDelete([]string{"acl"})
}
return nil
}