From 9148ced12b38023c806232fbc235b79b32239d4c Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Thu, 10 Oct 2024 14:06:04 +0200 Subject: [PATCH 1/8] feat: neatening up error handling --- .../provider/resources/group.go | 56 ++++++++-------- .../resources/network_group_assignment.go | 55 +++++++++------- .../resources/sensor_group_assignment.go | 65 +++++++++---------- .../resources/service_group_assignment.go | 15 +++-- .../provider/resources/wired_network.go | 20 ++++-- .../provider/resources/wireless_network.go | 20 ++++-- .../provider/util/error_handling.go | 49 ++++++++++++++ .../test/resources/group_test.go | 2 +- 8 files changed, 176 insertions(+), 106 deletions(-) create mode 100644 pkg/config-api-provider/provider/util/error_handling.go diff --git a/pkg/config-api-provider/provider/resources/group.go b/pkg/config-api-provider/provider/resources/group.go index 635d05e6..601f9621 100644 --- a/pkg/config-api-provider/provider/resources/group.go +++ b/pkg/config-api-provider/provider/resources/group.go @@ -26,13 +26,6 @@ type groupResourceModel struct { ParentGroupId types.String `tfsdk:"parent_group_id"` } -// TODO: Remove this once we rely fully on client for groups - -// TODO: Remove this once we rely fully on client for groups -type GroupUpdateRequestModel struct { - Name string -} - func NewGroupResource() resource.Resource { return &groupResource{} } @@ -99,12 +92,14 @@ func (r *groupResource) Create(ctx context.Context, req resource.CreateRequest, groups_post_request := config_api_client.NewGroupsPostRequest(plan.ParentGroupId.ValueString(), plan.Name.ValueString()) request := r.client.ConfigurationAPI.GroupsPostUxiV1alpha1GroupsPost(ctx).GroupsPostRequest(*groups_post_request) - group, _, err := util.RetryFor429(request.Execute) - if err != nil { - resp.Diagnostics.AddError( - "Error creating group", - "Could not create group, unexpected error: "+err.Error(), - ) + group, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.ResponseStatusCheck{ + Response: response, + Err: err, + }.RaiseForStatus() + + if errorPresent { + resp.Diagnostics.AddError(util.GenerateErrorSummary("create", "uxi_group"), errorDetail) return } @@ -133,20 +128,27 @@ func (r *groupResource) Read(ctx context.Context, req resource.ReadRequest, resp request := r.client.ConfigurationAPI. GroupsGetUxiV1alpha1GroupsGet(ctx). Uid(state.ID.ValueString()) + groupResponse, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.ResponseStatusCheck{ + Response: response, + Err: err, + }.RaiseForStatus() - groupResponse, _, err := util.RetryFor429(request.Execute) + errorSummary := util.GenerateErrorSummary("read", "uxi_group") - if err != nil || len(groupResponse.Items) != 1 { - resp.Diagnostics.AddError( - "Error reading Group", - "Could not retrieve Group, unexpected error: "+err.Error(), - ) + if errorPresent { + resp.Diagnostics.AddError(errorSummary, errorDetail) + return + } + + if len(groupResponse.Items) != 1 { + resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") return } group := groupResponse.Items[0] if util.IsRoot(group) { - resp.Diagnostics.AddError("operation not supported", "the root group cannot be used as a resource") + resp.Diagnostics.AddError(errorSummary, "The root group cannot be used as a resource") return } @@ -175,13 +177,15 @@ func (r *groupResource) Update(ctx context.Context, req resource.UpdateRequest, request := r.client.ConfigurationAPI. GroupsPatchUxiV1alpha1GroupsGroupUidPatch(ctx, plan.ID.ValueString()). GroupsPatchRequest(*patchRequest) - group, _, err := util.RetryFor429(request.Execute) + group, response, err := util.RetryFor429(request.Execute) - if err != nil { - resp.Diagnostics.AddError( - "Error updating Group", - "Could not update Group, unexpected error: "+err.Error(), - ) + errorPresent, errorDetail := util.ResponseStatusCheck{ + Response: response, + Err: err, + }.RaiseForStatus() + + if errorPresent { + resp.Diagnostics.AddError(util.GenerateErrorSummary("update", "uxi_group"), errorDetail) return } diff --git a/pkg/config-api-provider/provider/resources/network_group_assignment.go b/pkg/config-api-provider/provider/resources/network_group_assignment.go index 589261f8..26d1ed53 100644 --- a/pkg/config-api-provider/provider/resources/network_group_assignment.go +++ b/pkg/config-api-provider/provider/resources/network_group_assignment.go @@ -25,12 +25,6 @@ type networkGroupAssignmentResourceModel struct { GroupID types.String `tfsdk:"group_id"` } -type NetworkGroupAssignmentResponseModel struct { - UID string // - GroupUID string // , - NetworkUID string // -} - func NewNetworkGroupAssignmentResource() resource.Resource { return &networkGroupAssignmentResource{} } @@ -102,12 +96,14 @@ func (r *networkGroupAssignmentResource) Create(ctx context.Context, req resourc request := r.client.ConfigurationAPI. PostUxiV1alpha1NetworkGroupAssignmentsPost(ctx). NetworkGroupAssignmentsPostRequest(*postRequest) - networkGroupAssignment, _, err := util.RetryFor429(request.Execute) - if err != nil { - resp.Diagnostics.AddError( - "Error updating Network Group Assignment", - "Could not update Network Group Assignment, unexpected error: "+err.Error(), - ) + networkGroupAssignment, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.ResponseStatusCheck{ + Response: response, + Err: err, + }.RaiseForStatus() + + if errorPresent { + resp.Diagnostics.AddError(util.GenerateErrorSummary("create", "uxi_network_group_assignment"), errorDetail) return } @@ -136,13 +132,21 @@ func (r *networkGroupAssignmentResource) Read(ctx context.Context, req resource. request := r.client.ConfigurationAPI. GetUxiV1alpha1NetworkGroupAssignmentsGet(ctx). Uid(state.ID.ValueString()) - networkGroupAssignmentResponse, _, err := util.RetryFor429(request.Execute) + networkGroupAssignmentResponse, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.ResponseStatusCheck{ + Response: response, + Err: err, + }.RaiseForStatus() - if err != nil || len(networkGroupAssignmentResponse.Items) != 1 { - resp.Diagnostics.AddError( - "Error reading Network Group Assignment", - "Could not retrieve Network Group Assignment, unexpected error: "+err.Error(), - ) + errorSummary := util.GenerateErrorSummary("read", "uxi_network_group_assignment") + + if errorPresent { + resp.Diagnostics.AddError(errorSummary, errorDetail) + return + } + + if len(networkGroupAssignmentResponse.Items) != 1 { + resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") return } networkGroupAssignment := networkGroupAssignmentResponse.Items[0] @@ -181,13 +185,14 @@ func (r *networkGroupAssignmentResource) Delete(ctx context.Context, req resourc request := r.client.ConfigurationAPI. DeleteNetworkGroupAssignmentUxiV1alpha1NetworkGroupAssignmentsIdDelete(ctx, state.ID.ValueString()) - _, _, err := util.RetryFor429(request.Execute) - - if err != nil { - resp.Diagnostics.AddError( - "Error deleting Network Group Assignment", - "Could not delete Network Group Assignment, unexpected error: "+err.Error(), - ) + _, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.ResponseStatusCheck{ + Response: response, + Err: err, + }.RaiseForStatus() + + if errorPresent { + resp.Diagnostics.AddError(util.GenerateErrorSummary("delete", "uxi_network_group_assignment"), errorDetail) return } } diff --git a/pkg/config-api-provider/provider/resources/sensor_group_assignment.go b/pkg/config-api-provider/provider/resources/sensor_group_assignment.go index e47fcdb0..6ce72dab 100644 --- a/pkg/config-api-provider/provider/resources/sensor_group_assignment.go +++ b/pkg/config-api-provider/provider/resources/sensor_group_assignment.go @@ -25,12 +25,6 @@ type sensorGroupAssignmentResourceModel struct { GroupID types.String `tfsdk:"group_id"` } -type SensorGroupAssignmentResponseModel struct { - UID string `json:"uid"` - GroupUID string `json:"group_uid"` - SensorUID string `json:"sensor_uid"` -} - type SensorGroupAssignmentRequestModel struct { GroupUID string // , SensorUID string // @@ -105,12 +99,14 @@ func (r *sensorGroupAssignmentResource) Create(ctx context.Context, req resource postRequest := config_api_client.NewSensorGroupAssignmentsPostRequest(plan.GroupID.ValueString(), plan.SensorID.ValueString()) request := r.client.ConfigurationAPI.PostUxiV1alpha1SensorGroupAssignmentsPost(ctx).SensorGroupAssignmentsPostRequest(*postRequest) - sensorGroupAssignment, _, err := util.RetryFor429(request.Execute) - if err != nil { - resp.Diagnostics.AddError( - "Error creating sensor group assignment", - "Could not create sensor group assignment, unexpected error: "+err.Error(), - ) + sensorGroupAssignment, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.ResponseStatusCheck{ + Response: response, + Err: err, + }.RaiseForStatus() + + if errorPresent { + resp.Diagnostics.AddError(util.GenerateErrorSummary("create", "uxi_sensor_group_assignment"), errorDetail) return } @@ -137,13 +133,21 @@ func (r *sensorGroupAssignmentResource) Read(ctx context.Context, req resource.R request := r.client.ConfigurationAPI. GetUxiV1alpha1SensorGroupAssignmentsGet(ctx). Uid(state.ID.ValueString()) - sensorGroupAssignmentResponse, _, err := util.RetryFor429(request.Execute) + sensorGroupAssignmentResponse, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.ResponseStatusCheck{ + Response: response, + Err: err, + }.RaiseForStatus() - if err != nil || len(sensorGroupAssignmentResponse.Items) != 1 { - resp.Diagnostics.AddError( - "Error reading Sensor Group Assignment", - "Could not retrieve Sensor Group Assignment, unexpected error: "+err.Error(), - ) + errorSummary := util.GenerateErrorSummary("create", "uxi_sensor_group_assignment") + + if errorPresent { + resp.Diagnostics.AddError(errorSummary, errorDetail) + return + } + + if len(sensorGroupAssignmentResponse.Items) != 1 { + resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") return } @@ -184,13 +188,14 @@ func (r *sensorGroupAssignmentResource) Delete(ctx context.Context, req resource // Delete existing sensorGroupAssignment using the plan_id request := r.client.ConfigurationAPI. DeleteSensorGroupAssignmentUxiV1alpha1SensorGroupAssignmentsIdDelete(ctx, state.ID.ValueString()) - _, _, err := util.RetryFor429(request.Execute) - - if err != nil { - resp.Diagnostics.AddError( - "Error deleting Sensor Group Assignment", - "Could not deleting Sensor Group Assignment, unexpected error: "+err.Error(), - ) + _, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.ResponseStatusCheck{ + Response: response, + Err: err, + }.RaiseForStatus() + + if errorPresent { + resp.Diagnostics.AddError(util.GenerateErrorSummary("delete", "uxi_sensor_group_assignment"), errorDetail) return } } @@ -198,13 +203,3 @@ func (r *sensorGroupAssignmentResource) Delete(ctx context.Context, req resource func (r *sensorGroupAssignmentResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) } - -var CreateSensorGroupAssignment = func(request SensorGroupAssignmentRequestModel) SensorGroupAssignmentResponseModel { - // TODO: Query the sensorGroupAssignment using the client - - return SensorGroupAssignmentResponseModel{ - UID: "mock_uid", - GroupUID: "mock_group_uid", - SensorUID: "mock_sensor_uid", - } -} diff --git a/pkg/config-api-provider/provider/resources/service_group_assignment.go b/pkg/config-api-provider/provider/resources/service_group_assignment.go index e2b91694..8d03e770 100644 --- a/pkg/config-api-provider/provider/resources/service_group_assignment.go +++ b/pkg/config-api-provider/provider/resources/service_group_assignment.go @@ -96,13 +96,14 @@ func (r *serviceTestGroupAssignmentResource) Create(ctx context.Context, req res request := r.client.ConfigurationAPI. PostUxiV1alpha1ServiceTestGroupAssignmentsPost(ctx). ServiceTestGroupAssignmentsPostRequest(*postRequest) - serviceTestGroupAssignment, _, err := util.RetryFor429(request.Execute) - - if err != nil { - resp.Diagnostics.AddError( - "Error creating Service Test Group Assignment", - "Could not create Network Group Assignment, unexpected error: "+err.Error(), - ) + serviceTestGroupAssignment, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.ResponseStatusCheck{ + Response: response, + Err: err, + }.RaiseForStatus() + + if errorPresent { + resp.Diagnostics.AddError(util.GenerateErrorSummary("delete", "uxi_service_test_group_assignment"), errorDetail) return } diff --git a/pkg/config-api-provider/provider/resources/wired_network.go b/pkg/config-api-provider/provider/resources/wired_network.go index 19e4d41e..ca35e348 100644 --- a/pkg/config-api-provider/provider/resources/wired_network.go +++ b/pkg/config-api-provider/provider/resources/wired_network.go @@ -92,13 +92,21 @@ func (r *wiredNetworkResource) Read(ctx context.Context, req resource.ReadReques request := r.client.ConfigurationAPI. GetUxiV1alpha1WiredNetworksGet(ctx). Uid(state.ID.ValueString()) - networkResponse, _, err := util.RetryFor429(request.Execute) + networkResponse, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.ResponseStatusCheck{ + Response: response, + Err: err, + }.RaiseForStatus() - if err != nil || len(networkResponse.Items) != 1 { - resp.Diagnostics.AddError( - "Error reading Wired Network", - "Could not retrieve Wired Network, unexpected error: "+err.Error(), - ) + errorSummary := util.GenerateErrorSummary("read", "uxi_wired_network") + + if errorPresent { + resp.Diagnostics.AddError(errorSummary, errorDetail) + return + } + + if len(networkResponse.Items) != 1 { + resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") return } diff --git a/pkg/config-api-provider/provider/resources/wireless_network.go b/pkg/config-api-provider/provider/resources/wireless_network.go index 8de52291..46c078bf 100644 --- a/pkg/config-api-provider/provider/resources/wireless_network.go +++ b/pkg/config-api-provider/provider/resources/wireless_network.go @@ -92,13 +92,21 @@ func (r *wirelessNetworkResource) Read(ctx context.Context, req resource.ReadReq request := r.client.ConfigurationAPI. GetUxiV1alpha1WirelessNetworksGet(ctx). Uid(state.ID.ValueString()) - networkResponse, _, err := util.RetryFor429(request.Execute) + networkResponse, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.ResponseStatusCheck{ + Response: response, + Err: err, + }.RaiseForStatus() - if err != nil || len(networkResponse.Items) != 1 { - resp.Diagnostics.AddError( - "Error reading Wireless Networks", - "Could not retrieve Wireless Network, unexpected error: "+err.Error(), - ) + errorSummary := util.GenerateErrorSummary("read", "uxi_wireless_network") + + if errorPresent { + resp.Diagnostics.AddError(errorSummary, errorDetail) + return + } + + if len(networkResponse.Items) != 1 { + resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") return } diff --git a/pkg/config-api-provider/provider/util/error_handling.go b/pkg/config-api-provider/provider/util/error_handling.go new file mode 100644 index 00000000..6ef1c319 --- /dev/null +++ b/pkg/config-api-provider/provider/util/error_handling.go @@ -0,0 +1,49 @@ +package util + +import ( + "encoding/json" + "fmt" + "net/http" + "net/url" +) + +func GenerateErrorSummary(actionName string, entityName string) string { + return fmt.Sprintf("Error performing %s on %s", actionName, entityName) +} + +type ResponseStatusCheck struct { + Response *http.Response + Err error +} + +func (r ResponseStatusCheck) RaiseForStatus() (bool, string) { + if r.Err != nil { + var detail string + var data map[string]interface{} + + switch e := r.Err.(type) { + case *url.Error: + detail = r.handleURLError(e) + default: + err := json.NewDecoder(r.Response.Body).Decode(&data) + if err != nil { + detail = "Unexpected error: " + r.Err.Error() + } else { + detail = data["message"].(string) + "\nDebugID: " + data["debugId"].(string) + } + } + + return true, detail + } + return false, "" +} + +func (r ResponseStatusCheck) handleURLError(uErr *url.Error) string { + if uErr.Timeout() { + return "Error: Request timed out. Please check your network." + } else if uErr.Temporary() { + return "Error: Temporary network error. Please try again later." + } else { + return fmt.Sprintf("URL Error: %v\n", uErr) + } +} diff --git a/pkg/config-api-provider/test/resources/group_test.go b/pkg/config-api-provider/test/resources/group_test.go index 2db55206..be620f37 100644 --- a/pkg/config-api-provider/test/resources/group_test.go +++ b/pkg/config-api-provider/test/resources/group_test.go @@ -149,7 +149,7 @@ func TestRootGroupResource(t *testing.T) { to = uxi_group.my_root_group id = "my_root_group_uid" }`, - ExpectError: regexp.MustCompile(`the root group cannot be used as a resource`), + ExpectError: regexp.MustCompile(`The root group cannot be used as a resource`), }, }, }) From 1c9ff3096b5c0785d456a489fb2a38394cb6e63b Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Thu, 10 Oct 2024 14:12:32 +0200 Subject: [PATCH 2/8] neatening up --- pkg/config-api-provider/provider/util/error_handling.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/config-api-provider/provider/util/error_handling.go b/pkg/config-api-provider/provider/util/error_handling.go index 6ef1c319..95841630 100644 --- a/pkg/config-api-provider/provider/util/error_handling.go +++ b/pkg/config-api-provider/provider/util/error_handling.go @@ -25,8 +25,7 @@ func (r ResponseStatusCheck) RaiseForStatus() (bool, string) { case *url.Error: detail = r.handleURLError(e) default: - err := json.NewDecoder(r.Response.Body).Decode(&data) - if err != nil { + if err := json.NewDecoder(r.Response.Body).Decode(&data); err != nil { detail = "Unexpected error: " + r.Err.Error() } else { detail = data["message"].(string) + "\nDebugID: " + data["debugId"].(string) From be54e3782d9c304c4039c68d544b6247fbac8c9a Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Thu, 10 Oct 2024 14:22:38 +0200 Subject: [PATCH 3/8] Neaten up and do data sources --- .../provider/data-sources/group.go | 19 ++++++++++++------- .../data-sources/network_group_assignment.go | 17 +++++++++++------ .../data-sources/sensor_group_assignment.go | 17 +++++++++++------ .../provider/data-sources/wired_network.go | 17 +++++++++++------ .../provider/data-sources/wireless_network.go | 17 +++++++++++------ .../provider/resources/group.go | 15 +++------------ .../resources/network_group_assignment.go | 15 +++------------ .../resources/sensor_group_assignment.go | 15 +++------------ .../resources/service_group_assignment.go | 5 +---- .../provider/resources/wired_network.go | 5 +---- .../provider/resources/wireless_network.go | 5 +---- .../provider/util/error_handling.go | 19 +++++++------------ .../test/data-sources/group_test.go | 2 +- 13 files changed, 76 insertions(+), 92 deletions(-) diff --git a/pkg/config-api-provider/provider/data-sources/group.go b/pkg/config-api-provider/provider/data-sources/group.go index 183a2521..6eecc854 100644 --- a/pkg/config-api-provider/provider/data-sources/group.go +++ b/pkg/config-api-provider/provider/data-sources/group.go @@ -78,19 +78,24 @@ func (d *groupDataSource) Read(ctx context.Context, req datasource.ReadRequest, GroupsGetUxiV1alpha1GroupsGet(ctx). Uid(*state.Filter.GroupID) - groupResponse, _, err := util.RetryFor429(request.Execute) + groupResponse, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.RaiseForStatus(response, err) - if err != nil || len(groupResponse.Items) != 1 { - resp.Diagnostics.AddError( - "Error reading Group", - "Could not retrieve Group, unexpected error: "+err.Error(), - ) + errorSummary := util.GenerateErrorSummary("read", "uxi_group") + + if errorPresent { + resp.Diagnostics.AddError(errorSummary, errorDetail) + return + } + + if len(groupResponse.Items) != 1 { + resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") return } group := groupResponse.Items[0] if util.IsRoot(group) { - resp.Diagnostics.AddError("operation not supported", "the root group cannot be used as a data source") + resp.Diagnostics.AddError(errorSummary, "The root group cannot be used as a data source") return } diff --git a/pkg/config-api-provider/provider/data-sources/network_group_assignment.go b/pkg/config-api-provider/provider/data-sources/network_group_assignment.go index 55c94b3a..51586b31 100644 --- a/pkg/config-api-provider/provider/data-sources/network_group_assignment.go +++ b/pkg/config-api-provider/provider/data-sources/network_group_assignment.go @@ -72,13 +72,18 @@ func (d *networkGroupAssignmentDataSource) Read(ctx context.Context, req datasou request := d.client.ConfigurationAPI. GetUxiV1alpha1NetworkGroupAssignmentsGet(ctx). Uid(state.Filter.NetworkGroupAssignmentID) - networkGroupAssignmentResponse, _, err := util.RetryFor429(request.Execute) + networkGroupAssignmentResponse, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.RaiseForStatus(response, err) - if err != nil || len(networkGroupAssignmentResponse.Items) != 1 { - resp.Diagnostics.AddError( - "Error reading Network Group Assignment", - "Could not retrieve Network Group Assignment, unexpected error: "+err.Error(), - ) + errorSummary := util.GenerateErrorSummary("read", "uxi_network_group_assignment") + + if errorPresent { + resp.Diagnostics.AddError(errorSummary, errorDetail) + return + } + + if len(networkGroupAssignmentResponse.Items) != 1 { + resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") return } diff --git a/pkg/config-api-provider/provider/data-sources/sensor_group_assignment.go b/pkg/config-api-provider/provider/data-sources/sensor_group_assignment.go index e4cacac4..a6ef935d 100644 --- a/pkg/config-api-provider/provider/data-sources/sensor_group_assignment.go +++ b/pkg/config-api-provider/provider/data-sources/sensor_group_assignment.go @@ -74,13 +74,18 @@ func (d *sensorGroupAssignmentDataSource) Read(ctx context.Context, req datasour request := d.client.ConfigurationAPI. GetUxiV1alpha1SensorGroupAssignmentsGet(ctx). Uid(state.Filter.SensorGroupAssignmentID) - sensorGroupAssignmentResponse, _, err := util.RetryFor429(request.Execute) + sensorGroupAssignmentResponse, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.RaiseForStatus(response, err) - if err != nil || len(sensorGroupAssignmentResponse.Items) != 1 { - resp.Diagnostics.AddError( - "Error reading Sensor Group Assignment", - "Could not retrieve Sensor Group Assignment, unexpected error: "+err.Error(), - ) + errorSummary := util.GenerateErrorSummary("create", "uxi_sensor_group_assignment") + + if errorPresent { + resp.Diagnostics.AddError(errorSummary, errorDetail) + return + } + + if len(sensorGroupAssignmentResponse.Items) != 1 { + resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") return } diff --git a/pkg/config-api-provider/provider/data-sources/wired_network.go b/pkg/config-api-provider/provider/data-sources/wired_network.go index 42a221b6..9a813c4c 100644 --- a/pkg/config-api-provider/provider/data-sources/wired_network.go +++ b/pkg/config-api-provider/provider/data-sources/wired_network.go @@ -99,13 +99,18 @@ func (d *wiredNetworkDataSource) Read(ctx context.Context, req datasource.ReadRe request := d.client.ConfigurationAPI. GetUxiV1alpha1WiredNetworksGet(ctx). Uid(state.Filter.WiredNetworkID) - networkResponse, _, err := util.RetryFor429(request.Execute) + networkResponse, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.RaiseForStatus(response, err) - if err != nil || len(networkResponse.Items) != 1 { - resp.Diagnostics.AddError( - "Error reading Wired Network", - "Could not retrieve Wired Network, unexpected error: "+err.Error(), - ) + errorSummary := util.GenerateErrorSummary("read", "uxi_wired_network") + + if errorPresent { + resp.Diagnostics.AddError(errorSummary, errorDetail) + return + } + + if len(networkResponse.Items) != 1 { + resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") return } diff --git a/pkg/config-api-provider/provider/data-sources/wireless_network.go b/pkg/config-api-provider/provider/data-sources/wireless_network.go index 5128a781..3ef471a8 100644 --- a/pkg/config-api-provider/provider/data-sources/wireless_network.go +++ b/pkg/config-api-provider/provider/data-sources/wireless_network.go @@ -107,13 +107,18 @@ func (d *wirelessNetworkDataSource) Read(ctx context.Context, req datasource.Rea request := d.client.ConfigurationAPI. GetUxiV1alpha1WirelessNetworksGet(ctx). Uid(state.Filter.WirelessNetworkID) - networkResponse, _, err := util.RetryFor429(request.Execute) + networkResponse, response, err := util.RetryFor429(request.Execute) + errorPresent, errorDetail := util.RaiseForStatus(response, err) - if err != nil || len(networkResponse.Items) != 1 { - resp.Diagnostics.AddError( - "Error reading Wireless Network", - "Could not retrieve Wireless Network, unexpected error: "+err.Error(), - ) + errorSummary := util.GenerateErrorSummary("read", "uxi_wireless_network") + + if errorPresent { + resp.Diagnostics.AddError(errorSummary, errorDetail) + return + } + + if len(networkResponse.Items) != 1 { + resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") return } diff --git a/pkg/config-api-provider/provider/resources/group.go b/pkg/config-api-provider/provider/resources/group.go index 601f9621..7e4f86cb 100644 --- a/pkg/config-api-provider/provider/resources/group.go +++ b/pkg/config-api-provider/provider/resources/group.go @@ -93,10 +93,7 @@ func (r *groupResource) Create(ctx context.Context, req resource.CreateRequest, groups_post_request := config_api_client.NewGroupsPostRequest(plan.ParentGroupId.ValueString(), plan.Name.ValueString()) request := r.client.ConfigurationAPI.GroupsPostUxiV1alpha1GroupsPost(ctx).GroupsPostRequest(*groups_post_request) group, response, err := util.RetryFor429(request.Execute) - errorPresent, errorDetail := util.ResponseStatusCheck{ - Response: response, - Err: err, - }.RaiseForStatus() + errorPresent, errorDetail := util.RaiseForStatus(response, err) if errorPresent { resp.Diagnostics.AddError(util.GenerateErrorSummary("create", "uxi_group"), errorDetail) @@ -129,10 +126,7 @@ func (r *groupResource) Read(ctx context.Context, req resource.ReadRequest, resp GroupsGetUxiV1alpha1GroupsGet(ctx). Uid(state.ID.ValueString()) groupResponse, response, err := util.RetryFor429(request.Execute) - errorPresent, errorDetail := util.ResponseStatusCheck{ - Response: response, - Err: err, - }.RaiseForStatus() + errorPresent, errorDetail := util.RaiseForStatus(response, err) errorSummary := util.GenerateErrorSummary("read", "uxi_group") @@ -179,10 +173,7 @@ func (r *groupResource) Update(ctx context.Context, req resource.UpdateRequest, GroupsPatchRequest(*patchRequest) group, response, err := util.RetryFor429(request.Execute) - errorPresent, errorDetail := util.ResponseStatusCheck{ - Response: response, - Err: err, - }.RaiseForStatus() + errorPresent, errorDetail := util.RaiseForStatus(response, err) if errorPresent { resp.Diagnostics.AddError(util.GenerateErrorSummary("update", "uxi_group"), errorDetail) diff --git a/pkg/config-api-provider/provider/resources/network_group_assignment.go b/pkg/config-api-provider/provider/resources/network_group_assignment.go index 26d1ed53..1bf70038 100644 --- a/pkg/config-api-provider/provider/resources/network_group_assignment.go +++ b/pkg/config-api-provider/provider/resources/network_group_assignment.go @@ -97,10 +97,7 @@ func (r *networkGroupAssignmentResource) Create(ctx context.Context, req resourc PostUxiV1alpha1NetworkGroupAssignmentsPost(ctx). NetworkGroupAssignmentsPostRequest(*postRequest) networkGroupAssignment, response, err := util.RetryFor429(request.Execute) - errorPresent, errorDetail := util.ResponseStatusCheck{ - Response: response, - Err: err, - }.RaiseForStatus() + errorPresent, errorDetail := util.RaiseForStatus(response, err) if errorPresent { resp.Diagnostics.AddError(util.GenerateErrorSummary("create", "uxi_network_group_assignment"), errorDetail) @@ -133,10 +130,7 @@ func (r *networkGroupAssignmentResource) Read(ctx context.Context, req resource. GetUxiV1alpha1NetworkGroupAssignmentsGet(ctx). Uid(state.ID.ValueString()) networkGroupAssignmentResponse, response, err := util.RetryFor429(request.Execute) - errorPresent, errorDetail := util.ResponseStatusCheck{ - Response: response, - Err: err, - }.RaiseForStatus() + errorPresent, errorDetail := util.RaiseForStatus(response, err) errorSummary := util.GenerateErrorSummary("read", "uxi_network_group_assignment") @@ -186,10 +180,7 @@ func (r *networkGroupAssignmentResource) Delete(ctx context.Context, req resourc request := r.client.ConfigurationAPI. DeleteNetworkGroupAssignmentUxiV1alpha1NetworkGroupAssignmentsIdDelete(ctx, state.ID.ValueString()) _, response, err := util.RetryFor429(request.Execute) - errorPresent, errorDetail := util.ResponseStatusCheck{ - Response: response, - Err: err, - }.RaiseForStatus() + errorPresent, errorDetail := util.RaiseForStatus(response, err) if errorPresent { resp.Diagnostics.AddError(util.GenerateErrorSummary("delete", "uxi_network_group_assignment"), errorDetail) diff --git a/pkg/config-api-provider/provider/resources/sensor_group_assignment.go b/pkg/config-api-provider/provider/resources/sensor_group_assignment.go index 6ce72dab..047a1067 100644 --- a/pkg/config-api-provider/provider/resources/sensor_group_assignment.go +++ b/pkg/config-api-provider/provider/resources/sensor_group_assignment.go @@ -100,10 +100,7 @@ func (r *sensorGroupAssignmentResource) Create(ctx context.Context, req resource postRequest := config_api_client.NewSensorGroupAssignmentsPostRequest(plan.GroupID.ValueString(), plan.SensorID.ValueString()) request := r.client.ConfigurationAPI.PostUxiV1alpha1SensorGroupAssignmentsPost(ctx).SensorGroupAssignmentsPostRequest(*postRequest) sensorGroupAssignment, response, err := util.RetryFor429(request.Execute) - errorPresent, errorDetail := util.ResponseStatusCheck{ - Response: response, - Err: err, - }.RaiseForStatus() + errorPresent, errorDetail := util.RaiseForStatus(response, err) if errorPresent { resp.Diagnostics.AddError(util.GenerateErrorSummary("create", "uxi_sensor_group_assignment"), errorDetail) @@ -134,10 +131,7 @@ func (r *sensorGroupAssignmentResource) Read(ctx context.Context, req resource.R GetUxiV1alpha1SensorGroupAssignmentsGet(ctx). Uid(state.ID.ValueString()) sensorGroupAssignmentResponse, response, err := util.RetryFor429(request.Execute) - errorPresent, errorDetail := util.ResponseStatusCheck{ - Response: response, - Err: err, - }.RaiseForStatus() + errorPresent, errorDetail := util.RaiseForStatus(response, err) errorSummary := util.GenerateErrorSummary("create", "uxi_sensor_group_assignment") @@ -189,10 +183,7 @@ func (r *sensorGroupAssignmentResource) Delete(ctx context.Context, req resource request := r.client.ConfigurationAPI. DeleteSensorGroupAssignmentUxiV1alpha1SensorGroupAssignmentsIdDelete(ctx, state.ID.ValueString()) _, response, err := util.RetryFor429(request.Execute) - errorPresent, errorDetail := util.ResponseStatusCheck{ - Response: response, - Err: err, - }.RaiseForStatus() + errorPresent, errorDetail := util.RaiseForStatus(response, err) if errorPresent { resp.Diagnostics.AddError(util.GenerateErrorSummary("delete", "uxi_sensor_group_assignment"), errorDetail) diff --git a/pkg/config-api-provider/provider/resources/service_group_assignment.go b/pkg/config-api-provider/provider/resources/service_group_assignment.go index 8d03e770..269fbdeb 100644 --- a/pkg/config-api-provider/provider/resources/service_group_assignment.go +++ b/pkg/config-api-provider/provider/resources/service_group_assignment.go @@ -97,10 +97,7 @@ func (r *serviceTestGroupAssignmentResource) Create(ctx context.Context, req res PostUxiV1alpha1ServiceTestGroupAssignmentsPost(ctx). ServiceTestGroupAssignmentsPostRequest(*postRequest) serviceTestGroupAssignment, response, err := util.RetryFor429(request.Execute) - errorPresent, errorDetail := util.ResponseStatusCheck{ - Response: response, - Err: err, - }.RaiseForStatus() + errorPresent, errorDetail := util.RaiseForStatus(response, err) if errorPresent { resp.Diagnostics.AddError(util.GenerateErrorSummary("delete", "uxi_service_test_group_assignment"), errorDetail) diff --git a/pkg/config-api-provider/provider/resources/wired_network.go b/pkg/config-api-provider/provider/resources/wired_network.go index ca35e348..e902a93c 100644 --- a/pkg/config-api-provider/provider/resources/wired_network.go +++ b/pkg/config-api-provider/provider/resources/wired_network.go @@ -93,10 +93,7 @@ func (r *wiredNetworkResource) Read(ctx context.Context, req resource.ReadReques GetUxiV1alpha1WiredNetworksGet(ctx). Uid(state.ID.ValueString()) networkResponse, response, err := util.RetryFor429(request.Execute) - errorPresent, errorDetail := util.ResponseStatusCheck{ - Response: response, - Err: err, - }.RaiseForStatus() + errorPresent, errorDetail := util.RaiseForStatus(response, err) errorSummary := util.GenerateErrorSummary("read", "uxi_wired_network") diff --git a/pkg/config-api-provider/provider/resources/wireless_network.go b/pkg/config-api-provider/provider/resources/wireless_network.go index 46c078bf..1f0c7c29 100644 --- a/pkg/config-api-provider/provider/resources/wireless_network.go +++ b/pkg/config-api-provider/provider/resources/wireless_network.go @@ -93,10 +93,7 @@ func (r *wirelessNetworkResource) Read(ctx context.Context, req resource.ReadReq GetUxiV1alpha1WirelessNetworksGet(ctx). Uid(state.ID.ValueString()) networkResponse, response, err := util.RetryFor429(request.Execute) - errorPresent, errorDetail := util.ResponseStatusCheck{ - Response: response, - Err: err, - }.RaiseForStatus() + errorPresent, errorDetail := util.RaiseForStatus(response, err) errorSummary := util.GenerateErrorSummary("read", "uxi_wireless_network") diff --git a/pkg/config-api-provider/provider/util/error_handling.go b/pkg/config-api-provider/provider/util/error_handling.go index 95841630..24743753 100644 --- a/pkg/config-api-provider/provider/util/error_handling.go +++ b/pkg/config-api-provider/provider/util/error_handling.go @@ -11,22 +11,17 @@ func GenerateErrorSummary(actionName string, entityName string) string { return fmt.Sprintf("Error performing %s on %s", actionName, entityName) } -type ResponseStatusCheck struct { - Response *http.Response - Err error -} - -func (r ResponseStatusCheck) RaiseForStatus() (bool, string) { - if r.Err != nil { +func RaiseForStatus(response *http.Response, err error) (bool, string) { + if err != nil { var detail string var data map[string]interface{} - switch e := r.Err.(type) { + switch e := err.(type) { case *url.Error: - detail = r.handleURLError(e) + detail = handleURLError(e) default: - if err := json.NewDecoder(r.Response.Body).Decode(&data); err != nil { - detail = "Unexpected error: " + r.Err.Error() + if jsonDecodeErr := json.NewDecoder(response.Body).Decode(&data); jsonDecodeErr != nil { + detail = "Unexpected error: " + jsonDecodeErr.Error() } else { detail = data["message"].(string) + "\nDebugID: " + data["debugId"].(string) } @@ -37,7 +32,7 @@ func (r ResponseStatusCheck) RaiseForStatus() (bool, string) { return false, "" } -func (r ResponseStatusCheck) handleURLError(uErr *url.Error) string { +func handleURLError(uErr *url.Error) string { if uErr.Timeout() { return "Error: Request timed out. Please check your network." } else if uErr.Temporary() { diff --git a/pkg/config-api-provider/test/data-sources/group_test.go b/pkg/config-api-provider/test/data-sources/group_test.go index ab0ac488..29b0d8f5 100644 --- a/pkg/config-api-provider/test/data-sources/group_test.go +++ b/pkg/config-api-provider/test/data-sources/group_test.go @@ -61,7 +61,7 @@ func TestGroupDataSource(t *testing.T) { } } `, - ExpectError: regexp.MustCompile(`the root group cannot be used as a data source`), + ExpectError: regexp.MustCompile(`The root group cannot be used as a data source`), }, }, }) From ccebe2b4de392fa21d04d275c82522f0cbe80c9f Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Thu, 10 Oct 2024 15:20:02 +0200 Subject: [PATCH 4/8] Add http error handling tests --- .../test/data-sources/group_test.go | 33 +++ .../network_group_assignment_test.go | 36 +++- .../sensor_group_assignment_test.go | 35 +++- ...ed_group_test.go => wired_network_test.go} | 34 +++ ...group_test.go => wireless_network_test.go} | 34 +++ .../test/resources/group_test.go | 79 +++++++ .../network_group_assignment_test.go | 185 ++++++++++++++++- .../resources/sensor_group_assignment_test.go | 195 ++++++++++++++++++ .../service_test_group_assignment_test.go | 82 +++++++- 9 files changed, 709 insertions(+), 4 deletions(-) rename pkg/config-api-provider/test/data-sources/{wired_group_test.go => wired_network_test.go} (75%) rename pkg/config-api-provider/test/data-sources/{wireless_group_test.go => wireless_network_test.go} (77%) diff --git a/pkg/config-api-provider/test/data-sources/group_test.go b/pkg/config-api-provider/test/data-sources/group_test.go index 29b0d8f5..2b1ca722 100644 --- a/pkg/config-api-provider/test/data-sources/group_test.go +++ b/pkg/config-api-provider/test/data-sources/group_test.go @@ -111,3 +111,36 @@ func TestGroupDataSource429Handling(t *testing.T) { mockOAuth.Mock.Disable() } + +func TestGroupDataSourceHttpErrorHandling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + PreConfig: func() { + gock.New("https://test.api.capenetworks.com"). + Get("/uxi/v1alpha1/groups"). + Reply(500). + JSON(map[string]interface{}{ + "httpStatusCode": 500, + "errorCode": "HPE_GL_ERROR_INTERNAL_SERVER_ERROR", + "message": "Current request cannot be processed due to unknown issue", + "debugId": "12312-123123-123123-1231212", + }) + }, + Config: provider.ProviderConfig + ` + data "uxi_group" "my_group" { + filter = { + group_id = "uid" + } + } + `, + ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/data-sources/network_group_assignment_test.go b/pkg/config-api-provider/test/data-sources/network_group_assignment_test.go index 39477bbc..e314a738 100644 --- a/pkg/config-api-provider/test/data-sources/network_group_assignment_test.go +++ b/pkg/config-api-provider/test/data-sources/network_group_assignment_test.go @@ -1,6 +1,7 @@ package data_source_test import ( + "regexp" "testing" "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" @@ -46,7 +47,7 @@ func TestNetworkGroupAssignmentDataSource(t *testing.T) { mockOAuth.Mock.Disable() } -func TestNetworkGroupAssignmentSource429Handling(t *testing.T) { +func TestNetworkGroupAssignmentDataSource429Handling(t *testing.T) { defer gock.Off() mockOAuth := util.MockOAuth() var mock429 *gock.Response @@ -87,3 +88,36 @@ func TestNetworkGroupAssignmentSource429Handling(t *testing.T) { mockOAuth.Mock.Disable() } + +func TestNetworkGroupAssignmentDataSourceHttpErrorHandling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + PreConfig: func() { + gock.New("https://test.api.capenetworks.com"). + Get("/uxi/v1alpha1/network-group-assignments"). + Reply(500). + JSON(map[string]interface{}{ + "httpStatusCode": 500, + "errorCode": "HPE_GL_ERROR_INTERNAL_SERVER_ERROR", + "message": "Current request cannot be processed due to unknown issue", + "debugId": "12312-123123-123123-1231212", + }) + }, + Config: provider.ProviderConfig + ` + data "uxi_network_group_assignment" "my_network_group_assignment" { + filter = { + network_group_assignment_id = "uid" + } + } + `, + ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go b/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go index 8e018d06..31b36ea2 100644 --- a/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go +++ b/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go @@ -1,6 +1,7 @@ package data_source_test import ( + "regexp" "testing" "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" @@ -46,7 +47,7 @@ func TestSensorGroupAssignmentDataSource(t *testing.T) { mockOAuth.Mock.Disable() } -func TestSensorGroupAssignmentSource429Handling(t *testing.T) { +func TestSensorGroupAssignmentDataSource429Handling(t *testing.T) { defer gock.Off() mockOAuth := util.MockOAuth() var mock429 *gock.Response @@ -87,3 +88,35 @@ func TestSensorGroupAssignmentSource429Handling(t *testing.T) { mockOAuth.Mock.Disable() } +func TestSensorGroupAssignmentDataSourceHttpErrorHandling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + PreConfig: func() { + gock.New("https://test.api.capenetworks.com"). + Get("/uxi/v1alpha1/sensor-group-assignments"). + Reply(500). + JSON(map[string]interface{}{ + "httpStatusCode": 500, + "errorCode": "HPE_GL_ERROR_INTERNAL_SERVER_ERROR", + "message": "Current request cannot be processed due to unknown issue", + "debugId": "12312-123123-123123-1231212", + }) + }, + Config: provider.ProviderConfig + ` + data "uxi_sensor_group_assignment" "my_sensor_group_assignment" { + filter = { + sensor_group_assignment_id = "uid" + } + } + `, + ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/data-sources/wired_group_test.go b/pkg/config-api-provider/test/data-sources/wired_network_test.go similarity index 75% rename from pkg/config-api-provider/test/data-sources/wired_group_test.go rename to pkg/config-api-provider/test/data-sources/wired_network_test.go index 8fffb9c4..09eb0f3f 100644 --- a/pkg/config-api-provider/test/data-sources/wired_group_test.go +++ b/pkg/config-api-provider/test/data-sources/wired_network_test.go @@ -1,6 +1,7 @@ package data_source_test import ( + "regexp" "testing" "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" @@ -94,3 +95,36 @@ func TestWiredNetworkDataSource429Handling(t *testing.T) { mockOAuth.Mock.Disable() } + +func TestWiredNetworkAssignmentDataSourceHttpErrorHandling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + PreConfig: func() { + gock.New("https://test.api.capenetworks.com"). + Get("/uxi/v1alpha1/wired-networks"). + Reply(500). + JSON(map[string]interface{}{ + "httpStatusCode": 500, + "errorCode": "HPE_GL_ERROR_INTERNAL_SERVER_ERROR", + "message": "Current request cannot be processed due to unknown issue", + "debugId": "12312-123123-123123-1231212", + }) + }, + Config: provider.ProviderConfig + ` + data "uxi_wired_network" "my_wired_network" { + filter = { + wired_network_id = "uid" + } + } + `, + ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/data-sources/wireless_group_test.go b/pkg/config-api-provider/test/data-sources/wireless_network_test.go similarity index 77% rename from pkg/config-api-provider/test/data-sources/wireless_group_test.go rename to pkg/config-api-provider/test/data-sources/wireless_network_test.go index 336edac7..d952d029 100644 --- a/pkg/config-api-provider/test/data-sources/wireless_group_test.go +++ b/pkg/config-api-provider/test/data-sources/wireless_network_test.go @@ -1,6 +1,7 @@ package data_source_test import ( + "regexp" "testing" "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" @@ -96,3 +97,36 @@ func TestWirelessNetworkDataSource429Handling(t *testing.T) { mockOAuth.Mock.Disable() } + +func TestWirelessNetworkAssignmentDataSourceHttpErrorHandling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + PreConfig: func() { + gock.New("https://test.api.capenetworks.com"). + Get("/uxi/v1alpha1/wireless-networks"). + Reply(500). + JSON(map[string]interface{}{ + "httpStatusCode": 500, + "errorCode": "HPE_GL_ERROR_INTERNAL_SERVER_ERROR", + "message": "Current request cannot be processed due to unknown issue", + "debugId": "12312-123123-123123-1231212", + }) + }, + Config: provider.ProviderConfig + ` + data "uxi_wireless_network" "my_wireless_network" { + filter = { + wireless_network_id = "uid" + } + } + `, + ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/resources/group_test.go b/pkg/config-api-provider/test/resources/group_test.go index be620f37..b54672dc 100644 --- a/pkg/config-api-provider/test/resources/group_test.go +++ b/pkg/config-api-provider/test/resources/group_test.go @@ -233,3 +233,82 @@ func TestGroupResource429Handling(t *testing.T) { mockOAuth.Mock.Disable() } + +func TestGroupResourceHttpErrorHandling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Create 4xx + { + PreConfig: func() { + gock.New("https://test.api.capenetworks.com"). + Post("/uxi/v1alpha1/groups"). + Reply(400). + JSON(map[string]interface{}{ + "httpStatusCode": 400, + "errorCode": "HPE_GL_ERROR_BAD_REQUEST", + "message": "Validation error - bad request", + "debugId": "12312-123123-123123-1231212", + }) + }, + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + }`, + ExpectError: regexp.MustCompile(`(?s)Validation error - bad request\s*DebugID: 12312-123123-123123-1231212`), + }, + // Create group in prep for next step + { + PreConfig: func() { + util.MockPostGroup(util.GenerateGroupResponseModel("uid", "", ""), 1) + util.MockGetGroup( + "uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("uid", "", "")}), + 1, + ) + }, + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_group.my_group", "id", "uid"), + ), + }, + // Update 4xx + { + PreConfig: func() { + // existing group + util.MockGetGroup("uid", util.GeneratePaginatedResponse( + []map[string]interface{}{util.GenerateGroupResponseModel("uid", "", "")}), + 1, + ) + // new group - with error + gock.New("https://test.api.capenetworks.com"). + Patch("/uxi/v1alpha1/groups/uid"). + Reply(422). + JSON(map[string]interface{}{ + "httpStatusCode": 422, + "errorCode": "HPE_GL_UXI_DUPLICATE_SIBLING_GROUP_NAME", + "message": "Unable to create group - a sibling group already has the specified name", + "debugId": "12312-123123-123123-1231212", + }) + }, + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name_2" + parent_group_id = "parent_uid" + }`, + ExpectError: regexp.MustCompile(`(?s)Unable to create group - a sibling group already has the specified name\s*DebugID: 12312-123123-123123-1231212`), + }, + // TODO: Test Deleting Error Handling + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/resources/network_group_assignment_test.go b/pkg/config-api-provider/test/resources/network_group_assignment_test.go index e881de84..9880c1fd 100644 --- a/pkg/config-api-provider/test/resources/network_group_assignment_test.go +++ b/pkg/config-api-provider/test/resources/network_group_assignment_test.go @@ -1,6 +1,7 @@ package resource_test import ( + "regexp" "testing" "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/test/provider" @@ -455,7 +456,7 @@ func TestNetworkGroupAssignmentResourceForWirelessNetwork(t *testing.T) { mockOAuth.Mock.Disable() } -func TestNetworkGroupAssignmentSource429Handling(t *testing.T) { +func TestNetworkGroupAssignmentResource429Handling(t *testing.T) { defer gock.Off() mockOAuth := util.MockOAuth() var mock429 *gock.Response @@ -567,3 +568,185 @@ func TestNetworkGroupAssignmentSource429Handling(t *testing.T) { mockOAuth.Mock.Disable() } + +func TestNetworkGroupAssignmentResourceHttpErrorHandling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Creating a network group assignment - errors + { + PreConfig: func() { + util.MockGetWiredNetwork( + "network_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid", "")}), + 2, + ) + + // required for group create + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup("group_uid", util.GeneratePaginatedResponse( + []map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}), + 2, + ) + + // network group assignment create + gock.New("https://test.api.capenetworks.com"). + Post("/uxi/v1alpha1/network-group-assignments"). + Reply(400). + JSON(map[string]interface{}{ + "httpStatusCode": 400, + "errorCode": "HPE_GL_ERROR_BAD_REQUEST", + "message": "Validation error - bad request", + "debugId": "12312-123123-123123-1231212", + }) + }, + + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_wired_network" "my_network" { + alias = "alias" + } + + import { + to = uxi_wired_network.my_network + id = "network_uid" + } + + resource "uxi_network_group_assignment" "my_network_group_assignment" { + network_id = uxi_wired_network.my_network.id + group_id = uxi_group.my_group.id + }`, + ExpectError: regexp.MustCompile(`(?s)Validation error - bad request\s*DebugID: 12312-123123-123123-1231212`), + }, + // Actually creating a network group assignment - for next step + { + PreConfig: func() { + util.MockGetWiredNetwork( + "network_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid", "")}), + 2, + ) + + // required for group create + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup("group_uid", util.GeneratePaginatedResponse( + []map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}), + 2, + ) + + // required for network group assignment create + util.MockPostNetworkGroupAssignment( + "network_group_assignment_uid", + util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", ""), + 1, + ) + util.MockGetNetworkGroupAssignment( + "network_group_assignment_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "")}), + 1, + ) + }, + + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_wired_network" "my_network" { + alias = "alias" + } + + import { + to = uxi_wired_network.my_network + id = "network_uid" + } + + resource "uxi_network_group_assignment" "my_network_group_assignment" { + network_id = uxi_wired_network.my_network.id + group_id = uxi_group.my_group.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_network_group_assignment.my_network_group_assignment", "network_id", "network_uid"), + ), + }, + // Delete network-group assignment and remove networks from state - errors + { + PreConfig: func() { + util.MockGetWiredNetwork( + "network_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid", "")}), + 1, + ) + util.MockGetGroup("group_uid", util.GeneratePaginatedResponse( + []map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}), + 1, + ) + util.MockGetNetworkGroupAssignment( + "network_group_assignment_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "")}), + 1, + ) + + // network group assignment create + gock.New("https://test.api.capenetworks.com"). + Delete("/uxi/v1alpha1/network-group-assignments"). + Reply(403). + JSON(map[string]interface{}{ + "httpStatusCode": 403, + "errorCode": "HPE_GL_ERROR_FORBIDDEN", + "message": "Forbidden - user has insufficient permissions to complete the request", + "debugId": "12312-123123-123123-1231212", + }) + + }, + Config: provider.ProviderConfig + ` + removed { + from = uxi_wired_network.my_network + + lifecycle { + destroy = false + } + }`, + ExpectError: regexp.MustCompile(`(?s)Forbidden - user has insufficient permissions to complete the request\s*DebugID: 12312-123123-123123-1231212`), + }, + // Actually delete network-group assignment and remove networks from state + { + PreConfig: func() { + util.MockGetWiredNetwork( + "network_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid", "")}), + 1, + ) + util.MockGetGroup("group_uid", util.GeneratePaginatedResponse( + []map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}), + 1, + ) + util.MockGetNetworkGroupAssignment( + "network_group_assignment_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateNetworkGroupAssignmentResponse("network_group_assignment_uid", "")}), + 1, + ) + util.MockDeleteNetworkGroupAssignment("network_group_assignment_uid", 1) + }, + Config: provider.ProviderConfig + ` + removed { + from = uxi_wired_network.my_network + + lifecycle { + destroy = false + } + }`, + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go b/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go index baa538c2..41ba88c3 100644 --- a/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go +++ b/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go @@ -1,6 +1,7 @@ package resource_test import ( + "regexp" "testing" "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/terraform-provider-configuration/provider/resources" @@ -343,3 +344,197 @@ func TestSensorGroupAssignmentResource429Handling(t *testing.T) { mockOAuth.Mock.Disable() } + +func TestSensorGroupAssignmentResourceHttpErrorHandling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Creating a sensor group assignment - errors + { + PreConfig: func() { + // required for sensor import + resources.GetSensor = func(uid string) resources.SensorResponseModel { + return util.GenerateSensorResponseModel(uid, "") + } + + // required for group create + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup( + "group_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}), + 1, + ) + + // required for sensor group assignment create + gock.New("https://test.api.capenetworks.com"). + Post("/uxi/v1alpha1/sensor-group-assignments"). + Reply(400). + JSON(map[string]interface{}{ + "httpStatusCode": 400, + "errorCode": "HPE_GL_ERROR_BAD_REQUEST", + "message": "Validation error - bad request", + "debugId": "12312-123123-123123-1231212", + }) + }, + + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_sensor" "my_sensor" { + name = "name" + address_note = "address_note" + notes = "notes" + pcap_mode = "light" + } + + import { + to = uxi_sensor.my_sensor + id = "sensor_uid" + } + + resource "uxi_sensor_group_assignment" "my_sensor_group_assignment" { + sensor_id = uxi_sensor.my_sensor.id + group_id = uxi_group.my_group.id + }`, + ExpectError: regexp.MustCompile(`(?s)Validation error - bad request\s*DebugID: 12312-123123-123123-1231212`), + }, + // Actually Creating a sensor group assignment - needed for next step + { + PreConfig: func() { + // required for sensor import + resources.GetSensor = func(uid string) resources.SensorResponseModel { + return util.GenerateSensorResponseModel(uid, "") + } + + // required for group create + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup( + "group_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}), + 1, + ) + + // required for sensor group assignment create + util.MockPostSensorGroupAssignment( + "sensor_group_assignment_uid", + util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", ""), + 1, + ) + util.MockGetSensorGroupAssignment( + "sensor_group_assignment_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")}), + 1, + ) + }, + + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_sensor" "my_sensor" { + name = "name" + address_note = "address_note" + notes = "notes" + pcap_mode = "light" + } + + import { + to = uxi_sensor.my_sensor + id = "sensor_uid" + } + + resource "uxi_sensor_group_assignment" "my_sensor_group_assignment" { + sensor_id = uxi_sensor.my_sensor.id + group_id = uxi_group.my_group.id + }`, + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("uxi_sensor_group_assignment.my_sensor_group_assignment", "id", "sensor_group_assignment_uid"), + ), + }, + // Delete sensor-group assignments and remove sensors from state - errors + { + PreConfig: func() { + util.MockGetGroup( + "group_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}), + 1, + ) + util.MockGetSensorGroupAssignment( + "sensor_group_assignment_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")}), + 1, + ) + + gock.New("https://test.api.capenetworks.com"). + Delete("/uxi/v1alpha1/sensor-group-assignments/sensor_group_assignment_uid"). + Reply(400). + JSON(map[string]interface{}{ + "httpStatusCode": 400, + "errorCode": "HPE_GL_ERROR_BAD_REQUEST", + "message": "Validation error - bad request", + "debugId": "12312-123123-123123-1231212", + }) + }, + Config: provider.ProviderConfig + ` + removed { + from = uxi_sensor.my_sensor + + lifecycle { + destroy = false + } + } + + removed { + from = uxi_sensor.my_sensor_2 + + lifecycle { + destroy = false + } + }`, + ExpectError: regexp.MustCompile(`(?s)Validation error - bad request\s*DebugID: 12312-123123-123123-1231212`), + }, + // Actually Delete sensor-group assignments and remove sensors from state + { + PreConfig: func() { + util.MockGetGroup( + "group_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}), + 1, + ) + util.MockGetSensorGroupAssignment( + "sensor_group_assignment_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateSensorGroupAssignmentResponse("sensor_group_assignment_uid", "")}), + 1, + ) + util.MockDeleteSensorGroupAssignment("sensor_group_assignment_uid", 1) + }, + Config: provider.ProviderConfig + ` + removed { + from = uxi_sensor.my_sensor + + lifecycle { + destroy = false + } + } + + removed { + from = uxi_sensor.my_sensor_2 + + lifecycle { + destroy = false + } + }`, + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/resources/service_test_group_assignment_test.go b/pkg/config-api-provider/test/resources/service_test_group_assignment_test.go index 40c5d252..f1ac4179 100644 --- a/pkg/config-api-provider/test/resources/service_test_group_assignment_test.go +++ b/pkg/config-api-provider/test/resources/service_test_group_assignment_test.go @@ -1,6 +1,7 @@ package resource_test import ( + "regexp" "testing" config_api_client "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client" @@ -282,7 +283,86 @@ func TestServiceTestGroupAssignmentResource429Handling(t *testing.T) { } }`, }, - // Delete testing automatically occurs in TestCase + }, + }) + + mockOAuth.Mock.Disable() +} + +func TestServiceTestGroupAssignmentResourceHttpErrorHandling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + // Creating a serviceTest group assignment - errors + { + PreConfig: func() { + // required for serviceTest import + resources.GetServiceTest = func(uid string) resources.ServiceTestResponseModel { + return util.GenerateServiceTestResponseModel(uid, "") + } + + // required for group create + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup( + "group_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}), + 1, + ) + + // required for serviceTest group assignment create + gock.New("https://test.api.capenetworks.com"). + Post("/uxi/v1alpha1/service-test-group-assignments"). + Reply(400). + JSON(map[string]interface{}{ + "httpStatusCode": 400, + "errorCode": "HPE_GL_ERROR_BAD_REQUEST", + "message": "Validation error - bad request", + "debugId": "12312-123123-123123-1231212", + }) + }, + + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_service_test" "my_service_test" { + title = "title" + } + + import { + to = uxi_service_test.my_service_test + id = "service_test_uid" + } + + resource "uxi_service_test_group_assignment" "my_service_test_group_assignment" { + service_test_id = uxi_service_test.my_service_test.id + group_id = uxi_group.my_group.id + }`, + ExpectError: regexp.MustCompile(`(?s)Validation error - bad request\s*DebugID: 12312-123123-123123-1231212`), + }, + // Remove serviceTests from state + { + PreConfig: func() { + util.MockGetGroup( + "group_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}), + 1, + ) + }, + Config: provider.ProviderConfig + ` + removed { + from = uxi_service_test.my_service_test + + lifecycle { + destroy = false + } + }`, + }, }, }) From 648cde32291006989112ec16eb9868e8ca27f58e Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Thu, 10 Oct 2024 15:48:50 +0200 Subject: [PATCH 5/8] Error handling for get resources --- .../provider/data-sources/group.go | 2 +- .../data-sources/network_group_assignment.go | 2 +- .../data-sources/sensor_group_assignment.go | 2 +- .../provider/data-sources/wired_network.go | 2 +- .../provider/data-sources/wireless_network.go | 2 +- .../test/data-sources/group_test.go | 19 +++ .../network_group_assignment_test.go | 17 +++ .../sensor_group_assignment_test.go | 17 +++ .../test/data-sources/wired_network_test.go | 17 +++ .../data-sources/wireless_network_test.go | 17 +++ .../test/resources/group_test.go | 49 ++++++++ .../network_group_assignment_test.go | 105 +++++++++++++++++ .../resources/sensor_group_assignment_test.go | 111 ++++++++++++++++++ .../test/resources/wired_network_test.go | 59 ++++++++++ .../test/resources/wireless_network_test.go | 59 ++++++++++ 15 files changed, 475 insertions(+), 5 deletions(-) diff --git a/pkg/config-api-provider/provider/data-sources/group.go b/pkg/config-api-provider/provider/data-sources/group.go index 6eecc854..8f5f1934 100644 --- a/pkg/config-api-provider/provider/data-sources/group.go +++ b/pkg/config-api-provider/provider/data-sources/group.go @@ -89,7 +89,7 @@ func (d *groupDataSource) Read(ctx context.Context, req datasource.ReadRequest, } if len(groupResponse.Items) != 1 { - resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") + resp.Diagnostics.AddError(errorSummary, "Could not find specified data source") return } diff --git a/pkg/config-api-provider/provider/data-sources/network_group_assignment.go b/pkg/config-api-provider/provider/data-sources/network_group_assignment.go index 51586b31..6ed6022f 100644 --- a/pkg/config-api-provider/provider/data-sources/network_group_assignment.go +++ b/pkg/config-api-provider/provider/data-sources/network_group_assignment.go @@ -83,7 +83,7 @@ func (d *networkGroupAssignmentDataSource) Read(ctx context.Context, req datasou } if len(networkGroupAssignmentResponse.Items) != 1 { - resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") + resp.Diagnostics.AddError(errorSummary, "Could not find specified data source") return } diff --git a/pkg/config-api-provider/provider/data-sources/sensor_group_assignment.go b/pkg/config-api-provider/provider/data-sources/sensor_group_assignment.go index a6ef935d..09592464 100644 --- a/pkg/config-api-provider/provider/data-sources/sensor_group_assignment.go +++ b/pkg/config-api-provider/provider/data-sources/sensor_group_assignment.go @@ -85,7 +85,7 @@ func (d *sensorGroupAssignmentDataSource) Read(ctx context.Context, req datasour } if len(sensorGroupAssignmentResponse.Items) != 1 { - resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") + resp.Diagnostics.AddError(errorSummary, "Could not find specified data source") return } diff --git a/pkg/config-api-provider/provider/data-sources/wired_network.go b/pkg/config-api-provider/provider/data-sources/wired_network.go index 9a813c4c..dbd13d18 100644 --- a/pkg/config-api-provider/provider/data-sources/wired_network.go +++ b/pkg/config-api-provider/provider/data-sources/wired_network.go @@ -110,7 +110,7 @@ func (d *wiredNetworkDataSource) Read(ctx context.Context, req datasource.ReadRe } if len(networkResponse.Items) != 1 { - resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") + resp.Diagnostics.AddError(errorSummary, "Could not find specified data source") return } diff --git a/pkg/config-api-provider/provider/data-sources/wireless_network.go b/pkg/config-api-provider/provider/data-sources/wireless_network.go index 3ef471a8..60cb6a01 100644 --- a/pkg/config-api-provider/provider/data-sources/wireless_network.go +++ b/pkg/config-api-provider/provider/data-sources/wireless_network.go @@ -118,7 +118,7 @@ func (d *wirelessNetworkDataSource) Read(ctx context.Context, req datasource.Rea } if len(networkResponse.Items) != 1 { - resp.Diagnostics.AddError(errorSummary, "Could not find specified resource") + resp.Diagnostics.AddError(errorSummary, "Could not find specified data source") return } diff --git a/pkg/config-api-provider/test/data-sources/group_test.go b/pkg/config-api-provider/test/data-sources/group_test.go index 2b1ca722..40692ea7 100644 --- a/pkg/config-api-provider/test/data-sources/group_test.go +++ b/pkg/config-api-provider/test/data-sources/group_test.go @@ -118,6 +118,7 @@ func TestGroupDataSourceHttpErrorHandling(t *testing.T) { resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ + // 5xx error { PreConfig: func() { gock.New("https://test.api.capenetworks.com"). @@ -139,6 +140,24 @@ func TestGroupDataSourceHttpErrorHandling(t *testing.T) { `, ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), }, + // Not found error + { + PreConfig: func() { + util.MockGetGroup( + "uid", + util.GeneratePaginatedResponse([]map[string]interface{}{}), + 1, + ) + }, + Config: provider.ProviderConfig + ` + data "uxi_group" "my_group" { + filter = { + group_id = "uid" + } + } + `, + ExpectError: regexp.MustCompile(`Could not find specified data source`), + }, }, }) diff --git a/pkg/config-api-provider/test/data-sources/network_group_assignment_test.go b/pkg/config-api-provider/test/data-sources/network_group_assignment_test.go index e314a738..7cfc4e25 100644 --- a/pkg/config-api-provider/test/data-sources/network_group_assignment_test.go +++ b/pkg/config-api-provider/test/data-sources/network_group_assignment_test.go @@ -116,6 +116,23 @@ func TestNetworkGroupAssignmentDataSourceHttpErrorHandling(t *testing.T) { `, ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), }, + { + PreConfig: func() { + util.MockGetNetworkGroupAssignment( + "uid", + util.GeneratePaginatedResponse([]map[string]interface{}{}), + 1, + ) + }, + Config: provider.ProviderConfig + ` + data "uxi_network_group_assignment" "my_network_group_assignment" { + filter = { + network_group_assignment_id = "uid" + } + } + `, + ExpectError: regexp.MustCompile(`Could not find specified data source`), + }, }, }) diff --git a/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go b/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go index 31b36ea2..04bde552 100644 --- a/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go +++ b/pkg/config-api-provider/test/data-sources/sensor_group_assignment_test.go @@ -115,6 +115,23 @@ func TestSensorGroupAssignmentDataSourceHttpErrorHandling(t *testing.T) { `, ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), }, + { + PreConfig: func() { + util.MockGetSensorGroupAssignment( + "uid", + util.GeneratePaginatedResponse([]map[string]interface{}{}), + 1, + ) + }, + Config: provider.ProviderConfig + ` + data "uxi_sensor_group_assignment" "my_sensor_group_assignment" { + filter = { + sensor_group_assignment_id = "uid" + } + } + `, + ExpectError: regexp.MustCompile(`Could not find specified data source`), + }, }, }) diff --git a/pkg/config-api-provider/test/data-sources/wired_network_test.go b/pkg/config-api-provider/test/data-sources/wired_network_test.go index 09eb0f3f..ea80fb65 100644 --- a/pkg/config-api-provider/test/data-sources/wired_network_test.go +++ b/pkg/config-api-provider/test/data-sources/wired_network_test.go @@ -123,6 +123,23 @@ func TestWiredNetworkAssignmentDataSourceHttpErrorHandling(t *testing.T) { `, ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), }, + { + PreConfig: func() { + util.MockGetWiredNetwork( + "uid", + util.GeneratePaginatedResponse([]map[string]interface{}{}), + 1, + ) + }, + Config: provider.ProviderConfig + ` + data "uxi_wired_network" "my_wired_network" { + filter = { + wired_network_id = "uid" + } + } + `, + ExpectError: regexp.MustCompile(`Could not find specified data source`), + }, }, }) diff --git a/pkg/config-api-provider/test/data-sources/wireless_network_test.go b/pkg/config-api-provider/test/data-sources/wireless_network_test.go index d952d029..b1e98de2 100644 --- a/pkg/config-api-provider/test/data-sources/wireless_network_test.go +++ b/pkg/config-api-provider/test/data-sources/wireless_network_test.go @@ -125,6 +125,23 @@ func TestWirelessNetworkAssignmentDataSourceHttpErrorHandling(t *testing.T) { `, ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), }, + { + PreConfig: func() { + util.MockGetWirelessNetwork( + "uid", + util.GeneratePaginatedResponse([]map[string]interface{}{}), + 1, + ) + }, + Config: provider.ProviderConfig + ` + data "uxi_wireless_network" "my_wireless_network" { + filter = { + wireless_network_id = "uid" + } + } + `, + ExpectError: regexp.MustCompile(`Could not find specified data source`), + }, }, }) diff --git a/pkg/config-api-provider/test/resources/group_test.go b/pkg/config-api-provider/test/resources/group_test.go index b54672dc..f61b71e4 100644 --- a/pkg/config-api-provider/test/resources/group_test.go +++ b/pkg/config-api-provider/test/resources/group_test.go @@ -241,6 +241,55 @@ func TestGroupResourceHttpErrorHandling(t *testing.T) { resource.Test(t, resource.TestCase{ ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, Steps: []resource.TestStep{ + // read 5xx error + { + PreConfig: func() { + gock.New("https://test.api.capenetworks.com"). + Get("/uxi/v1alpha1/groups"). + Reply(500). + JSON(map[string]interface{}{ + "httpStatusCode": 500, + "errorCode": "HPE_GL_ERROR_INTERNAL_SERVER_ERROR", + "message": "Current request cannot be processed due to unknown issue", + "debugId": "12312-123123-123123-1231212", + }) + }, + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + import { + to = uxi_group.my_group + id = "uid" + } + `, + ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), + }, + // Read not found + { + PreConfig: func() { + util.MockGetGroup( + "uid", + util.GeneratePaginatedResponse([]map[string]interface{}{}), + 1, + ) + }, + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + import { + to = uxi_group.my_group + id = "uid" + } + `, + + ExpectError: regexp.MustCompile(`Could not find specified resource`), + }, // Create 4xx { PreConfig: func() { diff --git a/pkg/config-api-provider/test/resources/network_group_assignment_test.go b/pkg/config-api-provider/test/resources/network_group_assignment_test.go index 9880c1fd..dca10e55 100644 --- a/pkg/config-api-provider/test/resources/network_group_assignment_test.go +++ b/pkg/config-api-provider/test/resources/network_group_assignment_test.go @@ -625,6 +625,111 @@ func TestNetworkGroupAssignmentResourceHttpErrorHandling(t *testing.T) { }`, ExpectError: regexp.MustCompile(`(?s)Validation error - bad request\s*DebugID: 12312-123123-123123-1231212`), }, + // read not found error + { + PreConfig: func() { + util.MockGetWiredNetwork( + "network_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid", "")}), + 2, + ) + + // required for group create + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup("group_uid", util.GeneratePaginatedResponse( + []map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}), + 2, + ) + + // network group assignment read + util.MockGetNetworkGroupAssignment( + "network_group_assignment_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{}), + 1, + ) + }, + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_wired_network" "my_network" { + alias = "alias" + } + + import { + to = uxi_wired_network.my_network + id = "network_uid" + } + + resource "uxi_network_group_assignment" "my_network_group_assignment" { + network_id = uxi_wired_network.my_network.id + group_id = uxi_group.my_group.id + } + + import { + to = uxi_network_group_assignment.my_network_group_assignment + id = "network_group_assignment_uid" + } + `, + ExpectError: regexp.MustCompile(`Could not find specified resource`), + }, + // Read 5xx error + { + PreConfig: func() { + util.MockGetWiredNetwork( + "network_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateWiredNetworkResponse("network_uid", "")}), + 2, + ) + + // required for group create + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup("group_uid", util.GeneratePaginatedResponse( + []map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}), + 2, + ) + + // network group assignment read + gock.New("https://test.api.capenetworks.com"). + Get("/uxi/v1alpha1/network-group-assignments"). + Reply(500). + JSON(map[string]interface{}{ + "httpStatusCode": 500, + "errorCode": "HPE_GL_ERROR_INTERNAL_SERVER_ERROR", + "message": "Current request cannot be processed due to unknown issue", + "debugId": "12312-123123-123123-1231212", + }) + }, + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_wired_network" "my_network" { + alias = "alias" + } + + import { + to = uxi_wired_network.my_network + id = "network_uid" + } + + resource "uxi_network_group_assignment" "my_network_group_assignment" { + network_id = uxi_wired_network.my_network.id + group_id = uxi_group.my_group.id + } + + import { + to = uxi_network_group_assignment.my_network_group_assignment + id = "network_group_assignment_uid" + } + `, + + ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), + }, // Actually creating a network group assignment - for next step { PreConfig: func() { diff --git a/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go b/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go index 41ba88c3..663b6af7 100644 --- a/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go +++ b/pkg/config-api-provider/test/resources/sensor_group_assignment_test.go @@ -404,6 +404,117 @@ func TestSensorGroupAssignmentResourceHttpErrorHandling(t *testing.T) { }`, ExpectError: regexp.MustCompile(`(?s)Validation error - bad request\s*DebugID: 12312-123123-123123-1231212`), }, + // read not found error + { + PreConfig: func() { + // required for sensor import + resources.GetSensor = func(uid string) resources.SensorResponseModel { + return util.GenerateSensorResponseModel(uid, "") + } + + // required for group create + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup( + "group_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}), + 1, + ) + + // sensor group assignment create + util.MockGetSensorGroupAssignment( + "sensor_group_assignment_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{}), + 1, + ) + }, + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_sensor" "my_sensor" { + name = "name" + address_note = "address_note" + notes = "notes" + pcap_mode = "light" + } + + import { + to = uxi_sensor.my_sensor + id = "sensor_uid" + } + + resource "uxi_sensor_group_assignment" "my_sensor_group_assignment" { + sensor_id = uxi_sensor.my_sensor.id + group_id = uxi_group.my_group.id + } + + import { + to = uxi_sensor_group_assignment.my_sensor_group_assignment + id = "sensor_group_assignment_uid" + } + `, + ExpectError: regexp.MustCompile(`Could not find specified resource`), + }, + // Read 5xx error + { + PreConfig: func() { + // required for sensor import + resources.GetSensor = func(uid string) resources.SensorResponseModel { + return util.GenerateSensorResponseModel(uid, "") + } + + // required for group create + util.MockPostGroup(util.StructToMap(util.GenerateGroupResponseModel("group_uid", "", "")), 1) + util.MockGetGroup( + "group_uid", + util.GeneratePaginatedResponse([]map[string]interface{}{util.GenerateGroupResponseModel("group_uid", "", "")}), + 1, + ) + + // required for sensor group assignment read + gock.New("https://test.api.capenetworks.com"). + Get("/uxi/v1alpha1/sensor-group-assignments"). + Reply(500). + JSON(map[string]interface{}{ + "httpStatusCode": 500, + "errorCode": "HPE_GL_ERROR_INTERNAL_SERVER_ERROR", + "message": "Current request cannot be processed due to unknown issue", + "debugId": "12312-123123-123123-1231212", + }) + }, + Config: provider.ProviderConfig + ` + resource "uxi_group" "my_group" { + name = "name" + parent_group_id = "parent_uid" + } + + resource "uxi_sensor" "my_sensor" { + name = "name" + address_note = "address_note" + notes = "notes" + pcap_mode = "light" + } + + import { + to = uxi_sensor.my_sensor + id = "sensor_uid" + } + + resource "uxi_sensor_group_assignment" "my_sensor_group_assignment" { + sensor_id = uxi_sensor.my_sensor.id + group_id = uxi_group.my_group.id + } + + import { + to = uxi_sensor_group_assignment.my_sensor_group_assignment + id = "sensor_group_assignment_uid" + } + `, + + ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), + }, // Actually Creating a sensor group assignment - needed for next step { PreConfig: func() { diff --git a/pkg/config-api-provider/test/resources/wired_network_test.go b/pkg/config-api-provider/test/resources/wired_network_test.go index 12fcffbd..33388d2b 100644 --- a/pkg/config-api-provider/test/resources/wired_network_test.go +++ b/pkg/config-api-provider/test/resources/wired_network_test.go @@ -111,3 +111,62 @@ func TestWiredNetworkResource(t *testing.T) { mockOAuth.Mock.Disable() } + +func TestWiredNetworkResourceHttpErrorHandling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + // we required terraform 1.7.0 and above for the `removed` block + tfversion.RequireAbove(tfversion.Version1_7_0), + }, + Steps: []resource.TestStep{ + { + PreConfig: func() { + util.MockGetWiredNetwork( + "uid", + util.GeneratePaginatedResponse([]map[string]interface{}{}), + 1, + ) + }, + Config: provider.ProviderConfig + ` + resource "uxi_wired_network" "my_wired_network" { + alias = "alias" + } + + import { + to = uxi_wired_network.my_wired_network + id = "uid" + }`, + ExpectError: regexp.MustCompile(`Could not find specified resource`), + }, + { + PreConfig: func() { + gock.New("https://test.api.capenetworks.com"). + Get("/uxi/v1alpha1/wired-networks"). + Reply(500). + JSON(map[string]interface{}{ + "httpStatusCode": 500, + "errorCode": "HPE_GL_ERROR_INTERNAL_SERVER_ERROR", + "message": "Current request cannot be processed due to unknown issue", + "debugId": "12312-123123-123123-1231212", + }) + }, + Config: provider.ProviderConfig + ` + resource "uxi_wired_network" "my_wired_network" { + alias = "alias" + } + + import { + to = uxi_wired_network.my_wired_network + id = "uid" + }`, + ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), + }, + }, + }) + + mockOAuth.Mock.Disable() +} diff --git a/pkg/config-api-provider/test/resources/wireless_network_test.go b/pkg/config-api-provider/test/resources/wireless_network_test.go index d5481f32..5cc34862 100644 --- a/pkg/config-api-provider/test/resources/wireless_network_test.go +++ b/pkg/config-api-provider/test/resources/wireless_network_test.go @@ -118,3 +118,62 @@ func TestWirelessNetworkResource(t *testing.T) { mockOAuth.Mock.Disable() } + +func TestWirelessNetworkResourceHttpErrorHandling(t *testing.T) { + defer gock.Off() + mockOAuth := util.MockOAuth() + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + // we required terraform 1.7.0 and above for the `removed` block + tfversion.RequireAbove(tfversion.Version1_7_0), + }, + Steps: []resource.TestStep{ + { + PreConfig: func() { + util.MockGetWirelessNetwork( + "uid", + util.GeneratePaginatedResponse([]map[string]interface{}{}), + 1, + ) + }, + Config: provider.ProviderConfig + ` + resource "uxi_wireless_network" "my_wireless_network" { + alias = "alias" + } + + import { + to = uxi_wireless_network.my_wireless_network + id = "uid" + }`, + ExpectError: regexp.MustCompile(`Could not find specified resource`), + }, + { + PreConfig: func() { + gock.New("https://test.api.capenetworks.com"). + Get("/uxi/v1alpha1/wireless-networks"). + Reply(500). + JSON(map[string]interface{}{ + "httpStatusCode": 500, + "errorCode": "HPE_GL_ERROR_INTERNAL_SERVER_ERROR", + "message": "Current request cannot be processed due to unknown issue", + "debugId": "12312-123123-123123-1231212", + }) + }, + Config: provider.ProviderConfig + ` + resource "uxi_wireless_network" "my_wireless_network" { + alias = "alias" + } + + import { + to = uxi_wireless_network.my_wireless_network + id = "uid" + }`, + ExpectError: regexp.MustCompile(`(?s)Current request cannot be processed due to unknown issue\s*DebugID: 12312-123123-123123-1231212`), + }, + }, + }) + + mockOAuth.Mock.Disable() +} From f3de22df25583ab9bd332eb4707a13f03c4aed91 Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Thu, 10 Oct 2024 17:20:17 +0200 Subject: [PATCH 6/8] better error handling even --- .../provider/util/error_handling.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/pkg/config-api-provider/provider/util/error_handling.go b/pkg/config-api-provider/provider/util/error_handling.go index 24743753..6d155c97 100644 --- a/pkg/config-api-provider/provider/util/error_handling.go +++ b/pkg/config-api-provider/provider/util/error_handling.go @@ -5,6 +5,8 @@ import ( "fmt" "net/http" "net/url" + + config_api_client "github.com/aruba-uxi/configuration-api-terraform-provider/pkg/config-api-client" ) func GenerateErrorSummary(actionName string, entityName string) string { @@ -19,12 +21,19 @@ func RaiseForStatus(response *http.Response, err error) (bool, string) { switch e := err.(type) { case *url.Error: detail = handleURLError(e) - default: + case *config_api_client.GenericOpenAPIError: if jsonDecodeErr := json.NewDecoder(response.Body).Decode(&data); jsonDecodeErr != nil { detail = "Unexpected error: " + jsonDecodeErr.Error() + } else if message, ok := data["message"]; ok { + detail = message.(string) + if debugId, ok := data["debugId"]; ok { + detail += "\nDebugID: " + debugId.(string) + } } else { - detail = data["message"].(string) + "\nDebugID: " + data["debugId"].(string) + detail = "Unexpected error: " + e.Error() } + default: + detail = "Unexpected error: " + e.Error() } return true, detail From bcd8225f6c516a0296a54e8991b8424ea0550b69 Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Thu, 10 Oct 2024 17:20:53 +0200 Subject: [PATCH 7/8] fix: rename alias and title to name --- .../provider/data-sources/wired_network.go | 6 ++--- .../provider/data-sources/wireless_network.go | 6 ++--- .../provider/resources/service.go | 12 +++++----- .../provider/resources/wired_network.go | 8 +++---- .../provider/resources/wireless_network.go | 8 +++---- .../test/data-sources/wired_network_test.go | 2 +- .../data-sources/wireless_network_test.go | 2 +- .../network_group_assignment_test.go | 22 +++++++++---------- .../test/resources/service_test.go | 8 +++---- .../service_test_group_assignment_test.go | 10 ++++----- .../test/resources/wired_network_test.go | 12 +++++----- .../test/resources/wireless_network_test.go | 12 +++++----- pkg/config-api-provider/test/util/utils.go | 8 +++---- 13 files changed, 58 insertions(+), 58 deletions(-) diff --git a/pkg/config-api-provider/provider/data-sources/wired_network.go b/pkg/config-api-provider/provider/data-sources/wired_network.go index dbd13d18..f283cd27 100644 --- a/pkg/config-api-provider/provider/data-sources/wired_network.go +++ b/pkg/config-api-provider/provider/data-sources/wired_network.go @@ -26,7 +26,7 @@ type wiredNetworkDataSource struct { type wiredNetworkDataSourceModel struct { ID types.String `tfsdk:"id"` - Alias types.String `tfsdk:"alias"` + Name types.String `tfsdk:"name"` IpVersion types.String `tfsdk:"ip_version"` Security types.String `tfsdk:"security"` DnsLookupDomain types.String `tfsdk:"dns_lookup_domain"` @@ -50,7 +50,7 @@ func (d *wiredNetworkDataSource) Schema(_ context.Context, _ datasource.SchemaRe "id": schema.StringAttribute{ Computed: true, }, - "alias": schema.StringAttribute{ + "name": schema.StringAttribute{ Computed: true, }, "ip_version": schema.StringAttribute{ @@ -116,7 +116,7 @@ func (d *wiredNetworkDataSource) Read(ctx context.Context, req datasource.ReadRe network := networkResponse.Items[0] state.ID = types.StringValue(network.Id) - state.Alias = types.StringValue(network.Name) + state.Name = types.StringValue(network.Name) state.IpVersion = types.StringValue(network.IpVersion) state.Security = types.StringValue(*network.Security.Get()) state.DnsLookupDomain = types.StringValue(*network.DnsLookupDomain.Get()) diff --git a/pkg/config-api-provider/provider/data-sources/wireless_network.go b/pkg/config-api-provider/provider/data-sources/wireless_network.go index 60cb6a01..61972da1 100644 --- a/pkg/config-api-provider/provider/data-sources/wireless_network.go +++ b/pkg/config-api-provider/provider/data-sources/wireless_network.go @@ -27,7 +27,7 @@ type wirelessNetworkDataSource struct { type wirelessNetworkDataSourceModel struct { ID types.String `tfsdk:"id"` Ssid types.String `tfsdk:"ssid"` - Alias types.String `tfsdk:"alias"` + Name types.String `tfsdk:"name"` IpVersion types.String `tfsdk:"ip_version"` Security types.String `tfsdk:"security"` Hidden types.Bool `tfsdk:"hidden"` @@ -55,7 +55,7 @@ func (d *wirelessNetworkDataSource) Schema(_ context.Context, _ datasource.Schem "ssid": schema.StringAttribute{ Computed: true, }, - "alias": schema.StringAttribute{ + "name": schema.StringAttribute{ Computed: true, }, "ip_version": schema.StringAttribute{ @@ -125,7 +125,7 @@ func (d *wirelessNetworkDataSource) Read(ctx context.Context, req datasource.Rea network := networkResponse.Items[0] state.ID = types.StringValue(network.Id) state.Ssid = types.StringValue(network.Ssid) - state.Alias = types.StringValue(network.Name) + state.Name = types.StringValue(network.Name) state.IpVersion = types.StringValue(network.IpVersion) state.Security = types.StringValue(*network.Security.Get()) state.Hidden = types.BoolValue(network.Hidden) diff --git a/pkg/config-api-provider/provider/resources/service.go b/pkg/config-api-provider/provider/resources/service.go index 288e634c..e3990cdf 100644 --- a/pkg/config-api-provider/provider/resources/service.go +++ b/pkg/config-api-provider/provider/resources/service.go @@ -19,15 +19,15 @@ var ( ) type serviceTestResourceModel struct { - ID types.String `tfsdk:"id"` - Title types.String `tfsdk:"title"` + ID types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` } // TODO: Switch this to use the Client Model when that becomes available type ServiceTestResponseModel struct { Uid string // , Category string // , - Title string // , + Name string // , Target string // Nullable<>, Template string // , IsEnabled bool // @@ -52,7 +52,7 @@ func (r *serviceTestResource) Schema(_ context.Context, _ resource.SchemaRequest stringplanmodifier.UseStateForUnknown(), }, }, - "title": schema.StringAttribute{ + "name": schema.StringAttribute{ Required: true, }, }, @@ -82,7 +82,7 @@ func (r *serviceTestResource) Read(ctx context.Context, req resource.ReadRequest response := GetServiceTest(state.ID.ValueString()) // Update state from client response - state.Title = types.StringValue(response.Title) + state.Name = types.StringValue(response.Name) // Set refreshed state diags = resp.State.Set(ctx, &state) @@ -119,7 +119,7 @@ var GetServiceTest = func(uid string) ServiceTestResponseModel { return ServiceTestResponseModel{ Uid: uid, Category: "category", - Title: "title", + Name: "name", Target: "target", Template: "template", IsEnabled: true, diff --git a/pkg/config-api-provider/provider/resources/wired_network.go b/pkg/config-api-provider/provider/resources/wired_network.go index e902a93c..b9eee1ec 100644 --- a/pkg/config-api-provider/provider/resources/wired_network.go +++ b/pkg/config-api-provider/provider/resources/wired_network.go @@ -20,8 +20,8 @@ var ( ) type wiredNetworkResourceModel struct { - ID types.String `tfsdk:"id"` - Alias types.String `tfsdk:"alias"` + ID types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` } func NewWiredNetworkResource() resource.Resource { @@ -45,7 +45,7 @@ func (r *wiredNetworkResource) Schema(_ context.Context, _ resource.SchemaReques stringplanmodifier.UseStateForUnknown(), }, }, - "alias": schema.StringAttribute{ + "name": schema.StringAttribute{ Required: true, }, }, @@ -110,7 +110,7 @@ func (r *wiredNetworkResource) Read(ctx context.Context, req resource.ReadReques network := networkResponse.Items[0] // Update state from client response - state.Alias = types.StringValue(network.Name) + state.Name = types.StringValue(network.Name) // Set refreshed state diags = resp.State.Set(ctx, &state) diff --git a/pkg/config-api-provider/provider/resources/wireless_network.go b/pkg/config-api-provider/provider/resources/wireless_network.go index 1f0c7c29..ad029364 100644 --- a/pkg/config-api-provider/provider/resources/wireless_network.go +++ b/pkg/config-api-provider/provider/resources/wireless_network.go @@ -20,8 +20,8 @@ var ( ) type wirelessNetworkResourceModel struct { - ID types.String `tfsdk:"id"` - Alias types.String `tfsdk:"alias"` + ID types.String `tfsdk:"id"` + Name types.String `tfsdk:"name"` } func NewWirelessNetworkResource() resource.Resource { @@ -45,7 +45,7 @@ func (r *wirelessNetworkResource) Schema(_ context.Context, _ resource.SchemaReq stringplanmodifier.UseStateForUnknown(), }, }, - "alias": schema.StringAttribute{ + "name": schema.StringAttribute{ Required: true, }, }, @@ -110,7 +110,7 @@ func (r *wirelessNetworkResource) Read(ctx context.Context, req resource.ReadReq network := networkResponse.Items[0] // Update state from client response - state.Alias = types.StringValue(network.Name) + state.Name = types.StringValue(network.Name) // Set refreshed state diags = resp.State.Set(ctx, &state) diff --git a/pkg/config-api-provider/test/data-sources/wired_network_test.go b/pkg/config-api-provider/test/data-sources/wired_network_test.go index ea80fb65..aec5d96c 100644 --- a/pkg/config-api-provider/test/data-sources/wired_network_test.go +++ b/pkg/config-api-provider/test/data-sources/wired_network_test.go @@ -38,7 +38,7 @@ func TestWiredNetworkDataSource(t *testing.T) { `, Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "id", "uid"), - resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "alias", "alias"), + resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "name", "name"), resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "ip_version", "ip_version"), resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "security", "security"), resource.TestCheckResourceAttr("data.uxi_wired_network.my_wired_network", "dns_lookup_domain", "dns_lookup_domain"), diff --git a/pkg/config-api-provider/test/data-sources/wireless_network_test.go b/pkg/config-api-provider/test/data-sources/wireless_network_test.go index b1e98de2..b0f3a651 100644 --- a/pkg/config-api-provider/test/data-sources/wireless_network_test.go +++ b/pkg/config-api-provider/test/data-sources/wireless_network_test.go @@ -39,7 +39,7 @@ func TestWirelessNetworkDataSource(t *testing.T) { Check: resource.ComposeAggregateTestCheckFunc( resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "id", "uid"), resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "ssid", "ssid"), - resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "alias", "alias"), + resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "name", "name"), resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "ip_version", "ip_version"), resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "security", "security"), resource.TestCheckResourceAttr("data.uxi_wireless_network.my_wireless_network", "hidden", "false"), diff --git a/pkg/config-api-provider/test/resources/network_group_assignment_test.go b/pkg/config-api-provider/test/resources/network_group_assignment_test.go index dca10e55..adecd50f 100644 --- a/pkg/config-api-provider/test/resources/network_group_assignment_test.go +++ b/pkg/config-api-provider/test/resources/network_group_assignment_test.go @@ -56,7 +56,7 @@ func TestNetworkGroupAssignmentResourceForWiredNetwork(t *testing.T) { } resource "uxi_wired_network" "my_network" { - alias = "alias" + name = "name" } import { @@ -140,7 +140,7 @@ func TestNetworkGroupAssignmentResourceForWiredNetwork(t *testing.T) { } resource "uxi_wired_network" "my_network" { - alias = "alias" + name = "name" } import { @@ -155,7 +155,7 @@ func TestNetworkGroupAssignmentResourceForWiredNetwork(t *testing.T) { } resource "uxi_wired_network" "my_network_2" { - alias = "alias_2" + name = "name_2" } import { @@ -277,7 +277,7 @@ func TestNetworkGroupAssignmentResourceForWirelessNetwork(t *testing.T) { } resource "uxi_wireless_network" "my_network" { - alias = "alias" + name = "name" } import { @@ -362,7 +362,7 @@ func TestNetworkGroupAssignmentResourceForWirelessNetwork(t *testing.T) { } resource "uxi_wireless_network" "my_network" { - alias = "alias" + name = "name" } import { @@ -377,7 +377,7 @@ func TestNetworkGroupAssignmentResourceForWirelessNetwork(t *testing.T) { } resource "uxi_wireless_network" "my_network_2" { - alias = "alias_2" + name = "name_2" } import { @@ -504,7 +504,7 @@ func TestNetworkGroupAssignmentResource429Handling(t *testing.T) { } resource "uxi_wired_network" "my_network" { - alias = "alias" + name = "name" } import { @@ -611,7 +611,7 @@ func TestNetworkGroupAssignmentResourceHttpErrorHandling(t *testing.T) { } resource "uxi_wired_network" "my_network" { - alias = "alias" + name = "name" } import { @@ -655,7 +655,7 @@ func TestNetworkGroupAssignmentResourceHttpErrorHandling(t *testing.T) { } resource "uxi_wired_network" "my_network" { - alias = "alias" + name = "name" } import { @@ -709,7 +709,7 @@ func TestNetworkGroupAssignmentResourceHttpErrorHandling(t *testing.T) { } resource "uxi_wired_network" "my_network" { - alias = "alias" + name = "name" } import { @@ -766,7 +766,7 @@ func TestNetworkGroupAssignmentResourceHttpErrorHandling(t *testing.T) { } resource "uxi_wired_network" "my_network" { - alias = "alias" + name = "name" } import { diff --git a/pkg/config-api-provider/test/resources/service_test.go b/pkg/config-api-provider/test/resources/service_test.go index 4e08c199..28f286cd 100644 --- a/pkg/config-api-provider/test/resources/service_test.go +++ b/pkg/config-api-provider/test/resources/service_test.go @@ -27,7 +27,7 @@ func TestServiceTestResource(t *testing.T) { { Config: provider.ProviderConfig + ` resource "uxi_service_test" "my_service_test" { - title = "title" + name = "name" }`, ExpectError: regexp.MustCompile(`(?s)creating a service_test is not supported; service_tests can only be\s*imported`), @@ -41,7 +41,7 @@ func TestServiceTestResource(t *testing.T) { }, Config: provider.ProviderConfig + ` resource "uxi_service_test" "my_service_test" { - title = "title" + name = "name" } import { @@ -50,7 +50,7 @@ func TestServiceTestResource(t *testing.T) { }`, Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("uxi_service_test.my_service_test", "title", "title"), + resource.TestCheckResourceAttr("uxi_service_test.my_service_test", "name", "name"), resource.TestCheckResourceAttr("uxi_service_test.my_service_test", "id", "uid"), ), }, @@ -64,7 +64,7 @@ func TestServiceTestResource(t *testing.T) { { Config: provider.ProviderConfig + ` resource "uxi_service_test" "my_service_test" { - title = "updated_title" + name = "updated_name" }`, ExpectError: regexp.MustCompile(`(?s)updating a service_test is not supported; service_tests can only be updated\s*through the dashboard`), }, diff --git a/pkg/config-api-provider/test/resources/service_test_group_assignment_test.go b/pkg/config-api-provider/test/resources/service_test_group_assignment_test.go index f1ac4179..8e8f6703 100644 --- a/pkg/config-api-provider/test/resources/service_test_group_assignment_test.go +++ b/pkg/config-api-provider/test/resources/service_test_group_assignment_test.go @@ -56,7 +56,7 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) { } resource "uxi_service_test" "my_service_test" { - title = "title" + name = "name" } import { @@ -126,7 +126,7 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) { } resource "uxi_service_test" "my_service_test" { - title = "title" + name = "name" } import { @@ -141,7 +141,7 @@ func TestServiceTestGroupAssignmentResource(t *testing.T) { } resource "uxi_service_test" "my_service_test_2" { - title = "title_2" + name = "name_2" } import { @@ -245,7 +245,7 @@ func TestServiceTestGroupAssignmentResource429Handling(t *testing.T) { } resource "uxi_service_test" "my_service_test" { - title = "title" + name = "name" } import { @@ -331,7 +331,7 @@ func TestServiceTestGroupAssignmentResourceHttpErrorHandling(t *testing.T) { } resource "uxi_service_test" "my_service_test" { - title = "title" + name = "name" } import { diff --git a/pkg/config-api-provider/test/resources/wired_network_test.go b/pkg/config-api-provider/test/resources/wired_network_test.go index 33388d2b..0ea1c5bc 100644 --- a/pkg/config-api-provider/test/resources/wired_network_test.go +++ b/pkg/config-api-provider/test/resources/wired_network_test.go @@ -26,7 +26,7 @@ func TestWiredNetworkResource(t *testing.T) { { Config: provider.ProviderConfig + ` resource "uxi_wired_network" "my_wired_network" { - alias = "alias" + name = "name" }`, ExpectError: regexp.MustCompile(`(?s)creating a wired_network is not supported; wired_networks can only be\s*imported`), @@ -42,7 +42,7 @@ func TestWiredNetworkResource(t *testing.T) { }, Config: provider.ProviderConfig + ` resource "uxi_wired_network" "my_wired_network" { - alias = "alias" + name = "name" } import { @@ -51,7 +51,7 @@ func TestWiredNetworkResource(t *testing.T) { }`, Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("uxi_wired_network.my_wired_network", "alias", "alias"), + resource.TestCheckResourceAttr("uxi_wired_network.my_wired_network", "name", "name"), resource.TestCheckResourceAttr("uxi_wired_network.my_wired_network", "id", "uid"), ), }, @@ -79,7 +79,7 @@ func TestWiredNetworkResource(t *testing.T) { }, Config: provider.ProviderConfig + ` resource "uxi_wired_network" "my_wired_network" { - alias = "updated_alias" + name = "updated_name" }`, ExpectError: regexp.MustCompile(`(?s)updating a wired_network is not supported; wired_networks can only be updated\s*through the dashboard`), }, @@ -133,7 +133,7 @@ func TestWiredNetworkResourceHttpErrorHandling(t *testing.T) { }, Config: provider.ProviderConfig + ` resource "uxi_wired_network" "my_wired_network" { - alias = "alias" + name = "name" } import { @@ -156,7 +156,7 @@ func TestWiredNetworkResourceHttpErrorHandling(t *testing.T) { }, Config: provider.ProviderConfig + ` resource "uxi_wired_network" "my_wired_network" { - alias = "alias" + name = "name" } import { diff --git a/pkg/config-api-provider/test/resources/wireless_network_test.go b/pkg/config-api-provider/test/resources/wireless_network_test.go index 5cc34862..486550f0 100644 --- a/pkg/config-api-provider/test/resources/wireless_network_test.go +++ b/pkg/config-api-provider/test/resources/wireless_network_test.go @@ -26,7 +26,7 @@ func TestWirelessNetworkResource(t *testing.T) { { Config: provider.ProviderConfig + ` resource "uxi_wireless_network" "my_wireless_network" { - alias = "alias" + name = "name" }`, ExpectError: regexp.MustCompile(`(?s)creating a wireless_network is not supported; wireless_networks can only be\s*imported`), @@ -42,7 +42,7 @@ func TestWirelessNetworkResource(t *testing.T) { }, Config: provider.ProviderConfig + ` resource "uxi_wireless_network" "my_wireless_network" { - alias = "alias" + name = "name" } import { @@ -51,7 +51,7 @@ func TestWirelessNetworkResource(t *testing.T) { }`, Check: resource.ComposeAggregateTestCheckFunc( - resource.TestCheckResourceAttr("uxi_wireless_network.my_wireless_network", "alias", "alias"), + resource.TestCheckResourceAttr("uxi_wireless_network.my_wireless_network", "name", "name"), resource.TestCheckResourceAttr("uxi_wireless_network.my_wireless_network", "id", "uid"), ), }, @@ -79,7 +79,7 @@ func TestWirelessNetworkResource(t *testing.T) { }, Config: provider.ProviderConfig + ` resource "uxi_wireless_network" "my_wireless_network" { - alias = "updated_alias" + name = "updated_name" }`, ExpectError: regexp.MustCompile(`(?s)updating a wireless_network is not supported; wireless_networks can only be\s*updated through the dashboard`), }, @@ -140,7 +140,7 @@ func TestWirelessNetworkResourceHttpErrorHandling(t *testing.T) { }, Config: provider.ProviderConfig + ` resource "uxi_wireless_network" "my_wireless_network" { - alias = "alias" + name = "name" } import { @@ -163,7 +163,7 @@ func TestWirelessNetworkResourceHttpErrorHandling(t *testing.T) { }, Config: provider.ProviderConfig + ` resource "uxi_wireless_network" "my_wireless_network" { - alias = "alias" + name = "name" } import { diff --git a/pkg/config-api-provider/test/util/utils.go b/pkg/config-api-provider/test/util/utils.go index ce5294d5..112d0d83 100644 --- a/pkg/config-api-provider/test/util/utils.go +++ b/pkg/config-api-provider/test/util/utils.go @@ -61,7 +61,7 @@ func GenerateServiceTestResponseModel(uid string, postfix string) resources.Serv return resources.ServiceTestResponseModel{ Uid: uid, Category: "external" + postfix, - Title: "title" + postfix, + Name: "name" + postfix, Target: "target" + postfix, Template: "template" + postfix, IsEnabled: true, @@ -71,7 +71,7 @@ func GenerateServiceTestResponseModel(uid string, postfix string) resources.Serv func GenerateWiredNetworkResponse(uid string, postfix string) map[string]interface{} { return map[string]interface{}{ "id": uid, - "name": "alias" + postfix, + "name": "name" + postfix, "createdAt": "2024-09-11T12:00:00.000Z", "updatedAt": "2024-09-11T12:00:00.000Z", "ipVersion": "ip_version" + postfix, @@ -91,7 +91,7 @@ func GenerateWirelessNetworkResponse(uid string, postfix string) map[string]inte "ssid": "ssid" + postfix, "createdAt": "2024-09-11T12:00:00.000Z", "updatedAt": "2024-09-11T12:00:00.000Z", - "name": "alias" + postfix, + "name": "name" + postfix, "ipVersion": "ip_version" + postfix, "security": "security" + postfix, "hidden": false, @@ -140,7 +140,7 @@ func GenerateServiceTestGroupAssignmentResponse(uid string, postfix string) conf } } -// Converts a struct to a map while maintaining the json alias as keys +// Converts a struct to a map while maintaining the json name as keys func StructToMap(obj interface{}) map[string]interface{} { data, _ := json.Marshal(obj) // Convert to a json string From bd329827aae95545df87b1e0f7f1e05975c88009 Mon Sep 17 00:00:00 2001 From: 1riatsila1 Date: Thu, 10 Oct 2024 17:22:48 +0200 Subject: [PATCH 8/8] also update the example --- pkg/config-api-provider/examples/full-demo/main.tf | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/config-api-provider/examples/full-demo/main.tf b/pkg/config-api-provider/examples/full-demo/main.tf index 3a74a0e2..565da15c 100644 --- a/pkg/config-api-provider/examples/full-demo/main.tf +++ b/pkg/config-api-provider/examples/full-demo/main.tf @@ -82,7 +82,7 @@ removed { } */ resource "uxi_wireless_network" "my_wireless_network" { - alias = "alias" + name = "name" } import { @@ -102,7 +102,7 @@ removed { } */ resource "uxi_wired_network" "my_wired_network" { - alias = "alias" + name = "name" } import { @@ -121,7 +121,7 @@ removed { } */ resource "uxi_service_test" "my_service_test" { - title = "title" + name = "name" } import {