From 3d57afd2d917520a71b637b03bc905e7511a8d31 Mon Sep 17 00:00:00 2001 From: Kuba Wieczorek Date: Sun, 14 Mar 2021 23:20:55 +0000 Subject: [PATCH 1/3] Adds validation to broker_name and security_groups arguments in resource_aws_mq_broker --- aws/resource_aws_mq_broker.go | 8 +++++--- aws/validators.go | 5 +++++ aws/validators_test.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_mq_broker.go b/aws/resource_aws_mq_broker.go index 519dd6019bb5..afd9e99f2f52 100644 --- a/aws/resource_aws_mq_broker.go +++ b/aws/resource_aws_mq_broker.go @@ -57,9 +57,10 @@ func resourceAwsMqBroker() *schema.Resource { ForceNew: true, }, "broker_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validateMQBrokerName, }, "configuration": { Type: schema.TypeList, @@ -264,6 +265,7 @@ func resourceAwsMqBroker() *schema.Resource { Type: schema.TypeSet, Elem: &schema.Schema{Type: schema.TypeString}, Optional: true, + MaxItems: 5, }, "storage_type": { Type: schema.TypeString, diff --git a/aws/validators.go b/aws/validators.go index f0b4b2a5aee6..9d4f087f7064 100644 --- a/aws/validators.go +++ b/aws/validators.go @@ -2402,6 +2402,11 @@ var validateCloudWatchEventArchiveName = validation.All( validation.StringMatch(regexp.MustCompile(`^[\.\-_A-Za-z0-9]+`), ""), ) +var validateMQBrokerName = validation.All( + validation.StringLenBetween(1, 50), + validation.StringMatch(regexp.MustCompile(`^[0-9A-Za-z._~-]+$`), ""), +) + var validateServiceDiscoveryNamespaceName = validation.All( validation.StringLenBetween(1, 1024), validation.StringMatch(regexp.MustCompile(`^[0-9A-Za-z._-]+$`), ""), diff --git a/aws/validators_test.go b/aws/validators_test.go index 8ac81ed7b262..4b376e756696 100644 --- a/aws/validators_test.go +++ b/aws/validators_test.go @@ -3206,6 +3206,39 @@ func TestCloudWatchEventCustomEventBusEventSourceName(t *testing.T) { } } +func TestValidateMQBrokerName(t *testing.T) { + validNames := []string{ + "ValidName", + "V_-.~dN01e", + "0", + ".", + "-", + "_", + "~", + strings.Repeat("x", 50), + } + for _, v := range validNames { + _, errors := validateMQBrokerName(v, "name") + if len(errors) != 0 { + t.Fatalf("%q should be a valid broker name: %q", v, errors) + } + } + + invalidNames := []string{ + "Inval:dName", + "Invalid Name", + "*", + "", + strings.Repeat("x", 51), + } + for _, v := range invalidNames { + _, errors := validateMQBrokerName(v, "name") + if len(errors) == 0 { + t.Fatalf("%q should be an invalid broker name", v) + } + } +} + func TestValidateServiceDiscoveryNamespaceName(t *testing.T) { validNames := []string{ "ValidName", From 5e74705b316bee980fb3195b5d098614e90b8983 Mon Sep 17 00:00:00 2001 From: Kuba Wieczorek Date: Sun, 14 Mar 2021 23:34:26 +0000 Subject: [PATCH 2/3] Adds .changelog/18088.txt --- .changelog/18088.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/18088.txt diff --git a/.changelog/18088.txt b/.changelog/18088.txt new file mode 100644 index 000000000000..53142c90a3af --- /dev/null +++ b/.changelog/18088.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_mq_broker: Add validation to broker_name and security_groups arguments +``` From 228d5297d550d7b4eb76b93e1e52b72d9d031f17 Mon Sep 17 00:00:00 2001 From: Kuba Wieczorek Date: Sun, 20 Jun 2021 12:43:23 +0100 Subject: [PATCH 3/3] Fixes validation regex in validateMQBrokerName in aws/validators.go and relevant tests in aws/validators_test.go. Adds info about the new validations to the docs in website/docs/r/mq_broker.html.markdown --- aws/validators.go | 2 +- aws/validators_test.go | 6 ++---- website/docs/r/mq_broker.html.markdown | 2 +- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/aws/validators.go b/aws/validators.go index 9d4f087f7064..330d23c89b2d 100644 --- a/aws/validators.go +++ b/aws/validators.go @@ -2404,7 +2404,7 @@ var validateCloudWatchEventArchiveName = validation.All( var validateMQBrokerName = validation.All( validation.StringLenBetween(1, 50), - validation.StringMatch(regexp.MustCompile(`^[0-9A-Za-z._~-]+$`), ""), + validation.StringMatch(regexp.MustCompile(`^[0-9A-Za-z_-]+$`), ""), ) var validateServiceDiscoveryNamespaceName = validation.All( diff --git a/aws/validators_test.go b/aws/validators_test.go index 4b376e756696..54b1d8d81f0e 100644 --- a/aws/validators_test.go +++ b/aws/validators_test.go @@ -3209,12 +3209,10 @@ func TestCloudWatchEventCustomEventBusEventSourceName(t *testing.T) { func TestValidateMQBrokerName(t *testing.T) { validNames := []string{ "ValidName", - "V_-.~dN01e", + "V_-dN01e", "0", - ".", "-", "_", - "~", strings.Repeat("x", 50), } for _, v := range validNames { @@ -3225,7 +3223,7 @@ func TestValidateMQBrokerName(t *testing.T) { } invalidNames := []string{ - "Inval:dName", + "Inval:d.~Name", "Invalid Name", "*", "", diff --git a/website/docs/r/mq_broker.html.markdown b/website/docs/r/mq_broker.html.markdown index 541ef856da58..bc413370a361 100644 --- a/website/docs/r/mq_broker.html.markdown +++ b/website/docs/r/mq_broker.html.markdown @@ -74,7 +74,7 @@ resource "aws_mq_broker" "example" { The following arguments are required: -* `broker_name` - (Required) Name of the broker. +* `broker_name` - (Required) Name of the broker. This value must be unique in your AWS account, 1-50 characters long, must contain only letters, numbers, dashes, and underscores, and must not contain white spaces, brackets, wildcard characters, or special characters. * `engine_type` - (Required) Type of broker engine. Valid values are `ActiveMQ` and `RabbitMQ`. * `engine_version` - (Required) Version of the broker engine. See the [AmazonMQ Broker Engine docs](https://docs.aws.amazon.com/amazon-mq/latest/developer-guide/broker-engine.html) for supported versions. For example, `5.15.0`. * `host_instance_type` - (Required) Broker's instance type. For example, `mq.t3.micro`, `mq.m5.large`.