Skip to content

Commit

Permalink
refactor: move error handling code to internal/errors, move SetMap to…
Browse files Browse the repository at this point in the history
… internal/schema (#467)

This PR is moving error code to internal/errors, and SetMap function to
internal/schema.

I'm open to change the directory/package names.
  • Loading branch information
t0mk authored Dec 4, 2023
2 parents e1583cb + 90d0053 commit cb37f06
Show file tree
Hide file tree
Showing 35 changed files with 281 additions and 203 deletions.
4 changes: 3 additions & 1 deletion equinix/data_source_metal_facility.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"

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

"github.com/equinix/terraform-provider-equinix/internal/config"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -122,7 +124,7 @@ func dataSourceMetalFacilityRead(d *schema.ResourceData, meta interface{}) error
}
}
d.SetId(f.ID)
return setMap(d, map[string]interface{}{
return equinix_schema.SetMap(d, map[string]interface{}{
"code": f.Code,
"name": f.Name,
"features": f.Features,
Expand Down
4 changes: 3 additions & 1 deletion equinix/data_source_metal_hardware_reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package equinix
import (
"fmt"

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

"github.com/equinix/terraform-provider-equinix/internal/config"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -118,5 +120,5 @@ func dataSourceMetalHardwareReservationRead(d *schema.ResourceData, meta interfa
}

d.SetId(hr.ID)
return setMap(d, m)
return equinix_schema.SetMap(d, m)
}
4 changes: 3 additions & 1 deletion equinix/data_source_metal_metro.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package equinix
import (
"fmt"

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

"github.com/equinix/terraform-provider-equinix/internal/config"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -70,7 +72,7 @@ func dataSourceMetalMetroRead(d *schema.ResourceData, meta interface{}) error {
for _, m := range metros {
if m.Code == code {
d.SetId(m.ID)
return setMap(d, map[string]interface{}{
return equinix_schema.SetMap(d, map[string]interface{}{
"id": m.ID,
"code": m.Code,
"name": m.Name,
Expand Down
4 changes: 3 additions & 1 deletion equinix/data_source_metal_organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"path"

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

"github.com/equinix/terraform-provider-equinix/internal/config"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -147,7 +149,7 @@ func dataSourceMetalOrganizationRead(d *schema.ResourceData, meta interface{}) e
}

d.SetId(org.ID)
return setMap(d, map[string]interface{}{
return equinix_schema.SetMap(d, map[string]interface{}{
"organization_id": org.ID,
"name": org.Name,
"description": org.Description,
Expand Down
3 changes: 2 additions & 1 deletion equinix/data_source_metal_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path"

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

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -157,7 +158,7 @@ func dataSourceMetalProjectRead(d *schema.ResourceData, meta interface{}) error
if bgpConf.ID != "" {
err := d.Set("bgp_config", flattenBGPConfig(bgpConf))
if err != nil {
err = friendlyError(err)
err = equinix_errors.FriendlyError(err)
return err
}
}
Expand Down
3 changes: 2 additions & 1 deletion equinix/data_source_metal_project_ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

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

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -86,7 +87,7 @@ func dataSourceMetalProjectSSHKeyRead(d *schema.ResourceData, meta interface{})
}
keys, _, err := client.Projects.ListSSHKeys(projectID, searchOpts)
if err != nil {
err = fmt.Errorf("Error listing project ssh keys: %s", friendlyError(err))
err = fmt.Errorf("Error listing project ssh keys: %s", equinix_errors.FriendlyError(err))
return err
}

Expand Down
9 changes: 6 additions & 3 deletions equinix/data_source_metal_spot_market_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"strings"
"time"

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

"github.com/equinix/terraform-provider-equinix/internal/config"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -81,8 +84,8 @@ func dataSourceMetalSpotMarketRequestRead(ctx context.Context, d *schema.Resourc

smr, _, err := client.SpotMarketRequests.Get(id, &packngo.GetOptions{Includes: []string{"project", "devices", "facilities", "metro"}})
if err != nil {
err = friendlyError(err)
if isNotFound(err) {
err = equinix_errors.FriendlyError(err)
if equinix_errors.IsNotFound(err) {
d.SetId("")
return nil
}
Expand All @@ -104,7 +107,7 @@ func dataSourceMetalSpotMarketRequestRead(ctx context.Context, d *schema.Resourc

d.SetId(id)

return setMap(d, map[string]interface{}{
return equinix_schema.SetMap(d, map[string]interface{}{
"device_ids": deviceIDs,
"end_at": func(d *schema.ResourceData, k string) error {
if smr.EndAt != nil {
Expand Down
17 changes: 10 additions & 7 deletions equinix/data_source_metal_vlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package equinix
import (
"fmt"

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

"github.com/equinix/terraform-provider-equinix/internal/config"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -78,7 +81,7 @@ func dataSourceMetalVlanRead(d *schema.ResourceData, meta interface{}) error {
facilityRaw, facilityOk := d.GetOk("facility")

if !(vlanIdOk || (vxlanOk || projectOk || metroOk || facilityOk)) {
return friendlyError(fmt.Errorf("You must set either vlan_id or a combination of vxlan, project_id, and, metro or facility"))
return equinix_errors.FriendlyError(fmt.Errorf("You must set either vlan_id or a combination of vxlan, project_id, and, metro or facility"))
}

var vlan *packngo.VirtualNetwork
Expand All @@ -90,7 +93,7 @@ func dataSourceMetalVlanRead(d *schema.ResourceData, meta interface{}) error {
&packngo.GetOptions{Includes: []string{"assigned_to"}},
)
if err != nil {
return friendlyError(err)
return equinix_errors.FriendlyError(err)
}

} else {
Expand All @@ -103,12 +106,12 @@ func dataSourceMetalVlanRead(d *schema.ResourceData, meta interface{}) error {
&packngo.GetOptions{Includes: []string{"assigned_to"}},
)
if err != nil {
return friendlyError(err)
return equinix_errors.FriendlyError(err)
}

vlan, err = matchingVlan(vlans.VirtualNetworks, vxlan, projectID, facility, metro)
if err != nil {
return friendlyError(err)
return equinix_errors.FriendlyError(err)
}
}

Expand All @@ -119,7 +122,7 @@ func dataSourceMetalVlanRead(d *schema.ResourceData, meta interface{}) error {

d.SetId(vlan.ID)

return setMap(d, map[string]interface{}{
return equinix_schema.SetMap(d, map[string]interface{}{
"vlan_id": vlan.ID,
"project_id": vlan.Project.ID,
"vxlan": vlan.VXLAN,
Expand All @@ -144,11 +147,11 @@ func matchingVlan(vlans []packngo.VirtualNetwork, vxlan int, projectID, facility
matches = append(matches, v)
}
if len(matches) > 1 {
return nil, friendlyError(fmt.Errorf("Project %s has more than one matching VLAN", projectID))
return nil, equinix_errors.FriendlyError(fmt.Errorf("Project %s has more than one matching VLAN", projectID))
}

if len(matches) == 0 {
return nil, friendlyError(fmt.Errorf("Project %s does not have matching VLANs", projectID))
return nil, equinix_errors.FriendlyError(fmt.Errorf("Project %s does not have matching VLANs", projectID))
}
return &matches[0], nil
}
4 changes: 3 additions & 1 deletion equinix/helpers_device.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"sync"
"time"

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

metalv1 "github.com/equinix-labs/metal-go/metal/v1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -144,7 +146,7 @@ func hwReservationStateRefreshFunc(client *packngo.Client, reservationId, instan
state := deprovisioning
switch {
case err != nil:
err = friendlyError(err)
err = equinix_errors.FriendlyError(err)
state = errstate
case r != nil && r.Provisionable:
state = provisionable
Expand Down
4 changes: 3 additions & 1 deletion equinix/resource_fabric_cloud_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"time"

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

"github.com/equinix/terraform-provider-equinix/internal/config"

v4 "github.com/equinix-labs/fabric-go/fabric/v4"
Expand Down Expand Up @@ -98,7 +100,7 @@ func resourceCloudRouterRead(ctx context.Context, d *schema.ResourceData, meta i

func setCloudRouterMap(d *schema.ResourceData, fcr v4.CloudRouter) diag.Diagnostics {
diags := diag.Diagnostics{}
err := setMap(d, map[string]interface{}{
err := equinix_schema.SetMap(d, map[string]interface{}{
"name": fcr.Name,
"href": fcr.Href,
"type": fcr.Type_,
Expand Down
4 changes: 3 additions & 1 deletion equinix/resource_fabric_connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"time"

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

"github.com/equinix/terraform-provider-equinix/internal/config"

v4 "github.com/equinix-labs/fabric-go/fabric/v4"
Expand Down Expand Up @@ -172,7 +174,7 @@ func resourceFabricConnectionRead(ctx context.Context, d *schema.ResourceData, m

func setFabricMap(d *schema.ResourceData, conn v4.Connection) diag.Diagnostics {
diags := diag.Diagnostics{}
err := setMap(d, map[string]interface{}{
err := equinix_schema.SetMap(d, map[string]interface{}{
"name": conn.Name,
"bandwidth": conn.Bandwidth,
"href": conn.Href,
Expand Down
6 changes: 4 additions & 2 deletions equinix/resource_fabric_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"runtime/debug"
"strings"

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

"github.com/equinix/terraform-provider-equinix/internal/config"

"github.com/antihax/optional"
Expand All @@ -33,7 +35,7 @@ func resourceFabricPortRead(ctx context.Context, d *schema.ResourceData, meta in

func setFabricPortMap(d *schema.ResourceData, port v4.Port) diag.Diagnostics {
diags := diag.Diagnostics{}
err := setMap(d, map[string]interface{}{
err := equinix_schema.SetMap(d, map[string]interface{}{
"name": port.Name,
"bandwidth": port.Bandwidth,
"available_bandwidth": port.AvailableBandwidth,
Expand All @@ -60,7 +62,7 @@ func setFabricPortMap(d *schema.ResourceData, port v4.Port) diag.Diagnostics {

func setPortsListMap(d *schema.ResourceData, spl v4.AllPortsResponse) diag.Diagnostics {
diags := diag.Diagnostics{}
err := setMap(d, map[string]interface{}{
err := equinix_schema.SetMap(d, map[string]interface{}{
"data": fabricPortsListToTerra(spl),
})
if err != nil {
Expand Down
6 changes: 4 additions & 2 deletions equinix/resource_fabric_routing_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"time"

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

"github.com/equinix/terraform-provider-equinix/internal/config"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

Expand Down Expand Up @@ -280,7 +282,7 @@ func setFabricRoutingProtocolMap(d *schema.ResourceData, rp v4.RoutingProtocolDa

err := error(nil)
if rp.Type_ == "BGP" {
err = setMap(d, map[string]interface{}{
err = equinix_schema.SetMap(d, map[string]interface{}{
"name": rp.RoutingProtocolBgpData.Name,
"href": rp.RoutingProtocolBgpData.Href,
"type": rp.RoutingProtocolBgpData.Type_,
Expand All @@ -296,7 +298,7 @@ func setFabricRoutingProtocolMap(d *schema.ResourceData, rp v4.RoutingProtocolDa
"change_log": changeLogToTerra(rp.RoutingProtocolBgpData.Changelog),
})
} else if rp.Type_ == "DIRECT" {
err = setMap(d, map[string]interface{}{
err = equinix_schema.SetMap(d, map[string]interface{}{
"name": rp.RoutingProtocolDirectData.Name,
"href": rp.RoutingProtocolDirectData.Href,
"type": rp.RoutingProtocolDirectData.Type_,
Expand Down
6 changes: 4 additions & 2 deletions equinix/resource_fabric_service_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"strings"
"time"

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

"github.com/antihax/optional"
"github.com/equinix/terraform-provider-equinix/internal/config"

Expand Down Expand Up @@ -236,7 +238,7 @@ func resourceFabricServiceProfileDelete(ctx context.Context, d *schema.ResourceD

func setFabricServiceProfileMap(d *schema.ResourceData, sp v4.ServiceProfile) diag.Diagnostics {
diags := diag.Diagnostics{}
err := setMap(d, map[string]interface{}{
err := equinix_schema.SetMap(d, map[string]interface{}{
"href": sp.Href,
"type": sp.Type_,
"name": sp.Name,
Expand Down Expand Up @@ -265,7 +267,7 @@ func setFabricServiceProfileMap(d *schema.ResourceData, sp v4.ServiceProfile) di

func setFabricServiceProfilesListMap(d *schema.ResourceData, spl v4.ServiceProfiles) diag.Diagnostics {
diags := diag.Diagnostics{}
err := setMap(d, map[string]interface{}{
err := equinix_schema.SetMap(d, map[string]interface{}{
"data": fabricServiceProfilesListToTerra(spl),
})
if err != nil {
Expand Down
10 changes: 6 additions & 4 deletions equinix/resource_metal_bgp_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package equinix
import (
"log"

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

"github.com/equinix/terraform-provider-equinix/internal/config"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -64,7 +66,7 @@ func resourceMetalBGPSessionCreate(d *schema.ResourceData, meta interface{}) err
DefaultRoute: &defaultRoute,
})
if err != nil {
return friendlyError(err)
return equinix_errors.FriendlyError(err)
}

d.SetId(bgpSession.ID)
Expand All @@ -78,8 +80,8 @@ func resourceMetalBGPSessionRead(d *schema.ResourceData, meta interface{}) error
bgpSession, _, err := client.BGPSessions.Get(d.Id(),
&packngo.GetOptions{Includes: []string{"device"}})
if err != nil {
err = friendlyError(err)
if isNotFound(err) {
err = equinix_errors.FriendlyError(err)
if equinix_errors.IsNotFound(err) {
log.Printf("[WARN] BGP Session (%s) not found, removing from state", d.Id())

d.SetId("")
Expand All @@ -105,5 +107,5 @@ func resourceMetalBGPSessionDelete(d *schema.ResourceData, meta interface{}) err
meta.(*config.Config).AddModuleToMetalUserAgent(d)
client := meta.(*config.Config).Metal
resp, err := client.BGPSessions.Delete(d.Id())
return ignoreResponseErrors(httpForbidden, httpNotFound)(resp, err)
return equinix_errors.IgnoreResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err)
}
Loading

0 comments on commit cb37f06

Please sign in to comment.