Skip to content

Commit

Permalink
Merge branch 'develop' into fix/SCALRCORE-20543
Browse files Browse the repository at this point in the history
  • Loading branch information
soltysss committed May 17, 2022
2 parents 49f0210 + 4347446 commit 518c471
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
39 changes: 39 additions & 0 deletions workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ type Workspaces interface {

// Delete deletes a workspace by its ID.
Delete(ctx context.Context, workspaceID string) error

// SetSchedule sets run schedules for workspace.
SetSchedule(ctx context.Context, workspaceID string, options WorkspaceRunScheduleOptions) (*Workspace, error)
}

// workspaces implements Workspaces.
Expand Down Expand Up @@ -59,9 +62,12 @@ type Workspace struct {
TerraformVersion string `jsonapi:"attr,terraform-version"`
VCSRepo *WorkspaceVCSRepo `jsonapi:"attr,vcs-repo"`
WorkingDirectory string `jsonapi:"attr,working-directory"`
ApplySchedule string `jsonapi:"attr,apply-schedule"`
DestroySchedule string `jsonapi:"attr,destroy-schedule"`
HasResources bool `jsonapi:"attr,has-resources"`
Hooks *Hooks `jsonapi:"attr,hooks"`
RunOperationTimeout *int `jsonapi:"attr,run-operation-timeout"`
VarFiles []string `jsonapi:"attr,var-files"`

// Relations
CurrentRun *Run `jsonapi:"relation,current-run"`
Expand Down Expand Up @@ -119,6 +125,12 @@ type WorkspaceListOptions struct {
Include string `url:"include,omitempty"`
}

// WorkspaceRunScheduleOptions represents option for setting run schedules for workspace
type WorkspaceRunScheduleOptions struct {
ApplySchedule string `json:"apply-schedule"`
DestroySchedule string `json:"destroy-schedule"`
}

// List all the workspaces within an environment.
func (s *workspaces) List(ctx context.Context, options WorkspaceListOptions) (*WorkspaceList, error) {
req, err := s.client.newRequest("GET", "workspaces", &options)
Expand Down Expand Up @@ -178,6 +190,9 @@ type WorkspaceCreateOptions struct {
// Specifies the AgentPool for workspace.
AgentPool *AgentPool `jsonapi:"relation,agent-pool,omitempty"`

// Specifies the VarFiles for workspace.
VarFiles []string `jsonapi:"attr,var-files"`

// Specifies the ModuleVersion based on create workspace
ModuleVersion *ModuleVersion `jsonapi:"relation,module-version,omitempty"`

Expand Down Expand Up @@ -338,6 +353,9 @@ type WorkspaceUpdateOptions struct {
// Specifies the AgentPool for workspace.
AgentPool *AgentPool `jsonapi:"relation,agent-pool"`

//Specifies the VarFiles for workspace.
VarFiles []string `jsonapi:"attr,var_files"`

// Specifies the ModuleVersion based on create workspace
ModuleVersion *ModuleVersion `jsonapi:"relation,module-version"`

Expand Down Expand Up @@ -383,3 +401,24 @@ func (s *workspaces) Delete(ctx context.Context, workspaceID string) error {

return s.client.do(ctx, req, nil)
}

// SetSchedule set scheduled runs
func (s *workspaces) SetSchedule(ctx context.Context, workspaceID string, options WorkspaceRunScheduleOptions) (*Workspace, error) {
if !validStringID(&workspaceID) {
return nil, errors.New("invalid value for workspace ID")
}

u := fmt.Sprintf("workspaces/%s/actions/set-schedule", url.QueryEscape(workspaceID))
req, err := s.client.newJsonRequest("POST", u, &options)
if err != nil {
return nil, err
}

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

return w, nil
}
46 changes: 46 additions & 0 deletions workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,3 +445,49 @@ func TestWorkspacesDelete(t *testing.T) {
assert.EqualError(t, err, "invalid value for workspace ID")
})
}

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

envTest, envTestCleanup := createEnvironment(t, client)
defer envTestCleanup()

wTest, _ := createWorkspace(t, client, envTest)

t.Run("with valid options", func(t *testing.T) {
options := WorkspaceRunScheduleOptions{
ApplySchedule: "30 3 5 3-5 2",
DestroySchedule: "30 5 5 3-5 2",
}

w, err := client.Workspaces.SetSchedule(ctx, wTest.ID, options)
require.NoError(t, err)

// Get a refreshed view of the workspace from the API
refreshed, err := client.Workspaces.ReadByID(ctx, wTest.ID)
require.NoError(t, err)

for _, item := range []*Workspace{
w,
refreshed,
} {
assert.Equal(t, options.ApplySchedule, item.ApplySchedule)
assert.Equal(t, options.DestroySchedule, item.DestroySchedule)
}
})

t.Run("when an error is returned from the api", func(t *testing.T) {
w, err := client.Workspaces.SetSchedule(ctx, wTest.ID, WorkspaceRunScheduleOptions{
ApplySchedule: "bla-bla-bla",
})
assert.Nil(t, w)
assert.Error(t, err)
})

t.Run("without a valid workspace ID", func(t *testing.T) {
w, err := client.Workspaces.SetSchedule(ctx, badIdentifier, WorkspaceRunScheduleOptions{})
assert.Nil(t, w)
assert.EqualError(t, err, "invalid value for workspace ID")
})
}

0 comments on commit 518c471

Please sign in to comment.