Skip to content

Commit

Permalink
Fix: variable set assignment drifts with parent projects (#961)
Browse files Browse the repository at this point in the history
* Fix: variable set assignment drifts with parent projects

* change comment wording

* use AssignmentScopeId instead

* requested to removed a filter that is not required
  • Loading branch information
TomerHeber authored Oct 7, 2024
1 parent 3bd6be6 commit 1209c07
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 22 deletions.
12 changes: 7 additions & 5 deletions client/configuration_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,17 @@ type UpdateConfigurationSetPayload struct {
}

type ConfigurationSet struct {
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
AssignmentScope string `json:"assignmentScope"`
CreationScopeId string `json:"creationScopeId"`
Id string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
AssignmentScope string `json:"assignmentScope"`
CreationScopeId string `json:"creationScopeId"`
AssignmentScopeId string `json:"assignmentScopeId"`
}

func (client *ApiClient) ConfigurationSetCreate(payload *CreateConfigurationSetPayload) (*ConfigurationSet, error) {
var result ConfigurationSet

var err error

if payload.Scope == "organization" && payload.ScopeId == "" {
Expand Down
2 changes: 2 additions & 0 deletions env0/resource_variable_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,13 @@ func mergeVariables(schema []client.ConfigurationVariable, api []client.Configur
for _, avariable := range api {
if svariable.Name == avariable.Name && *svariable.Type == *avariable.Type {
found = true

if avariable.IsSensitive != nil && *avariable.IsSensitive {
// Sensitive - to avoid drift use the value from the schema
avariable.Value = svariable.Value
}
res.currentVariables = append(res.currentVariables, avariable)

break
}
}
Expand Down
3 changes: 2 additions & 1 deletion env0/resource_variable_set_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ func resourceVariableSetAssignmentRead(ctx context.Context, d *schema.ResourceDa
}

for _, apiConfigurationSet := range apiConfigurationSets {
if !strings.EqualFold(apiConfigurationSet.AssignmentScope, assignmentSchema.Scope) {
// Filter out inherited assignments (e.g parent project).
if apiConfigurationSet.AssignmentScopeId != assignmentSchema.ScopeId {
continue
}

Expand Down
45 changes: 29 additions & 16 deletions env0/resource_variable_set_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,35 +20,46 @@ func TestUnitVariableSetAssignmentResource(t *testing.T) {
setIds := []string{"a1", "a2"}
configurationSetIds := []client.ConfigurationSet{
{
Id: "a1",
AssignmentScope: scope,
Id: "a1",
AssignmentScope: scope,
AssignmentScopeId: scopeId,
},
{
Id: "a2",
AssignmentScope: scope,
Id: "a2",
AssignmentScope: scope,
AssignmentScopeId: scopeId,
},
{
Id: "filtered_out",
AssignmentScope: scope,
AssignmentScopeId: "otherAssignmentScopeId",
},
}
// Validates that drifts do not occur due to ordering.
flippedConfigurationSetIds := []client.ConfigurationSet{
{
Id: "a2",
AssignmentScope: scope,
Id: "a2",
AssignmentScope: scope,
AssignmentScopeId: scopeId,
},
{
Id: "a1",
AssignmentScope: scope,
Id: "a1",
AssignmentScope: scope,
AssignmentScopeId: scopeId,
},
}

updatedSetIds := []string{"a1", "a3"}
updatedConfigurationSetIds := []client.ConfigurationSet{
{
Id: "a3",
AssignmentScope: scope,
Id: "a3",
AssignmentScope: scope,
AssignmentScopeId: scopeId,
},
{
Id: "a1",
AssignmentScope: scope,
Id: "a1",
AssignmentScope: scope,
AssignmentScopeId: scopeId,
},
}

Expand Down Expand Up @@ -103,12 +114,14 @@ func TestUnitVariableSetAssignmentResource(t *testing.T) {
setIds := []string{"a1"}
configurationSetIds := []client.ConfigurationSet{
{
Id: "a1",
AssignmentScope: scope,
Id: "a1",
AssignmentScope: scope,
AssignmentScopeId: scopeId,
},
{
Id: "a2",
AssignmentScope: scope,
Id: "a2",
AssignmentScope: scope,
AssignmentScopeId: scopeId,
},
}

Expand Down

0 comments on commit 1209c07

Please sign in to comment.