-
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 data source for env0_variable_set
- Loading branch information
1 parent
1bc1849
commit 06c3d2d
Showing
10 changed files
with
363 additions
and
1 deletion.
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,82 @@ | ||
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) | ||
|
||
organizationId := "" | ||
|
||
switch resource.Scope { | ||
case "ORGANIZATION": | ||
var err error | ||
organizationId, 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") | ||
} | ||
} | ||
|
||
variableSets, err := apiClient.ConfigurationSets(organizationId, resource.ProjectId) | ||
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(organizationId string, projectId 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(organizationId, projectId).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("", 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(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("", 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("", 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
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,14 @@ | ||
data "env0_project" "project" { | ||
name = "my project name" | ||
} | ||
|
||
data "env0_variable_set" "variable_set_project_scope" { | ||
name = "variable set name" | ||
scope = "PROJECT" | ||
project_id = data.env0_project.project.id | ||
} | ||
|
||
data "env0_variable_set" "variable_set_organization_scope" { | ||
name = "variable set name" | ||
scope = "ORGANIZATION" | ||
} |
Oops, something went wrong.