From 1ad1a89c81486cfcc20819313dcf26ed66379b74 Mon Sep 17 00:00:00 2001 From: Martin Helmich Date: Wed, 11 Sep 2024 13:49:26 +0200 Subject: [PATCH] Correctly handle diagnostics from building app installation inputs --- .../provider/resource/appresource/model_api.go | 17 +++++++++++------ .../provider/resource/appresource/resource.go | 2 +- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/internal/provider/resource/appresource/model_api.go b/internal/provider/resource/appresource/model_api.go index 9393519..3cacfb8 100644 --- a/internal/provider/resource/appresource/model_api.go +++ b/internal/provider/resource/appresource/model_api.go @@ -7,16 +7,17 @@ import ( "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types/basetypes" + "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/mittwald/terraform-provider-mittwald/api/mittwaldv2" "github.com/mittwald/terraform-provider-mittwald/internal/provider/providerutil" "github.com/mittwald/terraform-provider-mittwald/internal/valueutil" ) -func (m *ResourceModel) ToCreateRequestWithUpdaters(ctx context.Context, d diag.Diagnostics, appClient mittwaldv2.AppClient) (mittwaldv2.AppRequestAppinstallationJSONRequestBody, []mittwaldv2.AppInstallationUpdater) { +func (m *ResourceModel) ToCreateRequestWithUpdaters(ctx context.Context, d *diag.Diagnostics, appClient mittwaldv2.AppClient) (mittwaldv2.AppRequestAppinstallationJSONRequestBody, []mittwaldv2.AppInstallationUpdater) { return m.ToCreateRequest(ctx, d, appClient), m.ToCreateUpdaters(ctx, d, appClient) } -func (m *ResourceModel) ToCreateUpdaters(ctx context.Context, d diag.Diagnostics, appClient mittwaldv2.AppClient) []mittwaldv2.AppInstallationUpdater { +func (m *ResourceModel) ToCreateUpdaters(ctx context.Context, d *diag.Diagnostics, appClient mittwaldv2.AppClient) []mittwaldv2.AppInstallationUpdater { updaters := make([]mittwaldv2.AppInstallationUpdater, 0) if !m.DocumentRoot.IsNull() { @@ -29,7 +30,7 @@ func (m *ResourceModel) ToCreateUpdaters(ctx context.Context, d diag.Diagnostics if !m.Dependencies.IsNull() { depUpdater := providerutil. - Try[mittwaldv2.AppInstallationUpdater](&d, "error while building dependency updaters"). + Try[mittwaldv2.AppInstallationUpdater](d, "error while building dependency updaters"). DoVal(m.dependenciesToUpdater(ctx, appClient, nil)) updaters = append(updaters, depUpdater) } @@ -37,18 +38,22 @@ func (m *ResourceModel) ToCreateUpdaters(ctx context.Context, d diag.Diagnostics return updaters } -func (m *ResourceModel) ToCreateRequest(ctx context.Context, d diag.Diagnostics, appClient mittwaldv2.AppClient) (b mittwaldv2.AppRequestAppinstallationJSONRequestBody) { +func (m *ResourceModel) ToCreateRequest(ctx context.Context, d *diag.Diagnostics, appClient mittwaldv2.AppClient) (b mittwaldv2.AppRequestAppinstallationJSONRequestBody) { + tflog.Debug(ctx, "building create request for app", map[string]any{"app": m.App.ValueString()}) + appID, ok := appNames[m.App.ValueString()] if !ok { - d.AddError("app", "App not found") + d.AddAttributeError(path.Root("app"), "invalid value", fmt.Sprintf("app %s not found", m.App.ValueString())) return } b.Description = m.Description.ValueString() b.UpdatePolicy = mittwaldv2.DeMittwaldV1AppAppUpdatePolicy(m.UpdatePolicy.ValueString()) + tflog.Debug(ctx, "create body", map[string]any{"request": b}) + appVersions := providerutil. - Try[[]mittwaldv2.DeMittwaldV1AppAppVersion](&d, "error while listing app versions"). + Try[[]mittwaldv2.DeMittwaldV1AppAppVersion](d, "error while listing app versions"). DoVal(appClient.ListAppVersions(ctx, appID)) for _, appVersion := range appVersions { diff --git a/internal/provider/resource/appresource/resource.go b/internal/provider/resource/appresource/resource.go index bb89ac4..fffac73 100644 --- a/internal/provider/resource/appresource/resource.go +++ b/internal/provider/resource/appresource/resource.go @@ -204,7 +204,7 @@ func (r *Resource) Create(ctx context.Context, req resource.CreateRequest, resp } appClient := r.client.App() - appInput, appUpdaters := data.ToCreateRequestWithUpdaters(ctx, resp.Diagnostics, appClient) + appInput, appUpdaters := data.ToCreateRequestWithUpdaters(ctx, &resp.Diagnostics, appClient) if resp.Diagnostics.HasError() { return