Skip to content

Commit 91ccc79

Browse files
Alexander-Kitakavya498
authored andcommitted
granular CRN for volumes
1 parent 405900d commit 91ccc79

10 files changed

+162
-11
lines changed

ibm/service/power/data_source_ibm_pi_instance_volumes.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ package power
55

66
import (
77
"context"
8+
"log"
89

910
"github.com/IBM-Cloud/power-go-client/clients/instance"
1011
"github.com/IBM-Cloud/power-go-client/power/models"
1112
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
13+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1214
"github.com/hashicorp/go-uuid"
1315
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1416
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -49,6 +51,11 @@ func DataSourceIBMPIInstanceVolumes() *schema.Resource {
4951
Description: "Indicates if the volume is boot capable.",
5052
Type: schema.TypeBool,
5153
},
54+
Attr_CRN: {
55+
Computed: true,
56+
Description: "The CRN of this resource.",
57+
Type: schema.TypeString,
58+
},
5259
Attr_Href: {
5360
Computed: true,
5461
Description: "The hyper link of the volume.",
@@ -89,6 +96,13 @@ func DataSourceIBMPIInstanceVolumes() *schema.Resource {
8996
Description: "The disk type that is used for this volume.",
9097
Type: schema.TypeString,
9198
},
99+
Attr_UserTags: {
100+
Computed: true,
101+
Description: "List of user tags attached to the resource.",
102+
Elem: &schema.Schema{Type: schema.TypeString},
103+
Set: schema.HashString,
104+
Type: schema.TypeSet,
105+
},
92106
},
93107
},
94108
Type: schema.TypeList,
@@ -114,12 +128,12 @@ func dataSourceIBMPIInstanceVolumesRead(ctx context.Context, d *schema.ResourceD
114128
var clientgenU, _ = uuid.GenerateUUID()
115129
d.SetId(clientgenU)
116130
d.Set(Attr_BootVolumeID, *volumedata.Volumes[0].VolumeID)
117-
d.Set(Attr_InstanceVolumes, flattenVolumesInstances(volumedata.Volumes))
131+
d.Set(Attr_InstanceVolumes, flattenVolumesInstances(volumedata.Volumes, meta))
118132

119133
return nil
120134
}
121135

