Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: neatening up error handling #41

Merged
merged 7 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions pkg/config-api-provider/provider/data-sources/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 data source")
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
}

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

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

Expand Down
17 changes: 11 additions & 6 deletions pkg/config-api-provider/provider/data-sources/wired_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 data source")
return
}

Expand Down
17 changes: 11 additions & 6 deletions pkg/config-api-provider/provider/data-sources/wireless_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 data source")
return
}

Expand Down
47 changes: 21 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 @@ -102,12 +95,11 @@ func (r *groupResource) Create(ctx context.Context, req resource.CreateRequest,
groups_post_request.SetParentId(plan.ParentGroupId.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.RaiseForStatus(response, err)

if errorPresent {
resp.Diagnostics.AddError(util.GenerateErrorSummary("create", "uxi_group"), errorDetail)
return
}

Expand Down Expand Up @@ -136,20 +128,24 @@ 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.RaiseForStatus(response, err)

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
}

Expand Down Expand Up @@ -178,13 +174,12 @@ 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.RaiseForStatus(response, err)

if errorPresent {
resp.Diagnostics.AddError(util.GenerateErrorSummary("update", "uxi_group"), errorDetail)
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,11 @@ 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.RaiseForStatus(response, err)

if errorPresent {
resp.Diagnostics.AddError(util.GenerateErrorSummary("create", "uxi_network_group_assignment"), errorDetail)
return
}

Expand Down Expand Up @@ -136,13 +129,18 @@ 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.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
}
networkGroupAssignment := networkGroupAssignmentResponse.Items[0]
Expand Down Expand Up @@ -181,13 +179,11 @@ func (r *networkGroupAssignmentResource) Delete(ctx context.Context, req resourc

request := r.client.ConfigurationAPI.
DeleteNetworkGroupAssignmentUxiV1alpha1NetworkGroupAssignmentsIdDelete(ctx, state.ID.ValueString())
_, _, err := util.RetryFor429(request.Execute)
_, response, err := util.RetryFor429(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

if err != nil {
resp.Diagnostics.AddError(
"Error deleting Network Group Assignment",
"Could not delete Network Group Assignment, unexpected error: "+err.Error(),
)
if errorPresent {
resp.Diagnostics.AddError(util.GenerateErrorSummary("delete", "uxi_network_group_assignment"), errorDetail)
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,11 @@ 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.RaiseForStatus(response, err)

if errorPresent {
resp.Diagnostics.AddError(util.GenerateErrorSummary("create", "uxi_sensor_group_assignment"), errorDetail)
return
}

Expand All @@ -137,13 +130,18 @@ 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.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
}

Expand Down Expand Up @@ -184,27 +182,15 @@ 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)
_, response, err := util.RetryFor429(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

if err != nil {
resp.Diagnostics.AddError(
"Error deleting Sensor Group Assignment",
"Could not deleting Sensor Group Assignment, unexpected error: "+err.Error(),
)
if errorPresent {
resp.Diagnostics.AddError(util.GenerateErrorSummary("delete", "uxi_sensor_group_assignment"), errorDetail)
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,11 @@ func (r *serviceTestGroupAssignmentResource) Create(ctx context.Context, req res
request := r.client.ConfigurationAPI.
PostUxiV1alpha1ServiceTestGroupAssignmentsPost(ctx).
ServiceTestGroupAssignmentsPostRequest(*postRequest)
serviceTestGroupAssignment, _, err := util.RetryFor429(request.Execute)
serviceTestGroupAssignment, response, err := util.RetryFor429(request.Execute)
errorPresent, errorDetail := util.RaiseForStatus(response, err)

if err != nil {
resp.Diagnostics.AddError(
"Error creating Service Test Group Assignment",
"Could not create Network Group Assignment, unexpected error: "+err.Error(),
)
if errorPresent {
resp.Diagnostics.AddError(util.GenerateErrorSummary("delete", "uxi_service_test_group_assignment"), errorDetail)
return
}

Expand Down
Loading
Loading