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

Feature/variables data source #369

Merged
merged 21 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
124 changes: 124 additions & 0 deletions tfe/data_source_variables.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package tfe

import (
"fmt"
"log"

tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceTFEWorkspaceVariables() *schema.Resource {
varSchema := map[string]*schema.Schema{
"category": {
Type: schema.TypeString,
Computed: true,
},
"hcl": {
Type: schema.TypeBool,
Computed: true,
},
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"sensitive": {
Type: schema.TypeBool,
Computed: true,
},
"value": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
},
}
return &schema.Resource{
Read: dataSourceVariableRead,

Schema: map[string]*schema.Schema{
kilwa0 marked this conversation as resolved.
Show resolved Hide resolved
"env": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: varSchema,
},
},
"terraform": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: varSchema,
},
},
"variables": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: varSchema,
},
},
"workspace_id": {
Type: schema.TypeString,
Required: true,
},
},
}
}

func dataSourceVariableRead(d *schema.ResourceData, meta interface{}) error {
tfeClient := meta.(*tfe.Client)

// Get the name and organization.
workspaceID := d.Get("workspace_id").(string)

log.Printf("[DEBUG] Read configuration of workspace: %s", workspaceID)

totalEnvVariables := make([]interface{}, 0)
totalTerraformVariables := make([]interface{}, 0)

options := tfe.VariableListOptions{}

for {
variableList, err := tfeClient.Variables.List(ctx, workspaceID, options)
if err != nil {
return fmt.Errorf("Error retrieving variable list: %w", err)
}
terraformVars := make([]interface{}, 0)
envVars := make([]interface{}, 0)
for _, variable := range variableList.Items {
result := make(map[string]interface{})
result["id"] = variable.ID
result["category"] = variable.Category
result["hcl"] = variable.HCL
result["name"] = variable.Key
result["sensitive"] = variable.Sensitive
result["value"] = variable.Value
if variable.Category == "terraform" {
terraformVars = append(terraformVars, result)
} else if variable.Category == "env" {
envVars = append(envVars, result)
}
}

totalEnvVariables = append(totalEnvVariables, envVars...)
totalTerraformVariables = append(totalTerraformVariables, terraformVars...)

// Exit the loop when we've seen all pages.
if variableList.CurrentPage >= variableList.TotalPages {
break
}

// Update the page number to get the next page.
options.PageNumber = variableList.NextPage
}

d.SetId(fmt.Sprintf("variables/%v", workspaceID))
d.Set("variables", append(totalTerraformVariables, totalEnvVariables...))
d.Set("terraform", totalTerraformVariables)
d.Set("env", totalEnvVariables)
return nil
}
79 changes: 79 additions & 0 deletions tfe/data_source_variables_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package tfe

import (
"fmt"
"math/rand"
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccTFEVariablesDataSource_basic(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccTFEVariablesDataSourceConfig_basic(rInt),
Check: resource.ComposeAggregateTestCheckFunc(
// variables attribute
resource.TestCheckResourceAttrSet("data.tfe_variables.foobar", "id"),
resource.TestCheckOutput("variables", "foo"),
resource.TestCheckOutput("env", "foo"),
resource.TestCheckOutput("terraform", "foo"),
),
},
},
},
)
}

func testAccTFEVariablesDataSourceConfig_basic(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "org-%d"
email = "admin@company.com"
}

resource "tfe_workspace" "foobar" {
name = "workspace-foo-%d"
organization = tfe_organization.foobar.id
}

resource "tfe_variable" "terrbar" {
key = "foo"
value = "bar"
category = "terraform"
workspace_id = tfe_workspace.foobar.id
}

resource "tfe_variable" "envbar" {
key = "foo"
value = "bar"
category = "env"
workspace_id = tfe_workspace.foobar.id
}

data "tfe_variables" "foobar" {
workspace_id = tfe_workspace.foobar.id
depends_on = [
tfe_variable.terrbar,
tfe_variable.envbar
]
}

output "variables" {
value = data.tfe_variables.foobar.variables[0]["name"]
}

output "env" {
value = data.tfe_variables.foobar.env[0]["name"]
}

output "terraform" {
value = data.tfe_variables.foobar.terraform[0]["name"]
}`, rInt, rInt)
}
1 change: 1 addition & 0 deletions tfe/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func Provider() *schema.Provider {
"tfe_team_access": dataSourceTFETeamAccess(),
"tfe_workspace": dataSourceTFEWorkspace(),
"tfe_workspace_ids": dataSourceTFEWorkspaceIDs(),
"tfe_variables": dataSourceTFEWorkspaceVariables(),
},

ResourcesMap: map[string]*schema.Resource{
Expand Down
45 changes: 45 additions & 0 deletions website/docs/d/variables.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
layout: "tfe"
page_title: "Terraform Enterprise: tfe_variables"
sidebar_current: "docs-datasource-tfe-variables-x"
description: |-
Get information on a workspace variables.
---

# Data Source: tfe_variables

This data source is used to retrieve all variables defined in a specified workspace

## Example Usage

```hcl
data "tfe_workspace" "test" {
name = "my-workspace-name"
organization = "my-org-name"
}

data "tfe_variables" "test" {
workspace_id = data.tfe_workspace.test.id
}
```

## Argument Reference

The following arguments are supported:

* `workspace_id` - (Required) ID of the workspace.

## Attributes Reference

* `variables` - List containing all terraform and environment variables configured on the workspace
* `terraform` - List containing terraform variables configured on the workspace
* `env` - List containing environment variables configured on the workspace

The `variables, terraform and env` blocks contains:

* `id` - The variable Id
* `name` - The variable Key name
* `value` - The variable value. If the variable is sensitive this value will be empty.
* `category` - The category of the variable (terraform or environment)
* `sensitive` - If the variable is marked as sensitive or not
* `hcl` - If the variable is marked as HCL or not
kilwa0 marked this conversation as resolved.
Show resolved Hide resolved