Skip to content

Commit

Permalink
Rename, use pointers, validate, hide details
Browse files Browse the repository at this point in the history
  • Loading branch information
rexredinger committed Mar 23, 2022
1 parent 20889e3 commit 8ca362f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
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"`

// 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
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

0 comments on commit 8ca362f

Please sign in to comment.