-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #452 from hashicorp/varsets
Manage Variable Sets
- Loading branch information
Showing
15 changed files
with
1,358 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package tfe | ||
|
||
import ( | ||
"fmt" | ||
|
||
tfe "github.com/hashicorp/go-tfe" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func dataSourceTFEVariableSet() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceTFEVariableSetRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"organization": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"global": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
|
||
"workspace_ids": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
|
||
"variable_ids": { | ||
Type: schema.TypeSet, | ||
Optional: true, | ||
Computed: true, | ||
Elem: &schema.Schema{Type: schema.TypeString}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceTFEVariableSetRead(d *schema.ResourceData, meta interface{}) error { | ||
tfeClient := meta.(*tfe.Client) | ||
|
||
// Get the name and organization. | ||
name := d.Get("name").(string) | ||
organization := d.Get("organization").(string) | ||
|
||
// Create an options struct. | ||
options := tfe.VariableSetListOptions{} | ||
|
||
for { | ||
// Variable Set relations, vars and workspaces, are omitted from the querying until | ||
// we find the desired variable set. | ||
l, err := tfeClient.VariableSets.List(ctx, organization, &options) | ||
if err != nil { | ||
if err == tfe.ErrResourceNotFound { | ||
return fmt.Errorf("could not find variable set%s/%s", organization, name) | ||
} | ||
return fmt.Errorf("Error retrieving variable set: %v", err) | ||
} | ||
|
||
for _, vs := range l.Items { | ||
if vs.Name == name { | ||
d.Set("name", vs.Name) | ||
d.Set("description", vs.Description) | ||
d.Set("global", vs.Global) | ||
|
||
//Only now include vars and workspaces to cut down on request load. | ||
readOptions := tfe.VariableSetReadOptions{ | ||
Include: &[]tfe.VariableSetIncludeOpt{tfe.VariableSetWorkspaces, tfe.VariableSetVars}, | ||
} | ||
|
||
vs, err = tfeClient.VariableSets.Read(ctx, vs.ID, &readOptions) | ||
if err != nil { | ||
return fmt.Errorf("Error retrieving variable set relations: %v", err) | ||
} | ||
|
||
var workspaces []interface{} | ||
for _, workspace := range vs.Workspaces { | ||
workspaces = append(workspaces, workspace.ID) | ||
} | ||
d.Set("workspace_ids", workspaces) | ||
|
||
var variables []interface{} | ||
for _, variable := range vs.Variables { | ||
variables = append(variables, variable.ID) | ||
} | ||
d.Set("variable_ids", variables) | ||
|
||
d.SetId(vs.ID) | ||
return nil | ||
} | ||
} | ||
|
||
// Exit the loop when we've seen all pages. | ||
if l.CurrentPage >= l.TotalPages { | ||
break | ||
} | ||
|
||
// Update the page number to get the next page. | ||
options.PageNumber = l.NextPage | ||
} | ||
|
||
return fmt.Errorf("Could not find variable set %s/%s", organization, name) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package tfe | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"testing" | ||
"time" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccTFEVariableSetsDataSource_basic(t *testing.T) { | ||
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int() | ||
orgName := fmt.Sprintf("org-%d", rInt) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccTFEVariableSetsDataSourceConfig_basic(rInt), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.tfe_variable_set.foobar", "id"), | ||
resource.TestCheckResourceAttr( | ||
"data.tfe_variable_set.foobar", "name", fmt.Sprintf("varset-foo-%d", rInt)), | ||
resource.TestCheckResourceAttr( | ||
"data.tfe_variable_set.foobar", "description", "a description"), | ||
resource.TestCheckResourceAttr( | ||
"data.tfe_variable_set.foobar", "global", "false"), | ||
resource.TestCheckResourceAttr( | ||
"data.tfe_variable_set.foobar", "organization", orgName), | ||
), | ||
}, | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
func TestAccTFEVariableSetsDataSource_full(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: testAccTFEVariableSetsDataSourceConfig_full(rInt), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.tfe_variable_set.foobar", "id"), | ||
resource.TestCheckResourceAttr( | ||
"data.tfe_variable_set.foobar", "name", fmt.Sprintf("varset-foo-%d", rInt)), | ||
resource.TestCheckResourceAttr( | ||
"data.tfe_variable_set.foobar", "workspace_ids.#", "1"), | ||
resource.TestCheckResourceAttr( | ||
"data.tfe_variable_set.foobar", "variable_ids.#", "1"), | ||
), | ||
}, | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
func testAccTFEVariableSetsDataSourceConfig_basic(rInt int) string { | ||
return fmt.Sprintf(` | ||
resource "tfe_organization" "foobar" { | ||
name = "org-%d" | ||
email = "[email protected]" | ||
} | ||
resource "tfe_variable_set" "foobar" { | ||
name = "varset-foo-%d" | ||
description = "a description" | ||
organization = tfe_organization.foobar.id | ||
} | ||
data "tfe_variable_set" "foobar" { | ||
name = tfe_variable_set.foobar.name | ||
organization = tfe_variable_set.foobar.organization | ||
}`, rInt, rInt) | ||
} | ||
|
||
func testAccTFEVariableSetsDataSourceConfig_full(rInt int) string { | ||
return fmt.Sprintf(` | ||
resource "tfe_organization" "foobar" { | ||
name = "org-%d" | ||
email = "[email protected]" | ||
} | ||
resource "tfe_workspace" "foobar" { | ||
name = "workspace-foo-%d" | ||
organization = tfe_organization.foobar.id | ||
} | ||
resource "tfe_variable_set" "foobar" { | ||
name = "varset-foo-%d" | ||
description = "a description" | ||
organization = tfe_organization.foobar.id | ||
workspace_ids = [tfe_workspace.foobar.id] | ||
} | ||
resource "tfe_variable" "envfoo" { | ||
key = "vfoo" | ||
value = "bar" | ||
category = "env" | ||
variable_set_id = tfe_variable_set.foobar.id | ||
} | ||
data "tfe_variable_set" "foobar" { | ||
name = tfe_variable_set.foobar.name | ||
organization = tfe_variable_set.foobar.organization | ||
}`, rInt, rInt, rInt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.