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

f/resource_aws_mq_broker: Adds validation to broker_name and security_groups arguments in resource_aws_mq_broker #18088

Merged
merged 4 commits into from
Mar 31, 2022
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/18088.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_mq_broker: Add validation to broker_name and security_groups arguments
```
14 changes: 11 additions & 3 deletions internal/service/mq/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"strconv"
"strings"

Expand Down Expand Up @@ -58,9 +59,10 @@ func ResourceBroker() *schema.Resource {
Default: false,
},
"broker_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: ValidateBrokerName,
},
"configuration": {
Type: schema.TypeList,
Expand Down Expand Up @@ -264,6 +266,7 @@ func ResourceBroker() *schema.Resource {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
MaxItems: 5,
},
"storage_type": {
Type: schema.TypeString,
Expand Down Expand Up @@ -1093,3 +1096,8 @@ func expandMQLDAPServerMetadata(tfList []interface{}) *mq.LdapServerMetadataInpu

return apiObject
}

var ValidateBrokerName = validation.All(
validation.StringLenBetween(1, 50),
validation.StringMatch(regexp.MustCompile(`^[0-9A-Za-z_-]+$`), ""),
)
31 changes: 31 additions & 0 deletions internal/service/mq/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,37 @@ import (
tfmq "github.com/hashicorp/terraform-provider-aws/internal/service/mq"
)

func TestValidateBrokerName(t *testing.T) {
validNames := []string{
"ValidName",
"V_-dN01e",
"0",
"-",
"_",
strings.Repeat("x", 50),
}
for _, v := range validNames {
_, errors := tfmq.ValidateBrokerName(v, "name")
if len(errors) != 0 {
t.Fatalf("%q should be a valid broker name: %q", v, errors)
}
}

invalidNames := []string{
"Inval:d.~Name",
"Invalid Name",
"*",
"",
strings.Repeat("x", 51),
}
for _, v := range invalidNames {
_, errors := tfmq.ValidateBrokerName(v, "name")
if len(errors) == 0 {
t.Fatalf("%q should be an invalid broker name", v)
}
}
}

func TestBrokerPasswordValidation(t *testing.T) {
cases := []struct {
Value string
Expand Down