github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/aws/resource_aws_api_gateway_model.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/apigateway"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsApiGatewayModel() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsApiGatewayModelCreate,
    18  		Read:   resourceAwsApiGatewayModelRead,
    19  		Update: resourceAwsApiGatewayModelUpdate,
    20  		Delete: resourceAwsApiGatewayModelDelete,
    21  
    22  		Schema: map[string]*schema.Schema{
    23  			"rest_api_id": &schema.Schema{
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"name": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  				ForceNew: true,
    33  			},
    34  
    35  			"description": &schema.Schema{
    36  				Type:     schema.TypeString,
    37  				Optional: true,
    38  			},
    39  
    40  			"schema": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Optional: true,
    43  			},
    44  
    45  			"content_type": &schema.Schema{
    46  				Type:     schema.TypeString,
    47  				Required: true,
    48  				ForceNew: true,
    49  			},
    50  		},
    51  	}
    52  }
    53  
    54  func resourceAwsApiGatewayModelCreate(d *schema.ResourceData, meta interface{}) error {
    55  	conn := meta.(*AWSClient).apigateway
    56  	log.Printf("[DEBUG] Creating API Gateway Model")
    57  
    58  	var description *string
    59  	if v, ok := d.GetOk("description"); ok {
    60  		description = aws.String(v.(string))
    61  	}
    62  	var schema *string
    63  	if v, ok := d.GetOk("schema"); ok {
    64  		schema = aws.String(v.(string))
    65  	}
    66  
    67  	var err error
    68  	model, err := conn.CreateModel(&apigateway.CreateModelInput{
    69  		Name:        aws.String(d.Get("name").(string)),
    70  		RestApiId:   aws.String(d.Get("rest_api_id").(string)),
    71  		ContentType: aws.String(d.Get("content_type").(string)),
    72  
    73  		Description: description,
    74  		Schema:      schema,
    75  	})
    76  
    77  	if err != nil {
    78  		return fmt.Errorf("Error creating API Gateway Model: %s", err)
    79  	}
    80  
    81  	d.SetId(*model.Id)
    82  
    83  	return nil
    84  }
    85  
    86  func resourceAwsApiGatewayModelRead(d *schema.ResourceData, meta interface{}) error {
    87  	conn := meta.(*AWSClient).apigateway
    88  
    89  	log.Printf("[DEBUG] Reading API Gateway Model %s", d.Id())
    90  	out, err := conn.GetModel(&apigateway.GetModelInput{
    91  		ModelName: aws.String(d.Get("name").(string)),
    92  		RestApiId: aws.String(d.Get("rest_api_id").(string)),
    93  	})
    94  	if err != nil {
    95  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
    96  			d.SetId("")
    97  			return nil
    98  		}
    99  		return err
   100  	}
   101  	log.Printf("[DEBUG] Received API Gateway Model: %s", out)
   102  	d.SetId(*out.Id)
   103  	d.Set("description", out.Description)
   104  	d.Set("schema", out.Schema)
   105  	d.Set("content_type", out.ContentType)
   106  
   107  	return nil
   108  }
   109  
   110  func resourceAwsApiGatewayModelUpdate(d *schema.ResourceData, meta interface{}) error {
   111  	conn := meta.(*AWSClient).apigateway
   112  
   113  	log.Printf("[DEBUG] Reading API Gateway Model %s", d.Id())
   114  	operations := make([]*apigateway.PatchOperation, 0)
   115  	if d.HasChange("description") {
   116  		operations = append(operations, &apigateway.PatchOperation{
   117  			Op:    aws.String("replace"),
   118  			Path:  aws.String("/description"),
   119  			Value: aws.String(d.Get("description").(string)),
   120  		})
   121  	}
   122  	if d.HasChange("schema") {
   123  		operations = append(operations, &apigateway.PatchOperation{
   124  			Op:    aws.String("replace"),
   125  			Path:  aws.String("/schema"),
   126  			Value: aws.String(d.Get("schema").(string)),
   127  		})
   128  	}
   129  
   130  	out, err := conn.UpdateModel(&apigateway.UpdateModelInput{
   131  		ModelName:       aws.String(d.Get("name").(string)),
   132  		RestApiId:       aws.String(d.Get("rest_api_id").(string)),
   133  		PatchOperations: operations,
   134  	})
   135  	if err != nil {
   136  		return err
   137  	}
   138  	log.Printf("[DEBUG] Received API Gateway Model: %s", out)
   139  
   140  	return resourceAwsApiGatewayModelRead(d, meta)
   141  }
   142  
   143  func resourceAwsApiGatewayModelDelete(d *schema.ResourceData, meta interface{}) error {
   144  	conn := meta.(*AWSClient).apigateway
   145  	log.Printf("[DEBUG] Deleting API Gateway Model: %s", d.Id())
   146  
   147  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   148  		log.Printf("[DEBUG] schema is %#v", d)
   149  		_, err := conn.DeleteModel(&apigateway.DeleteModelInput{
   150  			ModelName: aws.String(d.Get("name").(string)),
   151  			RestApiId: aws.String(d.Get("rest_api_id").(string)),
   152  		})
   153  		if err == nil {
   154  			return nil
   155  		}
   156  
   157  		apigatewayErr, ok := err.(awserr.Error)
   158  		if apigatewayErr.Code() == "NotFoundException" {
   159  			return nil
   160  		}
   161  
   162  		if !ok {
   163  			return resource.NonRetryableError(err)
   164  		}
   165  
   166  		return resource.NonRetryableError(err)
   167  	})
   168  }