Skip to content

Commit 6327194

Browse files
committed
[Datasource] Volume Snapshot, Volumes Snapshots
Update with new endpoints
1 parent eb56218 commit 6327194

10 files changed

+413
-5
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ toolchain go1.22.5
66

77
require (
88
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113
9-
github.com/IBM-Cloud/power-go-client v1.7.0
9+
github.com/IBM-Cloud/power-go-client v1.8.0-beta9
1010
github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca
1111
github.com/IBM/appconfiguration-go-admin-sdk v0.3.0
1212
github.com/IBM/appid-management-go-sdk v0.0.0-20210908164609-dd0e0eaf732f
@@ -25,8 +25,8 @@ require (
2525
github.com/IBM/ibm-hpcs-uko-sdk v0.0.20-beta
2626
github.com/IBM/keyprotect-go-client v0.14.0
2727
github.com/IBM/logs-go-sdk v0.3.0
28-
github.com/IBM/networking-go-sdk v0.49.0
2928
github.com/IBM/logs-router-go-sdk v1.0.3
29+
github.com/IBM/networking-go-sdk v0.49.0
3030
github.com/IBM/platform-services-go-sdk v0.65.0
3131
github.com/IBM/project-go-sdk v0.3.5
3232
github.com/IBM/push-notifications-go-sdk v0.0.0-20210310100607-5790b96c47f5

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ github.com/IBM-Cloud/bluemix-go v0.0.0-20240719075425-078fcb3a55be/go.mod h1:/7h
118118
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113 h1:f2Erqfea1dKpaTFagTJM6W/wnD3JGq/Vn9URh8nuRwk=
119119
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113/go.mod h1:xUQL9SGAjoZFd4GNjrjjtEpjpkgU7RFXRyHesbKTjiY=
120120
github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.5.3/go.mod h1:RiUvKuHKTBmBApDMUQzBL14pQUGKcx/IioKQPIcRQjs=
121-
github.com/IBM-Cloud/power-go-client v1.7.0 h1:/GuGwPMTKoCZACfnwt7b6wKr4v32q1VO1AMFGNETRN4=
122-
github.com/IBM-Cloud/power-go-client v1.7.0/go.mod h1:9izycYAmNQ+NAdVPXDC3fHYxqWLjlR2YiwqKYveMv5Y=
121+
github.com/IBM-Cloud/power-go-client v1.8.0-beta9 h1:AmE07DS/JPc8fpuJTxv0r4VK/prEV8qfMgCZMY2WD4w=
122+
github.com/IBM-Cloud/power-go-client v1.8.0-beta9/go.mod h1:oAkZiHX25cmr2Yun5V0q6CpnUemegvSrpcEy/oQcjzU=
123123
github.com/IBM-Cloud/softlayer-go v1.0.5-tf h1:koUAyF9b6X78lLLruGYPSOmrfY2YcGYKOj/Ug9nbKNw=
124124
github.com/IBM-Cloud/softlayer-go v1.0.5-tf/go.mod h1:6HepcfAXROz0Rf63krk5hPZyHT6qyx2MNvYyHof7ik4=
125125
github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca h1:crniVcf+YcmgF03NmmfonXwSQ73oJF+IohFYBwknMxs=

ibm/provider/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,8 @@ func Provider() *schema.Provider {
680680
"ibm_pi_volume_onboarding": power.DataSourceIBMPIVolumeOnboarding(),
681681
"ibm_pi_volume_onboardings": power.DataSourceIBMPIVolumeOnboardings(),
682682
"ibm_pi_volume_remote_copy_relationship": power.DataSourceIBMPIVolumeRemoteCopyRelationship(),
683+
"ibm_pi_volume_snapshot": power.DataSourceIBMPIVolumeSnapshot(),
684+
"ibm_pi_volume_snapshots": power.DataSourceIBMPIVolumeSnapshots(),
683685
"ibm_pi_volume": power.DataSourceIBMPIVolume(),
684686
"ibm_pi_workspace": power.DatasourceIBMPIWorkspace(),
685687
"ibm_pi_workspaces": power.DatasourceIBMPIWorkspaces(),
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright IBM Corp. 2024 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package power
5+
6+
import (
7+
"context"
8+
9+
"github.com/IBM-Cloud/power-go-client/clients/instance"
10+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
14+
)
15+
16+
func DataSourceIBMPIVolumeSnapshot() *schema.Resource {
17+
return &schema.Resource{
18+
ReadContext: dataSourceIBMPIVolumeSnapshotRead,
19+
20+
Schema: map[string]*schema.Schema{
21+
// Arguments
22+
Arg_CloudInstanceID: {
23+
Description: "The GUID of the service instance associated with an account.",
24+
Required: true,
25+
Type: schema.TypeString,
26+
ValidateFunc: validation.NoZeroValues,
27+
},
28+
Arg_VolumeSnapshotID: {
29+
Description: "The volume snapshot id.",
30+
Required: true,
31+
Type: schema.TypeString,
32+
ValidateFunc: validation.NoZeroValues,
33+
},
34+
35+
// Attributes
36+
Attr_CreationDate: {
37+
Computed: true,
38+
Description: "The date and time when the volume snapshot was created.",
39+
Type: schema.TypeString,
40+
},
41+
Attr_CRN: {
42+
Computed: true,
43+
Description: "The CRN of the volume snapshot.",
44+
Type: schema.TypeString,
45+
},
46+
Attr_Name: {
47+
Computed: true,
48+
Description: "The volume snapshot name.",
49+
Type: schema.TypeString,
50+
},
51+
Attr_Size: {
52+
Computed: true,
53+
Description: "The size of the volume snapshot, in gibibytes (GiB).",
54+
Type: schema.TypeFloat,
55+
},
56+
Attr_Status: {
57+
Computed: true,
58+
Description: "The status for the volume snapshot.",
59+
Type: schema.TypeString,
60+
},
61+
Attr_UpdatedDate: {
62+
Computed: true,
63+
Description: "The date and time when the volume snapshot was last updated.",
64+
Type: schema.TypeString,
65+
},
66+
Attr_VolumeID: {
67+
Computed: true,
68+
Description: "The volume UUID associated with the snapshot.",
69+
Type: schema.TypeString,
70+
},
71+
},
72+
}
73+
}
74+
75+
func dataSourceIBMPIVolumeSnapshotRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
76+
sess, err := meta.(conns.ClientSession).IBMPISession()
77+
if err != nil {
78+
return diag.FromErr(err)
79+
}
80+
81+
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
82+
snapshotID := d.Get(Arg_VolumeSnapshotID).(string)
83+
84+
client := instance.NewIBMPISnapshotClient(ctx, sess, cloudInstanceID)
85+
snapshot, err := client.V1VolumeSnapshotsGet(snapshotID)
86+
if err != nil {
87+
return diag.FromErr(err)
88+
}
89+
d.SetId(*snapshot.ID)
90+
d.Set(Attr_CreationDate, snapshot.CreationDate.String())
91+
d.Set(Attr_CRN, snapshot.Crn)
92+
d.Set(Attr_Name, *snapshot.Name)
93+
d.Set(Attr_Size, *snapshot.Size)
94+
d.Set(Attr_Status, snapshot.Status)
95+
d.Set(Attr_UpdatedDate, snapshot.UpdatedDate.String())
96+
d.Set(Attr_VolumeID, *snapshot.VolumeID)
97+
return nil
98+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright IBM Corp. 2024 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package power_test
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
)
13+
14+
func TestAccIBMPISnapshotDataSourceBasic(t *testing.T) {
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { acc.TestAccPreCheck(t) },
17+
Providers: acc.TestAccProviders,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: testAccCheckIBMPISnapshotDataSourceConfigBasic(),
21+
Check: resource.ComposeTestCheckFunc(
22+
resource.TestCheckResourceAttrSet("data.ibm_pi_volume_snapshot.snapshot_instance", "id"),
23+
resource.TestCheckResourceAttrSet("data.ibm_pi_volume_snapshot.snapshot_instance", "name"),
24+
),
25+
},
26+
},
27+
})
28+
}
29+
30+
func testAccCheckIBMPISnapshotDataSourceConfigBasic() string {
31+
return fmt.Sprintf(`
32+
data "ibm_pi_volume_snapshot" "snapshot_instance" {
33+
pi_cloud_instance_id = "%s"
34+
pi_volume_snapshot_id = "%s"
35+
}
36+
`, acc.Pi_cloud_instance_id, acc.Pi_snapshot_id)
37+
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
// Copyright IBM Corp. 2024 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package power
5+
6+
import (
7+
"context"
8+
9+
"github.com/IBM-Cloud/power-go-client/clients/instance"
10+
"github.com/IBM-Cloud/power-go-client/power/models"
11+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
12+
"github.com/hashicorp/go-uuid"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
14+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
15+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
16+
)
17+
18+
func DataSourceIBMPIVolumeSnapshots() *schema.Resource {
19+
return &schema.Resource{
20+
ReadContext: dataSourceIBMPIVolumeSnapshotsRead,
21+
22+
Schema: map[string]*schema.Schema{
23+
// Arguments
24+
Arg_CloudInstanceID: {
25+
Description: "The GUID of the service instance associated with an account.",
26+
Required: true,
27+
Type: schema.TypeString,
28+
ValidateFunc: validation.NoZeroValues,
29+
},
30+
31+
// Attributes
32+
Attr_VolumesSnapshots: {
33+
Computed: true,
34+
Description: "The list of volume snapshots.",
35+
Elem: &schema.Resource{
36+
Schema: map[string]*schema.Schema{
37+
Attr_CreationDate: {
38+
Computed: true,
39+
Description: "The date and time when the volume snapshot was created.",
40+
Type: schema.TypeString,
41+
},
42+
Attr_CRN: {
43+
Computed: true,
44+
Description: "The CRN of the volume snapshot.",
45+
Type: schema.TypeString,
46+
},
47+
Attr_ID: {
48+
Computed: true,
49+
Description: "The snapshot UUID.",
50+
Type: schema.TypeString,
51+
},
52+
Attr_Name: {
53+
Computed: true,
54+
Description: "The volume snapshot name.",
55+
Type: schema.TypeString,
56+
},
57+
Attr_Size: {
58+
Computed: true,
59+
Description: "The size of the volume snapshot, in gibibytes (GiB).",
60+
Type: schema.TypeFloat,
61+
},
62+
Attr_Status: {
63+
Computed: true,
64+
Description: "The status for the volume snapshot.",
65+
Type: schema.TypeString,
66+
},
67+
Attr_UpdatedDate: {
68+
Computed: true,
69+
Description: "The date and time when the volume snapshot was last updated.",
70+
Type: schema.TypeString,
71+
},
72+
Attr_VolumeID: {
73+
Computed: true,
74+
Description: "The volume UUID associated with the snapshot.",
75+
Type: schema.TypeString,
76+
},
77+
},
78+
},
79+
Type: schema.TypeSet,
80+
},
81+
},
82+
}
83+
}
84+
85+
func dataSourceIBMPIVolumeSnapshotsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
86+
sess, err := meta.(conns.ClientSession).IBMPISession()
87+
if err != nil {
88+
return diag.FromErr(err)
89+
}
90+
91+
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
92+
client := instance.NewIBMPISnapshotClient(ctx, sess, cloudInstanceID)
93+
snapshots, err := client.V1VolumeSnapshotsGetall()
94+
if err != nil {
95+
return diag.FromErr(err)
96+
}
97+
d.Set(Attr_VolumesSnapshots, flattenSnapshotsV1(snapshots.VolumeSnapshots))
98+
var clientgenU, _ = uuid.GenerateUUID()
99+
d.SetId(clientgenU)
100+
101+
return nil
102+
}
103+
104+
func flattenSnapshotsV1(snapshotList []*models.SnapshotV1) []map[string]interface{} {
105+
snapshots := make([]map[string]interface{}, 0, len(snapshotList))
106+
for _, snap := range snapshotList {
107+
snapshot := map[string]interface{}{
108+
Attr_CreationDate: snap.CreationDate.String(),
109+
Attr_CRN: snap.Crn,
110+
Attr_ID: *snap.ID,
111+
Attr_Name: *snap.Name,
112+
Attr_Size: *snap.Size,
113+
Attr_Status: *snap.Status,
114+
Attr_UpdatedDate: snap.UpdatedDate.String(),
115+
Attr_VolumeID: *snap.VolumeID,
116+
}
117+
snapshots = append(snapshots, snapshot)
118+
}
119+
return snapshots
120+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright IBM Corp. 2024 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package power_test
5+
6+
import (
7+
"fmt"
8+
"testing"
9+
10+
acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
)
13+
14+
func TestAccIBMPISnapshotsDataSourceBasic(t *testing.T) {
15+
resource.Test(t, resource.TestCase{
16+
PreCheck: func() { acc.TestAccPreCheck(t) },
17+
Providers: acc.TestAccProviders,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: testAccCheckIBMPISnapshotsDataSourceConfigBasic(),
21+
Check: resource.ComposeTestCheckFunc(
22+
resource.TestCheckResourceAttrSet("data.ibm_pi_volume_snapshots.snapshots", "id"),
23+
),
24+
},
25+
},
26+
})
27+
}
28+
29+
func testAccCheckIBMPISnapshotsDataSourceConfigBasic() string {
30+
return fmt.Sprintf(`
31+
data "ibm_pi_volume_snapshots" "snapshots" {
32+
pi_cloud_instance_id = "%s"
33+
}
34+
`, acc.Pi_cloud_instance_id)
35+
}

