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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable sets #305

Merged
merged 32 commits into from Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
cffa1a2
Add VariableSets
rexredinger Feb 7, 2022
be30a5e
Add VariableSetVariables, needs more tests
rexredinger Feb 7, 2022
3b76d5b
VariableSetVariable tests (not working yet)
rexredinger Feb 8, 2022
86c145f
Fix variableSetVariable tests
rexredinger Feb 10, 2022
20c32bf
Cleanup ws and go.sum
rexredinger Feb 10, 2022
f0c80c7
Remove TODOs
rexredinger Feb 10, 2022
95f8fc0
Use typed strings for include options
rexredinger Feb 15, 2022
a1990e6
Use option pointers in the 1.0 style
rexredinger Feb 15, 2022
30d11f1
fix lint complaint
rexredinger Feb 15, 2022
b3e6a26
recognize organizaiton relation for VariableSet
rexredinger Feb 17, 2022
79ddc1a
Fix varset interface typo
rexredinger Mar 2, 2022
d4e919d
Add VariablSetVariable Read function
rexredinger Mar 2, 2022
e815f86
Fix style issue in params
rexredinger Mar 15, 2022
b9513fc
omitempty for nonessential create params
rexredinger Mar 15, 2022
a351fa4
fix bad merge
rexredinger Mar 21, 2022
e946aae
Refactor to typed errors
rexredinger Mar 21, 2022
31b4676
Address more error typing and lint fixes
rexredinger Mar 21, 2022
ddf76d7
somehow I thought I already committed this
rexredinger Mar 21, 2022
566ce6f
variable set variable test fix
rexredinger Mar 21, 2022
30c245e
Remove latin that go lint hates
rexredinger Mar 21, 2022
716b4e4
Merge branch 'main' into variable_sets
rexredinger Mar 22, 2022
d7c7b8c
Update Assign documentation to be clearer
rexredinger Mar 22, 2022
fa9ea31
remove more latin from the lint trap
rexredinger Mar 22, 2022
2ca2ccf
Bring include opt usage inline with other resources
rexredinger Mar 22, 2022
6ab16fd
fix missing url.QueryEscape
rexredinger Mar 22, 2022
7ef83ee
Put that back. Can you tell I'm not firing on all cylinders
rexredinger Mar 22, 2022
6bd3ead
_Apply_ variable sets
rexredinger Mar 22, 2022
20889e3
typo
rexredinger Mar 22, 2022
8ca362f
Rename, use pointers, validate, hide details
rexredinger Mar 23, 2022
5bb1a6c
spelling~
rexredinger Mar 24, 2022
c67cf9b
spelling~
rexredinger Mar 24, 2022
9188b96
spelling~
rexredinger Mar 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 35 additions & 20 deletions variable_set.go
Expand Up @@ -30,7 +30,7 @@ type VariableSets interface {
Delete(ctx context.Context, variableSetID string) error

// Update list of workspaces to which the variable set is applied to match the supplied list
Apply(ctx context.Context, variableSetID string, options *VariableSetApplyOptions) (*VariableSet, error)
UpdateWorkspaces(ctx context.Context, variableSetID string, options *VariableSetUpdateWorkspacesOptions) (*VariableSet, error)
}

