Skip to content

Commit

Permalink
Re-order variable_set.go: types, receiver methods, validators
Browse files Browse the repository at this point in the history
  • Loading branch information
byronwolfman committed Apr 8, 2022
1 parent 1e64cb4 commit ba9fb5d
Showing 1 changed file with 115 additions and 115 deletions.
230 changes: 115 additions & 115 deletions variable_set.go
Expand Up @@ -78,38 +78,33 @@ type VariableSetListOptions struct {
Include string `url:"include"`
}

func (o *VariableSetListOptions) valid() error {
return nil
}
// VariableSetCreateOptions represents the options for creating a new variable set within in a organization.
type VariableSetCreateOptions 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,varsets"`

// List all Variable Sets in the organization
func (s *variableSets) List(ctx context.Context, organization string, options *VariableSetListOptions) (*VariableSetList, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
if options != nil {
if err := options.valid(); err != nil {
return nil, err
}
}
// The name of the variable set.
// Affects variable precedence when there are conflicts between Variable Sets
// https://www.terraform.io/cloud-docs/api-docs/variable-sets#apply-variable-set-to-workspaces
Name *string `jsonapi:"attr,name"`

u := fmt.Sprintf("organizations/%s/varsets", url.QueryEscape(organization))
req, err := s.client.newRequest("GET", u, options)
if err != nil {
return nil, err
}
// A description to provide context for the variable set.
Description *string `jsonapi:"attr,description,omitempty"`

vl := &VariableSetList{}
err = s.client.do(ctx, req, vl)
if err != nil {
return nil, err
}
// If true the variable set is considered in all runs in the organization.
Global *bool `jsonapi:"attr,global,omitempty"`
}

return vl, nil
// VariableSetReadOptions represents the options for reading variable sets.
type VariableSetReadOptions struct {
Include *[]VariableSetIncludeOpt `url:"include:omitempty"`
}

