/
resource_huaweicloud_as_lifecycle_hook.go
243 lines (218 loc) · 7.61 KB
/
resource_huaweicloud_as_lifecycle_hook.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
package as
import (
"context"
"fmt"
"log"
"regexp"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/chnsz/golangsdk/openstack/autoscaling/v1/lifecyclehooks"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
)
var hookTypeMap = map[string]string{
"ADD": "INSTANCE_LAUNCHING",
"REMOVE": "INSTANCE_TERMINATING",
}
// @API AS DELETE /autoscaling-api/v1/{project_id}/scaling_lifecycle_hook/{groupID}/{hookName}
// @API AS GET /autoscaling-api/v1/{project_id}/scaling_lifecycle_hook/{groupID}/{hookName}
// @API AS PUT /autoscaling-api/v1/{project_id}/scaling_lifecycle_hook/{groupID}/{hookName}
// @API AS POST /autoscaling-api/v1/{project_id}/scaling_lifecycle_hook/{groupID}
func ResourceASLifecycleHook() *schema.Resource {
return &schema.Resource{
CreateContext: resourceASLifecycleHookCreate,
ReadContext: resourceASLifecycleHookRead,
UpdateContext: resourceASLifecycleHookUpdate,
DeleteContext: resourceASLifecycleHookDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceASLifecycleHookImportState,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"ADD", "REMOVE",
}, false),
},
"notification_topic_urn": {
Type: schema.TypeString,
Required: true,
},
"scaling_group_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"default_result": {
Type: schema.TypeString,
Optional: true,
Default: "ABANDON",
ValidateFunc: validation.StringInSlice([]string{
"ABANDON", "CONTINUE",
}, false),
},
"timeout": {
Type: schema.TypeInt,
Optional: true,
Default: 3600,
ValidateFunc: validation.IntBetween(300, 86400),
},
"notification_message": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^[^()<>&']{1,256}$`),
"The 'notification_message' of the lifecycle hook has special character"),
},
"notification_topic_name": {
Type: schema.TypeString,
Computed: true,
},
"create_time": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceASLifecycleHookCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conf := meta.(*config.Config)
client, err := conf.AutoscalingV1Client(conf.GetRegion(d))
if err != nil {
return diag.Errorf("error creating autoscaling client: %s", err)
}
groupId := d.Get("scaling_group_id").(string)
createOpts := lifecyclehooks.CreateOpts{
Name: d.Get("name").(string),
DefaultResult: d.Get("default_result").(string),
Timeout: d.Get("timeout").(int),
NotificationTopicURN: d.Get("notification_topic_urn").(string),
NotificationMetadata: d.Get("notification_message").(string),
}
hookType := d.Get("type").(string)
v, ok := hookTypeMap[hookType]
if !ok {
return diag.Errorf("lifecycle hook type (%s) is not in the map (%#v)", hookType, hookTypeMap)
}
createOpts.Type = v
log.Printf("[DEBUG] Create Options: %#v", createOpts)
hook, err := lifecyclehooks.Create(client, createOpts, groupId).Extract()
if err != nil {
return diag.Errorf("error creating lifecycle hook: %s", err)
}
d.SetId(hook.Name)
return resourceASLifecycleHookRead(ctx, d, meta)
}
func resourceASLifecycleHookRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conf := meta.(*config.Config)
region := conf.GetRegion(d)
client, err := conf.AutoscalingV1Client(region)
if err != nil {
return diag.Errorf("error creating autoscaling client: %s", err)
}
groupId := d.Get("scaling_group_id").(string)
hook, err := lifecyclehooks.Get(client, groupId, d.Id()).Extract()
if err != nil {
return common.CheckDeletedDiag(d, err, "error getting the specifies lifecycle hook of the autoscaling service")
}
log.Printf("[DEBUG] Retrieved lifecycle hook of AS group %s: %#v", groupId, hook)
d.Set("region", region)
if err = setASLifecycleHookToState(d, hook); err != nil {
return diag.Errorf("error setting the lifecycle hook to state: %s", err)
}
return nil
}
func resourceASLifecycleHookUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conf := meta.(*config.Config)
client, err := conf.AutoscalingV1Client(conf.GetRegion(d))
if err != nil {
return diag.Errorf("error creating autoscaling client: %s", err)
}
updateOpts := lifecyclehooks.UpdateOpts{}
if d.HasChange("type") {
hookType := d.Get("type").(string)
v, ok := hookTypeMap[hookType]
if !ok {
return diag.Errorf("the type (%s) of hook is not in the map (%#v)", hookType, hookTypeMap)
}
updateOpts.Type = v
}
if d.HasChange("default_result") {
updateOpts.DefaultResult = d.Get("default_result").(string)
}
if d.HasChange("timeout") {
updateOpts.Timeout = d.Get("timeout").(int)
}
if d.HasChange("notification_topic_urn") {
updateOpts.NotificationTopicURN = d.Get("notification_topic_urn").(string)
}
if d.HasChange("notification_message") {
updateOpts.NotificationMetadata = d.Get("notification_message").(string)
}
log.Printf("[DEBUG] Update Options: %#v", updateOpts)
groupId := d.Get("scaling_group_id").(string)
_, err = lifecyclehooks.Update(client, updateOpts, groupId, d.Id()).Extract()
if err != nil {
return diag.Errorf("error updating the lifecycle hook of the autoscaling service: %s", err)
}
return resourceASLifecycleHookRead(ctx, d, meta)
}
func resourceASLifecycleHookDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conf := meta.(*config.Config)
client, err := conf.AutoscalingV1Client(conf.GetRegion(d))
if err != nil {
return diag.Errorf("error creating autoscaling client: %s", err)
}
groupId := d.Get("scaling_group_id").(string)
err = lifecyclehooks.Delete(client, groupId, d.Id()).ExtractErr()
if err != nil {
return diag.Errorf("error deleting the lifecycle hook of the autoscaling service: %s", err)
}
return nil
}
func setASLifecycleHookToState(d *schema.ResourceData, hook *lifecyclehooks.Hook) error {
mErr := multierror.Append(
d.Set("name", hook.Name),
d.Set("default_result", hook.DefaultResult),
d.Set("timeout", hook.Timeout),
d.Set("notification_topic_urn", hook.NotificationTopicURN),
d.Set("notification_message", hook.NotificationMetadata),
setASLifecycleHookType(d, hook),
d.Set("notification_topic_name", hook.NotificationTopicName),
d.Set("create_time", hook.CreateTime),
)
err := mErr.ErrorOrNil()
return err
}
func setASLifecycleHookType(d *schema.ResourceData, hook *lifecyclehooks.Hook) error {
for k, v := range hookTypeMap {
if v == hook.Type {
err := d.Set("type", k)
return err
}
}
return fmt.Errorf("the type of hook response is not in the map")
}
func resourceASLifecycleHookImportState(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData, error) {
parts := strings.SplitN(d.Id(), "/", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid format specified for lifecycle hook, must be <scaling_group_id>/<hook_id>")
}
d.SetId(parts[1])
d.Set("scaling_group_id", parts[0])
return []*schema.ResourceData{d}, nil
}