From 4c172ef5fea0e0efdc5b1cc76f4df9c49f753835 Mon Sep 17 00:00:00 2001 From: Mitchell Nielsen Date: Mon, 28 Oct 2024 17:56:25 -0500 Subject: [PATCH] Check if null before unmarshalling Checks if an attribute is null before attempting to unmarshal it. Closes https://github.com/PrefectHQ/terraform-provider-prefect/issues/289 --- internal/provider/resources/deployment.go | 40 ++++++++++++++--------- internal/provider/resources/work_pool.go | 16 +++++---- 2 files changed, 35 insertions(+), 21 deletions(-) diff --git a/internal/provider/resources/deployment.go b/internal/provider/resources/deployment.go index 207db7b..95d1e75 100644 --- a/internal/provider/resources/deployment.go +++ b/internal/provider/resources/deployment.go @@ -308,21 +308,27 @@ func (r *DeploymentResource) Create(ctx context.Context, req resource.CreateRequ } var parameters map[string]interface{} - resp.Diagnostics.Append(plan.Parameters.Unmarshal(¶meters)...) - if resp.Diagnostics.HasError() { - return + if !plan.Parameters.IsNull() { + resp.Diagnostics.Append(plan.Parameters.Unmarshal(¶meters)...) + if resp.Diagnostics.HasError() { + return + } } var jobVariables map[string]interface{} - resp.Diagnostics.Append(plan.JobVariables.Unmarshal(&jobVariables)...) - if resp.Diagnostics.HasError() { - return + if !plan.JobVariables.IsNull() { + resp.Diagnostics.Append(plan.JobVariables.Unmarshal(&jobVariables)...) + if resp.Diagnostics.HasError() { + return + } } var parameterOpenAPISchema map[string]interface{} - resp.Diagnostics.Append(plan.ParameterOpenAPISchema.Unmarshal(¶meterOpenAPISchema)...) - if resp.Diagnostics.HasError() { - return + if !plan.ParameterOpenAPISchema.IsNull() { + resp.Diagnostics.Append(plan.ParameterOpenAPISchema.Unmarshal(¶meterOpenAPISchema)...) + if resp.Diagnostics.HasError() { + return + } } deployment, err := client.Create(ctx, api.DeploymentCreate{ @@ -473,15 +479,19 @@ func (r *DeploymentResource) Update(ctx context.Context, req resource.UpdateRequ } var parameters map[string]interface{} - resp.Diagnostics.Append(model.Parameters.Unmarshal(¶meters)...) - if resp.Diagnostics.HasError() { - return + if !model.Parameters.IsNull() { + resp.Diagnostics.Append(model.Parameters.Unmarshal(¶meters)...) + if resp.Diagnostics.HasError() { + return + } } var jobVariables map[string]interface{} - resp.Diagnostics.Append(model.JobVariables.Unmarshal(&jobVariables)...) - if resp.Diagnostics.HasError() { - return + if !model.JobVariables.IsNull() { + resp.Diagnostics.Append(model.JobVariables.Unmarshal(&jobVariables)...) + if resp.Diagnostics.HasError() { + return + } } payload := api.DeploymentUpdate{ diff --git a/internal/provider/resources/work_pool.go b/internal/provider/resources/work_pool.go index 190b80d..a0d92cc 100644 --- a/internal/provider/resources/work_pool.go +++ b/internal/provider/resources/work_pool.go @@ -199,9 +199,11 @@ func (r *WorkPoolResource) Create(ctx context.Context, req resource.CreateReques } baseJobTemplate := map[string]interface{}{} - resp.Diagnostics.Append(plan.BaseJobTemplate.Unmarshal(&baseJobTemplate)...) - if resp.Diagnostics.HasError() { - return + if !plan.BaseJobTemplate.IsNull() { + resp.Diagnostics.Append(plan.BaseJobTemplate.Unmarshal(&baseJobTemplate)...) + if resp.Diagnostics.HasError() { + return + } } client, err := r.client.WorkPools(plan.AccountID.ValueUUID(), plan.WorkspaceID.ValueUUID()) @@ -278,9 +280,11 @@ func (r *WorkPoolResource) Update(ctx context.Context, req resource.UpdateReques } baseJobTemplate := map[string]interface{}{} - resp.Diagnostics.Append(plan.BaseJobTemplate.Unmarshal(&baseJobTemplate)...) - if resp.Diagnostics.HasError() { - return + if !plan.BaseJobTemplate.IsNull() { + resp.Diagnostics.Append(plan.BaseJobTemplate.Unmarshal(&baseJobTemplate)...) + if resp.Diagnostics.HasError() { + return + } } client, err := r.client.WorkPools(plan.AccountID.ValueUUID(), plan.WorkspaceID.ValueUUID())