-
Notifications
You must be signed in to change notification settings - Fork 9.3k
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 #8740 from terraform-providers/f-aws_msk_configura…
…tion New Resource: aws_msk_configuration
- Loading branch information
Showing
5 changed files
with
407 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
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,137 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/kafka" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsMskConfiguration() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsMskConfigurationCreate, | ||
Read: resourceAwsMskConfigurationRead, | ||
Delete: schema.Noop, | ||
|
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"kafka_versions": { | ||
Type: schema.TypeSet, | ||
Required: true, | ||
ForceNew: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
}, | ||
}, | ||
"latest_revision": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"server_properties": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsMskConfigurationCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).kafkaconn | ||
|
||
input := &kafka.CreateConfigurationInput{ | ||
KafkaVersions: expandStringSet(d.Get("kafka_versions").(*schema.Set)), | ||
Name: aws.String(d.Get("name").(string)), | ||
ServerProperties: []byte(d.Get("server_properties").(string)), | ||
} | ||
|
||
if v, ok := d.GetOk("description"); ok { | ||
input.Description = aws.String(v.(string)) | ||
} | ||
|
||
output, err := conn.CreateConfiguration(input) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error creating MSK Configuration: %s", err) | ||
} | ||
|
||
d.SetId(aws.StringValue(output.Arn)) | ||
|
||
return resourceAwsMskConfigurationRead(d, meta) | ||
} | ||
|
||
func resourceAwsMskConfigurationRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).kafkaconn | ||
|
||
configurationInput := &kafka.DescribeConfigurationInput{ | ||
Arn: aws.String(d.Id()), | ||
} | ||
|
||
configurationOutput, err := conn.DescribeConfiguration(configurationInput) | ||
|
||
if isAWSErr(err, kafka.ErrCodeNotFoundException, "") { | ||
log.Printf("[WARN] MSK Configuration (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error describing MSK Configuration (%s): %s", d.Id(), err) | ||
} | ||
|
||
if configurationOutput == nil { | ||
return fmt.Errorf("error describing MSK Configuration (%s): missing result", d.Id()) | ||
} | ||
|
||
if configurationOutput.LatestRevision == nil { | ||
return fmt.Errorf("error describing MSK Configuration (%s): missing latest revision", d.Id()) | ||
} | ||
|
||
revision := configurationOutput.LatestRevision.Revision | ||
revisionInput := &kafka.DescribeConfigurationRevisionInput{ | ||
Arn: aws.String(d.Id()), | ||
Revision: revision, | ||
} | ||
|
||
revisionOutput, err := conn.DescribeConfigurationRevision(revisionInput) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error describing MSK Configuration (%s) Revision (%d): %s", d.Id(), aws.Int64Value(revision), err) | ||
} | ||
|
||
if revisionOutput == nil { | ||
return fmt.Errorf("error describing MSK Configuration (%s) Revision (%d): missing result", d.Id(), aws.Int64Value(revision)) | ||
} | ||
|
||
d.Set("arn", aws.StringValue(configurationOutput.Arn)) | ||
d.Set("description", aws.StringValue(configurationOutput.Description)) | ||
|
||
if err := d.Set("kafka_versions", aws.StringValueSlice(configurationOutput.KafkaVersions)); err != nil { | ||
return fmt.Errorf("error setting kafka_versions: %s", err) | ||
} | ||
|
||
d.Set("latest_revision", aws.Int64Value(revision)) | ||
d.Set("name", aws.StringValue(configurationOutput.Name)) | ||
d.Set("server_properties", string(revisionOutput.ServerProperties)) | ||
|
||
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,213 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/kafka" | ||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAWSMskConfiguration_basic(t *testing.T) { | ||
var configuration1 kafka.DescribeConfigurationOutput | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
resourceName := "aws_msk_configuration.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckMskConfigurationDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccMskConfigurationConfig(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckMskConfigurationExists(resourceName, &configuration1), | ||
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "kafka", regexp.MustCompile(`configuration/.+`)), | ||
resource.TestCheckResourceAttr(resourceName, "description", ""), | ||
resource.TestCheckResourceAttr(resourceName, "kafka_versions.#", "1"), | ||
resource.TestCheckResourceAttr(resourceName, "latest_revision", "1"), | ||
resource.TestCheckResourceAttr(resourceName, "name", rName), | ||
resource.TestMatchResourceAttr(resourceName, "server_properties", regexp.MustCompile(`auto.create.topics.enable = true`)), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSMskConfiguration_Description(t *testing.T) { | ||
var configuration1 kafka.DescribeConfigurationOutput | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
resourceName := "aws_msk_configuration.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckMskConfigurationDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccMskConfigurationConfigDescription(rName, "description1"), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckMskConfigurationExists(resourceName, &configuration1), | ||
resource.TestCheckResourceAttr(resourceName, "description", "description1"), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSMskConfiguration_KafkaVersions(t *testing.T) { | ||
var configuration1 kafka.DescribeConfigurationOutput | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
resourceName := "aws_msk_configuration.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckMskConfigurationDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccMskConfigurationConfigKafkaVersions(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckMskConfigurationExists(resourceName, &configuration1), | ||
resource.TestCheckResourceAttr(resourceName, "kafka_versions.#", "2"), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSMskConfiguration_ServerProperties(t *testing.T) { | ||
var configuration1 kafka.DescribeConfigurationOutput | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
resourceName := "aws_msk_configuration.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckMskConfigurationDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccMskConfigurationConfigServerProperties(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckMskConfigurationExists(resourceName, &configuration1), | ||
resource.TestMatchResourceAttr(resourceName, "server_properties", regexp.MustCompile(`auto.create.topics.enable = false`)), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckMskConfigurationDestroy(s *terraform.State) error { | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_msk_configuration" { | ||
continue | ||
} | ||
|
||
// The API does not support deletions at this time | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckMskConfigurationExists(resourceName string, configuration *kafka.DescribeConfigurationOutput) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", resourceName) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("Resource ID not set: %s", resourceName) | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).kafkaconn | ||
|
||
input := &kafka.DescribeConfigurationInput{ | ||
Arn: aws.String(rs.Primary.ID), | ||
} | ||
|
||
output, err := conn.DescribeConfiguration(input) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error describing MSK Cluster (%s): %s", rs.Primary.ID, err) | ||
} | ||
|
||
*configuration = *output | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccMskConfigurationConfig(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_msk_configuration" "test" { | ||
kafka_versions = ["2.1.0"] | ||
name = %[1]q | ||
server_properties = <<PROPERTIES | ||
auto.create.topics.enable = true | ||
delete.topic.enable = true | ||
PROPERTIES | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccMskConfigurationConfigDescription(rName, description string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_msk_configuration" "test" { | ||
description = %[2]q | ||
kafka_versions = ["2.1.0"] | ||
name = %[1]q | ||
server_properties = <<PROPERTIES | ||
auto.create.topics.enable = true | ||
PROPERTIES | ||
} | ||
`, rName, description) | ||
} | ||
|
||
func testAccMskConfigurationConfigKafkaVersions(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_msk_configuration" "test" { | ||
kafka_versions = ["1.1.1", "2.1.0"] | ||
name = %[1]q | ||
server_properties = <<PROPERTIES | ||
auto.create.topics.enable = true | ||
PROPERTIES | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccMskConfigurationConfigServerProperties(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_msk_configuration" "test" { | ||
kafka_versions = ["2.1.0"] | ||
name = %[1]q | ||
server_properties = <<PROPERTIES | ||
auto.create.topics.enable = false | ||
PROPERTIES | ||
} | ||
`, 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
Oops, something went wrong.