type variableSets struct {
Expand Down Expand Up @@ -68,7 +68,7 @@ type VariableSetListOptions struct {
Include string `url:"include"`
}

func (o VariableSetListOptions) valid() error {
func (o *VariableSetListOptions) valid() error {
return nil
}

Expand Down Expand Up @@ -104,7 +104,7 @@ type VariableSetCreateOptions struct {
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
// https://jsonapi.org/format/#crud-creating
Type string `jsonapi:"primary,vars"`
Type string `jsonapi:"primary,varsets"`
rexredinger marked this conversation as resolved.
Show resolved Hide resolved

// The name of the variable set.
// Affects variable precedence when there are conflicts between Variable Sets
Expand All @@ -118,7 +118,10 @@ type VariableSetCreateOptions struct {
Global *bool `jsonapi:"attr,global,omitempty"`
}

func (o VariableSetCreateOptions) valid() error {
func (o *VariableSetCreateOptions) valid() error {
if o == nil {
return nil
}
if !validString(o.Name) {
return ErrRequiredName
}
Expand All @@ -133,10 +136,8 @@ func (s *variableSets) Create(ctx context.Context, organization string, options
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
if options != nil {
if err := options.valid(); err != nil {
return nil, err
}
if err := options.valid(); err != nil {
return nil, err
}

u := fmt.Sprintf("organizations/%s/varsets", url.QueryEscape(organization))
Expand Down Expand Up @@ -185,7 +186,7 @@ type VariableSetUpdateOptions struct {
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
// https://jsonapi.org/format/#crud-creating
Type string `jsonapi:"primary,vars"`
Type string `jsonapi:"primary,varsets"`

// The name of the variable set.
// Affects variable precedence when there are conflicts between Variable Sets
Expand Down Expand Up @@ -234,32 +235,46 @@ func (s *variableSets) Delete(ctx context.Context, variableSetID string) error {
return s.client.do(ctx, req, nil)
}

// VariableSetApplyOptions represents a subset of update options specifically for applying variable sets to workspaces
type VariableSetApplyOptions struct {
// VariableSetUpdateWorkspacesOptions represents a subset of update options specifically for applying variable sets to workspaces
type VariableSetUpdateWorkspacesOptions struct {
// Type is a public field utilized by JSON:API to
// set the resource type via the field tag.
// It is not a user-defined value and does not need to be set.
// https://jsonapi.org/format/#crud-creating
Type string `jsonapi:"primary,vars"`

// Used to set the variable set from Global to not Global if necessary
Global *bool `jsonapi:"attr,global"`
Type string `jsonapi:"primary,varsets"`

// The workspaces to be applied to. An empty set means remove all applied
Workspaces []*Workspace `jsonapi:"relation,workspaces"`
}

func (o *VariableSetUpdateWorkspacesOptions) valid() error {
if o == nil || o.Workspaces == nil {
return ErrRequiredWorkspacesList
}
return nil
}

type privateVariableSetUpdateWorkspacesOptions struct {
Type string `jsonapi:"primary,varsets"`
Global bool `jsonapi:"attr,global"`
Workspaces []*Workspace `jsonapi:"relation,workspaces"`
}

// Update variable set to be applied to only the workspaces in the supplied list.
func (s *variableSets) Apply(ctx context.Context, variableSetID string, options *VariableSetApplyOptions) (*VariableSet, error) {
if options == nil || options.Workspaces == nil {
return nil, ErrRequiredWorkspacesList
func (s *variableSets) UpdateWorkspaces(ctx context.Context, variableSetID string, options *VariableSetUpdateWorkspacesOptions) (*VariableSet, error) {
if err := options.valid(); err != nil {
return nil, err
}

options.Global = Bool(false)
// Use private strcut to ensure global is set to false when applying to workspaces
rexredinger marked this conversation as resolved.
Show resolved Hide resolved
o := privateVariableSetUpdateWorkspacesOptions{
Global: bool(false),
Workspaces: options.Workspaces,
}

// We force inclusion of workspaces as that is the primary data for which we are concerned with confirming changes.
u := fmt.Sprintf("varsets/%s?include=%s", url.QueryEscape(variableSetID), VariableSetWorkspaces)
req, err := s.client.newRequest("PATCH", u, options)
req, err := s.client.newRequest("PATCH", u, &o)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions variable_set_test.go
Expand Up @@ -190,7 +190,7 @@ func TestVariableSetsDelete(t *testing.T) {
})
}

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

Expand All @@ -202,21 +202,21 @@ func TestVariableSetsApply(t *testing.T) {
wTest, _ := createWorkspace(t, client, orgTest)

t.Run("with valid workspaces", func(t *testing.T) {
options := VariableSetApplyOptions{
options := VariableSetUpdateWorkspacesOptions{
Workspaces: []*Workspace{wTest},
}

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

assert.Equal(t, len(options.Workspaces), len(vsAfter.Workspaces))
assert.Equal(t, options.Workspaces[0].ID, vsAfter.Workspaces[0].ID)

options = VariableSetApplyOptions{
options = VariableSetUpdateWorkspacesOptions{
Workspaces: []*Workspace{},
}

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

assert.Equal(t, len(options.Workspaces), len(vsAfter.Workspaces))
Expand Down