Skip to content

Commit 3301c13

Browse files
committed
Add volumes data source
1 parent 4e073d5 commit 3301c13

File tree

6 files changed

+370
-5
lines changed

6 files changed

+370
-5
lines changed

ibm/acctest/acctest.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ var (
250250
Pi_placement_group_name string
251251
Pi_remote_id string
252252
Pi_remote_type string
253-
Pi_replication_volume_name string
253+
Pi_replication_volume_id string
254254
Pi_resource_group_id string
255255
Pi_route_filter_id string
256256
Pi_route_id string
@@ -1267,10 +1267,10 @@ func init() {
12671267
fmt.Println("[INFO] Set the environment variable PI_VOLUME_ID for testing ibm_pi_volume_flash_copy_mappings resource else it is set to default value 'terraform-test-power'")
12681268
}
12691269

1270-
Pi_replication_volume_name = os.Getenv("PI_REPLICATION_VOLUME_NAME")
1271-
if Pi_replication_volume_name == "" {
1272-
Pi_replication_volume_name = "terraform-test-power"
1273-
fmt.Println("[INFO] Set the environment variable PI_REPLICATION_VOLUME_NAME for testing ibm_pi_volume resource else it is set to default value 'terraform-test-power'")
1270+
Pi_replication_volume_id = os.Getenv("PI_REPLICATION_VOLUME_ID")
1271+
if Pi_replication_volume_id == "" {
1272+
Pi_replication_volume_id = "terraform-test-power"
1273+
fmt.Println("[INFO] Set the environment variable PI_REPLICATION_VOLUME_ID for testing ibm_pi_volume resource else it is set to default value 'terraform-test-power'")
12741274
}
12751275

12761276
Pi_volume_onboarding_source_crn = os.Getenv("PI_VOLUME_ONBARDING_SOURCE_CRN")

ibm/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,7 @@ func Provider() *schema.Provider {
791791
"ibm_pi_volume_snapshot": power.DataSourceIBMPIVolumeSnapshot(),
792792
"ibm_pi_volume_snapshots": power.DataSourceIBMPIVolumeSnapshots(),
793793
"ibm_pi_volume": power.DataSourceIBMPIVolume(),
794+
"ibm_pi_volumes": power.DataSourceIBMPIVolumes(),
794795
"ibm_pi_workspace": power.DatasourceIBMPIWorkspace(),
795796
"ibm_pi_workspaces": power.DatasourceIBMPIWorkspaces(),
796797

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
2+
// Licensed under the Mozilla Public License v2.0
3+
4+
package power
5+
6+
import (
7+
"context"
8+
"log"
9+
10+
"github.com/IBM-Cloud/power-go-client/clients/instance"
11+
"github.com/IBM-Cloud/power-go-client/power/models"
12+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
13+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
14+
"github.com/hashicorp/go-uuid"
15+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
16+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
18+
)
19+
20+
func DataSourceIBMPIVolumes() *schema.Resource {
21+
return &schema.Resource{
22+
ReadContext: dataSourceIBMPIVolumesRead,
23+
Schema: map[string]*schema.Schema{
24+
// Arguments
25+
Arg_CloudInstanceID: {
26+
Description: "The GUID of the service instance associated with an account.",
27+
Required: true,
28+
Type: schema.TypeString,
29+
ValidateFunc: validation.NoZeroValues,
30+
},
31+
32+
// Attributes
33+
Attr_Volumes: {
34+
Computed: true,
35+
Description: "List of all volumes.",
36+
Elem: &schema.Resource{
37+
Schema: map[string]*schema.Schema{
38+
Attr_Auxiliary: {
39+
Computed: true,
40+
Description: "Indicates if the volume is auxiliary or not.",
41+
Type: schema.TypeBool,
42+
},
43+
Attr_AuxiliaryVolumeName: {
44+
Computed: true,
45+
Description: "The auxiliary volume name.",
46+
Type: schema.TypeString,
47+
},
48+
Attr_Bootable: {
49+
Computed: true,
50+
Description: "Indicates if the volume is boot capable.",
51+
Type: schema.TypeBool,
52+
},
53+
Attr_CRN: {
54+
Computed: true,
55+
Description: "The CRN of this resource.",
56+
Type: schema.TypeString,
57+
},
58+
Attr_ConsistencyGroupName: {
59+
Computed: true,
60+
Description: "Consistency group name if volume is a part of volume group.",
61+
Type: schema.TypeString,
62+
},
63+
Attr_CreationDate: {
64+
Computed: true,
65+
Description: "Date volume was created.",
66+
Type: schema.TypeString,
67+
},
68+
Attr_DiskType: {
69+
Computed: true,
70+
Description: "The disk type that is used for the volume.",
71+
Type: schema.TypeString,
72+
},
73+
Attr_FreezeTime: {
74+
Computed: true,
75+
Description: "The freeze time of remote copy.",
76+
Type: schema.TypeString,
77+
},
78+
Attr_GroupID: {
79+
Computed: true,
80+
Description: "The volume group id in which the volume belongs.",
81+
Type: schema.TypeString,
82+
},
83+
Attr_ID: {
84+
Computed: true,
85+
Description: "The unique identifier of the volume.",
86+
Type: schema.TypeString,
87+
},
88+
Attr_IOThrottleRate: {
89+
Computed: true,
90+
Description: "Amount of iops assigned to the volume",
91+
Type: schema.TypeString,
92+
},
93+
Attr_LastUpdateDate: {
94+
Computed: true,
95+
Description: "The last updated date of the volume.",
96+
Type: schema.TypeString,
97+
},
98+
Attr_MasterVolumeName: {
99+
Computed: true,
100+
Description: "The master volume name.",
101+
Type: schema.TypeString,
102+
},
103+
Attr_MirroringState: {
104+
Computed: true,
105+
Description: "Mirroring state for replication enabled volume.",
106+
Type: schema.TypeString,
107+
},
108+
Attr_OutOfBandDeleted: {
109+
Computed: true,
110+
Description: "Indicates if the volume does not exist on storage controller.",
111+
Type: schema.TypeBool,
112+
},
113+
Attr_PrimaryRole: {
114+
Computed: true,
115+
Description: "Indicates whether master/auxiliary volume is playing the primary role.",
116+
Type: schema.TypeString,
117+
},
118+
Attr_ReplicationEnabled: {
119+
Computed: true,
120+
Description: "Indicates if the volume should be replication enabled or not.",
121+
Type: schema.TypeBool,
122+
},
123+
Attr_ReplicationSites: {
124+
Computed: true,
125+
Description: "List of replication sites for volume replication.",
126+
Elem: &schema.Schema{Type: schema.TypeString},
127+
Type: schema.TypeList,
128+
},
129+
Attr_ReplicationStatus: {
130+
Computed: true,
131+
Description: "The replication status of the volume.",
132+
Type: schema.TypeString,
133+
},
134+
Attr_ReplicationType: {
135+
Computed: true,
136+
Description: "The replication type of the volume, metro or global.",
137+
Type: schema.TypeString,
138+
},
139+
Attr_Shareable: {
140+
Computed: true,
141+
Description: "Indicates if the volume is shareable between VMs.",
142+
Type: schema.TypeBool,
143+
},
144+
Attr_Size: {
145+
Computed: true,
146+
Description: "The size of the volume in GB.",
147+
Type: schema.TypeInt,
148+
},
149+
Attr_State: {
150+
Computed: true,
151+
Description: "The state of the volume.",
152+
Type: schema.TypeString,
153+
},
154+
Attr_UserTags: {
155+
Computed: true,
156+
Description: "List of user tags attached to the resource.",
157+
Elem: &schema.Schema{Type: schema.TypeString},
158+
Set: schema.HashString,
159+
Type: schema.TypeSet,
160+
},
161+
Attr_VolumeName: {
162+
Computed: true,
163+
Description: "The name of the volume.",
164+
Type: schema.TypeString,
165+
},
166+
Attr_VolumePool: {
167+
Computed: true,
168+
Description: "Volume pool, name of storage pool where the volume is located.",
169+
Type: schema.TypeString,
170+
},
171+
Attr_WWN: {
172+
Computed: true,
173+
Description: "The world wide name of the volume.",
174+
Type: schema.TypeString,
175+
},
176+
},
177+
},
178+
Type: schema.TypeList,
179+
},
180+
},
181+
}
182+
}
183+
184+
func dataSourceIBMPIVolumesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
185+
sess, err := meta.(conns.ClientSession).IBMPISession()
186+
if err != nil {
187+
return diag.FromErr(err)
188+
}
189+
190+
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
191+
volumeC := instance.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID)
192+
volumeData, err := volumeC.GetAll()
193+
if err != nil {
194+
return diag.FromErr(err)
195+
}
196+
197+
var clientgenU, _ = uuid.GenerateUUID()
198+
d.SetId(clientgenU)
199+
d.Set(Attr_Volumes, flattenVolumes(volumeData.Volumes, meta))
200+
return nil
201+
}
202+
203+
func flattenVolumes(list []*models.VolumeReference, meta any) []map[string]any {
204+
log.Printf("Calling the flattenVolumeGroups call with list %d", len(list))
205+
result := make([]map[string]any, 0, len(list))
206+
for _, i := range list {
207+
volume := map[string]any{
208+
Attr_Auxiliary: *i.Auxiliary,
209+
Attr_AuxiliaryVolumeName: i.AuxVolumeName,
210+
Attr_Bootable: *i.Bootable,
211+
Attr_ConsistencyGroupName: i.ConsistencyGroupName,
212+
Attr_CreationDate: i.CreationDate.String(),
213+
Attr_DiskType: *i.DiskType,
214+
Attr_GroupID: i.GroupID,
215+
Attr_ID: *i.VolumeID,
216+
Attr_IOThrottleRate: i.IoThrottleRate,
217+
Attr_LastUpdateDate: i.LastUpdateDate.String(),
218+
Attr_MasterVolumeName: i.MasterVolumeName,
219+
Attr_MirroringState: i.MirroringState,
220+
Attr_OutOfBandDeleted: i.OutOfBandDeleted,
221+
Attr_PrimaryRole: i.PrimaryRole,
222+
Attr_ReplicationEnabled: *i.ReplicationEnabled,
223+
Attr_ReplicationStatus: i.ReplicationStatus,
224+
Attr_ReplicationType: i.ReplicationType,
225+
Attr_Shareable: *i.Shareable,
226+
Attr_Size: *i.Size,
227+
Attr_State: *i.State,
228+
Attr_VolumeName: *i.Name,
229+
Attr_VolumePool: i.VolumePool,
230+
Attr_WWN: *i.Wwn,
231+
}
232+
if i.FreezeTime != nil {
233+
volume[Attr_FreezeTime] = i.FreezeTime.String()
234+
}
235+
if len(i.ReplicationSites) > 0 {
236+
volume[Attr_ReplicationSites] = i.ReplicationSites
237+
}
238+
volumeCRN := string(i.Crn)
239+
if volumeCRN != "" {
240+
volume[Attr_CRN] = i.Crn
241+
tags, err := flex.GetGlobalTagsUsingCRN(meta, volumeCRN, "", UserTagType)
242+
if err != nil {
243+
log.Printf("Error on get of pi volume (%s) user_tags: %s", *i.VolumeID, err)
244+
}
245+
volume[Attr_UserTags] = tags
246+
}
247+
result = append(result, volume)
248+
}
249+
return result
250+
}
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 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+
}

ibm/service/power/ibm_pi_constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,7 @@ const (
550550
Attr_VolumeGroupStatus = "volume_group_status"
551551
Attr_VolumeID = "volume_id"
552552
Attr_VolumeIDs = "volume_ids"
553+
Attr_VolumeName = "volume_name"
553554
Attr_VolumePool = "volume_pool"
554555
Attr_Volumes = "volumes"
555556
Attr_VolumeSnapshots = "volume_snapshots"

0 commit comments

Comments
 (0)