Skip to content

Commit

Permalink
Validate that the template exists before proceeding (#833)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcasperson authored Dec 4, 2024
1 parent e709e02 commit 9361f05
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions octopusdeploy_framework/resource_tenant_project_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ func (t *tenantProjectVariableResource) Read(ctx context.Context, req resource.R
return
}

if !checkIfTemplateExists(tenantVariables, state) {
// The template no longer exists, so the variable can no longer exist either
return
}

isSensitive, err := checkIfVariableIsSensitive(tenantVariables, state)
if err != nil {
resp.Diagnostics.AddError("Error checking if variable is sensitive", err.Error())
Expand Down Expand Up @@ -251,6 +256,17 @@ func (t *tenantProjectVariableResource) ImportState(ctx context.Context, req res
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("template_id"), idParts[3])...)
}

func checkIfTemplateExists(tenantVariables *variables.TenantVariables, plan tenantProjectVariableResourceModel) bool {
if projectVariable, ok := tenantVariables.ProjectVariables[plan.ProjectID.ValueString()]; ok {
for _, template := range projectVariable.Templates {
if template.GetID() == plan.TemplateID.ValueString() {
return true
}
}
}
return false
}

func checkIfVariableIsSensitive(tenantVariables *variables.TenantVariables, plan tenantProjectVariableResourceModel) (bool, error) {
if projectVariable, ok := tenantVariables.ProjectVariables[plan.ProjectID.ValueString()]; ok {
for _, template := range projectVariable.Templates {
Expand Down

0 comments on commit 9361f05

Please sign in to comment.