forked from heroku/sudo-sandwich
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresources_controller.rb
78 lines (66 loc) · 1.79 KB
/
resources_controller.rb
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
module Heroku
class ResourcesController < ApplicationController
def create
if plan == Sandwich::BASE_PLAN # synchronous provisioning
message = 'Thanks for using Sudo Sandwich. Your add-on is available for use immediately!'
status = 200
state = 'provisioned'
payload = {
config: {
SUDO_SANDWICH_COMMAND: Sandwich::PLAN_CONFIG[plan],
}
}
else # async provisioning
message = 'Sudo Sandwich is being provisioned. It will be available shortly.'
status = 202
state = 'provisioning'
end
sandwich = create_sandwich(state)
enqueue_token_exchange_job(sandwich)
render(
json: {
id: heroku_uuid,
message: message
}.merge(payload || {}),
status: status
)
end
def update
sandwich = Sandwich.find_by(heroku_uuid: params[:id])
original_plan = sandwich.plan
new_plan = params[:plan]
sandwich.update!(plan: new_plan)
render json: {
config: {
SUDO_SANDWICH_COMMAND: Sandwich::PLAN_CONFIG[new_plan]
},
message: "Successfully changed from #{original_plan} to #{new_plan}"
}
end
def destroy
Sandwich.find_by(heroku_uuid: params[:id]).destroy!
render status: 204
end
private
def create_sandwich(state)
Sandwich.create!(
heroku_uuid: heroku_uuid,
oauth_grant_code: oauth_grant_code,
plan: plan,
state: state,
)
end
def enqueue_token_exchange_job(sandwich)
ExchangeGrantTokenJob.perform_later(sandwich_id: sandwich.id)
end
def heroku_uuid
params[:uuid]
end
def oauth_grant_code
params[:oauth_grant][:code]
end
def plan
params[:plan]
end
end
end