Skip to content

Commit

Permalink
Merge pull request #28436 from DrFaust92/appsync-resolver-js
Browse files Browse the repository at this point in the history
r/appsync_resolver - add support for js
  • Loading branch information
ewbankkit authored Dec 21, 2022
2 parents 83671d7 + 646eb2c commit 12d1458
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 46 deletions.
7 changes: 7 additions & 0 deletions .changelog/28436.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_appsync_resolver: Add `runtime` and `code` arguments
```

```release-note:enhancement
resource/aws_appsync_resolver: Add plan time validation for `caching_config.ttl`
```
1 change: 1 addition & 0 deletions internal/service/appsync/appsync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func TestAccAppSync_serial(t *testing.T) {
},
"Resolver": {
"basic": testAccResolver_basic,
"code": testAccResolver_code,
"disappears": testAccResolver_disappears,
"dataSource": testAccResolver_dataSource,
"DataSource_lambda": testAccResolver_DataSource_lambda,
Expand Down
134 changes: 97 additions & 37 deletions internal/service/appsync/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,40 +34,58 @@ func ResourceResolver() *schema.Resource {
Required: true,
ForceNew: true,
},
"type": {
"arn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Computed: true,
},
"field": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
"caching_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"caching_keys": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"ttl": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(1, 3600),
},
},
},
},
"code": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"runtime"},
ValidateFunc: validation.StringLenBetween(1, 32768),
},
"data_source": {
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"pipeline_config"},
},
"max_batch_size": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(0, 2000),
},
"request_template": {
Type: schema.TypeString,
Optional: true,
},
"response_template": {
"field": {
Type: schema.TypeString,
Optional: true,
Required: true,
ForceNew: true,
},
"kind": {
Type: schema.TypeString,
Optional: true,
Default: appsync.ResolverKindUnit,
ValidateFunc: validation.StringInSlice(appsync.ResolverKind_Values(), true),
},
"max_batch_size": {
Type: schema.TypeInt,
Optional: true,
ValidateFunc: validation.IntBetween(0, 2000),
},
"pipeline_config": {
Type: schema.TypeList,
Optional: true,
Expand All @@ -85,22 +103,29 @@ func ResourceResolver() *schema.Resource {
},
},
},
"caching_config": {
Type: schema.TypeList,
"request_template": {
Type: schema.TypeString,
Optional: true,
MaxItems: 1,
},
"response_template": {
Type: schema.TypeString,
Optional: true,
},
"runtime": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
RequiredWith: []string{"code"},
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"caching_keys": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(appsync.RuntimeName_Values(), false),
},
"ttl": {
Type: schema.TypeInt,
Optional: true,
"runtime_version": {
Type: schema.TypeString,
Required: true,
},
},
},
Expand Down Expand Up @@ -138,9 +163,10 @@ func ResourceResolver() *schema.Resource {
},
},
},
"arn": {
"type": {
Type: schema.TypeString,
Computed: true,
Required: true,
ForceNew: true,
},
},
}
Expand All @@ -156,6 +182,10 @@ func resourceResolverCreate(d *schema.ResourceData, meta interface{}) error {
Kind: aws.String(d.Get("kind").(string)),
}

if v, ok := d.GetOk("code"); ok {
input.Code = aws.String(v.(string))
}

if v, ok := d.GetOkExists("max_batch_size"); ok {
input.MaxBatchSize = aws.Int64(int64(v.(int)))
}
Expand All @@ -168,11 +198,8 @@ func resourceResolverCreate(d *schema.ResourceData, meta interface{}) error {
input.DataSourceName = aws.String(v.(string))
}

if v, ok := d.GetOk("pipeline_config"); ok {
config := v.([]interface{})[0].(map[string]interface{})
input.PipelineConfig = &appsync.PipelineConfig{
Functions: flex.ExpandStringList(config["functions"].([]interface{})),
}
if v, ok := d.GetOk("pipeline_config"); ok && len(v.([]interface{})) > 0 {
input.PipelineConfig = expandPipelineConfig(v.([]interface{}))
}

if v, ok := d.GetOk("request_template"); ok {
Expand All @@ -187,6 +214,10 @@ func resourceResolverCreate(d *schema.ResourceData, meta interface{}) error {
input.CachingConfig = expandResolverCachingConfig(v.([]interface{}))
}

if v, ok := d.GetOk("runtime"); ok && len(v.([]interface{})) > 0 {
input.Runtime = expandRuntime(v.([]interface{}))
}

mutexKey := fmt.Sprintf("appsync-schema-%s", d.Get("api_id").(string))
conns.GlobalMutexKV.Lock(mutexKey)
defer conns.GlobalMutexKV.Unlock(mutexKey)
Expand Down Expand Up @@ -241,6 +272,7 @@ func resourceResolverRead(d *schema.ResourceData, meta interface{}) error {
d.Set("response_template", resolver.ResponseMappingTemplate)
d.Set("kind", resolver.Kind)
d.Set("max_batch_size", resolver.MaxBatchSize)
d.Set("code", resolver.Code)

if err := d.Set("sync_config", flattenSyncConfig(resolver.SyncConfig)); err != nil {
return fmt.Errorf("error setting sync_config: %w", err)
Expand All @@ -254,6 +286,10 @@ func resourceResolverRead(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error setting caching_config: %w", err)
}

if err := d.Set("runtime", flattenRuntime(resolver.Runtime)); err != nil {
return fmt.Errorf("error setting runtime: %w", err)
}

return nil
}

Expand All @@ -267,6 +303,10 @@ func resourceResolverUpdate(d *schema.ResourceData, meta interface{}) error {
Kind: aws.String(d.Get("kind").(string)),
}

if v, ok := d.GetOk("code"); ok {
input.Code = aws.String(v.(string))
}

if v, ok := d.GetOk("data_source"); ok {
input.DataSourceName = aws.String(v.(string))
}
Expand Down Expand Up @@ -298,6 +338,10 @@ func resourceResolverUpdate(d *schema.ResourceData, meta interface{}) error {
input.SyncConfig = expandSyncConfig(v.([]interface{}))
}

if v, ok := d.GetOk("runtime"); ok && len(v.([]interface{})) > 0 {
input.Runtime = expandRuntime(v.([]interface{}))
}

mutexKey := fmt.Sprintf("appsync-schema-%s", d.Get("api_id").(string))
conns.GlobalMutexKV.Lock(mutexKey)
defer conns.GlobalMutexKV.Unlock(mutexKey)
Expand Down Expand Up @@ -369,6 +413,22 @@ func expandResolverCachingConfig(l []interface{}) *appsync.CachingConfig {
return cachingConfig
}

func expandPipelineConfig(l []interface{}) *appsync.PipelineConfig {
if len(l) < 1 || l[0] == nil {
return nil
}

m := l[0].(map[string]interface{})

config := &appsync.PipelineConfig{}

if v, ok := m["functions"].([]interface{}); ok && len(v) > 0 {
config.Functions = flex.ExpandStringList(v)
}

return config
}

func flattenPipelineConfig(c *appsync.PipelineConfig) []interface{} {
if c == nil {
return nil
Expand Down
63 changes: 63 additions & 0 deletions internal/service/appsync/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,36 @@ func testAccResolver_basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "request_template"),
resource.TestCheckResourceAttr(resourceName, "max_batch_size", "0"),
resource.TestCheckResourceAttr(resourceName, "sync_config.#", "0"),
resource.TestCheckResourceAttr(resourceName, "runtime.#", "0"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccResolver_code(t *testing.T) {
var resolver1 appsync.Resolver
rName := fmt.Sprintf("tfacctest%d", sdkacctest.RandInt())
resourceName := "aws_appsync_resolver.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t); acctest.PreCheckPartitionHasService(appsync.EndpointsID, t) },
ErrorCheck: acctest.ErrorCheck(t, appsync.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckResolverDestroy,
Steps: []resource.TestStep{
{
Config: testAccResolverConfig_code(rName, "test-fixtures/test-code.js"),
Check: resource.ComposeTestCheckFunc(
testAccCheckResolverExists(resourceName, &resolver1),
resource.TestCheckResourceAttr(resourceName, "runtime.#", "1"),
resource.TestCheckResourceAttr(resourceName, "runtime.0.name", "APPSYNC_JS"),
resource.TestCheckResourceAttr(resourceName, "runtime.0.runtime_version", "1.0.0"),
),
},
{
Expand Down Expand Up @@ -853,3 +883,36 @@ EOF
}
`, rName)
}

func testAccResolverConfig_code(rName, code string) string {
return testAccResolverConfig_base(rName) + fmt.Sprintf(`
resource "aws_appsync_function" "test" {
api_id = aws_appsync_graphql_api.test.id
data_source = aws_appsync_datasource.test.name
name = %[1]q
code = file("%[2]s")
runtime {
name = "APPSYNC_JS"
runtime_version = "1.0.0"
}
}
resource "aws_appsync_resolver" "test" {
api_id = aws_appsync_graphql_api.test.id
field = "singlePost"
type = "Query"
code = file("%[2]s")
kind = "PIPELINE"
pipeline_config {
functions = [aws_appsync_function.test.function_id]
}
runtime {
name = "APPSYNC_JS"
runtime_version = "1.0.0"
}
}
`, rName, code)
}
Loading

0 comments on commit 12d1458

Please sign in to comment.