Skip to content

Commit

Permalink
add tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Guibert committed Apr 13, 2021
1 parent 8f8f4a5 commit 9aed083
Show file tree
Hide file tree
Showing 66 changed files with 944 additions and 214 deletions.
35 changes: 21 additions & 14 deletions pkg/analyser/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/zclconf/go-cty/cty/gocty"

"github.com/cloudskiff/driftctl/mocks"
"github.com/cloudskiff/driftctl/pkg/dctlcty"
"github.com/cloudskiff/driftctl/pkg/resource/github"

"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -235,7 +237,7 @@ func TestAnalyze(t *testing.T) {
"bar_foo",
},
},
// TODO Computed: true,
Computed: true,
},
{
Change: diff.Change{
Expand Down Expand Up @@ -268,16 +270,16 @@ func TestAnalyze(t *testing.T) {
"baz",
},
},
// TODO Computed: true,
Computed: true,
},
},
},
},
/* TODO alerts: alerter.Alerts{
"": {
alerts: alerter.Alerts{
"": {
NewComputedDiffAlert(),
},
},*/
},
},
hasDrifted: true,
},
Expand Down Expand Up @@ -345,16 +347,16 @@ func TestAnalyze(t *testing.T) {
"bar_foo",
},
},
// TODO Computed: true,
Computed: true,
},
},
},
},
/* TODO alerts: alerter.Alerts{
alerts: alerter.Alerts{
"": {
NewComputedDiffAlert(),
},
},*/
},
},
hasDrifted: true,
},
Expand Down Expand Up @@ -580,7 +582,7 @@ func TestAnalyze(t *testing.T) {
"bar_foo",
},
},
// todo Computed: true,
Computed: true,
},
{
Change: diff.Change{
Expand Down Expand Up @@ -613,7 +615,7 @@ func TestAnalyze(t *testing.T) {
"baz",
},
},
// todo Computed: true,
Computed: true,
},
{
Change: diff.Change{
Expand All @@ -627,7 +629,7 @@ func TestAnalyze(t *testing.T) {
"0",
},
},
// todo Computed: true,
Computed: true,
},
{
Change: diff.Change{
Expand All @@ -640,7 +642,7 @@ func TestAnalyze(t *testing.T) {
"string",
},
},
// todo Computed: true,
Computed: true,
},
},
},
Expand All @@ -655,9 +657,9 @@ func TestAnalyze(t *testing.T) {
"other.resource": {
&alerter.FakeAlert{Msg: "Should not be ignored"},
},
/* todo "": {
"": {
NewComputedDiffAlert(),
},*/
},
},
},
hasDrifted: true,
Expand Down Expand Up @@ -780,6 +782,11 @@ func TestAnalyze(t *testing.T) {

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
testresource.InitFakeResourceMetadata()
dctlcty.SetMetadata("fakeres", testresource.FakeResourceTags, testresource.FakeResourceNormalizer)
dctlcty.SetMetadata("other", testresource.FakeResourceTags, testresource.FakeResourceNormalizer)
aws.InitResourcesMetadata()
github.InitMetadatas()
for _, r := range c.cloud {
fres, ok := r.(*testresource.FakeResource)
if ok {
Expand Down
21 changes: 3 additions & 18 deletions pkg/cmd/scan/output/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/r3labs/diff/v2"

"github.com/cloudskiff/driftctl/pkg/analyser"
"github.com/cloudskiff/driftctl/pkg/dctlcty"
"github.com/cloudskiff/driftctl/pkg/remote"
"github.com/cloudskiff/driftctl/pkg/resource"
)
Expand Down Expand Up @@ -77,7 +78,8 @@ func (c *Console) Write(analysis *analyser.Analysis) error {
pref = fmt.Sprintf("%s %s:", color.RedString("-"), path)
}
if change.Type == diff.UPDATE {
isJsonString := isFieldJsonString(difference.Res, path)
attr := dctlcty.AsAttrs(difference.Res.CtyValue(), difference.Res.TerraformType())
isJsonString := attr.IsJsonStringField(change.Path)
if isJsonString {
prefix := " "
fmt.Printf(" %s\n%s%s\n", pref, prefix, jsonDiff(change.From, change.To, prefix))
Expand Down Expand Up @@ -181,23 +183,6 @@ func groupByType(resources []resource.Resource) map[string][]resource.Resource {
return result
}

func isFieldJsonString(res resource.Resource, fieldName string) bool {
t := reflect.TypeOf(res)
var field reflect.StructField
var ok bool
if t.Kind() == reflect.Ptr {
field, ok = t.Elem().FieldByName(fieldName)
}
if t.Kind() != reflect.Ptr {
field, ok = t.FieldByName(fieldName)
}
if !ok {
return false
}

return field.Tag.Get("jsonstring") == "true"
}

func jsonDiff(a, b interface{}, prefix string) string {
aStr := fmt.Sprintf("%s", a)
bStr := fmt.Sprintf("%s", b)
Expand Down
43 changes: 43 additions & 0 deletions pkg/cmd/scan/output/console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/zclconf/go-cty/cty/gocty"

"github.com/cloudskiff/driftctl/test/goldenfile"
testresource "github.com/cloudskiff/driftctl/test/resource"

"github.com/cloudskiff/driftctl/pkg/analyser"
)
Expand Down Expand Up @@ -70,6 +72,47 @@ func TestConsole_Write(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
testresource.InitFakeResourceMetadata()
for _, res := range tt.args.analysis.Managed() {
res, ok := res.(*testresource.FakeResource)
if ok {
impliedType, _ := gocty.ImpliedType(res)
value, _ := gocty.ToCtyValue(res, impliedType)
res.CtyVal = &value
continue
}
}

for _, res := range tt.args.analysis.Unmanaged() {
res, ok := res.(*testresource.FakeResource)
if ok {
impliedType, _ := gocty.ImpliedType(res)
value, _ := gocty.ToCtyValue(res, impliedType)
res.CtyVal = &value
continue
}
}

for _, res := range tt.args.analysis.Deleted() {
res, ok := res.(*testresource.FakeResource)
if ok {
impliedType, _ := gocty.ImpliedType(res)
value, _ := gocty.ToCtyValue(res, impliedType)
res.CtyVal = &value
continue
}
}

for _, d := range tt.args.analysis.Differences() {
res, ok := d.Res.(*testresource.FakeResource)
if ok {
impliedType, _ := gocty.ImpliedType(res)
value, _ := gocty.ToCtyValue(res, impliedType)
res.CtyVal = &value
continue
}
}

c := NewConsole()

stdout := os.Stdout // keep backup of the real stdout
Expand Down
5 changes: 5 additions & 0 deletions pkg/dctlcty/cty_attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,8 @@ func (a *CtyAttributes) IsComputedField(path []string) bool {
tags := a.Tags(path)
return tags.Get("computed") == "true"
}

func (a *CtyAttributes) IsJsonStringField(path []string) bool {
tags := a.Tags(path)
return tags.Get("jsonstring") == "true"
}
51 changes: 51 additions & 0 deletions pkg/dctlcty/cty_attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,3 +311,54 @@ func TestCtyAttributes_IsComputedField(t *testing.T) {
})
}
}

func TestCtyAttributes_IsJsonStringField(t *testing.T) {
tests := []struct {
name string
metadata *Metadata
path []string
want bool
}{
{
"Is json",
&Metadata{
tags: map[string]string{
"test.has.tags": "cty:\"instance_tenancy\" computed:\"true\" jsonstring:\"true\"",
},
},
[]string{"test", "has", "tags"},
true,
},
{
"Not json",
&Metadata{
tags: map[string]string{
"test.has.tags": "cty:\"instance_tenancy\"",
},
},
[]string{"test", "has", "tags"},
false,
},
{
"No tags",
&Metadata{
tags: map[string]string{
"test.has.tags": "cty:\"instance_tenancy\"",
},
},
[]string{"test", "has", "no", "tags"},
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := &CtyAttributes{
Attrs: nil,
metadata: tt.metadata,
}
if got := a.IsJsonStringField(tt.path); got != tt.want {
t.Errorf("IsComputedField() = %v, want %v", got, tt.want)
}
})
}
}
10 changes: 7 additions & 3 deletions pkg/driftctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
filter2 "github.com/cloudskiff/driftctl/pkg/filter"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/resource/github"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/cloudskiff/driftctl/test"
testresource "github.com/cloudskiff/driftctl/test/resource"
Expand All @@ -34,6 +35,9 @@ type TestCase struct {
type TestCases []TestCase

func runTest(t *testing.T, cases TestCases) {
testresource.InitFakeResourceMetadata()
aws.InitResourcesMetadata()
github.InitMetadatas()
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
testAlerter := alerter.NewAlerter()
Expand Down Expand Up @@ -188,7 +192,7 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
From: "barfoo",
To: "foobar",
},
// TODO Computed: true,
Computed: true,
})
},
},
Expand Down Expand Up @@ -491,7 +495,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
From: "gp2",
To: "gp3",
},
// TODO Computed: true,
Computed: true,
})
result.AssertResourceHasDrift("vol-018c5ae89895aca4c", "aws_ebs_volume", analyser.Change{
Change: diff.Change{
Expand All @@ -500,7 +504,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
From: true,
To: false,
},
// TODO Computed: true,
Computed: true,
})
},
},
Expand Down
15 changes: 13 additions & 2 deletions pkg/resource/aws/aws_ami.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,19 @@ func (r *AwsAmi) CtyValue() *cty.Value {
return r.CtyVal
}

