Skip to content

Commit

Permalink
Add Volumes Bulk Attach, Detach
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkad committed Apr 26, 2024
1 parent 6c7c7d9 commit 22c741a
Show file tree
Hide file tree
Showing 12 changed files with 430 additions and 4 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240216115622-a311507b4b5b
github.com/IBM-Cloud/power-go-client v1.6.0
github.com/IBM-Cloud/power-go-client v1.6.0-beta10
github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca
github.com/IBM/appconfiguration-go-admin-sdk v0.3.0
github.com/IBM/appid-management-go-sdk v0.0.0-20210908164609-dd0e0eaf732f
Expand Down Expand Up @@ -65,6 +65,7 @@ require (
github.com/IBM/go-sdk-core v1.1.0
github.com/IBM/mqcloud-go-sdk v0.0.4
github.com/IBM/sarama v1.41.2
github.com/IBM/vmware-go-sdk v0.1.2
github.com/stretchr/testify v1.9.0
k8s.io/utils v0.0.0-20230313181309-38a27ef9d749
sigs.k8s.io/controller-runtime v0.14.1
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ github.com/IBM-Cloud/bluemix-go v0.0.0-20240402122236-5c5b994a3f1a/go.mod h1:/7h
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240216115622-a311507b4b5b h1:Wnq0BuprazpP41+nQlRpxpmAs8+8jyOqU50KrvFdJQ4=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240216115622-a311507b4b5b/go.mod h1:xUQL9SGAjoZFd4GNjrjjtEpjpkgU7RFXRyHesbKTjiY=
github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.5.3/go.mod h1:RiUvKuHKTBmBApDMUQzBL14pQUGKcx/IioKQPIcRQjs=
github.com/IBM-Cloud/power-go-client v1.6.0 h1:X+QX+WSF66+aouyaf4r+IeBLXUurAJj9+Bd+vH7G5I0=
github.com/IBM-Cloud/power-go-client v1.6.0/go.mod h1:0ad5Lcq1utoYVJx0uqooMjCpUaYaK0ItP9QJYtY6k0Y=
github.com/IBM-Cloud/power-go-client v1.6.0-beta10 h1:8Y1qyzBwYYN2MmgAuGFrHytltD8xtJLEbFvHZG6P6Xk=
github.com/IBM-Cloud/power-go-client v1.6.0-beta10/go.mod h1:bvErc8Wjz3yBlL+BHJ+ScuzuJtR+5EFasMvusluF+Yc=
github.com/IBM-Cloud/softlayer-go v1.0.5-tf h1:koUAyF9b6X78lLLruGYPSOmrfY2YcGYKOj/Ug9nbKNw=
github.com/IBM-Cloud/softlayer-go v1.0.5-tf/go.mod h1:6HepcfAXROz0Rf63krk5hPZyHT6qyx2MNvYyHof7ik4=
github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca h1:crniVcf+YcmgF03NmmfonXwSQ73oJF+IohFYBwknMxs=
Expand Down Expand Up @@ -172,6 +172,8 @@ github.com/IBM/schematics-go-sdk v0.2.3 h1:lgTt0Sbudii3cuSk1YSQgrtiZAXDbBABAoVj3
github.com/IBM/schematics-go-sdk v0.2.3/go.mod h1:Tw2OSAPdpC69AxcwoyqcYYaGTTW6YpERF9uNEU+BFRQ=
github.com/IBM/secrets-manager-go-sdk/v2 v2.0.4 h1:xa9e+POVqaXxXHXkSMCOVAbKdUNEu86jQmo5hcpd+L4=
github.com/IBM/secrets-manager-go-sdk/v2 v2.0.4/go.mod h1:5gq8D8uWOIbqOm1uztay6lpOysgJaxxEsaVZLWGWb40=
github.com/IBM/vmware-go-sdk v0.1.2 h1:5lKWFyInWz9e2hwGsoFTEoLa1jYkD30SReN0fQ10w9M=
github.com/IBM/vmware-go-sdk v0.1.2/go.mod h1:2UGPBJju3jiv5VKKBBm9a5L6bzF/aJdKOKAzJ7HaOjA=
github.com/IBM/vpc-beta-go-sdk v0.6.0 h1:wfM3AcW3zOM3xsRtZ+EA6+sESlGUjQ6Yf4n5QQyz4uc=
github.com/IBM/vpc-beta-go-sdk v0.6.0/go.mod h1:fzHDAQIqH/5yJmYsKodKHLcqxMDT+yfH6vZjdiw8CQA=
github.com/IBM/vpc-go-sdk v0.50.0 h1:+vnXYK0FXFXYqaS/5/X1XEqH0bbRotkzkerRk21ZEjE=
Expand Down
6 changes: 6 additions & 0 deletions ibm/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ var (
Pi_target_storage_tier string
Pi_volume_clone_task_id string
Pi_resource_group_id string
Pi_instance_id string
)

var (
Expand Down Expand Up @@ -1074,6 +1075,11 @@ func init() {
fmt.Println("[INFO] Set the environment variable PI_PVM_INSTANCE_ID for testing Pi_instance_name resource else it is set to default value 'terraform-test-power'")
}

Pi_instance_id = os.Getenv("PI_PVM_INSTANCE_ID")
if Pi_instance_id == "" {
fmt.Println("[INFO] Set the environment variable PI_PVM_INSTANCE_ID for testing resource else it is not set")
}

Pi_dhcp_id = os.Getenv("PI_DHCP_ID")
if Pi_dhcp_id == "" {
Pi_dhcp_id = "terraform-test-power"
Expand Down
2 changes: 2 additions & 0 deletions ibm/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1219,6 +1219,8 @@ func Provider() *schema.Provider {
"ibm_pi_volume_group": power.ResourceIBMPIVolumeGroup(),
"ibm_pi_volume_onboarding": power.ResourceIBMPIVolumeOnboarding(),
"ibm_pi_volume": power.ResourceIBMPIVolume(),
"ibm_pi_volumes_attach": power.ResourceIBMPIVolumesAttach(),
"ibm_pi_volumes_detach": power.ResourceIBMPIVolumesDetach(),
"ibm_pi_vpn_connection": power.ResourceIBMPIVPNConnection(),
"ibm_pi_workspace": power.ResourceIBMPIWorkspace(),

Expand Down
5 changes: 5 additions & 0 deletions ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import "time"

const (
// Arguments
Arg_BootVolumeID = "pi_boot_volume_id"
Arg_CloudConnectionName = "pi_cloud_connection_name"
Arg_CloudInstanceID = "pi_cloud_instance_id"
Arg_DatacenterZone = "pi_datacenter_zone"
Arg_DetachAllVolumes = "pi_detach_all_volumes"
Arg_DetachPrimaryBootVolume = "pi_detach_primary_boot_volume"
Arg_DhcpCidr = "pi_cidr"
Arg_DhcpCloudConnectionID = "pi_cloud_connection_id"
Arg_DhcpDnsServer = "pi_dns_server"
Expand All @@ -17,6 +20,7 @@ const (
Arg_IBMiPHA = "pi_ibmi_pha"
Arg_IBMiRDSUsers = "pi_ibmi_rds_users"
Arg_ImageName = "pi_image_name"
Arg_InstanceID = "pi_instance_id"
Arg_InstanceName = "pi_instance_name"
Arg_KeyName = "pi_key_name"
Arg_NetworkName = "pi_network_name"
Expand All @@ -41,6 +45,7 @@ const (
Arg_StorageType = "pi_storage_type"
Arg_VolumeGroupID = "pi_volume_group_id"
Arg_VolumeID = "pi_volume_id"
Arg_VolumeIDs = "pi_volume_ids"
Arg_VolumeName = "pi_volume_name"
Arg_VolumeOnboardingID = "pi_volume_onboarding_id"
Arg_VTL = "vtl"
Expand Down
102 changes: 102 additions & 0 deletions ibm/service/power/resource_ibm_pi_volumes_attach.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package power

import (
"context"
"fmt"
"log"

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/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func ResourceIBMPIVolumesAttach() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMPIVolumesAttachCreate,
ReadContext: resourceIBMPIVolumesAttachRead,
DeleteContext: resourceIBMPIVolumesAttachDelete,
Importer: &schema.ResourceImporter{},

Schema: map[string]*schema.Schema{
// Arguments
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_InstanceID: {
Description: "The unique identifier of the instance.",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_VolumeIDs: {
Description: "List of volumes to be detached from a pi_instance; required if detachAllVolumes is not provided.",
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true,
Required: true,
Type: schema.TypeList,
ValidateFunc: validation.NoZeroValues,
},
Arg_BootVolumeID: {
Description: "Primary Boot Volume Id.",
ForceNew: true,
Optional: true,
Type: schema.TypeString,
},
},
}
}

func resourceIBMPIVolumesAttachCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}

body := &models.VolumesAttach{}
pvmInstanceID := d.Get(Arg_InstanceID).(string)
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)

var volumeIDs []string
if _, ok := d.GetOk(Arg_VolumeIDs); ok {
for _, v := range d.Get(Arg_VolumeIDs).([]interface{}) {
volumeIDsItem := v.(string)
volumeIDs = append(volumeIDs, volumeIDsItem)
}
body.VolumeIDs = volumeIDs
}

if _, ok := d.GetOk(Arg_BootVolumeID); ok {
body.BootVolumeID = d.Get(Arg_BootVolumeID).(string)
}
volClient := st.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID)
volinfo, err := volClient.BulkVolumeAttach(pvmInstanceID, body)
if err != nil {
return diag.FromErr(err)
}

log.Printf("[DEBUG] Volumes attach accepted: %s", *volinfo.Summary)

d.SetId(fmt.Sprintf("%s/%s", cloudInstanceID, pvmInstanceID))

return resourceIBMPIVolumesAttachRead(ctx, d, meta)
}

func resourceIBMPIVolumesAttachRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return nil
}

func resourceIBMPIVolumesAttachDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
d.SetId("")
return nil
}
37 changes: 37 additions & 0 deletions ibm/service/power/resource_ibm_pi_volumes_attach_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package power_test

import (
"fmt"
"testing"

acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccIBMPIVolumesAttach(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPIVolumesAttachConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("ibm_pi_volumes_attach.pi_volumes_attach_instance", "id"),
),
},
},
})
}

