From 21d1cca46e3fa8325ce1660f38922767ad6eb339 Mon Sep 17 00:00:00 2001 From: bill fumerola Date: Thu, 26 Sep 2024 11:02:00 -0700 Subject: [PATCH 1/3] Rename errors.FriendlyError to errors.Friendly --- internal/errors/errors.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/errors/errors.go b/internal/errors/errors.go index 9a2f43cb5..4f281397a 100644 --- a/internal/errors/errors.go +++ b/internal/errors/errors.go @@ -13,6 +13,12 @@ import ( // FriendlyError improves error messages when the API error is blank or in an // alternate format (as is the case with invalid token or loadbalancer errors) func FriendlyError(err error) error { + return Friendly(err) +} + +// Friendly improves error messages when the API error is blank or in an +// alternate format (as is the case with invalid token or loadbalancer errors) +func Friendly(err error) error { if e, ok := err.(*packngo.ErrorResponse); ok { resp := e.Response errors := Errors(e.Errors) From 9e98cfb1142bb7eedab6f4700f038fe470558470 Mon Sep 17 00:00:00 2001 From: bill fumerola Date: Thu, 26 Sep 2024 11:02:44 -0700 Subject: [PATCH 2/3] `gofmt -w -r 'a.FriendlyError(b) -> a.Friendly(b)'` --- .../data_source_metal_spot_market_request.go | 2 +- equinix/resource_metal_bgp_session.go | 4 ++-- equinix/resource_metal_device_network_type.go | 2 +- equinix/resource_metal_ip_attachment.go | 4 ++-- equinix/resource_metal_port_vlan_attachment.go | 6 +++--- equinix/resource_metal_project_api_key.go | 6 +++--- equinix/resource_metal_reserved_ip_block.go | 2 +- equinix/resource_metal_spot_market_request.go | 2 +- internal/resources/metal/device/helpers.go | 2 +- internal/resources/metal/device/resource.go | 10 +++++----- internal/resources/metal/gateway/resource.go | 2 +- .../resources/metal/organization/datasource.go | 4 ++-- .../resources/metal/organization/resource.go | 12 ++++++------ .../metal/organization_member/resource.go | 16 ++++++++-------- internal/resources/metal/port/resource.go | 4 ++-- internal/resources/metal/project/bgp_config.go | 2 +- .../metal/project_ssh_key/datasource.go | 2 +- .../metal/project_ssh_key/resource.go | 8 ++++---- internal/resources/metal/ssh_key/resource.go | 8 ++++---- internal/resources/metal/vlan/datasource.go | 14 +++++++------- internal/resources/metal/vlan/resource.go | 18 +++++++++--------- internal/resources/metal/vrf/resource.go | 6 +++--- 22 files changed, 68 insertions(+), 68 deletions(-) diff --git a/equinix/data_source_metal_spot_market_request.go b/equinix/data_source_metal_spot_market_request.go index e18ffd7ab..c54fb32f1 100644 --- a/equinix/data_source_metal_spot_market_request.go +++ b/equinix/data_source_metal_spot_market_request.go @@ -90,7 +90,7 @@ 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 = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) if equinix_errors.IsNotFound(err) { d.SetId("") return nil diff --git a/equinix/resource_metal_bgp_session.go b/equinix/resource_metal_bgp_session.go index f1a83e9db..c82186b1d 100644 --- a/equinix/resource_metal_bgp_session.go +++ b/equinix/resource_metal_bgp_session.go @@ -66,7 +66,7 @@ func resourceMetalBGPSessionCreate(d *schema.ResourceData, meta interface{}) err DefaultRoute: &defaultRoute, }) if err != nil { - return equinix_errors.FriendlyError(err) + return equinix_errors.Friendly(err) } d.SetId(bgpSession.ID) @@ -80,7 +80,7 @@ func resourceMetalBGPSessionRead(d *schema.ResourceData, meta interface{}) error bgpSession, _, err := client.BGPSessions.Get(d.Id(), &packngo.GetOptions{Includes: []string{"device"}}) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) if equinix_errors.IsNotFound(err) { log.Printf("[WARN] BGP Session (%s) not found, removing from state", d.Id()) diff --git a/equinix/resource_metal_device_network_type.go b/equinix/resource_metal_device_network_type.go index 6282824b2..0a200b48d 100644 --- a/equinix/resource_metal_device_network_type.go +++ b/equinix/resource_metal_device_network_type.go @@ -96,7 +96,7 @@ func resourceMetalDeviceNetworkTypeRead(d *schema.ResourceData, meta interface{} _, devNType, err := getDevIDandNetworkType(d, client) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) if equinix_errors.IsNotFound(err) { log.Printf("[WARN] Device (%s) for Network Type request not found, removing from state", d.Id()) diff --git a/equinix/resource_metal_ip_attachment.go b/equinix/resource_metal_ip_attachment.go index 1dfdf3370..255e58ad0 100644 --- a/equinix/resource_metal_ip_attachment.go +++ b/equinix/resource_metal_ip_attachment.go @@ -59,7 +59,7 @@ func resourceMetalIPAttachmentRead(d *schema.ResourceData, meta interface{}) err client := meta.(*config.Config).Metal assignment, _, err := client.DeviceIPs.Get(d.Id(), nil) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) // If the IP attachment was already destroyed, mark as succesfully gone. if equinix_errors.IsNotFound(err) { @@ -96,7 +96,7 @@ func resourceMetalIPAttachmentDelete(d *schema.ResourceData, meta interface{}) e resp, err := client.DeviceIPs.Unassign(d.Id()) if equinix_errors.IgnoreResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil { - return equinix_errors.FriendlyError(err) + return equinix_errors.Friendly(err) } d.SetId("") diff --git a/equinix/resource_metal_port_vlan_attachment.go b/equinix/resource_metal_port_vlan_attachment.go index ca41068c3..ec3d4c57a 100644 --- a/equinix/resource_metal_port_vlan_attachment.go +++ b/equinix/resource_metal_port_vlan_attachment.go @@ -171,7 +171,7 @@ func resourceMetalPortVlanAttachmentRead(d *schema.ResourceData, meta interface{ dev, _, err := client.Devices.Get(deviceID, &packngo.GetOptions{Includes: []string{"virtual_networks,project,native_virtual_network"}}) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) if equinix_errors.IsNotFound(err) { log.Printf("[WARN] Device (%s) for Port Vlan Attachment not found, removing from state", d.Id()) @@ -265,11 +265,11 @@ func resourceMetalPortVlanAttachmentDelete(d *schema.ResourceData, meta interfac portName := d.Get("port_name").(string) port, err := client.DevicePorts.GetPortByName(deviceID, portName) if err != nil { - return equinix_errors.FriendlyError(err) + return equinix_errors.Friendly(err) } _, _, err = client.DevicePorts.Bond(port, false) if err != nil { - return equinix_errors.FriendlyError(err) + return equinix_errors.Friendly(err) } } return nil diff --git a/equinix/resource_metal_project_api_key.go b/equinix/resource_metal_project_api_key.go index 2dc71b141..acd1527e5 100644 --- a/equinix/resource_metal_project_api_key.go +++ b/equinix/resource_metal_project_api_key.go @@ -70,7 +70,7 @@ func resourceMetalAPIKeyCreate(d *schema.ResourceData, meta interface{}) error { apiKey, _, err := client.APIKeys.Create(createRequest) if err != nil { - return equinix_errors.FriendlyError(err) + return equinix_errors.Friendly(err) } d.SetId(apiKey.ID) @@ -106,7 +106,7 @@ func resourceMetalAPIKeyRead(d *schema.ResourceData, meta interface{}) error { } if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) // If the key is somehow already destroyed, mark as // succesfully gone if equinix_errors.IsNotFound(err) { @@ -143,7 +143,7 @@ func resourceMetalAPIKeyDelete(d *schema.ResourceData, meta interface{}) error { resp, err := client.APIKeys.Delete(d.Id()) if equinix_errors.IgnoreResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil { - return equinix_errors.FriendlyError(err) + return equinix_errors.Friendly(err) } d.SetId("") diff --git a/equinix/resource_metal_reserved_ip_block.go b/equinix/resource_metal_reserved_ip_block.go index 047ae84c5..e31586942 100644 --- a/equinix/resource_metal_reserved_ip_block.go +++ b/equinix/resource_metal_reserved_ip_block.go @@ -474,7 +474,7 @@ func resourceMetalReservedIPBlockRead(ctx context.Context, d *schema.ResourceDat reservedBlock, _, err := client.ProjectIPs.Get(id, getOpts) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) if equinix_errors.IsNotFound(err) { log.Printf("[WARN] Reserved IP Block (%s) not found, removing from state", d.Id()) d.SetId("") diff --git a/equinix/resource_metal_spot_market_request.go b/equinix/resource_metal_spot_market_request.go index b44c11017..fc13d59b8 100644 --- a/equinix/resource_metal_spot_market_request.go +++ b/equinix/resource_metal_spot_market_request.go @@ -371,7 +371,7 @@ func resourceMetalSpotMarketRequestRead(ctx context.Context, d *schema.ResourceD smr, _, err := client.SpotMarketRequests.Get(d.Id(), &packngo.GetOptions{Includes: []string{"project", "devices", "facilities", "metro"}}) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) if equinix_errors.IsNotFound(err) { log.Printf("[WARN] SpotMarketRequest (%s) not found, removing from state", d.Id()) d.SetId("") diff --git a/internal/resources/metal/device/helpers.go b/internal/resources/metal/device/helpers.go index a2aeae2b8..695a6c48e 100644 --- a/internal/resources/metal/device/helpers.go +++ b/internal/resources/metal/device/helpers.go @@ -120,7 +120,7 @@ func hwReservationStateRefreshFunc(ctx context.Context, client *metalv1.APIClien state := deprovisioning switch { case err != nil: - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) state = errstate case r != nil && r.GetProvisionable(): state = provisionable diff --git a/internal/resources/metal/device/resource.go b/internal/resources/metal/device/resource.go index 9405998aa..e2da1f994 100644 --- a/internal/resources/metal/device/resource.go +++ b/internal/resources/metal/device/resource.go @@ -553,7 +553,7 @@ func resourceMetalDeviceCreate(ctx context.Context, d *schema.ResourceData, meta projectID := d.Get("project_id").(string) newDevice, _, err := client.DevicesApi.CreateDevice(ctx, projectID).CreateDeviceRequest(createRequest).Execute() if err != nil { - retErr := equinix_errors.FriendlyError(err) + retErr := equinix_errors.Friendly(err) if equinix_errors.IsNotFound(retErr) { retErr = fmt.Errorf("%s, make sure project \"%s\" exists", retErr, projectID) } @@ -737,7 +737,7 @@ func resourceMetalDeviceUpdate(ctx context.Context, d *schema.ResourceData, meta start := time.Now() if !reflect.DeepEqual(ur, metalv1.DeviceUpdateInput{}) { if _, _, err := client.DevicesApi.UpdateDevice(ctx, d.Id()).DeviceUpdateInput(ur).Execute(); err != nil { - return diag.FromErr(equinix_errors.FriendlyError(err)) + return diag.FromErr(equinix_errors.Friendly(err)) } } @@ -774,7 +774,7 @@ func doReinstall(ctx context.Context, client *metalv1.APIClient, d *schema.Resou } if _, err := client.DevicesApi.PerformAction(ctx, d.Id()).DeviceActionInput(reinstallOptions).Execute(); err != nil { - return equinix_errors.FriendlyError(err) + return equinix_errors.Friendly(err) } updateTimeout := d.Timeout(schema.TimeoutUpdate) - 30*time.Second - time.Since(start) @@ -799,7 +799,7 @@ func resourceMetalDeviceDelete(ctx context.Context, d *schema.ResourceData, meta resp, err := client.DevicesApi.DeleteDevice(ctx, d.Id()).ForceDelete(fdv).Execute() if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil { - return diag.FromErr(equinix_errors.FriendlyError(err)) + return diag.FromErr(equinix_errors.Friendly(err)) } resId, resIdOk := d.GetOk("deployed_hardware_reservation_id") @@ -844,7 +844,7 @@ func WaitForActiveDevice(ctx context.Context, d *schema.ResourceData, meta inter state, err := waitForDeviceAttribute(ctx, d, stateConf) if err != nil { d.SetId("") - fErr := equinix_errors.FriendlyError(err) + fErr := equinix_errors.Friendly(err) if equinix_errors.IsForbidden(fErr) { // If the device doesn't get to the active state, we can't recover it from here. diff --git a/internal/resources/metal/gateway/resource.go b/internal/resources/metal/gateway/resource.go index 345febef7..48acee133 100644 --- a/internal/resources/metal/gateway/resource.go +++ b/internal/resources/metal/gateway/resource.go @@ -202,7 +202,7 @@ func getGatewayAndParse(ctx context.Context, client *metalv1.APIClient, state *R // API call to get the Metal Gateway gw, _, err := client.MetalGatewaysApi.FindMetalGatewayById(ctx, id).Include(includes).Execute() if err != nil { - return diags, equinix_errors.FriendlyError(err) + return diags, equinix_errors.Friendly(err) } // Parse the API response into the Terraform state diff --git a/internal/resources/metal/organization/datasource.go b/internal/resources/metal/organization/datasource.go index 614bbc01d..760a3ee51 100644 --- a/internal/resources/metal/organization/datasource.go +++ b/internal/resources/metal/organization/datasource.go @@ -58,7 +58,7 @@ func (r *DataSource) Read( if !name.IsNull() { orgList, _, err := client.Organizations.List(&packngo.GetOptions{Includes: []string{"address"}}) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) resp.Diagnostics.AddError( "Error listing Organizations", err.Error(), @@ -77,7 +77,7 @@ func (r *DataSource) Read( orgID := orgId.ValueString() org, _, err := client.Organizations.Get(orgID, &packngo.GetOptions{Includes: []string{"address"}}) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) resp.Diagnostics.AddError( "Error getting Organization", err.Error(), diff --git a/internal/resources/metal/organization/resource.go b/internal/resources/metal/organization/resource.go index f0273bd61..ab87e847d 100644 --- a/internal/resources/metal/organization/resource.go +++ b/internal/resources/metal/organization/resource.go @@ -74,7 +74,7 @@ func (r *Resource) Create( if err != nil { resp.Diagnostics.AddError( "Failed to create Organizations", - equinix_errors.FriendlyError(err).Error(), + equinix_errors.Friendly(err).Error(), ) return } @@ -117,7 +117,7 @@ func (r *Resource) Read( diags, err := getOrganizationAndParse(ctx, client, &state, id) resp.Diagnostics.Append(diags...) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) // If the key is somehow already destroyed, mark as // succesfully gone @@ -210,7 +210,7 @@ func (r *Resource) Update( // Update the resource _, _, err := client.Organizations.Update(id, updateRequest) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) resp.Diagnostics.AddError( "Error updating resource", "Could not update Metal Organization with ID "+id+": "+err.Error(), @@ -222,7 +222,7 @@ func (r *Resource) Update( diags, err := getOrganizationAndParse(ctx, client, &plan, id) resp.Diagnostics.Append(diags...) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) resp.Diagnostics.AddError( "Error updating resource", "Could not read Metal Organization with ID "+id+": "+err.Error(), @@ -255,7 +255,7 @@ func (r *Resource) Delete( // Use API client to delete the resource deleteResp, err := client.Organizations.Delete(id) if equinix_errors.IgnoreResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(deleteResp, err) != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) resp.Diagnostics.AddError( fmt.Sprintf("Failed to delete Organizations %s", id), err.Error(), @@ -268,7 +268,7 @@ func getOrganizationAndParse(ctx context.Context, client *packngo.Client, state includes := &packngo.GetOptions{Includes: []string{"address"}} org, _, err := client.Organizations.Get(id, includes) if err != nil { - return diags, equinix_errors.FriendlyError(err) + return diags, equinix_errors.Friendly(err) } // Parse the API response into the Terraform state diags = state.parse(ctx, org) diff --git a/internal/resources/metal/organization_member/resource.go b/internal/resources/metal/organization_member/resource.go index 7b7c7ec88..61a2ff5a9 100644 --- a/internal/resources/metal/organization_member/resource.go +++ b/internal/resources/metal/organization_member/resource.go @@ -116,7 +116,7 @@ func (r *Resource) Create( listOpts := &packngo.ListOptions{Includes: []string{"user"}} invitations, _, err := client.Invitations.List(orgID, listOpts) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) // If the org was destroyed, mark as gone. if equinix_errors.IsNotFound(err) { plan.OrganizationID = basetypes.NewStringNull() @@ -131,7 +131,7 @@ func (r *Resource) Create( members, _, err := client.Members.List(orgID, listOpts) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) // If the org was destroyed, mark as gone. if equinix_errors.IsNotFound(err) { return @@ -189,7 +189,7 @@ func (r *Resource) Read( listOpts := &packngo.ListOptions{Includes: []string{"user"}} invitations, _, err := client.Invitations.List(orgID, listOpts) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) // If the org was destroyed, mark as gone. if equinix_errors.IsNotFound(err) { data.OrganizationID = basetypes.NewStringNull() @@ -204,7 +204,7 @@ func (r *Resource) Read( members, _, err := client.Members.List(orgID, &packngo.GetOptions{Includes: []string{"user"}}) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) // If the org was destroyed, mark as gone. if equinix_errors.IsNotFound(err) { data.OrganizationID = basetypes.NewStringNull() @@ -246,7 +246,7 @@ func (r *Resource) Delete( listOpts := &packngo.ListOptions{Includes: []string{"user"}} invitations, _, err := client.Invitations.List(data.OrganizationID.ValueString(), listOpts) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) // If the org was destroyed, mark as gone. if equinix_errors.IsNotFound(err) { data.OrganizationID = types.StringNull() @@ -261,7 +261,7 @@ func (r *Resource) Delete( org, _, err := client.Organizations.Get(data.OrganizationID.ValueString(), &packngo.GetOptions{Includes: []string{"members", "members.user"}}) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) // If the org was destroyed, mark as gone. if equinix_errors.IsNotFound(err) { data.OrganizationID = types.StringNull() @@ -284,7 +284,7 @@ func (r *Resource) Delete( if member.isMember() { _, err = client.Members.Delete(data.OrganizationID.ValueString(), member.Member.ID) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) // If the member was deleted, mark as gone. if equinix_errors.IsNotFound(err) { data.OrganizationID = types.StringNull() @@ -299,7 +299,7 @@ func (r *Resource) Delete( } else if member.isInvitation() { _, err = client.Invitations.Delete(member.Invitation.ID) if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) // If the invitation was deleted, mark as gone. if equinix_errors.IsNotFound(err) { data.OrganizationID = types.StringNull() diff --git a/internal/resources/metal/port/resource.go b/internal/resources/metal/port/resource.go index fbcb01d69..65b30f116 100644 --- a/internal/resources/metal/port/resource.go +++ b/internal/resources/metal/port/resource.go @@ -132,7 +132,7 @@ func resourceMetalPortUpdate(ctx context.Context, d *schema.ResourceData, meta i start := time.Now() cpr, _, err := getClientPortResource(ctx, d, meta) if err != nil { - return diag.FromErr(equinix_errors.FriendlyError(err)) + return diag.FromErr(equinix_errors.Friendly(err)) } for _, f := range [](func(context.Context, *ClientPortResource) error){ @@ -146,7 +146,7 @@ func resourceMetalPortUpdate(ctx context.Context, d *schema.ResourceData, meta i updateNativeVlan, } { if err := f(ctx, cpr); err != nil { - return diag.FromErr(equinix_errors.FriendlyError(err)) + return diag.FromErr(equinix_errors.Friendly(err)) } } diff --git a/internal/resources/metal/project/bgp_config.go b/internal/resources/metal/project/bgp_config.go index 20793845a..eab43549a 100644 --- a/internal/resources/metal/project/bgp_config.go +++ b/internal/resources/metal/project/bgp_config.go @@ -15,7 +15,7 @@ func fetchBGPConfig(ctx context.Context, client *metalv1.APIClient, projectID st bgpConfig, _, err := client.BGPApi.FindBgpConfigByProject(ctx, projectID).Execute() if err != nil { - friendlyErr := equinix_errors.FriendlyError(err) + friendlyErr := equinix_errors.Friendly(err) diags.AddError( "Error reading BGP configuration", "Could not read BGP configuration for project with ID "+projectID+": "+friendlyErr.Error(), diff --git a/internal/resources/metal/project_ssh_key/datasource.go b/internal/resources/metal/project_ssh_key/datasource.go index 6dcfd7e58..c7da73ee8 100644 --- a/internal/resources/metal/project_ssh_key/datasource.go +++ b/internal/resources/metal/project_ssh_key/datasource.go @@ -51,7 +51,7 @@ func (r *DataSource) Read( // Use API client to list SSH keys keysList, _, err := client.SSHKeysApi.FindProjectSSHKeys(ctx, projectID).Query(search).Execute() if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) resp.Diagnostics.AddError( "Error listing project ssh keys", err.Error(), diff --git a/internal/resources/metal/project_ssh_key/resource.go b/internal/resources/metal/project_ssh_key/resource.go index 26f863388..9183c86c7 100644 --- a/internal/resources/metal/project_ssh_key/resource.go +++ b/internal/resources/metal/project_ssh_key/resource.go @@ -53,7 +53,7 @@ func (r *Resource) Create( if err != nil { resp.Diagnostics.AddError( "Failed to create Project SSH Key", - equinix_errors.FriendlyError(err).Error(), + equinix_errors.Friendly(err).Error(), ) return } @@ -88,7 +88,7 @@ func (r *Resource) Read( // Use API client to get the current state of the resource key, _, err := client.SSHKeysApi.FindSSHKeyById(ctx, id).Include(nil).Execute() if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) // If the key is somehow already destroyed, mark as // succesfully gone @@ -145,7 +145,7 @@ func (r *Resource) Update( // Update the resource key, _, err := client.SSHKeysApi.UpdateSSHKey(ctx, id).SSHKeyInput(*updateRequest).Execute() if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) resp.Diagnostics.AddError( "Error updating resource", "Could not update resource with ID "+id+": "+err.Error(), @@ -183,7 +183,7 @@ func (r *Resource) Delete( // Use API client to delete the resource deleteResp, err := client.SSHKeysApi.DeleteSSHKey(ctx, id).Execute() if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(deleteResp, err) != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) resp.Diagnostics.AddError( fmt.Sprintf("Failed to delete Project SSHKey %s", id), err.Error(), diff --git a/internal/resources/metal/ssh_key/resource.go b/internal/resources/metal/ssh_key/resource.go index 4b76e4ec9..2b8e2b25a 100644 --- a/internal/resources/metal/ssh_key/resource.go +++ b/internal/resources/metal/ssh_key/resource.go @@ -51,7 +51,7 @@ func (r *Resource) Create( if err != nil { resp.Diagnostics.AddError( "Failed to create SSH Key", - equinix_errors.FriendlyError(err).Error(), + equinix_errors.Friendly(err).Error(), ) return } @@ -86,7 +86,7 @@ func (r *Resource) Read( // Use API client to get the current state of the resource key, _, err := client.SSHKeysApi.FindSSHKeyById(ctx, id).Include(nil).Execute() if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) // If the key is somehow already destroyed, mark as // succesfully gone @@ -143,7 +143,7 @@ func (r *Resource) Update( // Update the resource key, _, err := client.SSHKeysApi.UpdateSSHKey(ctx, id).SSHKeyInput(*updateRequest).Execute() if err != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) resp.Diagnostics.AddError( "Error updating resource", "Could not update resource with ID "+id+": "+err.Error(), @@ -181,7 +181,7 @@ func (r *Resource) Delete( // Use API client to delete the resource deleteResp, err := client.SSHKeysApi.DeleteSSHKey(ctx, id).Execute() if equinix_errors.IgnoreHttpResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(deleteResp, err) != nil { - err = equinix_errors.FriendlyError(err) + err = equinix_errors.Friendly(err) resp.Diagnostics.AddError( fmt.Sprintf("Failed to delete SSHKey %s", id), err.Error(), diff --git a/internal/resources/metal/vlan/datasource.go b/internal/resources/metal/vlan/datasource.go index 9a751d450..6ce4328a5 100644 --- a/internal/resources/metal/vlan/datasource.go +++ b/internal/resources/metal/vlan/datasource.go @@ -57,8 +57,8 @@ func (r *DataSource) Read( if data.VlanID.IsNull() && (data.Vxlan.IsNull() && data.ProjectID.IsNull() && data.Metro.IsNull() && data.Facility.IsNull()) { resp.Diagnostics.AddError("Error fetching Vlan datasource", - equinix_errors. - FriendlyError(fmt.Errorf("You must set either vlan_id or a combination of vxlan, project_id, and, metro or facility")). + equinix_errors.Friendly( + fmt.Errorf("You must set either vlan_id or a combination of vxlan, project_id, and, metro or facility")). Error()) return } @@ -72,7 +72,7 @@ func (r *DataSource) Read( &packngo.GetOptions{Includes: []string{"assigned_to"}}, ) if err != nil { - resp.Diagnostics.AddError("Error fetching Vlan using vlanId", equinix_errors.FriendlyError(err).Error()) + resp.Diagnostics.AddError("Error fetching Vlan using vlanId", equinix_errors.Friendly(err).Error()) return } @@ -83,14 +83,14 @@ func (r *DataSource) Read( ) if err != nil { resp.Diagnostics.AddError("Error fetching vlan list for projectId", - equinix_errors.FriendlyError(err).Error()) + equinix_errors.Friendly(err).Error()) return } vlan, err = MatchingVlan(vlans.VirtualNetworks, int(data.Vxlan.ValueInt64()), data.ProjectID.ValueString(), data.Facility.ValueString(), data.Metro.ValueString()) if err != nil { - resp.Diagnostics.AddError("Error expected vlan not found", equinix_errors.FriendlyError(err).Error()) + resp.Diagnostics.AddError("Error expected vlan not found", equinix_errors.Friendly(err).Error()) return } } @@ -125,11 +125,11 @@ func MatchingVlan(vlans []packngo.VirtualNetwork, vxlan int, projectID, facility matches = append(matches, v) } if len(matches) > 1 { - return nil, equinix_errors.FriendlyError(fmt.Errorf("Project %s has more than one matching VLAN", projectID)) + return nil, equinix_errors.Friendly(fmt.Errorf("Project %s has more than one matching VLAN", projectID)) } if len(matches) == 0 { - return nil, equinix_errors.FriendlyError(fmt.Errorf("Project %s does not have matching VLANs for vlan [%d] and metro [%s]", projectID, vxlan, metro)) + return nil, equinix_errors.Friendly(fmt.Errorf("Project %s does not have matching VLANs for vlan [%d] and metro [%s]", projectID, vxlan, metro)) } return &matches[0], nil } diff --git a/internal/resources/metal/vlan/resource.go b/internal/resources/metal/vlan/resource.go index 80d13d0c1..a5c03641a 100644 --- a/internal/resources/metal/vlan/resource.go +++ b/internal/resources/metal/vlan/resource.go @@ -60,12 +60,12 @@ func (r *Resource) Create(ctx context.Context, request resource.CreateRequest, r if data.Facility.IsNull() && data.Metro.IsNull() { response.Diagnostics.AddError("Invalid input params", - equinix_errors.FriendlyError(errors.New("one of facility or metro must be configured")).Error()) + equinix_errors.Friendly(errors.New("one of facility or metro must be configured")).Error()) return } if !data.Facility.IsNull() && !data.Vxlan.IsNull() { response.Diagnostics.AddError("Invalid input params", - equinix_errors.FriendlyError(errors.New("you can set vxlan only for metro vlan")).Error()) + equinix_errors.Friendly(errors.New("you can set vxlan only for metro vlan")).Error()) return } @@ -82,14 +82,14 @@ func (r *Resource) Create(ctx context.Context, request resource.CreateRequest, r } vlan, _, err := client.ProjectVirtualNetworks.Create(createRequest) if err != nil { - response.Diagnostics.AddError("Error creating Vlan", equinix_errors.FriendlyError(err).Error()) + response.Diagnostics.AddError("Error creating Vlan", equinix_errors.Friendly(err).Error()) return } // get the current state of newly created vlan with default include fields vlan, _, err = client.ProjectVirtualNetworks.Get(vlan.ID, &packngo.GetOptions{Includes: vlanDefaultIncludes}) if err != nil { - response.Diagnostics.AddError("Error reading Vlan after create", equinix_errors.FriendlyError(err).Error()) + response.Diagnostics.AddError("Error reading Vlan after create", equinix_errors.Friendly(err).Error()) return } @@ -127,7 +127,7 @@ func (r *Resource) Read(ctx context.Context, request resource.ReadRequest, respo return } response.Diagnostics.AddError("Error fetching Vlan using vlanId", - equinix_errors.FriendlyError(err).Error()) + equinix_errors.Friendly(err).Error()) return } @@ -170,12 +170,12 @@ func (r *Resource) Delete(ctx context.Context, request resource.DeleteRequest, r if equinix_errors.IgnoreResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil { response.Diagnostics.AddWarning( "Equinix Metal Vlan not found during delete", - equinix_errors.FriendlyError(err).Error(), + equinix_errors.Friendly(err).Error(), ) return } response.Diagnostics.AddError("Error fetching Vlan using vlanId", - equinix_errors.FriendlyError(err).Error()) + equinix_errors.Friendly(err).Error()) return } @@ -187,7 +187,7 @@ func (r *Resource) Delete(ctx context.Context, request resource.DeleteRequest, r _, resp, err = client.Ports.Unassign(port.ID, vlan.ID) if equinix_errors.IgnoreResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(resp, err) != nil { response.Diagnostics.AddError("Error unassign port with Vlan", - equinix_errors.FriendlyError(err).Error()) + equinix_errors.Friendly(err).Error()) return } } @@ -197,7 +197,7 @@ func (r *Resource) Delete(ctx context.Context, request resource.DeleteRequest, r if err := equinix_errors.IgnoreResponseErrors(equinix_errors.HttpForbidden, equinix_errors.HttpNotFound)(client.ProjectVirtualNetworks.Delete(vlan.ID)); err != nil { response.Diagnostics.AddError("Error deleting Vlan", - equinix_errors.FriendlyError(err).Error()) + equinix_errors.Friendly(err).Error()) return } } diff --git a/internal/resources/metal/vrf/resource.go b/internal/resources/metal/vrf/resource.go index e8bf6688d..f04a22c4f 100644 --- a/internal/resources/metal/vrf/resource.go +++ b/internal/resources/metal/vrf/resource.go @@ -82,7 +82,7 @@ func resourceMetalVRFCreate(ctx context.Context, d *schema.ResourceData, meta in VrfCreateInput(createRequest). Execute() if err != nil { - return diag.FromErr(equinix_errors.FriendlyError(err)) + return diag.FromErr(equinix_errors.Friendly(err)) } d.SetId(vrf.GetId()) @@ -113,7 +113,7 @@ func resourceMetalVRFUpdate(ctx context.Context, d *schema.ResourceData, meta in VrfUpdateInput(updateRequest). Execute() if err != nil { - return diag.FromErr(equinix_errors.FriendlyError(err)) + return diag.FromErr(equinix_errors.Friendly(err)) } return resourceMetalVRFRead(ctx, d, meta) @@ -156,5 +156,5 @@ func resourceMetalVRFDelete(ctx context.Context, d *schema.ResourceData, meta in d.SetId("") } - return diag.FromErr(equinix_errors.FriendlyError(err)) + return diag.FromErr(equinix_errors.Friendly(err)) } From 8387c5a5db50ade8fdb3bff6e67eac475b1ddc2f Mon Sep 17 00:00:00 2001 From: bill fumerola Date: Thu, 26 Sep 2024 11:03:39 -0700 Subject: [PATCH 3/3] Remove FriendlyError --- internal/errors/errors.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/internal/errors/errors.go b/internal/errors/errors.go index 4f281397a..5f1d58968 100644 --- a/internal/errors/errors.go +++ b/internal/errors/errors.go @@ -10,12 +10,6 @@ import ( "github.com/packethost/packngo" ) -// FriendlyError improves error messages when the API error is blank or in an -// alternate format (as is the case with invalid token or loadbalancer errors) -func FriendlyError(err error) error { - return Friendly(err) -} - // Friendly improves error messages when the API error is blank or in an // alternate format (as is the case with invalid token or loadbalancer errors) func Friendly(err error) error {