Skip to content

Commit

Permalink
Add userTags
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkad committed Oct 10, 2024
1 parent 310023c commit 54ca060
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 13 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-20240926024252-81b3928fd062
github.com/IBM-Cloud/container-services-go-sdk v0.0.0-20240725064144-454a2ae23113
github.com/IBM-Cloud/power-go-client v1.8.0-beta17
github.com/IBM-Cloud/power-go-client v1.8.0-beta19
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-20240926024252-81b3928fd062/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.8.0-beta17 h1:zL/qQUs/bb8L0FEazcBcpr7wTCpv0CJp/dbpg6351Qc=
github.com/IBM-Cloud/power-go-client v1.8.0-beta17/go.mod h1:oAkZiHX25cmr2Yun5V0q6CpnUemegvSrpcEy/oQcjzU=
github.com/IBM-Cloud/power-go-client v1.8.0-beta19 h1:Iyt0umKBc79NLMibaZ8xdpl4TB/zihzjehU1jhy58J0=
github.com/IBM-Cloud/power-go-client v1.8.0-beta19/go.mod h1:oAkZiHX25cmr2Yun5V0q6CpnUemegvSrpcEy/oQcjzU=
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
18 changes: 17 additions & 1 deletion ibm/service/power/data_source_ibm_pi_network_security_group.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/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

Expand Down Expand Up @@ -182,6 +184,13 @@ func DataSourceIBMPINetworkSecurityGroup() *schema.Resource {
},
Type: schema.TypeList,
},
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 @@ -201,7 +210,14 @@ func dataSourceIBMPINetworkSecurityGroupRead(ctx context.Context, d *schema.Reso
}

d.SetId(*networkSecurityGroup.ID)
d.Set(Attr_CRN, networkSecurityGroup.Crn)
if networkSecurityGroup.Crn != nil {
d.Set(Attr_CRN, networkSecurityGroup.Crn)
userTags, err := flex.GetTagsUsingCRN(meta, string(*networkSecurityGroup.Crn))
if err != nil {
log.Printf("Error on get of pi network security group (%s) user_tags: %s", *networkSecurityGroup.ID, err)
}
d.Set(Attr_UserTags, userTags)
}

if len(networkSecurityGroup.Members) > 0 {
members := []map[string]interface{}{}
Expand Down
23 changes: 20 additions & 3 deletions ibm/service/power/data_source_ibm_pi_network_security_groups.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/power-go-client/power/models"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

Expand Down Expand Up @@ -182,6 +184,13 @@ func DataSourceIBMPINetworkSecurityGroups() *schema.Resource {
},
Type: schema.TypeList,
},
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 @@ -209,7 +218,7 @@ func dataSourceIBMPINetworkSecurityGroupsRead(ctx context.Context, d *schema.Res
networkSecurityGroups := []map[string]interface{}{}
if len(nsgResp.NetworkSecurityGroups) > 0 {
for _, nsg := range nsgResp.NetworkSecurityGroups {
networkSecurityGroup := networkSecurityGroupToMap(nsg)
networkSecurityGroup := networkSecurityGroupToMap(nsg, meta)
networkSecurityGroups = append(networkSecurityGroups, networkSecurityGroup)
}
}
Expand All @@ -219,9 +228,17 @@ func dataSourceIBMPINetworkSecurityGroupsRead(ctx context.Context, d *schema.Res
return nil
}

func networkSecurityGroupToMap(nsg *models.NetworkSecurityGroup) map[string]interface{} {
func networkSecurityGroupToMap(nsg *models.NetworkSecurityGroup, meta interface{}) map[string]interface{} {
networkSecurityGroup := make(map[string]interface{})
networkSecurityGroup[Attr_CRN] = nsg.Crn
if nsg.Crn != nil {
networkSecurityGroup[Attr_CRN] = nsg.Crn
userTags, err := flex.GetTagsUsingCRN(meta, string(*nsg.Crn))
if err != nil {
log.Printf("Error on get of pi network security group (%s) user_tags: %s", *nsg.ID, err)
}
networkSecurityGroup[Attr_UserTags] = userTags
}

networkSecurityGroup[Attr_ID] = nsg.ID
if len(nsg.Members) > 0 {
members := []map[string]interface{}{}
Expand Down
28 changes: 22 additions & 6 deletions ibm/service/power/resource_ibm_pi_network_security_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package power
import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -52,7 +53,6 @@ func ResourceIBMPINetworkSecurityGroup() *schema.Resource {
Arg_UserTags: {
Description: "The user tags associated with this resource.",
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true,
Optional: true,
Set: schema.HashString,
Type: schema.TypeSet,
Expand Down Expand Up @@ -226,16 +226,24 @@ func resourceIBMPINetworkSecurityGroupCreate(ctx context.Context, d *schema.Reso
Name: &name,
}
if v, ok := d.GetOk(Arg_UserTags); ok {
if len(flex.FlattenSet(v.(*schema.Set))) > 0 {
userTags := flex.FlattenSet(v.(*schema.Set))
body.UserTags = userTags
}
userTags := flex.FlattenSet(v.(*schema.Set))
body.UserTags = userTags
}

networkSecurityGroup, err := nsgClient.Create(body)
if err != nil {
return diag.FromErr(err)
}
if _, ok := d.GetOk(Arg_UserTags); ok {
crn := *networkSecurityGroup.Crn
if crn != "" {
oldList, newList := d.GetChange(Arg_UserTags)
err := flex.UpdateGlobalTagsUsingCRN(oldList, newList, meta, string(*networkSecurityGroup.Crn), "", UserTagType)
if err != nil {
log.Printf("Error on update of pi network security group (%s) pi_user_tags during creation: %s", *networkSecurityGroup.ID, err)
}
}
}
nsgID := *networkSecurityGroup.ID
d.SetId(fmt.Sprintf("%s/%s", cloudInstanceID, nsgID))

Expand All @@ -257,7 +265,15 @@ func resourceIBMPINetworkSecurityGroupRead(ctx context.Context, d *schema.Resour
return diag.FromErr(err)
}
d.Set(Arg_Name, networkSecurityGroup.Name)
d.Set(Attr_CRN, networkSecurityGroup.Crn)
crn := networkSecurityGroup.Crn
if crn != nil {
d.Set(Attr_CRN, networkSecurityGroup.Crn)
userTags, err := flex.GetGlobalTagsUsingCRN(meta, string(*networkSecurityGroup.Crn), "", UserTagType)
if err != nil {
log.Printf("Error on get of network security group (%s) pi_user_tags: %s", nsgID, err)
}
d.Set(Arg_UserTags, userTags)
}

if len(networkSecurityGroup.Members) > 0 {
members := []map[string]interface{}{}
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/pi_network_security_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,4 @@ In addition to all argument reference list, you can access the following attribu
Nested schema for `source_port`:
- `maximum` - (Integer) The end of the port range, if applicable. If the value is not present then the default value of 65535 will be the maximum port number.
- `minimum` - (Integer) The start of the port range, if applicable. If the value is not present then the default value of 1 will be the minimum port number.
- `user_tags` - (List) List of user tags attached to the resource.
1 change: 1 addition & 0 deletions website/docs/d/pi_network_security_groups.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,4 @@ After your data source is created, you can read values from the following attrib
Nested schema for `source_port`:
- `maximum` - (Integer) The end of the port range, if applicable. If the value is not present then the default value of 65535 will be the maximum port number.
- `minimum` - (Integer) The start of the port range, if applicable. If the value is not present then the default value of 1 will be the minimum port number.
- `user_tags` - (List) List of user tags attached to the resource.

0 comments on commit 54ca060

Please sign in to comment.