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
bac46bd8
Commit
bac46bd8
authored
Aug 09, 2011
by
John E. Vincent
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
linking DRYd up
parent
3f1a3d55
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
119 additions
and
42 deletions
+119
-42
app.rb
lib/noah/app.rb
+1
-0
link_helpers.rb
lib/noah/helpers/link_helpers.rb
+45
-0
tag_helpers.rb
lib/noah/helpers/tag_helpers.rb
+1
-0
applications.rb
lib/noah/routes/applications.rb
+0
-8
configurations.rb
lib/noah/routes/configurations.rb
+0
-9
ephemerals.rb
lib/noah/routes/ephemerals.rb
+2
-0
hosts.rb
lib/noah/routes/hosts.rb
+0
-9
links.rb
lib/noah/routes/links.rb
+12
-0
services.rb
lib/noah/routes/services.rb
+0
-16
noahapp_link_spec.rb
spec/noahapp_link_spec.rb
+58
-0
No files found.
lib/noah/app.rb
View file @
bac46bd8
...
...
@@ -6,6 +6,7 @@ module Noah
class
App
<
Sinatra
::
Base
helpers
Noah
::
SinatraBaseHelpers
helpers
Noah
::
SinatraTagHelpers
helpers
Noah
::
SinatraLinkHelpers
configure
do
set
:app_file
,
__FILE__
...
...
lib/noah/helpers/link_helpers.rb
View file @
bac46bd8
module
Noah
module
SinatraLinkHelpers
def
link
(
primitive
,
name
,
link
)
case
primitive
when
"services"
servicename
,
hostname
=
name
.
split
(
'/'
)
obj
=
host_service
(
hostname
,
servicename
)
when
'hosts'
obj
=
Noah
::
Host
.
find
(
:name
=>
name
).
first
when
'configurations'
obj
=
Noah
::
Configuration
.
find
(
:name
=>
name
).
first
when
'applications'
obj
=
Noah
::
Application
.
find
(
:name
=>
name
).
first
when
'ephemerals'
obj
=
Noah
::
Ephemeral
.
find
(
:path
=>
"/
#{
name
}
"
).
first
else
halt
404
end
obj
.
nil?
?
(
halt
404
)
:
(
obj
.
link!
link
)
obj
.
to_json
end
def
unlink
(
primitive
,
name
,
link
)
case
primitive
when
"services"
servicename
,
hostname
=
name
.
split
(
'/'
)
obj
=
host_service
(
hostname
,
servicename
)
when
"hosts"
obj
=
Noah
::
Host
.
find
(
:name
=>
name
).
first
when
"configurations"
obj
=
Noah
::
Configuration
.
find
(
:name
=>
name
).
first
when
"applications"
obj
=
Noah
::
Application
.
find
(
:name
=>
name
).
first
when
"ephemerals"
obj
=
Noah
::
Ephemeral
.
find
(
:path
=>
"/
#{
name
}
"
).
first
else
halt
404
end
obj
.
nil?
?
(
halt
404
)
:
(
obj
.
unlink!
link
)
obj
.
to_json
end
end
end
lib/noah/helpers/tag_helpers.rb
View file @
bac46bd8
...
...
@@ -39,6 +39,7 @@ module Noah
else
halt
404
end
obj
.
nil?
?
(
halt
404
)
:
(
obj
.
untag!
(
tags
))
obj
.
to_json
end
...
...
lib/noah/routes/applications.rb
View file @
bac46bd8
...
...
@@ -21,14 +21,6 @@ class Noah::App
w
.
to_json
end
put
'/applications/:appname/link'
do
|
appname
|
required_params
=
[
"link_name"
]
data
=
JSON
.
parse
(
request
.
body
.
read
)
(
data
.
keys
.
sort
==
required_params
.
sort
)
?
(
a
=
Noah
::
Application
.
find
(:
name
=>
appname
).
first
)
:
(
raise
"Missing Parameters"
)
a
.
nil?
?
(
halt
404
)
:
(
a
.
link!
data
[
"link_name"
])
a
.
to_json
end
put
'/applications/:appname/configurations/:configname/?'
do
|
appname
,
configname
|
required_params
=
[
"format"
,
"body"
]
raise
"Missing Parameters"
if
request
.
body
.
nil?
...
...
lib/noah/routes/configurations.rb
View file @
bac46bd8
...
...
@@ -29,15 +29,6 @@ class Noah::App
configs
.
to_json
end
# Add configuration object to a custom link hierarchy
put
'/configurations/:configname/link'
do
|
configname
|
required_params
=
[
"link_name"
]
data
=
JSON
.
parse
(
request
.
body
.
read
)
(
data
.
keys
.
sort
==
required_params
.
sort
)
?
(
a
=
Noah
::
Configuration
.
find
(:
name
=>
configname
).
first
)
:
(
raise
"Missing Parameters"
)
a
.
nil?
?
(
halt
404
)
:
(
a
.
link!
data
[
"link_name"
])
a
.
to_json
end
# Add a watch to a configuration object
put
'/configurations/:configname/watch'
do
|
configname
|
required_params
=
[
"endpoint"
]
...
...
lib/noah/routes/ephemerals.rb
View file @
bac46bd8
...
...
@@ -19,6 +19,7 @@ class Noah::App
end
put
'/ephemerals/*'
do
pass
if
params
[
"splat"
][
0
].
match
(
/^.*\/link/
)
raise
(
"Data too large"
)
if
request
.
body
.
size
>
512
d
=
request
.
body
.
read
||
nil
opts
=
{
:path
=>
"/
#{
params
[
:splat
][
0
]
}
"
,
:data
=>
d
}
...
...
@@ -33,6 +34,7 @@ class Noah::App
end
delete
'/ephemerals/*'
do
pass
if
params
[
"splat"
][
0
].
match
(
/^.*\/link/
)
p
=
params
[
:splat
][
0
]
e
=
Noah
::
Ephemeral
.
find
(
:path
=>
"/"
+
p
).
first
if
e
...
...
lib/noah/routes/hosts.rb
View file @
bac46bd8
...
...
@@ -41,15 +41,6 @@ class Noah::App
w
.
to_json
end
put
'/hosts/:hostname/link'
do
|
hostname
|
required_params
=
[
"link_name"
]
data
=
JSON
.
parse
(
request
.
body
.
read
)
raise
"Missing parameters"
if
data
.
nil?
(
data
.
keys
.
sort
==
required_params
.
sort
)
?
(
a
=
Noah
::
Host
.
find
(:
name
=>
hostname
).
first
)
:
(
raise
"Missing Parameters"
)
a
.
nil?
?
(
halt
404
)
:
(
a
.
link!
data
[
"link_name"
])
a
.
to_json
end
put
'/hosts/:hostname/?'
do
|
hostname
|
required_params
=
[
"status"
]
data
=
JSON
.
parse
(
request
.
body
.
read
)
...
...
lib/noah/routes/links.rb
View file @
bac46bd8
class
Noah
::
App
put
'/:primitive/*/link'
do
|
primitive
,
name
|
required_params
=
[
"link_name"
]
data
=
JSON
.
parse
(
request
.
body
.
read
)
(
data
.
keys
.
sort
==
required_params
.
sort
)
?
(
link
(
primitive
,
name
,
data
[
"link_name"
]))
:
(
raise
"Missing Parameters"
)
end
delete
'/:primitive/*/link'
do
|
primitive
,
name
|
required_params
=
[
"link_name"
]
data
=
JSON
.
parse
(
request
.
body
.
read
)
(
data
.
keys
.
sort
==
required_params
.
sort
)
?
(
unlink
(
primitive
,
name
,
data
[
"link_name"
]))
:
(
raise
"Missing Parameters"
)
end
get
'/:link_name/:model_name/:item/?'
do
|
path
,
model
,
item
|
link_name
=
find_named_link
(
path
)
(
halt
404
)
if
link_name
.
to_hash
.
has_key?
(
model
.
to_sym
)
==
false
...
...
lib/noah/routes/services.rb
View file @
bac46bd8
...
...
@@ -22,22 +22,6 @@ class Noah::App
services
.
to_json
end
put
'/services/:servicename/link'
do
|
appname
|
required_params
=
[
"link_name"
]
data
=
JSON
.
parse
(
request
.
body
.
read
)
(
data
.
keys
.
sort
==
required_params
.
sort
)
?
(
a
=
Noah
::
Service
.
find
(:
name
=>
servicename
).
first
)
:
(
raise
"Missing Parameters"
)
a
.
nil?
?
(
halt
404
)
:
(
a
.
link!
data
[
"link_name"
])
a
.
to_json
end
put
'/services/:servicename/:hostname/link'
do
|
servicename
,
hostname
|
required_params
=
[
"link_name"
]
data
=
JSON
.
parse
(
request
.
body
.
read
)
(
data
.
keys
.
sort
==
required_params
.
sort
)
?
(
a
=
host_service
(
hostname
,
servicename
))
:
(
raise
"Missing Parameters"
)
a
.
nil?
?
(
halt
404
)
:
(
a
.
link!
data
[
"link_name"
])
a
.
to_json
end
put
'/services/:servicename/watch'
do
|
servicename
|
required_params
=
[
"endpoint"
]
data
=
JSON
.
parse
(
request
.
body
.
read
)
...
...
spec/noahapp_link_spec.rb
0 → 100644
View file @
bac46bd8
require
File
.
expand_path
(
File
.
dirname
(
__FILE__
)
+
'/spec_helper'
)
describe
"Using the Link API"
,
:reset_redis
=>
true
do
before
(
:each
)
do
Ohm
.
redis
.
flushdb
@link_name
=
{
"link_name"
=>
"rspec_org"
}
@host
=
Noah
::
Host
.
create
(
:name
=>
'linked_host'
,
:status
=>
'up'
)
@service
=
Noah
::
Service
.
create
(
:name
=>
'linked_service'
,
:status
=>
'down'
,
:host_id
=>
@host
.
id
)
@application
=
Noah
::
Application
.
create
(
:name
=>
'linked_application'
)
@configuration
=
Noah
::
Configuration
.
create
(
:name
=>
'linked_configuration'
,
:format
=>
'string'
,
:body
=>
'some_string'
)
@ephemeral
=
Noah
::
Ephemeral
.
create
(
:path
=>
'/tagged/ephemeral'
)
end
after
(
:each
)
do
Ohm
.
redis
.
flushdb
end
describe
"calling"
do
describe
"PUT"
do
[
'host'
,
'service'
,
'application'
,
'configuration'
,
'ephemeral'
].
each
do
|
primitive
|
it
"link to
#{
primitive
}
should work"
do
obj
=
instance_variable_get
(
"@
#{
primitive
}
"
)
case
primitive
when
"ephemeral"
put
"/
#{
primitive
}
s
#{
obj
.
path
}
/link"
,
@link_name
.
to_json
when
"service"
put
"/
#{
primitive
}
s/
#{
obj
.
name
}
/
#{
@host
.
name
}
/link"
,
@link_name
.
to_json
else
put
"/
#{
primitive
}
s/
#{
obj
.
name
}
/link"
,
@link_name
.
to_json
end
last_response
.
should
be_ok
response
=
last_response
.
should
return_json
response
[
'links'
][
0
].
should
==
@link_name
[
"link_name"
]
end
end
end
describe
"DELETE"
do
# ['host', 'service', 'application', 'configuration', 'ephemeral'].each do |primitive|
# it "multiple tags from #{primitive} should work" do
# obj = instance_variable_get("@#{primitive}")
# case primitive
# when "ephemeral"
# delete "/#{primitive}s#{obj.path}/tag", @multi_tags.to_json
# when "service"
# delete "/#{primitive}s/#{obj.name}/#{@host.name}/tag", @multi_tags.to_json
# else
# delete "/#{primitive}s/#{obj.name}/tag", @multi_tags.to_json
# end
# last_response.should be_ok
# response = last_response.should return_json
# response['tags'].sort.should_not == @multi_tags["tags"].sort
# response['tags'].size.should == 0
# end
# 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