From e4e07f336145e065ff1faa541cb4ff6e0662aeca Mon Sep 17 00:00:00 2001 From: Bradley Fisher Date: Thu, 2 Sep 2021 08:58:26 -0500 Subject: [PATCH] Add fix for trusted advisor metadata coming in unexpected order (#14) --- CHANGELOG.md | 6 +++++- VERSION | 2 +- pkg/cloudig/trustedadvisor.go | 25 +++++++++++++++++++++---- 3 files changed, 27 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11d5314..2956553 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ -## v0.1.2 ( 9 August 2021) +## v0.1.4 ( 1 September 2021) + +* Add fix for trusted advisor metadata coming in unexpected order + +## v0.1.3 ( 9 August 2021) * Add --exclude-regions option to health command * Add --include-regions option to health command diff --git a/VERSION b/VERSION index 04e1946..8c43fb4 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v0.1.3 +v0.1.4 diff --git a/pkg/cloudig/trustedadvisor.go b/pkg/cloudig/trustedadvisor.go index f973eac..07f8934 100644 --- a/pkg/cloudig/trustedadvisor.go +++ b/pkg/cloudig/trustedadvisor.go @@ -65,10 +65,27 @@ func processTrustedAdvisorResults(results map[*support.TrustedAdvisorCheckDescri for _, resource := range result.FlaggedResources { if resource.Metadata != nil { if !awslocal.SdkStringContains(resource.Metadata, aws.String("Green")) && aws.BoolValue(resource.IsSuppressed) == false { - flaggedResource := aws.StringValue(resource.Metadata[1]) - if aws.StringValue(resource.Metadata[2]) != "" && aws.StringValue(check.Metadata[1]) == "Region" { - flaggedResourceMeta := aws.StringValue(resource.Metadata[2]) - flaggedResource = strings.Join([]string{flaggedResource, flaggedResourceMeta}, "/") + var flaggedResource string + // if not region, important flagged resource will be in field 1 + if len(resource.Metadata) > 1 { + flaggedResource = aws.StringValue(resource.Metadata[1]) + } else if len(resource.Metadata) == 1 { + // only pick metadata 0 if no other option + flaggedResource = aws.StringValue(resource.Metadata[0]) + } + + // clarify which region flaggedResource is part of + regionIndex := -1 + for i, metadata := range check.Metadata { + // Convers Region and Region/AZ and other variations + if strings.Contains(aws.StringValue(metadata), "Region") { + regionIndex = i + break + } + } + if regionIndex > -1 && len(resource.Metadata) >= (regionIndex+1) && aws.StringValue(resource.Metadata[regionIndex]) != "" { + // important resource is after region in metadata + flaggedResource = strings.Join([]string{aws.StringValue(resource.Metadata[regionIndex]), aws.StringValue(resource.Metadata[regionIndex+1])}, "/") } finding.FlaggedResources = append(finding.FlaggedResources, flaggedResource) }