Skip to content

Commit

Permalink
Merge pull request #39215 from acwwat/f-aws_amplify_app-add_cache_con…
Browse files Browse the repository at this point in the history
…fig_arg

feat: Add cache_config arg for aws_amplify_app
  • Loading branch information
jar-b authored Sep 11, 2024
2 parents 00d6762 + 4a0817f commit 8d73939
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .changelog/39215.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_amplify_app: Add `cache_config` argument
```
1 change: 1 addition & 0 deletions internal/service/amplify/amplify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func TestAccAmplify_serial(t *testing.T) {
"AutoBranchCreationConfig": testAccApp_AutoBranchCreationConfig,
"BasicAuthCredentials": testAccApp_BasicAuthCredentials,
"BuildSpec": testAccApp_BuildSpec,
"CacheConfig": testAccApp_CacheConfig,
"CustomRules": testAccApp_CustomRules,
"Description": testAccApp_Description,
"EnvironmentVariables": testAccApp_EnvironmentVariables,
Expand Down
55 changes: 55 additions & 0 deletions internal/service/amplify/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ func resourceApp() *schema.Resource {
Computed: true,
ValidateFunc: validation.StringLenBetween(1, 25000),
},
"cache_config": {
Type: schema.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
names.AttrType: {
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: enum.Validate[types.CacheConfigType](),
},
},
},
},
"custom_headers": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -328,6 +343,10 @@ func resourceAppCreate(ctx context.Context, d *schema.ResourceData, meta interfa
input.BuildSpec = aws.String(v.(string))
}

if v, ok := d.GetOk("cache_config"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.CacheConfig = expandCacheConfig(v.([]interface{})[0].(map[string]interface{}))
}

if v, ok := d.GetOk("custom_headers"); ok {
input.CustomHeaders = aws.String(v.(string))
}
Expand Down Expand Up @@ -414,6 +433,11 @@ func resourceAppRead(ctx context.Context, d *schema.ResourceData, meta interface
d.Set("auto_branch_creation_patterns", aws.StringSlice(app.AutoBranchCreationPatterns))
d.Set("basic_auth_credentials", app.BasicAuthCredentials)
d.Set("build_spec", app.BuildSpec)
if app.CacheConfig != nil {
if err := d.Set("cache_config", []interface{}{flattenCacheConfig(app.CacheConfig)}); err != nil {
return sdkdiag.AppendErrorf(diags, "setting cache_config: %s", err)
}
}
d.Set("custom_headers", app.CustomHeaders)
if err := d.Set("custom_rule", flattenCustomRules(app.CustomRules)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting custom_rule: %s", err)
Expand Down Expand Up @@ -479,6 +503,12 @@ func resourceAppUpdate(ctx context.Context, d *schema.ResourceData, meta interfa
input.BuildSpec = aws.String(d.Get("build_spec").(string))
}

if d.HasChange("cache_config") {
if v, ok := d.GetOk("cache_config"); ok && len(v.([]interface{})) > 0 && v.([]interface{})[0] != nil {
input.CacheConfig = expandCacheConfig(v.([]interface{})[0].(map[string]interface{}))
}
}

if d.HasChange("custom_headers") {
input.CustomHeaders = aws.String(d.Get("custom_headers").(string))
}
Expand Down Expand Up @@ -692,6 +722,31 @@ func flattenAutoBranchCreationConfig(apiObject *types.AutoBranchCreationConfig)
return tfMap
}

func expandCacheConfig(tfMap map[string]interface{}) *types.CacheConfig {
if tfMap == nil {
return nil
}

apiObject := &types.CacheConfig{}

if v, ok := tfMap[names.AttrType].(string); ok {
apiObject.Type = types.CacheConfigType(v)
}

return apiObject
}

func flattenCacheConfig(apiObject *types.CacheConfig) map[string]interface{} {
if apiObject == nil {
return nil
}

tfMap := map[string]interface{}{}
tfMap[names.AttrType] = string(apiObject.Type)

return tfMap
}

func expandCustomRule(tfMap map[string]interface{}) *types.CustomRule {
if tfMap == nil {
return nil
Expand Down
51 changes: 51 additions & 0 deletions internal/service/amplify/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func testAccApp_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "auto_branch_creation_patterns.#", acctest.Ct0),
resource.TestCheckResourceAttr(resourceName, "basic_auth_credentials", ""),
resource.TestCheckResourceAttr(resourceName, "build_spec", ""),
resource.TestCheckResourceAttr(resourceName, "cache_config.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "cache_config.0.type", "AMPLIFY_MANAGED"),
resource.TestCheckResourceAttr(resourceName, "custom_headers", ""),
resource.TestCheckResourceAttr(resourceName, "custom_rule.#", acctest.Ct0),
resource.TestMatchResourceAttr(resourceName, "default_domain", regexache.MustCompile(`\.amplifyapp\.com$`)),
Expand Down Expand Up @@ -284,6 +286,43 @@ func testAccApp_BuildSpec(t *testing.T) {
})
}

func testAccApp_CacheConfig(t *testing.T) {
ctx := acctest.Context(t)
var app types.App
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_amplify_app.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); testAccPreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, names.AmplifyServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckAppDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccAppConfig_cacheConfig(rName, "AMPLIFY_MANAGED"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppExists(ctx, resourceName, &app),
resource.TestCheckResourceAttr(resourceName, "cache_config.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "cache_config.0.type", "AMPLIFY_MANAGED"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAppConfig_cacheConfig(rName, "AMPLIFY_MANAGED_NO_COOKIES"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppExists(ctx, resourceName, &app),
resource.TestCheckResourceAttr(resourceName, "cache_config.#", acctest.Ct1),
resource.TestCheckResourceAttr(resourceName, "cache_config.0.type", "AMPLIFY_MANAGED_NO_COOKIES"),
),
},
},
})
}

func testAccApp_CustomRules(t *testing.T) {
ctx := acctest.Context(t)
var app types.App
Expand Down Expand Up @@ -727,6 +766,18 @@ resource "aws_amplify_app" "test" {
`, rName, buildSpec)
}

