Skip to content
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] Volume Group Action #135

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const (
Arg_VirtualOpticalDevice = "pi_virtual_optical_device"
Arg_VolumeCloneName = "pi_volume_clone_name"
Arg_VolumeCloneTaskID = "pi_volume_clone_task_id"
Arg_VolumeGroupAction = "pi_volume_group_action"
Arg_VolumeGroupID = "pi_volume_group_id"
Arg_VolumeGroupName = "pi_volume_group_name"
Arg_VolumeID = "pi_volume_id"
Expand All @@ -119,10 +120,6 @@ const (
Arg_VolumeType = "pi_volume_type"
Arg_VTL = "vtl"

// Duplicate Arguments, will be removed as refactoring take course.
PIVolumeGroupID = "pi_volume_group_id"
PIVolumeOnboardingID = "pi_volume_onboarding_id"

// Attributes
Attr_Access = "access"
Attr_AccessConfig = "access_config"
Expand Down Expand Up @@ -341,6 +338,7 @@ const (
Attr_ReservedCore = "reserved_core"
Attr_ReservedCores = "reserved_cores"
Attr_ReservedMemory = "reserved_memory"
Attr_Reset = "reset"
Attr_ResultsOnboardedVolumes = "results_onboarded_volumes"
Attr_ResultsVolumeOnboardingFailures = "results_volume_onboarding_failures"
Attr_Rules = "rules"
Expand Down Expand Up @@ -373,6 +371,7 @@ const (
Attr_SourceChecksum = "source_checksum"
Attr_SourcePort = "source_port"
Attr_SourceVolumeID = "source_volume_id"
Attr_Source = "source"
Attr_SourceVolumeName = "source_volume_name"
Attr_Speed = "speed"
Attr_SPPPlacementGroupID = "spp_placement_group_id"
Expand All @@ -381,12 +380,14 @@ const (
Attr_SPPPlacementGroupPolicy = "policy"
Attr_SPPPlacementGroups = "spp_placement_groups"
Attr_SSHKey = "ssh_key"
Attr_Start = "start"
Attr_StartTime = "start_time"
Attr_State = "state"
Attr_Status = "status"
Attr_StatusDescriptionErrors = "status_description_errors"
Attr_StatusDetail = "status_detail"
Attr_StorageConnection = "storage_connection"
Attr_Stop = "stop"
Attr_StoragePool = "storage_pool"
Attr_StoragePoolAffinity = "storage_pool_affinity"
Attr_StoragePoolsCapacity = "storage_pools_capacity"
Expand Down Expand Up @@ -476,6 +477,7 @@ const (
Allow = "allow"
AntiAffinity = "anti-affinity"
Attach = "attach"
Aux = "aux"
BYOL = "byol"
Capped = "capped"
Critical = "CRITICAL"
Expand All @@ -497,8 +499,9 @@ const (
HostGroup = "hostGroup"
ICMP = "icmp"
IPV4_Address = "ipv4-address"
NAG = "network-address-group"
Master = "master"
MaxVolumeSupport = "maxVolumeSupport"
NAG = "network-address-group"
Netweaver = "Netweaver"
Network_Interface = "network-interface"
None = "none"
Expand Down Expand Up @@ -568,11 +571,6 @@ const (
State_Up = "up"
State_Updating = "updating"
State_VerifyResize = "verify_resize"
Status_Active = "ACTIVE"
Status_Deleting = "deleting"
Status_Error = "ERROR"
Status_Pending = "PENDING"
Status_Shutoff = "SHUTOFF"

// Timeout values
Timeout_Active = 2 * time.Minute
Expand All @@ -593,6 +591,7 @@ const (
PIVolumeGroupName = "pi_volume_group_name"
PIVolumeGroupConsistencyGroupName = "pi_consistency_group_name"
PIVolumeGroupAction = "pi_volume_group_action"
PIVolumeOnboardingID = "pi_volume_onboarding_id"

// VPN
PIVPNConnectionId = "connection_id"
Expand Down
147 changes: 78 additions & 69 deletions ibm/service/power/resource_ibm_pi_volume_group_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"fmt"
"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/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/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/softlayer/softlayer-go/sl"
)

Expand All @@ -30,90 +30,99 @@ func ResourceIBMPIVolumeGroupAction() *schema.Resource {
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.",
},
PIVolumeGroupID: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Volume Group ID",
// Arguments
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
PIVolumeGroupAction: {
Type: schema.TypeList,
Required: true,
ForceNew: true,
MaxItems: 1,
MinItems: 1,
Description: "Performs an action (start stop reset ) on a volume group(one at a time).",
Arg_VolumeGroupAction: {
Description: "Performs an action (start stop reset) on a volume group(one at a time).",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"start": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ForceNew: true,
Attr_Start: {
Description: "Performs start action on a volume group.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"source": {
Type: schema.TypeString,
Attr_Source: {
Description: "Indicates the source of the action `master` or `aux`.",
Required: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{"master", "aux"}),
Type: schema.TypeString,
ValidateFunc: validate.ValidateAllowedStringValues([]string{Master, Aux}),
},
},
},
},
"stop": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ForceNew: true,
MaxItems: 1,
Optional: true,
Type: schema.TypeList,
},
Attr_Stop: {
Description: "Performs stop action on a volume group.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"access": {
Type: schema.TypeBool,
Required: true,
Attr_Access: {
Description: "Indicates the access mode of aux volumes.",
Required: true,
Type: schema.TypeBool,
},
},
},
},
"reset": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
ForceNew: true,
MaxItems: 1,
Optional: true,
Type: schema.TypeList,
},
Attr_Reset: {
Description: "Performs reset action on the volume group to update its value.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"status": {
Type: schema.TypeString,
Attr_Status: {
Description: "New status to be set for a volume group.",
Required: true,
ValidateFunc: validate.ValidateAllowedStringValues([]string{"available"}),
Type: schema.TypeString,
ValidateFunc: validate.ValidateAllowedStringValues([]string{State_Available}),
},
},
},
ForceNew: true,
MaxItems: 1,
Optional: true,
Type: schema.TypeList,
},
},
},
ForceNew: true,
MaxItems: 1,
MinItems: 1,
Required: true,
Type: schema.TypeList,
},
Arg_VolumeGroupID: {
Description: "Volume Group ID",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},

