forked from IBM-Cloud/terraform-provider-ibm
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c7c7d9
commit 22c741a
Showing
12 changed files
with
430 additions
and
4 deletions.
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
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
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 |
---|---|---|
@@ -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 | ||
} |
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 |
---|---|---|
@@ -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) | ||
} |
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 |
---|---|---|
@@ -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 | ||
} |
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 |
---|---|---|
@@ -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) | ||
} |
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.