|
| 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 resourceAwsApiGatewayIntegration() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Create: resourceAwsApiGatewayIntegrationCreate, |
| 18 | + Read: resourceAwsApiGatewayIntegrationRead, |
| 19 | + Update: resourceAwsApiGatewayIntegrationUpdate, |
| 20 | + Delete: resourceAwsApiGatewayIntegrationDelete, |
| 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 | + "resource_id": &schema.Schema{ |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + ForceNew: true, |
| 33 | + }, |
| 34 | + |
| 35 | + "http_method": &schema.Schema{ |
| 36 | + Type: schema.TypeString, |
| 37 | + Required: true, |
| 38 | + ForceNew: true, |
| 39 | + ValidateFunc: validateHTTPMethod, |
| 40 | + }, |
| 41 | + |
| 42 | + "type": &schema.Schema{ |
| 43 | + Type: schema.TypeString, |
| 44 | + Required: true, |
| 45 | + ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { |
| 46 | + value := v.(string) |
| 47 | + if value != "MOCK" && value != "AWS" && value != "HTTP" { |
| 48 | + errors = append(errors, fmt.Errorf( |
| 49 | + "%q must be one of 'AWS', 'MOCK', 'HTTP'", k)) |
| 50 | + } |
| 51 | + return |
| 52 | + }, |
| 53 | + }, |
| 54 | + |
| 55 | + "uri": &schema.Schema{ |
| 56 | + Type: schema.TypeString, |
| 57 | + Optional: true, |
| 58 | + }, |
| 59 | + |
| 60 | + "credentials": &schema.Schema{ |
| 61 | + Type: schema.TypeString, |
| 62 | + Optional: true, |
| 63 | + }, |
| 64 | + |
| 65 | + "integration_http_method": &schema.Schema{ |
| 66 | + Type: schema.TypeString, |
| 67 | + Optional: true, |
| 68 | + ValidateFunc: validateHTTPMethod, |
| 69 | + }, |
| 70 | + |
| 71 | + "request_templates": &schema.Schema{ |
| 72 | + Type: schema.TypeMap, |
| 73 | + Optional: true, |
| 74 | + Elem: schema.TypeString, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func resourceAwsApiGatewayIntegrationCreate(d *schema.ResourceData, meta interface{}) error { |
| 81 | + conn := meta.(*AWSClient).apigateway |
| 82 | + |
| 83 | + var integrationHttpMethod *string |
| 84 | + if v, ok := d.GetOk("integration_http_method"); ok { |
| 85 | + integrationHttpMethod = aws.String(v.(string)) |
| 86 | + } |
| 87 | + var uri *string |
| 88 | + if v, ok := d.GetOk("uri"); ok { |
| 89 | + uri = aws.String(v.(string)) |
| 90 | + } |
| 91 | + templates := make(map[string]string) |
| 92 | + for k, v := range d.Get("request_templates").(map[string]interface{}) { |
| 93 | + templates[k] = v.(string) |
| 94 | + } |
| 95 | + |
| 96 | + var credentials *string |
| 97 | + if val, ok := d.GetOk("credentials"); ok { |
| 98 | + credentials = aws.String(val.(string)) |
| 99 | + } |
| 100 | + |
| 101 | + _, err := conn.PutIntegration(&apigateway.PutIntegrationInput{ |
| 102 | + HttpMethod: aws.String(d.Get("http_method").(string)), |
| 103 | + ResourceId: aws.String(d.Get("resource_id").(string)), |
| 104 | + RestApiId: aws.String(d.Get("rest_api_id").(string)), |
| 105 | + Type: aws.String(d.Get("type").(string)), |
| 106 | + IntegrationHttpMethod: integrationHttpMethod, |
| 107 | + Uri: uri, |
| 108 | + // TODO implement once [GH-2143](https://github.com/hashicorp/terraform/issues/2143) has been implemented |
| 109 | + RequestParameters: nil, |
| 110 | + RequestTemplates: aws.StringMap(templates), |
| 111 | + Credentials: credentials, |
| 112 | + CacheNamespace: nil, |
| 113 | + CacheKeyParameters: nil, |
| 114 | + }) |
| 115 | + if err != nil { |
| 116 | + return fmt.Errorf("Error creating API Gateway Integration: %s", err) |
| 117 | + } |
| 118 | + |
| 119 | + d.SetId(fmt.Sprintf("agi-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string))) |
| 120 | + |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +func resourceAwsApiGatewayIntegrationRead(d *schema.ResourceData, meta interface{}) error { |
| 125 | + conn := meta.(*AWSClient).apigateway |
| 126 | + |
| 127 | + log.Printf("[DEBUG] Reading API Gateway Integration %s", d.Id()) |
| 128 | + integration, err := conn.GetIntegration(&apigateway.GetIntegrationInput{ |
| 129 | + HttpMethod: aws.String(d.Get("http_method").(string)), |
| 130 | + ResourceId: aws.String(d.Get("resource_id").(string)), |
| 131 | + RestApiId: aws.String(d.Get("rest_api_id").(string)), |
| 132 | + }) |
| 133 | + if err != nil { |
| 134 | + if awsErr, ok := err.(awserr.Error); ok && awsErr.Code() == "NotFoundException" { |
| 135 | + d.SetId("") |
| 136 | + return nil |
| 137 | + } |
| 138 | + return err |
| 139 | + } |
| 140 | + log.Printf("[DEBUG] Received API Gateway Integration: %s", integration) |
| 141 | + d.SetId(fmt.Sprintf("agi-%s-%s-%s", d.Get("rest_api_id").(string), d.Get("resource_id").(string), d.Get("http_method").(string))) |
| 142 | + |
| 143 | + // AWS converts "" to null on their side, convert it back |
| 144 | + if v, ok := integration.RequestTemplates["application/json"]; ok && v == nil { |
| 145 | + integration.RequestTemplates["application/json"] = aws.String("") |
| 146 | + } |
| 147 | + |
| 148 | + d.Set("request_templates", aws.StringValueMap(integration.RequestTemplates)) |
| 149 | + d.Set("credentials", integration.Credentials) |
| 150 | + d.Set("type", integration.Type) |
| 151 | + d.Set("uri", integration.Uri) |
| 152 | + |
| 153 | + return nil |
| 154 | +} |
| 155 | + |
| 156 | +func resourceAwsApiGatewayIntegrationUpdate(d *schema.ResourceData, meta interface{}) error { |
| 157 | + return resourceAwsApiGatewayIntegrationCreate(d, meta) |
| 158 | +} |
| 159 | + |
| 160 | +func resourceAwsApiGatewayIntegrationDelete(d *schema.ResourceData, meta interface{}) error { |
| 161 | + conn := meta.(*AWSClient).apigateway |
| 162 | + log.Printf("[DEBUG] Deleting API Gateway Integration: %s", d.Id()) |
| 163 | + |
| 164 | + resourceId := d.Get("resource_id").(string) |
| 165 | + if o, n := d.GetChange("resource_id"); o.(string) != n.(string) { |
| 166 | + resourceId = o.(string) |
| 167 | + } |
| 168 | + httpMethod := d.Get("http_method").(string) |
| 169 | + if o, n := d.GetChange("http_method"); o.(string) != n.(string) { |
| 170 | + httpMethod = o.(string) |
| 171 | + } |
| 172 | + restApiID := d.Get("rest_api_id").(string) |
| 173 | + if o, n := d.GetChange("rest_api_id"); o.(string) != n.(string) { |
| 174 | + restApiID = o.(string) |
| 175 | + } |
| 176 | + |
| 177 | + return resource.Retry(5*time.Minute, func() error { |
| 178 | + log.Printf("[DEBUG] schema is %#v", d) |
| 179 | + _, err := conn.DeleteIntegration(&apigateway.DeleteIntegrationInput{ |
| 180 | + HttpMethod: aws.String(httpMethod), |
| 181 | + ResourceId: aws.String(resourceId), |
| 182 | + RestApiId: aws.String(restApiID), |
| 183 | + }) |
| 184 | + if err == nil { |
| 185 | + return nil |
| 186 | + } |
| 187 | + |
| 188 | + apigatewayErr, ok := err.(awserr.Error) |
| 189 | + if apigatewayErr.Code() == "NotFoundException" { |
| 190 | + return nil |
| 191 | + } |
| 192 | + |
| 193 | + if !ok { |
| 194 | + return resource.RetryError{Err: err} |
| 195 | + } |
| 196 | + |
| 197 | + return resource.RetryError{Err: err} |
| 198 | + }) |
| 199 | +} |
0 commit comments