Middleware to manage tags_all attribute

main
William Beuil 2021-06-22 16:33:12 +02:00
parent 846b32801a
commit 618d7ebf54
No known key found for this signature in database
GPG Key ID: BED2072C5C2BF537
3 changed files with 130 additions and 0 deletions

View File

@ -97,6 +97,7 @@ func (d DriftCTL) Run() (*analyser.Analysis, error) {
middlewares.NewAwsDefaultSqsQueuePolicy(), middlewares.NewAwsDefaultSqsQueuePolicy(),
middlewares.NewAwsSNSTopicPolicyExpander(d.resourceFactory, d.resourceSchemaRepository), middlewares.NewAwsSNSTopicPolicyExpander(d.resourceFactory, d.resourceSchemaRepository),
middlewares.NewAwsRoleManagedPolicyExpander(d.resourceFactory), middlewares.NewAwsRoleManagedPolicyExpander(d.resourceFactory),
middlewares.NewTagsAllManager(),
) )
if !d.opts.StrictMode { if !d.opts.StrictMode {

View File

@ -0,0 +1,31 @@
package middlewares
import (
"github.com/cloudskiff/driftctl/pkg/resource"
)
// Manage tags_all attribute on each compatible resources
type TagsAllManager struct{}
func NewTagsAllManager() TagsAllManager {
return TagsAllManager{}
}
func (a TagsAllManager) Execute(remoteResources, resourcesFromState *[]resource.Resource) error {
for _, remoteRes := range *remoteResources {
if res, ok := remoteRes.(*resource.AbstractResource); ok {
if _, exist := res.Attrs.Get("tags_all"); exist {
res.Attrs.SafeDelete([]string{"tags_all"})
}
}
}
for _, stateRes := range *resourcesFromState {
if res, ok := stateRes.(*resource.AbstractResource); ok {
if allTags, exist := res.Attrs.Get("tags_all"); exist {
_ = res.Attrs.SafeSet([]string{"tags"}, allTags)
res.Attrs.SafeDelete([]string{"tags_all"})
}
}
}
return nil
}

View File

@ -0,0 +1,98 @@
package middlewares
import (
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws/awsutil"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/r3labs/diff/v2"
)
func TestTagsAllManager_Execute(t *testing.T) {
tests := []struct {
name string
remoteResources *[]resource.Resource
resourcesFromState *[]resource.Resource
wantErr bool
}{
{
name: "With multiple resources that are tags_all compatible",
remoteResources: &[]resource.Resource{
&resource.AbstractResource{
Id: "dummy-instance",
Type: "aws_instance",
Attrs: &resource.Attributes{
"tags": map[string]interface{}{
"Name": "toto",
"Terraform": "true",
},
"tags_all": map[string]interface{}{
"Name": "toto",
"Terraform": "true",
},
},
},
&resource.AbstractResource{
Id: "dummy-ebs-volume",
Type: "aws_ebs_volume",
Attrs: &resource.Attributes{
"tags": map[string]interface{}{
"Name": "tata",
"Terraform": "true",
},
"tags_all": map[string]interface{}{
"Name": "tata",
"Terraform": "true",
},
},
},
},
resourcesFromState: &[]resource.Resource{
&resource.AbstractResource{
Id: "dummy-instance",
Type: "aws_instance",
Attrs: &resource.Attributes{
"tags": map[string]interface{}{
"Name": "toto",
},
"tags_all": map[string]interface{}{
"Name": "toto",
"Terraform": "true",
},
},
},
&resource.AbstractResource{
Id: "dummy-ebs-volume",
Type: "aws_ebs_volume",
Attrs: &resource.Attributes{
"tags": map[string]interface{}{
"Name": "tata",
},
"tags_all": map[string]interface{}{
"Name": "tata",
"Terraform": "true",
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := NewTagsAllManager()
if err := a.Execute(tt.remoteResources, tt.resourcesFromState); (err != nil) != tt.wantErr {
t.Errorf("Execute() error = %v, wantErr %v", err, tt.wantErr)
}
changelog, err := diff.Diff(tt.resourcesFromState, tt.remoteResources)
if err != nil {
t.Error(err)
}
if len(changelog) > 0 {
for _, change := range changelog {
t.Errorf("%s got = %v, want %v", strings.Join(change.Path, "."), awsutil.Prettify(change.From), awsutil.Prettify(change.To))
}
}
})
}
}