Skip to content

Commit

Permalink
Add bulk volumes delete
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkad committed May 30, 2024
1 parent f19d2b8 commit 134dce3
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 33 deletions.
12 changes: 1 addition & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.22.3

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-beta14
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 @@ -77,7 +77,6 @@ require (
require (
cloud.google.com/go/kms v1.10.1 // indirect
cloud.google.com/go/monitoring v1.13.0 // indirect
github.com/Bowery/prompt v0.0.0-20190916142128-fa8279994f75 // indirect
github.com/Logicalis/asn1 v0.0.0-20190312173541-d60463189a56 // indirect
github.com/PromonLogicalis/asn1 v0.0.0-20190312173541-d60463189a56 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230828082145-3c4c8a2d2371 // indirect
Expand All @@ -96,7 +95,6 @@ require (
github.com/coreos/pkg v0.0.0-20220810130054-c7d1c02cb6cf // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dchest/bcrypt_pbkdf v0.0.0-20150205184540-83f37f9c154a // indirect
github.com/dchest/safefile v0.0.0-20151022103144-855e8d98f185 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/eapache/go-resiliency v1.4.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 // indirect
Expand Down Expand Up @@ -129,7 +127,6 @@ require (
github.com/google/gnostic v0.6.9 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand Down Expand Up @@ -170,7 +167,6 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kardianos/govendor v1.0.9 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/kube-object-storage/lib-bucket-provisioner v0.0.0-20221122204822-d1a8c34382f1 // indirect
github.com/leodido/go-urn v1.4.0 // indirect
Expand All @@ -182,8 +178,6 @@ require (
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/gox v1.0.1 // indirect
github.com/mitchellh/iochan v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/moby/spdystream v0.2.0 // indirect
Expand Down Expand Up @@ -224,9 +218,6 @@ require (
golang.org/x/term v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.16.1 // indirect
golang.org/x/tools/cmd/cover v0.1.0-deprecated // indirect
golang.org/x/tools/go/vcs v0.1.0-deprecated // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
Expand All @@ -250,7 +241,6 @@ replace github.com/dgrijalva/jwt-go v3.2.0+incompatible => github.com/golang-jwt
// add sdk changes.
replace github.com/portworx/sched-ops v0.0.0-20200831185134-3e8010dc7056 => github.com/portworx/sched-ops v0.20.4-openstorage-rc3 // required by rook v1.7


exclude (
github.com/kubernetes-incubator/external-storage v0.20.4-openstorage-rc2
k8s.io/client-go v11.0.1-0.20190409021438-1a26190bd76a+incompatible
Expand Down
150 changes: 130 additions & 20 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions ibm/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,7 @@ func Provider() *schema.Provider {
"ibm_pi_volume_group": power.ResourceIBMPIVolumeGroup(),
"ibm_pi_volume_onboarding": power.ResourceIBMPIVolumeOnboarding(),
"ibm_pi_volume": power.ResourceIBMPIVolume(),
"ibm_pi_volumes_delete": power.ResourceIBMPIVolumesDelete(),
"ibm_pi_vpn_connection": power.ResourceIBMPIVPNConnection(),
"ibm_pi_workspace": power.ResourceIBMPIWorkspace(),

Expand Down
4 changes: 3 additions & 1 deletion ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,9 @@ const (
Attr_WorkspaceStatus = "pi_workspace_status"
Attr_WorkspaceType = "pi_workspace_type"
Attr_WWN = "wwn"
OS_IBMI = "ibmi"

// OS Type
OS_IBMI = "ibmi"

// Affinty Values
Affinity = "affinity"
Expand Down
2 changes: 1 addition & 1 deletion ibm/service/power/resource_ibm_pi_shared_processor_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func resourceIBMPISharedProcessorPoolUpdate(ctx context.Context, d *schema.Resou
}
if d.HasChange(Arg_SharedProcessorPoolReservedCores) {
reservedCores := int64(d.Get(Arg_SharedProcessorPoolReservedCores).(int))
body.ReservedCores = reservedCores
body.ReservedCores = &reservedCores
}

_, err = client.Update(sppID, body)
Expand Down
94 changes: 94 additions & 0 deletions ibm/service/power/resource_ibm_pi_volumes_delete.go
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
}
71 changes: 71 additions & 0 deletions ibm/service/power/resource_ibm_pi_volumes_delete_test.go
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
}
49 changes: 49 additions & 0 deletions website/docs/r/pi_volumes_delete.html.markdown
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.

0 comments on commit 134dce3

Please sign in to comment.