forked from hashicorp/terraform-provider-aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_aws_appmesh_virtual_router.go
250 lines (215 loc) · 7.31 KB
/
resource_aws_appmesh_virtual_router.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package aws
import (
"bytes"
"fmt"
"log"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/appmesh"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceAwsAppmeshVirtualRouter() *schema.Resource {
return &schema.Resource{
Create: resourceAwsAppmeshVirtualRouterCreate,
Read: resourceAwsAppmeshVirtualRouterRead,
Update: resourceAwsAppmeshVirtualRouterUpdate,
Delete: resourceAwsAppmeshVirtualRouterDelete,
Importer: &schema.ResourceImporter{
State: resourceAwsAppmeshVirtualRouterImport,
},
SchemaVersion: 1,
MigrateState: resourceAwsAppmeshVirtualRouterMigrateState,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
"mesh_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(1, 255),
},
"spec": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"service_names": {
Type: schema.TypeSet,
Removed: "Use `aws_appmesh_virtual_service` resources instead",
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"listener": {
Type: schema.TypeSet,
Required: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"port_mapping": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"port": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validation.IntBetween(1, 65535),
},
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
appmesh.PortProtocolHttp,
appmesh.PortProtocolTcp,
}, false),
},
},
},
},
},
},
Set: appmeshVirtualRouterListenerHash,
},
},
},
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"created_date": {
Type: schema.TypeString,
Computed: true,
},
"last_updated_date": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsAppmeshVirtualRouterCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appmeshconn
req := &appmesh.CreateVirtualRouterInput{
MeshName: aws.String(d.Get("mesh_name").(string)),
VirtualRouterName: aws.String(d.Get("name").(string)),
Spec: expandAppmeshVirtualRouterSpec(d.Get("spec").([]interface{})),
}
log.Printf("[DEBUG] Creating App Mesh virtual router: %#v", req)
resp, err := conn.CreateVirtualRouter(req)
if err != nil {
return fmt.Errorf("error creating App Mesh virtual router: %s", err)
}
d.SetId(aws.StringValue(resp.VirtualRouter.Metadata.Uid))
return resourceAwsAppmeshVirtualRouterRead(d, meta)
}
func resourceAwsAppmeshVirtualRouterRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appmeshconn
resp, err := conn.DescribeVirtualRouter(&appmesh.DescribeVirtualRouterInput{
MeshName: aws.String(d.Get("mesh_name").(string)),
VirtualRouterName: aws.String(d.Get("name").(string)),
})
if isAWSErr(err, appmesh.ErrCodeNotFoundException, "") {
log.Printf("[WARN] App Mesh virtual router (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("error reading App Mesh virtual router: %s", err)
}
if aws.StringValue(resp.VirtualRouter.Status.Status) == appmesh.VirtualRouterStatusCodeDeleted {
log.Printf("[WARN] App Mesh virtual router (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
d.Set("name", resp.VirtualRouter.VirtualRouterName)
d.Set("mesh_name", resp.VirtualRouter.MeshName)
d.Set("arn", resp.VirtualRouter.Metadata.Arn)
d.Set("created_date", resp.VirtualRouter.Metadata.CreatedAt.Format(time.RFC3339))
d.Set("last_updated_date", resp.VirtualRouter.Metadata.LastUpdatedAt.Format(time.RFC3339))
err = d.Set("spec", flattenAppmeshVirtualRouterSpec(resp.VirtualRouter.Spec))
if err != nil {
return fmt.Errorf("error setting spec: %s", err)
}
return nil
}
func resourceAwsAppmeshVirtualRouterUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appmeshconn
if d.HasChange("spec") {
_, v := d.GetChange("spec")
req := &appmesh.UpdateVirtualRouterInput{
MeshName: aws.String(d.Get("mesh_name").(string)),
VirtualRouterName: aws.String(d.Get("name").(string)),
Spec: expandAppmeshVirtualRouterSpec(v.([]interface{})),
}
log.Printf("[DEBUG] Updating App Mesh virtual router: %#v", req)
_, err := conn.UpdateVirtualRouter(req)
if err != nil {
return fmt.Errorf("error updating App Mesh virtual router: %s", err)
}
}
return resourceAwsAppmeshVirtualRouterRead(d, meta)
}
func resourceAwsAppmeshVirtualRouterDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appmeshconn
log.Printf("[DEBUG] Deleting App Mesh virtual router: %s", d.Id())
_, err := conn.DeleteVirtualRouter(&appmesh.DeleteVirtualRouterInput{
MeshName: aws.String(d.Get("mesh_name").(string)),
VirtualRouterName: aws.String(d.Get("name").(string)),
})
if isAWSErr(err, appmesh.ErrCodeNotFoundException, "") {
return nil
}
if err != nil {
return fmt.Errorf("error deleting App Mesh virtual router: %s", err)
}
return nil
}
func resourceAwsAppmeshVirtualRouterImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return []*schema.ResourceData{}, fmt.Errorf("Wrong format of resource: %s. Please follow 'mesh-name/virtual-router-name'", d.Id())
}
mesh := parts[0]
name := parts[1]
log.Printf("[DEBUG] Importing App Mesh virtual router %s from mesh %s", name, mesh)
conn := meta.(*AWSClient).appmeshconn
resp, err := conn.DescribeVirtualRouter(&appmesh.DescribeVirtualRouterInput{
MeshName: aws.String(mesh),
VirtualRouterName: aws.String(name),
})
if err != nil {
return nil, err
}
d.SetId(aws.StringValue(resp.VirtualRouter.Metadata.Uid))
d.Set("name", resp.VirtualRouter.VirtualRouterName)
d.Set("mesh_name", resp.VirtualRouter.MeshName)
return []*schema.ResourceData{d}, nil
}
func appmeshVirtualRouterListenerHash(vListener interface{}) int {
var buf bytes.Buffer
mListener := vListener.(map[string]interface{})
if vPortMapping, ok := mListener["port_mapping"].([]interface{}); ok && len(vPortMapping) > 0 && vPortMapping[0] != nil {
mPortMapping := vPortMapping[0].(map[string]interface{})
if v, ok := mPortMapping["port"].(int); ok {
buf.WriteString(fmt.Sprintf("%d-", v))
}
if v, ok := mPortMapping["protocol"].(string); ok {
buf.WriteString(fmt.Sprintf("%s-", v))
}
}
return hashcode.String(buf.String())
}