-
Notifications
You must be signed in to change notification settings - Fork 670
/
resource_ibm_is_dedicated_host_group.go
456 lines (390 loc) · 16.2 KB
/
resource_ibm_is_dedicated_host_group.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"context"
"fmt"
"log"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/vpc-go-sdk/vpcv1"
)
func ResourceIbmIsDedicatedHostGroup() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIbmIsDedicatedHostGroupCreate,
ReadContext: resourceIbmIsDedicatedHostGroupRead,
UpdateContext: resourceIbmIsDedicatedHostGroupUpdate,
DeleteContext: resourceIbmIsDedicatedHostGroupDelete,
Importer: &schema.ResourceImporter{},
Schema: map[string]*schema.Schema{
"class": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The dedicated host profile class for hosts in this group.",
},
"family": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.InvokeValidator("ibm_is_dedicated_host_group", "family"),
Description: "The dedicated host profile family for hosts in this group.",
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validate.InvokeValidator("ibm_is_dedicated_host_group", "name"),
Description: "The unique user-defined name for this dedicated host group. If unspecified, the name will be a hyphenated list of randomly-selected words.",
},
"resource_group": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: "The unique identifier of the resource group to use. If unspecified, the account's [default resourcegroup](https://cloud.ibm.com/apidocs/resource-manager#introduction) is used.",
},
"zone": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The globally unique name of the zone this dedicated host group will reside in.",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time that the dedicated host group was created.",
},
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this dedicated host group.",
},
"dedicated_hosts": {
Type: schema.TypeList,
Computed: true,
Description: "The dedicated hosts that are in this dedicated host group.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"crn": {
Type: schema.TypeString,
Computed: true,
Description: "The CRN for this dedicated host.",
},
"deleted": {
Type: schema.TypeList,
Computed: true,
Description: "If present, this property indicates the referenced resource has been deleted and providessome supplementary information.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"more_info": {
Type: schema.TypeString,
Computed: true,
Description: "Link to documentation about deleted resources.",
},
},
},
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this dedicated host.",
},
"id": {
Type: schema.TypeString,
Computed: true,
Description: "The unique identifier for this dedicated host.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The unique user-defined name for this dedicated host. If unspecified, the name will be a hyphenated list of randomly-selected words.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of resource referenced.",
},
},
},
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this dedicated host group.",
},
"resource_type": {
Type: schema.TypeString,
Computed: true,
Description: "The type of resource referenced.",
},
"supported_instance_profiles": {
Type: schema.TypeList,
Computed: true,
Description: "Array of instance profiles that can be used by instances placed on this dedicated host group.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"href": {
Type: schema.TypeString,
Computed: true,
Description: "The URL for this virtual server instance profile.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The globally unique name for this virtual server instance profile.",
},
},
},
},
},
}
}
func ResourceIbmIsDedicatedHostGroupValidator() *validate.ResourceValidator {
validateSchema := make([]validate.ValidateSchema, 0)
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "family",
ValidateFunctionIdentifier: validate.ValidateAllowedStringValue,
Type: validate.TypeString,
Optional: true,
AllowedValues: "balanced, compute, memory",
})
validateSchema = append(validateSchema,
validate.ValidateSchema{
Identifier: "name",
ValidateFunctionIdentifier: validate.ValidateRegexpLen,
Type: validate.TypeString,
Optional: true,
Regexp: `^([a-z]|[a-z][-a-z0-9]*[a-z0-9])$`,
MinValueLength: 1,
MaxValueLength: 63,
})
resourceValidator := validate.ResourceValidator{ResourceName: "ibm_is_dedicated_host_group", Schema: validateSchema}
return &resourceValidator
}
func resourceIbmIsDedicatedHostGroupCreate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
createDedicatedHostGroupOptions := &vpcv1.CreateDedicatedHostGroupOptions{}
if class, ok := d.GetOk("class"); ok {
createDedicatedHostGroupOptions.SetClass(class.(string))
}
if family, ok := d.GetOk("family"); ok {
createDedicatedHostGroupOptions.SetFamily(family.(string))
}
if name, ok := d.GetOk("name"); ok {
createDedicatedHostGroupOptions.SetName(name.(string))
}
if resgroup, ok := d.GetOk("resource_group"); ok {
resgroupstr := resgroup.(string)
resourceGroup := vpcv1.ResourceGroupIdentity{
ID: &resgroupstr,
}
createDedicatedHostGroupOptions.SetResourceGroup(&resourceGroup)
}
if zone, ok := d.GetOk("zone"); ok {
zonestr := zone.(string)
zoneidentity := vpcv1.ZoneIdentity{
Name: &zonestr,
}
createDedicatedHostGroupOptions.SetZone(&zoneidentity)
}
dedicatedHostGroup, response, err := vpcClient.CreateDedicatedHostGroupWithContext(context, createDedicatedHostGroupOptions)
if err != nil {
log.Printf("[DEBUG] CreateDedicatedHostGroupWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
d.SetId(*dedicatedHostGroup.ID)
return resourceIbmIsDedicatedHostGroupRead(context, d, meta)
}
func resourceIbmIsDedicatedHostGroupMapToResourceGroupIdentity(resourceGroupIdentityMap map[string]interface{}) vpcv1.ResourceGroupIdentity {
resourceGroupIdentity := vpcv1.ResourceGroupIdentity{}
if resourceGroupIdentityMap["id"] != nil {
resourceGroupIdentity.ID = core.StringPtr(resourceGroupIdentityMap["id"].(string))
}
return resourceGroupIdentity
}
func resourceIbmIsDedicatedHostGroupMapToResourceGroupIdentityByID(resourceGroupIdentityByIDMap map[string]interface{}) vpcv1.ResourceGroupIdentityByID {
resourceGroupIdentityByID := vpcv1.ResourceGroupIdentityByID{}
resourceGroupIdentityByID.ID = core.StringPtr(resourceGroupIdentityByIDMap["id"].(string))
return resourceGroupIdentityByID
}
func resourceIbmIsDedicatedHostGroupRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
getDedicatedHostGroupOptions := &vpcv1.GetDedicatedHostGroupOptions{}
getDedicatedHostGroupOptions.SetID(d.Id())
dedicatedHostGroup, response, err := vpcClient.GetDedicatedHostGroupWithContext(context, getDedicatedHostGroupOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
log.Printf("[DEBUG] GetDedicatedHostGroupWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
if err = d.Set("class", dedicatedHostGroup.Class); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting class: %s", err))
}
if err = d.Set("family", dedicatedHostGroup.Family); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting family: %s", err))
}
if err = d.Set("name", dedicatedHostGroup.Name); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting name: %s", err))
}
if dedicatedHostGroup.ResourceGroup != nil {
resourceGroupID := *dedicatedHostGroup.ResourceGroup.ID
if err = d.Set("resource_group", resourceGroupID); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting resource_group: %s", err))
}
}
if dedicatedHostGroup.Zone != nil {
zoneName := *dedicatedHostGroup.Zone.Name
if err = d.Set("zone", zoneName); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting zone: %s", err))
}
}
if err = d.Set("created_at", dedicatedHostGroup.CreatedAt.String()); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting created_at: %s", err))
}
if err = d.Set("crn", dedicatedHostGroup.CRN); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting crn: %s", err))
}
dedicatedHosts := []map[string]interface{}{}
for _, dedicatedHostsItem := range dedicatedHostGroup.DedicatedHosts {
dedicatedHostsItemMap := resourceIbmIsDedicatedHostGroupDedicatedHostReferenceToMap(dedicatedHostsItem)
dedicatedHosts = append(dedicatedHosts, dedicatedHostsItemMap)
}
if err = d.Set("dedicated_hosts", dedicatedHosts); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting dedicated_hosts: %s", err))
}
if err = d.Set("href", dedicatedHostGroup.Href); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting href: %s", err))
}
if err = d.Set("resource_type", dedicatedHostGroup.ResourceType); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting resource_type: %s", err))
}
supportedInstanceProfiles := []map[string]interface{}{}
for _, supportedInstanceProfilesItem := range dedicatedHostGroup.SupportedInstanceProfiles {
supportedInstanceProfilesItemMap := resourceIbmIsDedicatedHostGroupInstanceProfileReferenceToMap(supportedInstanceProfilesItem)
supportedInstanceProfiles = append(supportedInstanceProfiles, supportedInstanceProfilesItemMap)
}
if err = d.Set("supported_instance_profiles", supportedInstanceProfiles); err != nil {
return diag.FromErr(fmt.Errorf("[ERROR] Error setting supported_instance_profiles: %s", err))
}
return nil
}
func resourceIbmIsDedicatedHostGroupResourceGroupIdentityToMap(resourceGroupIdentity vpcv1.ResourceGroupIdentity) map[string]interface{} {
resourceGroupIdentityMap := map[string]interface{}{}
resourceGroupIdentityMap["id"] = resourceGroupIdentity.ID
return resourceGroupIdentityMap
}
func resourceIbmIsDedicatedHostGroupResourceGroupIdentityByIDToMap(resourceGroupIdentityByID vpcv1.ResourceGroupIdentityByID) map[string]interface{} {
resourceGroupIdentityByIDMap := map[string]interface{}{}
resourceGroupIdentityByIDMap["id"] = resourceGroupIdentityByID.ID
return resourceGroupIdentityByIDMap
}
func resourceIbmIsDedicatedHostGroupZoneIdentityToMap(zoneIdentity vpcv1.ZoneIdentity) map[string]interface{} {
zoneIdentityMap := map[string]interface{}{}
zoneIdentityMap["name"] = zoneIdentity.Name
zoneIdentityMap["href"] = zoneIdentity.Href
return zoneIdentityMap
}
func resourceIbmIsDedicatedHostGroupZoneIdentityByNameToMap(zoneIdentityByName vpcv1.ZoneIdentityByName) map[string]interface{} {
zoneIdentityByNameMap := map[string]interface{}{}
zoneIdentityByNameMap["name"] = zoneIdentityByName.Name
return zoneIdentityByNameMap
}
func resourceIbmIsDedicatedHostGroupZoneIdentityByHrefToMap(zoneIdentityByHref vpcv1.ZoneIdentityByHref) map[string]interface{} {
zoneIdentityByHrefMap := map[string]interface{}{}
zoneIdentityByHrefMap["href"] = zoneIdentityByHref.Href
return zoneIdentityByHrefMap
}
func resourceIbmIsDedicatedHostGroupDedicatedHostReferenceToMap(dedicatedHostReference vpcv1.DedicatedHostReference) map[string]interface{} {
dedicatedHostReferenceMap := map[string]interface{}{}
dedicatedHostReferenceMap["crn"] = dedicatedHostReference.CRN
if dedicatedHostReference.Deleted != nil {
DeletedMap := resourceIbmIsDedicatedHostGroupDedicatedHostReferenceDeletedToMap(*dedicatedHostReference.Deleted)
dedicatedHostReferenceMap["deleted"] = []map[string]interface{}{DeletedMap}
}
dedicatedHostReferenceMap["href"] = dedicatedHostReference.Href
dedicatedHostReferenceMap["id"] = dedicatedHostReference.ID
dedicatedHostReferenceMap["name"] = dedicatedHostReference.Name
dedicatedHostReferenceMap["resource_type"] = dedicatedHostReference.ResourceType
return dedicatedHostReferenceMap
}
func resourceIbmIsDedicatedHostGroupDedicatedHostReferenceDeletedToMap(dedicatedHostReferenceDeleted vpcv1.DedicatedHostReferenceDeleted) map[string]interface{} {
dedicatedHostReferenceDeletedMap := map[string]interface{}{}
dedicatedHostReferenceDeletedMap["more_info"] = dedicatedHostReferenceDeleted.MoreInfo
return dedicatedHostReferenceDeletedMap
}
func resourceIbmIsDedicatedHostGroupInstanceProfileReferenceToMap(instanceProfileReference vpcv1.InstanceProfileReference) map[string]interface{} {
instanceProfileReferenceMap := map[string]interface{}{}
instanceProfileReferenceMap["href"] = instanceProfileReference.Href
instanceProfileReferenceMap["name"] = instanceProfileReference.Name
return instanceProfileReferenceMap
}
func resourceIbmIsDedicatedHostGroupUpdate(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
updateDedicatedHostGroupOptions := &vpcv1.UpdateDedicatedHostGroupOptions{}
updateDedicatedHostGroupOptions.SetID(d.Id())
hasChange := false
if d.HasChange("name") {
groupnamestr := d.Get("name").(string)
dedicatedHostGroupPatchModel := vpcv1.DedicatedHostGroupPatch{
Name: &groupnamestr,
}
dedicatedHostGroupPatch, err := dedicatedHostGroupPatchModel.AsPatch()
if err != nil {
log.Printf("[DEBUG] Error calling asPatch for DedicatedHostGroupPatch: %s", err)
return diag.FromErr(err)
}
updateDedicatedHostGroupOptions.DedicatedHostGroupPatch = dedicatedHostGroupPatch
hasChange = true
}
if hasChange {
_, response, err := vpcClient.UpdateDedicatedHostGroupWithContext(context, updateDedicatedHostGroupOptions)
if err != nil {
log.Printf("[DEBUG] UpdateDedicatedHostGroupWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
}
return resourceIbmIsDedicatedHostGroupRead(context, d, meta)
}
func resourceIbmIsDedicatedHostGroupDelete(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vpcClient, err := meta.(conns.ClientSession).VpcV1API()
if err != nil {
return diag.FromErr(err)
}
getDedicatedHostGroupOptions := &vpcv1.GetDedicatedHostGroupOptions{}
getDedicatedHostGroupOptions.SetID(d.Id())
_, response, err := vpcClient.GetDedicatedHostGroupWithContext(context, getDedicatedHostGroupOptions)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
log.Printf("[DEBUG] GetDedicatedHostGroupWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
deleteDedicatedHostGroupOptions := &vpcv1.DeleteDedicatedHostGroupOptions{}
deleteDedicatedHostGroupOptions.SetID(d.Id())
response, err = vpcClient.DeleteDedicatedHostGroupWithContext(context, deleteDedicatedHostGroupOptions)
if err != nil {
log.Printf("[DEBUG] DeleteDedicatedHostGroupWithContext failed %s\n%s", err, response)
return diag.FromErr(err)
}
d.SetId("")
return nil
}