Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Granular CRN for network datasources and resources #5649

Merged
merged 20 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 @@ -85,6 +85,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 @@ -371,6 +372,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 @@ -443,6 +445,7 @@ const (
Shared = "shared"
Soft = "soft"
Suffix = "suffix"
UserTagType = "user"
Vlan = "vlan"
vSCSI = "vSCSI"
Warning = "WARNING"
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
Loading