-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: add support for variable set assignment resource (#870)
* Feat: add support for variable set assignment resource * Add tests * fix test * update test * remove token id to check failures in integration test * add back token id
- Loading branch information
1 parent
de81881
commit c9b6370
Showing
9 changed files
with
422 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
package env0 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
type variableSetAssignmentSchema struct { | ||
Scope string | ||
ScopeId string | ||
SetIds []string | ||
} | ||
|
||
func resourceVariableSetAssignment() *schema.Resource { | ||
return &schema.Resource{ | ||
CreateContext: resourceVariableSetAssignmentCreate, | ||
UpdateContext: resourceVariableSetAssignmentUpdate, | ||
ReadContext: resourceVariableSetAssignmentRead, | ||
DeleteContext: resourceVariableSetAssignmentDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"scope": { | ||
Type: schema.TypeString, | ||
Description: "the resource(scope) type to assign to. Valid values: 'template', 'environment', 'module', 'organization', 'project', 'deployment'", | ||
Required: true, | ||
ValidateDiagFunc: NewStringInValidator([]string{"template", "environment", "module", "organization", "project", "deployment"}), | ||
ForceNew: true, | ||
}, | ||
"scope_id": { | ||
Type: schema.TypeString, | ||
Description: "the resource(scope)id (e.g. template id)", | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"set_ids": { | ||
Type: schema.TypeList, | ||
Description: "list of variable sets", | ||
Required: true, | ||
Elem: &schema.Schema{ | ||
Type: schema.TypeString, | ||
Description: "the variable set id", | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceVariableSetAssignmentCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
var assignmentSchema variableSetAssignmentSchema | ||
|
||
if err := readResourceData(&assignmentSchema, d); err != nil { | ||
return diag.Errorf("schema resource data deserialization failed: %v", err) | ||
} | ||
|
||
if len(assignmentSchema.SetIds) > 0 { | ||
if err := apiClient.AssignConfigurationSets(assignmentSchema.Scope, assignmentSchema.ScopeId, assignmentSchema.SetIds); err != nil { | ||
return diag.Errorf("failed to assign variable sets to the scope: %v", err) | ||
} | ||
} | ||
|
||
d.SetId(assignmentSchema.ScopeId) | ||
|
||
return nil | ||
} | ||
|
||
func resourceVariableSetAssignmentUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
var assignmentSchema variableSetAssignmentSchema | ||
|
||
if err := readResourceData(&assignmentSchema, d); err != nil { | ||
return diag.Errorf("schema resource data deserialization failed: %v", err) | ||
} | ||
|
||
apiConfigurationSets, err := apiClient.ConfigurationSetsAssignments(assignmentSchema.Scope, assignmentSchema.ScopeId) | ||
if err != nil { | ||
return diag.Errorf("failed to get variable sets assignments: %v", err) | ||
} | ||
|
||
// Compare between apiSetIds and schemaSetIds to find what to set ids to delete and what set ids to add. | ||
var toDelete, toAdd []string | ||
|
||
// In API but not in Schema - delete. | ||
for _, apiConfigurationSet := range apiConfigurationSets { | ||
found := false | ||
|
||
apiSetId := apiConfigurationSet.Id | ||
for _, schemaSetId := range assignmentSchema.SetIds { | ||
if apiSetId == schemaSetId { | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
toDelete = append(toDelete, apiSetId) | ||
} | ||
} | ||
|
||
// In Schema but not in API - add. | ||
for _, schemaSetId := range assignmentSchema.SetIds { | ||
found := false | ||
|
||
for _, apiConfigurationSet := range apiConfigurationSets { | ||
apiSetId := apiConfigurationSet.Id | ||
if schemaSetId == apiSetId { | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
toAdd = append(toAdd, schemaSetId) | ||
} | ||
} | ||
|
||
if len(toDelete) > 0 { | ||
if err := apiClient.UnassignConfigurationSets(assignmentSchema.Scope, assignmentSchema.ScopeId, toDelete); err != nil { | ||
return diag.Errorf("failed to unassign variable sets: %v", err) | ||
} | ||
} | ||
|
||
if len(toAdd) > 0 { | ||
if err := apiClient.AssignConfigurationSets(assignmentSchema.Scope, assignmentSchema.ScopeId, toAdd); err != nil { | ||
return diag.Errorf("failed to assign variable sets: %v", err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceVariableSetAssignmentDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
var assignmentSchema variableSetAssignmentSchema | ||
|
||
if err := readResourceData(&assignmentSchema, d); err != nil { | ||
return diag.Errorf("schema resource data deserialization failed: %v", err) | ||
} | ||
|
||
if len(assignmentSchema.SetIds) > 0 { | ||
if err := apiClient.UnassignConfigurationSets(assignmentSchema.Scope, assignmentSchema.ScopeId, assignmentSchema.SetIds); err != nil { | ||
return diag.Errorf("failed to unassign variable sets: %v", err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func resourceVariableSetAssignmentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
var assignmentSchema variableSetAssignmentSchema | ||
|
||
if err := readResourceData(&assignmentSchema, d); err != nil { | ||
return diag.Errorf("schema resource data deserialization failed: %v", err) | ||
} | ||
|
||
apiConfigurationSets, err := apiClient.ConfigurationSetsAssignments(assignmentSchema.Scope, assignmentSchema.ScopeId) | ||
if err != nil { | ||
return diag.Errorf("failed to get variable sets assignments: %v", err) | ||
} | ||
|
||
newSchemaSetIds := []string{} | ||
|
||
// To avoid drifts keep the schema order as much as possible. | ||
for _, schemaSetId := range assignmentSchema.SetIds { | ||
for _, apiConfigurationSet := range apiConfigurationSets { | ||
apiSetId := apiConfigurationSet.Id | ||
|
||
if schemaSetId == apiSetId { | ||
newSchemaSetIds = append(newSchemaSetIds, schemaSetId) | ||
break | ||
} | ||
} | ||
} | ||
|
||
for _, apiConfigurationSet := range apiConfigurationSets { | ||
apiSetId := apiConfigurationSet.Id | ||
found := false | ||
|
||
for _, schemaSetId := range assignmentSchema.SetIds { | ||
if schemaSetId == apiSetId { | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
newSchemaSetIds = append(newSchemaSetIds, apiSetId) | ||
} | ||
} | ||
|
||
if err := d.Set("set_ids", newSchemaSetIds); err != nil { | ||
return diag.Errorf("failed to set 'set_ids': %v", err) | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.