Skip to content

Commit

Permalink
[Datasource, Resource] Network Interface/s
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkad committed Aug 15, 2024
1 parent a97889a commit 17eacf3
Show file tree
Hide file tree
Showing 18 changed files with 1,037 additions and 16 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ toolchain go1.22.5

require (
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.0-beta5
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.0-beta5 h1:23FxbCYsE4vqBMAQO/8tkksmFu57oXSITVFcyRVgtpg=
github.com/IBM-Cloud/power-go-client v1.8.0-beta5/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
15 changes: 13 additions & 2 deletions ibm/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ var (
Pi_instance_name string
Pi_key_name string
Pi_network_name string
Pi_network_id string
Pi_network_interface_id string
Pi_placement_group_name string
Pi_replication_volume_name string
Pi_resource_group_id string
Expand Down Expand Up @@ -1048,7 +1050,16 @@ func init() {
Pi_network_name = "terraform-test-power"
fmt.Println("[INFO] Set the environment variable PI_NETWORK_NAME for testing ibm_pi_network_name resource else it is set to default value 'terraform-test-power'")
}

Pi_network_id = os.Getenv("PI_NETWORK_ID")
if Pi_network_id == "" {
Pi_network_id = "terraform-test-power"
fmt.Println("[INFO] Set the environment variable PI_NETWORK_ID for testing ibm_pi_network_interface resource else it is set to default value 'terraform-test-power'")
}
Pi_network_interface_id = os.Getenv("PI_NETWORK_INTERFACE_ID")
if Pi_network_interface_id == "" {
Pi_network_interface_id = "terraform-test-power"
fmt.Println("[INFO] Set the environment variable PI_NETWORK_INTERFACE_ID for testing ibm_pi_network_interface resource else it is set to default value 'terraform-test-power'")
}
Pi_volume_name = os.Getenv("PI_VOLUME_NAME")
if Pi_volume_name == "" {
Pi_volume_name = "terraform-test-power"
Expand Down Expand Up @@ -1200,12 +1211,12 @@ func init() {
Pi_host_group_id = ""
fmt.Println("[WARN] Set the environment variable PI_HOST_GROUP_ID for testing ibm_pi_host resource else it is set to default value ''")
}

Pi_host_id = os.Getenv("PI_HOST_ID")
if Pi_host_id == "" {
Pi_host_id = ""
fmt.Println("[WARN] Set the environment variable PI_HOST_ID for testing ibm_pi_host resource else it is set to default value ''")
}

WorkspaceID = os.Getenv("SCHEMATICS_WORKSPACE_ID")
if WorkspaceID == "" {
WorkspaceID = "us-south.workspace.tf-acc-test-schematics-state-test.392cd99f"
Expand Down
3 changes: 3 additions & 0 deletions ibm/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,8 @@ func Provider() *schema.Provider {
"ibm_pi_instances": power.DataSourceIBMPIInstances(),
"ibm_pi_key": power.DataSourceIBMPIKey(),
"ibm_pi_keys": power.DataSourceIBMPIKeys(),
"ibm_pi_network_interface": power.DataSourceIBMPINetworkInterface(),
"ibm_pi_network_interfaces": power.DataSourceIBMPINetworkInterfaces(),
"ibm_pi_network_port": power.DataSourceIBMPINetworkPort(),
"ibm_pi_network": power.DataSourceIBMPINetwork(),
"ibm_pi_networks": power.DataSourceIBMPINetworks(),
Expand Down Expand Up @@ -1279,6 +1281,7 @@ func Provider() *schema.Provider {
"ibm_pi_instance": power.ResourceIBMPIInstance(),
"ibm_pi_ipsec_policy": power.ResourceIBMPIIPSecPolicy(),
"ibm_pi_key": power.ResourceIBMPIKey(),
"ibm_pi_network_interface": power.ResourceIBMPINetworkInterface(),
"ibm_pi_network_port_attach": power.ResourceIBMPINetworkPortAttach(),
"ibm_pi_network": power.ResourceIBMPINetwork(),
"ibm_pi_placement_group": power.ResourceIBMPIPlacementGroup(),
Expand Down
2 changes: 1 addition & 1 deletion ibm/service/power/data_source_ibm_pi_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func DataSourceIBMPIInstance() *schema.Resource {
Description: "The IP address of the instance.",
Type: schema.TypeString,
},
Attr_MacAddress: {
Attr_Macaddress: {
Computed: true,
Description: "The MAC address of the instance.",
Type: schema.TypeString,
Expand Down
4 changes: 2 additions & 2 deletions ibm/service/power/data_source_ibm_pi_instance_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func DataSourceIBMPIInstanceIP() *schema.Resource {
Description: "The IP octet of the network that is attached to this instance.",
Type: schema.TypeString,
},
Attr_MacAddress: {
Attr_Macaddress: {
Computed: true,
Description: "The MAC address of the network that is attached to this instance.",
Type: schema.TypeString,
Expand Down Expand Up @@ -96,7 +96,7 @@ func dataSourceIBMPIInstancesIPRead(ctx context.Context, d *schema.ResourceData,
d.SetId(network.NetworkID)
d.Set(Attr_ExternalIP, network.ExternalIP)
d.Set(Attr_IP, network.IPAddress)
d.Set(Attr_MacAddress, network.MacAddress)
d.Set(Attr_Macaddress, network.MacAddress)
d.Set(Attr_NetworkID, network.NetworkID)
d.Set(Attr_Type, network.Type)

Expand Down
4 changes: 2 additions & 2 deletions ibm/service/power/data_source_ibm_pi_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func DataSourceIBMPIInstances() *schema.Resource {
Description: "The IP address of the instance.",
Type: schema.TypeString,
},
Attr_MacAddress: {
Attr_Macaddress: {
Computed: true,
Description: "The MAC address of the instance.",
Type: schema.TypeString,
Expand Down Expand Up @@ -267,7 +267,7 @@ func flattenPvmInstanceNetworks(list []*models.PVMInstanceNetwork) (networks []m
p := make(map[string]interface{})
p[Attr_ExternalIP] = pvmip.ExternalIP
p[Attr_IP] = pvmip.IPAddress
p[Attr_MacAddress] = pvmip.MacAddress
p[Attr_Macaddress] = pvmip.MacAddress
p[Attr_NetworkID] = pvmip.NetworkID
p[Attr_NetworkName] = pvmip.NetworkName
p[Attr_Type] = pvmip.Type
Expand Down
151 changes: 151 additions & 0 deletions ibm/service/power/data_source_ibm_pi_network_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package power

import (
"context"

"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/power-go-client/power/models"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
)

func DataSourceIBMPINetworkInterface() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPINetworkInterfaceRead,

Schema: map[string]*schema.Schema{
// Arguments
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_NetworkID: {
Description: "Network ID.",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_NetworkInterfaceID: {
Description: "Network Interface ID.",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
// Attributes
Attr_CRN: {
Computed: true,
Description: "The Network Interface's crn.",
Type: schema.TypeString,
},
Attr_IPAddress: {
Computed: true,
Description: "The ip address of this Network Interface.",
Type: schema.TypeString,
},
Attr_MacAddress: {
Computed: true,
Description: "The mac address of the Network Interface.",
Type: schema.TypeString,
},
Attr_Name: {
Computed: true,
Description: "Name of the Network Interface (not unique or indexable).",
Type: schema.TypeString,
},
Attr_NetworkSecurityGroupID: {
Computed: true,
Description: "ID of the Network Security Group the network interface will be added to.",
Type: schema.TypeString,
},
Attr_PVMInstance: {
Computed: true,
Description: "The attached pvm-instance to this Network Interface.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_Href: {
Computed: true,
Description: "Link to pvm-instance resource.",
Type: schema.TypeString,
},
Attr_InstanceID: {
Computed: true,
Description: "The attahed instance ID.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
Attr_Status: {
Computed: true,
Description: "The status of the network address group.",
Type: schema.TypeString,
},
Attr_UserTags: {
Computed: true,
Description: "The user tags associated with this resource.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
Type: schema.TypeList,
},
},
}
}

func dataSourceIBMPINetworkInterfaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}

cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
networkID := d.Get(Arg_NetworkID).(string)
networkInterfaceID := d.Get(Arg_NetworkInterfaceID).(string)
networkC := instance.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
networkInterface, err := networkC.GetNetworkInterface(networkID, networkInterfaceID)
if err != nil {
return diag.FromErr(err)
}
d.SetId(*networkInterface.ID)
d.Set(Attr_CRN, networkInterface.Crn)
d.Set(Attr_IPAddress, networkInterface.IPAddress)
d.Set(Attr_MacAddress, networkInterface.MacAddress)
d.Set(Attr_Name, networkInterface.Name)
d.Set(Attr_NetworkSecurityGroupID, networkInterface.NetworkSecurityGroupID)

if networkInterface.PvmInstance != nil {
pvmInstance := []map[string]interface{}{}
instanceMap := pvmInstanceToMap(networkInterface.PvmInstance)
pvmInstance = append(pvmInstance, instanceMap)
d.Set(Attr_PVMInstance, pvmInstance)
}
d.Set(Attr_Status, networkInterface.Status)
if len(networkInterface.UserTags) > 0 {
d.Set(Attr_UserTags, networkInterface.UserTags)
}

return nil
}

func pvmInstanceToMap(pvm *models.NetworkInterfacePvmInstance) map[string]interface{} {
instanceMap := make(map[string]interface{})
if pvm.Href != "" {
instanceMap[Attr_Href] = pvm.Href
}
if pvm.PvmInstanceID != "" {
instanceMap[Attr_InstanceID] = pvm.PvmInstanceID
}
return instanceMap
}
42 changes: 42 additions & 0 deletions ibm/service/power/data_source_ibm_pi_network_interface_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package power_test

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
)

func TestAccIBMPINetworkInterfaceDataSourceBasic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPINetworkInterfaceDataSourceConfigBasic(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.ibm_pi_network_interface.network_interface", "id"),
resource.TestCheckResourceAttrSet("data.ibm_pi_network_interface.network_interface", "crn"),
resource.TestCheckResourceAttrSet("data.ibm_pi_network_interface.network_interface", "ip_address"),
resource.TestCheckResourceAttrSet("data.ibm_pi_network_interface.network_interface", "mac_address"),
resource.TestCheckResourceAttrSet("data.ibm_pi_network_interface.network_interface", "name"),
resource.TestCheckResourceAttrSet("data.ibm_pi_network_interface.network_interface", "status"),
),
},
},
})
}

func testAccCheckIBMPINetworkInterfaceDataSourceConfigBasic() string {
return fmt.Sprintf(`
data "ibm_pi_network_interface" "network_interface" {
pi_cloud_instance_id = "%s"
pi_network_id = "%s"
pi_network_interface_id = "%s"
}`, acc.Pi_cloud_instance_id, acc.Pi_network_id, acc.Pi_network_interface_id)
}
Loading

0 comments on commit 17eacf3

Please sign in to comment.