-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
219 lines (161 loc) · 4.3 KB
/
app.rb
File metadata and controls
219 lines (161 loc) · 4.3 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
require 'sinatra/base'
require 'json'
require 'mongoid'
require 'httparty'
require 'erb'
require_relative 'models/snapshot'
require_relative 'models/edge'
require_relative 'models/node'
require_relative 'models/application'
require_relative 'models/serverset'
class App < Sinatra::Base
# =========================
# Enter authentication information for your pod below
# =========================
USERNAME =
API_KEY =
get '/' do
erb :index
end
configure do
Mongoid.load!('mongoid.yml')
enable :logging
end
before '/api*' do
content_type :json
end
# =========================
# API Calls to Appfirst for Parsing
# =========================
get '/api/appfirst' do
$i = 0
$j = 0
auth = {:username => USERNAME, :password => API_KEY}
url = "https://wwws.appfirst.com/api/topology"
response = HTTParty.get(url,
:basic_auth => auth,
:headers => {'Content-Type' => 'application/json'})
json = JSON.parse(response.to_s().gsub('=>', ':'))
url = "https://wwws.appfirst.com/api/servers"
response = HTTParty.get(url,
:basic_auth => auth,
:headers => {'Content-Type' => 'application/json'})
serverJSON = JSON.parse(response.to_s().gsub('=>', ':'))
new_snapshot = Snapshot.create
new_snapshot.update_attributes(time: Time.new.to_i)
# Add nodes to snapshots from API
while json["Node"][$i] != nil do
new_node = new_snapshot.nodes.create
name = json["Node"][$i]["id"]
id = ""
$k = 0
while serverJSON[$k]
if serverJSON[$k]["nickname"] == name
id = serverJSON[$k]["id"]
end
$k = $k +1
end
new_node.update_attributes(
hostname: name,
server_id: id
)
$i = $i + 1
end
# Add edges to snapshots from API
while json["Edge"][$i] != nil do
new_edge = new_snapshot.edges.create
new_edge.update_attributes(
toID: json["Edge"][$i]["toID"],
fromID: json["Edge"][$i]["fromID"],
swrite: json["Edge"][$i]["swrite"],
sread: json["Edge"][$i]["sread"],
tsnapshot1: json["Edge"][$i]["tsnapshot1"],
tsnapshot2: json["Edge"][$i]["tsnapshot2"]
)
$i = $i + 1
end
end
get '/api/appfirstApplication' do
$i = 0;
auth = {:username => USERNAME, :password => API_KEY}
url = "https://wwws.appfirst.com/api/applications"
response = HTTParty.get(url,
:basic_auth => auth,
:headers => {'Content-Type' => 'application/json'})
json = JSON.parse(response.to_s().gsub('=>', ':'))
while json[$i] != nil do
new_app = Application.create
new_app.update_attributes(
name: json[$i]["name"],
server_ids: json[$i]["servers"],
app_id: json[$i]["id"]
)
$i = $i + 1
end
Application.all.to_json
end
get '/api/appfirstServerSets' do
$i = 0;
auth = {:username => USERNAME, :password => API_KEY}
url = "https://wwws.appfirst.com/api/server_sets"
response = HTTParty.get(url,
:basic_auth => auth,
:headers => {'Content-Type' => 'application/json'})
json = JSON.parse(response.to_s().gsub('=>', ':'))
while json[$i] != nil do
new_set = Serverset.create
new_set.update_attributes(
name: json[$i]["name"],
server_ids: json[$i]["applications"]
)
$i = $i + 1
end
Serverset.all.to_json
end
# =========================
# Custom API Calls
# =========================
get '/api/snapshot' do
Snapshot.all.to_json
end
get '/api/snapshot/:id' do
Snapshot.find(params[:id]).to_json
end
get '/api/snapshot/:id/nodes' do
Snapshot.find(params[:id]).nodes.to_json
end
get '/api/snapshot/:id/node/:node_name' do
data = Array.new
data.push("[")
Edge.all_in(snapshot_id: [params[:id]]).each do |edge|
if edge.fromID == params[:node_name] or edge.toID == params[:node_name]
data.push(edge.to_json )
data.push(",")
end
end
data[-1] = "]"
data.join("")
return data
end
post '/api/snapshot' do
data = JSON.parse params[:snapshot]
new_snapshot = Snapshot.create snapshot: data['snapshot']
response['Location'] = '/server/#{new_snapshot.id}'
end
put '/api/snapshot' do
data = JSON.parse params[:snapshot]
Snapshot.find(data['_id']).update_attributes snapshot: data['snapshot']
end
delete '/api/server/:id' do
Snapshot.find(params[:id]).delete
end
get '/api/server' do
Server.all.to_json
end
get '/api/application' do
Application.all.to_json
end
get '/api/serverset' do
Serverset.all.to_json
end
end