Skip to content

Commit

Permalink
chore: fix errcheck lint violations
Browse files Browse the repository at this point in the history
Signed-off-by: Marques Johansson <[email protected]>
  • Loading branch information
displague committed Sep 25, 2024
1 parent 2404fa2 commit 7fb2dce
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 71 deletions.
3 changes: 2 additions & 1 deletion equinix/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ func TestProvider_setSchemaValueIfNotEmpty(t *testing.T) {
var b *int = nil
d := schema.TestResourceDataRaw(t, s, make(map[string]interface{}))
// when
setSchemaValueIfNotEmpty(key, b, d)
err := setSchemaValueIfNotEmpty(key, b, d)
assert.NoError(t, err, "Setting nil value does not return error")
// then
_, ok := d.GetOk(key)
assert.False(t, ok, "Key was not set")
Expand Down
151 changes: 83 additions & 68 deletions equinix/resource_metal_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (

"github.com/equinix/terraform-provider-equinix/internal/converters"
"github.com/equinix/terraform-provider-equinix/internal/network"
equinix_schema "github.com/equinix/terraform-provider-equinix/internal/schema"

equinix_errors "github.com/equinix/terraform-provider-equinix/internal/errors"

Expand Down Expand Up @@ -586,65 +587,6 @@ func resourceMetalDeviceRead(ctx context.Context, d *schema.ResourceData, meta i
return diag.FromErr(err)
}

d.Set("hostname", device.GetHostname())
d.Set("plan", device.Plan.GetSlug())
d.Set("deployed_facility", device.Facility.GetCode())
d.Set("facilities", []string{device.Facility.GetCode()})
if device.Metro != nil {
d.Set("metro", device.Metro.GetCode())
}
d.Set("operating_system", device.OperatingSystem.GetSlug())
d.Set("state", device.GetState())
d.Set("billing_cycle", device.GetBillingCycle())
d.Set("locked", device.GetLocked())
d.Set("created", device.GetCreatedAt().Format(time.RFC3339))
d.Set("updated", device.GetUpdatedAt().Format(time.RFC3339))
d.Set("ipxe_script_url", device.GetIpxeScriptUrl())
d.Set("always_pxe", device.GetAlwaysPxe())
d.Set("root_password", device.GetRootPassword())
d.Set("project_id", device.Project.GetId())
d.Set("sos_hostname", device.GetSos())
if device.Storage != nil {
rawStorageBytes, err := json.Marshal(device.Storage)
if err != nil {
return diag.Errorf("[ERR] Error getting storage JSON string for device (%s): %s", d.Id(), err)
}

storageString, err := structure.NormalizeJsonString(string(rawStorageBytes))
if err != nil {
return diag.Errorf("[ERR] Error normalizing storage JSON string for device (%s): %s", d.Id(), err)
}
d.Set("storage", storageString)
}
if device.HardwareReservation != nil {
d.Set("deployed_hardware_reservation_id", device.HardwareReservation.GetId())
}

networkType, err := getNetworkType(device)
if err != nil {
return diag.Errorf("[ERR] Error computing network type for device (%s): %s", d.Id(), err)
}
d.Set("network_type", networkType)

wfrd := "wait_for_reservation_deprovision"
if _, ok := d.GetOk(wfrd); !ok {
d.Set(wfrd, nil)
}
fdv := "force_detach_volumes"
if _, ok := d.GetOk(fdv); !ok {
d.Set(fdv, nil)
}
tt := "termination_time"
if _, ok := d.GetOk(tt); !ok {
d.Set(tt, nil)
}

d.Set("tags", device.Tags)
keyIDs := []string{}
for _, k := range device.SshKeys {
keyIDs = append(keyIDs, path.Base(k.Href))
}
d.Set("ssh_key_ids", keyIDs)
networkInfo := getNetworkInfo(device.IpAddresses)

sort.SliceStable(networkInfo.Networks, func(i, j int) bool {
Expand All @@ -655,22 +597,95 @@ func resourceMetalDeviceRead(ctx context.Context, d *schema.ResourceData, meta i
return getNetworkRank(int(famI), pubI) < getNetworkRank(int(famJ), pubJ)
})

d.Set("network", networkInfo.Networks)
d.Set("access_public_ipv4", networkInfo.PublicIPv4)
d.Set("access_private_ipv4", networkInfo.PrivateIPv4)
d.Set("access_public_ipv6", networkInfo.PublicIPv6)

ports := getPorts(device.NetworkPorts)
d.Set("ports", ports)

if networkInfo.Host != "" {
d.SetConnInfo(map[string]string{
"type": "ssh",
"host": networkInfo.Host,
})
}

return nil
return equinix_schema.SetDiagnosticsMap(d, map[string]interface{}{
"hostname": device.GetHostname(),
"plan": device.Plan.GetSlug(),
"deployed_facility": device.Facility.GetCode(),
"facilities": []string{device.Facility.GetCode()},
"metro": func(d *schema.ResourceData, k string) error {
if device.Metro != nil {
return d.Set(k, device.Metro.GetCode())
}
return nil
},
"operating_system": device.OperatingSystem.GetSlug(),
"state": device.GetState(),
"billing_cycle": device.GetBillingCycle(),
"locked": device.GetLocked(),
"created": device.GetCreatedAt().Format(time.RFC3339),
"updated": device.GetUpdatedAt().Format(time.RFC3339),
"ipxe_script_url": device.GetIpxeScriptUrl(),
"always_pxe": device.GetAlwaysPxe(),
"root_password": device.GetRootPassword(),
"project_id": device.Project.GetId(),
"sos_hostname": device.GetSos(),
"storage": func(d *schema.ResourceData, k string) error {
if device.Storage != nil {
rawStorageBytes, err := json.Marshal(device.Storage)
if err != nil {
return fmt.Errorf("[ERR] Error getting storage JSON string for device (%s): %s", d.Id(), err)
}

storageString, err := structure.NormalizeJsonString(string(rawStorageBytes))
if err != nil {
return fmt.Errorf("[ERR] Error normalizing storage JSON string for device (%s): %s", d.Id(), err)
}
return d.Set(k, storageString)
}
return nil
},
"deployed_hardware_reservation_id": func(d *schema.ResourceData, k string) error {
if device.HardwareReservation != nil {
return d.Set(k, device.HardwareReservation.GetId())
}
return nil
},
"network_type": func(d *schema.ResourceData, k string) error {
networkType, err := getNetworkType(device)
if err != nil {
return fmt.Errorf("[ERR] Error computing network type for device (%s): %s", d.Id(), err)
}
return d.Set(k, networkType)
},
"wait_for_reservation_deprovision": func(d *schema.ResourceData, k string) error {
if _, ok := d.GetOk(k); !ok {
return d.Set(k, nil)
}
return nil
},
"force_detach_volumes": func(d *schema.ResourceData, k string) error {
if _, ok := d.GetOk(k); !ok {
return d.Set(k, nil)
}
return nil
},
"termination_time": func(d *schema.ResourceData, k string) error {
if _, ok := d.GetOk(k); !ok {
return d.Set(k, nil)
}
return nil
},
"tags": device.Tags,
"ssh_key_ids": func(d *schema.ResourceData, k string) error {
keyIDs := []string{}
for _, k := range device.SshKeys {
keyIDs = append(keyIDs, path.Base(k.Href))
}
return d.Set(k, keyIDs)
},
"network": networkInfo.Networks,
"access_public_ipv4": networkInfo.PublicIPv4,
"access_private_ipv4": networkInfo.PrivateIPv4,
"access_public_ipv6": networkInfo.PublicIPv6,
"ports": getPorts(device.NetworkPorts),
})
}

func resourceMetalDeviceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
3 changes: 2 additions & 1 deletion internal/resources/networkedge/ssh_user/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ func TestNetworkSSHUser_resourceFromResourceData(t *testing.T) {
}
deviceUUIDs := []string{"52c00d7f-c310-458e-9426-1d7549e1f600", "5f1483f4-c479-424d-98c5-43a266aae25c"}
d := schema.TestResourceDataRaw(t, createNetworkSSHUserResourceSchema(), rawData)
d.Set(networkSSHUserSchemaNames["DeviceUUIDs"], deviceUUIDs)
err := d.Set(networkSSHUserSchemaNames["DeviceUUIDs"], deviceUUIDs)
assert.NoError(t, err, "Setting DeviceUUIDs does not return error")
expected := ne.SSHUser{
Username: ne.String(rawData[networkSSHUserSchemaNames["Username"]].(string)),
Password: ne.String(rawData[networkSSHUserSchemaNames["Password"]].(string)),
Expand Down
22 changes: 21 additions & 1 deletion internal/schema/set_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import (
"sort"

"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type setFn = func(d *schema.ResourceData, key string) error

// setMap sets the map of values to ResourceData, checking and returning the
// SetMap sets the map of values to ResourceData, checking and returning the
// errors. Typically d.Set is not error checked. This helper makes checking
// those errors less tedious. Because this works with a map, the order of the
// errors would not be predictable, to avoid this the errors will be sorted.
Expand All @@ -31,3 +32,22 @@ func SetMap(d *schema.ResourceData, m map[string]interface{}) error {

return errs.ErrorOrNil()
}

// SetDiagnosticsMap sets the map of values to ResourceData, checking and returning
// the diagnostics. Typically d.Set is not error checked. This helper makes
// checking those errors less tedious.
func SetDiagnosticsMap(d *schema.ResourceData, m map[string]interface{}) diag.Diagnostics {
var diags diag.Diagnostics
for key, v := range m {
var diagErr diag.Diagnostics
if f, ok := v.(setFn); ok {
diagErr = diag.FromErr(f(d, key))
} else {
diagErr = diag.FromErr(d.Set(key, v))
}

diags = append(diags, diagErr...)
}

return diags
}

0 comments on commit 7fb2dce

Please sign in to comment.