Skip to content

Commit 7e6fb9a

Browse files
author
Shirshendu Mukherjee
committed
WIP
1 parent 296a352 commit 7e6fb9a

File tree

16 files changed

+294
-179
lines changed

16 files changed

+294
-179
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ gem 'activemodel', '4.2.6', require: 'active_model'
99
gem 'rake', '0.9.6'
1010
gem 'puma', '3.6.0'
1111
gem 'bcrypt', '3.1.11'
12+
gem 'httparty', '0.15.6'
13+
gem 'jwt', '1.5.6'
1214

1315
group :development do
1416
gem 'rubocop', require: false

Gemfile.lock

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,17 @@ GEM
2727
hashdiff (0.3.1)
2828
http-cookie (1.0.3)
2929
domain_name (~> 0.5)
30+
httparty (0.15.6)
31+
multi_xml (>= 0.5.2)
3032
i18n (0.7.0)
3133
json (1.8.3)
34+
jwt (1.5.6)
3235
mime-types (3.1)
3336
mime-types-data (~> 3.2015)
3437
mime-types-data (3.2016.0521)
3538
minitest (5.9.1)
3639
mixlib-log (1.7.1)
40+
multi_xml (0.6.0)
3741
netrc (0.11.0)
3842
parser (2.3.1.4)
3943
ast (~> 2.2)
@@ -107,6 +111,8 @@ DEPENDENCIES
107111
asciidoctor
108112
bcrypt (= 3.1.11)
109113
etcd (= 0.3.0)
114+
httparty (= 0.15.6)
115+
jwt (= 1.5.6)
110116
puma (= 3.6.0)
111117
rack-test
112118
rake (= 0.9.6)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class ActionsController < AuthenticatedUsersController
2+
before '/clusters/:cluster_id/?*?' do
3+
begin
4+
@cluster = Tendrl::Cluster.find(params[:cluster_id])
5+
rescue Etcd::KeyNotFound => e
6+
e = Tendrl::HttpResponseErrorHandler.new(
7+
e, cause: '/clusters/id', object_id: params[:cluster_id]
8+
)
9+
halt e.status, e.body.to_json
10+
end
11+
end
12+
13+
get '/clusters/:cluster_id' do
14+
ClusterPresenter.single(Tendrl::Cluster.find(params[:cluster_id])).to_json
15+
end
16+
17+
post '/clusters/:cluster_id/jobs' do
18+
HTTParty.post("http://localhost:8000/clusters/#{cluster_id}/jobs", body: { shell: 'echo "foobar"'}.to_json)
19+
end
20+
end

app/controllers/application_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ class ApplicationController < Sinatra::Base
7979
settings.http_allow_headers.join(',')
8080
end
8181

82+
helpers do
83+
def parsed_body
84+
@body ||= request.body.read
85+
JSON.parse(@body)
86+
end
87+
end
88+
8289
options '*' do
8390
status 200
8491
end

app/controllers/clusters_controller.rb

Lines changed: 121 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -4,148 +4,144 @@ class ClustersController < AuthenticatedUsersController
44
{ clusters: ClusterPresenter.list(clusters) }.to_json
55
end
66

7-
get '/clusters/:cluster_id' do
8-
cluster = Tendrl::Cluster.find(params[:cluster_id])
9-
status 200
10-
ClusterPresenter.single(
11-
params[:cluster_id] => cluster.attributes
12-
).to_json
7+
before '/clusters/:cluster_id/?*?' do
8+
@cluster = Tendrl::Cluster.new(params[:cluster_id])
9+
#@cluster.gd2.get("/ping")
1310
end
1411

15-
get '/clusters/:cluster_id/nodes' do
16-
nodes = Tendrl::Node.find_all_by_cluster_id(params[:cluster_id])
17-
node_list = NodePresenter.list(nodes).map do |node_data|
18-
bricks = Tendrl::Brick.find_all_by_cluster_id_and_node_fqdn(
19-
params[:cluster_id], node_data['fqdn']
20-
)
21-
node_data.merge(bricks_count: bricks.size)
22-
end
23-
{ nodes: node_list }.to_json
12+
get '/clusters/:cluster_id' do
13+
state = @cluster.gd2.state
14+
state.to_json
2415
end
2516

