github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_api_gateway_deployment.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 resourceAwsApiGatewayDeployment() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsApiGatewayDeploymentCreate,
    18  		Read:   resourceAwsApiGatewayDeploymentRead,
    19  		Update: resourceAwsApiGatewayDeploymentUpdate,
    20  		Delete: resourceAwsApiGatewayDeploymentDelete,
    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  			"stage_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  			"stage_description": &schema.Schema{
    41  				Type:     schema.TypeString,
    42  				Optional: true,
    43  				ForceNew: true,
    44  			},
    45  
    46  			"variables": &schema.Schema{
    47  				Type:     schema.TypeMap,
    48  				Optional: true,
    49  				ForceNew: true,
    50  				Elem:     schema.TypeString,
    51  			},
    52  		},
    53  	}
    54  }
    55  
    56  func resourceAwsApiGatewayDeploymentCreate(d *schema.ResourceData, meta interface{}) error {
    57  	conn := meta.(*AWSClient).apigateway
    58  	// Create the gateway
    59  	log.Printf("[DEBUG] Creating API Gateway Deployment")
    60  
    61  	variables := make(map[string]string)
    62  	for k, v := range d.Get("variables").(map[string]interface{}) {
    63  		variables[k] = v.(string)
    64  	}
    65  
    66  	var err error
    67  	deployment, err := conn.CreateDeployment(&apigateway.CreateDeploymentInput{
    68  		RestApiId:        aws.String(d.Get("rest_api_id").(string)),
    69  		StageName:        aws.String(d.Get("stage_name").(string)),
    70  		Description:      aws.String(d.Get("description").(string)),
    71  		StageDescription: aws.String(d.Get("stage_description").(string)),
    72  		Variables:        aws.StringMap(variables),
    73  	})
    74  	if err != nil {
    75  		return fmt.Errorf("Error creating API Gateway Deployment: %s", err)
    76  	}
    77  
    78  	d.SetId(*deployment.Id)
    79  	log.Printf("[DEBUG] API Gateway Deployment ID: %s", d.Id())
    80  
    81  	return nil
    82  }
    83  
    84  func resourceAwsApiGatewayDeploymentRead(d *schema.ResourceData, meta interface{}) error {
    85  	conn := meta.(*AWSClient).apigateway
    86  
    87  	log.Printf("[DEBUG] Reading API Gateway Deployment %s", d.Id())
    88  	out, err := conn.GetDeployment(&apigateway.GetDeploymentInput{
    89  		RestApiId:    aws.String(d.Get("rest_api_id").(string)),
    90  		DeploymentId: aws.String(d.Id()),
    91  	})
    92  	if err != nil {
    93  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
    94  			d.SetId("")
    95  			return nil
    96  		}
    97  		return err
    98  	}
    99  	log.Printf("[DEBUG] Received API Gateway Deployment: %s", out)
   100  	d.SetId(*out.Id)
   101  	d.Set("description", out.Description)
   102  
   103  	return nil
   104  }
   105  
   106  func resourceAwsApiGatewayDeploymentUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
   107  	operations := make([]*apigateway.PatchOperation, 0)
   108  
   109  	if d.HasChange("description") {
   110  		operations = append(operations, &apigateway.PatchOperation{
   111  			Op:    aws.String("replace"),
   112  			Path:  aws.String("/description"),
   113  			Value: aws.String(d.Get("description").(string)),
   114  		})
   115  	}
   116  
   117  	return operations
   118  }
   119  
   120  func resourceAwsApiGatewayDeploymentUpdate(d *schema.ResourceData, meta interface{}) error {
   121  	conn := meta.(*AWSClient).apigateway
   122  
   123  	log.Printf("[DEBUG] Updating API Gateway API Key: %s", d.Id())
   124  
   125  	_, err := conn.UpdateDeployment(&apigateway.UpdateDeploymentInput{
   126  		DeploymentId:    aws.String(d.Id()),
   127  		RestApiId:       aws.String(d.Get("rest_api_id").(string)),
   128  		PatchOperations: resourceAwsApiGatewayDeploymentUpdateOperations(d),
   129  	})
   130  	if err != nil {
   131  		return err
   132  	}
   133  
   134  	return resourceAwsApiGatewayDeploymentRead(d, meta)
   135  }
   136  
   137  func resourceAwsApiGatewayDeploymentDelete(d *schema.ResourceData, meta interface{}) error {
   138  	conn := meta.(*AWSClient).apigateway
   139  	log.Printf("[DEBUG] Deleting API Gateway Deployment: %s", d.Id())
   140  
   141  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   142  		log.Printf("[DEBUG] schema is %#v", d)
   143  		if _, err := conn.DeleteStage(&apigateway.DeleteStageInput{
   144  			StageName: aws.String(d.Get("stage_name").(string)),
   145  			RestApiId: aws.String(d.Get("rest_api_id").(string)),
   146  		}); err == nil {
   147  			return nil
   148  		}
   149  
   150  		_, err := conn.DeleteDeployment(&apigateway.DeleteDeploymentInput{
   151  			DeploymentId: aws.String(d.Id()),
   152  			RestApiId:    aws.String(d.Get("rest_api_id").(string)),
   153  		})
   154  		if err == nil {
   155  			return nil
   156  		}
   157  
   158  		apigatewayErr, ok := err.(awserr.Error)
   159  		if apigatewayErr.Code() == "NotFoundException" {
   160  			return nil
   161  		}
   162  
   163  		if !ok {
   164  			return resource.NonRetryableError(err)
   165  		}
   166  
   167  		return resource.NonRetryableError(err)
   168  	})
   169  }