-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat: add data source for env0_variable_set #897
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
06c3d2d
Feat: add data source for env0_variable_set
TomerHeber 8b8dd79
updated API call
TomerHeber 36c6fa9
Merge branch 'main' into feat-ds-variable-set-#892
TomerHeber 593cd9a
to lower case
TomerHeber 98d5bb5
Merge branch 'main' into feat-ds-variable-set-#892
TomerHeber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
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" | ||
) | ||
|
||
func dataVariableSet() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataVariableSetRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Description: "the name of the variable set", | ||
Required: true, | ||
}, | ||
"scope": { | ||
Type: schema.TypeString, | ||
Description: "the scope of the variable set. Valid values: 'ORGANIZATION', or 'PROJECT'", | ||
Required: true, | ||
ValidateDiagFunc: NewStringInValidator([]string{"ORGANIZATION", "PROJECT"}), | ||
}, | ||
"project_id": { | ||
Type: schema.TypeString, | ||
Description: "the id of the 'PROJECT' scope. Is not required for 'ORGANIZATION' scope", | ||
Optional: true, | ||
}, | ||
"id": { | ||
Type: schema.TypeString, | ||
Description: "the id variable set", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataVariableSetRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
resource := struct { | ||
Name string | ||
Scope string | ||
ProjectId string | ||
}{} | ||
|
||
if err := readResourceData(&resource, d); err != nil { | ||
return diag.Errorf("schema resource data deserialization failed: %v", err) | ||
} | ||
|
||
apiClient := meta.(client.ApiClientInterface) | ||
|
||
var scopeId string | ||
|
||
switch resource.Scope { | ||
case "ORGANIZATION": | ||
var err error | ||
scopeId, err = apiClient.OrganizationId() | ||
if err != nil { | ||
return diag.Errorf("could not get organization id: %v", err) | ||
} | ||
case "PROJECT": | ||
if resource.ProjectId == "" { | ||
return diag.Errorf("'project_id' is required") | ||
} | ||
scopeId = resource.ProjectId | ||
} | ||
|
||
variableSets, err := apiClient.ConfigurationSets(resource.Scope, scopeId) | ||
if err != nil { | ||
return diag.Errorf("could not get variable sets: %v", err) | ||
} | ||
|
||
for _, variableSet := range variableSets { | ||
if variableSet.Name == resource.Name { | ||
d.SetId(variableSet.Id) | ||
return nil | ||
} | ||
} | ||
|
||
return diag.Errorf("variable set not found") | ||
} |
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,158 @@ | ||
package env0 | ||
|
||
import ( | ||
"errors" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/env0/terraform-provider-env0/client" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestVariableSetDataSource(t *testing.T) { | ||
projectId := "project_id" | ||
organizationId := "organization_id" | ||
|
||
v1 := client.ConfigurationSet{ | ||
Id: "id1", | ||
Name: "name1", | ||
CreationScopeId: projectId, | ||
} | ||
|
||
v2 := client.ConfigurationSet{ | ||
Id: "id2", | ||
Name: "name2", | ||
CreationScopeId: projectId, | ||
} | ||
|
||
v3 := client.ConfigurationSet{ | ||
Id: "id3", | ||
Name: "name3", | ||
CreationScopeId: organizationId, | ||
} | ||
|
||
v4 := client.ConfigurationSet{ | ||
Id: "id4", | ||
Name: "name4", | ||
CreationScopeId: "some_other_id", | ||
} | ||
|
||
resourceType := "env0_variable_set" | ||
resourceName := "test_variable_set" | ||
accessor := dataSourceAccessor(resourceType, resourceName) | ||
|
||
getConfig := func(name string, scope string, projectId string) string { | ||
fields := map[string]interface{}{"name": name, "scope": scope} | ||
if projectId != "" { | ||
fields["project_id"] = projectId | ||
} | ||
return dataSourceConfigCreate(resourceType, resourceName, fields) | ||
} | ||
|
||
mockVariableSetsCall := func(scope string, scopeId string, returnValue []client.ConfigurationSet) func(mockFunc *client.MockApiClientInterface) { | ||
return func(mock *client.MockApiClientInterface) { | ||
if organizationId != "" { | ||
mock.EXPECT().OrganizationId().AnyTimes().Return(organizationId, nil) | ||
} | ||
mock.EXPECT().ConfigurationSets(scope, scopeId).AnyTimes().Return(returnValue, nil) | ||
} | ||
} | ||
|
||
t.Run("project id scope", func(t *testing.T) { | ||
runUnitTest(t, | ||
resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: getConfig(v2.Name, "PROJECT", v2.CreationScopeId), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "id", v2.Id), | ||
), | ||
}, | ||
}, | ||
}, | ||
mockVariableSetsCall("PROJECT", projectId, []client.ConfigurationSet{ | ||
v4, v1, v2, | ||
}), | ||
) | ||
}) | ||
|
||
t.Run("organization id scope", func(t *testing.T) { | ||
runUnitTest(t, | ||
resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: getConfig(v3.Name, "ORGANIZATION", ""), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttr(accessor, "id", v3.Id), | ||
), | ||
}, | ||
}, | ||
}, | ||
mockVariableSetsCall("ORGANIZATION", organizationId, []client.ConfigurationSet{ | ||
v4, v3, | ||
}), | ||
) | ||
}) | ||
|
||
t.Run("name not found", func(t *testing.T) { | ||
runUnitTest(t, | ||
resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: getConfig("name that isn't found", "PROJECT", v2.CreationScopeId), | ||
ExpectError: regexp.MustCompile("variable set not found"), | ||
}, | ||
}, | ||
}, | ||
mockVariableSetsCall("PROJECT", projectId, []client.ConfigurationSet{ | ||
v4, v1, v2, v3, | ||
}), | ||
) | ||
}) | ||
|
||
t.Run("get configuration sets api call failed", func(t *testing.T) { | ||
runUnitTest(t, | ||
resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: getConfig(v2.Name, "PROJECT", v2.CreationScopeId), | ||
ExpectError: regexp.MustCompile("could not get variable sets: error"), | ||
}, | ||
}, | ||
}, | ||
func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().ConfigurationSets("PROJECT", projectId).AnyTimes().Return(nil, errors.New("error")) | ||
}, | ||
) | ||
}) | ||
|
||
t.Run("get organization id api call failed", func(t *testing.T) { | ||
runUnitTest(t, | ||
resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: getConfig(v3.Name, "ORGANIZATION", ""), | ||
ExpectError: regexp.MustCompile("could not get organization id: error"), | ||
}, | ||
}, | ||
}, | ||
func(mock *client.MockApiClientInterface) { | ||
mock.EXPECT().OrganizationId().AnyTimes().Return("", errors.New("error")) | ||
}, | ||
) | ||
}) | ||
|
||
t.Run("project_id is required", func(t *testing.T) { | ||
runUnitTest(t, | ||
resource.TestCase{ | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: getConfig(v2.Name, "PROJECT", ""), | ||
ExpectError: regexp.MustCompile("'project_id' is required"), | ||
}, | ||
}, | ||
}, | ||
func(mock *client.MockApiClientInterface) {}, | ||
) | ||
}) | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think calling this
creationScope
is clearer, will also match our APIThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could do "creation_scope".
However, in all other places in the provider, it's scope.
So I prefer to keep it aligned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, I already approved regardless 😄