Skip to content

Commit

Permalink
Feat: add data provider for workflows thru data env0_environment (#957)
Browse files Browse the repository at this point in the history
* Feat: add data provider for workflows thru data env0_environment

* added another alias in the test

* add sort
  • Loading branch information
TomerHeber authored Sep 24, 2024
1 parent fdd9ac5 commit a6acb67
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
45 changes: 45 additions & 0 deletions env0/data_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package env0

import (
"context"
"slices"
"strings"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -98,6 +100,25 @@ func dataEnvironment() *schema.Resource {
Description: "The token id used for repo integrations (Used by Gitlab or Azure DevOps)",
Computed: true,
},
"sub_environment_configuration": {
Type: schema.TypeList,
Description: "the sub environments of the workflow environment. (Empty for non workflow environments)",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: "the id of the sub environment",
Computed: true,
},
"alias": {
Type: schema.TypeString,
Description: "sub environment alias name",
Computed: true,
},
},
},
},
},
}
}
Expand Down Expand Up @@ -127,6 +148,30 @@ func dataEnvironmentRead(ctx context.Context, d *schema.ResourceData, meta inter
return diag.FromErr(err)
}

subEnvironments := []interface{}{}

if environment.LatestDeploymentLog.WorkflowFile != nil {
for alias, subenv := range environment.LatestDeploymentLog.WorkflowFile.Environments {
subEnvironment := map[string]interface{}{
"id": subenv.EnvironmentId,
"alias": alias,
}
subEnvironments = append(subEnvironments, subEnvironment)
}
}

slices.SortFunc(subEnvironments, func(a, b interface{}) int {
amap := a.(map[string]interface{})
bmap := b.(map[string]interface{})

aalias := amap["alias"].(string)
balias := bmap["alias"].(string)

return strings.Compare(aalias, balias)
})

d.Set("sub_environment_configuration", subEnvironments)

templateId := environment.LatestDeploymentLog.BlueprintId

template, err := getTemplateById(templateId, meta)
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 @@ -32,6 +32,16 @@ func TestEnvironmentDataSource(t *testing.T) {
BlueprintId: template.Id,
BlueprintRevision: "revision",
Output: []byte(`{"a": "b"}`),
WorkflowFile: &client.WorkflowFile{
Environments: map[string]client.WorkflowSubEnvironment{
"db": {
EnvironmentId: "id_db",
},
"compute": {
EnvironmentId: "id_compute",
},
},
},
},
}

Expand Down Expand Up @@ -78,6 +88,10 @@ func TestEnvironmentDataSource(t *testing.T) {
resource.TestCheckResourceAttr(accessor, "token_id", template.TokenId),
resource.TestCheckResourceAttr(accessor, "github_installation_id", strconv.Itoa(template.GithubInstallationId)),
resource.TestCheckResourceAttr(accessor, "bitbucket_client_key", template.BitbucketClientKey),
resource.TestCheckResourceAttr(accessor, "sub_environment_configuration.0.alias", "compute"),
resource.TestCheckResourceAttr(accessor, "sub_environment_configuration.0.id", "id_compute"),
resource.TestCheckResourceAttr(accessor, "sub_environment_configuration.1.alias", "db"),
resource.TestCheckResourceAttr(accessor, "sub_environment_configuration.1.id", "id_db"),
),
},
},
Expand Down
15 changes: 9 additions & 6 deletions tests/integration/012_environment/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ resource "env0_template_project_assignment" "assignment_workflow" {
project_id = env0_project.test_project.id
}


resource "env0_variable_set" "variable_set1" {
name = "variable-set-project-${random_string.random.result}"
description = "description123"
Expand Down Expand Up @@ -253,10 +252,10 @@ resource "env0_environment" "workflow-environment" {
env0_template_project_assignment.assignment_workflow,
env0_template_project_assignment.assignment_sub_environment_null_template
]
force_destroy = true
name = "environment-workflow-${random_string.random.result}"
project_id = env0_project.test_project.id
template_id = env0_template.workflow_template.id
force_destroy = true
name = "environment-workflow-${random_string.random.result}"
project_id = env0_project.test_project.id
template_id = env0_template.workflow_template.id

configuration {
name = "n1"
Expand All @@ -268,7 +267,7 @@ resource "env0_environment" "workflow-environment" {
sub_environment_configuration {
alias = "rootService1"
revision = "master"
approve_plan_automatically = var.second_run ? false : true
approve_plan_automatically = var.second_run ? false : true
configuration {
name = "sub_env1_var1"
value = "hello"
Expand All @@ -280,6 +279,10 @@ resource "env0_environment" "workflow-environment" {
}
}

data "env0_environment" "test-workflow" {
id = env0_environment.workflow-environment.id
}

resource "env0_environment" "mark_as_archived" {
depends_on = [env0_template_project_assignment.assignment]
name = "environment-mark-as-archived-${random_string.random.result}"
Expand Down

0 comments on commit a6acb67

Please sign in to comment.