From ab87d91c1e59e75f1e75860309d2cb6a2058d958 Mon Sep 17 00:00:00 2001 From: Tomer Heber Date: Sun, 12 Nov 2023 17:44:21 -0600 Subject: [PATCH] Feat: env0_environment with project_id property (#742) --- env0/data_environment.go | 7 +++++-- env0/data_environment_test.go | 14 ++++++++++++++ env0/resource_environment.go | 8 ++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/env0/data_environment.go b/env0/data_environment.go index d5cf510b..49a2376d 100644 --- a/env0/data_environment.go +++ b/env0/data_environment.go @@ -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, @@ -105,6 +106,8 @@ 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) @@ -112,9 +115,9 @@ func dataEnvironmentRead(ctx context.Context, d *schema.ResourceData, meta inter 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 } diff --git a/env0/data_environment_test.go b/env0/data_environment_test.go index 05d4338e..dfd40f03 100644 --- a/env0/data_environment_test.go +++ b/env0/data_environment_test.go @@ -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, @@ -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" @@ -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"), diff --git a/env0/resource_environment.go b/env0/resource_environment.go index f89b76a4..499b9e79 100644 --- a/env0/resource_environment.go +++ b/env0/resource_environment.go @@ -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 { @@ -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) } @@ -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)