Skip to content

Commit

Permalink
Merge pull request #77 from mittwald/bugfix/app-create-errors
Browse files Browse the repository at this point in the history
Correctly handle diagnostics from building app installation inputs
  • Loading branch information
martin-helmich authored Sep 11, 2024
2 parents 56a7640 + 1ad1a89 commit d0f58c5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions internal/provider/resource/appresource/model_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -29,26 +30,30 @@ 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)
}

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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource/appresource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d0f58c5

Please sign in to comment.