github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_api_gateway_rest_api.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 resourceAwsApiGatewayRestApi() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceAwsApiGatewayRestApiCreate, 18 Read: resourceAwsApiGatewayRestApiRead, 19 Update: resourceAwsApiGatewayRestApiUpdate, 20 Delete: resourceAwsApiGatewayRestApiDelete, 21 22 Schema: map[string]*schema.Schema{ 23 "name": { 24 Type: schema.TypeString, 25 Required: true, 26 }, 27 28 "description": { 29 Type: schema.TypeString, 30 Optional: true, 31 }, 32 33 "binary_media_types": { 34 Type: schema.TypeList, 35 Optional: true, 36 ForceNew: true, 37 Elem: &schema.Schema{Type: schema.TypeString}, 38 }, 39 40 "root_resource_id": { 41 Type: schema.TypeString, 42 Computed: true, 43 }, 44 45 "created_date": { 46 Type: schema.TypeString, 47 Computed: true, 48 }, 49 }, 50 } 51 } 52 53 func resourceAwsApiGatewayRestApiCreate(d *schema.ResourceData, meta interface{}) error { 54 conn := meta.(*AWSClient).apigateway 55 log.Printf("[DEBUG] Creating API Gateway") 56 57 var description *string 58 if d.Get("description").(string) != "" { 59 description = aws.String(d.Get("description").(string)) 60 } 61 62 params := &apigateway.CreateRestApiInput{ 63 Name: aws.String(d.Get("name").(string)), 64 Description: description, 65 } 66 67 binaryMediaTypes, binaryMediaTypesOk := d.GetOk("binary_media_types") 68 if binaryMediaTypesOk { 69 params.BinaryMediaTypes = expandStringList(binaryMediaTypes.([]interface{})) 70 } 71 72 gateway, err := conn.CreateRestApi(params) 73 if err != nil { 74 return fmt.Errorf("Error creating API Gateway: %s", err) 75 } 76 77 d.SetId(*gateway.Id) 78 79 if err = resourceAwsApiGatewayRestApiRefreshResources(d, meta); err != nil { 80 return err 81 } 82 83 return resourceAwsApiGatewayRestApiRead(d, meta) 84 } 85 86 func resourceAwsApiGatewayRestApiRefreshResources(d *schema.ResourceData, meta interface{}) error { 87 conn := meta.(*AWSClient).apigateway 88 89 resp, err := conn.GetResources(&apigateway.GetResourcesInput{ 90 RestApiId: aws.String(d.Id()), 91 }) 92 if err != nil { 93 return err 94 } 95 96 for _, item := range resp.Items { 97 if *item.Path == "/" { 98 d.Set("root_resource_id", item.Id) 99 break 100 } 101 } 102 103 return nil 104 } 105 106 func resourceAwsApiGatewayRestApiRead(d *schema.ResourceData, meta interface{}) error { 107 conn := meta.(*AWSClient).apigateway 108 log.Printf("[DEBUG] Reading API Gateway %s", d.Id()) 109 110 api, err := conn.GetRestApi(&apigateway.GetRestApiInput{ 111 RestApiId: aws.String(d.Id()), 112 }) 113 if err != nil { 114 if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { 115 d.SetId("") 116 return nil 117 } 118 return err 119 } 120 121 d.Set("name", api.Name) 122 d.Set("description", api.Description) 123 d.Set("binary_media_types", api.BinaryMediaTypes) 124 125 if err := d.Set("created_date", api.CreatedDate.Format(time.RFC3339)); err != nil { 126 log.Printf("[DEBUG] Error setting created_date: %s", err) 127 } 128 129 return nil 130 } 131 132 func resourceAwsApiGatewayRestApiUpdateOperations(d *schema.ResourceData) []*apigateway.PatchOperation { 133 operations := make([]*apigateway.PatchOperation, 0) 134 135 if d.HasChange("name") { 136 operations = append(operations, &apigateway.PatchOperation{ 137 Op: aws.String("replace"), 138 Path: aws.String("/name"), 139 Value: aws.String(d.Get("name").(string)), 140 }) 141 } 142 143 if d.HasChange("description") { 144 operations = append(operations, &apigateway.PatchOperation{ 145 Op: aws.String("replace"), 146 Path: aws.String("/description"), 147 Value: aws.String(d.Get("description").(string)), 148 }) 149 } 150 151 return operations 152 } 153 154 func resourceAwsApiGatewayRestApiUpdate(d *schema.ResourceData, meta interface{}) error { 155 conn := meta.(*AWSClient).apigateway 156 log.Printf("[DEBUG] Updating API Gateway %s", d.Id()) 157 158 _, err := conn.UpdateRestApi(&apigateway.UpdateRestApiInput{ 159 RestApiId: aws.String(d.Id()), 160 PatchOperations: resourceAwsApiGatewayRestApiUpdateOperations(d), 161 }) 162 163 if err != nil { 164 return err 165 } 166 log.Printf("[DEBUG] Updated API Gateway %s", d.Id()) 167 168 return resourceAwsApiGatewayRestApiRead(d, meta) 169 } 170 171 func resourceAwsApiGatewayRestApiDelete(d *schema.ResourceData, meta interface{}) error { 172 conn := meta.(*AWSClient).apigateway 173 log.Printf("[DEBUG] Deleting API Gateway: %s", d.Id()) 174 175 return resource.Retry(10*time.Minute, func() *resource.RetryError { 176 _, err := conn.DeleteRestApi(&apigateway.DeleteRestApiInput{ 177 RestApiId: aws.String(d.Id()), 178 }) 179 if err == nil { 180 return nil 181 } 182 183 if apigatewayErr, ok := err.(awserr.Error); ok && apigatewayErr.Code() == "NotFoundException" { 184 return nil 185 } 186 187 return resource.NonRetryableError(err) 188 }) 189 }