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
f19d2b8
commit 134dce3
Showing
8 changed files
with
350 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright IBM Corp. 2024 All Rights Reserved. | ||
// Licensed under the Mozilla Public License v2.0 | ||
|
||
package power | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"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" | ||
|
||
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" | ||
) | ||
|
||
func ResourceIBMPIVolumesDelete() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceIBMPIVolumesDeleteCreate, | ||
ReadContext: resourceIBMPIVolumesDeleteRead, | ||
DeleteContext: resourceIBMPIVolumesDeleteDelete, | ||
Importer: &schema.ResourceImporter{}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Delete: schema.DefaultTimeout(30 * time.Minute), | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
Arg_CloudInstanceID: { | ||
Description: "The GUID of the service instance associated with an account.", | ||
ForceNew: true, | ||
Required: true, | ||
Type: schema.TypeString, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
Arg_VolumeIDs: { | ||
Description: "List of volumes to be deleted.", | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
ForceNew: true, | ||
Required: true, | ||
Type: schema.TypeList, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceIBMPIVolumesDeleteCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
sess, err := meta.(conns.ClientSession).IBMPISession() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string) | ||
var volumeIDs []string | ||
for _, v := range d.Get(Arg_VolumeIDs).([]interface{}) { | ||
volumeIDsItem := v.(string) | ||
volumeIDs = append(volumeIDs, volumeIDsItem) | ||
} | ||
body := &models.VolumesDelete{ | ||
VolumeIDs: volumeIDs, | ||
} | ||
volClient := st.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID) | ||
volinfo, err := volClient.BulkVolumeDelete(body) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
log.Printf("[DEBUG] Volumes delete accepted: %s", volinfo.Summary) | ||
|
||
for _, v := range d.Get(Arg_VolumeIDs).([]interface{}) { | ||
_, err = isWaitForIBMPIVolumeDeleted(ctx, volClient, v.(string), d.Timeout(schema.TimeoutDelete)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
} | ||
d.SetId(fmt.Sprintf("%s/%s", cloudInstanceID, strings.Join(volumeIDs, "/"))) | ||
|
||
return nil | ||
} | ||
|
||
func resourceIBMPIVolumesDeleteRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
return nil | ||
} | ||
|
||
func resourceIBMPIVolumesDeleteDelete(context 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,71 @@ | ||
// Copyright IBM Corp. 2024 All Rights Reserved. | ||
// Licensed under the Mozilla Public License v2.0 | ||
|
||
package power_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"testing" | ||
|
||
"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" | ||
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" | ||
) | ||
|
||
func TestAccIBMPIVolumesDeleteBasic(t *testing.T) { | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acc.TestAccPreCheck(t) }, | ||
Providers: acc.TestAccProviders, | ||
CheckDestroy: testAccCheckIBMPIVolumesDeleteDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccCheckIBMPIVolumesDeleteConfigBasic(), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr("ibm_pi_volumes_delete.volumes_delete", "pi_cloud_instance_id", acc.Pi_cloud_instance_id), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckIBMPIVolumesDeleteConfigBasic() string { | ||
return fmt.Sprintf(` | ||
resource "ibm_pi_volumes_delete" "volumes_delete" { | ||
pi_cloud_instance_id = "%s" | ||
pi_volume_ids = ["c1dc81e9-85f0-4e32-8c58-f22cc96a0037", "61b26eb2-50be-40b4-bda7-3c9b1662a742"] | ||
} | ||
`, acc.Pi_cloud_instance_id) | ||
} | ||
|
||
func testAccCheckIBMPIVolumesDeleteDestroy(s *terraform.State) error { | ||
sess, err := acc.TestAccProvider.Meta().(conns.ClientSession).IBMPISession() | ||
if err != nil { | ||
return err | ||
} | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "ibm_pi_volumes_delete" { | ||
continue | ||
} | ||
parts, err := flex.IdParts(rs.Primary.ID) | ||
if err != nil { | ||
return err | ||
} | ||
volumeC := st.NewIBMPIVolumeClient(context.Background(), sess, parts[0]) | ||
for _, volumeID := range parts[1:] { | ||
volume, err := volumeC.Get(volumeID) | ||
if err == nil { | ||
log.Println("volume*****", volume.State) | ||
return fmt.Errorf("PI Volume still exists: %s", rs.Primary.ID) | ||
} | ||
} | ||
} | ||
|
||
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,49 @@ | ||
--- | ||
subcategory: "Power Systems" | ||
layout: "ibm" | ||
page_title: "IBM : ibm_pi_volumes_attach" | ||
description: |- | ||
Manages volumes in Power Virtual Server cloud. | ||
--- | ||
|
||
# ibm_pi_volumes_delete | ||
|
||
Delete multiple volumes for a Power Systems Virtual Server resources, see [volumes](https://cloud.ibm.com/apidocs/power-cloud#pcloud-v2-pvminstances-volumes-post) | ||
|
||
## Example Usage | ||
|
||
```terraform | ||
resource "ibm_pi_volumes_delete" "pi_volumes_delete_instance" { | ||
pi_cloud_instance_id = "<value of the cloud_instance_id>" | ||
pi_volume_ids = ["600850d6-4b38-40cf-857d-123456","d783bb14-efeb-4ded-9847-123456"] | ||
} | ||
``` | ||
|
||
## Notes | ||
|
||
* Please find [supported Regions](https://cloud.ibm.com/apidocs/power-cloud#endpoint) for endpoints. | ||
* If a Power cloud instance is provisioned at `lon04`, The provider level attributes should be as follows: | ||
* `region` - `lon` | ||
* `zone` - `lon04` | ||
|
||
Example usage: | ||
|
||
```terraform | ||
provider "ibm" { | ||
region = "lon" | ||
zone = "lon04" | ||
} | ||
``` | ||
|
||
## Timeouts | ||
|
||
pi_volumes_attach provides the following [Timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) configuration options: | ||
|
||
* `delete` - (Default 30 minutes) Used for deleting volumes in power virtual server. | ||
|
||
## Argument Reference | ||
|
||
You can specify the following arguments for this resource. | ||
|
||
* `pi_cloud_instance_id` - (Required, String) The GUID of the service instance associated with an account. | ||
* `pi_volume_ids` - (Required, List) List of volumes to be deleted. |