-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8743 from terraform-providers/f-aws_msk_cluster-d…
…atasource New Data Source: aws_msk_cluster
- Loading branch information
Showing
5 changed files
with
220 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/kafka" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
) | ||
|
||
func dataSourceAwsMskCluster() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsMskClusterRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"bootstrap_brokers": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"cluster_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringLenBetween(1, 64), | ||
}, | ||
"kafka_version": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"number_of_broker_nodes": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"tags": tagsSchemaComputed(), | ||
"zookeeper_connect_string": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsMskClusterRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).kafkaconn | ||
|
||
listClustersInput := &kafka.ListClustersInput{ | ||
ClusterNameFilter: aws.String(d.Get("cluster_name").(string)), | ||
} | ||
|
||
var clusters []*kafka.ClusterInfo | ||
for { | ||
listClustersOutput, err := conn.ListClusters(listClustersInput) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error listing MSK Clusters: %s", err) | ||
} | ||
|
||
if listClustersOutput == nil { | ||
break | ||
} | ||
|
||
clusters = append(clusters, listClustersOutput.ClusterInfoList...) | ||
|
||
if aws.StringValue(listClustersOutput.NextToken) == "" { | ||
break | ||
} | ||
|
||
listClustersInput.NextToken = listClustersOutput.NextToken | ||
} | ||
|
||
if len(clusters) == 0 { | ||
return fmt.Errorf("error reading MSK Cluster: no results found") | ||
} | ||
|
||
if len(clusters) > 1 { | ||
return fmt.Errorf("error reading MSK Cluster: multiple results found, try adjusting search criteria") | ||
} | ||
|
||
cluster := clusters[0] | ||
|
||
bootstrapBrokersInput := &kafka.GetBootstrapBrokersInput{ | ||
ClusterArn: cluster.ClusterArn, | ||
} | ||
|
||
bootstrapBrokersoOutput, err := conn.GetBootstrapBrokers(bootstrapBrokersInput) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error reading MSK Cluster (%s) bootstrap brokers: %s", aws.StringValue(cluster.ClusterArn), err) | ||
} | ||
|
||
listTagsInput := &kafka.ListTagsForResourceInput{ | ||
ResourceArn: cluster.ClusterArn, | ||
} | ||
|
||
listTagsOutput, err := conn.ListTagsForResource(listTagsInput) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error reading MSK Cluster (%s) tags: %s", aws.StringValue(cluster.ClusterArn), err) | ||
} | ||
|
||
d.Set("arn", aws.StringValue(cluster.ClusterArn)) | ||
d.Set("bootstrap_brokers", aws.StringValue(bootstrapBrokersoOutput.BootstrapBrokerString)) | ||
d.Set("cluster_name", aws.StringValue(cluster.ClusterName)) | ||
d.Set("kafka_version", aws.StringValue(cluster.CurrentBrokerSoftwareInfo.KafkaVersion)) | ||
d.Set("number_of_broker_nodes", aws.Int64Value(cluster.NumberOfBrokerNodes)) | ||
|
||
if err := d.Set("tags", tagsToMapMskCluster(listTagsOutput.Tags)); err != nil { | ||
return fmt.Errorf("error setting tags: %s", err) | ||
} | ||
|
||
d.Set("zookeeper_connect_string", aws.StringValue(cluster.ZookeeperConnectString)) | ||
|
||
d.SetId(aws.StringValue(cluster.ClusterArn)) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAWSMskClusterDataSource_Name(t *testing.T) { | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
dataSourceName := "data.aws_msk_cluster.test" | ||
resourceName := "aws_msk_cluster.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckMskClusterDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccMskClusterDataSourceConfigName(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrPair(resourceName, "arn", dataSourceName, "arn"), | ||
resource.TestCheckResourceAttrSet(dataSourceName, "bootstrap_brokers"), | ||
resource.TestCheckResourceAttrPair(resourceName, "cluster_name", dataSourceName, "cluster_name"), | ||
resource.TestCheckResourceAttrPair(resourceName, "kafka_version", dataSourceName, "kafka_version"), | ||
resource.TestCheckResourceAttrPair(resourceName, "number_of_broker_nodes", dataSourceName, "number_of_broker_nodes"), | ||
resource.TestCheckResourceAttrPair(resourceName, "tags.%", dataSourceName, "tags.%"), | ||
resource.TestCheckResourceAttrPair(resourceName, "zookeeper_connect_string", dataSourceName, "zookeeper_connect_string"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccMskClusterDataSourceConfigName(rName string) string { | ||
return testAccMskClusterBaseConfig() + fmt.Sprintf(` | ||
resource "aws_msk_cluster" "test" { | ||
cluster_name = %[1]q | ||
kafka_version = "2.1.0" | ||
number_of_broker_nodes = 3 | ||
broker_node_group_info { | ||
client_subnets = ["${aws_subnet.example_subnet_az1.id}", "${aws_subnet.example_subnet_az2.id}", "${aws_subnet.example_subnet_az3.id}"] | ||
ebs_volume_size = 10 | ||
instance_type = "kafka.m5.large" | ||
security_groups = ["${aws_security_group.example_sg.id}"] | ||
} | ||
tags = { | ||
foo = "bar" | ||
} | ||
} | ||
data "aws_msk_cluster" "test" { | ||
cluster_name = "${aws_msk_cluster.test.cluster_name}" | ||
} | ||
`, rName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
layout: "aws" | ||
page_title: "AWS: aws_msk_cluster" | ||
sidebar_current: "docs-aws-datasource-msk-cluster" | ||
description: |- | ||
Get information on an Amazon MSK Cluster | ||
--- | ||
|
||
# Data Source: aws_msk_cluster | ||
|
||
Get information on an Amazon MSK Cluster. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "aws_msk_cluster" "example" { | ||
cluster_name = "example" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `cluster_name` - (Required) Name of the cluster. | ||
|
||
## Attribute Reference | ||
|
||
In addition to all arguments above, the following attributes are exported: | ||
|
||
* `arn` - Amazon Resource Name (ARN) of the MSK cluster. | ||
* `bootstrap_brokers` - A comma separated list of one or more hostname:port pairs of Kafka brokers suitable to boostrap connectivity to the Kafka cluster. | ||
* `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. | ||
* `zookeeper_connect_string` - A comma separated list of one or more IP:port pairs to use to connect to the Apache Zookeeper cluster. |