Skip to content

Commit

Permalink
Merge pull request #5645 from IBM-Cloud/network-interface-d
Browse files Browse the repository at this point in the history
[Datasource, Resource] Network Interface/s
  • Loading branch information
yussufsh authored Oct 11, 2024
2 parents 91ccc79 + 3c989b2 commit 040936f
Show file tree
Hide file tree
Showing 13 changed files with 1,151 additions and 2 deletions.
15 changes: 13 additions & 2 deletions ibm/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,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 @@ -1068,7 +1070,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 @@ -1220,12 +1231,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 @@ -655,6 +655,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 @@ -1304,6 +1306,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
1 change: 1 addition & 0 deletions ibm/service/power/data_source_ibm_pi_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,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
Expand Down
164 changes: 164 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,164 @@
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package power

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

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_Instance: {
Computed: true,
Description: "The attached instance to this Network Interface.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_Href: {
Computed: true,
Description: "Link to instance resource.",
Type: schema.TypeString,
},
Attr_InstanceID: {
Computed: true,
Description: "The attached instance ID.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
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_NetworkInterfaceID: {
Computed: true,
Description: "ID of the network interface.",
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_Status: {
Computed: true,
Description: "The status of the network address group.",
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,
},
},
}
}

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(fmt.Sprintf("%s/%s", networkID, *networkInterface.ID))
d.Set(Attr_IPAddress, networkInterface.IPAddress)
d.Set(Attr_MacAddress, networkInterface.MacAddress)
d.Set(Attr_Name, networkInterface.Name)
d.Set(Attr_NetworkInterfaceID, *networkInterface.ID)
d.Set(Attr_NetworkSecurityGroupID, networkInterface.NetworkSecurityGroupID)

if networkInterface.Instance != nil {
instance := []map[string]interface{}{}
instanceMap := pvmInstanceToMap(networkInterface.Instance)
instance = append(instance, instanceMap)
d.Set(Attr_Instance, instance)
}
d.Set(Attr_Status, networkInterface.Status)
if networkInterface.Crn != nil {
d.Set(Attr_CRN, networkInterface.Crn)
userTags, err := flex.GetTagsUsingCRN(meta, string(*networkInterface.Crn))
if err != nil {
log.Printf("Error on get of network interface (%s) user_tags: %s", *networkInterface.ID, err)
}
d.Set(Attr_UserTags, userTags)
}

return nil
}

func pvmInstanceToMap(pvm *models.NetworkInterfaceInstance) map[string]interface{} {
instanceMap := make(map[string]interface{})
if pvm.Href != "" {
instanceMap[Attr_Href] = pvm.Href
}
if pvm.InstanceID != "" {
instanceMap[Attr_InstanceID] = pvm.InstanceID
}
return instanceMap
}
43 changes: 43 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,43 @@
// 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"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/service/power"
)

func TestAccIBMPINetworkInterfaceDataSourceBasic(t *testing.T) {
netIntData := "data.ibm_pi_network_interface.network_interface"
resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMPINetworkInterfaceDataSourceConfigBasic(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(netIntData, power.Attr_ID),
resource.TestCheckResourceAttrSet(netIntData, power.Attr_IPAddress),
resource.TestCheckResourceAttrSet(netIntData, power.Attr_MacAddress),
resource.TestCheckResourceAttrSet(netIntData, power.Attr_Name),
resource.TestCheckResourceAttrSet(netIntData, power.Attr_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 040936f

Please sign in to comment.