-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathservice_broker_app.rb
More file actions
125 lines (109 loc) · 3.69 KB
/
service_broker_app.rb
File metadata and controls
125 lines (109 loc) · 3.69 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require 'sinatra'
require 'json'
require 'yaml'
require_relative 'github_service_helper'
class ServiceBrokerApp < Sinatra::Base
#configure the Sinatra app
use Rack::Auth::Basic do |username, password|
credentials = self.app_settings.fetch("basic_auth")
username == credentials.fetch("username") and password == credentials.fetch("password")
end
#declare the routes used by the app
# CATALOG
get "/v2/catalog" do
content_type :json
self.class.app_settings.fetch("catalog").to_json
end
# PROVISION
put "/v2/service_instances/:id" do |id|
content_type :json
repo_name = repository_name(id)
begin
repo_url = github_service.create_github_repo(repo_name)
status 201
{"dashboard_url" => repo_url}.to_json
rescue GithubServiceHelper::RepoAlreadyExistsError
status 409
{"description" => "The repo #{repo_name} already exists in the GitHub account"}.to_json
rescue GithubServiceHelper::GithubUnreachableError
status 504
{"description" => "GitHub is not reachable"}.to_json
rescue GithubServiceHelper::GithubError => e
status 502
{"description" => e.message}.to_json
end
end
# BIND
put '/v2/service_instances/:instance_id/service_bindings/:id' do |instance_id, binding_id|
content_type :json
begin
credentials = github_service.create_github_deploy_key(repo_name: repository_name(instance_id), deploy_key_title: binding_id)
status 201
{"credentials" => credentials}.to_json
rescue GithubServiceHelper::GithubResourceNotFoundError
status 404
{"description" => "GitHub resource not found"}.to_json
rescue GithubServiceHelper::BindingAlreadyExistsError
status 409
{"description" => "The binding #{binding_id} already exists"}.to_json
rescue GithubServiceHelper::GithubUnreachableError
status 504
{"description" => "GitHub is not reachable"}.to_json
rescue GithubServiceHelper::GithubError => e
status 502
{"description" => e.message}.to_json
end
end
# UNBIND
delete '/v2/service_instances/:instance_id/service_bindings/:id' do |instance_id, binding_id|
content_type :json
begin
if github_service.remove_github_deploy_key(repo_name: repository_name(instance_id), deploy_key_title: binding_id)
status 200
else
status 410
end
{}.to_json
rescue GithubServiceHelper::GithubResourceNotFoundError
status 410
{}.to_json
rescue GithubServiceHelper::GithubUnreachableError
status 504
{"description" => "GitHub is not reachable"}.to_json
rescue GithubServiceHelper::GithubError => e
status 502
{"description" => e.message}.to_json
end
end
# UNPROVISION
delete '/v2/service_instances/:instance_id' do |instance_id|
content_type :json
begin
if github_service.delete_github_repo(repository_name(instance_id))
status 200
else
status 410
end
{}.to_json
rescue GithubServiceHelper::GithubUnreachableError
status 504
{"description" => "GitHub is not reachable"}.to_json
rescue GithubServiceHelper::GithubError => e
status 502
{"description" => e.message}.to_json
end
end
#helper methods
private
def repository_name(id)
"github-service-#{id}"
end
def self.app_settings
settings_filename = defined?(SETTINGS_FILENAME) ? SETTINGS_FILENAME : 'config/settings.yml'
@app_settings ||= YAML.load_file(settings_filename)
end
def github_service
github_credentials = self.class.app_settings.fetch("github")
GithubServiceHelper.new(github_credentials.fetch("username"), github_credentials.fetch("access_token"), github_credentials.fetch("ssh"))
end
end