Skip to content

Commit

Permalink
testacc
Browse files Browse the repository at this point in the history
  • Loading branch information
atsushi-ishibashi committed Dec 22, 2017
1 parent 9b379d3 commit 910366b
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 23 deletions.
3 changes: 3 additions & 0 deletions aws/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/aws/aws-sdk-go/service/acm"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/aws/aws-sdk-go/service/applicationautoscaling"
"github.com/aws/aws-sdk-go/service/appsync"
"github.com/aws/aws-sdk-go/service/athena"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/batch"
Expand Down Expand Up @@ -194,6 +195,7 @@ type AWSClient struct {
athenaconn *athena.Athena
dxconn *directconnect.DirectConnect
mediastoreconn *mediastore.MediaStore
appsyncconn *appsync.AppSync
}

func (c *AWSClient) S3() *s3.S3 {
Expand Down Expand Up @@ -436,6 +438,7 @@ func (c *Config) Client() (interface{}, error) {
client.athenaconn = athena.New(sess)
client.dxconn = directconnect.New(sess)
client.mediastoreconn = mediastore.New(sess)
client.appsyncconn = appsync.New(sess)

// Workaround for https://github.com/aws/aws-sdk-go/issues/1376
client.kinesisconn.Handlers.Retry.PushBack(func(r *request.Request) {
Expand Down
1 change: 1 addition & 0 deletions aws/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ func Provider() terraform.ResourceProvider {
"aws_appautoscaling_target": resourceAwsAppautoscalingTarget(),
"aws_appautoscaling_policy": resourceAwsAppautoscalingPolicy(),
"aws_appautoscaling_scheduled_action": resourceAwsAppautoscalingScheduledAction(),
"aws_appsync_graphql_api": resourceAwsAppsyncGraphqlApi(),
"aws_athena_database": resourceAwsAthenaDatabase(),
"aws_athena_named_query": resourceAwsAthenaNamedQuery(),
"aws_autoscaling_attachment": resourceAwsAutoscalingAttachment(),
Expand Down
34 changes: 11 additions & 23 deletions aws/resource_aws_appsync_graphql_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/appsync"
"github.com/hashicorp/terraform/helper/schema"
)
Expand Down Expand Up @@ -123,21 +122,16 @@ func resourceAwsAppsyncGraphqlApiRead(d *schema.ResourceData, meta interface{})

resp, err := conn.GetGraphqlApi(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case appsync.ErrCodeNotFoundException:
d.SetId("")
return nil
}
if isAWSErr(err, appsync.ErrCodeNotFoundException, "") {
d.SetId("")
return nil
}
return err
}

d.Set("authentication_type", resp.GraphqlApi.AuthenticationType)
d.Set("name", resp.GraphqlApi.Name)
if err := d.Set("user_pool_config", flattenAppsyncGraphqlApiUserPoolConfig(resp.GraphqlApi.UserPoolConfig)); err != nil {
return fmt.Errorf("Failed setting user_pool_config: %s", err)
}
d.Set("user_pool_config", flattenAppsyncGraphqlApiUserPoolConfig(resp.GraphqlApi.UserPoolConfig))
d.Set("arn", resp.GraphqlApi.Arn)
return nil
}
Expand All @@ -159,12 +153,9 @@ func resourceAwsAppsyncGraphqlApiUpdate(d *schema.ResourceData, meta interface{}

_, err := conn.UpdateGraphqlApi(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case appsync.ErrCodeNotFoundException:
d.SetId("")
return nil
}
if isAWSErr(err, appsync.ErrCodeNotFoundException, "") {
d.SetId("")
return nil
}
return err
}
Expand All @@ -176,16 +167,13 @@ func resourceAwsAppsyncGraphqlApiDelete(d *schema.ResourceData, meta interface{}
conn := meta.(*AWSClient).appsyncconn

input := &appsync.DeleteGraphqlApiInput{
AppId: aws.String(d.Id()),
ApiId: aws.String(d.Id()),
}
_, err := conn.DeleteGraphqlApi(input)
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case appsync.ErrCodeNotFoundException:
d.SetId("")
return nil
}
if isAWSErr(err, appsync.ErrCodeNotFoundException, "") {
d.SetId("")
return nil
}
return err
}
Expand Down
111 changes: 111 additions & 0 deletions aws/resource_aws_appsync_graphql_api_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,112 @@
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"),
),
},
{
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"),
),
},
{
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 {
_, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

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(`
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 = "us-west-2"
default_action = "ALLOW"
user_pool_id = "${aws_cognito_user_pool.test.id}"
}
}
`, rName, rName)
}

0 comments on commit 910366b

Please sign in to comment.