/
resource_hcs_elb_l7rule.go
258 lines (214 loc) · 7.31 KB
/
resource_hcs_elb_l7rule.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
253
254
255
256
257
258
package elb
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/sdk/huaweicloud/openstack/elb/v3/l7policies"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/common"
"github.com/huaweicloud/terraform-provider-hcs/huaweicloudstack/config"
)
func ResourceL7RuleV3() *schema.Resource {
return &schema.Resource{
CreateContext: resourceL7RuleV3Create,
ReadContext: resourceL7RuleV3Read,
UpdateContext: resourceL7RuleV3Update,
DeleteContext: resourceL7RuleV3Delete,
Importer: &schema.ResourceImporter{
StateContext: resourceELBL7RuleImport,
},
Timeouts: &schema.ResourceTimeout{
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
"HOST_NAME", "PATH",
}, true),
},
"compare_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"STARTS_WITH", "EQUAL_TO", "REGEX",
}, true),
},
"l7policy_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"value": {
Type: schema.TypeString,
Required: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
if len(v.(string)) == 0 {
errors = append(errors, fmt.Errorf("'value' field should not be empty"))
}
return
},
},
},
}
}
func resourceL7RuleV3Create(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := config.GetHcsConfig(meta)
elbClient, err := cfg.ElbV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating ELB client: %s", err)
}
l7PolicyID := d.Get("l7policy_id").(string)
ruleType := d.Get("type").(string)
compareType := d.Get("compare_type").(string)
createOpts := l7policies.CreateRuleOpts{
RuleType: l7policies.RuleType(ruleType),
CompareType: l7policies.CompareType(compareType),
Value: d.Get("value").(string),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
l7Rule, err := l7policies.CreateRule(elbClient, l7PolicyID, createOpts).Extract()
if err != nil {
return diag.Errorf("error creating L7 Rule: %s", err)
}
timeout := d.Timeout(schema.TimeoutCreate)
// Wait for L7 Rule to become active before continuing
err = waitForElbV3Rule(ctx, elbClient, l7PolicyID, l7Rule.ID, "ACTIVE", timeout)
if err != nil {
return diag.FromErr(err)
}
d.SetId(l7Rule.ID)
return resourceL7RuleV3Read(ctx, d, meta)
}
func resourceL7RuleV3Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := config.GetHcsConfig(meta)
elbClient, err := cfg.ElbV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating ELB client: %s", err)
}
l7PolicyID := d.Get("l7policy_id").(string)
l7Rule, err := l7policies.GetRule(elbClient, l7PolicyID, d.Id()).Extract()
if err != nil {
return common.CheckDeletedDiag(d, err, "L7 Rule")
}
log.Printf("[DEBUG] Retrieved L7 Rule %s: %#v", d.Id(), l7Rule)
mErr := multierror.Append(nil,
d.Set("l7policy_id", l7PolicyID),
d.Set("type", l7Rule.RuleType),
d.Set("compare_type", l7Rule.CompareType),
d.Set("value", l7Rule.Value),
)
if err := mErr.ErrorOrNil(); err != nil {
return diag.Errorf("error setting Dedicated ELB l7rule fields: %s", err)
}
return nil
}
func resourceL7RuleV3Update(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := config.GetHcsConfig(meta)
elbClient, err := cfg.ElbV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating ELB client: %s", err)
}
l7PolicyID := d.Get("l7policy_id").(string)
var updateOpts l7policies.UpdateRuleOpts
if d.HasChange("compare_type") {
updateOpts.CompareType = l7policies.CompareType(d.Get("compare_type").(string))
}
if d.HasChange("value") {
updateOpts.Value = d.Get("value").(string)
}
log.Printf("[DEBUG] Updating L7 Rule %s with options: %#v", d.Id(), updateOpts)
_, err = l7policies.UpdateRule(elbClient, l7PolicyID, d.Id(), updateOpts).Extract()
if err != nil {
return diag.Errorf("unable to update L7 Rule %s: %s", d.Id(), err)
}
timeout := d.Timeout(schema.TimeoutUpdate)
// Wait for L7 Rule to become active before continuing
err = waitForElbV3Rule(ctx, elbClient, l7PolicyID, d.Id(), "ACTIVE", timeout)
if err != nil {
return diag.FromErr(err)
}
return resourceL7RuleV3Read(ctx, d, meta)
}
func resourceL7RuleV3Delete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := config.GetHcsConfig(meta)
elbClient, err := cfg.ElbV3Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating ELB client: %s", err)
}
l7PolicyID := d.Get("l7policy_id").(string)
log.Printf("[DEBUG] Attempting to delete L7 Rule %s", d.Id())
err = l7policies.DeleteRule(elbClient, l7PolicyID, d.Id()).ExtractErr()
if err != nil {
return common.CheckDeletedDiag(d, err, "error deleting L7 Rule")
}
timeout := d.Timeout(schema.TimeoutDelete)
err = waitForElbV3Rule(ctx, elbClient, l7PolicyID, d.Id(), "DELETED", timeout)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func waitForElbV3Rule(ctx context.Context, elbClient *golangsdk.ServiceClient, l7policyID string,
id string, target string, timeout time.Duration) error {
log.Printf("[DEBUG] Waiting for rule %s to become %s", id, target)
stateConf := &resource.StateChangeConf{
Target: []string{target},
Pending: nil,
Refresh: resourceElbV3RuleRefreshFunc(elbClient, l7policyID, id),
Timeout: timeout,
Delay: 5 * time.Second,
PollInterval: 3 * time.Second,
}
_, err := stateConf.WaitForStateContext(ctx)
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
switch target {
case "DELETED":
return nil
default:
return fmt.Errorf("error: rule %s not found: %s", id, err)
}
}
return fmt.Errorf("error waiting for rule %s to become %s: %s", id, target, err)
}
return nil
}
func resourceElbV3RuleRefreshFunc(elbClient *golangsdk.ServiceClient,
l7PolicyID string, id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
rule, err := l7policies.GetRule(elbClient, l7PolicyID, id).Extract()
if err != nil {
return nil, "", err
}
return rule, rule.ProvisioningStatus, nil
}
}
func resourceELBL7RuleImport(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) {
parts := strings.SplitN(d.Id(), "/", 2)
if len(parts) != 2 {
err := fmt.Errorf("invalid format specified for L7 Rule. Format must be <policy_id>/<rule_id>")
return nil, err
}
l7PolicyID := parts[0]
l7RuleID := parts[1]
d.SetId(l7RuleID)
d.Set("l7policy_id", l7PolicyID)
return []*schema.ResourceData{d}, nil
}