Skip to content

Commit

Permalink
feat: neatening up error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Oct 10, 2024
1 parent 04331b1 commit 9148ced
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 106 deletions.
56 changes: 30 additions & 26 deletions pkg/config-api-provider/provider/resources/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}
Expand Down Expand Up @@ -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)

Check warning on line 102 in pkg/config-api-provider/provider/resources/group.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/group.go#L102

Added line #L102 was not covered by tests
return
}

Expand Down Expand Up @@ -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

Check warning on line 141 in pkg/config-api-provider/provider/resources/group.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/group.go#L140-L141

Added lines #L140 - L141 were not covered by tests
}

if len(groupResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified resource")

Check warning on line 145 in pkg/config-api-provider/provider/resources/group.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/group.go#L145

Added line #L145 was not covered by tests
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
}

Expand Down Expand Up @@ -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)

Check warning on line 188 in pkg/config-api-provider/provider/resources/group.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/group.go#L188

Added line #L188 was not covered by tests
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,6 @@ type networkGroupAssignmentResourceModel struct {
GroupID types.String `tfsdk:"group_id"`
}

type NetworkGroupAssignmentResponseModel struct {
UID string // <assignment_uid>
GroupUID string // <group_uid:str>,
NetworkUID string // <network_uid:str>
}

func NewNetworkGroupAssignmentResource() resource.Resource {
return &networkGroupAssignmentResource{}
}
Expand Down Expand Up @@ -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)

Check warning on line 106 in pkg/config-api-provider/provider/resources/network_group_assignment.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/network_group_assignment.go#L106

Added line #L106 was not covered by tests
return
}

Expand Down Expand Up @@ -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

Check warning on line 145 in pkg/config-api-provider/provider/resources/network_group_assignment.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/network_group_assignment.go#L144-L145

Added lines #L144 - L145 were not covered by tests
}

if len(networkGroupAssignmentResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified resource")

Check warning on line 149 in pkg/config-api-provider/provider/resources/network_group_assignment.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/network_group_assignment.go#L149

Added line #L149 was not covered by tests
return
}
networkGroupAssignment := networkGroupAssignmentResponse.Items[0]
Expand Down Expand Up @@ -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)

Check warning on line 195 in pkg/config-api-provider/provider/resources/network_group_assignment.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/network_group_assignment.go#L195

Added line #L195 was not covered by tests
return
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 // <group_uid:str>,
SensorUID string // <sensor_uid:str>
Expand Down Expand Up @@ -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)

Check warning on line 109 in pkg/config-api-provider/provider/resources/sensor_group_assignment.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/sensor_group_assignment.go#L109

Added line #L109 was not covered by tests
return
}

Expand All @@ -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

Check warning on line 146 in pkg/config-api-provider/provider/resources/sensor_group_assignment.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/sensor_group_assignment.go#L145-L146

Added lines #L145 - L146 were not covered by tests
}

if len(sensorGroupAssignmentResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified resource")

Check warning on line 150 in pkg/config-api-provider/provider/resources/sensor_group_assignment.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/sensor_group_assignment.go#L150

Added line #L150 was not covered by tests
return
}

Expand Down Expand Up @@ -184,27 +188,18 @@ 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)

Check warning on line 198 in pkg/config-api-provider/provider/resources/sensor_group_assignment.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/sensor_group_assignment.go#L198

Added line #L198 was not covered by tests
return
}
}

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",
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 106 in pkg/config-api-provider/provider/resources/service_group_assignment.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/service_group_assignment.go#L106

Added line #L106 was not covered by tests
return
}

Expand Down
20 changes: 14 additions & 6 deletions pkg/config-api-provider/provider/resources/wired_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 105 in pkg/config-api-provider/provider/resources/wired_network.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/wired_network.go#L104-L105

Added lines #L104 - L105 were not covered by tests
}

if len(networkResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified resource")

Check warning on line 109 in pkg/config-api-provider/provider/resources/wired_network.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/wired_network.go#L109

Added line #L109 was not covered by tests
return
}

Expand Down
20 changes: 14 additions & 6 deletions pkg/config-api-provider/provider/resources/wireless_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 105 in pkg/config-api-provider/provider/resources/wireless_network.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/wireless_network.go#L104-L105

Added lines #L104 - L105 were not covered by tests
}

if len(networkResponse.Items) != 1 {
resp.Diagnostics.AddError(errorSummary, "Could not find specified resource")

Check warning on line 109 in pkg/config-api-provider/provider/resources/wireless_network.go

View check run for this annotation

Codecov / codecov/patch

pkg/config-api-provider/provider/resources/wireless_network.go#L109

Added line #L109 was not covered by tests
return
}

Expand Down
Loading

0 comments on commit 9148ced

Please sign in to comment.