Skip to content

Commit

Permalink
Fix: drift issues with variable sets (#889)
Browse files Browse the repository at this point in the history
* Fix: drift issues with variable sets

* don't ignore drift
  • Loading branch information
TomerHeber committed Jul 11, 2024
1 parent 5196058 commit 08b924d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 17 deletions.
7 changes: 4 additions & 3 deletions client/configuration_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ type UpdateConfigurationSetPayload struct {
}

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

func (client *ApiClient) ConfigurationSetCreate(payload *CreateConfigurationSetPayload) (*ConfigurationSet, error) {
Expand Down
6 changes: 4 additions & 2 deletions env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ func resourceEnvironment() *schema.Resource {
},
"variable_sets": {
Type: schema.TypeList,
Description: "a list of variable set to assign to this environment",
Description: "a list of variable set to assign to this environment. Note: must not be used with 'env0_variable_set_assignment'",
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -630,7 +630,9 @@ func getEnvironmentVariableSetIdsFromApi(d *schema.ResourceData, apiClient clien

var environmentVariableSetIds []string
for _, variableSet := range environmentVariableSets {
environmentVariableSetIds = append(environmentVariableSetIds, variableSet.Id)
if variableSet.AssignmentScope == "ENVIRONMENT" {
environmentVariableSetIds = append(environmentVariableSetIds, variableSet.Id)
}
}

return environmentVariableSetIds, nil
Expand Down
12 changes: 8 additions & 4 deletions env0/resource_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,19 +261,23 @@ func TestUnitEnvironmentResource(t *testing.T) {

configurationSets := []client.ConfigurationSet{
{
Id: "id1",
Id: "id1",
AssignmentScope: "ENVIRONMENT",
},
{
Id: "id2",
Id: "id2",
AssignmentScope: "ENVIRONMENT",
},
}

updatedConfigurationSets := []client.ConfigurationSet{
{
Id: "id3",
Id: "id3",
AssignmentScope: "ENVIRONMENT",
},
{
Id: "id2",
Id: "id2",
AssignmentScope: "ENVIRONMENT",
},
}

Expand Down
10 changes: 10 additions & 0 deletions env0/resource_variable_set_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ func resourceVariableSetAssignment() *schema.Resource {
ReadContext: resourceVariableSetAssignmentRead,
DeleteContext: resourceVariableSetAssignmentDelete,

Description: "note: avoid assigning to environments using 'variable_sets' (see environment schema).",

Schema: map[string]*schema.Schema{
"scope": {
Type: schema.TypeString,
Expand Down Expand Up @@ -87,6 +89,10 @@ func resourceVariableSetAssignmentUpdate(ctx context.Context, d *schema.Resource

// In API but not in Schema - delete.
for _, apiConfigurationSet := range apiConfigurationSets {
if apiConfigurationSet.AssignmentScope != assignmentSchema.Scope {
continue
}

found := false

apiSetId := apiConfigurationSet.Id
Expand Down Expand Up @@ -181,6 +187,10 @@ func resourceVariableSetAssignmentRead(ctx context.Context, d *schema.ResourceDa
}

for _, apiConfigurationSet := range apiConfigurationSets {
if apiConfigurationSet.AssignmentScope != assignmentSchema.Scope {
continue
}

apiSetId := apiConfigurationSet.Id
found := false

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

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

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

Expand Down

0 comments on commit 08b924d

Please sign in to comment.