Skip to content

Commit

Permalink
Communication Settings: support filter.statues (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamWang-TrendMicro authored Nov 3, 2023
1 parent 5c1d891 commit 1732529
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 3 deletions.
3 changes: 2 additions & 1 deletion conformity/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/trendmicro/terraform-provider-conformity/pkg/cloudconformity"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand All @@ -15,6 +14,8 @@ import (
"strings"
"testing"

"github.com/trendmicro/terraform-provider-conformity/pkg/cloudconformity"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down
12 changes: 11 additions & 1 deletion conformity/resource_conformity_communication_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package conformity
import (
"context"
"fmt"
"github.com/trendmicro/terraform-provider-conformity/pkg/cloudconformity"
"log"
"regexp"
"strings"

"github.com/trendmicro/terraform-provider-conformity/pkg/cloudconformity"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -223,6 +224,14 @@ func resourceConformityCommSetting() *schema.Resource {
"NIST4", "SOC2", "NIST-CSF", "ISO27001", "AGISM", "ASAE-3150", "MAS", "FEDRAMP"}, true),
},
},
"statuses": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{"SUCCESS", "FAILURE"}, true),
},
},
"filter_tags": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -475,6 +484,7 @@ func proccessInputCommSettingFilter(payload *cloudconformity.CommunicationSettin

filter.Categories = expandStringList(f["categories"].(*schema.Set).List())
filter.Compliances = expandStringList(f["compliances"].(*schema.Set).List())
filter.Statuses = expandStringList(f["statuses"].(*schema.Set).List())
filter.FilterTags = expandStringList(f["filter_tags"].(*schema.Set).List())
filter.Regions = expandStringList(f["regions"].(*schema.Set).List())
filter.RiskLevels = expandStringList(f["risk_levels"].(*schema.Set).List())
Expand Down
40 changes: 39 additions & 1 deletion conformity/resource_conformity_communication_setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package conformity

import (
"fmt"
"github.com/trendmicro/terraform-provider-conformity/pkg/cloudconformity"
"regexp"
"testing"

"github.com/trendmicro/terraform-provider-conformity/pkg/cloudconformity"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
Expand All @@ -18,6 +19,8 @@ func TestAccResourceConformityCommSetting(t *testing.T) {
slackUrl := "slack-url"
snsArn := "sns-arn"
snsChannelName := "sns-channel-name"
webhookToken := "#security-token-01"
webhookURL := "web-hook-url"
updatedAccountId := "80b880c9-336a-490d-b212-4e847956a62d"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccConformityPreCheck(t) },
Expand Down Expand Up @@ -49,7 +52,19 @@ func TestAccResourceConformityCommSetting(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("conformity_communication_setting.sns", "sns.0.channel_name", snsChannelName),
resource.TestCheckResourceAttr("conformity_communication_setting.sns", "sns.0.arn", snsArn),
resource.TestCheckResourceAttr("conformity_communication_setting.sns", "filter.0.statuses.0", "SUCCESS"),
),
ExpectNonEmptyPlan: true,
},

{
Config: testAccCheckCommunicationSettingWebhook(webhookToken, webhookURL),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("conformity_communication_setting.webhook", "webhook.0.security_token", webhookToken),
resource.TestCheckResourceAttr("conformity_communication_setting.webhook", "webhook.0.url", webhookURL),
resource.TestCheckResourceAttr("conformity_communication_setting.webhook", "filter.0.statuses.0", "FAILURE"),
),
ExpectNonEmptyPlan: true,
},

{
Expand Down Expand Up @@ -163,6 +178,7 @@ func testAccCheckCommunicationSettingSns(arn, channelName string) string {
}
filter {
categories = [ "security" ]
statuses = ["SUCCESS"]
}
relationships {
account {
Expand All @@ -175,6 +191,28 @@ func testAccCheckCommunicationSettingSns(arn, channelName string) string {
}
`, arn, channelName)
}
func testAccCheckCommunicationSettingWebhook(webhookToken, webhookURL string) string {
return fmt.Sprintf(`
resource "conformity_communication_setting" "webhook" {
webhook {
security_token = "%s"
url = "%s"
}
filter {
categories = [ "security" ]
statuses = ["FAILURE"]
}
relationships {
account {
id = "H19NxM15-"
}
organisation {
id = "ryqMcJn4b"
}
}
}
`, webhookToken, webhookURL)
}
func testAccCheckCommunicationSettingFail() string {
return `
resource "conformity_communication_setting" "multiplefail" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ resource "conformity_communication_setting" "comm_setting" {
// "ISO27001" "AGISM" "ASAE-3150" "MAS" "FEDRAMP"
compliances = []
// optional | type: array of string
// (only used for SNS and webhook channels) An array of statuses strings from the following: "SUCCESS" | "FAILURE"
statuses = []
// optional | type: array of string
filter_tags = []
Expand Down
1 change: 1 addition & 0 deletions example/communication_setting/sns/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ resource "conformity_communication_setting" "sns_setting" {
compliances = [
"FEDRAMP",
]
statuses = ["SUCCESS"]
filter_tags = [
"tagKey",
]
Expand Down
4 changes: 4 additions & 0 deletions example/communication_setting/template.tf
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ resource "conformity_communication_setting" "comm_setting" {
// "ISO27001" "AGISM" "ASAE-3150" "MAS" "FEDRAMP"
compliances = []

// optional | type: array of string
// (only used for SNS and webhook channels) An array of statuses strings from the following: SUCCESS | FAILURE
statuses = []

// optional | type: array of string
filter_tags = []

Expand Down
37 changes: 37 additions & 0 deletions pkg/cloudconformity/create_communication_setting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ func TestCreateCommunicationSettingSuccess(t *testing.T) {
// check the results
assert.Nil(t, err)
assert.Equal(t, response.Data[0].Attributes.Configuration.ChannelName, expectedChannelName)

assert.Equal(t, response.Data[1].Attributes.Configuration.ChannelName, "testSNSChannel")
assert.Equal(t, response.Data[1].Attributes.Filter.Statuses[0], "SUCCESS")
}

func TestCreateCommunicationSettingFail(t *testing.T) {
Expand Down Expand Up @@ -70,6 +73,40 @@ var testCreateCommunicationSettingSuccessResponse = `
}
}
}
},
{
"id": "communication:sns-3JD1mAub8",
"attributes": {
"type": "communication",
"channel": "sns",
"enabled": true,
"filter": {
"regions": ["us-east-1"],
"services": [
"EC2"
],
"statuses": ["SUCCESS"]
},
"configuration": {
"channel_name": "testSNSChannel",
"arn": "sns-t-UoU9CsK"
}
},
"type": "settings",
"relationships": {
"account": {
"data": {
"type": "accounts",
"id": "H19NxM15-"
}
},
"organisation": {
"data": {
"type": "organisations",
"id": "ryqMcJn4b"
}
}
}
}
]
}
Expand Down
1 change: 1 addition & 0 deletions pkg/cloudconformity/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ type CommunicationConfiguration struct {
type CommunicationFilter struct {
Categories []string `json:"categories,omitempty"`
Compliances []string `json:"compliances,omitempty"`
Statuses []string `json:"statuses,omitempty"`
FilterTags []string `json:"filterTags,omitempty"`
Regions []string `json:"regions,omitempty"`
RiskLevels []string `json:"riskLevels,omitempty"`
Expand Down

0 comments on commit 1732529

Please sign in to comment.