Skip to content

Commit

Permalink
Add user tags to host resource
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander-Kita committed Oct 2, 2024
1 parent 4dc5143 commit e6e8459
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
14 changes: 14 additions & 0 deletions ibm/service/power/data_source_ibm_pi_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ package power

import (
"context"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

"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 {
Expand Down Expand Up @@ -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,
},
},
}
}
Expand All @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions ibm/service/power/data_source_ibm_pi_hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package power

import (
"context"
"log"

"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,7 @@ const (
Shared = "shared"
Soft = "soft"
Suffix = "suffix"
UserTagType = "user"
Vlan = "vlan"
vSCSI = "vSCSI"
Warning = "WARNING"
Expand Down
17 changes: 16 additions & 1 deletion ibm/service/power/resource_ibm_pi_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/pi_host.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

1 change: 1 addition & 0 deletions website/docs/d/pi_hosts.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

0 comments on commit e6e8459

Please sign in to comment.