-
Notifications
You must be signed in to change notification settings - Fork 646
/
resource_ibm_pi_volume_attach.go
241 lines (197 loc) · 7.61 KB
/
resource_ibm_pi_volume_attach.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
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package power
import (
"context"
"errors"
"fmt"
"log"
"time"
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_volumes"
"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 ResourceIBMPIVolumeAttach() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMPIVolumeAttachCreate,
ReadContext: resourceIBMPIVolumeAttachRead,
DeleteContext: resourceIBMPIVolumeAttachDelete,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(15 * time.Minute),
Delete: schema.DefaultTimeout(15 * time.Minute),
},
Schema: map[string]*schema.Schema{
helpers.PICloudInstanceId: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: " Cloud Instance ID - This is the service_instance_id.",
},
helpers.PIVolumeId: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Id of the volume to attach. Note these volumes should have been created",
},
helpers.PIInstanceId: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "PI Instance Id",
},
// Computed Attribute
helpers.PIVolumeAttachStatus: {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceIBMPIVolumeAttachCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}
volumeID := d.Get(helpers.PIVolumeId).(string)
pvmInstanceID := d.Get(helpers.PIInstanceId).(string)
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
volClient := st.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID)
volinfo, err := volClient.Get(volumeID)
if err != nil {
return diag.FromErr(err)
}
if volinfo.State == "available" || *volinfo.Shareable {
log.Printf(" In the current state the volume can be attached to the instance ")
}
if volinfo.State == "in-use" && *volinfo.Shareable {
log.Printf("Volume State /Status is permitted and hence attaching the volume to the instance")
}
if volinfo.State == helpers.PIVolumeAllowableAttachStatus && !*volinfo.Shareable {
return diag.Errorf("the volume cannot be attached in the current state. The volume must be in the *available* state. No other states are permissible")
}
err = volClient.Attach(pvmInstanceID, volumeID)
if err != nil {
log.Printf("[DEBUG] err %s", err)
return diag.FromErr(err)
}
d.SetId(fmt.Sprintf("%s/%s/%s", cloudInstanceID, pvmInstanceID, *volinfo.VolumeID))
_, err = isWaitForIBMPIVolumeAttachAvailable(ctx, volClient, *volinfo.VolumeID, cloudInstanceID, pvmInstanceID, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.FromErr(err)
}
return resourceIBMPIVolumeAttachRead(ctx, d, meta)
}
func resourceIBMPIVolumeAttachRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}
ids, err := flex.IdParts(d.Id())
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID, pvmInstanceID, volumeID := ids[0], ids[1], ids[2]
client := st.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID)
vol, err := client.CheckVolumeAttach(pvmInstanceID, volumeID)
if err != nil {
return diag.FromErr(err)
}
d.Set(helpers.PIVolumeAttachStatus, vol.State)
return nil
}
func resourceIBMPIVolumeAttachDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}
ids, err := flex.IdParts(d.Id())
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID, pvmInstanceID, volumeID := ids[0], ids[1], ids[2]
client := st.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID)
log.Printf("the id of the volume to detach is %s ", volumeID)
err = client.Detach(pvmInstanceID, volumeID)
if err != nil {
uErr := errors.Unwrap(err)
switch uErr.(type) {
case *p_cloud_volumes.PcloudCloudinstancesVolumesGetNotFound:
log.Printf("[DEBUG] volume does not exist while detaching %v", err)
d.SetId("")
return nil
}
log.Printf("[DEBUG] volume detach failed %v", err)
return diag.FromErr(err)
}
_, err = isWaitForIBMPIVolumeDetach(ctx, client, volumeID, cloudInstanceID, pvmInstanceID, d.Timeout(schema.TimeoutDelete))
if err != nil {
return diag.FromErr(err)
}
// wait for power volume states to be back as available. if it's attached it will be in-use
d.SetId("")
return nil
}
func isWaitForIBMPIVolumeAttachAvailable(ctx context.Context, client *st.IBMPIVolumeClient, id, cloudInstanceID, pvmInstanceID string, timeout time.Duration) (interface{}, error) {
log.Printf("Waiting for Volume (%s) to be available for attachment", id)
stateConf := &resource.StateChangeConf{
Pending: []string{"retry", helpers.PIVolumeProvisioning},
Target: []string{helpers.PIVolumeAllowableAttachStatus},
Refresh: isIBMPIVolumeAttachRefreshFunc(client, id, cloudInstanceID, pvmInstanceID),
Delay: 10 * time.Second,
MinTimeout: 30 * time.Second,
Timeout: timeout,
}
return stateConf.WaitForStateContext(ctx)
}
func isIBMPIVolumeAttachRefreshFunc(client *st.IBMPIVolumeClient, id, cloudInstanceID, pvmInstanceID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
vol, err := client.Get(id)
if err != nil {
return nil, "", err
}
if vol.State == "in-use" && flex.StringContains(vol.PvmInstanceIDs, pvmInstanceID) {
return vol, helpers.PIVolumeAllowableAttachStatus, nil
}
return vol, helpers.PIVolumeProvisioning, nil
}
}
func isWaitForIBMPIVolumeDetach(ctx context.Context, client *st.IBMPIVolumeClient, id, cloudInstanceID, pvmInstanceID string, timeout time.Duration) (interface{}, error) {
log.Printf("Waiting for Volume (%s) to be available after detachment", id)
stateConf := &resource.StateChangeConf{
Pending: []string{"detaching", helpers.PowerVolumeAttachDeleting},
Target: []string{helpers.PIVolumeProvisioningDone},
Refresh: isIBMPIVolumeDetachRefreshFunc(client, id, cloudInstanceID, pvmInstanceID),
Delay: 10 * time.Second,
MinTimeout: 30 * time.Second,
Timeout: timeout,
}
return stateConf.WaitForStateContext(ctx)
}
func isIBMPIVolumeDetachRefreshFunc(client *st.IBMPIVolumeClient, id, cloudInstanceID, pvmInstanceID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
vol, err := client.Get(id)
if err != nil {
uErr := errors.Unwrap(err)
switch uErr.(type) {
case *p_cloud_volumes.PcloudCloudinstancesVolumesGetNotFound:
log.Printf("[DEBUG] volume does not exist while detaching %v", err)
return vol, helpers.PIVolumeProvisioningDone, nil
}
return nil, "", err
}
// Check if Instance ID is in the Volume's Instance list
// Also validate the Volume state is 'available' when it is not Sharable
// In case of Sharable Volume it can be `in-use` state
if !flex.StringContains(vol.PvmInstanceIDs, pvmInstanceID) &&
(*vol.Shareable || (!*vol.Shareable && vol.State == "available")) {
return vol, helpers.PIVolumeProvisioningDone, nil
}
return vol, "detaching", nil
}
}