forked from IBM-Cloud/terraform-provider-ibm
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Datasource, Resource] Network Interface/s
- Loading branch information
1 parent
a97889a
commit 17eacf3
Showing
18 changed files
with
1,037 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
ibm/service/power/data_source_ibm_pi_network_interface.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
42
ibm/service/power/data_source_ibm_pi_network_interface_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.