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

[BETA] Create a project-owned varset #992

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Add support for filtering by key/value tags by @brandonc [#987](https://github.com/hashicorp/go-tfe/pull/987)
* Add support for reading a registry module by its unique identifier by @dsa0x
[#988](https://github.com/hashicorp/go-tfe/pull/988)
* Adds BETA support for a variable set `Parent` relation, which is EXPERIMENTAL, SUBJECT TO CHANGE, and may not be available to all users by @jbonhag [#992](https://github.com/hashicorp/go-tfe/pull/992)

# v1.68.0

Expand Down
22 changes: 18 additions & 4 deletions variable_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ type VariableSetList struct {
Items []*VariableSet
}

// Parent represents the variable set's parent (currently only organizations and projects are supported).
// This relation is considered BETA, SUBJECT TO CHANGE, and likely unavailable to most users.
type Parent struct {
Organization *Organization
Project *Project
}

// VariableSet represents a Terraform Enterprise variable set.
type VariableSet struct {
ID string `jsonapi:"primary,varsets"`
Expand All @@ -74,10 +81,13 @@ type VariableSet struct {
Priority bool `jsonapi:"attr,priority"`

// Relations
Organization *Organization `jsonapi:"relation,organization"`
Workspaces []*Workspace `jsonapi:"relation,workspaces,omitempty"`
Projects []*Project `jsonapi:"relation,projects,omitempty"`
Variables []*VariableSetVariable `jsonapi:"relation,vars,omitempty"`
Organization *Organization `jsonapi:"relation,organization"`
// Optional: Parent represents the variable set's parent (currently only organizations and projects are supported).
// This relation is considered BETA, SUBJECT TO CHANGE, and likely unavailable to most users.
Parent *Parent `jsonapi:"polyrelation,parent"`
Workspaces []*Workspace `jsonapi:"relation,workspaces,omitempty"`
Projects []*Project `jsonapi:"relation,projects,omitempty"`
Variables []*VariableSetVariable `jsonapi:"relation,vars,omitempty"`
}

// A list of relations to include. See available resources
Expand Down Expand Up @@ -122,6 +132,10 @@ type VariableSetCreateOptions struct {
// If true the variables in the set override any other variable values set
// in a more specific scope including values set on the command line.
Priority *bool `jsonapi:"attr,priority,omitempty"`

// Optional: Parent represents the variable set's parent (currently only organizations and projects are supported).
// This relation is considered BETA, SUBJECT TO CHANGE, and likely unavailable to most users.
Parent *Parent `jsonapi:"polyrelation,parent"`
}

// VariableSetReadOptions represents the options for reading variable sets.
Expand Down
43 changes: 43 additions & 0 deletions variable_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,40 @@ func TestVariableSetsCreate(t *testing.T) {
assert.Nil(t, vs)
assert.EqualError(t, err, ErrRequiredGlobalFlag.Error())
})

t.Run("when creating project-owned variable set", func(t *testing.T) {
skipUnlessBeta(t)

prjTest, prjTestCleanup := createProject(t, client, orgTest)
t.Cleanup(prjTestCleanup)

options := VariableSetCreateOptions{
Name: String("project-varset"),
Description: String("a project variable set"),
Global: Bool(false),
Parent: &Parent{
Project: prjTest,
},
}

vs, err := client.VariableSets.Create(ctx, orgTest.Name, &options)
require.NoError(t, err)

// Get refreshed view from the API
refreshed, err := client.VariableSets.Read(ctx, vs.ID, nil)
require.NoError(t, err)

for _, item := range []*VariableSet{
vs,
refreshed,
} {
assert.NotEmpty(t, item.ID)
assert.Equal(t, *options.Name, item.Name)
assert.Equal(t, *options.Description, item.Description)
assert.Equal(t, *options.Global, item.Global)
assert.Equal(t, options.Parent.Project.ID, item.Parent.Project.ID)
}
})
}

func TestVariableSetsRead(t *testing.T) {
Expand All @@ -255,6 +289,15 @@ func TestVariableSetsRead(t *testing.T) {
assert.Nil(t, vs)
assert.Error(t, err)
})

t.Run("with parent relationship", func(t *testing.T) {
skipUnlessBeta(t)

vs, err := client.VariableSets.Read(ctx, vsTest.ID, nil)
require.NoError(t, err)
assert.Equal(t, vsTest, vs)
assert.Equal(t, orgTest.Name, vs.Parent.Organization.Name)
})
}

func TestVariableSetsUpdate(t *testing.T) {
Expand Down
Loading