var awsAmiTags = map[string]string{}
func initAwsAmiMetaData() {
dctlcty.SetMetadata(AwsAmiResourceType, AwsAmiTags, AwsAmiNormalizer)
}

var AwsAmiTags = map[string]string{
"arn": "computed:\"true\"",
"id": "computed:\"true\"",
"image_location": "computed:\"true\"",
"manage_ebs_snapshots": "computed:\"true\"",
"root_snapshot_id": "computed:\"true\"",
"volume_size": "computed:\"true\"",
}

func awsAmiNormalizer(val *dctlcty.CtyAttributes) {
func AwsAmiNormalizer(val *dctlcty.CtyAttributes) {
val.SafeDelete([]string{"timeouts"})
}
23 changes: 21 additions & 2 deletions pkg/resource/aws/aws_cloudfront_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,28 @@ func (r *AwsCloudfrontDistribution) CtyValue() *cty.Value {
return r.CtyVal
}

var awsCloudfrontDistributionTags = map[string]string{}
func initAwsCloudfrontDistributionMetaData() {
dctlcty.SetMetadata(AwsCloudfrontDistributionResourceType, AwsCloudfrontDistributionTags, AwsCloudfrontDistributionNormalizer)
}

var AwsCloudfrontDistributionTags = map[string]string{
"arn": "computed:\"true\"",
"caller_reference": "computed:\"true\"",
"domain_name": "computed:\"true\"",
"etag": "computed:\"true\"",
"hosted_zone_id": "computed:\"true\"",
"id": "computed:\"true\"",
"in_progress_validation_batches": "computed:\"true\"",
"last_modified_time": "computed:\"true\"",
"status": "computed:\"true\"",
"trusted_signers": "computed:\"true\"",
"trusted_signers.enabled": "computed:\"true\"",
"trusted_signers.items": "computed:\"true\"",
"trusted_signers.items.aws_account_number": "computed:\"true\"",
"trusted_signers.items.key_pair_ids": "computed:\"true\"",
}

func awsCloudfrontDistributionNormalizer(val *dctlcty.CtyAttributes) {
func AwsCloudfrontDistributionNormalizer(val *dctlcty.CtyAttributes) {
val.SafeDelete([]string{"etag"})
val.SafeDelete([]string{"last_modified_time"})
val.SafeDelete([]string{"retain_on_delete"})
Expand Down
Loading

0 comments on commit 9aed083

Please sign in to comment.