From 3037ac1b59877d058f7b5eb1c331b88c53b9cee6 Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Tue, 13 Jun 2023 20:57:23 -0400 Subject: [PATCH 1/4] suppress API error on 404 --- apstra/resource_interface_map.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/apstra/resource_interface_map.go b/apstra/resource_interface_map.go index c12a7328..1e5e6360 100644 --- a/apstra/resource_interface_map.go +++ b/apstra/resource_interface_map.go @@ -307,14 +307,16 @@ type rInterfaceMap struct { func (o *rInterfaceMap) fetchEmbeddedObjects(ctx context.Context, client *apstra.Client, diags *diag.Diagnostics) (*apstra.LogicalDevice, *apstra.DeviceProfile) { var ace apstra.ApstraClientErr + // fetch the logical device ld, err := client.GetLogicalDevice(ctx, apstra.ObjectId(o.LogicalDeviceId.ValueString())) if err != nil { if errors.As(err, &ace) && ace.Type() == apstra.ErrNotfound { diags.AddAttributeError(path.Root("logical_device_id"), errInvalidConfig, - fmt.Sprintf("logical device'%s' not found", o.DeviceProfileId.ValueString())) + fmt.Sprintf("logical device %q not found", o.DeviceProfileId)) + } else { + diags.AddError("error while fetching logical device", err.Error()) } - diags.AddError("error while fetching logical device", err.Error()) } // fetch the device profile specified by the user @@ -322,9 +324,10 @@ func (o *rInterfaceMap) fetchEmbeddedObjects(ctx context.Context, client *apstra if err != nil { if errors.As(err, &ace) && ace.Type() == apstra.ErrNotfound { diags.AddAttributeError(path.Root("device_profile_id"), errInvalidConfig, - fmt.Sprintf("device profile '%s' not found", o.DeviceProfileId.ValueString())) + fmt.Sprintf("device profile %q not found", o.DeviceProfileId)) + } else { + diags.AddError("error while fetching device profile", err.Error()) } - diags.AddError("error while fetching device profile", err.Error()) } return ld, dp @@ -346,6 +349,7 @@ func (o *rInterfaceMap) ldPortNames(ctx context.Context, diags *diag.Diagnostics for i, planIntf := range interfaces { result[i] = planIntf.LogicalDevicePort.ValueString() } + return result } From d1a429cc4794ac75d44c99fc6989776f3d4461af Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Wed, 14 Jun 2023 10:47:55 -0400 Subject: [PATCH 2/4] update error string per style guide --- apstra/resource_interface_map.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apstra/resource_interface_map.go b/apstra/resource_interface_map.go index 1e5e6360..a1585f9d 100644 --- a/apstra/resource_interface_map.go +++ b/apstra/resource_interface_map.go @@ -315,7 +315,7 @@ func (o *rInterfaceMap) fetchEmbeddedObjects(ctx context.Context, client *apstra diags.AddAttributeError(path.Root("logical_device_id"), errInvalidConfig, fmt.Sprintf("logical device %q not found", o.DeviceProfileId)) } else { - diags.AddError("error while fetching logical device", err.Error()) + diags.AddError("failed to fetch logical device", err.Error()) } } @@ -326,7 +326,7 @@ func (o *rInterfaceMap) fetchEmbeddedObjects(ctx context.Context, client *apstra diags.AddAttributeError(path.Root("device_profile_id"), errInvalidConfig, fmt.Sprintf("device profile %q not found", o.DeviceProfileId)) } else { - diags.AddError("error while fetching device profile", err.Error()) + diags.AddError("failed to fetch device profile", err.Error()) } } From 89195aa45a21ab38f6018261cd3cdf965bfdf34f Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Wed, 14 Jun 2023 17:54:05 -0400 Subject: [PATCH 3/4] use IsApstra404() --- apstra/resource_interface_map.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/apstra/resource_interface_map.go b/apstra/resource_interface_map.go index a1585f9d..3529b172 100644 --- a/apstra/resource_interface_map.go +++ b/apstra/resource_interface_map.go @@ -23,6 +23,7 @@ import ( "sort" "strconv" "strings" + "terraform-provider-apstra/apstra/utils" ) const ( @@ -311,7 +312,7 @@ func (o *rInterfaceMap) fetchEmbeddedObjects(ctx context.Context, client *apstra // fetch the logical device ld, err := client.GetLogicalDevice(ctx, apstra.ObjectId(o.LogicalDeviceId.ValueString())) if err != nil { - if errors.As(err, &ace) && ace.Type() == apstra.ErrNotfound { + if utils.IsApstra404(err) { diags.AddAttributeError(path.Root("logical_device_id"), errInvalidConfig, fmt.Sprintf("logical device %q not found", o.DeviceProfileId)) } else { @@ -322,7 +323,7 @@ func (o *rInterfaceMap) fetchEmbeddedObjects(ctx context.Context, client *apstra // fetch the device profile specified by the user dp, err := client.GetDeviceProfile(ctx, apstra.ObjectId(o.DeviceProfileId.ValueString())) if err != nil { - if errors.As(err, &ace) && ace.Type() == apstra.ErrNotfound { + if utils.IsApstra404(err) { diags.AddAttributeError(path.Root("device_profile_id"), errInvalidConfig, fmt.Sprintf("device profile %q not found", o.DeviceProfileId)) } else { From d7d0b9488be6f5f124fa15ef17d7dc2ef0358eaa Mon Sep 17 00:00:00 2001 From: Chris Marget Date: Wed, 14 Jun 2023 17:55:48 -0400 Subject: [PATCH 4/4] remove newly-unused `ApstraClientErr` --- apstra/resource_interface_map.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/apstra/resource_interface_map.go b/apstra/resource_interface_map.go index 3529b172..b6977b6c 100644 --- a/apstra/resource_interface_map.go +++ b/apstra/resource_interface_map.go @@ -307,8 +307,6 @@ type rInterfaceMap struct { } func (o *rInterfaceMap) fetchEmbeddedObjects(ctx context.Context, client *apstra.Client, diags *diag.Diagnostics) (*apstra.LogicalDevice, *apstra.DeviceProfile) { - var ace apstra.ApstraClientErr - // fetch the logical device ld, err := client.GetLogicalDevice(ctx, apstra.ObjectId(o.LogicalDeviceId.ValueString())) if err != nil {