forked from IBM-Cloud/terraform-provider-ibm
/
resource_ibm_pi_operations.go
256 lines (199 loc) · 6.62 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
package ibm
import (
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/client/p_cloud_p_vm_instances"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/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,
},
helpers.PIInstanceOperationStatus: {
Type: schema.TypeString,
Computed: true,
},
helpers.PIInstanceOperationServerName: {
Type: schema.TypeString,
Required: true,
},
"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,
},
helpers.PIInstanceOperationType: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateAllowedStringValue([]string{"start", "stop", "hard-reboot", "soft-reboot"}),
},
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 PowerVMCreate")
sess, err := meta.(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: ptrToString(operation)}
log.Printf("Calling the IBM PI Operations with the following attributes %s - %s", powerinstanceid, name)
client := st.NewIBMPIInstanceClient(sess, powerinstanceid)
/*
TODO
To add a check if the action performed is applicable on the current state of the instance
*/
pvmoperation, err := client.Action(&p_cloud_p_vm_instances.PcloudPvminstancesActionPostParams{
Body: body,
}, name, powerinstanceid)
if err != nil {
log.Printf("[DEBUG] err %s", isErrorToString(err))
return err
}
log.Printf("Printing the instance info %+v", &pvmoperation)
if operation == "stop" {
var targetStatus = "SHUTOFF"
log.Printf("Calling the check status operation to check for status %s", targetStatus)
_, err = isWaitForPIInstanceOperationStatus(client, name, d.Timeout(schema.TimeoutCreate), powerinstanceid, operation, targetStatus)
if err != nil {
return err
}
}
if operation == "start" || operation == "soft-reboot" || operation == "hard-reboot" {
var targetStatus = "ACTIVE"
log.Printf("Calling the check status operation to check for status %s", 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 id %s", d.Get(helpers.PICloudInstanceId).(string))
sess, err := meta.(ClientSession).IBMPISession()
if err != nil {
return err
}
powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)
powerC := st.NewIBMPIInstanceClient(sess, powerinstanceid)
powervmdata, err := powerC.Get(d.Id(), powerinstanceid)
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)
}
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.(ClientSession).IBMPISession()
if err != nil {
return false, err
}
id := d.Id()
powerinstanceid := d.Get(helpers.PICloudInstanceId).(string)
client := st.NewIBMPIInstanceClient(sess, powerinstanceid)
instance, err := client.Get(d.Id(), powerinstanceid)
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)", name, operation)
stateConf := &resource.StateChangeConf{
Pending: []string{"ACTIVE", "SHUTOFF", helpers.PIInstanceHealthWarning},
Target: []string{targetstatus},
Refresh: isPIOperationsRefreshFunc(client, name, powerinstanceid, targetstatus),
Delay: 1 * time.Minute,
MinTimeout: 30 * time.Second,
Timeout: 30 * 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, powerinstanceid)
if err != nil {
return nil, "", err
}
if *pvm.Status == targetstatus {
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
}
}