-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Resource: appsync_graphql_api #2494
Merged
radeksimko
merged 6 commits into
hashicorp:master
from
atsushi-ishibashi:appsync_graphql_api
Feb 12, 2018
+399
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9b379d3
[WIP]New Resource: appsync_graphql_api
atsushi-ishibashi 910366b
testacc
atsushi-ishibashi 813875c
docs
atsushi-ishibashi f8e7c19
fix typo
atsushi-ishibashi 210d7a8
update
atsushi-ishibashi c8bca34
Remove not-found check from Update
radeksimko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mind separating out the 3 steps into 3 tests? This will allow us to run them in parallel more easily. You can keep
testAccAppsyncGraphqlApiConfig_apikey
in_basic
here, just to stick to the convention.