diff --git a/docs/resources/corp_signal_tag.md b/docs/resources/corp_signal_tag.md index 188186a..e5b576f 100644 --- a/docs/resources/corp_signal_tag.md +++ b/docs/resources/corp_signal_tag.md @@ -24,7 +24,7 @@ resource "sigsci_corp_signal_tag" "test" { ### Required -- `short_name` (String) The display name of the signal tag +- `short_name` (String) The display name of the signal tag. Must be 3-25 char. ### Optional diff --git a/docs/resources/site_signal_tag.md b/docs/resources/site_signal_tag.md index 1b95055..cd353a7 100644 --- a/docs/resources/site_signal_tag.md +++ b/docs/resources/site_signal_tag.md @@ -25,7 +25,7 @@ resource "sigsci_site_signal_tag" "test" { ### Required -- `name` (String) The display name of the signal tag +- `name` (String) The display name of the signal tag. Must be 3-25 char. - `site_short_name` (String) Site short name ### Optional diff --git a/provider/resource_corp_signal_tag.go b/provider/resource_corp_signal_tag.go index 51462fb..23f892f 100644 --- a/provider/resource_corp_signal_tag.go +++ b/provider/resource_corp_signal_tag.go @@ -20,9 +20,15 @@ func resourceCorpSignalTag() *schema.Resource { Schema: map[string]*schema.Schema{ "short_name": { Type: schema.TypeString, - Description: "The display name of the signal tag", + Description: "The display name of the signal tag. Must be 3-25 char.", Required: true, ForceNew: true, + ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { + if !validStringLength(val.(string), 3, 25) { + return nil, []error{fmt.Errorf(`received short_name "%q" is invalid. should be min len 3, max len 25`, val.(string))} + } + return nil, nil + }, }, "description": { Type: schema.TypeString, diff --git a/provider/resource_corp_signal_tag_test.go b/provider/resource_corp_signal_tag_test.go index 94cdab6..cd25654 100644 --- a/provider/resource_corp_signal_tag_test.go +++ b/provider/resource_corp_signal_tag_test.go @@ -57,3 +57,29 @@ func testACCCheckCorpSignalTagDestroy(s *terraform.State) error { } return nil } + +func TestResourceCorpSignalTagShortNameValidation(t *testing.T) { + cases := []struct { + value string + expected bool + }{ + {"s", true}, + {"valid-name", false}, + {"this-name-is-way-too-long-for-the-validation-rules", true}, + } + + resource := resourceCorpSignalTag() + nameSchema := resource.Schema["short_name"] + + for _, tc := range cases { + _, errors := nameSchema.ValidateFunc(tc.value, "short_name") + + if tc.expected && len(errors) == 0 { + t.Errorf("Expected an error for value '%s', but got none", tc.value) + } + + if !tc.expected && len(errors) > 0 { + t.Errorf("Did not expect an error for value '%s', but got: %v", tc.value, errors) + } + } +} diff --git a/provider/resource_site.go b/provider/resource_site.go index 9a02e5d..a2669b8 100644 --- a/provider/resource_site.go +++ b/provider/resource_site.go @@ -1,6 +1,8 @@ package provider import ( + "fmt" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/signalsciences/go-sigsci" ) @@ -27,17 +29,35 @@ func resourceSite() *schema.Resource { Description: "Identifying name of the site", Required: true, ForceNew: true, + ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { + if !validStringLength(val.(string), 3, 100) { + return nil, []error{fmt.Errorf(`received short_name "%q" is invalid. should be min len 3, max len 100`, val.(string))} + } + return nil, nil + }, }, "display_name": { Type: schema.TypeString, Description: "Display name of the site", Required: true, + ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { + if !validStringLength(val.(string), 3, 100) { + return nil, []error{fmt.Errorf(`received display_name "%q" is invalid. should be min len 3, max len 100`, val.(string))} + } + return nil, nil + }, }, "agent_level": { Type: schema.TypeString, Description: "Agent action level - 'block', 'log' or 'off'", Optional: true, Default: "log", + ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { + if !existsInString(val.(string), "block", "log", "off") { + return nil, []error{fmt.Errorf(`received agent_level "%q" is invalid. should be "block", "log" or "off"`, val.(string))} + } + return nil, nil + }, }, "agent_anon_mode": { // Has issues on create -- will always be default, will update just fine to the correct value Type: schema.TypeString, diff --git a/provider/resource_site_signal_tag.go b/provider/resource_site_signal_tag.go index 6d5d976..f699af0 100644 --- a/provider/resource_site_signal_tag.go +++ b/provider/resource_site_signal_tag.go @@ -1,6 +1,8 @@ package provider import ( + "fmt" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/signalsciences/go-sigsci" ) @@ -22,14 +24,26 @@ func resourceSiteSignalTag() *schema.Resource { }, "name": { Type: schema.TypeString, - Description: "The display name of the signal tag", + Description: "The display name of the signal tag. Must be 3-25 char.", Required: true, ForceNew: true, // TODO Hopefully this can be changed in the api later + ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { + if !validStringLength(val.(string), 3, 25) { + return nil, []error{fmt.Errorf(`received name %q is invalid. should be min len 3, max len 25`, val.(string))} + } + return nil, nil + }, }, "description": { Type: schema.TypeString, Description: "description", Optional: true, + ValidateFunc: func(val interface{}, key string) (warns []string, errs []error) { + if !validStringLength(val.(string), 0, 140) { + return nil, []error{fmt.Errorf(`received description is invalid. should be max len 140`)} + } + return nil, nil + }, }, "configurable": { Type: schema.TypeBool, diff --git a/provider/resource_site_signal_tag_test.go b/provider/resource_site_signal_tag_test.go index ac1038f..5e85bc5 100644 --- a/provider/resource_site_signal_tag_test.go +++ b/provider/resource_site_signal_tag_test.go @@ -55,3 +55,55 @@ func TestAccResourceSiteSignalTagCRUD(t *testing.T) { }, }) } + +func TestResourceSiteSignalTagNameValidation(t *testing.T) { + cases := []struct { + value string + expected bool + }{ + {"s", true}, + {"valid-name", false}, + {"this-name-is-way-too-long-for-the-validation-rules", true}, + } + + resource := resourceSiteSignalTag() + nameSchema := resource.Schema["name"] + + for _, tc := range cases { + _, errors := nameSchema.ValidateFunc(tc.value, "name") + + if tc.expected && len(errors) == 0 { + t.Errorf("Expected an error for value '%s', but got none", tc.value) + } + + if !tc.expected && len(errors) > 0 { + t.Errorf("Did not expect an error for value '%s', but got: %v", tc.value, errors) + } + } +} + +func TestResourceSiteSignalTagDescriptionValidation(t *testing.T) { + cases := []struct { + value string + expected bool + }{ + {"", false}, + {"valid-description", false}, + {"this-is-way-too-long-for-the-validation-rules-and-this-is-way-too-long-for-the-validation-rules-and-this-is-way-too-long-for-the-validation-rules", true}, + } + + resource := resourceSiteSignalTag() + nameSchema := resource.Schema["description"] + + for _, tc := range cases { + _, errors := nameSchema.ValidateFunc(tc.value, "description") + + if tc.expected && len(errors) == 0 { + t.Errorf("Expected an error for value '%s', but got none", tc.value) + } + + if !tc.expected && len(errors) > 0 { + t.Errorf("Did not expect an error for value '%s', but got: %v", tc.value, errors) + } + } +} diff --git a/provider/resource_site_test.go b/provider/resource_site_test.go index 8267fdb..1896561 100644 --- a/provider/resource_site_test.go +++ b/provider/resource_site_test.go @@ -97,3 +97,83 @@ func testACCCheckSiteDestroy(s *terraform.State) error { } return nil } + +func TestResourceSiteShortNameValidation(t *testing.T) { + cases := []struct { + value string + expected bool + }{ + {"s", true}, + {"valid-name", false}, + {"this-name-is-way-too-long-for-the-validation-rules-and-this-name-is-way-too-long-for-the-validation-rules", true}, + } + + resource := resourceSite() + nameSchema := resource.Schema["short_name"] + + for _, tc := range cases { + _, errors := nameSchema.ValidateFunc(tc.value, "short_name") + + if tc.expected && len(errors) == 0 { + t.Errorf("Expected an error for value '%s', but got none", tc.value) + } + + if !tc.expected && len(errors) > 0 { + t.Errorf("Did not expect an error for value '%s', but got: %v", tc.value, errors) + } + } +} + +func TestResourceSiteDisplayNameValidation(t *testing.T) { + cases := []struct { + value string + expected bool + }{ + {"s", true}, + {"valid-name", false}, + {"this-name-is-way-too-long-for-the-validation-rules-and-this-name-is-way-too-long-for-the-validation-rules", true}, + } + + resource := resourceSite() + nameSchema := resource.Schema["display_name"] + + for _, tc := range cases { + _, errors := nameSchema.ValidateFunc(tc.value, "display_name") + + if tc.expected && len(errors) == 0 { + t.Errorf("Expected an error for value '%s', but got none", tc.value) + } + + if !tc.expected && len(errors) > 0 { + t.Errorf("Did not expect an error for value '%s', but got: %v", tc.value, errors) + } + } +} + +func TestResourceSiteAgentLevelValidation(t *testing.T) { + cases := []struct { + value string + expected bool + }{ + {"block", false}, + {"log", false}, + {"off", false}, + {"foobar", true}, + {"", true}, + } + + resource := resourceSite() + nameSchema := resource.Schema["agent_level"] + + for _, tc := range cases { + _, errors := nameSchema.ValidateFunc(tc.value, "agent_level") + + if tc.expected && len(errors) == 0 { + t.Errorf("Expected an error for value '%s', but got none", tc.value) + } + + if !tc.expected && len(errors) > 0 { + t.Errorf("Did not expect an error for value '%s', but got: %v", tc.value, errors) + } + } +}