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

[Datasource] VolumeSnapshot, VolumeSnaphots #5674

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ toolchain go1.22.5
require (
github.com/IBM-Cloud/bluemix-go v0.0.0-20240719075425-078fcb3a55be
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113
github.com/IBM-Cloud/power-go-client v1.7.0
github.com/IBM-Cloud/power-go-client v1.8.1
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
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ github.com/IBM-Cloud/bluemix-go v0.0.0-20240719075425-078fcb3a55be/go.mod h1:/7h
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113 h1:f2Erqfea1dKpaTFagTJM6W/wnD3JGq/Vn9URh8nuRwk=
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113/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.7.0 h1:/GuGwPMTKoCZACfnwt7b6wKr4v32q1VO1AMFGNETRN4=
github.com/IBM-Cloud/power-go-client v1.7.0/go.mod h1:9izycYAmNQ+NAdVPXDC3fHYxqWLjlR2YiwqKYveMv5Y=
github.com/IBM-Cloud/power-go-client v1.8.1 h1:tx1aPJmIQrNru1MD1VHGNasGx3eRIs0zzPZ0KvdFQrg=
github.com/IBM-Cloud/power-go-client v1.8.1/go.mod h1:N4RxrsMUvBQjSQ/qPk0iMZ8zK+fZPRTnHi/gTaASw0g=
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
2 changes: 2 additions & 0 deletions ibm/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,8 @@ func Provider() *schema.Provider {
"ibm_pi_volume_onboarding": power.DataSourceIBMPIVolumeOnboarding(),
"ibm_pi_volume_onboardings": power.DataSourceIBMPIVolumeOnboardings(),
"ibm_pi_volume_remote_copy_relationship": power.DataSourceIBMPIVolumeRemoteCopyRelationship(),
"ibm_pi_volume_snapshot": power.DataSourceIBMPIVolumeSnapshot(),
"ibm_pi_volume_snapshots": power.DataSourceIBMPIVolumeSnapshots(),
"ibm_pi_volume": power.DataSourceIBMPIVolume(),
"ibm_pi_workspace": power.DatasourceIBMPIWorkspace(),
"ibm_pi_workspaces": power.DatasourceIBMPIWorkspaces(),
Expand Down
98 changes: 98 additions & 0 deletions ibm/service/power/data_source_ibm_pi_volume_snapshot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package power

import (
"context"

"github.com/IBM-Cloud/power-go-client/clients/instance"
"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 DataSourceIBMPIVolumeSnapshot() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPIVolumeSnapshotRead,

Schema: map[string]*schema.Schema{
// Arguments
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_VolumeSnapshotID: {
Description: "The volume snapshot id.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},

// Attributes
Attr_CreationDate: {
Computed: true,
Description: "The date and time when the volume snapshot was created.",
Type: schema.TypeString,
},
Attr_CRN: {
Computed: true,
Description: "The CRN of the volume snapshot.",
Type: schema.TypeString,
},
Attr_Name: {
Computed: true,
Description: "The volume snapshot name.",
Type: schema.TypeString,
},
Attr_Size: {
Computed: true,
Description: "The size of the volume snapshot, in gibibytes (GiB).",
Type: schema.TypeFloat,
},
Attr_Status: {
Computed: true,
Description: "The status for the volume snapshot.",
Type: schema.TypeString,
},
Attr_UpdatedDate: {
Computed: true,
Description: "The date and time when the volume snapshot was last updated.",
Type: schema.TypeString,
},
Attr_VolumeID: {
Computed: true,
Description: "The volume UUID associated with the snapshot.",
Type: schema.TypeString,
},
},
}
}

func dataSourceIBMPIVolumeSnapshotRead(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)
snapshotID := d.Get(Arg_VolumeSnapshotID).(string)

client := instance.NewIBMPISnapshotClient(ctx, sess, cloudInstanceID)
snapshot, err := client.V1VolumeSnapshotsGet(snapshotID)
if err != nil {
return diag.FromErr(err)
}
d.SetId(*snapshot.ID)
d.Set(Attr_CreationDate, snapshot.CreationDate.String())
d.Set(Attr_CRN, snapshot.Crn)
d.Set(Attr_Name, *snapshot.Name)
d.Set(Attr_Size, *snapshot.Size)
d.Set(Attr_Status, snapshot.Status)
d.Set(Attr_UpdatedDate, snapshot.UpdatedDate.String())
d.Set(Attr_VolumeID, *snapshot.VolumeID)
return nil
}
37 changes: 37 additions & 0 deletions ibm/service/power/data_source_ibm_pi_volume_snapshot_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 TestAccIBMPIVolumeSnapshotDataSourceBasic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPIVolumeSnapshotDataSourceConfigBasic(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.ibm_pi_volume_snapshot.snapshot_instance", "id"),
resource.TestCheckResourceAttrSet("data.ibm_pi_volume_snapshot.snapshot_instance", "name"),
),
},
},
})
}

