github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_api_gateway_resource_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/aws/aws-sdk-go/aws" 8 "github.com/aws/aws-sdk-go/aws/awserr" 9 "github.com/aws/aws-sdk-go/service/apigateway" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 func TestAccAWSAPIGatewayResource_basic(t *testing.T) { 15 var conf apigateway.Resource 16 17 resource.Test(t, resource.TestCase{ 18 PreCheck: func() { testAccPreCheck(t) }, 19 Providers: testAccProviders, 20 CheckDestroy: testAccCheckAWSAPIGatewayResourceDestroy, 21 Steps: []resource.TestStep{ 22 resource.TestStep{ 23 Config: testAccAWSAPIGatewayResourceConfig, 24 Check: resource.ComposeTestCheckFunc( 25 testAccCheckAWSAPIGatewayResourceExists("aws_api_gateway_resource.test", &conf), 26 testAccCheckAWSAPIGatewayResourceAttributes(&conf), 27 resource.TestCheckResourceAttr( 28 "aws_api_gateway_resource.test", "path_part", "test"), 29 ), 30 }, 31 }, 32 }) 33 } 34 35 func testAccCheckAWSAPIGatewayResourceAttributes(conf *apigateway.Resource) resource.TestCheckFunc { 36 return func(s *terraform.State) error { 37 if *conf.Path != "/test" { 38 return fmt.Errorf("Wrong Path: %q", conf.Path) 39 } 40 41 return nil 42 } 43 } 44 45 func testAccCheckAWSAPIGatewayResourceExists(n string, res *apigateway.Resource) resource.TestCheckFunc { 46 return func(s *terraform.State) error { 47 rs, ok := s.RootModule().Resources[n] 48 if !ok { 49 return fmt.Errorf("Not found: %s", n) 50 } 51 52 if rs.Primary.ID == "" { 53 return fmt.Errorf("No API Gateway Resource ID is set") 54 } 55 56 conn := testAccProvider.Meta().(*AWSClient).apigateway 57 58 req := &apigateway.GetResourceInput{ 59 ResourceId: aws.String(rs.Primary.ID), 60 RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), 61 } 62 describe, err := conn.GetResource(req) 63 if err != nil { 64 return err 65 } 66 67 if *describe.Id != rs.Primary.ID { 68 return fmt.Errorf("APIGateway Resource not found") 69 } 70 71 *res = *describe 72 73 return nil 74 } 75 } 76 77 func testAccCheckAWSAPIGatewayResourceDestroy(s *terraform.State) error { 78 conn := testAccProvider.Meta().(*AWSClient).apigateway 79 80 for _, rs := range s.RootModule().Resources { 81 if rs.Type != "aws_api_gateway_resource" { 82 continue 83 } 84 85 req := &apigateway.GetResourcesInput{ 86 RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), 87 } 88 describe, err := conn.GetResources(req) 89 90 if err == nil { 91 if len(describe.Items) != 0 && 92 *describe.Items[0].Id == rs.Primary.ID { 93 return fmt.Errorf("API Gateway Resource still exists") 94 } 95 } 96 97 aws2err, ok := err.(awserr.Error) 98 if !ok { 99 return err 100 } 101 if aws2err.Code() != "NotFoundException" { 102 return err 103 } 104 105 return nil 106 } 107 108 return nil 109 } 110 111 const testAccAWSAPIGatewayResourceConfig = ` 112 resource "aws_api_gateway_rest_api" "test" { 113 name = "test" 114 } 115 116 resource "aws_api_gateway_resource" "test" { 117 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 118 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 119 path_part = "test" 120 } 121 `