Skip to content

Commit

Permalink
Add user tag code to prevent merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
ismirlia committed Oct 2, 2024
1 parent 6aaaf12 commit 68a3f90
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 3 additions & 0 deletions ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -444,6 +446,7 @@ const (
Shared = "shared"
Soft = "soft"
Suffix = "suffix"
UserTagType = "user"
Vlan = "vlan"
vSCSI = "vSCSI"
Warning = "WARNING"
Expand Down
46 changes: 46 additions & 0 deletions ibm/service/power/resource_ibm_pi_instance_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.",
Expand Down Expand Up @@ -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)
Expand All @@ -137,6 +155,16 @@ func resourceIBMPIInstanceSnapshotCreate(ctx context.Context, d *schema.Resource
return diag.FromErr(err)
}

if _, ok := d.GetOk(Arg_UserTags); ok {
if snapshotResponse.Crn != "" {
oldList, newList := d.GetChange(Arg_UserTags)
err := flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, string(snapshotResponse.Crn), "", UserTagType)
if err != nil {
log.Printf("Error on update of pi snapshot (%s) pi_user_tags during creation: %s", *snapshotResponse.SnapshotID, err)
}
}
}

return resourceIBMPIInstanceSnapshotRead(ctx, d, meta)
}

Expand All @@ -159,6 +187,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)
Expand Down Expand Up @@ -196,6 +232,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)
}

Expand Down
51 changes: 51 additions & 0 deletions ibm/service/power/resource_ibm_pi_instance_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
46 changes: 46 additions & 0 deletions ibm/service/power/resource_ibm_pi_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.",
Expand Down Expand Up @@ -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)
Expand All @@ -138,6 +156,16 @@ func resourceIBMPISnapshotCreate(ctx context.Context, d *schema.ResourceData, me
return diag.FromErr(err)
}

if _, ok := d.GetOk(Arg_UserTags); ok {
if snapshotResponse.Crn != "" {
oldList, newList := d.GetChange(Arg_UserTags)
err := flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, string(snapshotResponse.Crn), "", UserTagType)
if err != nil {
log.Printf("Error on update of pi snapshot (%s) pi_user_tags during creation: %s", *snapshotResponse.SnapshotID, err)
}
}
}

return resourceIBMPISnapshotRead(ctx, d, meta)
}

Expand All @@ -161,6 +189,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)
Expand Down Expand Up @@ -199,6 +235,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)
}

Expand Down
51 changes: 51 additions & 0 deletions ibm/service/power/resource_ibm_pi_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
2 changes: 2 additions & 0 deletions website/docs/r/pi_instance_snapshot.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ 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

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 <pi_cloud_instance_id>/<snapshot_id>.
- `last_update_date` - (String) The last updated date of the snapshot.
- `snapshot_id` - (String) ID of the PVM instance snapshot.
Expand Down
12 changes: 7 additions & 5 deletions website/docs/r/pi_snapshot.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ The following example enables you to create a snapshot:

```terraform
resource "ibm_pi_snapshot" "testacc_snapshot"{
pi_cloud_instance_id = "<value of the 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 = "<value of the 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"]
}
```

Expand Down Expand Up @@ -58,13 +58,15 @@ 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

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 <pi_cloud_instance_id>/<snapshot_id>.
- `last_update_date` - (String) The last updated date of the snapshot.
- `snapshot_id` - (String) ID of the PVM instance snapshot.
Expand Down

0 comments on commit 68a3f90

Please sign in to comment.