github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/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": {
    24  				Type:     schema.TypeString,
    25  				Required: true,
    26  				ForceNew: true,
    27  			},
    28  
    29  			"stage_name": {
    30  				Type:     schema.TypeString,
    31  				Required: true,
    32  				ForceNew: true,
    33  			},
    34  
    35  			"description": {
    36  				Type:     schema.TypeString,
    37  				Optional: true,
    38  			},
    39  
    40  			"stage_description": {
    41  				Type:     schema.TypeString,
    42  				Optional: true,
    43  				ForceNew: true,
    44  			},
    45  
    46  			"variables": {
    47  				Type:     schema.TypeMap,
    48  				Optional: true,
    49  				ForceNew: true,
    50  				Elem:     schema.TypeString,
    51  			},
    52  
    53  			"created_date": {
    54  				Type:     schema.TypeString,
    55  				Computed: true,
    56  			},
    57  
    58  			"invoke_url": {
    59  				Type:     schema.TypeString,
    60  				Computed: true,
    61  			},
    62  
    63  			"execution_arn": {
    64  				Type:     schema.TypeString,
    65  				Computed: true,
    66  			},
    67  		},
    68  	}
    69  }
    70  
    71  func resourceAwsApiGatewayDeploymentCreate(d *schema.ResourceData, meta interface{}) error {
    72  	conn := meta.(*AWSClient).apigateway
    73  	// Create the gateway
    74  	log.Printf("[DEBUG] Creating API Gateway Deployment")
    75  
    76  	variables := make(map[string]string)
    77  	for k, v := range d.Get("variables").(map[string]interface{}) {
    78  		variables[k] = v.(string)
    79  	}
    80  
    81  	var err error
    82  	deployment, err := conn.CreateDeployment(&apigateway.CreateDeploymentInput{
    83  		RestApiId:        aws.String(d.Get("rest_api_id").(string)),
    84  		StageName:        aws.String(d.Get("stage_name").(string)),
    85  		Description:      aws.String(d.Get("description").(string)),
    86  		StageDescription: aws.String(d.Get("stage_description").(string)),
    87  		Variables:        aws.StringMap(variables),
    88  	})
    89  	if err != nil {
    90  		return fmt.Errorf("Error creating API Gateway Deployment: %s", err)
    91  	}
    92  
    93  	d.SetId(*deployment.Id)
    94  	log.Printf("[DEBUG] API Gateway Deployment ID: %s", d.Id())
    95  
    96  	return resourceAwsApiGatewayDeploymentRead(d, meta)
    97  }
    98  
    99  func resourceAwsApiGatewayDeploymentRead(d *schema.ResourceData, meta interface{}) error {
   100  	conn := meta.(*AWSClient).apigateway
   101  
   102  	log.Printf("[DEBUG] Reading API Gateway Deployment %s", d.Id())
   103  	restApiId := d.Get("rest_api_id").(string)
   104  	out, err := conn.GetDeployment(&apigateway.GetDeploymentInput{
   105  		RestApiId:    aws.String(restApiId),
   106  		DeploymentId: aws.String(d.Id()),
   107  	})
   108  	if err != nil {
   109  		if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" {
   110  			d.SetId("")
   111  			return nil
   112  		}
   113  		return err
   114  	}
   115  	log.Printf("[DEBUG] Received API Gateway Deployment: %s", out)
   116  	d.Set("description", out.Description)
   117  
   118  	region := meta.(*AWSClient).region
   119  	stageName := d.Get("stage_name").(string)
   120  
   121  	d.Set("invoke_url", buildApiGatewayInvokeURL(restApiId, region, stageName))
   122  
   123  	accountId := meta.(*AWSClient).accountid
   124  	arn, err := buildApiGatewayExecutionARN(restApiId, region, accountId)
   125  	if err != nil {
   126  		return err
   127  	}
   128  	d.Set("execution_arn", arn+"/"+stageName)
   129  
   130  	if err := d.Set("created_date", out.CreatedDate.Format(time.RFC3339)); err != nil {
   131  		log.Printf("[DEBUG] Error setting created_date: %s", err)
   132  	}
   133  
   134  	return nil
   135  }
   136  
   137  func resourceAwsApiGatewayDeploymentUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation {
   138  	operations := make([]*apigateway.PatchOperation, 0)
   139  
   140  	if d.HasChange("description") {
   141  		operations = append(operations, &apigateway.PatchOperation{
   142  			Op:    aws.String("replace"),
   143  			Path:  aws.String("/description"),
   144  			Value: aws.String(d.Get("description").(string)),
   145  		})
   146  	}
   147  
   148  	return operations
   149  }
   150  
   151  func resourceAwsApiGatewayDeploymentUpdate(d *schema.ResourceData, meta interface{}) error {
   152  	conn := meta.(*AWSClient).apigateway
   153  
   154  	log.Printf("[DEBUG] Updating API Gateway API Key: %s", d.Id())
   155  
   156  	_, err := conn.UpdateDeployment(&apigateway.UpdateDeploymentInput{
   157  		DeploymentId:    aws.String(d.Id()),
   158  		RestApiId:       aws.String(d.Get("rest_api_id").(string)),
   159  		PatchOperations: resourceAwsApiGatewayDeploymentUpdateOperations(d),
   160  	})
   161  	if err != nil {
   162  		return err
   163  	}
   164  
   165  	return resourceAwsApiGatewayDeploymentRead(d, meta)
   166  }
   167  
   168  func resourceAwsApiGatewayDeploymentDelete(d *schema.ResourceData, meta interface{}) error {
   169  	conn := meta.(*AWSClient).apigateway
   170  	log.Printf("[DEBUG] Deleting API Gateway Deployment: %s", d.Id())
   171  
   172  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   173  		log.Printf("[DEBUG] schema is %#v", d)
   174  		if _, err := conn.DeleteStage(&apigateway.DeleteStageInput{
   175  			StageName: aws.String(d.Get("stage_name").(string)),
   176  			RestApiId: aws.String(d.Get("rest_api_id").(string)),
   177  		}); err == nil {
   178  			return nil
   179  		}
   180  
   181  		_, err := conn.DeleteDeployment(&apigateway.DeleteDeploymentInput{
   182  			DeploymentId: aws.String(d.Id()),
   183  			RestApiId:    aws.String(d.Get("rest_api_id").(string)),
   184  		})
   185  		if err == nil {
   186  			return nil
   187  		}
   188  
   189  		apigatewayErr, ok := err.(awserr.Error)
   190  		if apigatewayErr.Code() == "NotFoundException" {
   191  			return nil
   192  		}
   193  
   194  		if !ok {
   195  			return resource.NonRetryableError(err)
   196  		}
   197  
   198  		return resource.NonRetryableError(err)
   199  	})
   200  }