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

Add New MSK Broker Node Data Resource #20615

Merged
merged 5 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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/20615.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_msk_nodes
```
103 changes: 103 additions & 0 deletions aws/data_source_aws_msk_nodes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package aws

import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/kafka"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"sort"
)

func dataSourceAwsMskNodes() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsMskNodesRead,

Schema: map[string]*schema.Schema{
"cluster_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
"nodes": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"broker_id": {
Type: schema.TypeFloat,
Computed: true,
},
"attached_eni_id": {
Type: schema.TypeString,
Computed: true,
},
"client_subnet": {
Type: schema.TypeString,
Computed: true,
},
"endpoints": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
},
}
}

func dataSourceAwsMskNodesRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).kafkaconn

listNodesInput := &kafka.ListNodesInput{
ClusterArn: aws.String(d.Get("cluster_arn").(string)),
}

var nodes []*kafka.NodeInfo
for {
listNodesOutput, err := conn.ListNodes(listNodesInput)

if err != nil {
return fmt.Errorf("error listing MSK Nodes: %w", err)
}

if listNodesOutput == nil {
break
}

nodes = append(nodes, listNodesOutput.NodeInfoList...)

if aws.StringValue(listNodesOutput.NextToken) == "" {
break
}

listNodesInput.NextToken = listNodesOutput.NextToken
}

if len(nodes) == 0 {
return fmt.Errorf("error reading MSK Nodes: no results found")
}

// node list is returned unsorted sort on broker id
sort.Slice(nodes, func(i, j int) bool {
iBrokerId := aws.Float64Value(nodes[i].BrokerNodeInfo.BrokerId)
jBrokerId := aws.Float64Value(nodes[j].BrokerNodeInfo.BrokerId)
return iBrokerId < jBrokerId
})

brokerList := make([]interface{}, len(nodes))
for i, node := range nodes {
broker := map[string]interface{}{
"broker_id": aws.Float64Value(node.BrokerNodeInfo.BrokerId),
"attached_eni_id": aws.StringValue(node.BrokerNodeInfo.AttachedENIId),
"client_subnet": aws.StringValue(node.BrokerNodeInfo.ClientSubnet),
"endpoints": aws.StringValueSlice(node.BrokerNodeInfo.Endpoints),
}
brokerList[i] = broker
}

d.SetId(d.Get("cluster_arn").(string))
d.Set("nodes", brokerList)
return nil
}
69 changes: 69 additions & 0 deletions aws/data_source_aws_msk_nodes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package aws

import (
"fmt"
"regexp"
"testing"

"github.com/aws/aws-sdk-go/service/kafka"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccAWSMskNodesDataSource_Name(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
dataSourceName := "data.aws_msk_nodes.test"
resourceName := "aws_msk_cluster.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSMsk(t) },
ErrorCheck: testAccErrorCheck(t, kafka.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckMskClusterDestroy,
Steps: []resource.TestStep{
{
Config: testAccMskNodesDataSourceConfigName(rName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair(dataSourceName, "cluster_arn", resourceName, "arn"),
resource.TestCheckResourceAttrPair(dataSourceName, "nodes.#", resourceName, "number_of_broker_nodes"),
resource.TestCheckResourceAttr(dataSourceName, "nodes.0.broker_id", "1"),
resource.TestMatchResourceAttr(dataSourceName, "nodes.0.attached_eni_id", regexp.MustCompile(`^eni-.+`)),
resource.TestMatchResourceAttr(dataSourceName, "nodes.0.client_subnet", regexp.MustCompile(`^subnet-.+`)),
resource.TestMatchResourceAttr(dataSourceName, "nodes.0.endpoints.0", regexp.MustCompile(`^[\w\-\.]+\.kafka\.[\w\-]+\.amazonaws.com$`)),
resource.TestCheckResourceAttr(dataSourceName, "nodes.1.broker_id", "2"),
resource.TestMatchResourceAttr(dataSourceName, "nodes.1.attached_eni_id", regexp.MustCompile(`^eni-.+`)),
resource.TestMatchResourceAttr(dataSourceName, "nodes.1.client_subnet", regexp.MustCompile(`^subnet-.+`)),
resource.TestMatchResourceAttr(dataSourceName, "nodes.1.endpoints.0", regexp.MustCompile(`^[\w\-\.]+\.kafka\.[\w\-]+\.amazonaws.com$`)),
resource.TestCheckResourceAttr(dataSourceName, "nodes.2.broker_id", "3"),
resource.TestMatchResourceAttr(dataSourceName, "nodes.2.attached_eni_id", regexp.MustCompile(`^eni-.+`)),
resource.TestMatchResourceAttr(dataSourceName, "nodes.2.client_subnet", regexp.MustCompile(`^subnet-.+`)),
resource.TestMatchResourceAttr(dataSourceName, "nodes.2.endpoints.0", regexp.MustCompile(`^[\w\-\.]+\.kafka\.[\w\-]+\.amazonaws.com$`)),
),
},
},
})
}
func testAccMskNodesDataSourceConfigName(rName string) string {
return composeConfig(testAccMskClusterBaseConfig(rName), fmt.Sprintf(`
resource "aws_msk_cluster" "test" {
cluster_name = %[1]q
kafka_version = "2.2.1"
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.t3.small"
security_groups = [aws_security_group.example_sg.id]
}

tags = {
foo = "bar"
}
}

data "aws_msk_nodes" "test" {
cluster_arn = aws_msk_cluster.test.arn
}
`, rName))
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ func Provider() *schema.Provider {
"aws_mq_broker": dataSourceAwsMqBroker(),
"aws_msk_cluster": dataSourceAwsMskCluster(),
"aws_msk_configuration": dataSourceAwsMskConfiguration(),
"aws_msk_nodes": dataSourceAwsMskNodes(),
"aws_nat_gateway": dataSourceAwsNatGateway(),
"aws_neptune_orderable_db_instance": dataSourceAwsNeptuneOrderableDbInstance(),
"aws_neptune_engine_version": dataSourceAwsNeptuneEngineVersion(),
Expand Down
37 changes: 37 additions & 0 deletions website/docs/d/msk_nodes.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
subcategory: "Managed Streaming for Kafka (MSK)"
layout: "aws"
page_title: "AWS: aws_msk_nodes"
description: |-
Get information on an Amazon MSK Broker Nodes
---

# Data Source: aws_msk_nodes

Get information on an Amazon MSK Broker Nodes.

## Example Usage

```terraform
data "aws_msk_nodes" "example" {
cluster_arn = aws_msk_cluster.example.arn
}
```

## Argument Reference

The following arguments are supported:

* `cluster_arn` - (Required) The ARN of the cluster the nodes belong to.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* [`nodes`](#Nodes) - List of MSK Broker Nodes, sorted by broker ID in ascending order.

### Nodes
* `broker_id` - Numeric ID of the broker node
* `attached_eni_id` - The ENI associated with the broker node
* `client_subnet` - The subnet name the broker resides in
* `endpoints` - List of hostnames associates with the broker node. This does not include ports.