Skip to content

Commit

Permalink
Merge pull request #33805 from hashicorp/f-aws_msk_cluster.cluster_uuid
Browse files Browse the repository at this point in the history
MSK cluster UUID
  • Loading branch information
ewbankkit authored Oct 9, 2023
2 parents 3a33e81 + 6196160 commit 0ae76fd
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 8 deletions.
11 changes: 11 additions & 0 deletions .changelog/33805.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
```release-note:enhancement
resource/aws_msk_cluster: Add `cluster_uuid` attribute
```

```release-note:enhancement
data-source/aws_msk_cluster: Add `cluster_uuid` attribute
```

```release-note:enhancement
resource/aws_msk_serverless_cluster: Add `cluster_uuid` attribute
```
25 changes: 24 additions & 1 deletion internal/service/kafka/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"context"
"fmt"
"log"
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/kafka"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -304,6 +306,10 @@ func ResourceCluster() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 64),
},
"cluster_uuid": {
Type: schema.TypeString,
Computed: true,
},
"configuration_info": {
Type: schema.TypeList,
Optional: true,
Expand Down Expand Up @@ -638,7 +644,8 @@ func resourceClusterRead(ctx context.Context, d *schema.ResourceData, meta inter
return diag.Errorf("reading MSK Cluster (%s) bootstrap brokers: %s", d.Id(), err)
}

d.Set("arn", cluster.ClusterArn)
clusterARN := aws.StringValue(cluster.ClusterArn)
d.Set("arn", clusterARN)
d.Set("bootstrap_brokers", SortEndpointsString(aws.StringValue(output.BootstrapBrokerString)))
d.Set("bootstrap_brokers_public_sasl_iam", SortEndpointsString(aws.StringValue(output.BootstrapBrokerStringPublicSaslIam)))
d.Set("bootstrap_brokers_public_sasl_scram", SortEndpointsString(aws.StringValue(output.BootstrapBrokerStringPublicSaslScram)))
Expand All @@ -664,6 +671,8 @@ func resourceClusterRead(ctx context.Context, d *schema.ResourceData, meta inter
d.Set("client_authentication", nil)
}
d.Set("cluster_name", cluster.ClusterName)
clusterUUID, _ := clusterUUIDFromARN(clusterARN)
d.Set("cluster_uuid", clusterUUID)
if cluster.CurrentBrokerSoftwareInfo != nil {
if err := d.Set("configuration_info", []interface{}{flattenBrokerSoftwareInfo(cluster.CurrentBrokerSoftwareInfo)}); err != nil {
return diag.Errorf("setting configuration_info: %s", err)
Expand Down Expand Up @@ -1877,3 +1886,17 @@ func refreshClusterVersion(ctx context.Context, d *schema.ResourceData, meta int

return nil
}

func clusterUUIDFromARN(clusterARN string) (string, error) {
parsedARN, err := arn.Parse(clusterARN)
if err != nil {
return "", err
}

// arn:${Partition}:kafka:${Region}:${Account}:cluster/${ClusterName}/${Uuid}
parts := strings.Split(parsedARN.Resource, "/")
if len(parts) != 3 || parts[0] != "cluster" || parts[1] == "" || parts[2] == "" {
return "", fmt.Errorf("invalid MSK Cluster ARN (%s)", clusterARN)
}
return parts[2], nil
}
9 changes: 8 additions & 1 deletion internal/service/kafka/cluster_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ func DataSourceCluster() *schema.Resource {
Required: true,
ValidateFunc: validation.StringLenBetween(1, 64),
},
"cluster_uuid": {
Type: schema.TypeString,
Computed: true,
},
"kafka_version": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -125,7 +129,8 @@ func dataSourceClusterRead(ctx context.Context, d *schema.ResourceData, meta int
return sdkdiag.AppendErrorf(diags, "reading MSK Cluster (%s) bootstrap brokers: %s", aws.StringValue(cluster.ClusterArn), err)
}

d.Set("arn", cluster.ClusterArn)
clusterARN := aws.StringValue(cluster.ClusterArn)
d.Set("arn", clusterARN)
d.Set("bootstrap_brokers", SortEndpointsString(aws.StringValue(bootstrapBrokersOutput.BootstrapBrokerString)))
d.Set("bootstrap_brokers_public_sasl_iam", SortEndpointsString(aws.StringValue(bootstrapBrokersOutput.BootstrapBrokerStringPublicSaslIam)))
d.Set("bootstrap_brokers_public_sasl_scram", SortEndpointsString(aws.StringValue(bootstrapBrokersOutput.BootstrapBrokerStringPublicSaslScram)))
Expand All @@ -134,6 +139,8 @@ func dataSourceClusterRead(ctx context.Context, d *schema.ResourceData, meta int
d.Set("bootstrap_brokers_sasl_scram", SortEndpointsString(aws.StringValue(bootstrapBrokersOutput.BootstrapBrokerStringSaslScram)))
d.Set("bootstrap_brokers_tls", SortEndpointsString(aws.StringValue(bootstrapBrokersOutput.BootstrapBrokerStringTls)))
d.Set("cluster_name", cluster.ClusterName)
clusterUUID, _ := clusterUUIDFromARN(clusterARN)
d.Set("cluster_uuid", clusterUUID)
d.Set("kafka_version", cluster.CurrentBrokerSoftwareInfo.KafkaVersion)
d.Set("number_of_broker_nodes", cluster.NumberOfBrokerNodes)

Expand Down
1 change: 1 addition & 0 deletions internal/service/kafka/cluster_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func TestAccKafkaClusterDataSource_basic(t *testing.T) {
resource.TestCheckResourceAttrPair(dataSourceName, "bootstrap_brokers_sasl_scram", resourceName, "bootstrap_brokers_sasl_scram"),
resource.TestCheckResourceAttrPair(dataSourceName, "bootstrap_brokers_tls", resourceName, "bootstrap_brokers_tls"),
resource.TestCheckResourceAttrPair(dataSourceName, "cluster_name", resourceName, "cluster_name"),
resource.TestCheckResourceAttrPair(dataSourceName, "cluster_uuid", resourceName, "cluster_uuid"),
resource.TestCheckResourceAttrPair(dataSourceName, "kafka_version", resourceName, "kafka_version"),
resource.TestCheckResourceAttrPair(dataSourceName, "number_of_broker_nodes", resourceName, "number_of_broker_nodes"),
resource.TestCheckResourceAttrPair(dataSourceName, "tags.%", resourceName, "tags.%"),
Expand Down
1 change: 1 addition & 0 deletions internal/service/kafka/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func TestAccKafkaCluster_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "broker_node_group_info.0.storage_info.0.ebs_storage_info.0.provisioned_throughput.#", "0"),
resource.TestCheckResourceAttr(resourceName, "client_authentication.#", "0"),
resource.TestCheckResourceAttr(resourceName, "cluster_name", rName),
resource.TestCheckResourceAttrSet(resourceName, "cluster_uuid"),
resource.TestCheckResourceAttr(resourceName, "configuration_info.#", "1"),
resource.TestCheckResourceAttr(resourceName, "encryption_info.#", "1"),
acctest.MatchResourceAttrRegionalARN(resourceName, "encryption_info.0.encryption_at_rest_kms_key_arn", "kms", regexache.MustCompile(`key/.+`)),
Expand Down
14 changes: 9 additions & 5 deletions internal/service/kafka/serverless_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func ResourceServerlessCluster() *schema.Resource {
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 64),
},
"cluster_uuid": {
Type: schema.TypeString,
Computed: true,
},
names.AttrTags: tftags.TagsSchema(),
names.AttrTagsAll: tftags.TagsSchemaComputed(),
"vpc_config": {
Expand Down Expand Up @@ -133,7 +137,6 @@ func resourceServerlessClusterCreate(ctx context.Context, d *schema.ResourceData
Tags: getTagsIn(ctx),
}

log.Printf("[DEBUG] Creating MSK Serverless Cluster: %s", input)
output, err := conn.CreateClusterV2WithContext(ctx, input)

if err != nil {
Expand All @@ -142,9 +145,7 @@ func resourceServerlessClusterCreate(ctx context.Context, d *schema.ResourceData

d.SetId(aws.StringValue(output.ClusterArn))

_, err = waitClusterCreated(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate))

if err != nil {
if _, err := waitClusterCreated(ctx, conn, d.Id(), d.Timeout(schema.TimeoutCreate)); err != nil {
return diag.Errorf("waiting for MSK Serverless Cluster (%s) create: %s", d.Id(), err)
}

Expand All @@ -166,7 +167,8 @@ func resourceServerlessClusterRead(ctx context.Context, d *schema.ResourceData,
return diag.Errorf("reading MSK Serverless Cluster (%s): %s", d.Id(), err)
}

d.Set("arn", cluster.ClusterArn)
clusterARN := aws.StringValue(cluster.ClusterArn)
d.Set("arn", clusterARN)
if cluster.Serverless.ClientAuthentication != nil {
if err := d.Set("client_authentication", []interface{}{flattenServerlessClientAuthentication(cluster.Serverless.ClientAuthentication)}); err != nil {
return diag.Errorf("setting client_authentication: %s", err)
Expand All @@ -175,6 +177,8 @@ func resourceServerlessClusterRead(ctx context.Context, d *schema.ResourceData,
d.Set("client_authentication", nil)
}
d.Set("cluster_name", cluster.ClusterName)
clusterUUID, _ := clusterUUIDFromARN(clusterARN)
d.Set("cluster_uuid", clusterUUID)
if err := d.Set("vpc_config", flattenVpcConfigs(cluster.Serverless.VpcConfigs)); err != nil {
return diag.Errorf("setting vpc_config: %s", err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/service/kafka/serverless_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ func TestAccKafkaServerlessCluster_basic(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckServerlessClusterExists(ctx, resourceName, &v),
acctest.MatchResourceAttrRegionalARN(resourceName, "arn", "kafka", regexache.MustCompile(`cluster/.+$`)),
resource.TestCheckResourceAttr(resourceName, "cluster_name", rName),
resource.TestCheckResourceAttr(resourceName, "client_authentication.#", "1"),
resource.TestCheckResourceAttr(resourceName, "client_authentication.0.sasl.#", "1"),
resource.TestCheckResourceAttr(resourceName, "client_authentication.0.sasl.0.iam.#", "1"),
resource.TestCheckResourceAttr(resourceName, "client_authentication.0.sasl.0.iam.0.enabled", "true"),
resource.TestCheckResourceAttr(resourceName, "cluster_name", rName),
resource.TestCheckResourceAttrSet(resourceName, "cluster_uuid"),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
resource.TestCheckResourceAttr(resourceName, "vpc_config.#", "1"),
resource.TestCheckResourceAttr(resourceName, "vpc_config.0.security_group_ids.#", "1"),
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/msk_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ This data source exports the following attributes in addition to the arguments a
* `bootstrap_brokers_sasl_iam` - One or more DNS names (or IP addresses) and SASL IAM port pairs. For example, `b-1.exampleClusterName.abcde.c2.kafka.us-east-1.amazonaws.com:9098,b-2.exampleClusterName.abcde.c2.kafka.us-east-1.amazonaws.com:9098,b-3.exampleClusterName.abcde.c2.kafka.us-east-1.amazonaws.com:9098`. This attribute will have a value if `encryption_info.0.encryption_in_transit.0.client_broker` is set to `TLS_PLAINTEXT` or `TLS` and `client_authentication.0.sasl.0.iam` is set to `true`. The resource sorts the list alphabetically. AWS may not always return all endpoints so the values may not be stable across applies.
* `bootstrap_brokers_sasl_scram` - One or more DNS names (or IP addresses) and SASL SCRAM port pairs. For example, `b-1.exampleClusterName.abcde.c2.kafka.us-east-1.amazonaws.com:9096,b-2.exampleClusterName.abcde.c2.kafka.us-east-1.amazonaws.com:9096,b-3.exampleClusterName.abcde.c2.kafka.us-east-1.amazonaws.com:9096`. This attribute will have a value if `encryption_info.0.encryption_in_transit.0.client_broker` is set to `TLS_PLAINTEXT` or `TLS` and `client_authentication.0.sasl.0.scram` is set to `true`. The resource sorts the list alphabetically. AWS may not always return all endpoints so the values may not be stable across applies.
* `bootstrap_brokers_tls` - One or more DNS names (or IP addresses) and TLS port pairs. For example, `b-1.exampleClusterName.abcde.c2.kafka.us-east-1.amazonaws.com:9094,b-2.exampleClusterName.abcde.c2.kafka.us-east-1.amazonaws.com:9094,b-3.exampleClusterName.abcde.c2.kafka.us-east-1.amazonaws.com:9094`. This attribute will have a value if `encryption_info.0.encryption_in_transit.0.client_broker` is set to `TLS_PLAINTEXT` or `TLS`. The resource sorts the list alphabetically. AWS may not always return all endpoints so the values may not be stable across applies.
* `cluster_uuid` - UUID of the MSK cluster, for use in IAM policies.
* `kafka_version` - Apache Kafka version.
* `number_of_broker_nodes` - Number of broker nodes in the cluster.
* `tags` - Map of key-value pairs assigned to the cluster.
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/msk_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ This resource exports the following attributes in addition to the arguments abov
* `bootstrap_brokers_vpc_connectivity_sasl_iam` - A string containing one or more DNS names (or IP addresses) and SASL IAM port pairs for VPC connectivity. AWS may not always return all endpoints so the values may not be stable across applies.
* `bootstrap_brokers_vpc_connectivity_sasl_scram` - A string containing one or more DNS names (or IP addresses) and SASL SCRAM port pairs for VPC connectivity. AWS may not always return all endpoints so the values may not be stable across applies.
* `bootstrap_brokers_vpc_connectivity_tls` - A string containing one or more DNS names (or IP addresses) and TLS port pairs for VPC connectivity. AWS may not always return all endpoints so the values may not be stable across applies.
* `cluster_uuid` - UUID of the MSK cluster, for use in IAM policies.
* `current_version` - Current version of the MSK Cluster used for updates, e.g., `K13V1IB3VIYZZH`
* `encryption_info.0.encryption_at_rest_kms_key_arn` - The ARN of the KMS key used for encryption at rest of the broker data volumes.
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block).
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/msk_serverless_cluster.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ This resource supports the following arguments:
This resource exports the following attributes in addition to the arguments above:

* `arn` - The ARN of the serverless cluster.
* `cluster_uuid` - UUID of the serverless cluster, for use in IAM policies.
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block).

## Timeouts
Expand Down

0 comments on commit 0ae76fd

Please sign in to comment.