Skip to content

Commit

Permalink
fix: log when tenant not associated to common variable template (#792)
Browse files Browse the repository at this point in the history
* fix: log when tenant not associated to common variable template

fixes #790

* chore: PR feedback

* chore: PR feedback 2

* chore: PR feedback 3
  • Loading branch information
mjhilton authored Oct 4, 2024
1 parent d2f1fd3 commit 6e97b3c
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
}

err = checkIfCandidateVariableRequiredForTenant(tenant, tenantVariables, plan)
if 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) error {
if len(tenant.ProjectEnvironments) == 0 {
return 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 nil
}
}
} else {
return fmt.Errorf("tenant not connected to a project that includes variable set %s", plan.LibraryVariableSetID)
}

return fmt.Errorf("common template %s not found in variable set %s", plan.TemplateID, plan.LibraryVariableSetID)
}

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 6e97b3c

Please sign in to comment.