Skip to content

Commit

Permalink
Merge pull request #5649 from IBM-Cloud/crn-network
Browse files Browse the repository at this point in the history
Granular CRN for network datasources and resources
  • Loading branch information
yussufsh authored Oct 4, 2024
2 parents c2ea51f + ab5e103 commit b4249cb
Show file tree
Hide file tree
Showing 14 changed files with 183 additions and 26 deletions.
22 changes: 22 additions & 0 deletions ibm/service/power/data_source_ibm_pi_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package power

import (
"context"
"log"

"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/helpers"
"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/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -48,6 +50,11 @@ func DataSourceIBMPINetwork() *schema.Resource {
Description: "The CIDR of the network.",
Type: schema.TypeString,
},
Attr_CRN: {
Computed: true,
Description: "The CRN of this resource.",
Type: schema.TypeString,
},
Attr_DNS: {
Computed: true,
Description: "The DNS Servers for the network.",
Expand Down Expand Up @@ -91,6 +98,13 @@ func DataSourceIBMPINetwork() *schema.Resource {
Description: "The percentage of IP addresses used.",
Type: schema.TypeFloat,
},
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,
},
Attr_VLanID: {
Computed: true,
Description: "The VLAN ID that the network is connected to.",
Expand Down Expand Up @@ -122,6 +136,14 @@ func dataSourceIBMPINetworkRead(ctx context.Context, d *schema.ResourceData, met
if networkdata.Cidr != nil {
d.Set(Attr_CIDR, networkdata.Cidr)
}
if networkdata.Crn != "" {
d.Set(Attr_CRN, networkdata.Crn)
tags, err := flex.GetGlobalTagsUsingCRN(meta, string(networkdata.Crn), "", UserTagType)
if err != nil {
log.Printf("Error on get of pi network (%s) user_tags: %s", *networkdata.NetworkID, err)
}
d.Set(Attr_UserTags, tags)
}
if len(networkdata.DNSServers) > 0 {
d.Set(Attr_DNS, networkdata.DNSServers)
}
Expand Down
3 changes: 2 additions & 1 deletion ibm/service/power/data_source_ibm_pi_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
)

func TestAccIBMPINetworkDataSource_basic(t *testing.T) {
networkRes := "data.ibm_pi_network.testacc_ds_network"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPINetworkDataSourceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.ibm_pi_network.testacc_ds_network", "id"),
resource.TestCheckResourceAttrSet(networkRes, "id"),
),
},
},
Expand Down
27 changes: 25 additions & 2 deletions ibm/service/power/data_source_ibm_pi_networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package power

import (
"context"
"log"

"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/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -38,6 +40,11 @@ func DataSourceIBMPINetworks() *schema.Resource {
Description: "The network communication configuration option of the network (for satellite locations only).",
Type: schema.TypeString,
},
Attr_CRN: {
Computed: true,
Description: "The CRN of this resource.",
Type: schema.TypeString,
},
Attr_DhcpManaged: {
Computed: true,
Description: "Indicates if the network DHCP Managed.",
Expand Down Expand Up @@ -68,6 +75,13 @@ func DataSourceIBMPINetworks() *schema.Resource {
Description: "The type of network.",
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,
},
Attr_VLanID: {
Computed: true,
Description: "The VLAN ID that the network is connected to.",
Expand Down Expand Up @@ -97,12 +111,12 @@ func dataSourceIBMPINetworksRead(ctx context.Context, d *schema.ResourceData, me

var clientgenU, _ = uuid.GenerateUUID()
d.SetId(clientgenU)
d.Set(Attr_Networks, flattenNetworks(networkdata.Networks))
d.Set(Attr_Networks, flattenNetworks(networkdata.Networks, meta))

return nil
}

func flattenNetworks(list []*models.NetworkReference) []map[string]interface{} {
func flattenNetworks(list []*models.NetworkReference, meta interface{}) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(list))
for _, i := range list {
l := map[string]interface{}{
Expand All @@ -115,6 +129,15 @@ func flattenNetworks(list []*models.NetworkReference) []map[string]interface{} {
Attr_Type: *i.Type,
Attr_VLanID: *i.VlanID,
}

if i.Crn != "" {
l[Attr_CRN] = i.Crn
tags, err := flex.GetGlobalTagsUsingCRN(meta, string(i.Crn), "", UserTagType)
if err != nil {
log.Printf("Error on get of pi network (%s) user_tags: %s", *i.NetworkID, err)
}
l[Attr_UserTags] = tags
}
result = append(result, l)
}
return result
Expand Down
3 changes: 2 additions & 1 deletion ibm/service/power/data_source_ibm_pi_networks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
)

func TestAccIBMPINetworksDataSource_basic(t *testing.T) {
networksResData := "data.ibm_pi_networks.testacc_ds_networks"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPINetworksDataSourceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.ibm_pi_networks.testacc_ds_networks", "id"),
resource.TestCheckResourceAttrSet(networksResData, "id"),
),
},
},
Expand Down
8 changes: 8 additions & 0 deletions ibm/service/power/data_source_ibm_pi_public_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ func DataSourceIBMPIPublicNetwork() *schema.Resource {
},