26-
get '/clusters/:cluster_id/nodes/:node_id/bricks' do
27-
node = Tendrl::Node.find_by_cluster_id(
28-
params[:cluster_id], params[:node_id]
29-
)
30-
halt 404 unless node.present?
31-
bricks = Tendrl::Brick.find_all_by_cluster_id_and_node_fqdn(
32-
params[:cluster_id], node['fqdn']
33-
)
34-
{ bricks: BrickPresenter.list(bricks) }.to_json
17+
get '/clusters/:cluster_id/peers' do
18+
peers = @cluster.gd2.peers
19+
{ peers: peers }.to_json
3520
end
3621

3722
get '/clusters/:cluster_id/volumes' do
38-
volumes = Tendrl::Volume.find_all_by_cluster_id(params[:cluster_id])
39-
{ volumes: VolumePresenter.list(volumes) }.to_json
23+
volumes = @cluster.gd2.volumes
24+
{ volumes: volumes }.to_json
4025
end
4126

42-
get '/clusters/:cluster_id/volumes/:volume_id/bricks' do
43-
references = Tendrl::Brick.find_refs_by_cluster_id_and_volume_id(
44-
params[:cluster_id], params[:volume_id]
45-
)
46-
bricks = Tendrl::Brick.find_by_cluster_id_and_refs(params[:cluster_id], references)
47-
{ bricks: BrickPresenter.list(bricks) }.to_json
27+
post '/import' do
28+
new_endpoint = {
29+
gd2_url: parsed_body['gd2_url'],
30+
secret: parsed_body['secret']
31+
}
32+
gd2 = Gd2Client.new new_endpoint
33+
state = gd2.state
34+
cluster = Tendrl::Cluster.new state['cluster-id']
35+
unless cluster.endpoints.include? new_endpoint
36+
Tendrl.etcd.create_in_order(
37+
"/clusters/#{state['cluster-id']}/endpoints",
38+
value: new_endpoint.to_json
39+
)
40+
end
41+
status 201
42+
state.merge(endpoints: cluster.endpoints).to_json
4843
end
4944

50-
get '/clusters/:cluster_id/notifications' do
51-
notifications = Tendrl::Notification.all
52-
NotificationPresenter.list_by_integration_id(notifications, params[:cluster_id]).to_json
53-
end
45+
#get '/clusters/:cluster_id/nodes/:node_id/bricks' do
46+
#node = Tendrl::Node.find_by_cluster_id(
47+
#params[:cluster_id], params[:node_id]
48+
#)
49+
#halt 404 unless node.present?
50+
#bricks = Tendrl::Brick.find_all_by_cluster_id_and_node_fqdn(
51+
#params[:cluster_id], node['fqdn']
52+
#)
53+
#{ bricks: BrickPresenter.list(bricks) }.to_json
54+
#end
5455

55-
get '/clusters/:cluster_id/jobs' do
56-
begin
57-
jobs = Tendrl::Job.all
58-
rescue Etcd::KeyNotFound
59-
jobs = []
60-
end
61-
{ jobs: JobPresenter.list_by_integration_id(jobs, params[:cluster_id]) }.to_json
62-
end
56+
#get '/clusters/:cluster_id/volumes/:volume_id/bricks' do
57+
#references = Tendrl::Brick.find_refs_by_cluster_id_and_volume_id(
58+
#params[:cluster_id], params[:volume_id]
59+
#)
60+
#bricks = Tendrl::Brick.find_by_cluster_id_and_refs(params[:cluster_id], references)
61+
#{ bricks: BrickPresenter.list(bricks) }.to_json
62+
#end
6363

64-
post '/clusters/:cluster_id/import' do
65-
Tendrl.load_node_definitions
66-
Tendrl::Cluster.exist? params[:cluster_id]
67-
flow = Tendrl::Flow.new('namespace.tendrl', 'ImportCluster')
68-
body = JSON.parse(request.body.read)
69-
body['Cluster.volume_profiling_flag'] = if ['enable', 'disable'].include?(body['Cluster.volume_profiling_flag'])
70-
body['Cluster.volume_profiling_flag']
71-
else
72-
'leave-as-is'
73-
end
74-
job = Tendrl::Job.new(
75-
current_user,
76-
flow,
77-
integration_id: params[:cluster_id]).create(body)
78-
status 202
79-
{ job_id: job.job_id }.to_json
80-
end
64+
#get '/clusters/:cluster_id/notifications' do
65+
#notifications = Tendrl::Notification.all
66+
#NotificationPresenter.list_by_integration_id(notifications, params[:cluster_id]).to_json
67+
#end
8168

