-
Notifications
You must be signed in to change notification settings - Fork 670
/
resource_ibm_pi_operations.go
273 lines (214 loc) · 7.54 KB
/
resource_ibm_pi_operations.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package power
import (
"context"
"fmt"
st "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/helpers"
"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/IBM-Cloud/terraform-provider-ibm/ibm/validate"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"log"
"time"
)
/*
Transition states
The server can go from
ACTIVE --> SHUTOFF
ACTIVE --> HARD-REBOOT
ACTIVE --> SOFT-REBOOT
SHUTOFF--> ACTIVE
*/
func ResourceIBMPIIOperations() *schema.Resource {
return &schema.Resource{
Create: resourceIBMPIOperationsCreate,
Read: resourceIBMPIOperationsRead,
Update: resourceIBMPIOperationsUpdate,
Delete: resourceIBMPIOperationsDelete,
//Exists: resourceIBMPIOperationsExists,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(60 * time.Minute),
Delete: schema.DefaultTimeout(60 * time.Minute),
},
Schema: map[string]*schema.Schema{
helpers.PICloudInstanceId: {
Type: schema.TypeString,
Required: true,
Description: "PI Cloud instnce id",
},
helpers.PIInstanceOperationStatus: {
Type: schema.TypeString,
Computed: true,
Description: "PI instance operation status",
},
helpers.PIInstanceOperationServerName: {
Type: schema.TypeString,
Required: true,
Description: "PI instance Operation server name",
},
"addresses": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"ip": {
Type: schema.TypeString,
Computed: true,
},
"macaddress": {
Type: schema.TypeString,
Computed: true,
},
"networkid": {
Type: schema.TypeString,
Computed: true,
},
"networkname": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
helpers.PIInstanceHealthStatus: {
Type: schema.TypeString,
Computed: true,
Description: "PI instance health status",
},
helpers.PIInstanceOperationType: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{"start", "stop", "hard-reboot", "soft-reboot", "immediate-shutdown"}),
Description: "PI instance operation type",
},
helpers.PIInstanceOperationProgress: {
Type: schema.TypeFloat,
Computed: true,
Description: "Progress of the operation",
},
},
}
}
func resourceIBMPIOperationsCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("Now in the Power Operations Code")
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return err
}
powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)
operation := d.Get(helpers.PIInstanceOperationType).(string)
name := d.Get(helpers.PIInstanceOperationServerName).(string)
body := &models.PVMInstanceAction{Action: flex.PtrToString(operation)}
log.Printf("Calling the IBM PI Operations [ %s ] with on the instance with name [ %s ]", operation, name)
client := st.NewIBMPIInstanceClient(context.Background(), sess, powerinstanceid)
/*
TODO
To add a check if the action performed is applicable on the current state of the instance
*/
err = client.Action(name, body)
if err != nil {
log.Printf("[DEBUG] err %s", err)
return fmt.Errorf("[ERROR] Failed to perform the operation on the instance %v", err)
} else {
log.Printf("Executed the stop operation on the lpar")
}
if operation == "stop" || operation == "immediate-shutdown" {
var targetStatus = "SHUTOFF"
log.Printf("Calling the check opertion that was invoked [%s] to check for status [ %s ]", operation, targetStatus)
_, err = isWaitForPIInstanceOperationStatus(client, name, d.Timeout(schema.TimeoutCreate), powerinstanceid, operation, targetStatus)
if err != nil {
return err
} else {
log.Printf("Executed the start operation on the lpar")
}
}
if operation == "start" || operation == "soft-reboot" || operation == "hard-reboot" {
var targetStatus = "ACTIVE"
log.Printf("Calling the check opertion that was invoked [%s] to check for status [ %s ]", operation, targetStatus)
_, err = isWaitForPIInstanceOperationStatus(client, name, d.Timeout(schema.TimeoutCreate), powerinstanceid, operation, targetStatus)
if err != nil {
return err
}
}
return resourceIBMPIOperationsRead(d, meta)
}
func resourceIBMPIOperationsRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("Calling the PowerOperations Read code..for instance name %s", d.Get(helpers.PIInstanceOperationServerName).(string))
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return err
}
powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)
name := d.Get(helpers.PIInstanceOperationServerName).(string)
powerC := st.NewIBMPIInstanceClient(context.Background(), sess, powerinstanceid)
powervmdata, err := powerC.Get(name)
if err != nil {
return err
}
d.Set("status", powervmdata.Status)
d.Set("progress", powervmdata.Progress)
if powervmdata.Health != nil {
d.Set("healthstatus", powervmdata.Health.Status)
}
pvminstanceid := *powervmdata.PvmInstanceID
d.SetId(fmt.Sprintf("%s/%s", powerinstanceid, pvminstanceid))
return nil
}
func resourceIBMPIOperationsUpdate(d *schema.ResourceData, meta interface{}) error {
return nil
}
func resourceIBMPIOperationsDelete(data *schema.ResourceData, meta interface{}) error {
return nil
}
// Exists
func resourceIBMPIOperationsExists(d *schema.ResourceData, meta interface{}) (bool, error) {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return false, err
}
id := d.Id()
powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)
client := st.NewIBMPIInstanceClient(context.Background(), sess, powerinstanceid)
instance, err := client.Get(d.Id())
if err != nil {
return false, err
}
return instance.PvmInstanceID == &id, nil
}
func isWaitForPIInstanceOperationStatus(client *st.IBMPIInstanceClient, name string, timeout time.Duration, powerinstanceid, operation, targetstatus string) (interface{}, error) {
log.Printf("Waiting for the Operation [ %s ] to be performed on the instance with name [ %s ]", operation, name)
stateConf := &resource.StateChangeConf{
Pending: []string{"ACTIVE", "SHUTOFF", "WARNING"},
Target: []string{targetstatus},
Refresh: isPIOperationsRefreshFunc(client, name, powerinstanceid, targetstatus),
Delay: 1 * time.Minute,
MinTimeout: 2 * time.Minute,
Timeout: 120 * time.Minute,
}
return stateConf.WaitForState()
}
func isPIOperationsRefreshFunc(client *st.IBMPIInstanceClient, id, powerinstanceid, targetstatus string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Printf("Waiting for the target status to be [ %s ]", targetstatus)
pvm, err := client.Get(id)
if err != nil {
return nil, "", err
}
if *pvm.Status == targetstatus && pvm.Health.Status == helpers.PIInstanceHealthOk {
log.Printf("The health status is now ok")
//if *pvm.Status == "active" ; if *pvm.Addresses[0].IP == nil {
return pvm, targetstatus, nil
//}
}
return pvm, helpers.PIInstanceHealthWarning, nil
}
}