Skip to content

Commit

Permalink
Merge pull request #20638 from james-bjss/add_msk_kafka_version
Browse files Browse the repository at this point in the history
Add msk_kafka_version data source
  • Loading branch information
ewbankkit authored Sep 17, 2021
2 parents 11a47e8 + 748d570 commit 1194e7a
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/20638.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_msk_kafka_version
```
103 changes: 103 additions & 0 deletions aws/data_source_aws_msk_kafka_version.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"
)

func dataSourceAwsMskKafkaVersion() *schema.Resource {
return &schema.Resource{
Read: dataSourceAwsMskKafkaVersionRead,

Schema: map[string]*schema.Schema{
"preferred_versions": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ExactlyOneOf: []string{"version", "preferred_versions"},
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ExactlyOneOf: []string{"version", "preferred_versions"},
},
},
}
}

func findMskKafkaVersion(preferredVersions []interface{}, versions []*kafka.KafkaVersion) *kafka.KafkaVersion {
var found *kafka.KafkaVersion

for _, v := range preferredVersions {
preferredVersion, ok := v.(string)

if !ok {
continue
}

for _, kafkaVersion := range versions {
if preferredVersion == aws.StringValue(kafkaVersion.Version) {
found = kafkaVersion

break
}
}

if found != nil {
break
}
}

return found
}

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

var kafkaVersions []*kafka.KafkaVersion

err := conn.ListKafkaVersionsPages(&kafka.ListKafkaVersionsInput{}, func(page *kafka.ListKafkaVersionsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

kafkaVersions = append(kafkaVersions, page.KafkaVersions...)

return !lastPage
})

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

if len(kafkaVersions) == 0 {
return fmt.Errorf("no Kafka versions found")
}

var found *kafka.KafkaVersion

if v, ok := d.GetOk("preferred_versions"); ok {
found = findMskKafkaVersion(v.([]interface{}), kafkaVersions)
} else if v, ok := d.GetOk("version"); ok {
found = findMskKafkaVersion([]interface{}{v}, kafkaVersions)
}

if found == nil {
return fmt.Errorf("no Kafka versions match the criteria")
}

d.SetId(aws.StringValue(found.Version))

d.Set("status", found.Status)
d.Set("version", found.Version)

return nil
}
82 changes: 82 additions & 0 deletions aws/data_source_aws_msk_kafka_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package aws

import (
"fmt"
"testing"

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

func TestAccAWSMskKafkaVersionDataSource_basic(t *testing.T) {
dataSourceName := "data.aws_msk_kafka_version.test"
version := "2.4.1.1"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccAWSMskKafkaVersionPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, kafka.EndpointsID),
Providers: testAccProviders,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccAWSMskKafkaVersionDataSourceBasicConfig(version),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "version", version),
resource.TestCheckResourceAttrSet(dataSourceName, "status"),
),
},
},
})
}

func TestAccAWSMskKafkaVersionDataSource_preferred(t *testing.T) {
dataSourceName := "data.aws_msk_kafka_version.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccAWSMskKafkaVersionPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, kafka.EndpointsID),
Providers: testAccProviders,
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: testAccAWSMskKafkaVersionDataSourcePreferredConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "version", "2.4.1.1"),
resource.TestCheckResourceAttrSet(dataSourceName, "status"),
),
},
},
})
}

func testAccAWSMskKafkaVersionPreCheck(t *testing.T) {
conn := testAccProvider.Meta().(*AWSClient).kafkaconn

input := &kafka.ListKafkaVersionsInput{}

_, err := conn.ListKafkaVersions(input)

if testAccPreCheckSkipError(err) {
t.Skipf("skipping acceptance testing: %s", err)
}

if err != nil {
t.Fatalf("unexpected PreCheck error: %s", err)
}
}

func testAccAWSMskKafkaVersionDataSourceBasicConfig(version string) string {
return fmt.Sprintf(`
data "aws_msk_kafka_version" "test" {
version = %[1]q
}
`, version)
}

func testAccAWSMskKafkaVersionDataSourcePreferredConfig() string {
return `
data "aws_msk_kafka_version" "test" {
preferred_versions = ["2.4.1.1", "2.4.1", "2.2.1"]
}
`
}
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ func Provider() *schema.Provider {
"aws_msk_broker_nodes": dataSourceAwsMskBrokerNodes(),
"aws_msk_cluster": dataSourceAwsMskCluster(),
"aws_msk_configuration": dataSourceAwsMskConfiguration(),
"aws_msk_kafka_version": dataSourceAwsMskKafkaVersion(),
"aws_nat_gateway": dataSourceAwsNatGateway(),
"aws_neptune_orderable_db_instance": dataSourceAwsNeptuneOrderableDbInstance(),
"aws_neptune_engine_version": dataSourceAwsNeptuneEngineVersion(),
Expand Down
36 changes: 36 additions & 0 deletions website/docs/d/msk_kafka_version.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
subcategory: "Managed Streaming for Kafka (MSK)"
layout: "aws"
page_title: "AWS: aws_msk_kafka_version"
description: |-
Get information on a Amazon MSK Kafka Version
---

# Data Source: aws_msk_cluster

Get information on a Amazon MSK Kafka Version

## Example Usage

```terraform
data "aws_msk_kafka_version" "preferred" {
preferred_versions = ["2.4.1.1", "2.4.1", "2.2.1"]
}
data "aws_msk_kafka_version" "example" {
version = "2.8.0"
}
```

## Argument Reference

The following arguments are supported:

* `preferred_versions` - (Optional) Ordered list of preferred Kafka versions. The first match in this list will be returned. Either `preferred_versions` or `version` must be set.
* `version` - (Optional) Version of MSK Kafka. For example 2.4.1.1 or "2.2.1" etc. Either `preferred_versions` or `version` must be set.

## Attribute Reference

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

* `status` - Status of the MSK Kafka version eg. `ACTIVE` or `DEPRECATED`.

0 comments on commit 1194e7a

Please sign in to comment.