github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 } 59 } 60 61 func resourceAwsApiGatewayDeploymentCreate(d *schema.ResourceData, meta interface{}) error { 62 conn := meta.(*AWSClient).apigateway 63 // Create the gateway 64 log.Printf("[DEBUG] Creating API Gateway Deployment") 65 66 variables := make(map[string]string) 67 for k, v := range d.Get("variables").(map[string]interface{}) { 68 variables[k] = v.(string) 69 } 70 71 var err error 72 deployment, err := conn.CreateDeployment(&apigateway.CreateDeploymentInput{ 73 RestApiId: aws.String(d.Get("rest_api_id").(string)), 74 StageName: aws.String(d.Get("stage_name").(string)), 75 Description: aws.String(d.Get("description").(string)), 76 StageDescription: aws.String(d.Get("stage_description").(string)), 77 Variables: aws.StringMap(variables), 78 }) 79 if err != nil { 80 return fmt.Errorf("Error creating API Gateway Deployment: %s", err) 81 } 82 83 d.SetId(*deployment.Id) 84 log.Printf("[DEBUG] API Gateway Deployment ID: %s", d.Id()) 85 86 return resourceAwsApiGatewayDeploymentRead(d, meta) 87 } 88 89 func resourceAwsApiGatewayDeploymentRead(d *schema.ResourceData, meta interface{}) error { 90 conn := meta.(*AWSClient).apigateway 91 92 log.Printf("[DEBUG] Reading API Gateway Deployment %s", d.Id()) 93 out, err := conn.GetDeployment(&apigateway.GetDeploymentInput{ 94 RestApiId: aws.String(d.Get("rest_api_id").(string)), 95 DeploymentId: aws.String(d.Id()), 96 }) 97 if err != nil { 98 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { 99 d.SetId("") 100 return nil 101 } 102 return err 103 } 104 log.Printf("[DEBUG] Received API Gateway Deployment: %s", out) 105 d.Set("description", out.Description) 106 107 if err := d.Set("created_date", out.CreatedDate.Format(time.RFC3339)); err != nil { 108 log.Printf("[DEBUG] Error setting created_date: %s", err) 109 } 110 111 return nil 112 } 113 114 func resourceAwsApiGatewayDeploymentUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation { 115 operations := make([]*apigateway.PatchOperation, 0) 116 117 if d.HasChange("description") { 118 operations = append(operations, &apigateway.PatchOperation{ 119 Op: aws.String("replace"), 120 Path: aws.String("/description"), 121 Value: aws.String(d.Get("description").(string)), 122 }) 123 } 124 125 return operations 126 } 127 128 func resourceAwsApiGatewayDeploymentUpdate(d *schema.ResourceData, meta interface{}) error { 129 conn := meta.(*AWSClient).apigateway 130 131 log.Printf("[DEBUG] Updating API Gateway API Key: %s", d.Id()) 132 133 _, err := conn.UpdateDeployment(&apigateway.UpdateDeploymentInput{ 134 DeploymentId: aws.String(d.Id()), 135 RestApiId: aws.String(d.Get("rest_api_id").(string)), 136 PatchOperations: resourceAwsApiGatewayDeploymentUpdateOperations(d), 137 }) 138 if err != nil { 139 return err 140 } 141 142 return resourceAwsApiGatewayDeploymentRead(d, meta) 143 } 144 145 func resourceAwsApiGatewayDeploymentDelete(d *schema.ResourceData, meta interface{}) error { 146 conn := meta.(*AWSClient).apigateway 147 log.Printf("[DEBUG] Deleting API Gateway Deployment: %s", d.Id()) 148 149 return resource.Retry(5*time.Minute, func() *resource.RetryError { 150 log.Printf("[DEBUG] schema is %#v", d) 151 if _, err := conn.DeleteStage(&apigateway.DeleteStageInput{ 152 StageName: aws.String(d.Get("stage_name").(string)), 153 RestApiId: aws.String(d.Get("rest_api_id").(string)), 154 }); err == nil { 155 return nil 156 } 157 158 _, err := conn.DeleteDeployment(&apigateway.DeleteDeploymentInput{ 159 DeploymentId: aws.String(d.Id()), 160 RestApiId: aws.String(d.Get("rest_api_id").(string)), 161 }) 162 if err == nil { 163 return nil 164 } 165 166 apigatewayErr, ok := err.(awserr.Error) 167 if apigatewayErr.Code() == "NotFoundException" { 168 return nil 169 } 170 171 if !ok { 172 return resource.NonRetryableError(err) 173 } 174 175 return resource.NonRetryableError(err) 176 }) 177 }