github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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, "/test"), 27 resource.TestCheckResourceAttr( 28 "aws_api_gateway_resource.test", "path_part", "test"), 29 resource.TestCheckResourceAttr( 30 "aws_api_gateway_resource.test", "path", "/test"), 31 ), 32 }, 33 }, 34 }) 35 } 36 37 func TestAccAWSAPIGatewayResource_update(t *testing.T) { 38 var conf apigateway.Resource 39 40 resource.Test(t, resource.TestCase{ 41 PreCheck: func() { testAccPreCheck(t) }, 42 Providers: testAccProviders, 43 CheckDestroy: testAccCheckAWSAPIGatewayResourceDestroy, 44 Steps: []resource.TestStep{ 45 resource.TestStep{ 46 Config: testAccAWSAPIGatewayResourceConfig, 47 Check: resource.ComposeTestCheckFunc( 48 testAccCheckAWSAPIGatewayResourceExists("aws_api_gateway_resource.test", &conf), 49 testAccCheckAWSAPIGatewayResourceAttributes(&conf, "/test"), 50 resource.TestCheckResourceAttr( 51 "aws_api_gateway_resource.test", "path_part", "test"), 52 resource.TestCheckResourceAttr( 53 "aws_api_gateway_resource.test", "path", "/test"), 54 ), 55 }, 56 57 resource.TestStep{ 58 Config: testAccAWSAPIGatewayResourceConfig_updatePathPart, 59 Check: resource.ComposeTestCheckFunc( 60 testAccCheckAWSAPIGatewayResourceExists("aws_api_gateway_resource.test", &conf), 61 testAccCheckAWSAPIGatewayResourceAttributes(&conf, "/test_changed"), 62 resource.TestCheckResourceAttr( 63 "aws_api_gateway_resource.test", "path_part", "test_changed"), 64 resource.TestCheckResourceAttr( 65 "aws_api_gateway_resource.test", "path", "/test_changed"), 66 ), 67 }, 68 }, 69 }) 70 } 71 72 func testAccCheckAWSAPIGatewayResourceAttributes(conf *apigateway.Resource, path string) resource.TestCheckFunc { 73 return func(s *terraform.State) error { 74 if *conf.Path != path { 75 return fmt.Errorf("Wrong Path: %q", conf.Path) 76 } 77 78 return nil 79 } 80 } 81 82 func testAccCheckAWSAPIGatewayResourceExists(n string, res *apigateway.Resource) resource.TestCheckFunc { 83 return func(s *terraform.State) error { 84 rs, ok := s.RootModule().Resources[n] 85 if !ok { 86 return fmt.Errorf("Not found: %s", n) 87 } 88 89 if rs.Primary.ID == "" { 90 return fmt.Errorf("No API Gateway Resource ID is set") 91 } 92 93 conn := testAccProvider.Meta().(*AWSClient).apigateway 94 95 req := &apigateway.GetResourceInput{ 96 ResourceId: aws.String(rs.Primary.ID), 97 RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), 98 } 99 describe, err := conn.GetResource(req) 100 if err != nil { 101 return err 102 } 103 104 if *describe.Id != rs.Primary.ID { 105 return fmt.Errorf("APIGateway Resource not found") 106 } 107 108 *res = *describe 109 110 return nil 111 } 112 } 113 114 func testAccCheckAWSAPIGatewayResourceDestroy(s *terraform.State) error { 115 conn := testAccProvider.Meta().(*AWSClient).apigateway 116 117 for _, rs := range s.RootModule().Resources { 118 if rs.Type != "aws_api_gateway_resource" { 119 continue 120 } 121 122 req := &apigateway.GetResourcesInput{ 123 RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), 124 } 125 describe, err := conn.GetResources(req) 126 127 if err == nil { 128 if len(describe.Items) != 0 && 129 *describe.Items[0].Id == rs.Primary.ID { 130 return fmt.Errorf("API Gateway Resource still exists") 131 } 132 } 133 134 aws2err, ok := err.(awserr.Error) 135 if !ok { 136 return err 137 } 138 if aws2err.Code() != "NotFoundException" { 139 return err 140 } 141 142 return nil 143 } 144 145 return nil 146 } 147 148 const testAccAWSAPIGatewayResourceConfig = ` 149 resource "aws_api_gateway_rest_api" "test" { 150 name = "test" 151 } 152 153 resource "aws_api_gateway_resource" "test" { 154 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 155 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 156 path_part = "test" 157 } 158 ` 159 160 const testAccAWSAPIGatewayResourceConfig_updatePathPart = ` 161 resource "aws_api_gateway_rest_api" "test" { 162 name = "test" 163 } 164 165 resource "aws_api_gateway_resource" "test" { 166 rest_api_id = "${aws_api_gateway_rest_api.test.id}" 167 parent_id = "${aws_api_gateway_rest_api.test.root_resource_id}" 168 path_part = "test_changed" 169 } 170 `