diff --git a/.changelog/38509.txt b/.changelog/38509.txt new file mode 100644 index 00000000000..9185b5ba035 --- /dev/null +++ b/.changelog/38509.txt @@ -0,0 +1,3 @@ +```release-note:bug +resource/aws_cloudsearch_domain: Fix `index_name` character length validation +``` diff --git a/internal/service/cloudsearch/domain.go b/internal/service/cloudsearch/domain.go index ae6dff5ded5..5b12cf918db 100644 --- a/internal/service/cloudsearch/domain.go +++ b/internal/service/cloudsearch/domain.go @@ -126,7 +126,7 @@ func resourceDomain() *schema.Resource { "source_fields": { Type: schema.TypeString, Optional: true, - ValidateFunc: validation.StringDoesNotMatch(regexache.MustCompile(`score`), "Cannot be set to reserved field score"), + ValidateFunc: validation.StringDoesNotMatch(scoreRegex, "Cannot be set to reserved field score"), }, names.AttrType: { Type: schema.TypeString, @@ -145,7 +145,7 @@ func resourceDomain() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: validation.StringMatch(regexache.MustCompile(`^[a-z]([0-9a-z-]){2,27}$`), "Search domain names must start with a lowercase letter (a-z) and be at least 3 and no more than 28 lower-case letters, digits or hyphens"), + ValidateFunc: validation.StringMatch(nameRegex, "Search domain names must start with a lowercase letter (a-z) and be at least 3 and no more than 28 lower-case letters, digits or hyphens"), }, "scaling_parameters": { Type: schema.TypeList, @@ -181,6 +181,12 @@ func resourceDomain() *schema.Resource { } } +var ( + indexNameRegex = regexache.MustCompile(`^(\*?[a-z][0-9a-z_]{2,63}|[a-z][0-9a-z_]{0,63}\*?)$`) + nameRegex = regexache.MustCompile(`^[a-z]([0-9a-z-]){2,27}$`) + scoreRegex = regexache.MustCompile(`score`) +) + func resourceDomainCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics conn := meta.(*conns.AWSClient).CloudSearchClient(ctx) @@ -485,9 +491,9 @@ func resourceDomainDelete(ctx context.Context, d *schema.ResourceData, meta inte func validateIndexName(v interface{}, k string) (ws []string, es []error) { value := v.(string) - if !regexache.MustCompile(`^(\*?[a-z][0-9a-z_]{2,63}|[a-z][0-9a-z_]{2,63}\*?)$`).MatchString(value) { + if !indexNameRegex.MatchString(value) { es = append(es, fmt.Errorf( - "%q must begin with a letter and be at least 3 and no more than 64 characters long", k)) + "%q must begin with a letter and be at least 1 and no more than 64 characters long", k)) } if value == "score" { diff --git a/internal/service/cloudsearch/domain_test.go b/internal/service/cloudsearch/domain_test.go index 8215cdcabb6..be498610647 100644 --- a/internal/service/cloudsearch/domain_test.go +++ b/internal/service/cloudsearch/domain_test.go @@ -121,7 +121,7 @@ func TestAccCloudSearchDomain_indexFields(t *testing.T) { Config: testAccDomainConfig_indexFieldsUpdated(rName), Check: resource.ComposeAggregateTestCheckFunc( testAccDomainExists(ctx, resourceName, &v), - resource.TestCheckResourceAttr(resourceName, "index_field.#", acctest.Ct3), + resource.TestCheckResourceAttr(resourceName, "index_field.#", acctest.Ct4), resource.TestCheckTypeSetElemNestedAttrs(resourceName, "index_field.*", map[string]string{ names.AttrName: "literal_test", names.AttrType: "literal", @@ -141,6 +141,13 @@ func TestAccCloudSearchDomain_indexFields(t *testing.T) { "analysis_scheme": "_en_default_", "highlight": acctest.CtTrue, }), + resource.TestCheckTypeSetElemNestedAttrs(resourceName, "index_field.*", map[string]string{ + names.AttrName: "i", + names.AttrType: "literal", + "return": acctest.CtTrue, + "search": acctest.CtFalse, + "sort": acctest.CtTrue, + }), ), }, }, @@ -402,6 +409,14 @@ resource "aws_cloudsearch_domain" "test" { highlight = true search = true } + + index_field { + name = "i" + type = "literal" + return = true + search = false + sort = true + } } `, rName) } diff --git a/website/docs/r/cloudsearch_domain.html.markdown b/website/docs/r/cloudsearch_domain.html.markdown index c41816ef8eb..cf3132cc457 100644 --- a/website/docs/r/cloudsearch_domain.html.markdown +++ b/website/docs/r/cloudsearch_domain.html.markdown @@ -74,7 +74,7 @@ This configuration block supports the following attributes: This configuration block supports the following attributes: -* `name` - (Required) A unique name for the field. Field names must begin with a letter and be at least 3 and no more than 64 characters long. The allowed characters are: `a`-`z` (lower-case letters), `0`-`9`, and `_` (underscore). The name `score` is reserved and cannot be used as a field name. +* `name` - (Required) A unique name for the field. Field names must begin with a letter and be at least 1 and no more than 64 characters long. The allowed characters are: `a`-`z` (lower-case letters), `0`-`9`, and `_` (underscore). The name `score` is reserved and cannot be used as a field name. * `type` - (Required) The field type. Valid values: `date`, `date-array`, `double`, `double-array`, `int`, `int-array`, `literal`, `literal-array`, `text`, `text-array`. * `analysis_scheme` - (Optional) The analysis scheme you want to use for a `text` field. The analysis scheme specifies the language-specific text processing options that are used during indexing. * `default_value` - (Optional) The default value for the field. This value is used when no value is specified for the field in the document data.