82-
post '/clusters/:cluster_id/unmanage' do
83-
Tendrl.load_node_definitions
84-
flow = Tendrl::Flow.new('namespace.tendrl', 'UnmanageCluster')
85-
body = JSON.parse(request.body.string.present? ? request.body.string : '{}')
86-
job = Tendrl::Job.new(
87-
current_user,
88-
flow,
89-
integration_id: params[:cluster_id]).create(body)
90-
status 202
91-
{ job_id: job.job_id }.to_json
92-
end
69+
#get '/clusters/:cluster_id/jobs' do
70+
#begin
71+
#jobs = Tendrl::Job.all
72+
#rescue Etcd::KeyNotFound
73+
#jobs = []
74+
#end
75+
#{ jobs: JobPresenter.list_by_integration_id(jobs, params[:cluster_id]) }.to_json
76+
#end
9377

94-
post '/clusters/:cluster_id/expand' do
95-
Tendrl.load_node_definitions
96-
flow = Tendrl::Flow.new 'namespace.tendrl', 'ExpandClusterWithDetectedPeers'
97-
job = Tendrl::Job.new(
98-
current_user,
99-
flow,
100-
integration_id: params[:cluster_id]
101-
).create({})
102-
status 202
103-
{ job_id: job.job_id }.to_json
104-
end
78+
#post '/clusters/:cluster_id/unmanage' do
79+
#Tendrl.load_node_definitions
80+
#flow = Tendrl::Flow.new('namespace.tendrl', 'UnmanageCluster')
81+
#body = JSON.parse(request.body.string.present? ? request.body.string : '{}')
82+
#job = Tendrl::Job.new(
83+
#current_user,
84+
#flow,
85+
#integration_id: params[:cluster_id]).create(body)
86+
#status 202
87+
#{ job_id: job.job_id }.to_json
88+
#end
10589

106-
post '/clusters/:cluster_id/profiling' do
107-
Tendrl.load_definitions(params[:cluster_id])
108-
body = JSON.parse(request.body.read)
109-
volume_profiling_flag = if ['enable', 'disable'].include?(body['Cluster.volume_profiling_flag'])
110-
body['Cluster.volume_profiling_flag']
111-
else
112-
'leave-as-is'
113-
end
114-
flow = Tendrl::Flow.new('namespace.gluster', 'EnableDisableVolumeProfiling')
90+
#post '/clusters/:cluster_id/expand' do
91+
#Tendrl.load_node_definitions
92+
#flow = Tendrl::Flow.new 'namespace.tendrl', 'ExpandClusterWithDetectedPeers'
93+
#job = Tendrl::Job.new(
94+
#current_user,
95+
#flow,
96+
#integration_id: params[:cluster_id]
97+
#).create({})
98+
#status 202
99+
#{ job_id: job.job_id }.to_json
100+
#end
115101

116-
job = Tendrl::Job.new(
117-
current_user,
118-
flow,
119-
integration_id: params[:cluster_id],
120-
type: 'sds'
121-
).create('Cluster.volume_profiling_flag' => volume_profiling_flag)
122-
status 202
123-
{ job_id: job.job_id }.to_json
124-
end
102+
#post '/clusters/:cluster_id/profiling' do
103+
#Tendrl.load_definitions(params[:cluster_id])
104+
#body = JSON.parse(request.body.read)
105+
#volume_profiling_flag = if ['enable', 'disable'].include?(body['Cluster.volume_profiling_flag'])
106+
#body['Cluster.volume_profiling_flag']
107+
#else
108+
#'leave-as-is'
109+
#end
110+
#flow = Tendrl::Flow.new('namespace.gluster', 'EnableDisableVolumeProfiling')
125111

