github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_api_gateway_model_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 TestAccAWSAPIGatewayModel_basic(t *testing.T) {
    15  	var conf apigateway.Model
    16  
    17  	resource.Test(t, resource.TestCase{
    18  		PreCheck:     func() { testAccPreCheck(t) },
    19  		Providers:    testAccProviders,
    20  		CheckDestroy: testAccCheckAWSAPIGatewayModelDestroy,
    21  		Steps: []resource.TestStep{
    22  			resource.TestStep{
    23  				Config: testAccAWSAPIGatewayModelConfig,
    24  				Check: resource.ComposeTestCheckFunc(
    25  					testAccCheckAWSAPIGatewayModelExists("aws_api_gateway_model.test", &conf),
    26  					testAccCheckAWSAPIGatewayModelAttributes(&conf),
    27  					resource.TestCheckResourceAttr(
    28  						"aws_api_gateway_model.test", "name", "test"),
    29  					resource.TestCheckResourceAttr(
    30  						"aws_api_gateway_model.test", "description", "a test schema"),
    31  					resource.TestCheckResourceAttr(
    32  						"aws_api_gateway_model.test", "content_type", "application/json"),
    33  				),
    34  			},
    35  		},
    36  	})
    37  }
    38  
    39  func testAccCheckAWSAPIGatewayModelAttributes(conf *apigateway.Model) resource.TestCheckFunc {
    40  	return func(s *terraform.State) error {
    41  		if *conf.Name != "test" {
    42  			return fmt.Errorf("Wrong Name: %q", *conf.Name)
    43  		}
    44  		if *conf.Description != "a test schema" {
    45  			return fmt.Errorf("Wrong Description: %q", *conf.Description)
    46  		}
    47  		if *conf.ContentType != "application/json" {
    48  			return fmt.Errorf("Wrong ContentType: %q", *conf.ContentType)
    49  		}
    50  
    51  		return nil
    52  	}
    53  }
    54  
    55  func testAccCheckAWSAPIGatewayModelExists(n string, res *apigateway.Model) resource.TestCheckFunc {
    56  	return func(s *terraform.State) error {
    57  		rs, ok := s.RootModule().Resources[n]
    58  		if !ok {
    59  			return fmt.Errorf("Not found: %s", n)
    60  		}
    61  
    62  		if rs.Primary.ID == "" {
    63  			return fmt.Errorf("No API Gateway Model ID is set")
    64  		}
    65  
    66  		conn := testAccProvider.Meta().(*AWSClient).apigateway
    67  
    68  		req := &apigateway.GetModelInput{
    69  			ModelName: aws.String("test"),
    70  			RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
    71  		}
    72  		describe, err := conn.GetModel(req)
    73  		if err != nil {
    74  			return err
    75  		}
    76  		if *describe.Id != rs.Primary.ID {
    77  			return fmt.Errorf("APIGateway Model not found")
    78  		}
    79  
    80  		*res = *describe
    81  
    82  		return nil
    83  	}
    84  }
    85  
    86  func testAccCheckAWSAPIGatewayModelDestroy(s *terraform.State) error {
    87  	conn := testAccProvider.Meta().(*AWSClient).apigateway
    88  
    89  	for _, rs := range s.RootModule().Resources {
    90  		if rs.Type != "aws_api_gateway_model" {
    91  			continue
    92  		}
    93  
    94  		req := &apigateway.GetModelsInput{
    95  			RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID),
    96  		}
    97  		describe, err := conn.GetModels(req)
    98  
    99  		if err == nil {
   100  			if len(describe.Items) != 0 &&
   101  				*describe.Items[0].Id == rs.Primary.ID {
   102  				return fmt.Errorf("API Gateway Model still exists")
   103  			}
   104  		}
   105  
   106  		aws2err, ok := err.(awserr.Error)
   107  		if !ok {
   108  			return err
   109  		}
   110  		if aws2err.Code() != "NotFoundException" {
   111  			return err
   112  		}
   113  
   114  		return nil
   115  	}
   116  
   117  	return nil
   118  }
   119  
   120  const testAccAWSAPIGatewayModelConfig = `
   121  resource "aws_api_gateway_rest_api" "test" {
   122    name = "test"
   123  }
   124  
   125  resource "aws_api_gateway_model" "test" {
   126    rest_api_id = "${aws_api_gateway_rest_api.test.id}"
   127    name = "test"
   128    description = "a test schema"
   129    content_type = "application/json"
   130    schema = <<EOF
   131  {
   132    "type": "object"
   133  }
   134  EOF
   135  }
   136  `