// Attributes
Attr_CRN: {
Computed: true,
Description: "The CRN of this resource.",
Type: schema.TypeString,
},
Attr_Name: {
Computed: true,
Description: "The name of the network.",
Expand Down Expand Up @@ -63,6 +68,9 @@ func dataSourceIBMPIPublicNetworkRead(ctx context.Context, d *schema.ResourceDat
}

d.SetId(*networkdata.Networks[0].NetworkID)
if networkdata.Networks[0].Crn != "" {
d.Set(Attr_CRN, networkdata.Networks[0].Crn)
}
if networkdata.Networks[0].Name != nil {
d.Set(Attr_Name, networkdata.Networks[0].Name)
}
Expand Down
3 changes: 2 additions & 1 deletion ibm/service/power/data_source_ibm_pi_public_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import (
)

func TestAccIBMPIPublicNetworkDataSource_basic(t *testing.T) {
publicNetworkResData := "data.ibm_pi_public_network.testacc_ds_public_network"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPIPublicNetworkDataSourceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.ibm_pi_public_network.testacc_ds_public_network", "id"),
resource.TestCheckResourceAttrSet(publicNetworkResData, "id"),
),
},
},
Expand Down
45 changes: 43 additions & 2 deletions ibm/service/power/resource_ibm_pi_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,20 @@ func ResourceIBMPINetwork() *schema.Resource {
},
},
},
Arg_UserTags: {
Description: "The user tags attached to this resource.",
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Set: schema.HashString,
Type: schema.TypeSet,
},

//Computed Attributes
Attr_CRN: {
Computed: true,
Description: "The CRN of this resource.",
Type: schema.TypeString,
},
"network_id": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -158,7 +170,9 @@ func resourceIBMPINetworkCreate(ctx context.Context, d *schema.ResourceData, met
body.DNSServers = networkdns
}
}

if tags, ok := d.GetOk(Arg_UserTags); ok {
body.UserTags = flex.FlattenSet(tags.(*schema.Set))
}
if v, ok := d.GetOk(helpers.PINetworkJumbo); ok {
body.Jumbo = v.(bool)
}
Expand Down Expand Up @@ -223,6 +237,16 @@ func resourceIBMPINetworkCreate(ctx context.Context, d *schema.ResourceData, met
return diag.FromErr(err)
}

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

return resourceIBMPINetworkRead(ctx, d, meta)
}

Expand All @@ -242,7 +266,14 @@ func resourceIBMPINetworkRead(ctx context.Context, d *schema.ResourceData, meta
if err != nil {
return diag.FromErr(err)
}

if networkdata.Crn != "" {
d.Set(Attr_CRN, networkdata.Crn)
tags, err := flex.GetGlobalTagsUsingCRN(meta, string(networkdata.Crn), "", UserTagType)
if err != nil {
log.Printf("Error on get of pi network (%s) pi_user_tags: %s", *networkdata.NetworkID, err)
}
d.Set(Arg_UserTags, tags)
}
d.Set("network_id", networkdata.NetworkID)
d.Set(helpers.PINetworkCidr, networkdata.Cidr)
d.Set(helpers.PINetworkDNS, networkdata.DNSServers)
Expand Down Expand Up @@ -310,6 +341,16 @@ func resourceIBMPINetworkUpdate(ctx context.Context, d *schema.ResourceData, met
}
}

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 network (%s) pi_user_tags: %s", networkID, err)
}
}
}

return resourceIBMPINetworkRead(ctx, d, meta)
}

Expand Down
12 changes: 11 additions & 1 deletion ibm/service/power/resource_ibm_pi_network_port_attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ func ResourceIBMPINetworkPortAttach() *schema.Resource {
ForceNew: true,
Computed: true,
},
Arg_UserTags: {
Description: "The user tags attached to this resource.",
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true,
Optional: true,
Set: schema.HashString,
Type: schema.TypeSet,
},

//Computed Attributes
"macaddress": {
Expand Down Expand Up @@ -101,7 +109,9 @@ func resourceIBMPINetworkPortAttachCreate(ctx context.Context, d *schema.Resourc
ipaddress := v.(string)
nwportBody.IPAddress = ipaddress
}

if tags, ok := d.GetOk(Arg_UserTags); ok {
nwportBody.UserTags = flex.FlattenSet(tags.(*schema.Set))
}
nwportattachBody := &models.NetworkPortUpdate{
Description: &description,
PvmInstanceID: &instanceID,
Expand Down
Loading

0 comments on commit b4249cb

Please sign in to comment.