Skip to content

Commit

Permalink
refactor: migrate regional endpoints and specs
Browse files Browse the repository at this point in the history
  • Loading branch information
ksol committed Feb 11, 2024
1 parent 48df436 commit 93aded5
Show file tree
Hide file tree
Showing 32 changed files with 362 additions and 886 deletions.
118 changes: 14 additions & 104 deletions lib/scalingo/regional/addons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,118 +2,28 @@

module Scalingo
class Regional::Addons < API::Endpoint
def for(app_id, headers = nil, &block)
data = nil

connection.get(
"apps/#{app_id}/addons",
data,
headers,
&block
)
end

def find(app_id, addon_id, headers = nil, &block)
data = nil

connection.get(
"apps/#{app_id}/addons/#{addon_id}",
data,
headers,
&block
)
end

def provision(app_id, payload = {}, headers = nil, &block)
data = {addon: payload}

connection.post(
"apps/#{app_id}/addons",
data,
headers,
&block
)
end

def update(app_id, addon_id, payload = {}, headers = nil, &block)
data = {addon: payload}

connection.patch(
"apps/#{app_id}/addons/#{addon_id}",
data,
headers,
&block
)
end

def destroy(app_id, addon_id, headers = nil, &block)
data = nil

connection.delete(
"apps/#{app_id}/addons/#{addon_id}",
data,
headers,
&block
)
end

def sso(app_id, addon_id, headers = nil, &block)
data = nil

connection.get(
"apps/#{app_id}/addons/#{addon_id}/sso",
data,
headers,
&block
)
end

def authenticate!(app_id, addon_id, headers = nil, &block)
response = token(app_id, addon_id, headers, &block)
get :for, "apps/{app_id}/addons"
get :find, "apps/{app_id}/addons/{id}"
post :provision, "apps/{app_id}/addons", root_key: :addon
patch :update, "apps/{app_id}/addons/{id}", root_key: :addon
delete :destroy, "apps/{app_id}/addons/{id}"
get :sso, "apps/{app_id}/addons/{id}/sso"
post :token, "apps/{app_id}/addons/{id}/token"
get :categories, "addon_categories", connected: false
get :providers, "addon_providers", connected: false

def authenticate!(**params, &block)
response = token(**params, &block)
return response unless response.status == 200

token = response.body[:token]
client.token_holder.authenticate_database_with_bearer_token(
addon_id,
token,
params[:id],
response.body[:token],
expires_at: Time.now + 1.hour,
raise_on_expired_token: client.config.raise_on_expired_token
)

response
end

def token(app_id, addon_id, headers = nil, &block)
data = nil

connection.post(
"apps/#{app_id}/addons/#{addon_id}/token",
data,
headers,
&block
)
end

def categories(headers = nil, &block)
data = nil

connection(fallback_to_guest: true).get(
"addon_categories",
data,
headers,
&block
)
end

def providers(headers = nil, &block)
data = nil

connection(fallback_to_guest: true).get(
"addon_providers",
data,
headers,
&block
)
end
end
end
96 changes: 10 additions & 86 deletions lib/scalingo/regional/apps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,92 +2,16 @@

module Scalingo
class Regional::Apps < API::Endpoint
def all(headers = nil, &block)
data = nil

connection.get(
"apps",
data,
headers,
&block
)
end

def find(id, headers = nil, &block)
data = nil

connection.get(
"apps/#{id}",
data,
headers,
&block
)
end

def create(payload = {}, headers = nil, &block)
data = {app: payload}

dry_run = !!(payload[:dry_run] || payload["dry_run"])

request_headers = {}
request_headers["X-Dry-Run"] = "true" if dry_run
request_headers.update(headers) if headers

connection.post(
"apps",
data,
request_headers,
&block
)
end

def update(id, payload = {}, headers = nil, &block)
data = {app: payload}

connection.patch(
"apps/#{id}",
data,
headers,
&block
)
end

def logs_url(id, headers = nil, &block)
data = nil

connection.get(
"apps/#{id}/logs",
data,
headers,
&block
)
end

def destroy(id, payload = {}, headers = nil, &block)
connection.delete(
"apps/#{id}",
payload,
headers,
&block
)
end

def rename(id, payload = {}, headers = nil, &block)
connection.post(
"apps/#{id}/rename",
payload,
headers,
&block
)
end

def transfer(id, payload = {}, headers = nil, &block)
connection.patch(
"apps/#{id}",
payload,
headers,
&block
)
get :all, "apps"
get :find, "apps/{id}"
get :logs_url, "apps/{id}/logs"
post :rename, "apps/{id}/rename"
patch :update, "apps/{id}", root_key: :app
patch :transfer, "apps/{id}"
delete :destroy, "apps/{id}"

post :create, "apps", root_key: :app do |req, params|
req.headers["X-Dry-Run"] = "true" if params[:dry_run]
end
end
end
59 changes: 5 additions & 54 deletions lib/scalingo/regional/autoscalers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,10 @@

module Scalingo
class Regional::Autoscalers < API::Endpoint
def for(app_id, headers = nil, &block)
data = nil

connection.get(
"apps/#{app_id}/autoscalers",
data,
headers,
&block
)
end

def find(app_id, autoscaler_id, headers = nil, &block)
data = nil

connection.get(
"apps/#{app_id}/autoscalers/#{autoscaler_id}",
data,
headers,
&block
)
end

def create(app_id, payload = {}, headers = nil, &block)
data = {autoscaler: payload}

connection.post(
"apps/#{app_id}/autoscalers",
data,
headers,
&block
)
end

def update(app_id, autoscaler_id, payload = {}, headers = nil, &block)
data = {autoscaler: payload}

connection.patch(
"apps/#{app_id}/autoscalers/#{autoscaler_id}",
data,
headers,
&block
)
end

def destroy(app_id, autoscaler_id, headers = nil, &block)
data = nil

connection.delete(
"apps/#{app_id}/autoscalers/#{autoscaler_id}",
data,
headers,
&block
)
end
get :for, "apps/{app_id}/autoscalers"
get :find, "apps/{app_id}/autoscalers/{id}"
post :create, "apps/{app_id}/autoscalers", root_key: :autoscaler
patch :update, "apps/{app_id}/autoscalers/{id}", root_key: :autoscaler
delete :destroy, "apps/{app_id}/autoscalers/{id}"
end
end
47 changes: 4 additions & 43 deletions lib/scalingo/regional/collaborators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,9 @@

module Scalingo
class Regional::Collaborators < API::Endpoint
def for(app_id, headers = nil, &block)
data = nil

connection.get(
"apps/#{app_id}/collaborators",
data,
headers,
&block
)
end

def destroy(app_id, collaborator_id, headers = nil, &block)
data = nil

connection.delete(
"apps/#{app_id}/collaborators/#{collaborator_id}",
data,
headers,
&block
)
end

def invite(app_id, payload = {}, headers = nil, &block)
data = {collaborator: payload}

connection.post(
"apps/#{app_id}/collaborators",
data,
headers,
&block
)
end

def accept(token, headers = nil, &block)
data = {token: token}

connection.get(
"apps/collaboration",
data,
headers,
&block
)
end
get :for, "apps/{app_id}/collaborators"
get :accept, "apps/collaboration", root_key: "token"
post :invite, "apps/{app_id}/collaborators", root_key: "collaborator"
delete :destroy, "apps/{app_id}/collaborators/{id}"
end
end
Loading

0 comments on commit 93aded5

Please sign in to comment.