-
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 #19324 from suzuki-shunsuke/feat/appconfig_hosted_…
…configuration_version feat: support AppConfig Hosted Configuration Version
- Loading branch information
Showing
8 changed files
with
410 additions
and
2 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-resource | ||
aws_appconfig_hosted_configuration_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
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
190 changes: 190 additions & 0 deletions
190
aws/resource_aws_appconfig_hosted_configuration_version.go
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,190 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"regexp" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/arn" | ||
"github.com/aws/aws-sdk-go/service/appconfig" | ||
"github.com/hashicorp/aws-sdk-go-base/tfawserr" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func resourceAwsAppconfigHostedConfigurationVersion() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsAppconfigHostedConfigurationVersionCreate, | ||
Read: resourceAwsAppconfigHostedConfigurationVersionRead, | ||
Delete: resourceAwsAppconfigHostedConfigurationVersionDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"application_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringMatch(regexp.MustCompile(`[a-z0-9]{4,7}`), ""), | ||
}, | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"configuration_profile_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringMatch(regexp.MustCompile(`[a-z0-9]{4,7}`), ""), | ||
}, | ||
"content": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Sensitive: true, | ||
}, | ||
"content_type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringLenBetween(1, 255), | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ValidateFunc: validation.StringLenBetween(0, 1024), | ||
}, | ||
"version_number": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsAppconfigHostedConfigurationVersionCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).appconfigconn | ||
|
||
appID := d.Get("application_id").(string) | ||
profileID := d.Get("configuration_profile_id").(string) | ||
|
||
input := &appconfig.CreateHostedConfigurationVersionInput{ | ||
ApplicationId: aws.String(appID), | ||
ConfigurationProfileId: aws.String(profileID), | ||
Content: []byte(d.Get("content").(string)), | ||
ContentType: aws.String(d.Get("content_type").(string)), | ||
} | ||
|
||
if v, ok := d.GetOk("description"); ok { | ||
input.Description = aws.String(v.(string)) | ||
} | ||
|
||
output, err := conn.CreateHostedConfigurationVersion(input) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error creating AppConfig HostedConfigurationVersion for Application (%s): %w", appID, err) | ||
} | ||
|
||
d.SetId(fmt.Sprintf("%s/%s/%d", aws.StringValue(output.ApplicationId), aws.StringValue(output.ConfigurationProfileId), aws.Int64Value(output.VersionNumber))) | ||
|
||
return resourceAwsAppconfigHostedConfigurationVersionRead(d, meta) | ||
} | ||
|
||
func resourceAwsAppconfigHostedConfigurationVersionRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).appconfigconn | ||
|
||
appID, confProfID, versionNumber, err := resourceAwsAppconfigHostedConfigurationVersionParseID(d.Id()) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
input := &appconfig.GetHostedConfigurationVersionInput{ | ||
ApplicationId: aws.String(appID), | ||
ConfigurationProfileId: aws.String(confProfID), | ||
VersionNumber: aws.Int64(int64(versionNumber)), | ||
} | ||
|
||
output, err := conn.GetHostedConfigurationVersion(input) | ||
|
||
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, appconfig.ErrCodeResourceNotFoundException) { | ||
log.Printf("[WARN] Appconfig Hosted Configuration Version (%s) not found, removing from state", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error getting AppConfig Hosted Configuration Version (%s): %w", d.Id(), err) | ||
} | ||
|
||
if output == nil { | ||
return fmt.Errorf("error getting AppConfig Hosted Configuration Version (%s): empty response", d.Id()) | ||
} | ||
|
||
d.Set("application_id", output.ApplicationId) | ||
d.Set("configuration_profile_id", output.ConfigurationProfileId) | ||
d.Set("content", string(output.Content)) | ||
d.Set("content_type", output.ContentType) | ||
d.Set("description", output.Description) | ||
d.Set("version_number", output.VersionNumber) | ||
|
||
arn := arn.ARN{ | ||
AccountID: meta.(*AWSClient).accountid, | ||
Partition: meta.(*AWSClient).partition, | ||
Region: meta.(*AWSClient).region, | ||
Resource: fmt.Sprintf("application/%s/configurationprofile/%s/hostedconfigurationversion/%d", appID, confProfID, versionNumber), | ||
Service: "appconfig", | ||
}.String() | ||
|
||
d.Set("arn", arn) | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsAppconfigHostedConfigurationVersionDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).appconfigconn | ||
|
||
appID, confProfID, versionNumber, err := resourceAwsAppconfigHostedConfigurationVersionParseID(d.Id()) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
input := &appconfig.DeleteHostedConfigurationVersionInput{ | ||
ApplicationId: aws.String(appID), | ||
ConfigurationProfileId: aws.String(confProfID), | ||
VersionNumber: aws.Int64(int64(versionNumber)), | ||
} | ||
|
||
_, err = conn.DeleteHostedConfigurationVersion(input) | ||
|
||
if tfawserr.ErrCodeEquals(err, appconfig.ErrCodeResourceNotFoundException) { | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error deleting Appconfig Hosted Configuration Version (%s): %w", d.Id(), err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceAwsAppconfigHostedConfigurationVersionParseID(id string) (string, string, int, error) { | ||
parts := strings.Split(id, "/") | ||
|
||
if len(parts) != 3 || parts[0] == "" || parts[1] == "" || parts[2] == "" { | ||
return "", "", 0, fmt.Errorf("unexpected format of ID (%q), expected ApplicationID/ConfigurationProfileID/VersionNumber", id) | ||
} | ||
|
||
version, err := strconv.Atoi(parts[2]) | ||
if err != nil { | ||
return "", "", 0, fmt.Errorf("error parsing Hosted Configuration Version version_number: %w", err) | ||
} | ||
|
||
return parts[0], parts[1], version, nil | ||
} |
161 changes: 161 additions & 0 deletions
161
aws/resource_aws_appconfig_hosted_configuration_version_test.go
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,161 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/appconfig" | ||
"github.com/hashicorp/aws-sdk-go-base/tfawserr" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestAccAWSAppConfigHostedConfigurationVersion_basic(t *testing.T) { | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
resourceName := "aws_appconfig_hosted_configuration_version.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ErrorCheck: testAccErrorCheck(t, appconfig.EndpointsID), | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAppConfigHostedConfigurationVersionDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSAppConfigHostedConfigurationVersion(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAppConfigHostedConfigurationVersionExists(resourceName), | ||
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "appconfig", regexp.MustCompile(`application/[a-z0-9]{4,7}/configurationprofile/[a-z0-9]{4,7}/hostedconfigurationversion/[0-9]+`)), | ||
resource.TestCheckResourceAttrPair(resourceName, "application_id", "aws_appconfig_application.test", "id"), | ||
resource.TestCheckResourceAttrPair(resourceName, "configuration_profile_id", "aws_appconfig_configuration_profile.test", "configuration_profile_id"), | ||
resource.TestCheckResourceAttr(resourceName, "content", "{\"foo\":\"bar\"}"), | ||
resource.TestCheckResourceAttr(resourceName, "content_type", "application/json"), | ||
resource.TestCheckResourceAttr(resourceName, "description", rName), | ||
resource.TestCheckResourceAttr(resourceName, "version_number", "1"), | ||
), | ||
}, | ||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAWSAppConfigHostedConfigurationVersion_disappears(t *testing.T) { | ||
rName := acctest.RandomWithPrefix("tf-acc-test") | ||
resourceName := "aws_appconfig_hosted_configuration_version.test" | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
ErrorCheck: testAccErrorCheck(t, appconfig.EndpointsID), | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAppConfigHostedConfigurationVersionDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAWSAppConfigHostedConfigurationVersion(rName), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAWSAppConfigHostedConfigurationVersionExists(resourceName), | ||
testAccCheckResourceDisappears(testAccProvider, resourceAwsAppconfigHostedConfigurationVersion(), resourceName), | ||
), | ||
ExpectNonEmptyPlan: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAppConfigHostedConfigurationVersionDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).appconfigconn | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_appconfig_hosted_configuration_version" { | ||
continue | ||
} | ||
|
||
appID, confProfID, versionNumber, err := resourceAwsAppconfigHostedConfigurationVersionParseID(rs.Primary.ID) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
input := &appconfig.GetHostedConfigurationVersionInput{ | ||
ApplicationId: aws.String(appID), | ||
ConfigurationProfileId: aws.String(confProfID), | ||
VersionNumber: aws.Int64(int64(versionNumber)), | ||
} | ||
|
||
output, err := conn.GetHostedConfigurationVersion(input) | ||
|
||
if tfawserr.ErrCodeEquals(err, appconfig.ErrCodeResourceNotFoundException) { | ||
continue | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("error reading AppConfig Hosted Configuration Version (%s): %w", rs.Primary.ID, err) | ||
} | ||
|
||
if output != nil { | ||
return fmt.Errorf("AppConfig Hosted Configuration Version (%s) still exists", rs.Primary.ID) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testAccCheckAWSAppConfigHostedConfigurationVersionExists(resourceName string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[resourceName] | ||
if !ok { | ||
return fmt.Errorf("Resource not found: %s", resourceName) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("Resource (%s) ID not set", resourceName) | ||
} | ||
|
||
appID, confProfID, versionNumber, err := resourceAwsAppconfigHostedConfigurationVersionParseID(rs.Primary.ID) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).appconfigconn | ||
|
||
output, err := conn.GetHostedConfigurationVersion(&appconfig.GetHostedConfigurationVersionInput{ | ||
ApplicationId: aws.String(appID), | ||
ConfigurationProfileId: aws.String(confProfID), | ||
VersionNumber: aws.Int64(int64(versionNumber)), | ||
}) | ||
|
||
if err != nil { | ||
return fmt.Errorf("error reading AppConfig Hosted Configuration Version (%s): %w", rs.Primary.ID, err) | ||
} | ||
|
||
if output == nil { | ||
return fmt.Errorf("AppConfig Hosted Configuration Version (%s) not found", rs.Primary.ID) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccAWSAppConfigHostedConfigurationVersion(rName string) string { | ||
return composeConfig( | ||
testAccAWSAppConfigConfigurationProfileConfigName(rName), | ||
fmt.Sprintf(` | ||
resource "aws_appconfig_hosted_configuration_version" "test" { | ||
application_id = aws_appconfig_application.test.id | ||
configuration_profile_id = aws_appconfig_configuration_profile.test.configuration_profile_id | ||
content_type = "application/json" | ||
content = jsonencode({ | ||
foo = "bar" | ||
}) | ||
description = %q | ||
} | ||
`, 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.