func testAccCheckIBMPIVolumesAttachConfig() string {
return fmt.Sprintf(`
resource "ibm_pi_volumes_attach" "pi_volumes_attach_instance" {
pi_cloud_instance_id = "%[1]s"
pi_instance_id = "%[2]s"
pi_volume_ids = ["6679e1ef-d98a-4487-b2a6-51c242136884"]
}
`, acc.Pi_cloud_instance_id, acc.Pi_instance_id)
}
113 changes: 113 additions & 0 deletions ibm/service/power/resource_ibm_pi_volumes_detach.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package power

import (
"context"
"fmt"
"log"

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/flex"
"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"
)

func ResourceIBMPIVolumesDetach() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMPIVolumesDetachCreate,
ReadContext: resourceIBMPIVolumesDetachRead,
DeleteContext: resourceIBMPIVolumesDetachDelete,
Importer: &schema.ResourceImporter{},

Schema: map[string]*schema.Schema{
// Arguments
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_InstanceID: {
Description: "The unique identifier of the instance.",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_DetachAllVolumes: {
ConflictsWith: []string{Arg_VolumeIDs},
Description: "Indicates if all volumes, except primary boot volume, attached to the pi_instance should be detached (default=false); required if volumeIDs is not provided.",
ForceNew: true,
Optional: true,
Type: schema.TypeBool,
},
Arg_DetachPrimaryBootVolume: {
Description: "Indicates if primary boot volume attached to the pi_instance should be detached (default=false).",
ForceNew: true,
Optional: true,
Type: schema.TypeBool,
},
Arg_VolumeIDs: {
ConflictsWith: []string{Arg_DetachAllVolumes},
Description: "List of volumes to be detached from a pi_instance; required if detachAllVolumes is not provided.",
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true,
Optional: true,
Type: schema.TypeList,
},
},
}
}

func resourceIBMPIVolumesDetachCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}

pvmInstanceID := d.Get(Arg_InstanceID).(string)
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
body := &models.VolumesDetach{}
var volumeIDs []string
if _, ok := d.GetOk(Arg_VolumeIDs); ok {
for _, v := range d.Get(Arg_VolumeIDs).([]interface{}) {
volumeIDsItem := v.(string)
volumeIDs = append(volumeIDs, volumeIDsItem)
}
body.VolumeIDs = volumeIDs
}

if _, ok := d.GetOk(Arg_DetachAllVolumes); ok {
body.DetachAllVolumes = flex.PtrToBool(d.Get(Arg_DetachAllVolumes).(bool))
}
if _, ok := d.GetOk(Arg_DetachPrimaryBootVolume); ok {
body.DetachPrimaryBootVolume = flex.PtrToBool(d.Get(Arg_DetachPrimaryBootVolume).(bool))
}

volClient := st.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID)
volinfo, err := volClient.BulkVolumeDetach(pvmInstanceID, body)
if err != nil {
return diag.FromErr(err)
}

log.Printf("[DEBUG] Volumes detach accepted: %s", *volinfo.Summary)

d.SetId(fmt.Sprintf("%s/%s", cloudInstanceID, pvmInstanceID))

