Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
Noah
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Noah
Commits
87310930
Commit
87310930
authored
Apr 11, 2011
by
John E. Vincent
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor of host and service web api. no more arrays
parent
31b093bc
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
52 additions
and
44 deletions
+52
-44
host_routes.rb
lib/noah/host_routes.rb
+0
-1
hosts.rb
lib/noah/models/hosts.rb
+10
-4
link.rb
lib/noah/models/link.rb
+9
-8
services.rb
lib/noah/models/services.rb
+10
-1
service_routes.rb
lib/noah/service_routes.rb
+4
-12
host_spec.rb
spec/host_spec.rb
+5
-5
noahapp_host_spec.rb
spec/noahapp_host_spec.rb
+4
-4
noahapp_service_spec.rb
spec/noahapp_service_spec.rb
+8
-7
service_spec.rb
spec/service_spec.rb
+2
-2
No files found.
lib/noah/host_routes.rb
View file @
87310930
...
@@ -25,7 +25,6 @@ class Noah::App
...
@@ -25,7 +25,6 @@ class Noah::App
# GET all {Hosts}
# GET all {Hosts}
get
'/hosts/?'
do
get
'/hosts/?'
do
hosts
.
map
{
|
h
|
h
.
to_hash
}
if
hosts
.
size
==
0
if
hosts
.
size
==
0
halt
404
halt
404
else
else
...
...
lib/noah/models/hosts.rb
View file @
87310930
...
@@ -4,6 +4,7 @@ module Noah
...
@@ -4,6 +4,7 @@ module Noah
# Host model
# Host model
# @return {Host} a {Host} object
# @return {Host} a {Host} object
include
Taggable
include
Taggable
include
Linkable
attribute
:name
attribute
:name
attribute
:status
attribute
:status
collection
:services
,
Service
collection
:services
,
Service
...
@@ -21,9 +22,11 @@ module Noah
...
@@ -21,9 +22,11 @@ module Noah
# @return [Hash] A hash representation of a {Host}
# @return [Hash] A hash representation of a {Host}
def
to_hash
def
to_hash
arr
=
[]
services_hash
=
Hash
.
new
services
.
sort
.
each
{
|
s
|
arr
<<
{
:id
=>
s
.
id
,
:status
=>
s
.
status
,
:name
=>
s
.
name
}}
services
.
sort
.
each
do
|
svc
|
h
=
{
:name
=>
name
,
:status
=>
status
,
:created_at
=>
created_at
,
:updated_at
=>
updated_at
,
:services
=>
arr
}
services_hash
[
"
#{
svc
.
name
}
"
]
=
svc
.
status
end
h
=
{
:name
=>
name
,
:status
=>
status
,
:created_at
=>
created_at
,
:updated_at
=>
updated_at
,
:services
=>
services_hash
}
super
.
merge
(
h
)
super
.
merge
(
h
)
end
end
...
@@ -50,7 +53,10 @@ module Noah
...
@@ -50,7 +53,10 @@ module Noah
# @param [Hash] optional filters for results
# @param [Hash] optional filters for results
# @return [Array] Array of {Host} objects
# @return [Array] Array of {Host} objects
def
self
.
all
(
options
=
{})
def
self
.
all
(
options
=
{})
options
.
empty?
?
Noah
::
Host
.
all
.
sort
:
Noah
::
Host
.
find
(
options
).
sort
host_hash
=
Hash
.
new
options
.
empty?
?
hosts
=
Noah
::
Host
.
all
.
sort
:
hosts
=
Noah
::
Host
.
find
(
options
).
sort
hosts
.
each
{
|
x
|
host_hash
[
"
#{
x
.
name
}
"
]
=
x
.
to_hash
.
reject
{
|
k
,
v
|
k
==
:name
}
}
host_hash
end
end
end
end
end
end
lib/noah/models/link.rb
View file @
87310930
...
@@ -42,16 +42,17 @@ module Noah
...
@@ -42,16 +42,17 @@ module Noah
# all of this bs is because services are unique per [servicename, hostname]
# all of this bs is because services are unique per [servicename, hostname]
# so if I add multiple services just by name to the hash, I'll wipe the previous one
# so if I add multiple services just by name to the hash, I'll wipe the previous one
# a better option would be for services to be named slug style
# a better option would be for services to be named slug style
if
cls
==
"service"
hsh
[
"
#{
n
.
name
}
-
#{
n
.
id
}
"
]
=
Hash
.
new
unless
hsh
.
has_key?
(
"
#{
n
.
name
}
-
#{
n
.
id
}
"
)
hsh
[
"
#{
n
.
name
}
-
#{
n
.
id
}
"
].
merge!
({
n
.
to_hash
[
:host
]
=>
n
.
to_hash
})
else
hsh
[
"
#{
n
.
name
}
"
]
=
Hash
.
new
unless
hsh
.
has_key?
(
n
.
name
)
hsh
[
"
#{
n
.
name
}
"
]
=
Hash
.
new
unless
hsh
.
has_key?
(
n
.
name
)
hsh
[
n
.
name
].
merge!
(
n
.
to_hash
)
case
cls
when
"service"
sh
=
Noah
::
Host
[
n
.
host_id
].
name
hsh
[
"
#{
n
.
name
}
"
][
"
#{
sh
}
"
]
=
Hash
.
new
unless
hsh
[
"
#{
n
.
name
}
"
].
has_key?
(
"
#{
sh
}
"
)
hsh
[
"
#{
n
.
name
}
"
][
"
#{
sh
}
"
].
merge!
({
:id
=>
n
.
id
,
:status
=>
n
.
status
,
:tags
=>
n
.
to_hash
[
:tags
]})
when
"host"
hsh
[
"
#{
n
.
name
}
"
].
merge!
({
:id
=>
n
.
id
,
:status
=>
n
.
status
,
:tags
=>
n
.
to_hash
[
:tags
],
:services
=>
n
.
to_hash
[
:services
]})
else
hsh
[
n
.
name
].
merge!
(
n
.
to_hash
.
reject
{
|
key
,
value
|
key
==
:created_at
||
key
==
:updated_at
||
key
==
:name
})
end
end
# all of this bs is because services are unique per [servicename, hostname]
# so if I add multiple services just by name to the hash, I'll wipe the previous one
# a better option would be for services to be named slug style
end
end
end
end
h
=
{
:name
=>
name
,
:hosts
=>
@hosts
,
:services
=>
@services
,
:applications
=>
@applications
,
:configurations
=>
@configurations
,
:ephemerals
=>
@ephemerals
,
:created_at
=>
created_at
,
:updated_at
=>
updated_at
}
h
=
{
:name
=>
name
,
:hosts
=>
@hosts
,
:services
=>
@services
,
:applications
=>
@applications
,
:configurations
=>
@configurations
,
:ephemerals
=>
@ephemerals
,
:created_at
=>
created_at
,
:updated_at
=>
updated_at
}
...
...
lib/noah/models/services.rb
View file @
87310930
...
@@ -2,6 +2,7 @@ module Noah
...
@@ -2,6 +2,7 @@ module Noah
class
Service
<
Model
class
Service
<
Model
include
Taggable
include
Taggable
include
Linkable
attribute
:name
attribute
:name
attribute
:status
attribute
:status
reference
:host
,
Host
reference
:host
,
Host
...
@@ -48,7 +49,15 @@ module Noah
...
@@ -48,7 +49,15 @@ module Noah
class
Services
class
Services
def
self
.
all
(
options
=
{})
def
self
.
all
(
options
=
{})
options
.
empty?
?
Service
.
all
.
sort
:
Service
.
find
(
options
).
sort
service_hash
=
Hash
.
new
options
.
empty?
?
services
=
Service
.
all
.
sort
:
services
=
Service
.
find
(
options
).
sort
services
.
each
do
|
svc
|
service_hash
[
"
#{
svc
.
name
}
"
]
=
Hash
.
new
unless
service_hash
.
has_key?
(
svc
.
name
)
host_name
=
Noah
::
Host
[
svc
.
host_id
].
name
service_hash
[
"
#{
svc
.
name
}
"
][
"
#{
host_name
}
"
]
=
Hash
.
new
service_hash
[
"
#{
svc
.
name
}
"
][
"
#{
host_name
}
"
].
merge!
({
:id
=>
svc
.
id
,
:status
=>
svc
.
status
,
:tags
=>
svc
.
to_hash
[
:tags
],
:links
=>
svc
.
to_hash
[
:links
],
:created_at
=>
svc
.
created_at
,
:updated_at
=>
svc
.
updated_at
})
end
service_hash
end
end
end
end
end
end
lib/noah/service_routes.rb
View file @
87310930
...
@@ -13,22 +13,14 @@ class Noah::App
...
@@ -13,22 +13,14 @@ class Noah::App
get
'/services/:servicename/?'
do
|
servicename
|
get
'/services/:servicename/?'
do
|
servicename
|
s
=
services
(
:name
=>
servicename
)
s
=
services
(
:name
=>
servicename
)
s
.
map
{
|
x
|
x
.
to_hash
}
(
halt
404
)
if
s
.
size
==
0
if
s
.
empty?
halt
404
else
s
.
to_json
s
.
to_json
end
end
end
get
'/services/?'
do
get
'/services/?'
do
if
services
.
empty?
(
halt
404
)
if
services
.
size
==
0
halt
404
else
services
.
map
{
|
s
|
s
.
to_hash
}
services
.
to_json
services
.
to_json
end
end
end
put
'/services/:servicename/watch'
do
|
servicename
|
put
'/services/:servicename/watch'
do
|
servicename
|
required_params
=
[
"endpoint"
]
required_params
=
[
"endpoint"
]
...
...
spec/host_spec.rb
View file @
87310930
...
@@ -73,12 +73,12 @@ describe "Using the Host Model", :reset_redis => true do
...
@@ -73,12 +73,12 @@ describe "Using the Host Model", :reset_redis => true do
host2
=
Noah
::
Host
.
create
(
:name
=>
hostname2
,
:status
=>
status2
)
host2
=
Noah
::
Host
.
create
(
:name
=>
hostname2
,
:status
=>
status2
)
host1
.
save
host1
.
save
host2
.
save
host2
.
save
Noah
::
Hosts
.
all
.
class
.
should
==
Array
Noah
::
Hosts
.
all
.
class
.
should
==
Hash
Noah
::
Hosts
.
all
.
size
.
should
==
2
Noah
::
Hosts
.
all
.
size
.
should
==
2
Noah
::
Hosts
.
all
.
first
.
name
.
should
==
hostname1
Noah
::
Hosts
.
all
.
has_key?
(
hostname1
).
should
==
true
Noah
::
Hosts
.
all
.
first
.
status
.
should
==
status1
Noah
::
Hosts
.
all
[
hostname1
][
:status
]
.
should
==
status1
Noah
::
Hosts
.
all
.
last
.
name
.
should
==
hostname2
Noah
::
Hosts
.
all
.
has_key?
(
hostname2
).
should
==
true
Noah
::
Hosts
.
all
.
last
.
status
.
should
==
status2
Noah
::
Hosts
.
all
[
hostname2
][
:status
]
.
should
==
status2
end
end
end
end
...
...
spec/noahapp_host_spec.rb
View file @
87310930
...
@@ -20,10 +20,10 @@ describe "Using the Host API", :reset_redis => false, :populate_sample_data => t
...
@@ -20,10 +20,10 @@ describe "Using the Host API", :reset_redis => false, :populate_sample_data => t
response
[
"name"
].
should
==
"localhost"
response
[
"name"
].
should
==
"localhost"
response
[
"status"
].
should
==
"up"
response
[
"status"
].
should
==
"up"
services
.
size
.
should
==
2
services
.
size
.
should
==
2
services
.
first
[
"name"
].
should
==
"redis"
services
.
has_key?
(
"redis"
).
should
==
true
services
.
first
[
"status"
].
should
==
"up"
services
.
has_key?
(
"noah"
).
should
==
true
services
.
last
[
"name"
].
should
==
"noah
"
services
[
"redis"
].
should
==
"up
"
services
.
last
[
"status
"
].
should
==
"up"
services
[
"noah
"
].
should
==
"up"
end
end
it
"named service for host should work"
do
it
"named service for host should work"
do
...
...
spec/noahapp_service_spec.rb
View file @
87310930
...
@@ -16,18 +16,19 @@ describe "Using the Service API", :reset_redis => false, :populate_sample_data =
...
@@ -16,18 +16,19 @@ describe "Using the Service API", :reset_redis => false, :populate_sample_data =
get
'/services'
get
'/services'
last_response
.
should
be_ok
last_response
.
should
be_ok
response
=
last_response
.
should
return_json
response
=
last_response
.
should
return_json
response
.
is_a?
(
Array
).
should
==
true
response
.
is_a?
(
Hash
).
should
==
true
end
end
it
"all named services should work"
do
it
"all named services should work"
do
get
"/services/
#{
@sample_service
[
:name
]
}
"
get
"/services/
#{
@sample_service
[
:name
]
}
"
last_response
.
should
be_ok
last_response
.
should
be_ok
response
=
last_response
.
should
return_json
response
=
last_response
.
should
return_json
response
.
is_a?
(
Array
).
should
==
true
response
.
is_a?
(
Hash
).
should
==
true
s
=
response
.
first
response
.
has_key?
(
@s
.
name
).
should
==
true
s
[
"id"
].
should
==
@s
.
id
response
[
@s
.
name
].
is_a?
(
Hash
).
should
==
true
s
[
"name"
].
should
==
@s
.
name
response
[
@s
.
name
].
has_key?
(
@h
.
name
).
should
==
true
s
[
"status"
].
should
==
@s
.
status
response
[
@s
.
name
][
@h
.
name
].
is_a?
(
Hash
).
should
==
true
s
[
"host"
].
should
==
@h
.
name
response
[
@s
.
name
][
@h
.
name
][
"id"
].
should
==
@s
.
id
response
[
@s
.
name
][
@h
.
name
][
"status"
].
should
==
@s
.
status
end
end
it
"named service for host should work"
do
it
"named service for host should work"
do
get
"/services/
#{
@sample_service
[
:name
]
}
/
#{
@sample_host
[
:name
]
}
"
get
"/services/
#{
@sample_service
[
:name
]
}
/
#{
@sample_host
[
:name
]
}
"
...
...
spec/service_spec.rb
View file @
87310930
...
@@ -57,8 +57,8 @@ describe "Noah Service Model", :reset_redis => true do
...
@@ -57,8 +57,8 @@ describe "Noah Service Model", :reset_redis => true do
h
.
save
h
.
save
end
end
Noah
::
Services
.
all
.
size
.
should
==
2
Noah
::
Services
.
all
.
size
.
should
==
2
Noah
::
Services
.
all
.
first
.
name
.
should
==
"s1"
Noah
::
Services
.
all
.
has_key?
(
"s1"
).
should
==
true
Noah
::
Services
.
all
.
last
.
name
.
should
==
"s2"
Noah
::
Services
.
all
.
has_key?
(
"s2"
).
should
==
true
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment