Skip to content

Commit

Permalink
Feat: add data provider for workflows thru data env0_environment
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber committed Sep 22, 2024
1 parent fdd9ac5 commit 2bb1aa3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
33 changes: 33 additions & 0 deletions env0/data_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,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 +146,20 @@ 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)
}
}

d.Set("sub_environment_configuration", subEnvironments)

templateId := environment.LatestDeploymentLog.BlueprintId

template, err := getTemplateById(templateId, meta)
Expand Down
9 changes: 9 additions & 0 deletions env0/data_environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ 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",
},
},
},
},
}

Expand Down Expand Up @@ -78,6 +85,8 @@ 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", "db"),
resource.TestCheckResourceAttr(accessor, "sub_environment_configuration.0.id", "id"),
),
},
},
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 2bb1aa3

Please sign in to comment.