// Computed Attributes
"volume_group_name": {
// Attributes
Attr_ReplicationStatus: {
Computed: true,
Description: "Volume Group Replication Status",
Type: schema.TypeString,
},
Attr_VolumeGroupName: {
Computed: true,
Description: "Volume Group ID",
},
"volume_group_status": {
Type: schema.TypeString,
},
Attr_VolumeGroupStatus: {
Computed: true,
Description: "Volume Group Status",
},
"replication_status": {
Type: schema.TypeString,
Computed: true,
Description: "Volume Group Replication Status",
},
},
}
Expand All @@ -125,16 +134,16 @@ func resourceIBMPIVolumeGroupActionCreate(ctx context.Context, d *schema.Resourc
return diag.FromErr(err)
}

vgID := d.Get(PIVolumeGroupID).(string)
vgAction, err := expandVolumeGroupAction(d.Get(PIVolumeGroupAction).([]interface{}))
vgID := d.Get(Arg_VolumeGroupID).(string)
vgAction, err := expandVolumeGroupAction(d.Get(Arg_VolumeGroupAction).([]interface{}))
if err != nil {
return diag.FromErr(err)
}

cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
body := vgAction

client := st.NewIBMPIVolumeGroupClient(ctx, sess, cloudInstanceID)
client := instance.NewIBMPIVolumeGroupClient(ctx, sess, cloudInstanceID)
_, err = client.VolumeGroupAction(vgID, body)
if err != nil {
return diag.FromErr(err)
Expand All @@ -161,16 +170,16 @@ func resourceIBMPIVolumeGroupActionRead(ctx context.Context, d *schema.ResourceD
return diag.FromErr(err)
}

client := st.NewIBMPIVolumeGroupClient(ctx, sess, cloudInstanceID)
client := instance.NewIBMPIVolumeGroupClient(ctx, sess, cloudInstanceID)

vg, err := client.GetDetails(vgID)
if err != nil {
return diag.FromErr(err)
}

d.Set("volume_group_name", vg.Name)
d.Set("volume_group_status", vg.Status)
d.Set("replication_status", vg.ReplicationStatus)
d.Set(Attr_VolumeGroupName, vg.Name)
d.Set(Attr_VolumeGroupStatus, vg.Status)
d.Set(Attr_ReplicationStatus, vg.ReplicationStatus)

return nil
}
Expand All @@ -190,18 +199,18 @@ func expandVolumeGroupAction(data []interface{}) (*models.VolumeGroupAction, err
vgAction := models.VolumeGroupAction{}
action := data[0].(map[string]interface{})

if v, ok := action["start"]; ok && len(v.([]interface{})) != 0 {
vgAction.Start = expandVolumeGroupStartAction(action["start"].([]interface{}))
if v, ok := action[Attr_Start]; ok && len(v.([]interface{})) != 0 {
vgAction.Start = expandVolumeGroupStartAction(action[Attr_Start].([]interface{}))
return &vgAction, nil
}

if v, ok := action["stop"]; ok && len(v.([]interface{})) != 0 {
vgAction.Stop = expandVolumeGroupStopAction(action["stop"].([]interface{}))
if v, ok := action[Attr_Stop]; ok && len(v.([]interface{})) != 0 {
vgAction.Stop = expandVolumeGroupStopAction(action[Attr_Stop].([]interface{}))
return &vgAction, nil
}

if v, ok := action["reset"]; ok && len(v.([]interface{})) != 0 {
vgAction.Reset = expandVolumeGroupResetAction(action["reset"].([]interface{}))
if v, ok := action[Attr_Reset]; ok && len(v.([]interface{})) != 0 {
vgAction.Reset = expandVolumeGroupResetAction(action[Attr_Reset].([]interface{}))
return &vgAction, nil
}
return nil, fmt.Errorf("[ERROR] no pi_volume_group_action received")
Expand All @@ -215,7 +224,7 @@ func expandVolumeGroupStartAction(start []interface{}) *models.VolumeGroupAction
s := start[0].(map[string]interface{})

return &models.VolumeGroupActionStart{
Source: sl.String(s["source"].(string)),
Source: sl.String(s[Attr_Source].(string)),
}
}

Expand All @@ -227,7 +236,7 @@ func expandVolumeGroupStopAction(stop []interface{}) *models.VolumeGroupActionSt
s := stop[0].(map[string]interface{})

return &models.VolumeGroupActionStop{
Access: sl.Bool(s["access"].(bool)),
Access: sl.Bool(s[Attr_Access].(bool)),
}
}

Expand All @@ -239,6 +248,6 @@ func expandVolumeGroupResetAction(reset []interface{}) *models.VolumeGroupAction
s := reset[0].(map[string]interface{})

return &models.VolumeGroupActionReset{
Status: sl.String(s["status"].(string)),
Status: sl.String(s[Attr_Status].(string)),
}
}
4 changes: 2 additions & 2 deletions ibm/service/power/resource_ibm_pi_volume_group_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

st "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/clients/instance"
acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
Expand Down Expand Up @@ -68,7 +68,7 @@ func testAccCheckIBMPIVolumeGroupActionExists(n string) resource.TestCheckFunc {
return err
}
cloudInstanceID, vgID := ids[0], ids[1]
client := st.NewIBMPIVolumeGroupClient(context.Background(), sess, cloudInstanceID)
client := instance.NewIBMPIVolumeGroupClient(context.Background(), sess, cloudInstanceID)

_, err = client.Get(vgID)
if err != nil {
Expand Down
Loading
Loading