Skip to content
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: env0_environment with project_id property #742

Merged
merged 1 commit into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a new field and changed the order of the variables.
In case more variables are added, I'll convert it to a "struct filter".

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
Loading