This repository has been archived by the owner on Feb 19, 2020. It is now read-only.
forked from hashicorp/terraform
/
resource_aws_api_gateway_base_path_mapping.go
128 lines (104 loc) · 3.29 KB
/
resource_aws_api_gateway_base_path_mapping.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/apigateway"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsApiGatewayBasePathMapping() *schema.Resource {
return &schema.Resource{
Create: resourceAwsApiGatewayBasePathMappingCreate,
Read: resourceAwsApiGatewayBasePathMappingRead,
Delete: resourceAwsApiGatewayBasePathMappingDelete,
Schema: map[string]*schema.Schema{
"api_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"base_path": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"stage_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"domain_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceAwsApiGatewayBasePathMappingCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
err := resource.Retry(30*time.Second, func() *resource.RetryError {
_, err := conn.CreateBasePathMapping(&apigateway.CreateBasePathMappingInput{
RestApiId: aws.String(d.Get("api_id").(string)),
DomainName: aws.String(d.Get("domain_name").(string)),
BasePath: aws.String(d.Get("base_path").(string)),
Stage: aws.String(d.Get("stage_name").(string)),
})
if err != nil {
if err, ok := err.(awserr.Error); ok && err.Code() != "BadRequestException" {
return resource.NonRetryableError(err)
}
return resource.RetryableError(
fmt.Errorf("Error creating Gateway base path mapping: %s", err),
)
}
return nil
})
if err != nil {
return fmt.Errorf("Error creating Gateway base path mapping: %s", err)
}
id := fmt.Sprintf("%s/%s", d.Get("domain_name").(string), d.Get("base_path").(string))
d.SetId(id)
return resourceAwsApiGatewayBasePathMappingRead(d, meta)
}
func resourceAwsApiGatewayBasePathMappingRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
domainName := d.Get("domain_name").(string)
basePath := d.Get("base_path").(string)
if domainName == "" {
return nil
}
mapping, err := conn.GetBasePathMapping(&apigateway.GetBasePathMappingInput{
DomainName: aws.String(domainName),
BasePath: aws.String(basePath),
})
if err != nil {
if err, ok := err.(awserr.Error); ok && err.Code() == "NotFoundException" {
log.Printf("[WARN] API gateway base path mapping %s has vanished\n", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Gateway base path mapping: %s", err)
}
d.Set("base_path", mapping.BasePath)
d.Set("api_id", mapping.RestApiId)
d.Set("stage_name", mapping.Stage)
return nil
}
func resourceAwsApiGatewayBasePathMappingDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).apigateway
_, err := conn.DeleteBasePathMapping(&apigateway.DeleteBasePathMappingInput{
DomainName: aws.String(d.Get("domain_name").(string)),
BasePath: aws.String(d.Get("base_path").(string)),
})
if err != nil {
if err, ok := err.(awserr.Error); ok && err.Code() == "NotFoundException" {
return nil
}
return err
}
return nil
}