Skip to content

Commit e189f23

Browse files
committed
Add parent project ID to varsets
This new attribute specifies that the varset is owned by the given project.
1 parent 88c7362 commit e189f23

File tree

3 files changed

+122
-2
lines changed

3 files changed

+122
-2
lines changed

internal/provider/resource_tfe_variable_set.go

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package provider
1010

1111
import (
12+
"context"
1213
"fmt"
1314
"log"
1415
"regexp"
@@ -29,7 +30,16 @@ func resourceTFEVariableSet() *schema.Resource {
2930
StateContext: schema.ImportStatePassthroughContext,
3031
},
3132

32-
CustomizeDiff: customizeDiffIfProviderDefaultOrganizationChanged,
33+
CustomizeDiff: func(c context.Context, d *schema.ResourceDiff, meta interface{}) error {
34+
if err := customizeDiffIfProviderDefaultOrganizationChanged(c, d, meta); err != nil {
35+
return err
36+
}
37+
38+
if err := validateParentProjectID(d); err != nil {
39+
return err
40+
}
41+
return nil
42+
},
3343

3444
Schema: map[string]*schema.Schema{
3545
"name": {
@@ -68,6 +78,13 @@ func resourceTFEVariableSet() *schema.Resource {
6878
Computed: true,
6979
Elem: &schema.Schema{Type: schema.TypeString},
7080
},
81+
82+
"parent_project_id": {
83+
Type: schema.TypeString,
84+
Optional: true,
85+
Computed: true,
86+
ForceNew: true,
87+
},
7188
},
7289
}
7390
}
@@ -89,6 +106,14 @@ func resourceTFEVariableSetCreate(d *schema.ResourceData, meta interface{}) erro
89106
Priority: tfe.Bool(d.Get("priority").(bool)),
90107
}
91108

109+
if parentProject, ok := d.GetOk("parent_project_id"); ok {
110+
options.Parent = &tfe.Parent{
111+
Project: &tfe.Project{
112+
ID: parentProject.(string),
113+
},
114+
}
115+
}
116+
92117
if description, descriptionSet := d.GetOk("description"); descriptionSet {
93118
options.Description = tfe.String(description.(string))
94119
}
@@ -151,6 +176,10 @@ func resourceTFEVariableSetRead(d *schema.ResourceData, meta interface{}) error
151176
}
152177
d.Set("workspace_ids", wids)
153178

179+
if variableSet.Parent != nil && variableSet.Parent.Project != nil {
180+
d.Set("parent_project_id", variableSet.Parent.Project.ID)
181+
}
182+
154183
return nil
155184
}
156185

@@ -168,7 +197,7 @@ func resourceTFEVariableSetUpdate(d *schema.ResourceData, meta interface{}) erro
168197
log.Printf("[DEBUG] Update variable set: %s", d.Id())
169198
_, err := config.Client.VariableSets.Update(ctx, d.Id(), &options)
170199
if err != nil {
171-
return fmt.Errorf("Error updateing variable %s: %w", d.Id(), err)
200+
return fmt.Errorf("Error updating variable %s: %w", d.Id(), err)
172201
}
173202
}
174203

@@ -212,3 +241,19 @@ func resourceTFEVariableSetDelete(d *schema.ResourceData, meta interface{}) erro
212241
func warnWorkspaceIdsDeprecation() {
213242
log.Printf("[WARN] The workspace_ids field of tfe_variable_set is deprecated as of release 0.33.0 and may be removed in a future version. The preferred method of associating a variable set to a workspace is by using the tfe_workspace_variable_set resource.")
214243
}
244+
245+
func validateParentProjectID(d *schema.ResourceDiff) error {
246+
_, ok := d.GetOk("parent_project_id")
247+
if !ok {
248+
return nil
249+
}
250+
251+
// If parent_project_id is set, global must be false
252+
if global, ok := d.GetOk("global"); ok {
253+
if global.(bool) {
254+
return fmt.Errorf("global must be 'false' when setting parent_project_id")
255+
}
256+
}
257+
258+
return nil
259+
}

