Skip to content

Commit 9e8d7e6

Browse files
authored
[Refactor] Volume Attach (IBM-Cloud#5912)
1 parent 1993b1a commit 9e8d7e6

File tree

4 files changed

+139
-131
lines changed

4 files changed

+139
-131
lines changed

ibm/service/power/ibm_pi_constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ const (
611611
State_Creating = "creating"
612612
State_Deleted = "deleted"
613613
State_Deleting = "deleting"
614+
State_Detaching = "detaching"
614615
State_Down = "down"
615616
State_Error = "error"
616617
State_ERROR = "ERROR"

ibm/service/power/resource_ibm_pi_volume_attach.go

Lines changed: 54 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
"log"
1111
"time"
1212

13-
st "github.com/IBM-Cloud/power-go-client/clients/instance"
14-
"github.com/IBM-Cloud/power-go-client/helpers"
13+
"github.com/IBM-Cloud/power-go-client/clients/instance"
1514
"github.com/IBM-Cloud/power-go-client/power/client/p_cloud_volumes"
1615
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
1716
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1817
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
19-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
2019
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
2121
)
2222

2323
func ResourceIBMPIVolumeAttach() *schema.Resource {
@@ -33,32 +33,32 @@ func ResourceIBMPIVolumeAttach() *schema.Resource {
3333
},
3434

3535
Schema: map[string]*schema.Schema{
36-
37-
helpers.PICloudInstanceId: {
38-
Type: schema.TypeString,
39-
Required: true,
40-
ForceNew: true,
41-
Description: " Cloud Instance ID - This is the service_instance_id.",
36+
// Arguments
37+
Arg_CloudInstanceID: {
38+
Description: "The GUID of the service instance associated with an account.",
39+
ForceNew: true,
40+
Required: true,
41+
Type: schema.TypeString,
42+
ValidateFunc: validation.NoZeroValues,
4243
},
43-
44-
helpers.PIVolumeId: {
45-
Type: schema.TypeString,
46-
Required: true,
44+
Arg_InstanceID: {
45+
Description: "PI Instance Id",
4746
ForceNew: true,
48-
Description: "Id of the volume to attach. Note these volumes should have been created",
49-
},
50-
51-
helpers.PIInstanceId: {
52-
Type: schema.TypeString,
5347
Required: true,
48+
Type: schema.TypeString,
49+
},
50+
Arg_VolumeID: {
51+
Description: "Id of the volume to attach. Note these volumes should have been created",
5452
ForceNew: true,
55-
Description: "PI Instance Id",
53+
Required: true,
54+
Type: schema.TypeString,
5655
},
5756

58-
// Computed Attribute
59-
helpers.PIVolumeAttachStatus: {
60-
Type: schema.TypeString,
61-
Computed: true,
57+
// Attribute
58+
Attr_Status: {
59+
Computed: true,
60+
Description: "The status of the volume.",
61+
Type: schema.TypeString,
6262
},
6363
},
6464
}
@@ -70,26 +70,26 @@ func resourceIBMPIVolumeAttachCreate(ctx context.Context, d *schema.ResourceData
7070
return diag.FromErr(err)
7171
}
7272

73-
volumeID := d.Get(helpers.PIVolumeId).(string)
74-
pvmInstanceID := d.Get(helpers.PIInstanceId).(string)
75-
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
73+
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
74+
pvmInstanceID := d.Get(Arg_InstanceID).(string)
75+
volumeID := d.Get(Arg_VolumeID).(string)
7676

77-
volClient := st.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID)
77+
volClient := instance.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID)
7878
volinfo, err := volClient.Get(volumeID)
7979
if err != nil {
8080
return diag.FromErr(err)
8181
}
8282

83-
if volinfo.State == "available" || *volinfo.Shareable {
83+
if volinfo.State == State_Available || *volinfo.Shareable {
8484
log.Printf(" In the current state the volume can be attached to the instance ")
8585
}
8686

87-
if volinfo.State == "in-use" && *volinfo.Shareable {
87+
if volinfo.State == State_InUse && *volinfo.Shareable {
8888

8989
log.Printf("Volume State /Status is permitted and hence attaching the volume to the instance")
9090
}
9191

92-
if volinfo.State == helpers.PIVolumeAllowableAttachStatus && !*volinfo.Shareable {
92+
if volinfo.State == State_InUse && !*volinfo.Shareable {
9393
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")
9494
}
9595

@@ -101,7 +101,7 @@ func resourceIBMPIVolumeAttachCreate(ctx context.Context, d *schema.ResourceData
101101

102102
d.SetId(fmt.Sprintf("%s/%s/%s", cloudInstanceID, pvmInstanceID, *volinfo.VolumeID))
103103

104-
_, err = isWaitForIBMPIVolumeAttachAvailable(ctx, volClient, *volinfo.VolumeID, cloudInstanceID, pvmInstanceID, d.Timeout(schema.TimeoutCreate))
104+
_, err = isWaitForIBMPIVolumeAttachAvailable(ctx, volClient, *volinfo.VolumeID, pvmInstanceID, d.Timeout(schema.TimeoutCreate))
105105
if err != nil {
106106
return diag.FromErr(err)
107107
}
@@ -121,14 +121,14 @@ func resourceIBMPIVolumeAttachRead(ctx context.Context, d *schema.ResourceData,
121121
}
122122
cloudInstanceID, pvmInstanceID, volumeID := ids[0], ids[1], ids[2]
123123

124-
client := st.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID)
124+
client := instance.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID)
125125

126126
vol, err := client.CheckVolumeAttach(pvmInstanceID, volumeID)
127127
if err != nil {
128128
return diag.FromErr(err)
129129
}
130130

131-
d.Set(helpers.PIVolumeAttachStatus, vol.State)
131+
d.Set(Attr_Status, vol.State)
132132
return nil
133133
}
134134

@@ -143,7 +143,7 @@ func resourceIBMPIVolumeAttachDelete(ctx context.Context, d *schema.ResourceData
143143
return diag.FromErr(err)
144144
}
145145
cloudInstanceID, pvmInstanceID, volumeID := ids[0], ids[1], ids[2]
146-
client := st.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID)
146+
client := instance.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID)
147147

148148
log.Printf("the id of the volume to detach is %s ", volumeID)
149149

@@ -160,7 +160,7 @@ func resourceIBMPIVolumeAttachDelete(ctx context.Context, d *schema.ResourceData
160160
return diag.FromErr(err)
161161
}
162162

163-
_, err = isWaitForIBMPIVolumeDetach(ctx, client, volumeID, cloudInstanceID, pvmInstanceID, d.Timeout(schema.TimeoutDelete))
163+
_, err = isWaitForIBMPIVolumeDetach(ctx, client, volumeID, pvmInstanceID, d.Timeout(schema.TimeoutDelete))
164164
if err != nil {
165165
return diag.FromErr(err)
166166
}
@@ -170,13 +170,13 @@ func resourceIBMPIVolumeAttachDelete(ctx context.Context, d *schema.ResourceData
170170
return nil
171171
}
172172

173-
func isWaitForIBMPIVolumeAttachAvailable(ctx context.Context, client *st.IBMPIVolumeClient, id, cloudInstanceID, pvmInstanceID string, timeout time.Duration) (interface{}, error) {
173+
func isWaitForIBMPIVolumeAttachAvailable(ctx context.Context, client *instance.IBMPIVolumeClient, id, pvmInstanceID string, timeout time.Duration) (interface{}, error) {
174174
log.Printf("Waiting for Volume (%s) to be available for attachment", id)
175175

176-
stateConf := &resource.StateChangeConf{
177-
Pending: []string{"retry", helpers.PIVolumeProvisioning},
178-
Target: []string{helpers.PIVolumeAllowableAttachStatus},
179-
Refresh: isIBMPIVolumeAttachRefreshFunc(client, id, cloudInstanceID, pvmInstanceID),
176+
stateConf := &retry.StateChangeConf{
177+
Pending: []string{State_Retry, State_Creating},
178+
Target: []string{State_InUse},
179+
Refresh: isIBMPIVolumeAttachRefreshFunc(client, id, pvmInstanceID),
180180
Delay: 10 * time.Second,
181181
MinTimeout: 30 * time.Second,
182182
Timeout: timeout,
@@ -185,28 +185,28 @@ func isWaitForIBMPIVolumeAttachAvailable(ctx context.Context, client *st.IBMPIVo
185185
return stateConf.WaitForStateContext(ctx)
186186
}
187187

188-
func isIBMPIVolumeAttachRefreshFunc(client *st.IBMPIVolumeClient, id, cloudInstanceID, pvmInstanceID string) resource.StateRefreshFunc {
188+
func isIBMPIVolumeAttachRefreshFunc(client *instance.IBMPIVolumeClient, id, pvmInstanceID string) retry.StateRefreshFunc {
189189
return func() (interface{}, string, error) {
190190
vol, err := client.Get(id)
191191
if err != nil {
192192
return nil, "", err
193193
}
194194

195-
if vol.State == "in-use" && flex.StringContains(vol.PvmInstanceIDs, pvmInstanceID) {
196-
return vol, helpers.PIVolumeAllowableAttachStatus, nil
195+
if vol.State == State_InUse && flex.StringContains(vol.PvmInstanceIDs, pvmInstanceID) {
196+
return vol, State_InUse, nil
197197
}
198198

199-
return vol, helpers.PIVolumeProvisioning, nil
199+
return vol, State_Creating, nil
200200
}
201201
}
202202

203-
func isWaitForIBMPIVolumeDetach(ctx context.Context, client *st.IBMPIVolumeClient, id, cloudInstanceID, pvmInstanceID string, timeout time.Duration) (interface{}, error) {
203+
func isWaitForIBMPIVolumeDetach(ctx context.Context, client *instance.IBMPIVolumeClient, id, pvmInstanceID string, timeout time.Duration) (interface{}, error) {
204204
log.Printf("Waiting for Volume (%s) to be available after detachment", id)
205205

206-
stateConf := &resource.StateChangeConf{
207-
Pending: []string{"detaching", helpers.PowerVolumeAttachDeleting},
208-
Target: []string{helpers.PIVolumeProvisioningDone},
209-
Refresh: isIBMPIVolumeDetachRefreshFunc(client, id, cloudInstanceID, pvmInstanceID),
206+
stateConf := &retry.StateChangeConf{
207+
Pending: []string{State_Detaching, State_Deleting},
208+
Target: []string{State_Available},
209+
Refresh: isIBMPIVolumeDetachRefreshFunc(client, id, pvmInstanceID),
210210
Delay: 10 * time.Second,
211211
MinTimeout: 30 * time.Second,
212212
Timeout: timeout,
@@ -215,15 +215,15 @@ func isWaitForIBMPIVolumeDetach(ctx context.Context, client *st.IBMPIVolumeClien
215215
return stateConf.WaitForStateContext(ctx)
216216
}
217217

218-
func isIBMPIVolumeDetachRefreshFunc(client *st.IBMPIVolumeClient, id, cloudInstanceID, pvmInstanceID string) resource.StateRefreshFunc {
218+
func isIBMPIVolumeDetachRefreshFunc(client *instance.IBMPIVolumeClient, id, pvmInstanceID string) retry.StateRefreshFunc {
219219
return func() (interface{}, string, error) {
220220
vol, err := client.Get(id)
221221
if err != nil {
222222
uErr := errors.Unwrap(err)
223223
switch uErr.(type) {
224224
case *p_cloud_volumes.PcloudCloudinstancesVolumesGetNotFound:
225225
log.Printf("[DEBUG] volume does not exist while detaching %v", err)
226-
return vol, helpers.PIVolumeProvisioningDone, nil
226+
return vol, State_Available, nil
227227
}
228228
return nil, "", err
229229
}
@@ -232,10 +232,10 @@ func isIBMPIVolumeDetachRefreshFunc(client *st.IBMPIVolumeClient, id, cloudInsta
232232
// Also validate the Volume state is 'available' when it is not Sharable
233233
// In case of Sharable Volume it can be `in-use` state
234234
if !flex.StringContains(vol.PvmInstanceIDs, pvmInstanceID) &&
235-
(*vol.Shareable || (!*vol.Shareable && vol.State == "available")) {
236-
return vol, helpers.PIVolumeProvisioningDone, nil
235+
(*vol.Shareable || (!*vol.Shareable && vol.State == State_Available)) {
236+
return vol, State_Available, nil
237237
}
238238

239-
return vol, "detaching", nil
239+
return vol, State_Detaching, nil
240240
}
241241
}

0 commit comments

Comments
 (0)