ibm/service/power/ibm_pi_constants.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const (
9696
Arg_VolumePool = "pi_volume_pool"
9797
Arg_VolumeShareable = "pi_volume_shareable"
9898
Arg_VolumeSize = "pi_volume_size"
99+
Arg_VolumeSnapshotID = "pi_volume_snapshot_id"
99100
Arg_VolumeType = "pi_volume_type"
100101
Arg_VTL = "vtl"
101102

@@ -363,6 +364,7 @@ const (
363364
Attr_TotalStandardStorageConsumed = "total_standard_storage_consumed"
364365
Attr_Type = "type"
365366
Attr_Uncapped = "uncapped"
367+
Attr_UpdatedDate = "updated_date"
366368
Attr_URL = "url"
367369
Attr_UsedCore = "used_core"
368370
Attr_UsedIPCount = "used_ip_count"
@@ -380,6 +382,7 @@ const (
380382
Attr_VolumePool = "volume_pool"
381383
Attr_Volumes = "volumes"
382384
Attr_VolumeSnapshots = "volume_snapshots"
385+
Attr_VolumesSnapshots = "volume_snapshots"
383386
Attr_VolumeStatus = "volume_status"
384387
Attr_VPCCRNs = "vpc_crns"
385388
Attr_VPCEnabled = "vpc_enabled"
@@ -481,8 +484,8 @@ const (
481484
State_RESIZE = "RESIZE"
482485
State_Retry = "retry"
483486
State_Shutoff = "shutoff"
484-
State_Stopping = "stopping"
485487
State_SHUTOFF = "SHUTOFF"
488+
State_Stopping = "stopping"
486489
State_Up = "up"
487490
State_VerifyResize = "verify_resize"
488491
Status_Active = "ACTIVE"

0 commit comments

Comments
 (0)