Skip to content

Commit 7a7ed06

Browse files
committed
refactor: swap SDK for network_device_type data source
1 parent 1432afe commit 7a7ed06

File tree

3 files changed

+41
-25
lines changed

3 files changed

+41
-25
lines changed

equinix/network_edge_provider_maps.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ package equinix
22

33
import (
44
"github.com/equinix/terraform-provider-equinix/internal/resources/network_edge/account"
5+
"github.com/equinix/terraform-provider-equinix/internal/resources/network_edge/devicetype"
56
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
67
)
78

89
func networkEdgeDatasources() map[string]*schema.Resource {
910
return map[string]*schema.Resource{
1011
"equinix_network_account": account.DataSource(),
1112
"equinix_network_device": dataSourceNetworkDevice(),
12-
"equinix_network_device_type": dataSourceNetworkDeviceType(),
13+
"equinix_network_device_type": devicetype.DataSource(),
1314
"equinix_network_device_software": dataSourceNetworkDeviceSoftware(),
1415
"equinix_network_device_platform": dataSourceNetworkDevicePlatform(),
1516
}

equinix/data_source_network_device_type.go renamed to internal/resources/network_edge/devicetype/data_source.go

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
package equinix
1+
// Package devicetype provides the network_device_type data source
2+
package devicetype
23

34
import (
45
"context"
56
"fmt"
67
"strings"
78

9+
"github.com/equinix/terraform-provider-equinix/internal/comparisons"
810
"github.com/equinix/terraform-provider-equinix/internal/config"
911
"github.com/equinix/terraform-provider-equinix/internal/converters"
1012
equinix_validation "github.com/equinix/terraform-provider-equinix/internal/validation"
1113

12-
"github.com/equinix/ne-go"
14+
"github.com/equinix/equinix-sdk-go/services/networkedgev1"
1315
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1416
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1517
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -33,7 +35,8 @@ var networkDeviceTypeDescriptions = map[string]string{
3335
"MetroCodes": "List of metro codes where device type has to be available",
3436
}
3537

36-
func dataSourceNetworkDeviceType() *schema.Resource {
38+
// DataSource creates a new Terraform data source for retrieving device type data
39+
func DataSource() *schema.Resource {
3740
return &schema.Resource{
3841
ReadContext: dataSourceNetworkDeviceTypeRead,
3942
Description: "Use this data source to get Equinix Network Edge device type details",
@@ -85,28 +88,33 @@ func dataSourceNetworkDeviceType() *schema.Resource {
8588
}
8689

8790
func dataSourceNetworkDeviceTypeRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
88-
conf := m.(*config.Config)
8991
var diags diag.Diagnostics
90-
types, err := conf.Ne.GetDeviceTypes()
92+
client := m.(*config.Config).NewNetworkEdgeClientForSDK(ctx, d)
93+
types, _, err := client.SetupApi.GetVirtualDevicesUsingGET(ctx).Execute()
9194
name := d.Get(networkDeviceTypeSchemaNames["Name"]).(string)
9295
vendor := d.Get(networkDeviceTypeSchemaNames["Vendor"]).(string)
9396
category := d.Get(networkDeviceTypeSchemaNames["Category"]).(string)
9497
metroCodes := converters.SetToStringList(d.Get(networkDeviceTypeSchemaNames["MetroCodes"]).(*schema.Set))
9598
if err != nil {
9699
return diag.FromErr(err)
97100
}
98-
filtered := make([]ne.DeviceType, 0, len(types))
99-
for _, deviceType := range types {
100-
if name != "" && ne.StringValue(deviceType.Name) != name {
101+
filtered := make([]networkedgev1.VirtualDeviceType, 0, len(types.Data))
102+
for _, deviceType := range types.Data {
103+
if name != "" && deviceType.GetName() != name {
101104
continue
102105
}
103-
if vendor != "" && ne.StringValue(deviceType.Vendor) != vendor {
106+
if vendor != "" && deviceType.GetVendor() != vendor {
104107
continue
105108
}
106-
if category != "" && !strings.EqualFold(ne.StringValue(deviceType.Category), category) {
109+
if category != "" && !strings.EqualFold(deviceType.GetCategory(), category) {
107110
continue
108111
}
109-
if !stringsFound(metroCodes, deviceType.MetroCodes) {
112+
113+
availableMetros := make([]string, len(deviceType.AvailableMetros))
114+
for _, metro := range deviceType.AvailableMetros {
115+
availableMetros = append(availableMetros, metro.GetMetroCode())
116+
}
117+
if !comparisons.Subsets(metroCodes, availableMetros) {
110118
continue
111119
}
112120
filtered = append(filtered, deviceType)
@@ -123,24 +131,29 @@ func dataSourceNetworkDeviceTypeRead(ctx context.Context, d *schema.ResourceData
123131
return diags
124132
}
125133

126-
func updateNetworkDeviceTypeResource(deviceType ne.DeviceType, d *schema.ResourceData) error {
127-
d.SetId(ne.StringValue(deviceType.Code))
128-
if err := d.Set(networkDeviceTypeSchemaNames["Name"], deviceType.Name); err != nil {
134+
func updateNetworkDeviceTypeResource(deviceType networkedgev1.VirtualDeviceType, d *schema.ResourceData) error {
135+
d.SetId(deviceType.GetDeviceTypeCode())
136+
if err := d.Set(networkDeviceTypeSchemaNames["Name"], deviceType.GetName()); err != nil {
129137
return fmt.Errorf("error reading Name: %s", err)
130138
}
131-
if err := d.Set(networkDeviceTypeSchemaNames["Code"], deviceType.Code); err != nil {
139+
if err := d.Set(networkDeviceTypeSchemaNames["Code"], deviceType.GetDeviceTypeCode()); err != nil {
132140
return fmt.Errorf("error reading Code: %s", err)
133141
}
134-
if err := d.Set(networkDeviceTypeSchemaNames["Description"], deviceType.Description); err != nil {
142+
if err := d.Set(networkDeviceTypeSchemaNames["Description"], deviceType.GetDescription()); err != nil {
135143
return fmt.Errorf("error reading Description: %s", err)
136144
}
137-
if err := d.Set(networkDeviceTypeSchemaNames["Vendor"], deviceType.Vendor); err != nil {
145+
if err := d.Set(networkDeviceTypeSchemaNames["Vendor"], deviceType.GetVendor()); err != nil {
138146
return fmt.Errorf("error reading Vendor: %s", err)
139147
}
140-
if err := d.Set(networkDeviceTypeSchemaNames["Category"], deviceType.Category); err != nil {
148+
if err := d.Set(networkDeviceTypeSchemaNames["Category"], deviceType.GetCategory()); err != nil {
141149
return fmt.Errorf("error reading Category: %s", err)
142150
}
143-
if err := d.Set(networkDeviceTypeSchemaNames["MetroCodes"], deviceType.MetroCodes); err != nil {
151+
152+
availableMetros := make([]string, len(deviceType.AvailableMetros))
153+
for _, metro := range deviceType.AvailableMetros {
154+
availableMetros = append(availableMetros, metro.GetMetroCode())
155+
}
156+
if err := d.Set(networkDeviceTypeSchemaNames["MetroCodes"], availableMetros); err != nil {
144157
return fmt.Errorf("error reading MetroCodes: %s", err)
145158
}
146159
return nil

equinix/data_source_network_device_type_acc_test.go renamed to internal/resources/network_edge/devicetype/data_source_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
package equinix
1+
package devicetype_test
22

33
import (
44
"fmt"
55
"testing"
66

7+
"github.com/equinix/terraform-provider-equinix/internal/acceptance"
8+
"github.com/equinix/terraform-provider-equinix/internal/nprintf"
79
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
810
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
911
)
1012

1113
func TestAccDataSourceNetworkDeviceType_basic(t *testing.T) {
12-
metro, _ := schema.EnvDefaultFunc(networkDeviceMetroEnvVar, "SV")()
14+
metro, _ := schema.EnvDefaultFunc(acceptance.NetworkDeviceMetroEnvVar, "SV")()
1315
context := map[string]interface{}{
1416
"resourceName": "router",
1517
"category": "Router",
@@ -18,8 +20,8 @@ func TestAccDataSourceNetworkDeviceType_basic(t *testing.T) {
1820
}
1921
resourceName := fmt.Sprintf("data.equinix_network_device_type.%s", context["resourceName"].(string))
2022
resource.ParallelTest(t, resource.TestCase{
21-
PreCheck: func() { testAccPreCheck(t) },
22-
Providers: testAccProviders,
23+
PreCheck: func() { acceptance.TestAccPreCheck(t) },
24+
Providers: acceptance.TestAccProviders,
2325
Steps: []resource.TestStep{
2426
{
2527
Config: testAccDataSourceNetworkDeviceTypeConfig_basic(context),
@@ -34,7 +36,7 @@ func TestAccDataSourceNetworkDeviceType_basic(t *testing.T) {
3436
}
3537

3638
func testAccDataSourceNetworkDeviceTypeConfig_basic(ctx map[string]interface{}) string {
37-
return nprintf(`
39+
return nprintf.NPrintf(`
3840
data "equinix_network_device_type" "%{resourceName}" {
3941
category = "%{category}"
4042
vendor = "%{vendor}"

0 commit comments

Comments
 (0)