Skip to content

Commit 302ff32

Browse files
committed
Merge pull request #17579 from aarya123/fixBrokers
2 parents c473c9a + 8d8adb7 commit 302ff32

8 files changed

Lines changed: 171 additions & 122 deletions

.changelog/17579.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
```release-note:enhancement
2+
resource/aws_msk_cluster: Orders `bootstrap_brokers`, `bootstrap_brokers_sasl_scram`, `bootstrap_brokers_tls`, and `zookeeper_connect_string`
3+
```
4+
5+
```release-note:enhancement
6+
data-source/aws_msk_cluster: Orders `bootstrap_brokers`, `bootstrap_brokers_sasl_scram`, `bootstrap_brokers_tls`, and `zookeeper_connect_string`
7+
```

aws/data_source_aws_msk_cluster.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,16 @@ func dataSourceAwsMskClusterRead(d *schema.ResourceData, meta interface{}) error
9696
ClusterArn: cluster.ClusterArn,
9797
}
9898

99-
bootstrapBrokersoOutput, err := conn.GetBootstrapBrokers(bootstrapBrokersInput)
99+
bootstrapBrokersOutput, err := conn.GetBootstrapBrokers(bootstrapBrokersInput)
100100

101101
if err != nil {
102102
return fmt.Errorf("error reading MSK Cluster (%s) bootstrap brokers: %w", aws.StringValue(cluster.ClusterArn), err)
103103
}
104104

105105
d.Set("arn", aws.StringValue(cluster.ClusterArn))
106-
d.Set("bootstrap_brokers", aws.StringValue(bootstrapBrokersoOutput.BootstrapBrokerString))
107-
d.Set("bootstrap_brokers_sasl_scram", aws.StringValue(bootstrapBrokersoOutput.BootstrapBrokerStringSaslScram))
108-
d.Set("bootstrap_brokers_tls", aws.StringValue(bootstrapBrokersoOutput.BootstrapBrokerStringTls))
106+
d.Set("bootstrap_brokers", sortMskClusterEndpoints(aws.StringValue(bootstrapBrokersOutput.BootstrapBrokerString)))
107+
d.Set("bootstrap_brokers_sasl_scram", sortMskClusterEndpoints(aws.StringValue(bootstrapBrokersOutput.BootstrapBrokerStringSaslScram)))
108+
d.Set("bootstrap_brokers_tls", sortMskClusterEndpoints(aws.StringValue(bootstrapBrokersOutput.BootstrapBrokerStringTls)))
109109
d.Set("cluster_name", aws.StringValue(cluster.ClusterName))
110110
d.Set("kafka_version", aws.StringValue(cluster.CurrentBrokerSoftwareInfo.KafkaVersion))
111111
d.Set("number_of_broker_nodes", aws.Int64Value(cluster.NumberOfBrokerNodes))
@@ -114,7 +114,7 @@ func dataSourceAwsMskClusterRead(d *schema.ResourceData, meta interface{}) error
114114
return fmt.Errorf("error setting tags: %w", err)
115115
}
116116

117-
d.Set("zookeeper_connect_string", aws.StringValue(cluster.ZookeeperConnectString))
117+
d.Set("zookeeper_connect_string", sortMskClusterEndpoints(aws.StringValue(cluster.ZookeeperConnectString)))
118118

119119
d.SetId(aws.StringValue(cluster.ClusterArn))
120120

aws/data_source_aws_msk_cluster_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package aws
22

