Skip to content

Commit 2b79df5

Browse files
committed
Adds warning when changing default stack state
Polishing based on review comments Signed-off-by: Rashed Kamal <[email protected]>
1 parent 569c2bd commit 2b79df5

File tree

9 files changed

+56
-17
lines changed

9 files changed

+56
-17
lines changed

app/actions/stack_update.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def update(stack, message)
1616
MetadataUpdate.update(stack, message)
1717
Repositories::StackEventRepository.new.record_stack_update(stack, @user_audit_info, message.audit_hash)
1818
end
19-
@logger.info("Finished updating metadata on stack #{stack.guid}")
19+
@logger.info("Finished updating stack #{stack.guid}")
2020

2121
stack
2222
rescue Sequel::ValidationFailed => e

app/controllers/runtime/restages_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def restage(guid)
3535
process.app.update(droplet_guid: nil)
3636
AppStart.start_without_event(process.app, create_revision: false)
3737
end
38-
# V2::AppStage.new(stagers: @stagers).stage(process)
38+
3939
app_stage = V2::AppStage.new(stagers: @stagers)
4040
app_stage.stage(process)
4141
app_stage.warnings.each { |warning| add_warning(warning) }

app/controllers/v3/stacks_controller.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ def update
5252
message = StackUpdateMessage.new(hashed_params[:body])
5353
unprocessable!(message.errors.full_messages) unless message.valid?
5454

55+
changing_default_stack_state = stack.default? && message.requested?(:state)
5556
stack = StackUpdate.new(user_audit_info).update(stack, message)
57+
add_warning_headers(["Changing state on the default stack '#{stack.name}' may affect new application deployments."]) if changing_default_stack_state
5658

5759
render status: :ok, json: Presenters::V3::StackPresenter.new(stack)
5860
end

app/models/runtime/stack.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def around_save
4444
def validate
4545
validates_presence :name
4646
validates_unique :name
47-
validates_includes StackStates::VALID_STATES, :state, allow_nil: true
47+
validates_includes StackStates::VALID_STATES, :state, allow_missing: true
4848
end
4949

5050
def before_destroy
@@ -116,13 +116,5 @@ def restricted?
116116
def disabled?
117117
state == StackStates::STACK_DISABLED
118118
end
119-
120-
def can_stage_new_app?
121-
!restricted? && !disabled?
122-
end
123-
124-
def can_restage_apps?
125-
!disabled?
126-
end
127119
end
128120
end

docs/v3/source/includes/api_resources/_stacks.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
"updated_at": "2018-11-09T22:43:28Z",
9191
"name": "my-stack",
9292
"description": "Here is my stack!",
93-
"state": "ACTIVE",
93+
"state": "DISABLED",
9494
"build_rootfs_image": "my-stack",
9595
"run_rootfs_image": "my-stack",
9696
"default": true,

docs/v3/source/includes/resources/stacks/_update.md.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ curl "https://api.example.org/v3/stacks/[guid]" \
99
-X PATCH \
1010
-H "Authorization: bearer [token]" \
1111
-H "Content-Type: application/json" \
12-
-d '{ "metadata": { "labels": { "key": "value" }, "annotations": {"note": "detailed information"}, "state": "DISABLED" }}'
12+
-d '{ "metadata": { "labels": { "key": "value" }, "annotations": {"note": "detailed information"}},"state":"ACTIVE"}'
1313

1414
```
1515

spec/request/builds_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@
292292
end
293293

294294
context 'first build for app' do
295-
it 'returns 201 and does not create the build' do
295+
it 'returns 201 and stages the app' do
296296
expect(app_model.builds_dataset.count).to eq(0)
297297

298298
post '/v3/builds', create_request.to_json, developer_headers
@@ -318,7 +318,7 @@
318318
VCAP::CloudController::BuildModel.make(app: app_model, state: VCAP::CloudController::BuildModel::STAGED_STATE)
319319
end
320320

321-
it 'returns 201 and does not create the build' do
321+
it 'returns 201 and stages the app' do
322322
expect(app_model.builds_dataset.count).to eq(1)
323323

324324
post '/v3/builds', create_request.to_json, developer_headers

spec/unit/controllers/v3/stacks_controller_spec.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,51 @@
513513
end
514514
end
515515

516+
describe 'default stack state change warning' do
517+
let(:stack_config_file) { File.join(Paths::FIXTURES, 'config/stacks.yml') }
518+
let(:default_stack_name) { 'default-stack-name' }
519+
520+
before do
521+
VCAP::CloudController::Stack.dataset.destroy
522+
VCAP::CloudController::Stack.configure(stack_config_file)
523+
set_current_user_as_admin
524+
end
525+
526+
context 'when changing state on the default stack' do
527+
let!(:default_stack) { VCAP::CloudController::Stack.make(name: default_stack_name) }
528+
529+
it 'returns a warning header with the stack name' do
530+
patch :update, params: { guid: default_stack.guid, state: 'DEPRECATED' }, as: :json
531+
532+
expect(response).to have_http_status(:ok)
533+
expect(response).to have_warning_message("Changing state on the default stack '#{default_stack_name}' may affect new application deployments.")
534+
end
535+
end
536+
537+
context 'when changing state on a non-default stack' do
538+
let!(:default_stack) { VCAP::CloudController::Stack.make(name: default_stack_name) }
539+
let!(:other_stack) { VCAP::CloudController::Stack.make(name: 'other-stack') }
540+
541+
it 'does not return a warning header' do
542+
patch :update, params: { guid: other_stack.guid, state: 'DEPRECATED' }, as: :json
543+
544+
expect(response).to have_http_status(:ok)
545+
expect(response.headers['X-Cf-Warnings']).to be_nil
546+
end
547+
end
548+
549+
context 'when updating metadata on the default stack without changing state' do
550+
let!(:default_stack) { VCAP::CloudController::Stack.make(name: default_stack_name) }
551+
552+
it 'does not return a warning header' do
553+
patch :update, params: { guid: default_stack.guid, metadata: { labels: { foo: 'bar' } } }, as: :json
554+
555+
expect(response).to have_http_status(:ok)
556+
expect(response.headers['X-Cf-Warnings']).to be_nil
557+
end
558+
end
559+
end
560+
516561
describe 'authorization' do
517562
it_behaves_like 'permissions endpoint' do
518563
let(:roles_to_http_responses) do

spec/unit/models/runtime/stack_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ module VCAP::CloudController
4343
expect(stack.errors[:state]).to include(:includes)
4444
end
4545

46-
it 'allows nil state' do
46+
it 'does not allow nil state' do
4747
stack = Stack.make
4848
stack.state = nil
49-
expect(stack).to be_valid
49+
expect(stack).not_to be_valid
5050
end
5151
end
5252
end

0 commit comments

Comments
 (0)