-
Notifications
You must be signed in to change notification settings - Fork 650
/
resource_ibm_pi_instance_action.go
220 lines (182 loc) · 6.79 KB
/
resource_ibm_pi_instance_action.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
// Copyright IBM Corp. 2022 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/power/models"
"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/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"log"
"time"
)
func ResourceIBMPIInstanceAction() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMPIInstanceActionCreate,
ReadContext: resourceIBMPIInstanceActionRead,
UpdateContext: resourceIBMPIInstanceActionUpdate,
DeleteContext: resourceIBMPIInstanceActionDelete,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(15 * time.Minute),
Update: schema.DefaultTimeout(15 * time.Minute),
},
Schema: map[string]*schema.Schema{
// Arguments
Arg_CloudInstanceID: {
Type: schema.TypeString,
Required: true,
Description: "PI Cloud instance id",
},
Arg_PVMInstanceId: {
Type: schema.TypeString,
Required: true,
Description: "PVM instance ID",
},
Arg_PVMInstanceActionType: {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{"start", "stop", "hard-reboot", "soft-reboot", "immediate-shutdown", "reset-state"}),
Description: "PVM instance action type",
},
Arg_PVMInstanceHealthStatus: {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{PVMInstanceHealthOk, PVMInstanceHealthWarning}),
Default: PVMInstanceHealthOk,
Description: "Set the health status of the PVM instance to connect it faster",
},
// Computed
Attr_Status: {
Type: schema.TypeString,
Computed: true,
Description: "The status of the PVM instance",
},
Attr_Progress: {
Type: schema.TypeFloat,
Computed: true,
Description: "The progress of an operation",
},
Attr_HealthStatus: {
Type: schema.TypeString,
Computed: true,
Description: "The PVM's health status value",
},
},
}
}
func resourceIBMPIInstanceActionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
adiag := takeInstanceAction(ctx, d, meta, d.Timeout(schema.TimeoutCreate))
if adiag != nil {
return adiag
}
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
id := d.Get(Arg_PVMInstanceId).(string)
d.SetId(fmt.Sprintf("%s/%s", cloudInstanceID, id))
return resourceIBMPIInstanceActionRead(ctx, d, meta)
}
func resourceIBMPIInstanceActionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID, id, err := splitID(d.Id())
if err != nil {
return diag.FromErr(err)
}
client := st.NewIBMPIInstanceClient(context.Background(), sess, cloudInstanceID)
powervmdata, err := client.Get(id)
if err != nil {
return diag.FromErr(err)
}
d.Set(Attr_Status, powervmdata.Status)
d.Set(Attr_Progress, powervmdata.Progress)
if powervmdata.Health != nil {
d.Set(Attr_HealthStatus, powervmdata.Health.Status)
}
return nil
}
func resourceIBMPIInstanceActionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if d.HasChange(Arg_PVMInstanceActionType) {
takeInstanceAction(ctx, d, meta, d.Timeout(schema.TimeoutUpdate))
}
return resourceIBMPIInstanceActionRead(ctx, d, meta)
}
func resourceIBMPIInstanceActionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
// There is no delete or unset concept for instance action
d.SetId("")
return nil
}
func takeInstanceAction(ctx context.Context, d *schema.ResourceData, meta interface{}, timeout time.Duration) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
id := d.Get(Arg_PVMInstanceId).(string)
action := d.Get(Arg_PVMInstanceActionType).(string)
targetHealthStatus := d.Get(Arg_PVMInstanceHealthStatus).(string)
body := &models.PVMInstanceAction{Action: &action}
log.Printf("Calling the IBM PI Action %s on the instance %s", action, id)
client := st.NewIBMPIInstanceClient(ctx, sess, cloudInstanceID)
err = client.Action(id, body)
if err != nil {
log.Printf("[ERROR] failed to perform the action on the instance %v", err)
return diag.FromErr(err)
}
log.Printf("Executed the action on the instance")
var targetStatus string
if action == "stop" || action == "immediate-shutdown" {
targetStatus = "SHUTOFF"
} else if action == "reset-state" {
targetStatus = "ACTIVE"
targetHealthStatus = "CRITICAL"
} else {
// action is "start" or "soft-reboot" or "hard-reboot"
targetStatus = "ACTIVE"
}
log.Printf("Calling the check for %s opertion to check for status %s", action, targetStatus)
_, err = isWaitForPIInstanceActionStatus(ctx, client, id, timeout, targetStatus, targetHealthStatus)
if err != nil {
return diag.FromErr(err)
}
return nil
}
func isWaitForPIInstanceActionStatus(ctx context.Context, client *st.IBMPIInstanceClient, id string, timeout time.Duration, targetStatus, targetHealthStatus string) (interface{}, error) {
log.Printf("Waiting for the action to be performed on the instance %s", id)
stateConf := &resource.StateChangeConf{
Pending: []string{StatusPending},
Target: []string{targetStatus, StatusError, ""},
Refresh: isPIActionRefreshFunc(client, id, targetStatus, targetHealthStatus),
Delay: 30 * time.Second,
MinTimeout: 2 * time.Minute,
Timeout: 15 * time.Minute,
}
return stateConf.WaitForStateContext(ctx)
}
func isPIActionRefreshFunc(client *st.IBMPIInstanceClient, id, targetStatus, targetHealthStatus 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 == targetHealthStatus || pvm.Health.Status == PVMInstanceHealthOk) {
log.Printf("The health status is now %s", pvm.Health.Status)
return pvm, targetStatus, nil
}
if *pvm.Status == "ERROR" {
if pvm.Fault != nil {
err = fmt.Errorf("failed to perform the action on the instance: %s", pvm.Fault.Message)
} else {
err = fmt.Errorf("failed to perform the action on the instance")
}
return pvm, *pvm.Status, err
}
return pvm, StatusPending, nil
}
}