Skip to content

Commit

Permalink
Check if null before unmarshalling
Browse files Browse the repository at this point in the history
Checks if an attribute is null before attempting to unmarshal it.

Closes #289
  • Loading branch information
mitchnielsen committed Oct 28, 2024
1 parent e98e2d7 commit 4c172ef
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
40 changes: 25 additions & 15 deletions internal/provider/resources/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(&parameters)...)
if resp.Diagnostics.HasError() {
return
if !plan.Parameters.IsNull() {
resp.Diagnostics.Append(plan.Parameters.Unmarshal(&parameters)...)
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(&parameterOpenAPISchema)...)
if resp.Diagnostics.HasError() {
return
if !plan.ParameterOpenAPISchema.IsNull() {
resp.Diagnostics.Append(plan.ParameterOpenAPISchema.Unmarshal(&parameterOpenAPISchema)...)
if resp.Diagnostics.HasError() {
return
}
}

deployment, err := client.Create(ctx, api.DeploymentCreate{
Expand Down Expand Up @@ -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(&parameters)...)
if resp.Diagnostics.HasError() {
return
if !model.Parameters.IsNull() {
resp.Diagnostics.Append(model.Parameters.Unmarshal(&parameters)...)
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{
Expand Down
16 changes: 10 additions & 6 deletions internal/provider/resources/work_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 4c172ef

Please sign in to comment.