forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_aws_alb_listener_rule.go
252 lines (220 loc) · 6.96 KB
/
resource_aws_alb_listener_rule.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
251
252
package aws
import (
"errors"
"fmt"
"log"
"strconv"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/elbv2"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsAlbListenerRule() *schema.Resource {
return &schema.Resource{
Create: resourceAwsAlbListenerRuleCreate,
Read: resourceAwsAlbListenerRuleRead,
Update: resourceAwsAlbListenerRuleUpdate,
Delete: resourceAwsAlbListenerRuleDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"listener_arn": {
Type: schema.TypeString,
Required: true,
},
"priority": {
Type: schema.TypeInt,
Required: true,
ValidateFunc: validateAwsAlbListenerRulePriority,
},
"action": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"target_group_arn": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAwsAlbListenerActionType,
},
},
},
},
"condition": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"field": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateAwsListenerRuleField,
},
"values": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
},
},
},
},
}
}
func resourceAwsAlbListenerRuleCreate(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbv2conn
params := &elbv2.CreateRuleInput{
ListenerArn: aws.String(d.Get("listener_arn").(string)),
Priority: aws.Int64(int64(d.Get("priority").(int))),
}
actions := d.Get("action").([]interface{})
params.Actions = make([]*elbv2.Action, len(actions))
for i, action := range actions {
actionMap := action.(map[string]interface{})
params.Actions[i] = &elbv2.Action{
TargetGroupArn: aws.String(actionMap["target_group_arn"].(string)),
Type: aws.String(actionMap["type"].(string)),
}
}
conditions := d.Get("condition").([]interface{})
params.Conditions = make([]*elbv2.RuleCondition, len(conditions))
for i, condition := range conditions {
conditionMap := condition.(map[string]interface{})
values := conditionMap["values"].([]interface{})
params.Conditions[i] = &elbv2.RuleCondition{
Field: aws.String(conditionMap["field"].(string)),
Values: make([]*string, len(values)),
}
for j, value := range values {
params.Conditions[i].Values[j] = aws.String(value.(string))
}
}
resp, err := elbconn.CreateRule(params)
if err != nil {
return errwrap.Wrapf("Error creating ALB Listener Rule: {{err}}", err)
}
if len(resp.Rules) == 0 {
return errors.New("Error creating ALB Listener Rule: no rules returned in response")
}
d.SetId(*resp.Rules[0].RuleArn)
return resourceAwsAlbListenerRuleRead(d, meta)
}
func resourceAwsAlbListenerRuleRead(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbv2conn
resp, err := elbconn.DescribeRules(&elbv2.DescribeRulesInput{
RuleArns: []*string{aws.String(d.Id())},
})
if err != nil {
if isRuleNotFound(err) {
log.Printf("[WARN] DescribeRules - removing %s from state", d.Id())
d.SetId("")
return nil
}
return errwrap.Wrapf(fmt.Sprintf("Error retrieving Rules for listener %s: {{err}}", d.Id()), err)
}
if len(resp.Rules) != 1 {
return fmt.Errorf("Error retrieving Rule %q", d.Id())
}
rule := resp.Rules[0]
d.Set("arn", rule.RuleArn)
if priority, err := strconv.Atoi(*rule.Priority); err != nil {
return errwrap.Wrapf("Cannot convert rule priority %q to int: {{err}}", err)
} else {
d.Set("priority", priority)
}
actions := make([]interface{}, len(rule.Actions))
for i, action := range rule.Actions {
actionMap := make(map[string]interface{})
actionMap["target_group_arn"] = *action.TargetGroupArn
actionMap["type"] = *action.Type
actions[i] = actionMap
}
d.Set("action", actions)
conditions := make([]interface{}, len(rule.Conditions))
for i, condition := range rule.Conditions {
conditionMap := make(map[string]interface{})
conditionMap["field"] = *condition.Field
conditionValues := make([]string, len(condition.Values))
for k, value := range condition.Values {
conditionValues[k] = *value
}
conditionMap["values"] = conditionValues
conditions[i] = conditionMap
}
d.Set("condition", conditions)
return nil
}
func resourceAwsAlbListenerRuleUpdate(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbv2conn
params := &elbv2.ModifyRuleInput{
RuleArn: aws.String(d.Id()),
}
actions := d.Get("action").([]interface{})
params.Actions = make([]*elbv2.Action, len(actions))
for i, action := range actions {
actionMap := action.(map[string]interface{})
params.Actions[i] = &elbv2.Action{
TargetGroupArn: aws.String(actionMap["target_group_arn"].(string)),
Type: aws.String(actionMap["type"].(string)),
}
}
conditions := d.Get("condition").([]interface{})
params.Conditions = make([]*elbv2.RuleCondition, len(conditions))
for i, condition := range conditions {
conditionMap := condition.(map[string]interface{})
values := conditionMap["values"].([]interface{})
params.Conditions[i] = &elbv2.RuleCondition{
Field: aws.String(conditionMap["field"].(string)),
Values: make([]*string, len(values)),
}
for j, value := range values {
params.Conditions[i].Values[j] = aws.String(value.(string))
}
}
resp, err := elbconn.ModifyRule(params)
if err != nil {
return errwrap.Wrapf("Error modifying ALB Listener Rule: {{err}}", err)
}
if len(resp.Rules) == 0 {
return errors.New("Error modifying creating ALB Listener Rule: no rules returned in response")
}
return resourceAwsAlbListenerRuleRead(d, meta)
}
func resourceAwsAlbListenerRuleDelete(d *schema.ResourceData, meta interface{}) error {
elbconn := meta.(*AWSClient).elbv2conn
_, err := elbconn.DeleteRule(&elbv2.DeleteRuleInput{
RuleArn: aws.String(d.Id()),
})
if err != nil && !isRuleNotFound(err) {
return errwrap.Wrapf("Error deleting ALB Listener Rule: {{err}}", err)
}
return nil
}
func validateAwsAlbListenerRulePriority(v interface{}, k string) (ws []string, errors []error) {
value := v.(int)
if value < 1 || value > 99999 {
errors = append(errors, fmt.Errorf("%q must be in the range 1-99999", k))
}
return
}
func validateAwsListenerRuleField(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) > 64 {
errors = append(errors, fmt.Errorf("%q must be a maximum of 64 characters", k))
}
return
}
func isRuleNotFound(err error) bool {
elberr, ok := err.(awserr.Error)
return ok && elberr.Code() == "RuleNotFound"
}