From b4c99f10097220879e29d40bcf31a210b07ca710 Mon Sep 17 00:00:00 2001 From: Raphael Randschau Date: Sat, 5 Mar 2016 23:17:55 +0100 Subject: [PATCH] Add aws_api_gateway_model resource --- builtin/providers/aws/provider.go | 1 + .../aws/resource_aws_api_gateway_model.go | 168 ++++++++++++++++++ .../resource_aws_api_gateway_model_test.go | 136 ++++++++++++++ .../aws/r/api_gateway_model.html.markdown | 42 +++++ website/source/layouts/aws.erb | 3 + 5 files changed, 350 insertions(+) create mode 100644 builtin/providers/aws/resource_aws_api_gateway_model.go create mode 100644 builtin/providers/aws/resource_aws_api_gateway_model_test.go create mode 100644 website/source/docs/providers/aws/r/api_gateway_model.html.markdown diff --git a/builtin/providers/aws/provider.go b/builtin/providers/aws/provider.go index 65a213428..a4b99d8e9 100644 --- a/builtin/providers/aws/provider.go +++ b/builtin/providers/aws/provider.go @@ -115,6 +115,7 @@ func Provider() terraform.ResourceProvider { "aws_ami_copy": resourceAwsAmiCopy(), "aws_ami_from_instance": resourceAwsAmiFromInstance(), "aws_api_gateway_rest_api": resourceAwsApiGatewayRestApi(), + "aws_api_gateway_model": resourceAwsApiGatewayModel(), "aws_api_gateway_resource": resourceAwsApiGatewayResource(), "aws_api_gateway_method": resourceAwsApiGatewayMethod(), "aws_api_gateway_method_response": resourceAwsApiGatewayMethodResponse(), diff --git a/builtin/providers/aws/resource_aws_api_gateway_model.go b/builtin/providers/aws/resource_aws_api_gateway_model.go new file mode 100644 index 000000000..cca7517f7 --- /dev/null +++ b/builtin/providers/aws/resource_aws_api_gateway_model.go @@ -0,0 +1,168 @@ +package aws + +import ( + "fmt" + "log" + "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/service/apigateway" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/helper/schema" +) + +func resourceAwsApiGatewayModel() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsApiGatewayModelCreate, + Read: resourceAwsApiGatewayModelRead, + Update: resourceAwsApiGatewayModelUpdate, + Delete: resourceAwsApiGatewayModelDelete, + + Schema: map[string]*schema.Schema{ + "rest_api_id": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "description": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + }, + + "schema": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + }, + + "content_type": &schema.Schema{ + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + }, + } +} + +func resourceAwsApiGatewayModelCreate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigateway + log.Printf("[DEBUG] Creating API Gateway Model") + + var description *string + if v, ok := d.GetOk("description"); ok { + description = aws.String(v.(string)) + } + var schema *string + if v, ok := d.GetOk("schema"); ok { + schema = aws.String(v.(string)) + } + + var err error + model, err := conn.CreateModel(&apigateway.CreateModelInput{ + Name: aws.String(d.Get("name").(string)), + RestApiId: aws.String(d.Get("rest_api_id").(string)), + ContentType: aws.String(d.Get("content_type").(string)), + + Description: description, + Schema: schema, + }) + + if err != nil { + return fmt.Errorf("Error creating API Gateway Model: %s", err) + } + + d.SetId(*model.Id) + + return nil +} + +func resourceAwsApiGatewayModelRead(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigateway + + log.Printf("[DEBUG] Reading API Gateway Model %s", d.Id()) + out, err := conn.GetModel(&apigateway.GetModelInput{ + ModelName: aws.String(d.Get("name").(string)), + RestApiId: aws.String(d.Get("rest_api_id").(string)), + }) + if err != nil { + if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { + d.SetId("") + return nil + } + return err + } + log.Printf("[DEBUG] Received API Gateway Model: %s", out) + d.SetId(*out.Id) + d.Set("description", out.Description) + d.Set("schema", out.Schema) + d.Set("content_type", out.ContentType) + + return nil +} + +func resourceAwsApiGatewayModelUpdate(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigateway + + log.Printf("[DEBUG] Reading API Gateway Model %s", d.Id()) + operations := make([]*apigateway.PatchOperation, 0) + if d.HasChange("description") { + operations = append(operations, &apigateway.PatchOperation{ + Op: aws.String("replace"), + Path: aws.String("/description"), + Value: aws.String(d.Get("description").(string)), + }) + } + if d.HasChange("schema") { + operations = append(operations, &apigateway.PatchOperation{ + Op: aws.String("replace"), + Path: aws.String("/schema"), + Value: aws.String(d.Get("schema").(string)), + }) + } + + out, err := conn.UpdateModel(&apigateway.UpdateModelInput{ + ModelName: aws.String(d.Get("name").(string)), + RestApiId: aws.String(d.Get("rest_api_id").(string)), + PatchOperations: operations, + }) + if err != nil { + return err + } + log.Printf("[DEBUG] Received API Gateway Model: %s", out) + + return resourceAwsApiGatewayModelRead(d, meta) +} + +func resourceAwsApiGatewayModelDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).apigateway + log.Printf("[DEBUG] Deleting API Gateway Model: %s", d.Id()) + + return resource.Retry(5*time.Minute, func() error { + log.Printf("[DEBUG] schema is %#v", d) + _, err := conn.DeleteModel(&apigateway.DeleteModelInput{ + ModelName: aws.String(d.Get("name").(string)), + RestApiId: aws.String(d.Get("rest_api_id").(string)), + }) + if err == nil { + return nil + } + + apigatewayErr, ok := err.(awserr.Error) + if apigatewayErr.Code() == "NotFoundException" { + return nil + } + + if !ok { + return resource.RetryError{Err: err} + } + + return resource.RetryError{Err: err} + }) +} diff --git a/builtin/providers/aws/resource_aws_api_gateway_model_test.go b/builtin/providers/aws/resource_aws_api_gateway_model_test.go new file mode 100644 index 000000000..4708b73b2 --- /dev/null +++ b/builtin/providers/aws/resource_aws_api_gateway_model_test.go @@ -0,0 +1,136 @@ +package aws + +import ( + "fmt" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/service/apigateway" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccAWSAPIGatewayModel_basic(t *testing.T) { + var conf apigateway.Model + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAPIGatewayModelDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAPIGatewayModelConfig, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAPIGatewayModelExists("aws_api_gateway_model.test", &conf), + testAccCheckAWSAPIGatewayModelAttributes(&conf), + resource.TestCheckResourceAttr( + "aws_api_gateway_model.test", "name", "test"), + resource.TestCheckResourceAttr( + "aws_api_gateway_model.test", "description", "a test schema"), + resource.TestCheckResourceAttr( + "aws_api_gateway_model.test", "content_type", "application/json"), + ), + }, + }, + }) +} + +func testAccCheckAWSAPIGatewayModelAttributes(conf *apigateway.Model) resource.TestCheckFunc { + return func(s *terraform.State) error { + if *conf.Name != "test" { + return fmt.Errorf("Wrong Name: %q", *conf.Name) + } + if *conf.Description != "a test schema" { + return fmt.Errorf("Wrong Description: %q", *conf.Description) + } + if *conf.ContentType != "application/json" { + return fmt.Errorf("Wrong ContentType: %q", *conf.ContentType) + } + + return nil + } +} + +func testAccCheckAWSAPIGatewayModelExists(n string, res *apigateway.Model) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No API Gateway Model ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).apigateway + + req := &apigateway.GetModelInput{ + ModelName: aws.String("test"), + RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), + } + describe, err := conn.GetModel(req) + if err != nil { + return err + } + if *describe.Id != rs.Primary.ID { + return fmt.Errorf("APIGateway Model not found") + } + + *res = *describe + + return nil + } +} + +func testAccCheckAWSAPIGatewayModelDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).apigateway + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_api_gateway_model" { + continue + } + + req := &apigateway.GetModelsInput{ + RestApiId: aws.String(s.RootModule().Resources["aws_api_gateway_rest_api.test"].Primary.ID), + } + describe, err := conn.GetModels(req) + + if err == nil { + if len(describe.Items) != 0 && + *describe.Items[0].Id == rs.Primary.ID { + return fmt.Errorf("API Gateway Model still exists") + } + } + + aws2err, ok := err.(awserr.Error) + if !ok { + return err + } + if aws2err.Code() != "NotFoundException" { + return err + } + + return nil + } + + return nil +} + +const testAccAWSAPIGatewayModelConfig = ` +resource "aws_api_gateway_rest_api" "test" { + name = "test" +} + +resource "aws_api_gateway_model" "test" { + rest_api_id = "${aws_api_gateway_rest_api.test.id}" + name = "test" + description = "a test schema" + content_type = "application/json" + schema = <> aws_api_gateway_rest_api + > + aws_api_gateway_model + > aws_api_gateway_resource