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

TF-9286 Add the enforced attribute to variable_set #778

Merged
merged 3 commits into from
Sep 21, 2023
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<!-- Add CHANGELOG entry to this section for any PR awaiting the next release -->
<!-- Please also include if this is a Bug Fix, Enhancement, or Feature -->

## Enhancements
* Added BETA support for including `enforced` attribute to variable_set on create and update by @Netra2104 [#778](https://github.com/hashicorp/go-tfe/pull/778)

# v1.34.0
## Features
Expand Down
10 changes: 10 additions & 0 deletions variable_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ type VariableSet struct {
Name string `jsonapi:"attr,name"`
Description string `jsonapi:"attr,description"`
Global bool `jsonapi:"attr,global"`
// **Note: This field is still in BETA and subject to change.**
Enforced bool `jsonapi:"attr,enforced"`

// Relations
Organization *Organization `jsonapi:"relation,organization"`
Expand Down Expand Up @@ -113,6 +115,10 @@ type VariableSetCreateOptions struct {

// If true the variable set is considered in all runs in the organization.
Global *bool `jsonapi:"attr,global,omitempty"`

// **Note: This field is still in BETA and subject to change.**
// If true the variables in the set cannot be overwritten and take precedence.
Enforced *bool `jsonapi:"attr,enforced,omitempty"`
}

// VariableSetReadOptions represents the options for reading variable sets.
Expand All @@ -138,6 +144,10 @@ type VariableSetUpdateOptions struct {

// If true the variable set is considered in all runs in the organization.
Global *bool `jsonapi:"attr,global,omitempty"`

// **Note: This field is still in BETA and subject to change.**
// If true the variables in the set cannot be overwritten and take precedence.
Enforced *bool `jsonapi:"attr,enforced,omitempty"`
}

// VariableSetApplyToWorkspacesOptions represents the options for applying variable sets to workspaces.
Expand Down
96 changes: 96 additions & 0 deletions variable_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,58 @@ func TestVariableSetsCreate(t *testing.T) {
})
}

func TestVariableSetsWithEnforcedCreate(t *testing.T) {
skipUnlessBeta(t)
client := testClient(t)
ctx := context.Background()

orgTest, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)

t.Run("with valid options", func(t *testing.T) {
options := VariableSetCreateOptions{
Name: String("varset"),
Description: String("a variable set"),
Global: Bool(false),
Enforced: Bool(false),
}

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.Enforced, item.Enforced)
}
})

t.Run("when options is missing name", func(t *testing.T) {
vs, err := client.VariableSets.Create(ctx, "foo", &VariableSetCreateOptions{
Global: Bool(true),
})
assert.Nil(t, vs)
assert.EqualError(t, err, ErrRequiredName.Error())
})

t.Run("when options is missing global flag", func(t *testing.T) {
vs, err := client.VariableSets.Create(ctx, "foo", &VariableSetCreateOptions{
Name: String("foo"),
})
assert.Nil(t, vs)
assert.EqualError(t, err, ErrRequiredGlobalFlag.Error())
})
}

func TestVariableSetsRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
Expand Down Expand Up @@ -270,6 +322,50 @@ func TestVariableSetsUpdate(t *testing.T) {
})
}

func TestVariableSetsUpdateWithEnforced(t *testing.T) {
skipUnlessBeta(t)
Netra2104 marked this conversation as resolved.
Show resolved Hide resolved
client := testClient(t)
ctx := context.Background()

orgTest, orgTestCleanup := createOrganization(t, client)
t.Cleanup(orgTestCleanup)

vsTest, _ := createVariableSet(t, client, orgTest, VariableSetCreateOptions{
Name: String("OriginalName"),
Description: String("Original Description"),
Global: Bool(false),
Enforced: Bool(false),
})

t.Run("when updating a subset of values", func(t *testing.T) {
options := VariableSetUpdateOptions{
Name: String("UpdatedName"),
Description: String("Updated Description"),
Global: Bool(true),
Enforced: Bool(true),
}

vsAfter, err := client.VariableSets.Update(ctx, vsTest.ID, &options)
require.NoError(t, err)

assert.Equal(t, *options.Name, vsAfter.Name)
assert.Equal(t, *options.Description, vsAfter.Description)
assert.Equal(t, *options.Global, vsAfter.Global)
assert.Equal(t, *options.Enforced, vsAfter.Enforced)
})

t.Run("when options has an invalid variable set ID", func(t *testing.T) {
vsAfter, err := client.VariableSets.Update(ctx, badIdentifier, &VariableSetUpdateOptions{
Name: String("UpdatedName"),
Description: String("Updated Description"),
Global: Bool(true),
Enforced: Bool(true),
})
assert.Nil(t, vsAfter)
assert.EqualError(t, err, ErrInvalidVariableSetID.Error())
})
}

func TestVariableSetsDelete(t *testing.T) {
client := testClient(t)
ctx := context.Background()
Expand Down
Loading