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
2423cfa3
Commit
2423cfa3
authored
Apr 18, 2011
by
John E. Vincent
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
finished app/config refactor
parent
dd40590f
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
115 additions
and
73 deletions
+115
-73
applications.rb
lib/noah/models/applications.rb
+2
-1
configurations.rb
lib/noah/models/configurations.rb
+18
-0
configurations.rb
lib/noah/routes/configurations.rb
+14
-28
application_spec.rb
spec/application_spec.rb
+2
-2
configuration_spec.rb
spec/configuration_spec.rb
+13
-0
noahapp_configuration_spec.rb
spec/noahapp_configuration_spec.rb
+63
-39
sample_data.rb
spec/support/sample_data.rb
+3
-3
No files found.
lib/noah/models/applications.rb
View file @
2423cfa3
...
...
@@ -43,7 +43,8 @@ module Noah
def
self
.
all
(
options
=
{})
app_hash
=
Hash
.
new
options
.
empty?
?
apps
=
Application
.
all
.
sort
:
apps
=
Application
.
find
(
options
).
sort
apps
.
each
{
|
x
|
app_hash
[
"
#{
x
.
name
}
"
]
=
x
.
to_hash
.
reject
{
|
k
,
v
|
k
==
:name
}
}
#apps.each {|x| app_hash["#{x.name}"] = x.to_hash.reject {|k,v| k == :name} }
apps
.
each
{
|
x
|
app_hash
.
merge!
(
x
.
to_hash
)
}
app_hash
end
end
...
...
lib/noah/models/configurations.rb
View file @
2423cfa3
...
...
@@ -23,6 +23,24 @@ module Noah
super
.
merge
(
:name
=>
name
,
:format
=>
format
,
:body
=>
body
,
:created_at
=>
created_at
,
:updated_at
=>
updated_at
)
end
# Because we're not doing a 2-way relationship
# we need to clean up any applications that use
# this configuration ourself
def
delete
@affected_applications
=
Array
.
new
Noah
::
Application
.
all
.
each
do
|
app
|
if
app
.
configurations
.
member?
(
self
)
app
.
configurations
.
delete
(
self
)
@affected_applications
<<
app
.
name
end
end
super
end
def
affected_applications
@affected_applications
end
class
<<
self
def
find_or_create
(
opts
=
{})
begin
...
...
lib/noah/routes/configurations.rb
View file @
2423cfa3
...
...
@@ -8,14 +8,15 @@ class Noah::App
# GET the raw data of a configuration object
get
'/configurations/:configname/data/?'
do
|
configname
|
c
=
Noah
::
Configuration
.
find
(
:name
=>
configname
).
first
(
halt
404
)
if
c
.
empty
?
(
halt
404
)
if
c
.
nil
?
content_type
content_type_mapping
[
c
.
format
.
to_sym
]
if
content_type_mapping
[
c
.
format
.
to_sym
]
response
.
headers
[
'Content-Disposition'
]
=
"attachment; filename=
#{
configname
}
"
c
.
body
end
# GET the JSON representation of a configuration object
get
'/configurations/:configname/?'
do
|
configname
|
c
=
Noah
::
Configuration
.
find
(
:name
=>
configname
).
first
(
halt
404
)
if
c
.
empty
?
(
halt
404
)
if
c
.
nil
?
c
.
to_json
end
# GET all configurations
...
...
@@ -50,42 +51,27 @@ class Noah::App
w
.
to_json
end
# Attach a configuration object to an application object
put
'/configurations/:configname/:appname?'
do
|
configname
,
appname
|
app
=
Noah
::
Application
.
find_or_create
(
:name
=>
appname
)
dependency_action
=
app
.
is_new?
?
"created"
:
"updated"
config
=
Noah
::
Configuration
.
find_or_create
(
:name
=>
configname
)
put
'/configurations/:configname/?'
do
|
configname
|
required_params
=
[
"format"
,
"body"
]
data
=
JSON
.
parse
(
request
.
body
.
read
)
data
.
keys
.
sort
==
required_params
.
sort
?
(
config
.
format
=
data
[
"format"
];
config
.
body
=
data
[
"body"
])
:
(
raise
"Missing Parameters"
)
data
.
keys
.
sort
==
required_params
.
sort
?
config
=
Noah
::
Configuration
.
find_or_create
(:
name
=>
configname
)
:
(
raise
"Missing Parameters"
)
config
.
body
=
data
[
"body"
]
config
.
format
=
data
[
"format"
]
if
config
.
valid?
config
.
save
app
.
configurations
<<
config
app
.
save
action
=
config
.
is_new?
?
"create"
:
"update"
r
=
{
"result"
=>
"success"
,
"id"
=>
"
#{
config
.
id
}
"
,
"action"
=>
action
,
"
dependencies"
=>
dependency_action
,
"application"
=>
app
.
name
,
"
item"
=>
config
.
name
}
r
=
{
"result"
=>
"success"
,
"id"
=>
"
#{
config
.
id
}
"
,
"action"
=>
action
,
"item"
=>
config
.
name
}
r
.
to_json
else
raise
"
#{
format_errors
(
config
)
}
"
end
end
delete
'/configurations/:configname/:appname?'
do
|
configname
,
appname
|
app
=
Noah
::
Application
.
find
(
:name
=>
appname
).
first
cfg
=
app
.
configurations
.
find
(
:name
=>
configname
).
first
# Check with soveran. If we delete the member from the set using .delete, it removes the object. That would break any other users of that object.
(
halt
404
)
if
app
.
empty?
(
halt
404
)
if
config
.
empty?
if
app
config
=
app
.
configurations
.
find
(
:name
=>
element
).
first
if
config
app
.
configurations
.
delete
(
config
)
r
=
{
"result"
=>
"success"
,
"id"
=>
"
#{
config
.
id
}
"
,
"action"
=>
"delete"
,
"application"
=>
"
#{
app
.
name
}
"
,
"item"
=>
"
#{
element
}
"
}
r
.
to_json
else
halt
404
end
else
halt
404
end
delete
'/configurations/:configname/?'
do
|
configname
|
cfg
=
Noah
::
Configuration
.
find
(
:name
=>
configname
).
first
(
halt
404
)
if
cfg
.
nil?
cfg
.
delete
r
=
{
"result"
=>
"success"
,
"id"
=>
cfg
.
id
,
"action"
=>
"delete"
,
"affected_applications"
=>
cfg
.
affected_applications
,
"item"
=>
cfg
.
name
}
r
.
to_json
end
end
spec/application_spec.rb
View file @
2423cfa3
...
...
@@ -61,8 +61,8 @@ describe "Using the Application Model", :reset_redis => true do
b
=
Noah
::
Application
.
create
(
@appdata2
)
c
=
Noah
::
Applications
.
all
c
.
size
.
should
==
2
c
.
has_key
?
(
a
.
name
).
should
==
true
c
.
has_key
?
(
b
.
name
).
should
==
true
c
.
keys
.
member
?
(
a
.
name
).
should
==
true
c
.
keys
.
member
?
(
b
.
name
).
should
==
true
end
end
...
...
spec/configuration_spec.rb
View file @
2423cfa3
...
...
@@ -46,6 +46,19 @@ describe "Using the Configuration Model", :reset_redis => true do
c
=
Noah
::
Configuration
.
find
(
@appconf_string
).
first
c
.
nil?
.
should
==
true
end
it
"delete from Application when deleting Configuration"
do
# We have to test this because we override delete in Configuration
a
=
Noah
::
Configuration
.
find_or_create
(
@appconf_string
)
b
=
Noah
::
Configuration
.
find
(
@appconf_string
).
first
c
=
Noah
::
Application
.
create
(
:name
=>
"somerandomapp1234"
)
c
.
configurations
<<
a
b
.
should
==
a
a
.
delete
d
=
Noah
::
Configuration
.
find
(
@appconf_string
).
first
d
.
nil?
.
should
==
true
a
.
affected_applications
.
member?
(
c
.
name
).
should
==
true
c
.
configurations
.
size
.
should
==
0
end
it
"return all Configurations"
do
a
=
Noah
::
Configuration
.
find_or_create
(
@appconf_string
)
b
=
Noah
::
Configuration
.
find_or_create
(
@appconf_json
)
...
...
spec/noahapp_configuration_spec.rb
View file @
2423cfa3
require
File
.
expand_path
(
File
.
dirname
(
__FILE__
)
+
'/spec_helper'
)
describe
"Using the Configuration API"
,
:reset_redis
=>
false
,
:populate_sample_data
=>
true
do
SAMPLE_YAML
=
<<
EOY
development:
database: development_database
adapter: mysql
username: dev_user
password: dev_password
EOY
SAMPLE_JSON
=
<<
EOJ
{
"id":"hostname",
"data":"localhost"
}
EOJ
describe
"Using the Configuration API"
,
:reset_redis
=>
true
,
:populate_sample_data
=>
false
do
describe
"calling"
do
before
(
:each
)
do
Ohm
.
redis
.
flushdb
@redis_config
=
Noah
::
Configuration
.
create
(
:name
=>
'redis_url'
,
:format
=>
'string'
,
:body
=>
'redis://127.0.0.1:6379/0'
)
@json_config
=
Noah
::
Configuration
.
create
(
:name
=>
'json_config'
,
:format
=>
'json'
,
:body
=>
SAMPLE_JSON
)
@yaml_config
=
Noah
::
Configuration
.
create
(
:name
=>
'yaml_config'
,
:format
=>
'yaml'
,
:body
=>
SAMPLE_YAML
)
@sample_application
=
Noah
::
Application
.
create
(
:name
=>
'rspec_application'
)
end
after
(
:each
)
do
Ohm
.
redis
.
flushdb
end
describe
"GET"
do
it
"all configurations should work"
do
get
'/configurations'
last_response
.
should
be_ok
last_response
.
should
return_json
end
it
"named application should work"
do
get
'/configurations/noah'
last_response
.
should
be_ok
response
=
last_response
.
should
return_json
response
.
is_a?
(
Array
).
should
==
true
response
.
first
[
"name"
].
should
==
"redis"
response
.
first
[
"format"
].
should
==
"string"
response
.
first
[
"body"
].
should
==
"redis://127.0.0.1:6379/0"
response
.
keys
.
size
.
should
==
3
%w[redis_url json_config yaml_config]
.
each
do
|
c
|
response
.
keys
.
member?
(
c
).
should
==
true
%w[id tags links format body created_at updated_at]
.
each
do
|
ck
|
response
[
c
].
keys
.
member?
(
ck
).
should
==
true
end
end
end
it
"named configuration
for application
should work"
do
get
'/configurations/
noah/redis
'
it
"named configuration should work"
do
get
'/configurations/
redis_url
'
last_response
.
should
be_ok
response
=
last_response
.
body
response
.
should
==
"redis://127.0.0.1:6379/0"
response
=
last_response
.
should
return_json
response
.
is_a?
(
Hash
).
should
==
true
response
[
'name'
].
should
==
@redis_config
.
name
response
[
'id'
].
should
==
@redis_config
.
id
response
[
"format"
].
should
==
@redis_config
.
format
response
[
"body"
].
should
==
@redis_config
.
body
response
[
"tags"
].
size
.
should
==
0
response
[
"links"
].
size
.
should
==
0
end
it
"named configuration should work with mime-type"
do
require
'yaml'
get
'/configurations/
myrailsapp1/database.yml
'
get
'/configurations/
yaml_config/data
'
last_response
.
should
be_ok
last_response
.
headers
[
"Content-Type"
].
should
==
"text/x-yaml;charset=utf-8"
response
=
YAML
.
load
(
last_response
.
body
)
...
...
@@ -36,12 +64,12 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_
response
[
"development"
].
keys
.
sort
.
should
==
[
"adapter"
,
"database"
,
"password"
,
"username"
]
response
[
"development"
].
values
.
sort
.
should
==
[
"dev_password"
,
"dev_user"
,
"development_database"
,
"mysql"
]
end
it
"invalid
applic
ation should not work"
do
get
'/configurations/bad
app
'
it
"invalid
configur
ation should not work"
do
get
'/configurations/bad
config
'
last_response
.
should
be_missing
end
it
"invalid configuration
for application
should not work"
do
get
'/configurations/bad
app/badconfig
'
it
"invalid configuration
data
should not work"
do
get
'/configurations/bad
config/data
'
last_response
.
should
be_missing
end
end
...
...
@@ -49,60 +77,56 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_
describe
"PUT"
do
it
"new configuration should work"
do
config_data
=
{
:format
=>
"string"
,
:body
=>
"sample_config_entry"
}.
to_json
put
'/configurations/new
app/new
config'
,
config_data
,
"CONTENT_TYPE"
=>
"application/json"
put
'/configurations/newconfig'
,
config_data
,
"CONTENT_TYPE"
=>
"application/json"
last_response
.
should
be_ok
response
=
last_response
.
should
return_json
response
[
"result"
].
should
==
"success"
response
[
"action"
].
should
==
"create"
response
[
"dependencies"
].
should
==
"created"
response
[
"application"
].
should
==
"newapp"
response
[
"item"
].
should
==
"newconfig"
end
it
"existing configuration should work"
do
config_data
=
{
:format
=>
"string"
,
:body
=>
"sample_config_entry"
}.
to_json
put
'/configurations/newconfig'
,
config_data
,
"CONTENT_TYPE"
=>
"application/json"
sleep
3
put
'/configurations/new
app/new
config'
,
config_data
,
"CONTENT_TYPE"
=>
"application/json"
put
'/configurations/newconfig'
,
config_data
,
"CONTENT_TYPE"
=>
"application/json"
last_response
.
should
be_ok
response
=
last_response
.
should
return_json
response
[
"result"
].
should
==
"success"
response
[
"action"
].
should
==
"update"
response
[
"dependencies"
].
should
==
"updated"
response
[
"application"
].
should
==
"newapp"
response
[
"item"
].
should
==
"newconfig"
end
it
"new configuration with missing format should not work"
do
config_data
=
{
:body
=>
"a string"
}.
to_json
put
'/configurations/
newnewapp/
someconfig'
,
config_data
,
"CONTENT_TYPE"
=>
"application/json"
put
'/configurations/someconfig'
,
config_data
,
"CONTENT_TYPE"
=>
"application/json"
last_response
.
should
be_invalid
end
it
"new configuration with missing body should not work"
do
config_data
=
{
:body
=>
"a string"
}.
to_json
put
'/configurations/
newnewapp/
someconfig'
,
config_data
,
"CONTENT_TYPE"
=>
"application/json"
put
'/configurations/someconfig'
,
config_data
,
"CONTENT_TYPE"
=>
"application/json"
last_response
.
should
be_invalid
end
end
describe
"DELETE"
do
before
(
:all
)
do
@a
=
Noah
::
Application
.
create
(
:name
=>
'delete_test_app'
)
cparms
=
{
:name
=>
'a'
,
:format
=>
'string'
,
:body
=>
'asdf'
}
@a
.
configurations
<<
Noah
::
Configuration
.
create
(
cparms
)
@a
.
save
@c
=
@a
.
configurations
.
first
end
it
"existing configuration should work"
do
delete
"/configurations/
#{
@a
.
name
}
/
#{
@c
.
name
}
"
@a
=
Noah
::
Application
.
create
(
:name
=>
'delete_test_app'
)
cparms
=
{
:name
=>
'asdf'
,
:format
=>
'string'
,
:body
=>
'asdf'
}
@c
=
Noah
::
Configuration
.
create
(
cparms
)
@a
.
configurations
<<
@c
get
"/configurations/asdf"
p
last_response
delete
"/configurations/
#{
@c
.
name
}
"
p
last_response
last_response
.
should
be_ok
response
=
last_response
.
should
return_json
response
[
"result"
].
should
==
"success"
response
[
"id"
].
should
==
@c
.
id
response
[
"action"
].
should
==
"delete"
response
[
"a
pplication"
].
should
==
@a
.
nam
e
response
[
"a
ffected_applications"
].
member?
(
@a
.
name
).
should
==
tru
e
response
[
"item"
].
should
==
@c
.
name
end
it
"invalid configuration should not work"
do
delete
"/configurations/
#{
@a
.
name
}
/
#{
@c
.
name
}
"
delete
"/configurations/
somethingthatshouldnotexist
"
last_response
.
should
be_missing
end
end
...
...
spec/support/sample_data.rb
View file @
2423cfa3
...
...
@@ -20,9 +20,9 @@ puts "Creating Application entry for 'noah'"
a
=
Noah
::
Application
.
create
(
:name
=>
'noah'
)
if
a
.
save
puts
"Creating Configuration entry for 'noah'"
cr
=
Noah
::
Configuration
.
create
(
:name
=>
'redis'
,
:format
=>
'string'
,
:body
=>
'redis://127.0.0.1:6379/0'
)
ch
=
Noah
::
Configuration
.
create
(
:name
=>
'host'
,
:format
=>
'string'
,
:body
=>
'localhost'
)
cp
=
Noah
::
Configuration
.
create
(
:name
=>
'port'
,
:format
=>
'string'
,
:body
=>
'9292'
)
cr
=
Noah
::
Configuration
.
create
(
:name
=>
'redis
_url
'
,
:format
=>
'string'
,
:body
=>
'redis://127.0.0.1:6379/0'
)
ch
=
Noah
::
Configuration
.
create
(
:name
=>
'
noah_
host'
,
:format
=>
'string'
,
:body
=>
'localhost'
)
cp
=
Noah
::
Configuration
.
create
(
:name
=>
'
noah_
port'
,
:format
=>
'string'
,
:body
=>
'9292'
)
[
cr
,
ch
,
cp
].
each
do
|
c
|
a
.
configurations
<<
c
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