return resourceIBMPIVolumesAttachRead(ctx, d, meta)
}

func resourceIBMPIVolumesDetachRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return nil
}

func resourceIBMPIVolumesDetachDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
d.SetId("")
return nil
}
37 changes: 37 additions & 0 deletions ibm/service/power/resource_ibm_pi_volumes_detach_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package power_test

import (
"fmt"
"testing"

acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccIBMPIVolumesDetach(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPIVolumesDetachConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("ibm_pi_volumes_detach.pi_volumes_detach_instance", "id"),
),
},
},
})
}

func testAccCheckIBMPIVolumesDetachConfig() string {
return fmt.Sprintf(`
resource "ibm_pi_volumes_detach" "pi_volumes_detach_instance" {
pi_cloud_instance_id = "%[1]s"
pi_instance_id = "%[2]s"
pi_detach_all_volumes = true
}
`, acc.Pi_cloud_instance_id, acc.Pi_instance_id)
}
2 changes: 1 addition & 1 deletion website/docs/r/pi_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ Review the argument references that you can specify for your resource.
- `pi_sap_deployment_type` - (Optional, String) Custom SAP deployment type information (For Internal Use Only).
- `pi_shared_processor_pool` - (Optional, String) The shared processor pool for instance deployment. Conflicts with `pi_sap_profile_id`.
- `pi_storage_pool` - (Optional, String) Storage Pool for server deployment; if provided then `pi_affinity_policy` will be ignored; Only valid when you deploy one of the IBM supplied stock images. Storage pool for a custom image (an imported image or an image that is created from a VM capture) defaults to the storage pool the image was created in.
- `pi_storage_pool_affinity` - (Optional, Boolean) Indicates if all volumes attached to the server must reside in the same storage pool. The default value is `true`. To attach data volumes from a different storage pool (mixed storage) set to `false` and use `pi_volume_attach` resource. Once set to `false`, cannot be set back to `true` unless all volumes attached reside in the same storage type and pool.
- `pi_storage_pool_affinity` - (Optional, Boolean) Indicates if all volumes attached to the server must reside in the same storage pool. The default value is `true`. To attach data volumes from a different storage pool (mixed storage) set to `false` and use `ibm_pi_volume_attach` or `ibm_pi_volumes_attach` resource. Once set to `false`, cannot be set back to `true` unless all volumes attached reside in the same storage type and pool.
- `pi_storage_type` - (Optional, String) - Storage type for server deployment; If storage type is not provided the storage type will default to `tier3`.
- `pi_storage_connection` - (Optional, String) - Storage Connectivity Group (SCG) for server deployment. Only supported value is `vSCSI`.
- `pi_sys_type` - (Optional, String) The type of system on which to create the VM (s922/e880/e980/s1022).
Expand Down
Loading

0 comments on commit 22c741a

Please sign in to comment.