Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More readable aws resources scan output #189

Merged
merged 4 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions pkg/cmd/scan/output/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,11 @@ func (c *Console) Write(analysis *analyser.Analysis) error {
for ty, resources := range deletedByType {
fmt.Printf(" %s:\n", ty)
for _, res := range resources {
stringer, ok := res.(fmt.Stringer)
fmt.Printf(" - %s", res.TerraformId())
if ok {
fmt.Printf(" (%s)", stringer.String())
humanString := res.TerraformId()
if stringer, ok := res.(fmt.Stringer); ok {
humanString = stringer.String()
}
fmt.Println()
fmt.Printf(" - %s\n", humanString)
}
}
}
Expand All @@ -50,25 +49,23 @@ func (c *Console) Write(analysis *analyser.Analysis) error {
for ty, resource := range unmanagedByType {
fmt.Printf(" %s:\n", ty)
for _, res := range resource {
stringer, ok := res.(fmt.Stringer)
fmt.Printf(" - %s", res.TerraformId())
if ok {
fmt.Printf(" (%s)", stringer.String())
humanString := res.TerraformId()
if stringer, ok := res.(fmt.Stringer); ok {
humanString = stringer.String()
}
fmt.Println()
fmt.Printf(" - %s\n", humanString)
}
}
}

if analysis.Summary().TotalDrifted > 0 {
fmt.Printf("Found drifted resources:\n")
for _, difference := range analysis.Differences() {
stringer, ok := difference.Res.(fmt.Stringer)
humanString := difference.Res.TerraformType()
if ok {
humanString := difference.Res.TerraformId()
if stringer, ok := difference.Res.(fmt.Stringer); ok {
eliecharra marked this conversation as resolved.
Show resolved Hide resolved
humanString = stringer.String()
}
fmt.Printf(" - %s (%s):\n", difference.Res.TerraformId(), humanString)
fmt.Printf(" - %s (%s):\n", humanString, difference.Res.TerraformType())
for _, change := range difference.Changelog {
path := strings.Join(change.Path, ".")
pref := fmt.Sprintf("%s %s:", color.YellowString("~"), path)
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/scan/output/testdata/output_stringer_resources.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Found deleted resources:
FakeResourceStringer:
- dfjkgnbsgj (Name: 'deleted resource')
- Name: 'deleted resource'
Found unmanaged resources:
FakeResourceStringer:
- duysgkfdjfdgfhd (Name: 'unmanaged resource')
- Name: 'unmanaged resource'
Found drifted resources:
- gdsfhgkbn (Name: 'resource with diff'):
- Name: 'resource with diff' (FakeResourceStringer):
~ Name: "" => "resource with diff"
Found 3 resource(s)
- 33% coverage
Expand Down
8 changes: 8 additions & 0 deletions pkg/resource/aws/aws_route53_record_ext.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package aws

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -47,3 +48,10 @@ func (r *AwsRoute53Record) NormalizeForState() (resource.Resource, error) {
func (r *AwsRoute53Record) NormalizeForProvider() (resource.Resource, error) {
return r, nil
}

func (r *AwsRoute53Record) String() string {
if r.Name == nil || r.Fqdn == nil || r.Type == nil || r.ZoneId == nil {
return r.TerraformId()
}
return fmt.Sprintf("%s (%s) (Zone: %s)", *r.Fqdn, *r.Type, *r.ZoneId)
}
40 changes: 40 additions & 0 deletions pkg/resource/aws/aws_route53_record_ext_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package aws

import (
"testing"

"github.com/aws/aws-sdk-go/aws"
)

func TestAwsRoute53Record_String(t *testing.T) {
tests := []struct {
name string
record AwsRoute53Record
want string
}{
{name: "test route53 record stringer with name and fqdn and type and zoneId",
record: AwsRoute53Record{
Name: aws.String("example.com"),
Fqdn: aws.String("_github-challenge-cloudskiff.cloudskiff.com"),
Type: aws.String("TXT"),
ZoneId: aws.String("ZOS30SFDAFTU9"),
Id: "ZOS30SFDAFTU9__github-challenge-cloudskiff.cloudskiff.com_TXT",
},
want: "_github-challenge-cloudskiff.cloudskiff.com (TXT) (Zone: ZOS30SFDAFTU9)",
},
{name: "test route53 record stringer without values",
record: AwsRoute53Record{
Name: nil,
Id: "ZOS30SFDAFTU9__github-challenge-cloudskiff.cloudskiff.com_TXT",
},
want: "ZOS30SFDAFTU9__github-challenge-cloudskiff.cloudskiff.com_TXT",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.record.String(); got != tt.want {
t.Errorf("String() = %v, want %v", got, tt.want)
}
})
}
}
9 changes: 8 additions & 1 deletion pkg/resource/aws/aws_route53_zone_ext.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package aws

import (
"fmt"
)

func (r *AwsRoute53Zone) String() string {
return *r.Name
if r.Name == nil {
return r.TerraformId()
}
return fmt.Sprintf("%s (Id: %s)", *r.Name, r.TerraformId())
}
12 changes: 10 additions & 2 deletions pkg/resource/aws/aws_route53_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@ func TestAwsRoute53Zone_String(t *testing.T) {
zone AwsRoute53Zone
want string
}{
{name: "",
{name: "test route53 zone stringer with name and id",
zone: AwsRoute53Zone{
Name: aws.String("example.com"),
Id: "Z04218102KCRRR1DWDYJT",
},
want: "example.com",
want: "example.com (Id: Z04218102KCRRR1DWDYJT)",
},
{name: "test route53 zone stringer without name",
eliecharra marked this conversation as resolved.
Show resolved Hide resolved
zone: AwsRoute53Zone{
Name: nil,
Id: "Z04218102KCRRR1DWDYJT",
},
want: "Z04218102KCRRR1DWDYJT",
},
}
for _, tt := range tests {
Expand Down