-
Notifications
You must be signed in to change notification settings - Fork 642
/
resource_ibm_pi_shared_processor_pool.go
427 lines (371 loc) · 13.4 KB
/
resource_ibm_pi_shared_processor_pool.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
// Copyright IBM Corp. 2022 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package power
import (
"context"
"fmt"
"log"
"strings"
"time"
st "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/helpers"
models "github.com/IBM-Cloud/power-go-client/power/models"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"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"
)
func ResourceIBMPISharedProcessorPool() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMPISharedProcessorPoolCreate,
ReadContext: resourceIBMPISharedProcessorPoolRead,
UpdateContext: resourceIBMPISharedProcessorPoolUpdate,
DeleteContext: resourceIBMPISharedProcessorPoolDelete,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(60 * time.Minute),
Update: schema.DefaultTimeout(60 * time.Minute),
Delete: schema.DefaultTimeout(60 * time.Minute),
},
Schema: map[string]*schema.Schema{
// Required Arguments
Arg_SharedProcessorPoolName: {
Type: schema.TypeString,
Required: true,
Description: "Name of the shared processor pool",
},
Arg_SharedProcessorPoolHostGroup: {
Type: schema.TypeString,
Required: true,
Description: "Host group of the shared processor pool",
},
Arg_SharedProcessorPoolReservedCores: {
Type: schema.TypeInt,
Required: true,
Description: "The amount of reserved cores for the shared processor pool",
},
Arg_CloudInstanceID: {
Type: schema.TypeString,
Required: true,
Description: "PI cloud instance ID",
},
// Optional Arguments
Arg_SharedProcessorPoolPlacementGroupID: {
Type: schema.TypeString,
Optional: true,
Description: "Placement group the shared processor pool is created in",
},
// Attributes
Attr_SharedProcessorPoolID: {
Type: schema.TypeString,
Computed: true,
Description: "Shared processor pool ID",
},
Attr_SharedProcessorPoolAvailableCores: {
Type: schema.TypeInt,
Computed: true,
Description: "Shared processor pool available cores",
},
Attr_SharedProcessorPoolAllocatedCores: {
Type: schema.TypeFloat,
Computed: true,
Description: "Shared processor pool allocated cores",
},
Attr_SharedProcessorPoolHostID: {
Type: schema.TypeInt,
Computed: true,
Description: "The host ID where the shared processor pool resides",
},
Attr_SharedProcessorPoolStatus: {
Type: schema.TypeString,
Computed: true,
Description: "The status of the shared processor pool",
},
Attr_SharedProcessorPoolStatusDetail: {
Type: schema.TypeString,
Computed: true,
Description: "The status details of the shared processor pool",
},
Attr_SharedProcessorPoolPlacementGroups: {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "SPP placement groups the shared processor pool are in",
},
Attr_SharedProcessorPoolInstances: {
Type: schema.TypeList,
Computed: true,
Description: "List of server instances deployed in the shared processor pool",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_SharedProcessorPoolInstanceCpus: {
Type: schema.TypeInt,
Computed: true,
Description: "The amount of cpus for the server instance",
},
Attr_SharedProcessorPoolInstanceUncapped: {
Type: schema.TypeBool,
Computed: true,
Description: "Identifies if uncapped or not",
},
Attr_SharedProcessorPoolInstanceAvailabilityZone: {
Type: schema.TypeString,
Computed: true,
Description: "Availability zone for the server instances",
},
Attr_SharedProcessorPoolInstanceId: {
Type: schema.TypeString,
Computed: true,
Description: "The server instance ID",
},
Attr_SharedProcessorPoolInstanceMemory: {
Type: schema.TypeInt,
Computed: true,
Description: "The amount of memory for the server instance",
},
Attr_SharedProcessorPoolInstanceName: {
Type: schema.TypeString,
Computed: true,
Description: "The server instance name",
},
Attr_SharedProcessorPoolInstanceStatus: {
Type: schema.TypeString,
Computed: true,
Description: "Status of the server",
},
Attr_SharedProcessorPoolInstanceVcpus: {
Type: schema.TypeFloat,
Computed: true,
Description: "The amout of vcpus for the server instance",
},
},
},
},
},
}
}
func resourceIBMPISharedProcessorPoolCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
name := d.Get(Arg_SharedProcessorPoolName).(string)
hostGroup := d.Get(Arg_SharedProcessorPoolHostGroup).(string)
reservedCores := d.Get(Arg_SharedProcessorPoolReservedCores).(int)
cores := int64(reservedCores)
client := st.NewIBMPISharedProcessorPoolClient(ctx, sess, cloudInstanceID)
body := &models.SharedProcessorPoolCreate{
Name: &name,
HostGroup: &hostGroup,
ReservedCores: &cores,
}
if pg, ok := d.GetOk(Arg_SharedProcessorPoolPlacementGroupID); ok {
body.PlacementGroupID = pg.(string)
}
spp, err := client.Create(body)
if err != nil || spp == nil {
return diag.Errorf("error creating the shared processor pool: %v", err)
}
var sharedProcessorPoolReadyStatus string
d.SetId(fmt.Sprintf("%s/%s", cloudInstanceID, *spp.ID))
_, err = isWaitForPISharedProcessorPoolAvailable(ctx, d, client, *spp.ID, sharedProcessorPoolReadyStatus)
if err != nil {
return diag.FromErr(err)
}
return resourceIBMPISharedProcessorPoolRead(ctx, d, meta)
}
func isWaitForPISharedProcessorPoolAvailable(ctx context.Context, d *schema.ResourceData, client *st.IBMPISharedProcessorPoolClient, id string, sharedProcessorPoolReadyStatus string) (interface{}, error) {
log.Printf("Waiting for PISharedProcessorPool (%s) to be active ", id)
stateConf := &resource.StateChangeConf{
Pending: []string{"configuring"},
Target: []string{"active", "failed", ""},
Refresh: isPISharedProcessorPoolRefreshFunc(client, id, sharedProcessorPoolReadyStatus),
Delay: 20 * time.Second,
MinTimeout: activeTimeOut,
Timeout: d.Timeout(schema.TimeoutCreate),
}
return stateConf.WaitForStateContext(ctx)
}
func isPISharedProcessorPoolRefreshFunc(client *st.IBMPISharedProcessorPoolClient, id, sharedProcessorPoolReadyStatus string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
pool, err := client.Get(id)
if err != nil {
return nil, "", err
}
// Check for `sharedProcessorPoolReadyStatus` status
if pool.SharedProcessorPool.Status == "active" {
return pool, "active", nil
}
if pool.SharedProcessorPool.Status == "failed" {
err = fmt.Errorf("failed to create the shared processor pool")
return pool, pool.SharedProcessorPool.Status, err
}
return pool, "configuring", nil
}
}
func resourceIBMPISharedProcessorPoolRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}
parts, err := flex.IdParts(d.Id())
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID := parts[0]
client := st.NewIBMPISharedProcessorPoolClient(ctx, sess, cloudInstanceID)
response, err := client.Get(parts[1])
if err != nil || response == nil {
return diag.Errorf("error reading the shared processor pool: %v", err)
}
d.Set(Arg_CloudInstanceID, cloudInstanceID)
d.Set(Arg_SharedProcessorPoolHostGroup, response.SharedProcessorPool.HostGroup)
if response.SharedProcessorPool.Name != nil {
d.Set(Arg_SharedProcessorPoolName, response.SharedProcessorPool.Name)
}
if response.SharedProcessorPool.ID != nil {
d.Set(Attr_SharedProcessorPoolID, response.SharedProcessorPool.ID)
}
if response.SharedProcessorPool.ReservedCores != nil {
d.Set(Arg_SharedProcessorPoolReservedCores, response.SharedProcessorPool.ReservedCores)
}
if response.SharedProcessorPool.AllocatedCores != nil {
d.Set(Attr_SharedProcessorPoolAllocatedCores, response.SharedProcessorPool.AllocatedCores)
}
if response.SharedProcessorPool.AvailableCores != nil {
d.Set(Attr_SharedProcessorPoolAvailableCores, response.SharedProcessorPool.AvailableCores)
}
if response.SharedProcessorPool.AvailableCores != nil {
d.Set(Attr_SharedProcessorPoolAvailableCores, response.SharedProcessorPool.AvailableCores)
}
if response.SharedProcessorPool.SharedProcessorPoolPlacementGroups != nil {
pgIDs := make([]string, len(response.SharedProcessorPool.SharedProcessorPoolPlacementGroups))
for i, pg := range response.SharedProcessorPool.SharedProcessorPoolPlacementGroups {
pgIDs[i] = *pg.ID
}
d.Set(Attr_SharedProcessorPoolPlacementGroups, pgIDs)
}
d.Set(Attr_SharedProcessorPoolHostID, response.SharedProcessorPool.HostID)
d.Set(Attr_SharedProcessorPoolStatus, response.SharedProcessorPool.Status)
d.Set(Attr_SharedProcessorPoolStatusDetail, response.SharedProcessorPool.StatusDetail)
serversMap := []map[string]interface{}{}
if response.Servers != nil {
for _, s := range response.Servers {
if s != nil {
v := map[string]interface{}{
Attr_SharedProcessorPoolInstanceCpus: s.Cpus,
Attr_SharedProcessorPoolInstanceUncapped: s.Uncapped,
Attr_SharedProcessorPoolInstanceAvailabilityZone: s.AvailabilityZone,
Attr_SharedProcessorPoolInstanceId: s.ID,
Attr_SharedProcessorPoolInstanceMemory: s.Memory,
Attr_SharedProcessorPoolInstanceName: s.Name,
Attr_SharedProcessorPoolInstanceStatus: s.Status,
Attr_SharedProcessorPoolInstanceVcpus: s.Vcpus,
}
serversMap = append(serversMap, v)
}
}
}
d.Set(Attr_SharedProcessorPoolInstances, serversMap)
return nil
}
func resourceIBMPISharedProcessorPoolUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID, sppID, err := splitID(d.Id())
if err != nil {
return diag.FromErr(err)
}
client := st.NewIBMPISharedProcessorPoolClient(ctx, sess, cloudInstanceID)
body := &models.SharedProcessorPoolUpdate{}
if d.HasChange(Arg_SharedProcessorPoolName) {
name := d.Get(Arg_SharedProcessorPoolName).(string)
body.Name = name
}
if d.HasChange(Arg_SharedProcessorPoolReservedCores) {
reservedCores := int64(d.Get(Arg_SharedProcessorPoolReservedCores).(int))
body.ReservedCores = reservedCores
}
_, err = client.Update(sppID, body)
if err != nil {
return diag.Errorf("error updating the shared processor pool: %v", err)
}
if d.HasChange(Attr_SharedProcessorPoolPlacementGroups) {
pgClient := st.NewIBMPISPPPlacementGroupClient(ctx, sess, cloudInstanceID)
oldRaw, newRaw := d.GetChange(Attr_SharedProcessorPoolPlacementGroups)
old := oldRaw.([]interface{})
new := newRaw.([]interface{})
var oldPGs []string
for _, o := range old {
oldPGs = append(oldPGs, o.(string))
}
var newPGs []string
for _, n := range new {
newPGs = append(newPGs, n.(string))
}
// find removed pgs and remove them
pgsToRemove := getDifferences(oldPGs, newPGs)
for _, pgToRemove := range pgsToRemove {
if len(strings.TrimSpace(pgToRemove)) > 0 {
placementGroupID := pgToRemove
//remove spp from old placement group
_, err := pgClient.DeleteMember(placementGroupID, sppID)
if err != nil {
// ignore delete member error where the spp is already not in the PG
if !strings.Contains(err.Error(), "is not part of spp placement group") {
return diag.FromErr(err)
}
}
}
}
// find added pgs and then add them
pgsToAdd := getDifferences(newPGs, oldPGs)
for _, pgToAdd := range pgsToAdd {
if len(strings.TrimSpace(pgToAdd)) > 0 {
placementGroupID := pgToAdd
// add spp to a new placement group
_, err := pgClient.AddMember(placementGroupID, sppID)
if err != nil {
return diag.FromErr(err)
}
}
}
}
return resourceIBMPISharedProcessorPoolRead(ctx, d, meta)
}
// returns the elements in string array a that are not in array z
func getDifferences(a, z []string) []string {
mb := make(map[string]struct{}, len(z))
for _, x := range z {
mb[x] = struct{}{}
}
var diff []string
for _, x := range a {
if _, found := mb[x]; !found {
diff = append(diff, x)
}
}
return diff
}
func resourceIBMPISharedProcessorPoolDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}
parts, err := flex.IdParts(d.Id())
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID := parts[0]
client := st.NewIBMPISharedProcessorPoolClient(ctx, sess, cloudInstanceID)
err = client.Delete(parts[1])
if err != nil {
return diag.Errorf("error deleting the shared processor pool: %v", err)
}
d.SetId("")
return nil
}