-
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.
New Resource: appsync_graphql_api (#2494)
* [WIP]New Resource: appsync_graphql_api * testacc * docs * fix typo * update * Remove not-found check from Update
- Loading branch information
1 parent
d6a1a01
commit 8f671af
Showing
6 changed files
with
399 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
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,195 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"regexp" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/appsync" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
) | ||
|
||
func resourceAwsAppsyncGraphqlApi() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceAwsAppsyncGraphqlApiCreate, | ||
Read: resourceAwsAppsyncGraphqlApiRead, | ||
Update: resourceAwsAppsyncGraphqlApiUpdate, | ||
Delete: resourceAwsAppsyncGraphqlApiDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"authentication_type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
appsync.AuthenticationTypeApiKey, | ||
appsync.AuthenticationTypeAwsIam, | ||
appsync.AuthenticationTypeAmazonCognitoUserPools, | ||
}, false), | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { | ||
value := v.(string) | ||
if !regexp.MustCompile(`[_A-Za-z][_0-9A-Za-z]*`).MatchString(value) { | ||
errors = append(errors, fmt.Errorf("%q must match [_A-Za-z][_0-9A-Za-z]*", k)) | ||
} | ||
return | ||
}, | ||
}, | ||
"user_pool_config": { | ||
Type: schema.TypeList, | ||
Optional: true, | ||
MaxItems: 1, | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"app_id_client_regex": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
}, | ||
"aws_region": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
"default_action": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
appsync.DefaultActionAllow, | ||
appsync.DefaultActionDeny, | ||
}, false), | ||
}, | ||
"user_pool_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
"arn": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsAppsyncGraphqlApiCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).appsyncconn | ||
|
||
input := &appsync.CreateGraphqlApiInput{ | ||
AuthenticationType: aws.String(d.Get("authentication_type").(string)), | ||
Name: aws.String(d.Get("name").(string)), | ||
} | ||
|
||
if v, ok := d.GetOk("user_pool_config"); ok { | ||
input.UserPoolConfig = expandAppsyncGraphqlApiUserPoolConfig(v.([]interface{})) | ||
} | ||
|
||
resp, err := conn.CreateGraphqlApi(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(*resp.GraphqlApi.ApiId) | ||
d.Set("arn", resp.GraphqlApi.Arn) | ||
return nil | ||
} | ||
|
||
func resourceAwsAppsyncGraphqlApiRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).appsyncconn | ||
|
||
input := &appsync.GetGraphqlApiInput{ | ||
ApiId: aws.String(d.Id()), | ||
} | ||
|
||
resp, err := conn.GetGraphqlApi(input) | ||
if err != nil { | ||
if isAWSErr(err, appsync.ErrCodeNotFoundException, "") { | ||
log.Printf("[WARN] No such entity found for Appsync Graphql API (%s)", d.Id()) | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
d.Set("authentication_type", resp.GraphqlApi.AuthenticationType) | ||
d.Set("name", resp.GraphqlApi.Name) | ||
d.Set("user_pool_config", flattenAppsyncGraphqlApiUserPoolConfig(resp.GraphqlApi.UserPoolConfig)) | ||
d.Set("arn", resp.GraphqlApi.Arn) | ||
return nil | ||
} | ||
|
||
func resourceAwsAppsyncGraphqlApiUpdate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).appsyncconn | ||
|
||
input := &appsync.UpdateGraphqlApiInput{ | ||
ApiId: aws.String(d.Id()), | ||
Name: aws.String(d.Get("name").(string)), | ||
} | ||
|
||
if d.HasChange("authentication_type") { | ||
input.AuthenticationType = aws.String(d.Get("authentication_type").(string)) | ||
} | ||
if d.HasChange("user_pool_config") { | ||
input.UserPoolConfig = expandAppsyncGraphqlApiUserPoolConfig(d.Get("user_pool_config").([]interface{})) | ||
} | ||
|
||
_, err := conn.UpdateGraphqlApi(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return resourceAwsAppsyncGraphqlApiRead(d, meta) | ||
} | ||
|
||
func resourceAwsAppsyncGraphqlApiDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).appsyncconn | ||
|
||
input := &appsync.DeleteGraphqlApiInput{ | ||
ApiId: aws.String(d.Id()), | ||
} | ||
_, err := conn.DeleteGraphqlApi(input) | ||
if err != nil { | ||
if isAWSErr(err, appsync.ErrCodeNotFoundException, "") { | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func expandAppsyncGraphqlApiUserPoolConfig(config []interface{}) *appsync.UserPoolConfig { | ||
if len(config) < 1 { | ||
return nil | ||
} | ||
cg := config[0].(map[string]interface{}) | ||
upc := &appsync.UserPoolConfig{ | ||
AwsRegion: aws.String(cg["aws_region"].(string)), | ||
DefaultAction: aws.String(cg["default_action"].(string)), | ||
UserPoolId: aws.String(cg["user_pool_id"].(string)), | ||
} | ||
if v, ok := cg["app_id_client_regex"].(string); ok && v != "" { | ||
upc.AppIdClientRegex = aws.String(v) | ||
} | ||
return upc | ||
} | ||
|
||
func flattenAppsyncGraphqlApiUserPoolConfig(upc *appsync.UserPoolConfig) []interface{} { | ||
if upc == nil { | ||
return []interface{}{} | ||
} | ||
m := make(map[string]interface{}, 1) | ||
|
||
m["aws_region"] = *upc.AwsRegion | ||
m["default_action"] = *upc.DefaultAction | ||
m["user_pool_id"] = *upc.UserPoolId | ||
if upc.AppIdClientRegex != nil { | ||
m["app_id_client_regex"] = *upc.AppIdClientRegex | ||
} | ||
|
||
return []interface{}{m} | ||
} |
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,147 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/appsync" | ||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAwsAppsyncGraphqlApi_basic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAwsAppsyncGraphqlApiDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAppsyncGraphqlApiConfig_apikey(acctest.RandString(5)), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAwsAppsyncGraphqlApiExists("aws_appsync_graphql_api.test_apikey"), | ||
resource.TestCheckResourceAttrSet("aws_appsync_graphql_api.test_apikey", "arn"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAwsAppsyncGraphqlApi_iam(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAwsAppsyncGraphqlApiDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAppsyncGraphqlApiConfig_iam(acctest.RandString(5)), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAwsAppsyncGraphqlApiExists("aws_appsync_graphql_api.test_iam"), | ||
resource.TestCheckResourceAttrSet("aws_appsync_graphql_api.test_iam", "arn"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAwsAppsyncGraphqlApi_cognito(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckAwsAppsyncGraphqlApiDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccAppsyncGraphqlApiConfig_cognito(acctest.RandString(5)), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckAwsAppsyncGraphqlApiExists("aws_appsync_graphql_api.test_cognito"), | ||
resource.TestCheckResourceAttrSet("aws_appsync_graphql_api.test_cognito", "arn"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckAwsAppsyncGraphqlApiDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*AWSClient).appsyncconn | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "aws_appsync_graphql_api" { | ||
continue | ||
} | ||
|
||
input := &appsync.GetGraphqlApiInput{ | ||
ApiId: aws.String(rs.Primary.ID), | ||
} | ||
|
||
_, err := conn.GetGraphqlApi(input) | ||
if err != nil { | ||
if isAWSErr(err, appsync.ErrCodeNotFoundException, "") { | ||
return nil | ||
} | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func testAccCheckAwsAppsyncGraphqlApiExists(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
|
||
conn := testAccProvider.Meta().(*AWSClient).appsyncconn | ||
|
||
input := &appsync.GetGraphqlApiInput{ | ||
ApiId: aws.String(rs.Primary.ID), | ||
} | ||
|
||
_, err := conn.GetGraphqlApi(input) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccAppsyncGraphqlApiConfig_apikey(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_appsync_graphql_api" "test_apikey" { | ||
authentication_type = "API_KEY" | ||
name = "tf_appsync_%s" | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccAppsyncGraphqlApiConfig_iam(rName string) string { | ||
return fmt.Sprintf(` | ||
resource "aws_appsync_graphql_api" "test_iam" { | ||
authentication_type = "AWS_IAM" | ||
name = "tf_appsync_%s" | ||
} | ||
`, rName) | ||
} | ||
|
||
func testAccAppsyncGraphqlApiConfig_cognito(rName string) string { | ||
return fmt.Sprintf(` | ||
data "aws_region" "test" { | ||
current = true | ||
} | ||
resource "aws_cognito_user_pool" "test" { | ||
name = "tf-%s" | ||
} | ||
resource "aws_appsync_graphql_api" "test_cognito" { | ||
authentication_type = "AMAZON_COGNITO_USER_POOLS" | ||
name = "tf_appsync_%s" | ||
user_pool_config { | ||
aws_region = "${data.aws_region.test.name}" | ||
default_action = "ALLOW" | ||
user_pool_id = "${aws_cognito_user_pool.test.id}" | ||
} | ||
} | ||
`, rName, 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.