Skip to content

Commit b0e0954

Browse files
authored
Add new ibm_pi_volumes data source and documentation (IBM-Cloud#6520)
1 parent 5f21768 commit b0e0954

File tree

4 files changed

+375
-0
lines changed

4 files changed

+375
-0
lines changed

ibm/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -807,6 +807,7 @@ func Provider() *schema.Provider {
807807
"ibm_pi_volume_snapshot": power.DataSourceIBMPIVolumeSnapshot(),
808808
"ibm_pi_volume_snapshots": power.DataSourceIBMPIVolumeSnapshots(),
809809
"ibm_pi_volume": power.DataSourceIBMPIVolume(),
810+
"ibm_pi_volumes": power.DataSourceIBMPIVolumes(),
810811
"ibm_pi_workspace": power.DatasourceIBMPIWorkspace(),
811812
"ibm_pi_workspaces": power.DatasourceIBMPIWorkspaces(),
812813

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
// Copyright IBM Corp. 2025 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package power
5+
6+
import (
7+
"context"
8+
"fmt"
9+
"log"
10+
11+
"github.com/IBM-Cloud/power-go-client/clients/instance"
12+
"github.com/IBM-Cloud/power-go-client/power/models"
13+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
14+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
15+
"github.com/hashicorp/go-uuid"
16+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
19+
)
20+
21+
func DataSourceIBMPIVolumes() *schema.Resource {
22+
return &schema.Resource{
23+
ReadContext: dataSourceIBMPIVolumesRead,
24+
Schema: map[string]*schema.Schema{
25+
// Arguments
26+
Arg_CloudInstanceID: {
27+
Description: "The GUID of the service instance associated with an account.",
28+
Required: true,
29+
Type: schema.TypeString,
30+
ValidateFunc: validation.NoZeroValues,
31+
},
32+
33+
// Attributes
34+
Attr_Volumes: {
35+
Computed: true,
36+
Description: "List of all volumes.",
37+
Elem: &schema.Resource{
38+
Schema: map[string]*schema.Schema{
39+
Attr_Auxiliary: {
40+
Computed: true,
41+
Description: "Indicates if the volume is auxiliary or not.",
42+
Type: schema.TypeBool,
43+
},
44+
Attr_AuxiliaryVolumeName: {
45+
Computed: true,
46+
Description: "The auxiliary volume name.",
47+
Type: schema.TypeString,
48+
},
49+
Attr_Bootable: {
50+
Computed: true,
51+
Description: "Indicates if the volume is boot capable.",
52+
Type: schema.TypeBool,
53+
},
54+
Attr_CRN: {
55+
Computed: true,
56+
Description: "The CRN of this resource.",
57+
Type: schema.TypeString,
58+
},
59+
Attr_ConsistencyGroupName: {
60+
Computed: true,
61+
Description: "Consistency group name if volume is a part of volume group.",
62+
Type: schema.TypeString,
63+
},
64+
Attr_CreationDate: {
65+
Computed: true,
66+
Description: "Date volume was created.",
67+
Type: schema.TypeString,
68+
},
69+
Attr_DiskType: {
70+
Computed: true,
71+
Description: "The disk type that is used for the volume.",
72+
Type: schema.TypeString,
73+
},
74+
Attr_FreezeTime: {
75+
Computed: true,
76+
Description: "The freeze time of remote copy.",
77+
Type: schema.TypeString,
78+
},
79+
Attr_GroupID: {
80+
Computed: true,
81+
Description: "The volume group id in which the volume belongs.",
82+
Type: schema.TypeString,
83+
},
84+
Attr_ID: {
85+
Computed: true,
86+
Description: "The unique identifier of the volume.",
87+
Type: schema.TypeString,
88+
},
89+
Attr_IOThrottleRate: {
90+
Computed: true,
91+
Description: "Amount of iops assigned to the volume",
92+
Type: schema.TypeString,
93+
},
94+
Attr_LastUpdateDate: {
95+
Computed: true,
96+
Description: "The last updated date of the volume.",
97+
Type: schema.TypeString,
98+
},
99+
Attr_MasterVolumeName: {
100+
Computed: true,
101+
Description: "The master volume name.",
102+
Type: schema.TypeString,
103+
},
104+
Attr_MirroringState: {
105+
Computed: true,
106+
Description: "Mirroring state for replication enabled volume.",
107+
Type: schema.TypeString,
108+
},
109+
Attr_Name: {
110+
Computed: true,
111+
Description: "The name of the volume.",
112+
Type: schema.TypeString,
113+
},
114+
Attr_OutOfBandDeleted: {
115+
Computed: true,
116+
Description: "Indicates if the volume does not exist on storage controller.",
117+
Type: schema.TypeBool,
118+
},
119+
Attr_PrimaryRole: {
120+
Computed: true,
121+
Description: "Indicates whether master/auxiliary volume is playing the primary role.",
122+
Type: schema.TypeString,
123+
},
124+
Attr_ReplicationEnabled: {
125+
Computed: true,
126+
Description: "Indicates if the volume should be replication enabled or not.",
127+
Type: schema.TypeBool,
128+
},
129+
Attr_ReplicationSites: {
130+
Computed: true,
131+
Description: "List of replication sites for volume replication.",
132+
Elem: &schema.Schema{Type: schema.TypeString},
133+
Type: schema.TypeList,
134+
},
135+
Attr_ReplicationStatus: {
136+
Computed: true,
137+
Description: "The replication status of the volume.",
138+
Type: schema.TypeString,
139+
},
140+
Attr_ReplicationType: {
141+
Computed: true,
142+
Description: "The replication type of the volume, metro or global.",
143+
Type: schema.TypeString,
144+
},
145+
Attr_Shareable: {
146+
Computed: true,
147+
Description: "Indicates if the volume is shareable between VMs.",
148+
Type: schema.TypeBool,
149+
},
150+
Attr_Size: {
151+
Computed: true,
152+
Description: "The size of the volume in GB.",
153+
Type: schema.TypeInt,
154+
},
155+
Attr_State: {
156+
Computed: true,
157+
Description: "The state of the volume.",
158+
Type: schema.TypeString,
159+
},
160+
Attr_UserTags: {
161+
Computed: true,
162+
Description: "List of user tags attached to the resource.",
163+
Elem: &schema.Schema{Type: schema.TypeString},
164+
Set: schema.HashString,
165+
Type: schema.TypeSet,
166+
},
167+
Attr_VolumePool: {
168+
Computed: true,
169+
Description: "The name of storage pool where the volume is located.",
170+
Type: schema.TypeString,
171+
},
172+
Attr_VolumeType: {
173+
Computed: true,
174+
Description: "The name of storage template used to create the volume.",
175+
Type: schema.TypeString,
176+
},
177+
Attr_WWN: {
178+
Computed: true,
179+
Description: "The world wide name of the volume.",
180+
Type: schema.TypeString,
181+
},
182+
},
183+
},
184+
Type: schema.TypeList,
185+
},
186+
},
187+
}
188+
}
189+
190+
func dataSourceIBMPIVolumesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
191+
sess, err := meta.(conns.ClientSession).IBMPISession()
192+
if err != nil {
193+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("IBMPISession failed: %s", err.Error()), "(Data) ibm_pi_volumes", "read")
194+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
195+
return tfErr.GetDiag()
196+
}
197+
198+
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
199+
volumeC := instance.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID)
200+
volumeData, err := volumeC.GetAll()
201+
if err != nil {
202+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetAll failed: %s", err.Error()), "(Data) ibm_pi_volumes", "read")
203+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
204+
return tfErr.GetDiag()
205+
}
206+
207+
var clientgenU, _ = uuid.GenerateUUID()
208+
d.SetId(clientgenU)
209+
d.Set(Attr_Volumes, flattenVolumes(volumeData.Volumes, meta))
210+
return nil
211+
}
212+
213+
func flattenVolumes(list []*models.VolumeReference, meta any) []map[string]any {
214+
result := make([]map[string]any, 0, len(list))
215+
for _, i := range list {
216+
volume := map[string]any{
217+
Attr_Auxiliary: *i.Auxiliary,
218+
Attr_AuxiliaryVolumeName: i.AuxVolumeName,
219+
Attr_Bootable: *i.Bootable,
220+
Attr_ConsistencyGroupName: i.ConsistencyGroupName,
221+
Attr_CreationDate: i.CreationDate.String(),
222+
Attr_DiskType: *i.DiskType,
223+
Attr_GroupID: i.GroupID,
224+
Attr_ID: *i.VolumeID,
225+
Attr_IOThrottleRate: i.IoThrottleRate,
226+
Attr_LastUpdateDate: i.LastUpdateDate.String(),
227+
Attr_MasterVolumeName: i.MasterVolumeName,
228+
Attr_MirroringState: i.MirroringState,
229+
Attr_Name: *i.Name,
230+
Attr_OutOfBandDeleted: i.OutOfBandDeleted,
231+
Attr_PrimaryRole: i.PrimaryRole,
232+
Attr_ReplicationEnabled: *i.ReplicationEnabled,
233+
Attr_ReplicationStatus: i.ReplicationStatus,
234+
Attr_ReplicationType: i.ReplicationType,
235+
Attr_Shareable: *i.Shareable,
236+
Attr_Size: *i.Size,
237+
Attr_State: *i.State,
238+
Attr_VolumePool: i.VolumePool,
239+
Attr_VolumeType: i.VolumeType,
240+
Attr_WWN: *i.Wwn,
241+
}
242+
if i.FreezeTime != nil {
243+
volume[Attr_FreezeTime] = i.FreezeTime.String()
244+
}
245+
if len(i.ReplicationSites) > 0 {
246+
volume[Attr_ReplicationSites] = i.ReplicationSites
247+
}
248+
volumeCRN := string(i.Crn)
249+
if volumeCRN != "" {
250+
volume[Attr_CRN] = i.Crn
251+
tags, err := flex.GetGlobalTagsUsingCRN(meta, volumeCRN, "", UserTagType)
252+
if err != nil {
253+
log.Printf("Error on get of pi volume (%s) user_tags: %s", *i.VolumeID, err)
254+
}
255+
volume[Attr_UserTags] = tags
256+
}
257+
result = append(result, volume)
258+
}
259+
return result
260+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright IBM Corp. 2025 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 TestAccIBMPIVolumesDataSourceBasic(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: testAccCheckIBMPIVolumesDataSourceConfigBasic(),
21+
Check: resource.ComposeTestCheckFunc(
22+
resource.TestCheckResourceAttrSet("data.ibm_pi_volumes.volumes", "id"),
23+
),
24+
},
25+
},
26+
})
27+
}
28+
29+
func testAccCheckIBMPIVolumesDataSourceConfigBasic() string {
30+
return fmt.Sprintf(`
31+
data "ibm_pi_volumes" "volumes" {
32+
pi_cloud_instance_id = "%s"
33+
}
34+
`, acc.Pi_cloud_instance_id)
35+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
subcategory: "Power Systems"
3+
layout: "ibm"
4+
page_title: "IBM: ibm_pi_volumes"
5+
description: |-
6+
Manages volumes in the Power Virtual Server cloud.
7+
---
8+
9+
# ibm_pi_volumes
10+
11+
Retrieves information about all persistent storage volumes that in a Power Systems Virtual Server workspace. For more information, about managing volumes, see [moving data to the cloud](https://cloud.ibm.com/docs/power-iaas?topic=power-iaas-moving-data-to-the-cloud).
12+
13+
## Example Usage
14+
15+
The following example retrieves information about all volumes present in a Power Systems Virtual Server workspace.
16+
17+
```terraform
18+
data "ibm_pi_volumes" "ds_volume" {
19+
pi_cloud_instance_id = "49fba6c9-23f8-40bc-9899-aca322ee7d5b"
20+
}
21+
```
22+
23+
### Notes
24+
25+
- Please find [supported Regions](https://cloud.ibm.com/apidocs/power-cloud#endpoint) for endpoints.
26+
- If a Power cloud instance is provisioned at `lon04`, The provider level attributes should be as follows:
27+
- `region` - `lon`
28+
- `zone` - `lon04`
29+
30+
Example usage:
31+
32+
```terraform
33+
provider "ibm" {
34+
region = "lon"
35+
zone = "lon04"
36+
}
37+
```
38+
39+
## Argument Reference
40+
41+
Review the argument references that you can specify for your data source.
42+
43+
- `pi_cloud_instance_id` - (Required, String) The GUID of the service instance associated with an account.
44+
45+
## Attribute Reference
46+
47+
In addition to all argument reference list, you can access the following attribute references after your data source is created.
48+
49+
- `volumes` - (List) The list of volumes.
50+
51+
Nested schema for `volumes`:
52+
- `auxiliary_volume_name` - (String) The auxiliary volume name.
53+
- `auxiliary` - (Boolean) Indicates if the volume is auxiliary.
54+
- `bootable` - (Boolean) Indicates if the volume is boot capable.
55+
- `consistency_group_name` - (String) Consistency group name if volume is a part of volume group.
56+
- `creation_date` - (String) Date of volume creation.
57+
- `crn` - (String) The CRN of this resource.
58+
- `disk_type` - (String) The disk type that is used for the volume.
59+
- `freeze_time` - (String) Time of remote copy relationship.
60+
- `group_id` - (String) The volume group id in which the volume belongs.
61+
- `id` - (String) The unique identifier of the volume.
62+
- `io_throttle_rate` - (String) Amount of iops assigned to the volume.
63+
- `last_update_date` - (String) The date when the volume last updated.
64+
- `master_volume_name` - (String) The master volume name.
65+
- `mirroring_state` - (String) Mirroring state for replication enabled volume.
66+
- `name` - (String) The name of the volume.
67+
- `out_of_band_deleted` - (Bool) Indicates if the volume does not exist on storage controller.
68+
- `primary_role` - (String) Indicates whether `master`/`auxiliary` volume is playing the primary role.
69+
- `replication_enabled` - (Boolean) Indicates if the volume should be replication enabled or not.
70+
- `replication_sites` - (List) List of replication sites for volume replication.
71+
- `replication_status` - (String) The replication status of the volume.
72+
- `replication_type` - (String) The replication type of the volume, `metro` or `global`.
73+
- `shareable` - (String) Indicates if the volume is shareable between VMs.
74+
- `size` - (Integer) The size of the volume in GB.
75+
- `state` - (String) The state of the volume.
76+
- `user_tags` - (List) List of user tags attached to the resource.
77+
- `volume_pool` - (String) The name of storage pool where the volume is located.
78+
- `volume_type` - (String) The name of storage template used to create the volume.
79+
- `wwn` - (String) The world wide name of the volume.

0 commit comments

Comments
 (0)