-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathstacks_controller.rb
More file actions
100 lines (76 loc) · 3.07 KB
/
stacks_controller.rb
File metadata and controls
100 lines (76 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
require 'presenters/v3/stack_presenter'
require 'actions/stack_create'
require 'actions/stack_delete'
require 'actions/stack_update'
require 'messages/stack_create_message'
require 'messages/stack_update_message'
require 'messages/stacks_list_message'
require 'messages/stack_apps_list_message'
require 'fetchers/stack_list_fetcher'
class StacksController < ApplicationController
def index
message = StacksListMessage.from_params(query_params)
invalid_param!(message.errors.full_messages) unless message.valid?
dataset = StackListFetcher.fetch_all(message)
render status: :ok, json: Presenters::V3::PaginatedListPresenter.new(
presenter: Presenters::V3::StackPresenter,
paginated_result: SequelPaginator.new.get_page(dataset, message.try(:pagination_options)),
path: '/v3/stacks',
message: message
)
end
def show
stack = Stack.find(guid: hashed_params[:guid])
stack_not_found! unless stack
render status: :ok, json: Presenters::V3::StackPresenter.new(stack)
end
def create
unauthorized! unless permission_queryer.can_write_globally?
message = StackCreateMessage.new(hashed_params[:body])
unprocessable!(message.errors.full_messages) unless message.valid?
stack = StackCreate.new(user_audit_info).create(message)
render status: :created, json: Presenters::V3::StackPresenter.new(stack)
rescue StackCreate::Error => e
unprocessable! e
end
def update
stack = Stack.find(guid: hashed_params[:guid])
stack_not_found! unless stack
unauthorized! unless permission_queryer.can_write_globally?
message = StackUpdateMessage.new(hashed_params[:body])
unprocessable!(message.errors.full_messages) unless message.valid?
stack = StackUpdate.new(user_audit_info).update(stack, message)
render status: :ok, json: Presenters::V3::StackPresenter.new(stack)
end
def show_apps
stack = Stack.find(guid: hashed_params[:guid])
stack_not_found! unless stack
message = StackAppsListMessage.from_params(query_params, stack_name: stack.name)
invalid_param!(message.errors.full_messages) unless message.valid?
dataset = if permission_queryer.can_read_globally?
AppListFetcher.fetch_all(message)
else
AppListFetcher.fetch(message, permission_queryer.readable_space_guids)
end
render status: :ok, json: Presenters::V3::PaginatedListPresenter.new(
presenter: Presenters::V3::AppPresenter,
paginated_result: SequelPaginator.new.get_page(dataset, message.try(:pagination_options)),
path: "/v3/stacks/#{hashed_params[:guid]}/apps"
)
end
def destroy
stack = Stack.find(guid: hashed_params[:guid])
stack_not_found! unless stack
unauthorized! unless permission_queryer.can_write_globally?
begin
StackDelete.new(user_audit_info).delete(stack)
rescue Stack::AppsStillPresentError
unprocessable! "Cannot delete stack '#{stack.name}' because apps are currently using the stack."
end
head :no_content
end
private
def stack_not_found!
resource_not_found!(:stack)
end
end