Skip to content

Commit

Permalink
Merged automatically by CI pipeline
Browse files Browse the repository at this point in the history
SCALRCORE-25441 Deletion protection
  • Loading branch information
emocharnik committed May 8, 2023
2 parents 9bc6442 + 284b0e3 commit 5d0d321
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 45 deletions.
53 changes: 30 additions & 23 deletions workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,30 @@ type WorkspaceList struct {

// Workspace represents a Scalr workspace.
type Workspace struct {
ID string `jsonapi:"primary,workspaces"`
Actions *WorkspaceActions `jsonapi:"attr,actions"`
AutoApply bool `jsonapi:"attr,auto-apply"`
ForceLatestRun bool `jsonapi:"attr,force-latest-run"`
CanQueueDestroyPlan bool `jsonapi:"attr,can-queue-destroy-plan"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
FileTriggersEnabled bool `jsonapi:"attr,file-triggers-enabled"`
Locked bool `jsonapi:"attr,locked"`
MigrationEnvironment string `jsonapi:"attr,migration-environment"`
Name string `jsonapi:"attr,name"`
Operations bool `jsonapi:"attr,operations"`
ExecutionMode WorkspaceExecutionMode `jsonapi:"attr,execution-mode"`
Permissions *WorkspacePermissions `jsonapi:"attr,permissions"`
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"`
AutoQueueRuns WorkspaceAutoQueueRuns `jsonapi:"attr,auto-queue-runs"`
Hooks *Hooks `jsonapi:"attr,hooks"`
RunOperationTimeout *int `jsonapi:"attr,run-operation-timeout"`
VarFiles []string `jsonapi:"attr,var-files"`
ID string `jsonapi:"primary,workspaces"`
Actions *WorkspaceActions `jsonapi:"attr,actions"`
AutoApply bool `jsonapi:"attr,auto-apply"`
ForceLatestRun bool `jsonapi:"attr,force-latest-run"`
DeletionProtectionEnabled bool `jsonapi:"attr,deletion-protection-enabled"`
CanQueueDestroyPlan bool `jsonapi:"attr,can-queue-destroy-plan"`
CreatedAt time.Time `jsonapi:"attr,created-at,iso8601"`
FileTriggersEnabled bool `jsonapi:"attr,file-triggers-enabled"`
Locked bool `jsonapi:"attr,locked"`
MigrationEnvironment string `jsonapi:"attr,migration-environment"`
Name string `jsonapi:"attr,name"`
Operations bool `jsonapi:"attr,operations"`
ExecutionMode WorkspaceExecutionMode `jsonapi:"attr,execution-mode"`
Permissions *WorkspacePermissions `jsonapi:"attr,permissions"`
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"`
AutoQueueRuns WorkspaceAutoQueueRuns `jsonapi:"attr,auto-queue-runs"`
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 @@ -189,6 +190,9 @@ type WorkspaceCreateOptions struct {
// Whether to automatically raise the priority of the latest new run.
ForceLatestRun *bool `jsonapi:"attr,force-latest-run,omitempty"`

// Whether to prevent deletion when the workspace has resources.
DeletionProtectionEnabled *bool `jsonapi:"attr,deletion-protection-enabled,omitempty"`

// The name of the workspace, which can only include letters, numbers, -,
// and _. This will be used as an identifier and must be unique in the
// environment.
Expand Down Expand Up @@ -360,6 +364,9 @@ type WorkspaceUpdateOptions struct {
// Whether to automatically raise the priority of the latest new run.
ForceLatestRun *bool `jsonapi:"attr,force-latest-run,omitempty"`

// Whether to prevent deletion when the workspace has resources.
DeletionProtectionEnabled *bool `jsonapi:"attr,deletion-protection-enabled,omitempty"`

// A new name for the workspace, which can only include letters, numbers, -,
// and _. This will be used as an identifier and must be unique in the
// environment. Warning: Changing a workspace's name changes its URL in the
Expand Down
50 changes: 28 additions & 22 deletions workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,16 @@ func TestWorkspacesCreate(t *testing.T) {

t.Run("with valid options", func(t *testing.T) {
options := WorkspaceCreateOptions{
Environment: envTest,
Name: String(randomString(t)),
AutoApply: Bool(true),
ForceLatestRun: Bool(true),
ExecutionMode: WorkspaceExecutionModePtr(WorkspaceExecutionModeRemote),
TerraformVersion: String("1.1.9"),
WorkingDirectory: String("bar/"),
RunOperationTimeout: Int(15),
AutoQueueRuns: AutoQueueRunsModePtr(AutoQueueRunsModeNever),
Environment: envTest,
Name: String(randomString(t)),
AutoApply: Bool(true),
ForceLatestRun: Bool(true),
DeletionProtectionEnabled: Bool(false),
ExecutionMode: WorkspaceExecutionModePtr(WorkspaceExecutionModeRemote),
TerraformVersion: String("1.1.9"),
WorkingDirectory: String("bar/"),
RunOperationTimeout: Int(15),
AutoQueueRuns: AutoQueueRunsModePtr(AutoQueueRunsModeNever),
}

ws, err := client.Workspaces.Create(ctx, options)
Expand All @@ -121,6 +122,7 @@ func TestWorkspacesCreate(t *testing.T) {
assert.Equal(t, *options.Name, item.Name)
assert.Equal(t, *options.AutoApply, item.AutoApply)
assert.Equal(t, *options.ForceLatestRun, item.ForceLatestRun)
assert.Equal(t, *options.DeletionProtectionEnabled, item.DeletionProtectionEnabled)
assert.Equal(t, false, item.HasResources)
assert.Equal(t, *options.ExecutionMode, item.ExecutionMode)
assert.Equal(t, *options.TerraformVersion, item.TerraformVersion)
Expand Down Expand Up @@ -295,13 +297,14 @@ func TestWorkspacesUpdate(t *testing.T) {

t.Run("when updating a subset of values", func(t *testing.T) {
options := WorkspaceUpdateOptions{
Name: String(wsTest.Name),
AutoApply: Bool(true),
ForceLatestRun: Bool(true),
ExecutionMode: WorkspaceExecutionModePtr(WorkspaceExecutionModeRemote),
TerraformVersion: String("1.2.9"),
RunOperationTimeout: Int(20),
AutoQueueRuns: AutoQueueRunsModePtr(AutoQueueRunsModeAlways),
Name: String(wsTest.Name),
AutoApply: Bool(true),
ForceLatestRun: Bool(true),
DeletionProtectionEnabled: Bool(false),
ExecutionMode: WorkspaceExecutionModePtr(WorkspaceExecutionModeRemote),
TerraformVersion: String("1.2.9"),
RunOperationTimeout: Int(20),
AutoQueueRuns: AutoQueueRunsModePtr(AutoQueueRunsModeAlways),
}

wsAfter, err := client.Workspaces.Update(ctx, wsTest.ID, options)
Expand All @@ -312,6 +315,7 @@ func TestWorkspacesUpdate(t *testing.T) {
assert.Equal(t, *options.AutoQueueRuns, wsAfter.AutoQueueRuns)
assert.NotEqual(t, wsTest.AutoApply, wsAfter.AutoApply)
assert.NotEqual(t, wsTest.ForceLatestRun, wsAfter.ForceLatestRun)
assert.NotEqual(t, wsTest.DeletionProtectionEnabled, wsAfter.DeletionProtectionEnabled)
assert.NotEqual(t, wsTest.TerraformVersion, wsAfter.TerraformVersion)
assert.Equal(t, wsTest.WorkingDirectory, wsAfter.WorkingDirectory)
assert.Equal(t, int(20), *wsAfter.RunOperationTimeout)
Expand Down Expand Up @@ -340,12 +344,13 @@ func TestWorkspacesUpdate(t *testing.T) {

t.Run("with valid options", func(t *testing.T) {
options := WorkspaceUpdateOptions{
Name: String(randomString(t)),
AutoApply: Bool(false),
ForceLatestRun: Bool(false),
ExecutionMode: WorkspaceExecutionModePtr(WorkspaceExecutionModeLocal),
TerraformVersion: String("1.1.9"),
WorkingDirectory: String("baz/"),
Name: String(randomString(t)),
AutoApply: Bool(false),
ForceLatestRun: Bool(false),
DeletionProtectionEnabled: Bool(false),
ExecutionMode: WorkspaceExecutionModePtr(WorkspaceExecutionModeLocal),
TerraformVersion: String("1.1.9"),
WorkingDirectory: String("baz/"),
}

w, err := client.Workspaces.Update(ctx, wsTest.ID, options)
Expand All @@ -362,6 +367,7 @@ func TestWorkspacesUpdate(t *testing.T) {
assert.Equal(t, *options.Name, item.Name)
assert.Equal(t, *options.AutoApply, item.AutoApply)
assert.Equal(t, *options.ForceLatestRun, item.ForceLatestRun)
assert.Equal(t, *options.DeletionProtectionEnabled, item.DeletionProtectionEnabled)
assert.Equal(t, *options.ExecutionMode, item.ExecutionMode)
assert.Equal(t, *options.TerraformVersion, item.TerraformVersion)
assert.Equal(t, *options.WorkingDirectory, item.WorkingDirectory)
Expand Down

0 comments on commit 5d0d321

Please sign in to comment.