forked from openshift/terraform-provider-aws
/
resource_aws_appmesh_virtual_node.go
237 lines (209 loc) · 6.68 KB
/
resource_aws_appmesh_virtual_node.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
package aws
import (
"bytes"
"fmt"
"log"
"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 resourceAwsAppmeshVirtualNode() *schema.Resource {
return &schema.Resource{
Create: resourceAwsAppmeshVirtualNodeCreate,
Read: resourceAwsAppmeshVirtualNodeRead,
Update: resourceAwsAppmeshVirtualNodeUpdate,
Delete: resourceAwsAppmeshVirtualNodeDelete,
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{
"backends": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"listener": {
Type: schema.TypeSet,
Optional: true,
MinItems: 0,
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: appmeshVirtualNodeListenerHash,
},
"service_discovery": {
Type: schema.TypeList,
Optional: true,
MinItems: 0,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"dns": {
Type: schema.TypeList,
Required: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.NoZeroValues,
},
},
},
},
},
},
},
},
},
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"created_date": {
Type: schema.TypeString,
Computed: true,
},
"last_updated_date": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsAppmeshVirtualNodeCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appmeshconn
req := &appmesh.CreateVirtualNodeInput{
MeshName: aws.String(d.Get("mesh_name").(string)),
VirtualNodeName: aws.String(d.Get("name").(string)),
Spec: expandAppmeshVirtualNodeSpec(d.Get("spec").([]interface{})),
}
log.Printf("[DEBUG] Creating App Mesh virtual node: %#v", req)
resp, err := conn.CreateVirtualNode(req)
if err != nil {
return fmt.Errorf("error creating App Mesh virtual node: %s", err)
}
d.SetId(aws.StringValue(resp.VirtualNode.Metadata.Uid))
return resourceAwsAppmeshVirtualNodeRead(d, meta)
}
func resourceAwsAppmeshVirtualNodeRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appmeshconn
resp, err := conn.DescribeVirtualNode(&appmesh.DescribeVirtualNodeInput{
MeshName: aws.String(d.Get("mesh_name").(string)),
VirtualNodeName: aws.String(d.Get("name").(string)),
})
if err != nil {
if isAWSErr(err, appmesh.ErrCodeNotFoundException, "") {
log.Printf("[WARN] App Mesh virtual node (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("error reading App Mesh virtual node: %s", err)
}
if aws.StringValue(resp.VirtualNode.Status.Status) == appmesh.VirtualNodeStatusCodeDeleted {
log.Printf("[WARN] App Mesh virtual node (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}
d.Set("name", resp.VirtualNode.VirtualNodeName)
d.Set("mesh_name", resp.VirtualNode.MeshName)
d.Set("arn", resp.VirtualNode.Metadata.Arn)
d.Set("created_date", resp.VirtualNode.Metadata.CreatedAt.Format(time.RFC3339))
d.Set("last_updated_date", resp.VirtualNode.Metadata.LastUpdatedAt.Format(time.RFC3339))
err1 := d.Set("spec", flattenAppmeshVirtualNodeSpec(resp.VirtualNode.Spec))
return err1
}
func resourceAwsAppmeshVirtualNodeUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appmeshconn
if d.HasChange("spec") {
_, v := d.GetChange("spec")
req := &appmesh.UpdateVirtualNodeInput{
MeshName: aws.String(d.Get("mesh_name").(string)),
VirtualNodeName: aws.String(d.Get("name").(string)),
Spec: expandAppmeshVirtualNodeSpec(v.([]interface{})),
}
log.Printf("[DEBUG] Updating App Mesh virtual node: %#v", req)
_, err := conn.UpdateVirtualNode(req)
if err != nil {
return fmt.Errorf("error updating App Mesh virtual node: %s", err)
}
}
return resourceAwsAppmeshVirtualNodeRead(d, meta)
}
func resourceAwsAppmeshVirtualNodeDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).appmeshconn
log.Printf("[DEBUG] Deleting App Mesh virtual node: %s", d.Id())
_, err := conn.DeleteVirtualNode(&appmesh.DeleteVirtualNodeInput{
MeshName: aws.String(d.Get("mesh_name").(string)),
VirtualNodeName: aws.String(d.Get("name").(string)),
})
if err != nil {
if isAWSErr(err, appmesh.ErrCodeNotFoundException, "") {
return nil
}
return fmt.Errorf("error deleting App Mesh virtual node: %s", err)
}
return nil
}
func appmeshVirtualNodeListenerHash(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())
}