// VariableSetCreateOptions represents the options for creating a new variable set within in a organization.
type VariableSetCreateOptions struct {
// VariableSetUpdateOptions represents the options for updating a variable set.
type VariableSetUpdateOptions 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.
Expand All @@ -119,7 +114,7 @@ type VariableSetCreateOptions struct {
// The name of the variable set.
// Affects variable precedence when there are conflicts between Variable Sets
// https://www.terraform.io/cloud-docs/api-docs/variable-sets#apply-variable-set-to-workspaces
Name *string `jsonapi:"attr,name"`
Name *string `jsonapi:"attr,name,omitempty"`

// A description to provide context for the variable set.
Description *string `jsonapi:"attr,description,omitempty"`
Expand All @@ -128,17 +123,60 @@ type VariableSetCreateOptions struct {
Global *bool `jsonapi:"attr,global,omitempty"`
}

func (o *VariableSetCreateOptions) valid() error {
if o == nil {
return nil
// VariableSetApplyToWorkspacesOptions represents the options for applying variable sets to workspaces.
type VariableSetApplyToWorkspacesOptions struct {
// The workspaces to apply the variable set to (additive).
Workspaces []*Workspace
}

// VariableSetRemoveFromWorkspacesOptions represents the options for removing variable sets from workspaces.
type VariableSetRemoveFromWorkspacesOptions struct {
// The workspaces to remove the variable set from.
Workspaces []*Workspace
}

// 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,varsets"`

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

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

// List all Variable Sets in the organization
func (s *variableSets) List(ctx context.Context, organization string, options *VariableSetListOptions) (*VariableSetList, error) {
if !validStringID(&organization) {
return nil, ErrInvalidOrg
}
if !validString(o.Name) {
return ErrRequiredName
if options != nil {
if err := options.valid(); err != nil {
return nil, err
}
}
if o.Global == nil {
return ErrRequiredGlobalFlag

u := fmt.Sprintf("organizations/%s/varsets", url.QueryEscape(organization))
req, err := s.client.newRequest("GET", u, options)
if err != nil {
return nil, err
}
return nil

vl := &VariableSetList{}
err = s.client.do(ctx, req, vl)
if err != nil {
return nil, err
}

return vl, nil
}

// Create is used to create a new variable set.
Expand All @@ -165,11 +203,6 @@ func (s *variableSets) Create(ctx context.Context, organization string, options
return vl, nil
}

// VariableSetReadOptions represents the options for reading variable sets.
type VariableSetReadOptions struct {
Include *[]VariableSetIncludeOpt `url:"include:omitempty"`
}

// Read is used to inspect a given variable set based on ID
func (s *variableSets) Read(ctx context.Context, variableSetID string, options *VariableSetReadOptions) (*VariableSet, error) {
if !validStringID(&variableSetID) {
Expand All @@ -191,26 +224,6 @@ func (s *variableSets) Read(ctx context.Context, variableSetID string, options *
return vs, err
}

// VariableSetUpdateOptions represents the options for updating a variable set.
type VariableSetUpdateOptions 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,varsets"`

// The name of the variable set.
// Affects variable precedence when there are conflicts between Variable Sets
// https://www.terraform.io/cloud-docs/api-docs/variable-sets#apply-variable-set-to-workspaces
Name *string `jsonapi:"attr,name,omitempty"`

// A description to provide context for the variable set.
Description *string `jsonapi:"attr,description,omitempty"`

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

// Update an existing variable set.
func (s *variableSets) Update(ctx context.Context, variableSetID string, options *VariableSetUpdateOptions) (*VariableSet, error) {
if !validStringID(&variableSetID) {
Expand Down Expand Up @@ -247,21 +260,6 @@ func (s *variableSets) Delete(ctx context.Context, variableSetID string) error {
return s.client.do(ctx, req, nil)
}

// VariableSetApplyToWorkspacesOptions represents the options for applying variable sets to workspaces.
type VariableSetApplyToWorkspacesOptions struct {
// The workspaces to apply the variable set to (additive).
Workspaces []*Workspace
}

func (o *VariableSetApplyToWorkspacesOptions) valid() error {
for _, s := range o.Workspaces {
if !validStringID(&s.ID) {
return ErrRequiredWorkspaceID
}
}
return nil
}

// Apply variable set to workspaces in the supplied list.
// Note: this method will return an error if the variable set has global = true.
func (s *variableSets) ApplyToWorkspaces(ctx context.Context, variableSetID string, options *VariableSetApplyToWorkspacesOptions) error {
Expand All @@ -281,21 +279,6 @@ func (s *variableSets) ApplyToWorkspaces(ctx context.Context, variableSetID stri
return s.client.do(ctx, req, nil)
}

// VariableSetRemoveFromWorkspacesOptions represents the options for removing variable sets from workspaces.
type VariableSetRemoveFromWorkspacesOptions struct {
// The workspaces to remove the variable set from.
Workspaces []*Workspace
}

func (o *VariableSetRemoveFromWorkspacesOptions) valid() error {
for _, s := range o.Workspaces {
if !validStringID(&s.ID) {
return ErrRequiredWorkspaceID
}
}
return nil
}

// Remove variable set from workspaces in the supplied list.
// Note: this method will return an error if the variable set has global = true.
func (s *variableSets) RemoveFromWorkspaces(ctx context.Context, variableSetID string, options *VariableSetRemoveFromWorkspacesOptions) error {
Expand All @@ -315,31 +298,6 @@ func (s *variableSets) RemoveFromWorkspaces(ctx context.Context, variableSetID s
return s.client.do(ctx, req, nil)
}

// 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,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) UpdateWorkspaces(ctx context.Context, variableSetID string, options *VariableSetUpdateWorkspacesOptions) (*VariableSet, error) {
if err := options.valid(); err != nil {
Expand Down Expand Up @@ -367,3 +325,45 @@ func (s *variableSets) UpdateWorkspaces(ctx context.Context, variableSetID strin

return v, nil
}

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

func (o *VariableSetCreateOptions) valid() error {
if o == nil {
return nil
}
if !validString(o.Name) {
return ErrRequiredName
}
if o.Global == nil {
return ErrRequiredGlobalFlag
}
return nil
}

func (o *VariableSetApplyToWorkspacesOptions) valid() error {
for _, s := range o.Workspaces {
if !validStringID(&s.ID) {
return ErrRequiredWorkspaceID
}
}
return nil
}

func (o *VariableSetRemoveFromWorkspacesOptions) valid() error {
for _, s := range o.Workspaces {
if !validStringID(&s.ID) {
return ErrRequiredWorkspaceID
}
}
return nil
}

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

0 comments on commit ba9fb5d

Please sign in to comment.