-
Notifications
You must be signed in to change notification settings - Fork 658
/
resource_ibm_is_instance_group_membership.go
299 lines (273 loc) · 11.8 KB
/
resource_ibm_is_instance_group_membership.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isInstanceGroupMembership = "instance_group_membership"
isInstanceGroup = "instance_group"
isInstanceGroupMembershipName = "name"
isInstanceGroupMemershipActionDelete = "action_delete"
isInstanceGroupMemershipDeleteInstanceOnMembershipDelete = "delete_instance_on_membership_delete"
isInstanceGroupMemershipInstance = "instance"
isInstanceGroupMemershipInstanceName = "name"
isInstanceGroupMemershipInstanceTemplate = "instance_template"
isInstanceGroupMemershipInstanceTemplateName = "name"
isInstanceGroupMembershipCrn = "crn"
isInstanceGroupMembershipVirtualServerInstance = "virtual_server_instance"
isInstanceGroupMembershipLoadBalancerPoolMember = "load_balancer_pool_member"
isInstanceGroupMembershipStatus = "status"
)
func ResourceIBMISInstanceGroupMembership() *schema.Resource {
return &schema.Resource{
Create: resourceIBMISInstanceGroupMembershipUpdate,
Read: resourceIBMISInstanceGroupMembershipRead,
Update: resourceIBMISInstanceGroupMembershipUpdate,
Delete: resourceIBMISInstanceGroupMembershipDelete,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
isInstanceGroup: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.InvokeValidator("ibm_is_instance_group_membership", isInstanceGroup),
Description: "The instance group identifier.",
},
isInstanceGroupMembership: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.InvokeValidator("ibm_is_instance_group_membership", isInstanceGroupMembership),
Description: "The unique identifier for this instance group membership.",
},
isInstanceGroupMembershipName: {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.InvokeValidator("ibm_is_instance_group_membership", isInstanceGroupMembershipName),
Description: "The user-defined name for this instance group membership. Names must be unique within the instance group.",
},
isInstanceGroupMemershipActionDelete: {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "The delete flag for this instance group membership. Must be set to true to delete instance group membership.",
},
isInstanceGroupMemershipDeleteInstanceOnMembershipDelete: {
Type: schema.TypeBool,
Computed: true,
Description: "If set to true, when deleting the membership the instance will also be deleted.",
},
isInstanceGroupMemershipInstance: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isInstanceGroupMembershipCrn: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this virtual server instance.",
},
isInstanceGroupMembershipVirtualServerInstance: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this virtual server instance.",
},
isInstanceGroupMemershipInstanceName: {
Type: schema.TypeString,
Computed: true,
Description: "The user-defined name for this virtual server instance (and default system hostname).",
},
},
},
},
isInstanceGroupMemershipInstanceTemplate: {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isInstanceGroupMembershipCrn: {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this instance template.",
},
isInstanceGroupMemershipInstanceTemplate: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this instance template.",
},
isInstanceGroupMemershipInstanceTemplateName: {
Type: schema.TypeString,
Computed: true,
Description: "The unique user-defined name for this instance template.",
},
},
},
},
isInstanceGroupMembershipLoadBalancerPoolMember: {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this load balancer pool member.",
},
isInstanceGroupMembershipStatus: {
Type: schema.TypeString,
Computed: true,
Description: "The status of the instance group membership- `deleting`: Membership is deleting dependent resources- `failed`: Membership was unable to maintain dependent resources- `healthy`: Membership is active and serving in the group- `pending`: Membership is waiting for dependent resources- `unhealthy`: Membership has unhealthy dependent resources.",
},
},
}
}
func ResourceIBMISInstanceGroupMembershipValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isInstanceGroupMembershipName,
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Required: true,
Regexp: `^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$`,
MinValueLength: 1,
MaxValueLength: 63})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isInstanceGroup,
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Required: true,
Regexp: `^[-0-9a-z_]+$`,
MinValueLength: 1,
MaxValueLength: 64})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: isInstanceGroupMembership,
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Required: true,
Regexp: `^[-0-9a-z_]+$`,
MinValueLength: 1,
MaxValueLength: 64})
ibmISInstanceGroupMembershipResourceValidator := validate.ResourceValidator{ResourceName: "ibm_is_instance_group_membership", Schema: validateSchema}
return &ibmISInstanceGroupMembershipResourceValidator
}
func resourceIBMISInstanceGroupMembershipUpdate(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
instanceGroupID := d.Get(isInstanceGroup).(string)
instanceGroupMembershipID := d.Get(isInstanceGroupMembership).(string)
getInstanceGroupMembershipOptions := vpcv1.GetInstanceGroupMembershipOptions{
ID: &instanceGroupMembershipID,
InstanceGroupID: &instanceGroupID,
}
instanceGroupMembership, response, err := sess.GetInstanceGroupMembership(&getInstanceGroupMembershipOptions)
if err != nil || instanceGroupMembership == nil {
return fmt.Errorf("[ERROR] Error Getting InstanceGroup Membership: %s\n%s", err, response)
}
d.SetId(fmt.Sprintf("%s/%s", instanceGroupID, instanceGroupMembershipID))
if v, ok := d.GetOk(isInstanceGroupMemershipActionDelete); ok {
actionDelete := v.(bool)
if actionDelete {
return resourceIBMISInstanceGroupMembershipDelete(d, meta)
}
}
if v, ok := d.GetOk(isInstanceGroupMembershipName); ok {
name := v.(string)
if name != *instanceGroupMembership.Name {
updateInstanceGroupMembershipOptions := vpcv1.UpdateInstanceGroupMembershipOptions{}
instanceGroupMembershipPatchModel := &vpcv1.InstanceGroupMembershipPatch{}
instanceGroupMembershipPatchModel.Name = &name
updateInstanceGroupMembershipOptions.ID = &instanceGroupMembershipID
updateInstanceGroupMembershipOptions.InstanceGroupID = &instanceGroupID
instanceGroupMembershipPatch, err := instanceGroupMembershipPatchModel.AsPatch()
if err != nil {
return fmt.Errorf("[ERROR] Error calling asPatch for InstanceGroupMembershipPatch: %s", err)
}
updateInstanceGroupMembershipOptions.InstanceGroupMembershipPatch = instanceGroupMembershipPatch
_, response, err := sess.UpdateInstanceGroupMembership(&updateInstanceGroupMembershipOptions)
if err != nil {
return fmt.Errorf("[ERROR] Error updating InstanceGroup Membership: %s\n%s", err, response)
}
}
}
return resourceIBMISInstanceGroupMembershipRead(d, meta)
}
func resourceIBMISInstanceGroupMembershipRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
parts, err := flex.IdParts(d.Id())
if err != nil {
return err
}
instanceGroupID := parts[0]
instanceGroupMembershipID := parts[1]
getInstanceGroupMembershipOptions := vpcv1.GetInstanceGroupMembershipOptions{
ID: &instanceGroupMembershipID,
InstanceGroupID: &instanceGroupID,
}
instanceGroupMembership, response, err := sess.GetInstanceGroupMembership(&getInstanceGroupMembershipOptions)
if err != nil || instanceGroupMembership == nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Error Getting InstanceGroup Membership: %s\n%s", err, response)
}
d.Set(isInstanceGroupMemershipDeleteInstanceOnMembershipDelete, *instanceGroupMembership.DeleteInstanceOnMembershipDelete)
d.Set(isInstanceGroupMembership, *instanceGroupMembership.ID)
d.Set(isInstanceGroupMembershipStatus, *instanceGroupMembership.Status)
instances := make([]map[string]interface{}, 0)
if instanceGroupMembership.Instance != nil {
instance := map[string]interface{}{
isInstanceGroupMembershipCrn: *instanceGroupMembership.Instance.CRN,
isInstanceGroupMembershipVirtualServerInstance: *instanceGroupMembership.Instance.ID,
isInstanceGroupMemershipInstanceName: *instanceGroupMembership.Instance.Name,
}
instances = append(instances, instance)
}
d.Set(isInstanceGroupMemershipInstance, instances)
instance_templates := make([]map[string]interface{}, 0)
if instanceGroupMembership.InstanceTemplate != nil {
instance_template := map[string]interface{}{
isInstanceGroupMembershipCrn: *instanceGroupMembership.InstanceTemplate.CRN,
isInstanceGroupMemershipInstanceTemplate: *instanceGroupMembership.InstanceTemplate.ID,
isInstanceGroupMemershipInstanceTemplateName: *instanceGroupMembership.InstanceTemplate.Name,
}
instance_templates = append(instance_templates, instance_template)
}
d.Set(isInstanceGroupMemershipInstanceTemplate, instance_templates)
if instanceGroupMembership.PoolMember != nil && instanceGroupMembership.PoolMember.ID != nil {
d.Set(isInstanceGroupMembershipLoadBalancerPoolMember, *instanceGroupMembership.PoolMember.ID)
}
return nil
}
func resourceIBMISInstanceGroupMembershipDelete(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
parts, err := flex.IdParts(d.Id())
if err != nil {
return err
}
instanceGroupID := parts[0]
instanceGroupMembershipID := parts[1]
deleteInstanceGroupMembershipOptions := vpcv1.DeleteInstanceGroupMembershipOptions{
ID: &instanceGroupMembershipID,
InstanceGroupID: &instanceGroupID,
}
response, err := sess.DeleteInstanceGroupMembership(&deleteInstanceGroupMembershipOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
return fmt.Errorf("[ERROR] Error Deleting the InstanceGroup Membership: %s\n%s", err, response)
}
return nil
}