126-
post '/clusters/:cluster_id/volumes/:volume_id/start_profiling' do
127-
Tendrl.load_definitions(params[:cluster_id])
128-
flow = Tendrl::Flow.new('namespace.gluster', 'StartProfiling', 'Volume')
129-
job = Tendrl::Job.new(
130-
current_user,
131-
flow,
132-
integration_id: params[:cluster_id],
133-
type: 'sds'
134-
).create('Volume.vol_id' => params[:volume_id])
135-
status 202
136-
{ job_id: job.job_id }.to_json
137-
end
112+
#job = Tendrl::Job.new(
113+
#current_user,
114+
#flow,
115+
#integration_id: params[:cluster_id],
116+
#type: 'sds'
117+
#).create('Cluster.volume_profiling_flag' => volume_profiling_flag)
118+
#status 202
119+
#{ job_id: job.job_id }.to_json
120+
#end
138121

139-
post '/clusters/:cluster_id/volumes/:volume_id/stop_profiling' do
140-
Tendrl.load_definitions(params[:cluster_id])
141-
flow = Tendrl::Flow.new('namespace.gluster', 'StopProfiling', 'Volume')
142-
job = Tendrl::Job.new(
143-
current_user,
144-
flow,
145-
integration_id: params[:cluster_id],
146-
type: 'sds'
147-
).create('Volume.vol_id' => params[:volume_id])
148-
status 202
149-
{ job_id: job.job_id }.to_json
150-
end
122+
#post '/clusters/:cluster_id/volumes/:volume_id/start_profiling' do
123+
#Tendrl.load_definitions(params[:cluster_id])
124+
#flow = Tendrl::Flow.new('namespace.gluster', 'StartProfiling', 'Volume')
125+
#job = Tendrl::Job.new(
126+
#current_user,
127+
#flow,
128+
#integration_id: params[:cluster_id],
129+
#type: 'sds'
130+
#).create('Volume.vol_id' => params[:volume_id])
131+
#status 202
132+
#{ job_id: job.job_id }.to_json
133+
#end
134+
135+
#post '/clusters/:cluster_id/volumes/:volume_id/stop_profiling' do
136+
#Tendrl.load_definitions(params[:cluster_id])
137+
#flow = Tendrl::Flow.new('namespace.gluster', 'StopProfiling', 'Volume')
138+
#job = Tendrl::Job.new(
139+
#current_user,
140+
#flow,
141+
#integration_id: params[:cluster_id],
142+
#type: 'sds'
143+
#).create('Volume.vol_id' => params[:volume_id])
144+
#status 202
145+
#{ job_id: job.job_id }.to_json
146+
#end
151147
end

app/controllers/nodes_controller.rb

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class NodesController < AuthenticatedUsersController
22

33
before do
4-
Tendrl.load_node_definitions
4+
#Tendrl.load_node_definitions
55
end
66

77
get '/nodes' do
@@ -297,19 +297,6 @@ class NodesController < AuthenticatedUsersController
297297
{ job_id: job.job_id }.to_json
298298
end
299299

300-
post '/:flow' do
301-
flow = Tendrl::Flow.find_by_external_name_and_type(
302-
params[:flow], 'node_agent'
303-
)
304-
halt 404 if flow.nil?
305-
body = JSON.parse(request.body.read)
306-
job = Tendrl::Job.new(current_user, flow).create(body)
307-
308-
status 202
309-
{ job_id: job.job_id }.to_json
310-
end
311-
312-
313300
private
314301

315302
def detected_cluster_id(node_id)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class ObjectsController < AuthenticatedUsersController
2+
get '/objects/:type' do
3+
type = params[:type].singularize.humanize
4+
object_class = Tendrl.const_get type
5+
presenter_class = Object.const_get("#{type}Presenter")
6+
{ params[:type] => presenter_class.list(object_class.all) }.to_json
7+
end
8+
9+
get '/objects/:type/:id' do
10+
type = params[:type].singularize.humanize
11+
object_class = Tendrl.const_get type
12+
presenter_class = Object.const_get("#{type}Presenter")
13+
{ params[:type] => presenter_class.list(object_class.single) }.to_json
14+
end
15+
16+
get '/objects/:type/:id/:sub_object_type' do
17+
type = params[:type].singularize.humanize
18+
sub_object_type = params[:sub_object_type].singularize.humanize
19+
object_class = Tendrl.const_get type
20+
sub_object_class = Tendrl.const_get sub_object_type
21+
list = sub_object_class.public_send "find_all_by_#{type.snake_case}_id"
22+
presenter_class = Object.const_get("#{sub_object_type}Presenter")
23+
end
24+
#post '/objects/:type/:object_id/:flow_name'
25+
end

0 commit comments

Comments
 (0)