func testAccCheckIBMPIVolumeSnapshotDataSourceConfigBasic() string {
return fmt.Sprintf(`
data "ibm_pi_volume_snapshot" "snapshot_instance" {
pi_cloud_instance_id = "%s"
pi_volume_snapshot_id = "%s"
}
`, acc.Pi_cloud_instance_id, acc.Pi_snapshot_id)
}
120 changes: 120 additions & 0 deletions ibm/service/power/data_source_ibm_pi_volume_snapshots.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package power

import (
"context"

"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/go-uuid"
"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 DataSourceIBMPIVolumeSnapshots() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPIVolumeSnapshotsRead,

Schema: map[string]*schema.Schema{
// Arguments
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},

// Attributes
Attr_VolumesSnapshots: {
Computed: true,
Description: "The list of volume snapshots.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_CreationDate: {
Computed: true,
Description: "The date and time when the volume snapshot was created.",
Type: schema.TypeString,
},
Attr_CRN: {
Computed: true,
Description: "The CRN of the volume snapshot.",
Type: schema.TypeString,
},
Attr_ID: {
Computed: true,
Description: "The snapshot UUID.",
Type: schema.TypeString,
},
Attr_Name: {
Computed: true,
Description: "The volume snapshot name.",
Type: schema.TypeString,
},
Attr_Size: {
Computed: true,
Description: "The size of the volume snapshot, in gibibytes (GiB).",
Type: schema.TypeFloat,
},
Attr_Status: {
Computed: true,
Description: "The status for the volume snapshot.",
Type: schema.TypeString,
},
Attr_UpdatedDate: {
Computed: true,
Description: "The date and time when the volume snapshot was last updated.",
Type: schema.TypeString,
},
Attr_VolumeID: {
Computed: true,
Description: "The volume UUID associated with the snapshot.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeSet,
},
},
}
}

func dataSourceIBMPIVolumeSnapshotsRead(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)
client := instance.NewIBMPISnapshotClient(ctx, sess, cloudInstanceID)
snapshots, err := client.V1VolumeSnapshotsGetall()
if err != nil {
return diag.FromErr(err)
}
d.Set(Attr_VolumesSnapshots, flattenSnapshotsV1(snapshots.VolumeSnapshots))
var clientgenU, _ = uuid.GenerateUUID()
d.SetId(clientgenU)

return nil
}

func flattenSnapshotsV1(snapshotList []*models.SnapshotV1) []map[string]interface{} {
snapshots := make([]map[string]interface{}, 0, len(snapshotList))
for _, snap := range snapshotList {
snapshot := map[string]interface{}{
Attr_CreationDate: snap.CreationDate.String(),
Attr_CRN: snap.Crn,
Attr_ID: *snap.ID,
Attr_Name: *snap.Name,
Attr_Size: *snap.Size,
Attr_Status: *snap.Status,
Attr_UpdatedDate: snap.UpdatedDate.String(),
Attr_VolumeID: *snap.VolumeID,
}
snapshots = append(snapshots, snapshot)
}
return snapshots
}
35 changes: 35 additions & 0 deletions ibm/service/power/data_source_ibm_pi_volume_snapshots_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 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 TestAccIBMPIVolumeSnapshotsDataSourceBasic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPIVolumeSnapshotsDataSourceConfigBasic(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.ibm_pi_volume_snapshots.snapshots", "id"),
),
},
},
})
}

func testAccCheckIBMPIVolumeSnapshotsDataSourceConfigBasic() string {
return fmt.Sprintf(`
data "ibm_pi_volume_snapshots" "snapshots" {
pi_cloud_instance_id = "%s"
}
`, acc.Pi_cloud_instance_id)
}
5 changes: 4 additions & 1 deletion ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ const (
Arg_VolumePool = "pi_volume_pool"
Arg_VolumeShareable = "pi_volume_shareable"
Arg_VolumeSize = "pi_volume_size"
Arg_VolumeSnapshotID = "pi_volume_snapshot_id"
Arg_VolumeType = "pi_volume_type"
Arg_VTL = "vtl"

Expand Down Expand Up @@ -365,6 +366,7 @@ const (
Attr_TotalStandardStorageConsumed = "total_standard_storage_consumed"
Attr_Type = "type"
Attr_Uncapped = "uncapped"
Attr_UpdatedDate = "updated_date"
Attr_URL = "url"
Attr_UsedCore = "used_core"
Attr_UsedIPCount = "used_ip_count"
Expand All @@ -384,6 +386,7 @@ const (
Attr_VolumePool = "volume_pool"
Attr_Volumes = "volumes"
Attr_VolumeSnapshots = "volume_snapshots"
Attr_VolumesSnapshots = "volume_snapshots"
Attr_VolumeStatus = "volume_status"
Attr_VPCCRNs = "vpc_crns"
Attr_VPCEnabled = "vpc_enabled"
Expand Down Expand Up @@ -487,8 +490,8 @@ const (
State_RESIZE = "RESIZE"
State_Retry = "retry"
State_Shutoff = "shutoff"
State_Stopping = "stopping"
State_SHUTOFF = "SHUTOFF"
State_Stopping = "stopping"
State_Up = "up"
State_Updating = "updating"
State_VerifyResize = "verify_resize"
Expand Down
Loading
Loading