From 79886b18831ca02945255a6a0e2eb7afe7a0688f Mon Sep 17 00:00:00 2001 From: Axel Ismirlian Date: Tue, 1 Oct 2024 17:20:47 -0500 Subject: [PATCH] Add user tag code to prevent merge conflict --- go.mod | 2 +- go.sum | 4 +- ibm/service/power/ibm_pi_constants.go | 3 ++ .../resource_ibm_pi_instance_snapshot.go | 36 +++++++++++++ .../resource_ibm_pi_instance_snapshot_test.go | 51 +++++++++++++++++++ ibm/service/power/resource_ibm_pi_snapshot.go | 36 +++++++++++++ .../power/resource_ibm_pi_snapshot_test.go | 51 +++++++++++++++++++ .../docs/r/pi_instance_snapshot.html.markdown | 2 + website/docs/r/pi_snapshot.html.markdown | 12 +++-- 9 files changed, 189 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 9331c5dfe6..a784b178ad 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ toolchain go1.22.5 require ( github.com/IBM-Cloud/bluemix-go v0.0.0-20240719075425-078fcb3a55be github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113 - github.com/IBM-Cloud/power-go-client v1.7.0 + github.com/IBM-Cloud/power-go-client v1.8.1 github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca github.com/IBM/appconfiguration-go-admin-sdk v0.3.0 github.com/IBM/appid-management-go-sdk v0.0.0-20210908164609-dd0e0eaf732f diff --git a/go.sum b/go.sum index 5db19a9fe9..6e1a9cdfea 100644 --- a/go.sum +++ b/go.sum @@ -118,8 +118,8 @@ github.com/IBM-Cloud/bluemix-go v0.0.0-20240719075425-078fcb3a55be/go.mod h1:/7h github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113 h1:f2Erqfea1dKpaTFagTJM6W/wnD3JGq/Vn9URh8nuRwk= github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113/go.mod h1:xUQL9SGAjoZFd4GNjrjjtEpjpkgU7RFXRyHesbKTjiY= github.com/IBM-Cloud/ibm-cloud-cli-sdk v0.5.3/go.mod h1:RiUvKuHKTBmBApDMUQzBL14pQUGKcx/IioKQPIcRQjs= -github.com/IBM-Cloud/power-go-client v1.7.0 h1:/GuGwPMTKoCZACfnwt7b6wKr4v32q1VO1AMFGNETRN4= -github.com/IBM-Cloud/power-go-client v1.7.0/go.mod h1:9izycYAmNQ+NAdVPXDC3fHYxqWLjlR2YiwqKYveMv5Y= +github.com/IBM-Cloud/power-go-client v1.8.1 h1:tx1aPJmIQrNru1MD1VHGNasGx3eRIs0zzPZ0KvdFQrg= +github.com/IBM-Cloud/power-go-client v1.8.1/go.mod h1:N4RxrsMUvBQjSQ/qPk0iMZ8zK+fZPRTnHi/gTaASw0g= github.com/IBM-Cloud/softlayer-go v1.0.5-tf h1:koUAyF9b6X78lLLruGYPSOmrfY2YcGYKOj/Ug9nbKNw= github.com/IBM-Cloud/softlayer-go v1.0.5-tf/go.mod h1:6HepcfAXROz0Rf63krk5hPZyHT6qyx2MNvYyHof7ik4= github.com/IBM/apigateway-go-sdk v0.0.0-20210714141226-a5d5d49caaca h1:crniVcf+YcmgF03NmmfonXwSQ73oJF+IohFYBwknMxs= diff --git a/ibm/service/power/ibm_pi_constants.go b/ibm/service/power/ibm_pi_constants.go index 5ab1ac7751..2fff109039 100644 --- a/ibm/service/power/ibm_pi_constants.go +++ b/ibm/service/power/ibm_pi_constants.go @@ -86,6 +86,7 @@ const ( Arg_SysType = "pi_sys_type" Arg_TargetStorageTier = "pi_target_storage_tier" Arg_UserData = "pi_user_data" + Arg_UserTags = "pi_user_tags" Arg_VirtualCoresAssigned = "pi_virtual_cores_assigned" Arg_VirtualOpticalDevice = "pi_virtual_optical_device" Arg_VolumeCloneName = "pi_volume_clone_name" @@ -372,6 +373,7 @@ const ( Attr_UsedIPPercent = "used_ip_percent" Attr_UsedMemory = "used_memory" Attr_UserIPAddress = "user_ip_address" + Attr_UserTags = "user_tags" Attr_VCPUs = "vcpus" Attr_Vendor = "vendor" Attr_VirtualCoresAssigned = "virtual_cores_assigned" @@ -444,6 +446,7 @@ const ( Shared = "shared" Soft = "soft" Suffix = "suffix" + UserTagType = "user" Vlan = "vlan" vSCSI = "vSCSI" Warning = "WARNING" diff --git a/ibm/service/power/resource_ibm_pi_instance_snapshot.go b/ibm/service/power/resource_ibm_pi_instance_snapshot.go index 0e3043597a..6debb7d1f9 100644 --- a/ibm/service/power/resource_ibm_pi_instance_snapshot.go +++ b/ibm/service/power/resource_ibm_pi_instance_snapshot.go @@ -58,6 +58,13 @@ func ResourceIBMPIInstanceSnapshot() *schema.Resource { Type: schema.TypeString, ValidateFunc: validation.NoZeroValues, }, + Arg_UserTags: { + Description: "The user tags attached to this resource.", + Elem: &schema.Schema{Type: schema.TypeString}, + Optional: true, + Set: schema.HashString, + Type: schema.TypeSet, + }, Arg_VolumeIDs: { Description: "A list of volume IDs of the instance that will be part of the snapshot. If none are provided, then all the volumes of the instance will be part of the snapshot.", DiffSuppressFunc: flex.ApplyOnce, @@ -73,6 +80,11 @@ func ResourceIBMPIInstanceSnapshot() *schema.Resource { Description: "Creation date of the snapshot.", Type: schema.TypeString, }, + Attr_CRN: { + Computed: true, + Description: "The CRN of this resource.", + Type: schema.TypeString, + }, Attr_LastUpdateDate: { Computed: true, Description: "The last updated date of the snapshot.", @@ -123,6 +135,12 @@ func resourceIBMPIInstanceSnapshotCreate(ctx context.Context, d *schema.Resource log.Printf("no volumeids provided. Will snapshot the entire instance") } + if v, ok := d.GetOk(Arg_UserTags); ok { + if len(flex.FlattenSet(v.(*schema.Set))) > 0 { + snapshotBody.UserTags = flex.FlattenSet(v.(*schema.Set)) + } + } + snapshotResponse, err := client.CreatePvmSnapShot(instanceid, snapshotBody) if err != nil { log.Printf("[DEBUG] err %s", err) @@ -159,6 +177,14 @@ func resourceIBMPIInstanceSnapshotRead(ctx context.Context, d *schema.ResourceDa d.Set(Arg_SnapshotName, snapshotdata.Name) d.Set(Attr_CreationDate, snapshotdata.CreationDate.String()) + if snapshotdata.Crn != "" { + d.Set(Attr_CRN, snapshotdata.Crn) + tags, err := flex.GetGlobalTagsUsingCRN(meta, string(snapshotdata.Crn), "", UserTagType) + if err != nil { + log.Printf("Error on get of pi snapshot (%s) pi_user_tags: %s", *snapshotdata.SnapshotID, err) + } + d.Set(Arg_UserTags, tags) + } d.Set(Attr_LastUpdateDate, snapshotdata.LastUpdateDate.String()) d.Set(Attr_SnapshotID, *snapshotdata.SnapshotID) d.Set(Attr_Status, snapshotdata.Status) @@ -196,6 +222,16 @@ func resourceIBMPIInstanceSnapshotUpdate(ctx context.Context, d *schema.Resource } } + if d.HasChange(Arg_UserTags) { + if crn, ok := d.GetOk(Attr_CRN); ok { + oldList, newList := d.GetChange(Arg_UserTags) + err := flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, crn.(string), "", UserTagType) + if err != nil { + log.Printf("Error on update of pi snapshot (%s) pi_user_tags: %s", snapshotID, err) + } + } + } + return resourceIBMPIInstanceSnapshotRead(ctx, d, meta) } diff --git a/ibm/service/power/resource_ibm_pi_instance_snapshot_test.go b/ibm/service/power/resource_ibm_pi_instance_snapshot_test.go index 73b7657781..692cd94558 100644 --- a/ibm/service/power/resource_ibm_pi_instance_snapshot_test.go +++ b/ibm/service/power/resource_ibm_pi_instance_snapshot_test.go @@ -40,6 +40,45 @@ func TestAccIBMPIInstanceSnapshotbasic(t *testing.T) { }) } +func TestAccIBMPIInstanceSnapshotUserTags(t *testing.T) { + name := fmt.Sprintf("tf-pi-instance-snapshot-%d", acctest.RandIntRange(10, 100)) + snapshotRes := "ibm_pi_snapshot.power_snapshot" + userTagsString := `["env:dev","test_tag"]` + userTagsStringUpdated := `["env:dev","test_tag","test_tag2"]` + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + CheckDestroy: testAccCheckIBMPIInstanceSnapshotDestroy, + Steps: []resource.TestStep{ + { + Config: testAccCheckIBMPIInstanceSnapshotUserTagsConfig(name, power.OK, userTagsString), + Check: resource.ComposeTestCheckFunc( + testAccCheckIBMPIInstanceSnapshotExists(snapshotRes), + resource.TestCheckResourceAttr(snapshotRes, "pi_snap_shot_name", name), + resource.TestCheckResourceAttr(snapshotRes, "status", power.State_Available), + resource.TestCheckResourceAttrSet(snapshotRes, "id"), + resource.TestCheckResourceAttr(snapshotRes, "pi_user_tags.#", "2"), + resource.TestCheckTypeSetElemAttr(snapshotRes, "pi_user_tags.*", "env:dev"), + resource.TestCheckTypeSetElemAttr(snapshotRes, "pi_user_tags.*", "test_tag"), + ), + }, + { + Config: testAccCheckIBMPIInstanceSnapshotUserTagsConfig(name, power.OK, userTagsStringUpdated), + Check: resource.ComposeTestCheckFunc( + testAccCheckIBMPIInstanceSnapshotExists(snapshotRes), + resource.TestCheckResourceAttr(snapshotRes, "pi_snap_shot_name", name), + resource.TestCheckResourceAttr(snapshotRes, "status", power.State_Available), + resource.TestCheckResourceAttrSet(snapshotRes, "id"), + resource.TestCheckResourceAttr(snapshotRes, "pi_user_tags.#", "3"), + resource.TestCheckTypeSetElemAttr(snapshotRes, "pi_user_tags.*", "env:dev"), + resource.TestCheckTypeSetElemAttr(snapshotRes, "pi_user_tags.*", "test_tag"), + resource.TestCheckTypeSetElemAttr(snapshotRes, "pi_user_tags.*", "test_tag2"), + ), + }, + }, + }) +} + func testAccCheckIBMPIInstanceSnapshotDestroy(s *terraform.State) error { sess, err := acc.TestAccProvider.Meta().(conns.ClientSession).IBMPISession() if err != nil { @@ -103,3 +142,15 @@ func testAccCheckIBMPIInstanceSnapshotConfig(name, healthStatus string) string { pi_volume_ids = [ibm_pi_volume.power_volume.volume_id] }`, acc.Pi_cloud_instance_id, name) } + +func testAccCheckIBMPIInstanceSnapshotUserTagsConfig(name, healthStatus string, userTagsString string) string { + return testAccCheckIBMPIInstanceConfig(name, healthStatus) + fmt.Sprintf(` + resource "ibm_pi_snapshot" "power_snapshot"{ + depends_on=[ibm_pi_instance.power_instance] + pi_instance_name = ibm_pi_instance.power_instance.pi_instance_name + pi_cloud_instance_id = "%s" + pi_snap_shot_name = "%s" + pi_user_tags = %s + pi_volume_ids = [ibm_pi_volume.power_volume.volume_id] + }`, acc.Pi_cloud_instance_id, name, userTagsString) +} diff --git a/ibm/service/power/resource_ibm_pi_snapshot.go b/ibm/service/power/resource_ibm_pi_snapshot.go index 7a0a559d64..68e3aac90d 100644 --- a/ibm/service/power/resource_ibm_pi_snapshot.go +++ b/ibm/service/power/resource_ibm_pi_snapshot.go @@ -58,6 +58,13 @@ func ResourceIBMPISnapshot() *schema.Resource { Type: schema.TypeString, ValidateFunc: validation.NoZeroValues, }, + Arg_UserTags: { + Description: "The user tags attached to this resource.", + Elem: &schema.Schema{Type: schema.TypeString}, + Optional: true, + Set: schema.HashString, + Type: schema.TypeSet, + }, Arg_VolumeIDs: { Description: "A list of volume IDs of the instance that will be part of the snapshot. If none are provided, then all the volumes of the instance will be part of the snapshot.", DiffSuppressFunc: flex.ApplyOnce, @@ -73,6 +80,11 @@ func ResourceIBMPISnapshot() *schema.Resource { Description: "Creation date of the snapshot.", Type: schema.TypeString, }, + Attr_CRN: { + Computed: true, + Description: "The CRN of this resource.", + Type: schema.TypeString, + }, Attr_LastUpdateDate: { Computed: true, Description: "The last updated date of the snapshot.", @@ -124,6 +136,12 @@ func resourceIBMPISnapshotCreate(ctx context.Context, d *schema.ResourceData, me log.Printf("no volumeids provided. Will snapshot the entire instance") } + if v, ok := d.GetOk(Arg_UserTags); ok { + if len(flex.FlattenSet(v.(*schema.Set))) > 0 { + snapshotBody.UserTags = flex.FlattenSet(v.(*schema.Set)) + } + } + snapshotResponse, err := client.CreatePvmSnapShot(instanceid, snapshotBody) if err != nil { log.Printf("[DEBUG] err %s", err) @@ -161,6 +179,14 @@ func resourceIBMPISnapshotRead(ctx context.Context, d *schema.ResourceData, meta d.Set(Arg_SnapShotName, snapshotdata.Name) d.Set(Attr_CreationDate, snapshotdata.CreationDate.String()) + if snapshotdata.Crn != "" { + d.Set(Attr_CRN, snapshotdata.Crn) + tags, err := flex.GetGlobalTagsUsingCRN(meta, string(snapshotdata.Crn), "", UserTagType) + if err != nil { + log.Printf("Error on get of pi snapshot (%s) pi_user_tags: %s", *snapshotdata.SnapshotID, err) + } + d.Set(Arg_UserTags, tags) + } d.Set(Attr_LastUpdateDate, snapshotdata.LastUpdateDate.String()) d.Set(Attr_SnapshotID, *snapshotdata.SnapshotID) d.Set(Attr_Status, snapshotdata.Status) @@ -199,6 +225,16 @@ func resourceIBMPISnapshotUpdate(ctx context.Context, d *schema.ResourceData, me } } + if d.HasChange(Arg_UserTags) { + if crn, ok := d.GetOk(Attr_CRN); ok { + oldList, newList := d.GetChange(Arg_UserTags) + err := flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, crn.(string), "", UserTagType) + if err != nil { + log.Printf("Error on update of pi snapshot (%s) pi_user_tags: %s", snapshotID, err) + } + } + } + return resourceIBMPISnapshotRead(ctx, d, meta) } diff --git a/ibm/service/power/resource_ibm_pi_snapshot_test.go b/ibm/service/power/resource_ibm_pi_snapshot_test.go index aff5db751c..11b6222d51 100644 --- a/ibm/service/power/resource_ibm_pi_snapshot_test.go +++ b/ibm/service/power/resource_ibm_pi_snapshot_test.go @@ -40,6 +40,45 @@ func TestAccIBMPIInstanceSnapshotbasicV0(t *testing.T) { }) } +func TestAccIBMPIInstanceSnapshotUserTagsV0(t *testing.T) { + name := fmt.Sprintf("tf-pi-instance-snapshot-%d", acctest.RandIntRange(10, 100)) + snapshotRes := "ibm_pi_snapshot.power_snapshot" + userTagsString := `["env:dev","test_tag"]` + userTagsStringUpdated := `["env:dev","test_tag","test_tag2"]` + resource.Test(t, resource.TestCase{ + PreCheck: func() { acc.TestAccPreCheck(t) }, + Providers: acc.TestAccProviders, + CheckDestroy: testAccCheckIBMPIInstanceSnapshotDestroyV0, + Steps: []resource.TestStep{ + { + Config: testAccCheckIBMPIInstanceSnapshotUserTagsConfigV0(name, power.OK, userTagsString), + Check: resource.ComposeTestCheckFunc( + testAccCheckIBMPIInstanceSnapshotExistsV0(snapshotRes), + resource.TestCheckResourceAttr(snapshotRes, "pi_snap_shot_name", name), + resource.TestCheckResourceAttr(snapshotRes, "status", power.State_Available), + resource.TestCheckResourceAttrSet(snapshotRes, "id"), + resource.TestCheckResourceAttr(snapshotRes, "pi_user_tags.#", "2"), + resource.TestCheckTypeSetElemAttr(snapshotRes, "pi_user_tags.*", "env:dev"), + resource.TestCheckTypeSetElemAttr(snapshotRes, "pi_user_tags.*", "test_tag"), + ), + }, + { + Config: testAccCheckIBMPIInstanceSnapshotUserTagsConfigV0(name, power.OK, userTagsStringUpdated), + Check: resource.ComposeTestCheckFunc( + testAccCheckIBMPIInstanceSnapshotExistsV0(snapshotRes), + resource.TestCheckResourceAttr(snapshotRes, "pi_snap_shot_name", name), + resource.TestCheckResourceAttr(snapshotRes, "status", power.State_Available), + resource.TestCheckResourceAttrSet(snapshotRes, "id"), + resource.TestCheckResourceAttr(snapshotRes, "pi_user_tags.#", "3"), + resource.TestCheckTypeSetElemAttr(snapshotRes, "pi_user_tags.*", "env:dev"), + resource.TestCheckTypeSetElemAttr(snapshotRes, "pi_user_tags.*", "test_tag"), + resource.TestCheckTypeSetElemAttr(snapshotRes, "pi_user_tags.*", "test_tag2"), + ), + }, + }, + }) +} + func testAccCheckIBMPIInstanceSnapshotDestroyV0(s *terraform.State) error { sess, err := acc.TestAccProvider.Meta().(conns.ClientSession).IBMPISession() if err != nil { @@ -103,3 +142,15 @@ func testAccCheckIBMPIInstanceSnapshotConfigV0(name, healthStatus string) string pi_volume_ids = [ibm_pi_volume.power_volume.volume_id] }`, acc.Pi_cloud_instance_id, name) } + +func testAccCheckIBMPIInstanceSnapshotUserTagsConfigV0(name, healthStatus string, userTagsString string) string { + return testAccCheckIBMPIInstanceConfig(name, healthStatus) + fmt.Sprintf(` + resource "ibm_pi_snapshot" "power_snapshot"{ + depends_on=[ibm_pi_instance.power_instance] + pi_instance_name = ibm_pi_instance.power_instance.pi_instance_name + pi_cloud_instance_id = "%s" + pi_snap_shot_name = "%s" + pi_user_tags = %s + pi_volume_ids = [ibm_pi_volume.power_volume.volume_id] + }`, acc.Pi_cloud_instance_id, name, userTagsString) +} diff --git a/website/docs/r/pi_instance_snapshot.html.markdown b/website/docs/r/pi_instance_snapshot.html.markdown index 1f09b9d56f..d55ba7e430 100644 --- a/website/docs/r/pi_instance_snapshot.html.markdown +++ b/website/docs/r/pi_instance_snapshot.html.markdown @@ -56,6 +56,7 @@ Review the argument references that you can specify for your resource. - `pi_description` - (Optional, String) Description of the PVM instance snapshot. - `pi_instance_name` - (Required, String) The name of the instance you want to take a snapshot of. - `pi_snapshot_name` - (Required, String) The unique name of the snapshot. +- `pi_user_tags` - (Optional, List) The user tags attached to this resource. - `pi_volume_ids` - (Optional, String) A list of volume IDs of the instance that will be part of the snapshot. If none are provided, then all the volumes of the instance will be part of the snapshot. ## Attribute reference @@ -63,6 +64,7 @@ Review the argument references that you can specify for your resource. In addition to all argument reference list, you can access the following attribute reference after your resource is created. - `creation_date` - (String) Creation date of the snapshot. +- `crn` - (String) The CRN of this resource. - `id` - (String) The unique identifier of the snapshot. The ID is composed of /. - `last_update_date` - (String) The last updated date of the snapshot. - `snapshot_id` - (String) ID of the PVM instance snapshot. diff --git a/website/docs/r/pi_snapshot.html.markdown b/website/docs/r/pi_snapshot.html.markdown index 3649eb2455..cbe8ed399d 100644 --- a/website/docs/r/pi_snapshot.html.markdown +++ b/website/docs/r/pi_snapshot.html.markdown @@ -18,11 +18,11 @@ The following example enables you to create a snapshot: ```terraform resource "ibm_pi_snapshot" "testacc_snapshot"{ - pi_cloud_instance_id = "" - pi_description = "Testing snapshot for instance" - pi_instance_name = test-instance - pi_snap_shot_name = test-snapshot - pi_volume_ids = ["volumeid1","volumeid2"] + pi_cloud_instance_id = "" + pi_description = "Testing snapshot for instance" + pi_instance_name = test-instance + pi_snap_shot_name = test-snapshot + pi_volume_ids = ["volumeid1","volumeid2"] } ``` @@ -58,6 +58,7 @@ Review the argument references that you can specify for your resource. - `pi_description` - (Optional, String) Description of the PVM instance snapshot. - `pi_instance_name` - (Required, String) The name of the instance you want to take a snapshot of. - `pi_snap_shot_name` - (Required, String) The unique name of the snapshot. +- `pi_user_tags` - (Optional, List) The user tags attached to this resource. - `pi_volume_ids` - (Optional, String) A list of volume IDs of the instance that will be part of the snapshot. If none are provided, then all the volumes of the instance will be part of the snapshot. ## Attribute reference @@ -65,6 +66,7 @@ Review the argument references that you can specify for your resource. In addition to all argument reference list, you can access the following attribute reference after your resource is created. - `creation_date` - (String) Creation date of the snapshot. +- `crn` - (String) The CRN of this resource. - `id` - (String) The unique identifier of the snapshot. The ID is composed of /. - `last_update_date` - (String) The last updated date of the snapshot. - `snapshot_id` - (String) ID of the PVM instance snapshot.