func testAccAppConfig_cacheConfig(rName, cacheConfigType string) string {
return fmt.Sprintf(`
resource "aws_amplify_app" "test" {
name = %[1]q
cache_config {
type = %[2]q
}
}
`, rName, cacheConfigType)
}

func testAccAppConfig_customRules(rName string) string {
return fmt.Sprintf(`
resource "aws_amplify_app" "test" {
Expand Down
19 changes: 15 additions & 4 deletions website/docs/r/amplify_app.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,13 @@ This resource supports the following arguments:

* `name` - (Required) Name for an Amplify app.
* `access_token` - (Optional) Personal access token for a third-party source control system for an Amplify app. This token must have write access to the relevant repo to create a webhook and a read-only deploy key for the Amplify project. The token is not stored, so after applying this attribute can be removed and the setup token deleted.
* `auto_branch_creation_config` - (Optional) Automated branch creation configuration for an Amplify app. An `auto_branch_creation_config` block is documented below.
* `auto_branch_creation_config` - (Optional) Automated branch creation configuration for an Amplify app. See [`auto_branch_creation_config` Block](#auto_branch_creation_config-block) for details.
* `auto_branch_creation_patterns` - (Optional) Automated branch creation glob patterns for an Amplify app.
* `basic_auth_credentials` - (Optional) Credentials for basic authorization for an Amplify app.
* `build_spec` - (Optional) The [build specification](https://docs.aws.amazon.com/amplify/latest/userguide/build-settings.html) (build spec) for an Amplify app.
* `cache_config` - (Optional) Cache configuration for the Amplify app. See [`cache_config` Block](#cache_config-block) for details.
* `custom_headers` - (Optional) The [custom HTTP headers](https://docs.aws.amazon.com/amplify/latest/userguide/custom-headers.html) for an Amplify app.
* `custom_rule` - (Optional) Custom rewrite and redirect rules for an Amplify app. A `custom_rule` block is documented below.
* `custom_rule` - (Optional) Custom rewrite and redirect rules for an Amplify app. See [`custom_rule` Block](#custom_rule-block) for details.
* `description` - (Optional) Description for an Amplify app.
* `enable_auto_branch_creation` - (Optional) Enables automated branch creation for an Amplify app.
* `enable_basic_auth` - (Optional) Enables basic authorization for an Amplify app. This will apply to all branches that are part of this app.
Expand All @@ -184,7 +185,9 @@ This resource supports the following arguments:
* `repository` - (Optional) Repository for an Amplify app.
* `tags` - (Optional) Key-value mapping of resource tags. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

An `auto_branch_creation_config` block supports the following arguments:
### `auto_branch_creation_config` Block

The `auto_branch_creation_config` configuration block supports the following arguments:

* `basic_auth_credentials` - (Optional) Basic authorization credentials for the autocreated branch.
* `build_spec` - (Optional) Build specification (build spec) for the autocreated branch.
Expand All @@ -197,7 +200,15 @@ An `auto_branch_creation_config` block supports the following arguments:
* `pull_request_environment_name` - (Optional) Amplify environment name for the pull request.
* `stage` - (Optional) Describes the current stage for the autocreated branch. Valid values: `PRODUCTION`, `BETA`, `DEVELOPMENT`, `EXPERIMENTAL`, `PULL_REQUEST`.

A `custom_rule` block supports the following arguments:
### `cache_config` Block

The `cache_config` configuration block supports the following arguments:

- `type` - (Required) Type of cache configuration to use for an Amplify app. Valid values: `AMPLIFY_MANAGED`, `AMPLIFY_MANAGED_NO_COOKIES`.

### `custom_rule` Block

The `custom_rule` configuration block supports the following arguments:

* `condition` - (Optional) Condition for a URL rewrite or redirect rule, such as a country code.
* `source` - (Required) Source pattern for a URL rewrite or redirect rule.
Expand Down

0 comments on commit 8d73939

Please sign in to comment.