122-
func flattenVolumesInstances(list []*models.VolumeReference) []map[string]interface{} {
136+
func flattenVolumesInstances(list []*models.VolumeReference, meta interface{}) []map[string]interface{} {
123137
result := make([]map[string]interface{}, 0, len(list))
124138
for _, i := range list {
125139
l := map[string]interface{}{
@@ -133,6 +147,14 @@ func flattenVolumesInstances(list []*models.VolumeReference) []map[string]interf
133147
Attr_State: *i.State,
134148
Attr_Type: *i.DiskType,
135149
}
150+
if i.Crn != "" {
151+
l[Attr_CRN] = i.Crn
152+
tags, err := flex.GetGlobalTagsUsingCRN(meta, string(i.Crn), "", UserTagType)
153+
if err != nil {
154+
log.Printf("Error on get of volume (%s) user_tags: %s", *i.VolumeID, err)
155+
}
156+
l[Attr_UserTags] = tags
157+
}
136158
result = append(result, l)
137159
}
138160
return result

ibm/service/power/data_source_ibm_pi_volume.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ package power
55

66
import (
77
"context"
8+
"log"
89

910
"github.com/IBM-Cloud/power-go-client/clients/instance"
1011
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
12+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1214
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1315
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -47,6 +49,11 @@ func DataSourceIBMPIVolume() *schema.Resource {
4749
Description: "Indicates if the volume is boot capable.",
4850
Type: schema.TypeBool,
4951
},
52+
Attr_CRN: {
53+
Computed: true,
54+
Description: "The CRN of this resource.",
55+
Type: schema.TypeString,
56+
},
5057
Attr_ConsistencyGroupName: {
5158
Computed: true,
5259
Description: "Consistency group name if volume is a part of volume group.",
@@ -112,6 +119,13 @@ func DataSourceIBMPIVolume() *schema.Resource {
112119
Description: "The state of the volume.",
113120
Type: schema.TypeString,
114121
},
122+
Attr_UserTags: {
123+
Computed: true,
124+
Description: "List of user tags attached to the resource.",
125+
Elem: &schema.Schema{Type: schema.TypeString},
126+
Set: schema.HashString,
127+
Type: schema.TypeSet,
128+
},
115129
Attr_VolumePool: {
116130
Computed: true,
117131
Description: "Volume pool, name of storage pool where the volume is located.",
@@ -144,6 +158,14 @@ func dataSourceIBMPIVolumeRead(ctx context.Context, d *schema.ResourceData, meta
144158
d.Set(Attr_AuxiliaryVolumeName, volumedata.AuxVolumeName)
145159
d.Set(Attr_Bootable, volumedata.Bootable)
146160
d.Set(Attr_ConsistencyGroupName, volumedata.ConsistencyGroupName)
161+
if volumedata.Crn != "" {
162+
d.Set(Attr_CRN, volumedata.Crn)
163+
tags, err := flex.GetTagsUsingCRN(meta, string(volumedata.Crn))
164+
if err != nil {
165+
log.Printf("Error on get of pi volume (%s) user_tags: %s", *volumedata.VolumeID, err)
166+
}
167+
d.Set(Attr_UserTags, tags)
168+
}
147169
d.Set(Attr_DiskType, volumedata.DiskType)
148170
d.Set(Attr_GroupID, volumedata.GroupID)
149171
d.Set(Attr_IOThrottleRate, volumedata.IoThrottleRate)

ibm/service/power/data_source_ibm_pi_volume_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ import (
1313
)
1414

1515
func TestAccIBMPIVolumeDataSource_basic(t *testing.T) {
16+
volumeRes := "data.ibm_pi_volume.testacc_ds_volume"
1617
resource.Test(t, resource.TestCase{
1718
PreCheck: func() { acc.TestAccPreCheck(t) },
1819
Providers: acc.TestAccProviders,
1920
Steps: []resource.TestStep{
2021
{
2122
Config: testAccCheckIBMPIVolumeDataSourceConfig(),
2223
Check: resource.ComposeTestCheckFunc(
23-
resource.TestCheckResourceAttrSet("data.ibm_pi_volume.testacc_ds_volume", "id"),
24+
resource.TestCheckResourceAttrSet(volumeRes, "id"),
2425
),
2526
},
2627
},

ibm/service/power/resource_ibm_pi_volume.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,13 @@ func ResourceIBMPIVolume() *schema.Resource {
8787
Optional: true,
8888
Type: schema.TypeBool,
8989
},
90+
Arg_UserTags: {
91+
Description: "The user tags attached to this resource.",
92+
Elem: &schema.Schema{Type: schema.TypeString},
93+
Optional: true,
94+
Set: schema.HashString,
95+
Type: schema.TypeSet,
96+
},
9097
Arg_VolumeName: {
9198
Description: "The name of the volume.",
9299
Required: true,
@@ -136,6 +143,11 @@ func ResourceIBMPIVolume() *schema.Resource {
136143
Description: "The consistency group name if volume is a part of volume group.",
137144
Type: schema.TypeString,
138145
},
146+
Attr_CRN: {
147+
Computed: true,
148+
Description: "The CRN of this resource.",
149+
Type: schema.TypeString,
150+
},
139151
Attr_DeleteOnTermination: {
140152
Computed: true,
141153
Description: "Indicates if the volume should be deleted when the server terminates.",
@@ -265,6 +277,9 @@ func resourceIBMPIVolumeCreate(ctx context.Context, d *schema.ResourceData, meta
265277
}
266278

267279
}
280+
if v, ok := d.GetOk(Arg_UserTags); ok {
281+
body.UserTags = flex.FlattenSet(v.(*schema.Set))
282+
}
268283

269284
client := instance.NewIBMPIVolumeClient(ctx, sess, cloudInstanceID)
270285
vol, err := client.CreateVolume(body)
@@ -280,6 +295,14 @@ func resourceIBMPIVolumeCreate(ctx context.Context, d *schema.ResourceData, meta
280295
return diag.FromErr(err)
281296
}
282297

298+
if _, ok := d.GetOk(Arg_UserTags); ok {
299+
oldList, newList := d.GetChange(Arg_UserTags)
300+
err := flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, string(vol.Crn), "", UserTagType)
301+
if err != nil {
302+
log.Printf("Error on update of volume (%s) pi_user_tags during creation: %s", volumeid, err)
303+
}
304+
}
305+
283306
return resourceIBMPIVolumeRead(ctx, d, meta)
284307
}
285308

@@ -304,6 +327,14 @@ func resourceIBMPIVolumeRead(ctx context.Context, d *schema.ResourceData, meta i
304327
if vol.VolumeID != nil {
305328
d.Set(Attr_VolumeID, vol.VolumeID)
306329
}
330+
if vol.Crn != "" {
331+
d.Set(Attr_CRN, vol.Crn)
332+
tags, err := flex.GetGlobalTagsUsingCRN(meta, string(vol.Crn), "", UserTagType)
333+
if err != nil {
334+
log.Printf("Error on get of volume (%s) pi_user_tags: %s", *vol.VolumeID, err)
335+
}
336+
d.Set(Arg_UserTags, tags)
337+
}
307338
d.Set(Arg_VolumeName, vol.Name)
308339
d.Set(Arg_VolumePool, vol.VolumePool)
309340
if vol.Shareable != nil {
@@ -383,6 +414,16 @@ func resourceIBMPIVolumeUpdate(ctx context.Context, d *schema.ResourceData, meta
383414
}
384415
}
385416

417+
if d.HasChange(Arg_UserTags) {
418+
crn := d.Get(Attr_CRN)
419+
if crn != nil && crn != "" {
420+
oldList, newList := d.GetChange(Arg_UserTags)
421+
err := flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, crn.(string), "", UserTagType)
422+
if err != nil {
423+
log.Printf("Error on update of pi volume (%s) pi_user_tags: %s", volumeID, err)
424+
}
425+
}
426+
}
386427
return resourceIBMPIVolumeRead(ctx, d, meta)
387428
}
388429

ibm/service/power/resource_ibm_pi_volume_clone.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ func ResourceIBMPIVolumeClone() *schema.Resource {
5252
Optional: true,
5353
Type: schema.TypeBool,
5454
},
55+
Arg_UserTags: {
56+
Description: "The user tags attached to this resource.",
57+
Elem: &schema.Schema{Type: schema.TypeString},
58+
ForceNew: true,
59+
Optional: true,
60+
Set: schema.HashString,
61+
Type: schema.TypeSet,
62+
},
5563
Arg_VolumeCloneName: {
5664
Description: "The base name of the newly cloned volume(s).",
5765
ForceNew: true,
@@ -135,6 +143,10 @@ func resourceIBMPIVolumeCloneCreate(ctx context.Context, d *schema.ResourceData,
135143
body.TargetReplicationEnabled = flex.PtrToBool(d.Get(Arg_ReplicationEnabled).(bool))
136144
}
137145

146+
if v, ok := d.GetOk(Arg_UserTags); ok {
147+
body.UserTags = flex.FlattenSet(v.(*schema.Set))
148+
}
149+
138150
client := instance.NewIBMPICloneVolumeClient(ctx, sess, cloudInstanceID)
139151
volClone, err := client.Create(body)
140152
if err != nil {

ibm/service/power/resource_ibm_pi_volume_test.go

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
func TestAccIBMPIVolumebasic(t *testing.T) {
2323
name := fmt.Sprintf("tf-pi-volume-%d", acctest.RandIntRange(10, 100))
24+
volumeRes := "ibm_pi_volume.power_volume"
2425
resource.Test(t, resource.TestCase{
2526
PreCheck: func() { acc.TestAccPreCheck(t) },
2627
Providers: acc.TestAccProviders,
@@ -29,19 +30,16 @@ func TestAccIBMPIVolumebasic(t *testing.T) {
2930
{
3031
Config: testAccCheckIBMPIVolumeConfig(name),
3132
Check: resource.ComposeTestCheckFunc(
32-
testAccCheckIBMPIVolumeExists("ibm_pi_volume.power_volume"),
33-
resource.TestCheckResourceAttr(
34-
"ibm_pi_volume.power_volume", "pi_volume_name", name),
33+
testAccCheckIBMPIVolumeExists(volumeRes),
34+
resource.TestCheckResourceAttr(volumeRes, "pi_volume_name", name),
3535
),
3636
},
3737
{
3838
Config: testAccCheckIBMPIVolumeSizeConfig(name),
3939
Check: resource.ComposeTestCheckFunc(
40-
testAccCheckIBMPIVolumeExists("ibm_pi_volume.power_volume"),
41-
resource.TestCheckResourceAttr(
42-
"ibm_pi_volume.power_volume", "pi_volume_name", name),
43-
resource.TestCheckResourceAttr(
44-
"ibm_pi_volume.power_volume", "pi_volume_size", "30"),
40+
testAccCheckIBMPIVolumeExists(volumeRes),
41+
resource.TestCheckResourceAttr(volumeRes, "pi_volume_name", name),
42+
resource.TestCheckResourceAttr(volumeRes, "pi_volume_size", "30"),
4543
),
4644
},
4745
},
@@ -260,3 +258,50 @@ func testAccCheckIBMPIVolumeUpdateBasicConfig(name, piCloudInstanceId, piStorage
260258
pi_volume_type = "%[4]v"
261259
}`, name, piCloudInstanceId, piStoragePool, piStorageType)
262260
}
261+
262+
func TestAccIBMPIVolumeUserTags(t *testing.T) {
263+
name := fmt.Sprintf("tf-pi-volume-%d", acctest.RandIntRange(10, 100))
264+
volumeRes := "ibm_pi_volume.power_volume"
265+
userTagsString := `["env:dev","test_tag"]`
266+
userTagsStringUpdated := `["env:dev","test_tag","test_tag2"]`
267+
resource.Test(t, resource.TestCase{
268+
PreCheck: func() { acc.TestAccPreCheck(t) },
269+
Providers: acc.TestAccProviders,
270+
CheckDestroy: testAccCheckIBMPIVolumeDestroy,
271+
Steps: []resource.TestStep{
272+
{
273+
Config: testAccCheckIBMPIVolumeUserTagsConfig(name, userTagsString),
274+
Check: resource.ComposeTestCheckFunc(
275+
testAccCheckIBMPIVolumeExists(volumeRes),
276+
resource.TestCheckResourceAttr(volumeRes, "pi_volume_name", name),
277+
resource.TestCheckResourceAttr(volumeRes, "pi_user_tags.#", "2"),
278+
resource.TestCheckTypeSetElemAttr(volumeRes, "pi_user_tags.*", "env:dev"),
279+
resource.TestCheckTypeSetElemAttr(volumeRes, "pi_user_tags.*", "test_tag"),
280+
),
281+
},
282+
{
283+
Config: testAccCheckIBMPIVolumeUserTagsConfig(name, userTagsStringUpdated),
284+
Check: resource.ComposeTestCheckFunc(
285+
testAccCheckIBMPIVolumeExists(volumeRes),
286+
resource.TestCheckResourceAttr(volumeRes, "pi_volume_name", name),
287+
resource.TestCheckResourceAttr(volumeRes, "pi_user_tags.#", "3"),
288+
resource.TestCheckTypeSetElemAttr(volumeRes, "pi_user_tags.*", "env:dev"),
289+
resource.TestCheckTypeSetElemAttr(volumeRes, "pi_user_tags.*", "test_tag"),
290+
resource.TestCheckTypeSetElemAttr(volumeRes, "pi_user_tags.*", "test_tag2"),
291+
),
292+
},
293+
},
294+
})
295+
}
296+
297+
func testAccCheckIBMPIVolumeUserTagsConfig(name string, userTagsString string) string {
298+
return fmt.Sprintf(`
299+
resource "ibm_pi_volume" "power_volume" {
300+
pi_cloud_instance_id = "%[2]s"
301+
pi_volume_name = "%[1]s"
302+
pi_volume_shareable = true
303+
pi_volume_size = 20
304+
pi_volume_type = "tier1"
305+
pi_user_tags = %[3]s
306+
}`, name, acc.Pi_cloud_instance_id, userTagsString)
307+
}

website/docs/d/pi_instance_volumes.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ In addition to all argument reference list, you can access the following attribu
5353

5454
Nested scheme for `instance_volumes`:
5555
- `bootable`- (Boolean) Indicates if the volume is boot capable.
56+
- `crn` - (String) The CRN of this resource.
5657
- `href` - (String) The hyper link of the volume.
5758
- `id` - (String) The unique identifier of the volume.
5859
- `name` - (String) The name of the volume.
@@ -61,3 +62,4 @@ In addition to all argument reference list, you can access the following attribu
6162
- `size` - (Integer) The size of this volume in GB.
6263
- `state` - (String) The state of the volume.
6364
- `type` - (String) The disk type that is used for this volume.
65+
- `user_tags` - (List) List of user tags attached to the resource.

website/docs/d/pi_volume.html.markdown

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ In addition to all argument reference list, you can access the following attribu
5252
- `auxiliary_volume_name` - (String) The auxiliary volume name.
5353
- `bootable` - (Boolean) Indicates if the volume is boot capable.
5454
- `consistency_group_name` - (String) Consistency group name if volume is a part of volume group.
55+
- `crn` - (String) The CRN of this resource.
5556
- `disk_type` - (String) The disk type that is used for the volume.
5657
- `group_id` - (String) The volume group id in which the volume belongs.
5758
- `id` - (String) The unique identifier of the volume.
@@ -65,5 +66,7 @@ In addition to all argument reference list, you can access the following attribu
6566
- `shareable` - (String) Indicates if the volume is shareable between VMs.
6667
- `size` - (Integer) The size of the volume in GB.
6768
- `state` - (String) The state of the volume.
69+
- `user_tags` - (List) List of user tags attached to the resource.
6870
- `volume_pool` - (String) Volume pool, name of storage pool where the volume is located.
6971
- `wwn` - (String) The world wide name of the volume.
72+

website/docs/r/pi_volume.html.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ Review the argument references that you can specify for your resource.
5959
- `pi_anti_affinity_volumes`- (Optional, String) List of volumes to base volume anti-affinity policy against; required if requesting `anti-affinity` and `pi_anti_affinity_instances` is not provided.
6060
- `pi_cloud_instance_id` - (Required, String) The GUID of the service instance associated with an account.
6161
- `pi_replication_enabled` - (Optional, Boolean) Indicates if the volume should be replication enabled or not.
62+
- `pi_user_tags` - (Optional, List) The user tags attached to this resource.
6263
- `pi_volume_name` - (Required, String) The name of the volume.
6364
- `pi_volume_pool` - (Optional, String) Volume pool where the volume will be created; if provided then `pi_affinity_policy` values will be ignored.
6465
- `pi_volume_shareable` - (Required, Boolean) If set to **true**, the volume can be shared across Power Systems Virtual Server instances. If set to **false**, you can attach it only to one instance.
@@ -72,6 +73,7 @@ In addition to all argument reference list, you can access the following attribu
7273
- `auxiliary` - (Boolean) Indicates if the volume is auxiliary or not.
7374
- `auxiliary_volume_name` - (String) The auxiliary volume name.
7475
- `consistency_group_name` - (String) The consistency group name if volume is a part of volume group.
76+
- `crn` - (String) The CRN of this resource.
7577
- `delete_on_termination` - (Boolean) Indicates if the volume should be deleted when the server terminates.
7678
- `group_id` - (String) The volume group id to which volume belongs.
7779
- `id` - (String) The unique identifier of the volume. The ID is composed of `<cloud_instance_id>/<volume_id>`.

website/docs/r/pi_volume_clone.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Review the argument references that you can specify for your resource.
5454
- `pi_cloud_instance_id` - (Required, String) The GUID of the service instance associated with an account.
5555
- `pi_replication_enabled` - (Optional, Boolean) Indicates whether the cloned volume should have replication enabled. If no value is provided, it will default to the replication status of the source volume(s).
5656
- `pi_target_storage_tier` - (Optional, String) The storage tier for the cloned volume(s). To get a list of available storage tiers, please use the [ibm_pi_storage_types_capacity](https://registry.terraform.io/providers/IBM-Cloud/ibm/latest/docs/data-sources/pi_storage_types_capacity) data source.
57+
- `pi_user_tags` - (Optional, List) The user tags attached to this resource.
5758
- `pi_volume_clone_name` - (Required, String) The base name of the newly cloned volume(s).
5859
- `pi_volume_ids` - (Required, Set of String) List of volumes to be cloned.
5960

0 commit comments

Comments
 (0)