33
import (
44
"fmt"
5-
"regexp"
65
"testing"
76

87
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -21,16 +20,16 @@ func TestAccAWSMskClusterDataSource_Name(t *testing.T) {
2120
Steps: []resource.TestStep{
2221
{
2322
Config: testAccMskClusterDataSourceConfigName(rName),
24-
Check: resource.ComposeTestCheckFunc(
25-
resource.TestCheckResourceAttrPair(resourceName, "arn", dataSourceName, "arn"),
26-
resource.TestCheckResourceAttr(resourceName, "bootstrap_brokers", ""),
27-
resource.TestCheckResourceAttrPair(resourceName, "bootstrap_brokers_sasl_scram", dataSourceName, "bootstrap_brokers_sasl_scram"),
28-
resource.TestMatchResourceAttr(resourceName, "bootstrap_brokers_tls", regexp.MustCompile(`^(([-\w]+\.){1,}[\w]+:\d+,){2,}([-\w]+\.){1,}[\w]+:\d+$`)), // Hostname ordering not guaranteed between resource and data source reads
29-
resource.TestCheckResourceAttrPair(resourceName, "cluster_name", dataSourceName, "cluster_name"),
30-
resource.TestCheckResourceAttrPair(resourceName, "kafka_version", dataSourceName, "kafka_version"),
31-
resource.TestCheckResourceAttrPair(resourceName, "number_of_broker_nodes", dataSourceName, "number_of_broker_nodes"),
32-
resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceName, "tags.%"),
33-
resource.TestCheckResourceAttrPair(resourceName, "zookeeper_connect_string", dataSourceName, "zookeeper_connect_string"),
23+
Check: resource.ComposeAggregateTestCheckFunc(
24+
resource.TestCheckResourceAttrPair(dataSourceName, "arn", resourceName, "arn"),
25+
resource.TestCheckResourceAttrPair(dataSourceName, "bootstrap_brokers", resourceName, "bootstrap_brokers"),
26+
resource.TestCheckResourceAttrPair(dataSourceName, "bootstrap_brokers_sasl_scram", resourceName, "bootstrap_brokers_sasl_scram"),
27+
resource.TestCheckResourceAttrPair(dataSourceName, "bootstrap_brokers_tls", resourceName, "bootstrap_brokers_tls"),
28+
resource.TestCheckResourceAttrPair(dataSourceName, "cluster_name", resourceName, "cluster_name"),
29+
resource.TestCheckResourceAttrPair(dataSourceName, "kafka_version", resourceName, "kafka_version"),
30+
resource.TestCheckResourceAttrPair(dataSourceName, "number_of_broker_nodes", resourceName, "number_of_broker_nodes"),
31+
resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"),
32+
resource.TestCheckResourceAttrPair(dataSourceName, "zookeeper_connect_string", resourceName, "zookeeper_connect_string"),
3433
),
3534
},
3635
},

aws/provider_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"reflect"
99
"regexp"
10+
"sort"
1011
"strings"
1112
"sync"
1213
"testing"
@@ -1927,6 +1928,27 @@ provider "aws" {
19271928
data "aws_caller_identity" "current" {}
19281929
` //lintignore:AT004
19291930

1931+
func testCheckResourceAttrIsSortedCsv(resourceName, attributeName string) resource.TestCheckFunc {
1932+
return func(s *terraform.State) error {
1933+
is, err := primaryInstanceState(s, resourceName)
1934+
if err != nil {
1935+
return err
1936+
}
1937+
1938+
v, ok := is.Attributes[attributeName]
1939+
if !ok {
1940+
return fmt.Errorf("%s: No attribute %q found", resourceName, attributeName)
1941+
}
1942+
1943+
splitV := strings.Split(v, ",")
1944+
if !sort.StringsAreSorted(splitV) {
1945+
return fmt.Errorf("%s: Expected attribute %q to be sorted, got %q", resourceName, attributeName, v)
1946+
}
1947+
1948+
return nil
1949+
}
1950+
}
1951+
19301952
// composeConfig can be called to concatenate multiple strings to build test configurations
19311953
func composeConfig(config ...string) string {
19321954
var str strings.Builder

aws/resource_aws_msk_cluster.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"sort"
8+
"strings"
79
"time"
810

911
"github.com/aws/aws-sdk-go/aws"
@@ -455,9 +457,9 @@ func resourceAwsMskClusterRead(d *schema.ResourceData, meta interface{}) error {
455457
cluster := out.ClusterInfo
456458

457459
d.Set("arn", aws.StringValue(cluster.ClusterArn))
458-
d.Set("bootstrap_brokers", aws.StringValue(brokerOut.BootstrapBrokerString))
459-
d.Set("bootstrap_brokers_sasl_scram", aws.StringValue(brokerOut.BootstrapBrokerStringSaslScram))
460-
d.Set("bootstrap_brokers_tls", aws.StringValue(brokerOut.BootstrapBrokerStringTls))
460+
d.Set("bootstrap_brokers", sortMskClusterEndpoints(aws.StringValue(brokerOut.BootstrapBrokerString)))
461+
d.Set("bootstrap_brokers_sasl_scram", sortMskClusterEndpoints(aws.StringValue(brokerOut.BootstrapBrokerStringSaslScram)))
462+
d.Set("bootstrap_brokers_tls", sortMskClusterEndpoints(aws.StringValue(brokerOut.BootstrapBrokerStringTls)))
461463

462464
if err := d.Set("broker_node_group_info", flattenMskBrokerNodeGroupInfo(cluster.BrokerNodeGroupInfo)); err != nil {
463465
return fmt.Errorf("error setting broker_node_group_info: %s", err)
@@ -495,7 +497,7 @@ func resourceAwsMskClusterRead(d *schema.ResourceData, meta interface{}) error {
495497
return fmt.Errorf("error setting logging_info: %s", err)
496498
}
497499

498-
d.Set("zookeeper_connect_string", aws.StringValue(cluster.ZookeeperConnectString))
500+
d.Set("zookeeper_connect_string", sortMskClusterEndpoints(aws.StringValue(cluster.ZookeeperConnectString)))
499501

500502
return nil
501503
}
@@ -1206,3 +1208,9 @@ func waitForMskClusterOperation(conn *kafka.Kafka, clusterOperationARN string) e
12061208

12071209
return err
12081210
}
1211+
1212+
func sortMskClusterEndpoints(s string) string {
1213+
splitBootstrapBrokers := strings.Split(s, ",")
1214+
sort.Strings(splitBootstrapBrokers)
1215+
return strings.Join(splitBootstrapBrokers, ",")
1216+
}

0 commit comments

Comments
 (0)