Skip to content

Commit

Permalink
Feat: env0_environment with project_id property
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Nov 4, 2023
1 parent 8eb4736 commit 0561041
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
7 changes: 5 additions & 2 deletions env0/data_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func dataEnvironment() *schema.Resource {
Type: schema.TypeString,
Description: "project id of the environment",
Computed: true,
Optional: true,
},
"approve_plan_automatically": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -105,16 +106,18 @@ func dataEnvironmentRead(ctx context.Context, d *schema.ResourceData, meta inter
var err diag.Diagnostics
var environment client.Environment

projectId := d.Get("project_id").(string)

id, ok := d.GetOk("id")
if ok {
environment, err = getEnvironmentById(id.(string), meta)
if err != nil {
return err
}
} else {
name := d.Get("name")
name := d.Get("name").(string)
excludeArchived := d.Get("exclude_archived")
environment, err = getEnvironmentByName(name.(string), meta, excludeArchived.(bool))
environment, err = getEnvironmentByName(meta, name, projectId, excludeArchived.(bool))
if err != nil {
return err
}
Expand Down
14 changes: 14 additions & 0 deletions env0/data_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ func TestEnvironmentDataSource(t *testing.T) {
Name: "other-name",
}

environmentWithSameName := client.Environment{
Id: "other-id",
Name: environment.Name,
ProjectId: "other-project-id",
}

archivedEnvironment := client.Environment{
Id: "id-archived",
Name: environment.Name,
Expand All @@ -48,6 +54,7 @@ func TestEnvironmentDataSource(t *testing.T) {

environmentFieldsByName := map[string]interface{}{"name": environment.Name}
environmentFieldsByNameWithExclude := map[string]interface{}{"name": environment.Name, "exclude_archived": "true"}
environmentFieldByNameWithProjectId := map[string]interface{}{"name": environment.Name, "project_id": environment.ProjectId}
environmentFieldsById := map[string]interface{}{"id": environment.Id}

resourceType := "env0_environment"
Expand Down Expand Up @@ -130,6 +137,13 @@ func TestEnvironmentDataSource(t *testing.T) {
)
})

t.Run("By Name with Different Project Id", func(t *testing.T) {
runUnitTest(t,
getValidTestCase(environmentFieldByNameWithProjectId),
mockListEnvironmentsCall([]client.Environment{environment, environmentWithSameName, otherEnvironment}, &template),
)
})

t.Run("Throw error when no name or id is supplied", func(t *testing.T) {
runUnitTest(t,
getErrorTestCase(map[string]interface{}{}, "one of `id,name` must be specified"),
Expand Down
8 changes: 6 additions & 2 deletions env0/resource_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,7 +1058,7 @@ func getConfigurationVariableFromSchema(variable map[string]interface{}) client.
return configurationVariable
}

func getEnvironmentByName(name interface{}, meta interface{}, excludeArchived bool) (client.Environment, diag.Diagnostics) {
func getEnvironmentByName(meta interface{}, name string, projectId string, excludeArchived bool) (client.Environment, diag.Diagnostics) {
apiClient := meta.(client.ApiClientInterface)
environments, err := apiClient.Environments()
if err != nil {
Expand All @@ -1071,6 +1071,10 @@ func getEnvironmentByName(name interface{}, meta interface{}, excludeArchived bo
continue
}

if projectId != "" && candidate.ProjectId != projectId {
continue
}

if candidate.Name == name {
environmentsByName = append(environmentsByName, candidate)
}
Expand Down Expand Up @@ -1107,7 +1111,7 @@ func resourceEnvironmentImport(ctx context.Context, d *schema.ResourceData, meta
} else {
tflog.Info(ctx, "Resolving environment by name", map[string]interface{}{"name": id})

environment, getErr = getEnvironmentByName(id, meta, false)
environment, getErr = getEnvironmentByName(meta, id, "", false)
}
apiClient := meta.(client.ApiClientInterface)
d.SetId(environment.Id)
Expand Down

0 comments on commit 0561041

Please sign in to comment.