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 all 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: 2 additions & 0 deletions ibm/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,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 @@ -103,6 +103,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 @@ -374,6 +375,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 @@ -394,6 +396,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 @@ -498,8 +501,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
56 changes: 56 additions & 0 deletions website/docs/d/pi_volume_snapshot.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
---
layout: "ibm"
page_title: "IBM : ibm_pi_volume_snapshot"
description: |-
Get information about a volume snapshot in Power Virtual Server.
subcategory: "Power Systems"
---

# ibm_pi_volume_snapshot

Retrieve information about a volume snapshot.

## Example Usage

```terraform
data "ibm_pi_volume_snapshot" "snapshot" {
pi_cloud_instance_id = "<value of the cloud_instance_id>"
pi_snapshot_id = "snapshot_id"
}
```

### 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"
}
```

## Argument Reference

You can specify the following arguments for this data source.

- `pi_cloud_instance_id` - (Required, String) The GUID of the service instance associated with an account.
- `pi_snapshot_id` - (Required, String) The volume snapshot id.

## Attribute Reference

In addition to all argument reference list, you can access the following attribute references after your data source is created.

- `creation_date` - (String) The date and time when the volume snapshot was created.
- `crn` - (String) The CRN for this resource.
- `id` - (String) The unique identifier of the volume snapshot.
- `name` - (String) The volume snapshot name.
- `size` - (Float) The size of the volume snapshot, in gibibytes (GiB).
- `status` - (String) The status for the volume snapshot.
- `updated_date` - (String) The date and time when the volume snapshot was last updated.
- `volume_id` - (String) The volume UUID associated with the snapshot.
Loading
Loading