From e6e8459b085397fad83765ae4bb8c1ad3af10dc5 Mon Sep 17 00:00:00 2001 From: Alexander Kita Date: Wed, 2 Oct 2024 10:18:31 -0500 Subject: [PATCH] Add user tags to host resource --- ibm/service/power/data_source_ibm_pi_host.go | 14 ++++++++++++++ ibm/service/power/data_source_ibm_pi_hosts.go | 15 +++++++++++++++ ibm/service/power/ibm_pi_constants.go | 1 + ibm/service/power/resource_ibm_pi_host.go | 17 ++++++++++++++++- website/docs/d/pi_host.html.markdown | 2 ++ website/docs/d/pi_hosts.html.markdown | 1 + 6 files changed, 49 insertions(+), 1 deletion(-) diff --git a/ibm/service/power/data_source_ibm_pi_host.go b/ibm/service/power/data_source_ibm_pi_host.go index 5f1e31d5d6..bc8490d6de 100644 --- a/ibm/service/power/data_source_ibm_pi_host.go +++ b/ibm/service/power/data_source_ibm_pi_host.go @@ -5,6 +5,7 @@ package power import ( "context" + "log" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -12,6 +13,7 @@ import ( "github.com/IBM-Cloud/power-go-client/clients/instance" "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" ) func DataSourceIBMPIHost() *schema.Resource { @@ -114,6 +116,13 @@ func DataSourceIBMPIHost() *schema.Resource { Description: "System type.", Type: schema.TypeString, }, + Attr_UserTags: { + Computed: true, + Description: "List of user tags attached to the resource.", + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + Type: schema.TypeSet, + }, }, } } @@ -138,6 +147,11 @@ func dataSourceIBMPIHostRead(ctx context.Context, d *schema.ResourceData, meta i } if host.Crn != "" { d.Set(Attr_CRN, host.Crn) + tags, err := flex.GetGlobalTagsUsingCRN(meta, string(host.Crn), "", UserTagType) + if err != nil { + log.Printf("Error on get of pi host (%s) user_tags: %s", hostID, err) + } + d.Set(Attr_UserTags, tags) } if host.DisplayName != "" { d.Set(Attr_DisplayName, host.DisplayName) diff --git a/ibm/service/power/data_source_ibm_pi_hosts.go b/ibm/service/power/data_source_ibm_pi_hosts.go index 89ebe4bc5a..bef0b23f4f 100644 --- a/ibm/service/power/data_source_ibm_pi_hosts.go +++ b/ibm/service/power/data_source_ibm_pi_hosts.go @@ -5,6 +5,7 @@ package power import ( "context" + "log" "github.com/hashicorp/go-uuid" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -13,6 +14,7 @@ import ( "github.com/IBM-Cloud/power-go-client/clients/instance" "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" + "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex" ) func DataSourceIBMPIHosts() *schema.Resource { @@ -117,6 +119,13 @@ func DataSourceIBMPIHosts() *schema.Resource { Description: "System type.", Type: schema.TypeString, }, + Attr_UserTags: { + Computed: true, + Description: "List of user tags attached to the resource.", + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + Type: schema.TypeSet, + }, }, }, Type: schema.TypeList, @@ -149,6 +158,12 @@ func dataSourceIBMPIHostsRead(ctx context.Context, d *schema.ResourceData, meta } if host.Crn != "" { hs[Attr_CRN] = host.Crn + d.Set(Attr_CRN, host.Crn) + tags, err := flex.GetTagsUsingCRN(meta, string(host.Crn)) + if err != nil { + log.Printf("Error on get of pi host (%s) user_tags: %s", host.ID, err) + } + hs[Attr_UserTags] = tags } if host.DisplayName != "" { hs[Attr_DisplayName] = host.DisplayName diff --git a/ibm/service/power/ibm_pi_constants.go b/ibm/service/power/ibm_pi_constants.go index 5cf752a6a7..a081df931f 100644 --- a/ibm/service/power/ibm_pi_constants.go +++ b/ibm/service/power/ibm_pi_constants.go @@ -444,6 +444,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_host.go b/ibm/service/power/resource_ibm_pi_host.go index 0ffa2f8e7b..bb9981eb4b 100644 --- a/ibm/service/power/resource_ibm_pi_host.go +++ b/ibm/service/power/resource_ibm_pi_host.go @@ -69,7 +69,6 @@ func ResourceIBMPIHost() *schema.Resource { Attr_UserTags: { Description: "List of user tags attached to the resource.", Elem: &schema.Schema{Type: schema.TypeString}, - ForceNew: true, Optional: true, Set: schema.HashString, Type: schema.TypeSet, @@ -204,6 +203,17 @@ func resourceIBMPIHostCreate(ctx context.Context, d *schema.ResourceData, meta i if err != nil { return diag.FromErr(err) } + + host := hosts[0].(map[string]interface{}) + tags := flex.FlattenSet(host[Attr_UserTags].(*schema.Set)) + if len(tags) > 0 { + oldList, newList := d.GetChange(Attr_Hosts + ".0." + Attr_UserTags) + err := flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, string(hostResponse[0].Crn), "", UserTagType) + if err != nil { + log.Printf("Error on update of pi host (%s) pi_user_tags during creation: %s", hostResponse[0].ID, err) + } + } + return resourceIBMPIHostRead(ctx, d, meta) } @@ -232,6 +242,11 @@ func resourceIBMPIHostRead(ctx context.Context, d *schema.ResourceData, meta int } if host.Crn != "" { d.Set(Attr_CRN, host.Crn) + tags, err := flex.GetGlobalTagsUsingCRN(meta, string(host.Crn), "", UserTagType) + if err != nil { + log.Printf("Error on get of pi host (%s) pi_user_tags: %s", host.ID, err) + } + d.Set(Attr_UserTags, tags) } if host.DisplayName != "" { d.Set(Attr_DisplayName, host.DisplayName) diff --git a/website/docs/d/pi_host.html.markdown b/website/docs/d/pi_host.html.markdown index 29ef404508..b925e022ff 100644 --- a/website/docs/d/pi_host.html.markdown +++ b/website/docs/d/pi_host.html.markdown @@ -76,3 +76,5 @@ After your data source is created, you can read values from the following attrib - `sys_type` - (String) System type. +- `user_tags` - (List) List of user tags attached to the resource. + diff --git a/website/docs/d/pi_hosts.html.markdown b/website/docs/d/pi_hosts.html.markdown index 804edc3d2b..45faf7d7ee 100644 --- a/website/docs/d/pi_hosts.html.markdown +++ b/website/docs/d/pi_hosts.html.markdown @@ -73,3 +73,4 @@ After your data source is created, you can read values from the following attrib - `state` - (String) State of the host `up` or `down`. - `status` - (String) Status of the host `enabled` or `disabled`. - `sys_type` - (String) System type. + - `user_tags` - (List) List of user tags attached to the resource.