Skip to content

Commit

Permalink
fix: log when tenant not associated to common variable template
Browse files Browse the repository at this point in the history
fixes #790
  • Loading branch information
mjhilton committed Oct 3, 2024
1 parent d2f1fd3 commit 9b61074
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions octopusdeploy_framework/resource_tenant_common_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ func (t *tenantCommonVariableResource) Create(ctx context.Context, req resource.
return
}

tenantNeedsAValueForThisVariable, err := checkIfCandidateVariableRequiredForTenant(tenant, tenantVariables, plan)
if !tenantNeedsAValueForThisVariable && err != nil {
resp.Diagnostics.AddError("Tenant doesn't need a value for this Common Variable", "Tenants must be connected to a Project with an included Library Variable Set that defines Common Variable templates, before common variable values can be provided ("+err.Error()+")")
return
}

isSensitive, err := checkIfCommonVariableIsSensitive(tenantVariables, plan)
if err != nil {
resp.Diagnostics.AddError("Error checking if variable is sensitive", err.Error())
Expand Down Expand Up @@ -245,6 +251,24 @@ func checkIfCommonVariableIsSensitive(tenantVariables *variables.TenantVariables
return false, fmt.Errorf("unable to find template for tenant variable")
}

func checkIfCandidateVariableRequiredForTenant(tenant *tenants.Tenant, tenantVariables *variables.TenantVariables, plan tenantCommonVariableResourceModel) (bool, error) {
if tenant.ProjectEnvironments == nil || len(tenant.ProjectEnvironments) == 0 {
return false, fmt.Errorf("tenant not connected to any projects")
}

if libraryVariable, ok := tenantVariables.LibraryVariables[plan.LibraryVariableSetID.ValueString()]; ok {
for _, template := range libraryVariable.Templates {
if template.GetID() == plan.TemplateID.ValueString() {
return true, nil
}
}
} else {
return false, fmt.Errorf("tenant not connected to a project that includes variable set " + plan.LibraryVariableSetID.ValueString())
}

return false, fmt.Errorf("common template " + plan.TemplateID.ValueString() + " not found in variable set " + plan.LibraryVariableSetID.ValueString())
}

func updateTenantCommonVariable(tenantVariables *variables.TenantVariables, plan tenantCommonVariableResourceModel, isSensitive bool) error {
if libraryVariable, ok := tenantVariables.LibraryVariables[plan.LibraryVariableSetID.ValueString()]; ok {
libraryVariable.Variables[plan.TemplateID.ValueString()] = core.NewPropertyValue(plan.Value.ValueString(), isSensitive)
Expand Down

0 comments on commit 9b61074

Please sign in to comment.