Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Cloudsearch Index Fields name lenght restriction #38509

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/38509.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_cloudsearch_domain: Fix `index_name` character length validation
```
14 changes: 10 additions & 4 deletions internal/service/cloudsearch/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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" {
Expand Down
17 changes: 16 additions & 1 deletion internal/service/cloudsearch/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
}),
),
},
},
Expand Down Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/cloudsearch_domain.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading