Skip to content

Commit

Permalink
refactor: handle all errors through diagnostics (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
alrayyes authored Dec 30, 2024
1 parent 5815bc5 commit 0dc8676
Show file tree
Hide file tree
Showing 15 changed files with 295 additions and 286 deletions.
64 changes: 42 additions & 22 deletions internal/provider/publiccloud/image_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
Expand Down Expand Up @@ -33,23 +34,26 @@ type imageResourceModel struct {
func adaptImageDetailsToImageResource(
ctx context.Context,
imageDetails publiccloud.ImageDetails,
) (*imageResourceModel, error) {
marketApps, diags := basetypes.NewListValueFrom(
diags *diag.Diagnostics,
) *imageResourceModel {
marketApps, marketAppsDiags := basetypes.NewListValueFrom(
ctx,
basetypes.StringType{},
imageDetails.MarketApps,
)
if diags.HasError() {
return nil, fmt.Errorf(diags.Errors()[0].Summary(), diags.Errors()[0].Detail())
if marketAppsDiags.HasError() {
diags.Append(marketAppsDiags...)
return nil
}

storageTypes, diags := basetypes.NewListValueFrom(
storageTypes, storageTypesDiags := basetypes.NewListValueFrom(
ctx,
basetypes.StringType{},
imageDetails.StorageTypes,
)
if diags.HasError() {
return nil, fmt.Errorf(diags.Errors()[0].Summary(), diags.Errors()[0].Detail())
if storageTypesDiags.HasError() {
diags.Append(storageTypesDiags...)
return nil
}

image := imageResourceModel{
Expand All @@ -63,7 +67,7 @@ func adaptImageDetailsToImageResource(
Region: basetypes.NewStringValue(string(imageDetails.GetRegion())),
}

return &image, nil
return &image
}

type imageResource struct {
Expand Down Expand Up @@ -145,16 +149,24 @@ func (i *imageResource) Create(
}

imageDetails, httpResponse, err := i.PubliccloudAPI.CreateImage(ctx).
CreateImageOpts(*publiccloud.NewCreateImageOpts(plan.Name.ValueString(), plan.InstanceID.ValueString())).
CreateImageOpts(
*publiccloud.NewCreateImageOpts(
plan.Name.ValueString(),
plan.InstanceID.ValueString(),
),
).
Execute()
if err != nil {
utils.SdkError(ctx, &response.Diagnostics, err, httpResponse)
return
}

state, err := adaptImageDetailsToImageResource(ctx, *imageDetails)
if err != nil {
utils.GeneralError(&response.Diagnostics, ctx, err)
state := adaptImageDetailsToImageResource(
ctx,
*imageDetails,
&response.Diagnostics,
)
if response.Diagnostics.HasError() {
return
}
// instanceId has to be set manually as it isn't returned from the API
Expand All @@ -174,24 +186,29 @@ func (i *imageResource) Read(
return
}

images, httpResponse, err := getAllImages(ctx, i.PubliccloudAPI)
if err != nil {
utils.SdkError(ctx, &response.Diagnostics, err, httpResponse)
images := getAllImages(ctx, i.PubliccloudAPI, &response.Diagnostics)
if response.Diagnostics.HasError() {
return
}
imageDetails := images.findById(currentState.ID.ValueString())
if imageDetails == nil {
utils.GeneralError(
&response.Diagnostics,
ctx,
fmt.Errorf("imageDetails %s not found", currentState.ID.ValueString()),
fmt.Errorf(
"imageDetails %s not found",
currentState.ID.ValueString(),
),
)
return
}

state, err := adaptImageDetailsToImageResource(ctx, *imageDetails)
if err != nil {
utils.GeneralError(&response.Diagnostics, ctx, err)
state := adaptImageDetailsToImageResource(
ctx,
*imageDetails,
&response.Diagnostics,
)
if response.Diagnostics.HasError() {
return
}
// instanceId has to be set manually as it isn't returned from the API
Expand Down Expand Up @@ -221,9 +238,12 @@ func (i *imageResource) Update(
return
}

state, err := adaptImageDetailsToImageResource(ctx, *imageDetails)
if err != nil {
utils.GeneralError(&response.Diagnostics, ctx, err)
state := adaptImageDetailsToImageResource(
ctx,
*imageDetails,
&response.Diagnostics,
)
if response.Diagnostics.HasError() {
return
}

Expand Down
16 changes: 10 additions & 6 deletions internal/provider/publiccloud/image_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/leaseweb/leaseweb-go-sdk/v3/publiccloud"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_adaptImageDetailsToImageResource(t *testing.T) {
Expand Down Expand Up @@ -37,6 +37,14 @@ func Test_adaptImageDetailsToImageResource(t *testing.T) {
[]string{"CENTRAL"},
)

diags := diag.Diagnostics{}

got := adaptImageDetailsToImageResource(
context.TODO(),
sdkImageDetails,
&diags,
)

want := imageResourceModel{
ID: basetypes.NewStringValue("imageId"),
Name: basetypes.NewStringValue("name"),
Expand All @@ -47,11 +55,7 @@ func Test_adaptImageDetailsToImageResource(t *testing.T) {
Flavour: basetypes.NewStringValue("flavour"),
Region: basetypes.NewStringValue("eu-west-3"),
}
got, err := adaptImageDetailsToImageResource(
context.TODO(),
sdkImageDetails,
)

require.NoError(t, err)
assert.False(t, diags.HasError())
assert.Equal(t, want, *got)
}
16 changes: 8 additions & 8 deletions internal/provider/publiccloud/images_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package publiccloud

import (
"context"
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/leaseweb/leaseweb-go-sdk/v3/publiccloud"
Expand Down Expand Up @@ -71,7 +70,8 @@ func adaptImageDetailsToImageDataSource(imageDetails publiccloud.ImageDetails) i
func getAllImages(
ctx context.Context,
api publiccloud.PubliccloudAPI,
) (imageDetailsList, *http.Response, error) {
diags *diag.Diagnostics,
) imageDetailsList {
var images imageDetailsList
var offset *int32

Expand All @@ -80,7 +80,8 @@ func getAllImages(
for {
result, httpResponse, err := request.Execute()
if err != nil {
return nil, httpResponse, fmt.Errorf("getAllImages: %w", err)
utils.SdkError(ctx, diags, err, httpResponse)
return nil
}

images = append(images, result.GetImages()...)
Expand All @@ -100,7 +101,7 @@ func getAllImages(
request = request.Offset(*offset)
}

return images, nil, nil
return images
}

func imageSchemaAttributes() map[string]schema.Attribute {
Expand Down Expand Up @@ -164,9 +165,8 @@ func (i *imagesDataSource) Read(
_ datasource.ReadRequest,
response *datasource.ReadResponse,
) {
images, httpResponse, err := getAllImages(ctx, i.PubliccloudAPI)
if err != nil {
utils.SdkError(ctx, &response.Diagnostics, err, httpResponse)
images := getAllImages(ctx, i.PubliccloudAPI, &response.Diagnostics)
if response.Diagnostics.HasError() {
return
}

Expand Down
Loading

0 comments on commit 0dc8676

Please sign in to comment.