forked from IBM-Cloud/terraform-provider-ibm
-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Resource volume attach and documentation #89
Closed
+56
−55
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,7 @@ import ( | |
"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/clients/instance" | ||
"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" | ||
|
@@ -34,29 +33,29 @@ func ResourceIBMPIVolumeAttach() *schema.Resource { | |
|
||
Schema: map[string]*schema.Schema{ | ||
|
||
helpers.PICloudInstanceId: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Arg_CloudInstanceID: { | ||
Description: " Cloud Instance ID - This is the service_instance_id.", | ||
ForceNew: true, | ||
Required: true, | ||
Type: schema.TypeString, | ||
}, | ||
|
||
helpers.PIVolumeId: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Arg_VolumeID: { | ||
Description: "Id of the volume to attach. Note these volumes should have been created", | ||
ForceNew: true, | ||
Required: true, | ||
Type: schema.TypeString, | ||
}, | ||
|
||
helpers.PIInstanceId: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Arg_PVMInstanceId: { | ||
Description: "PI Instance Id", | ||
ForceNew: true, | ||
Required: true, | ||
Type: schema.TypeString, | ||
}, | ||
|
||
// Computed Attribute | ||
helpers.PIVolumeAttachStatus: { | ||
Attr_Status: { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
@@ -70,11 +69,11 @@ func resourceIBMPIVolumeAttachCreate(ctx context.Context, d *schema.ResourceData | |
return diag.FromErr(err) | ||
} | ||
|
||
volumeID := d.Get(helpers.PIVolumeId).(string) | ||
pvmInstanceID := d.Get(helpers.PIInstanceId).(string) | ||
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string) | ||
volumeID := d.Get(Arg_VolumeID).(string) | ||
pvmInstanceID := d.Get(Arg_PVMInstanceId).(string) | ||
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string) | ||
|
||
volClient := st.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID) | ||
volClient := instance.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID) | ||
volinfo, err := volClient.Get(volumeID) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
|
@@ -89,7 +88,7 @@ func resourceIBMPIVolumeAttachCreate(ctx context.Context, d *schema.ResourceData | |
log.Printf("Volume State /Status is permitted and hence attaching the volume to the instance") | ||
} | ||
|
||
if volinfo.State == helpers.PIVolumeAllowableAttachStatus && !*volinfo.Shareable { | ||
if volinfo.State == Attr_VolumeAllowableAttachStatus && !*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") | ||
} | ||
|
||
|
@@ -121,14 +120,14 @@ func resourceIBMPIVolumeAttachRead(ctx context.Context, d *schema.ResourceData, | |
} | ||
cloudInstanceID, pvmInstanceID, volumeID := ids[0], ids[1], ids[2] | ||
|
||
client := st.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID) | ||
client := instance.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID) | ||
|
||
vol, err := client.CheckVolumeAttach(pvmInstanceID, volumeID) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.Set(helpers.PIVolumeAttachStatus, vol.State) | ||
d.Set(Attr_Status, vol.State) | ||
return nil | ||
} | ||
|
||
|
@@ -143,7 +142,7 @@ func resourceIBMPIVolumeAttachDelete(ctx context.Context, d *schema.ResourceData | |
return diag.FromErr(err) | ||
} | ||
cloudInstanceID, pvmInstanceID, volumeID := ids[0], ids[1], ids[2] | ||
client := st.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID) | ||
client := instance.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID) | ||
|
||
log.Printf("the id of the volume to detach is %s ", volumeID) | ||
|
||
|
@@ -170,12 +169,12 @@ func resourceIBMPIVolumeAttachDelete(ctx context.Context, d *schema.ResourceData | |
return nil | ||
} | ||
|
||
func isWaitForIBMPIVolumeAttachAvailable(ctx context.Context, client *st.IBMPIVolumeClient, id, cloudInstanceID, pvmInstanceID string, timeout time.Duration) (interface{}, error) { | ||
func isWaitForIBMPIVolumeAttachAvailable(ctx context.Context, client *instance.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}, | ||
Pending: []string{"retry", Attr_VolumeProvisioning}, | ||
Target: []string{Attr_VolumeAllowableAttachStatus}, | ||
Comment on lines
+176
to
+177
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to update the const to be those
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changes made |
||
Refresh: isIBMPIVolumeAttachRefreshFunc(client, id, cloudInstanceID, pvmInstanceID), | ||
Delay: 10 * time.Second, | ||
MinTimeout: 30 * time.Second, | ||
|
@@ -185,27 +184,27 @@ func isWaitForIBMPIVolumeAttachAvailable(ctx context.Context, client *st.IBMPIVo | |
return stateConf.WaitForStateContext(ctx) | ||
} | ||
|
||
func isIBMPIVolumeAttachRefreshFunc(client *st.IBMPIVolumeClient, id, cloudInstanceID, pvmInstanceID string) resource.StateRefreshFunc { | ||
func isIBMPIVolumeAttachRefreshFunc(client *instance.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, Attr_VolumeAllowableAttachStatus, nil | ||
} | ||
|
||
return vol, helpers.PIVolumeProvisioning, nil | ||
return vol, Attr_VolumeProvisioning, nil | ||
} | ||
} | ||
|
||
func isWaitForIBMPIVolumeDetach(ctx context.Context, client *st.IBMPIVolumeClient, id, cloudInstanceID, pvmInstanceID string, timeout time.Duration) (interface{}, error) { | ||
func isWaitForIBMPIVolumeDetach(ctx context.Context, client *instance.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}, | ||
Pending: []string{"detaching", Attr_PowerVolumeAttachDeleting}, | ||
Target: []string{Attr_VolumeProvisioningDone}, | ||
Refresh: isIBMPIVolumeDetachRefreshFunc(client, id, cloudInstanceID, pvmInstanceID), | ||
Delay: 10 * time.Second, | ||
MinTimeout: 30 * time.Second, | ||
|
@@ -215,15 +214,15 @@ func isWaitForIBMPIVolumeDetach(ctx context.Context, client *st.IBMPIVolumeClien | |
return stateConf.WaitForStateContext(ctx) | ||
} | ||
|
||
func isIBMPIVolumeDetachRefreshFunc(client *st.IBMPIVolumeClient, id, cloudInstanceID, pvmInstanceID string) resource.StateRefreshFunc { | ||
func isIBMPIVolumeDetachRefreshFunc(client *instance.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 vol, Attr_VolumeProvisioningDone, nil | ||
} | ||
return nil, "", err | ||
} | ||
|
@@ -233,7 +232,7 @@ func isIBMPIVolumeDetachRefreshFunc(client *st.IBMPIVolumeClient, id, cloudInsta | |
// 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, Attr_VolumeProvisioningDone, nil | ||
} | ||
|
||
return vol, "detaching", nil | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The value of
PIVolumeAllowableAttachStatus = "in-use"
need to be updated