internal/provider/resource_tfe_variable_set_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,34 @@ func TestAccTFEVariableSet_import(t *testing.T) {
144144
})
145145
}
146146

147+
func TestAccTFEVariableSet_project_owned(t *testing.T) {
148+
skipUnlessBeta(t)
149+
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
150+
151+
resource.Test(t, resource.TestCase{
152+
PreCheck: func() { testAccPreCheck(t) },
153+
Providers: testAccProviders,
154+
CheckDestroy: testAccCheckTFEVariableSetDestroy,
155+
Steps: []resource.TestStep{
156+
{
157+
Config: testACCTFEVariableSet_ProjectOwned(rInt),
158+
Check: resource.ComposeTestCheckFunc(
159+
resource.TestCheckResourceAttrPair(
160+
"tfe_variable_set.project_owned", "parent_project_id", "tfe_project.foobar", "id"),
161+
),
162+
},
163+
164+
{
165+
Config: testACCTFEVariableSet_UpdateProjectOwned(rInt),
166+
Check: resource.ComposeTestCheckFunc(
167+
resource.TestCheckResourceAttrPair(
168+
"tfe_variable_set.project_owned", "parent_project_id", "tfe_project.updated", "id"),
169+
),
170+
},
171+
},
172+
})
173+
}
174+
147175
func testAccCheckTFEVariableSetExists(
148176
n string, variableSet *tfe.VariableSet) resource.TestCheckFunc {
149177
return func(s *terraform.State) error {
@@ -325,3 +353,49 @@ func testAccTFEVariableSet_update(rInt int) string {
325353
organization = tfe_organization.foobar.id
326354
}`, rInt)
327355
}
356+
357+
func testACCTFEVariableSet_ProjectOwned(rInt int) string {
358+
return fmt.Sprintf(`
359+
resource "tfe_organization" "foobar" {
360+
name = "tst-terraform-%d"
361+
362+
}
363+
364+
resource "tfe_project" "foobar" {
365+
organization = tfe_organization.foobar.id
366+
name = "tst-terraform-%d"
367+
}
368+
369+
resource "tfe_variable_set" "project_owned" {
370+
name = "project_owned_variable_set_test"
371+
description = "a project-owned test variable set"
372+
organization = tfe_organization.foobar.id
373+
parent_project_id = tfe_project.foobar.id
374+
}`, rInt, rInt)
375+
}
376+
377+
func testACCTFEVariableSet_UpdateProjectOwned(rInt int) string {
378+
return fmt.Sprintf(`
379+
resource "tfe_organization" "foobar" {
380+
name = "tst-terraform-%d"
381+
382+
}
383+
384+
resource "tfe_project" "foobar" {
385+
organization = tfe_organization.foobar.id
386+
name = "tst-terraform-%d"
387+
}
388+
389+
resource "tfe_project" "updated" {
390+
organization = tfe_organization.foobar.id
391+
name = "updated-%d"
392+
}
393+
394+
resource "tfe_variable_set" "project_owned" {
395+
name = "project_owned_variable_set_test"
396+
description = "a project-owned test variable set"
397+
organization = tfe_organization.foobar.id
398+
global = false
399+
parent_project_id = tfe_project.updated.id
400+
}`, rInt, rInt, rInt)
401+
}

website/docs/r/variable_set.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ The following arguments are supported:
139139
Must not be set if `global` is set. This argument is mutually exclusive with using the resource
140140
[tfe_workspace_variable_set](workspace_variable_set.html) which is the preferred method of associating a workspace
141141
with a variable set.
142+
* `parent_project_id` - (Optional) ID of the project that should own the variable set. If set, than the value of `global` must be `false`. This feature is currently in beta and is not available to all users.
142143

143144
## Attributes Reference
144145

0 commit comments

Comments
 (0)