-
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 #20638 from james-bjss/add_msk_kafka_version
Add msk_kafka_version data source
- Loading branch information
Showing
5 changed files
with
225 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,3 @@ | ||
```release-note:new-data-source | ||
aws_msk_kafka_version | ||
``` |
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,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 | ||
} |
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,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"] | ||
} | ||
` | ||
} |
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 @@ | ||
--- | ||
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`. |