Skip to content

Commit

Permalink
Merge pull request #12 from Optum/feature/health-region-filtering
Browse files Browse the repository at this point in the history
Region filtering for health notifications
  • Loading branch information
shubydo committed Aug 9, 2021
2 parents 65a0201 + 780b6b2 commit 8bad266
Show file tree
Hide file tree
Showing 5 changed files with 218 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v0.1.2 ( 9 August 2021)

* Add --exclude-regions option to health command
* Add --include-regions option to health command

## v0.1.2

Fix bug for trusted advisor where metadata doesn't come in expected order
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.1.2
v0.1.3
24 changes: 20 additions & 4 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ var (
output string
region string
pastDays string
healthExcludeRegions string
healthIncludeRegions string
details bool
ecrImageTag string
identityARNs string
Expand Down Expand Up @@ -98,12 +100,24 @@ var healthCmd = &cobra.Command{
Aliases: []string{"he", "h", "healthnotifications", "healthnotification"},

Run: func(cmd *cobra.Command, args []string) {
excludeRegionsArr := strings.Split(healthExcludeRegions, ",")
if healthExcludeRegions == "" {
excludeRegionsArr = []string{}
}
includeRegionsArr := strings.Split(healthIncludeRegions, ",")
if healthIncludeRegions == "" {
includeRegionsArr = []string{}
}
flags := struct {
Details bool
PastDays string
Details bool
PastDays string
ExcludeRegions []string
IncludeRegions []string
}{
Details: details,
PastDays: pastDays,
Details: details,
PastDays: pastDays,
ExcludeRegions: excludeRegionsArr,
IncludeRegions: includeRegionsArr,
}

execute(&cloudig.HealthReport{
Expand Down Expand Up @@ -213,6 +227,8 @@ func init() {
// healthCmd specific flags
healthCmd.PersistentFlags().BoolVarP(&details, "details", "d", false, "Flag to indicate level of printing for each notification (default false)")
healthCmd.PersistentFlags().StringVar(&pastDays, "pastdays", "", "Number of past days to get results from")
healthCmd.PersistentFlags().StringVar(&healthExcludeRegions, "exclude-regions", "", "Set of regions separated by [,] to exclude Health Notifications from. Takes precedence over include-regions")
healthCmd.PersistentFlags().StringVar(&healthIncludeRegions, "include-regions", "", "Set of regions separated by [,] to include Health Notifications from. Use \"global\" as a region if wanting notifications affecting all regions. Ignored when exclude-regions is set")

// ecrScanCmd specific flags
ecrScanCmd.PersistentFlags().StringVar(&ecrImageTag, "tag", "", "Tag of ECR image(s) to report scan results.")
Expand Down
12 changes: 10 additions & 2 deletions pkg/cloudig/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ type HealthReport struct {
}

type healthReportFlags struct {
Details bool
PastDays string
Details bool
PastDays string
ExcludeRegions []string
IncludeRegions []string
}

type healthReportFinding struct {
Expand Down Expand Up @@ -88,6 +90,12 @@ func (report *HealthReport) GetReport(client awslocal.APIs, comments []Comments)
StatusCode: *details.Event.StatusCode,
EventDescription: eventDes,
}
// exclude takes precedence over include
if len(report.Flags.ExcludeRegions) > 0 && Contains(report.Flags.ExcludeRegions, finding.Region) {
continue
} else if len(report.Flags.IncludeRegions) > 0 && !Contains(report.Flags.IncludeRegions, finding.Region) {
continue
}
report.Findings = append(report.Findings, finding)
}

Expand Down
186 changes: 182 additions & 4 deletions pkg/cloudig/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestGetHealthReport(t *testing.T) {
testCases := []struct {
name string
eventInput []*string
inputFlags healthReportFlags
eventFilter *health.EventFilter
eventAPIResponses []*health.DescribeEventsOutput
detailInput [][]*string
Expand All @@ -32,6 +33,174 @@ func TestGetHealthReport(t *testing.T) {
{
name: "Basic Get Report Run",
eventInput: []*string{nil},
inputFlags: healthReportFlags{
Details: false,
PastDays: "",
},
eventFilter: &health.EventFilter{
EventTypeCategories: []*string{aws.String("accountNotification")},
EventStatusCodes: []*string{aws.String("open"), aws.String("upcoming")},
LastUpdatedTimes: []*health.DateTimeRange{
{},
},
},
eventAPIResponses: []*health.DescribeEventsOutput{
{
Events: []*health.Event{
{
Arn: aws.String("arn1"),
},
},
NextToken: nil,
},
},
detailInput: [][]*string{
{aws.String("arn1")},
},
detailAPIResponses: []*health.DescribeEventDetailsOutput{
{
SuccessfulSet: []*health.EventDetails{
{
Event: &health.Event{
Arn: aws.String("arn1"),
Region: aws.String("region"),
EventTypeCode: aws.String("EVENT_CODE"),
LastUpdatedTime: &time.Time{},
StatusCode: aws.String("status"),
},
EventDescription: &health.EventDescription{
LatestDescription: aws.String("description"),
},
},
},
},
},
entityInputArn: [][]*string{
{aws.String("arn1")},
},
entityInputToken: []*string{nil, aws.String("a token")},
entityAPIResponses: []*health.DescribeAffectedEntitiesOutput{
{
Entities: []*health.AffectedEntity{
{
EntityValue: aws.String("entity value1"),
EventArn: aws.String("arn1"),
},
},
NextToken: nil,
},
},
expectedError: nil,
expectedOutput: []healthReportFinding{
{
AccountID: "account",
AffectedEntities: []string{"entity value1"},
Arn: "arn1",
Comments: "NEW_FINDING",
EventTypeCode: "Event Code",
LastUpdatedTime: "0001-01-01 00:00:00 +0000 UTC",
Region: "region",
StatusCode: "status",
EventDescription: "description",
},
},
},
{
name: "Exclude Region",
eventInput: []*string{nil},
inputFlags: healthReportFlags{
Details: false,
PastDays: "",
ExcludeRegions: []string{"region2"},
},
eventFilter: &health.EventFilter{
EventTypeCategories: []*string{aws.String("accountNotification")},
EventStatusCodes: []*string{aws.String("open"), aws.String("upcoming")},
LastUpdatedTimes: []*health.DateTimeRange{
{},
},
},
eventAPIResponses: []*health.DescribeEventsOutput{
{
Events: []*health.Event{
{
Arn: aws.String("arn1"),
},
},
NextToken: nil,
},
},
detailInput: [][]*string{
{aws.String("arn1")},
},
detailAPIResponses: []*health.DescribeEventDetailsOutput{
{
SuccessfulSet: []*health.EventDetails{
{
Event: &health.Event{
Arn: aws.String("arn1"),
Region: aws.String("region"),
EventTypeCode: aws.String("EVENT_CODE"),
LastUpdatedTime: &time.Time{},
StatusCode: aws.String("status"),
},
EventDescription: &health.EventDescription{
LatestDescription: aws.String("description"),
},
},
{
Event: &health.Event{
Arn: aws.String("arn1"),
Region: aws.String("region2"),
EventTypeCode: aws.String("EVENT_CODE"),
LastUpdatedTime: &time.Time{},
StatusCode: aws.String("status"),
},
EventDescription: &health.EventDescription{
LatestDescription: aws.String("description"),
},
},
},
},
},
entityInputArn: [][]*string{
{aws.String("arn1")},
},
entityInputToken: []*string{nil, aws.String("a token")},
entityAPIResponses: []*health.DescribeAffectedEntitiesOutput{
{
Entities: []*health.AffectedEntity{
{
EntityValue: aws.String("entity value1"),
EventArn: aws.String("arn1"),
},
},
NextToken: nil,
},
},
expectedError: nil,
expectedOutput: []healthReportFinding{
{
AccountID: "account",
AffectedEntities: []string{"entity value1"},
Arn: "arn1",
Comments: "NEW_FINDING",
EventTypeCode: "Event Code",
LastUpdatedTime: "0001-01-01 00:00:00 +0000 UTC",
Region: "region",
StatusCode: "status",
EventDescription: "description",
},
},
},
{
name: "Include Region",
eventInput: []*string{nil},
inputFlags: healthReportFlags{
Details: false,
PastDays: "",
IncludeRegions: []string{"region"},
},
eventFilter: &health.EventFilter{
EventTypeCategories: []*string{aws.String("accountNotification")},
EventStatusCodes: []*string{aws.String("open"), aws.String("upcoming")},
Expand Down Expand Up @@ -67,6 +236,18 @@ func TestGetHealthReport(t *testing.T) {
LatestDescription: aws.String("description"),
},
},
{
Event: &health.Event{
Arn: aws.String("arn1"),
Region: aws.String("region2"),
EventTypeCode: aws.String("EVENT_CODE"),
LastUpdatedTime: &time.Time{},
StatusCode: aws.String("status"),
},
EventDescription: &health.EventDescription{
LatestDescription: aws.String("description"),
},
},
},
},
},
Expand Down Expand Up @@ -118,10 +299,7 @@ func TestGetHealthReport(t *testing.T) {

comments := parseCommentsFile("../../test/data/comments.yaml")
report := &HealthReport{
Flags: healthReportFlags{
Details: false,
PastDays: "",
},
Flags: tc.inputFlags,
}
err := report.GetReport(mockAPIs, comments)

Expand Down

0 comments on commit 8bad266

Please sign in to comment.