Commit 2423cfa3 authored by John E. Vincent's avatar John E. Vincent

finished app/config refactor

parent dd40590f
...@@ -43,7 +43,8 @@ module Noah ...@@ -43,7 +43,8 @@ module Noah
def self.all(options = {}) def self.all(options = {})
app_hash = Hash.new app_hash = Hash.new
options.empty? ? apps=Application.all.sort : apps=Application.find(options).sort 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 app_hash
end end
end end
......
...@@ -23,6 +23,24 @@ module Noah ...@@ -23,6 +23,24 @@ module Noah
super.merge(:name => name, :format => format, :body => body, :created_at => created_at, :updated_at => updated_at) super.merge(:name => name, :format => format, :body => body, :created_at => created_at, :updated_at => updated_at)
end 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 class << self
def find_or_create(opts={}) def find_or_create(opts={})
begin begin
......
...@@ -8,14 +8,15 @@ class Noah::App ...@@ -8,14 +8,15 @@ class Noah::App
# GET the raw data of a configuration object # GET the raw data of a configuration object
get '/configurations/:configname/data/?' do |configname| get '/configurations/:configname/data/?' do |configname|
c = Noah::Configuration.find(:name => configname).first 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] 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 c.body
end end
# GET the JSON representation of a configuration object # GET the JSON representation of a configuration object
get '/configurations/:configname/?' do |configname| get '/configurations/:configname/?' do |configname|
c = Noah::Configuration.find(:name => configname).first c = Noah::Configuration.find(:name => configname).first
(halt 404) if c.empty? (halt 404) if c.nil?
c.to_json c.to_json
end end
# GET all configurations # GET all configurations
...@@ -50,42 +51,27 @@ class Noah::App ...@@ -50,42 +51,27 @@ class Noah::App
w.to_json w.to_json
end end
# Attach a configuration object to an application object # Attach a configuration object to an application object
put '/configurations/:configname/:appname?' do |configname, appname| put '/configurations/:configname/?' do |configname|
app = Noah::Application.find_or_create(:name => appname)
dependency_action = app.is_new? ? "created" : "updated"
config = Noah::Configuration.find_or_create(:name => configname)
required_params = ["format", "body"] required_params = ["format", "body"]
data = JSON.parse(request.body.read) 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? if config.valid?
config.save config.save
app.configurations << config
app.save
action = config.is_new? ? "create" : "update" 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 r.to_json
else else
raise "#{format_errors(config)}" raise "#{format_errors(config)}"
end end
end end
delete '/configurations/:configname/:appname?' do |configname, appname| delete '/configurations/:configname/?' do |configname|
app = Noah::Application.find(:name => appname).first cfg = Noah::Configuration.find(:name => configname).first
cfg = app.configurations.find(:name => configname).first (halt 404) if cfg.nil?
# 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. cfg.delete
(halt 404) if app.empty? r = {"result" => "success", "id" => cfg.id, "action" => "delete", "affected_applications" => cfg.affected_applications, "item" => cfg.name}
(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 r.to_json
else
halt 404
end
else
halt 404
end
end end
end end
...@@ -61,8 +61,8 @@ describe "Using the Application Model", :reset_redis => true do ...@@ -61,8 +61,8 @@ describe "Using the Application Model", :reset_redis => true do
b = Noah::Application.create(@appdata2) b = Noah::Application.create(@appdata2)
c = Noah::Applications.all c = Noah::Applications.all
c.size.should == 2 c.size.should == 2
c.has_key?(a.name).should == true c.keys.member?(a.name).should == true
c.has_key?(b.name).should == true c.keys.member?(b.name).should == true
end end
end end
......
...@@ -46,6 +46,19 @@ describe "Using the Configuration Model", :reset_redis => true do ...@@ -46,6 +46,19 @@ describe "Using the Configuration Model", :reset_redis => true do
c = Noah::Configuration.find(@appconf_string).first c = Noah::Configuration.find(@appconf_string).first
c.nil?.should == true c.nil?.should == true
end 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 it "return all Configurations" do
a = Noah::Configuration.find_or_create(@appconf_string) a = Noah::Configuration.find_or_create(@appconf_string)
b = Noah::Configuration.find_or_create(@appconf_json) b = Noah::Configuration.find_or_create(@appconf_json)
......
require File.expand_path(File.dirname(__FILE__) + '/spec_helper') 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 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 describe "GET" do
it "all configurations should work" do it "all configurations should work" do
get '/configurations' get '/configurations'
last_response.should be_ok 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 = last_response.should return_json
response.keys.size.should == 3
response.is_a?(Array).should == true %w[redis_url json_config yaml_config].each do |c|
response.first["name"].should == "redis" response.keys.member?(c).should == true
response.first["format"].should == "string" %w[id tags links format body created_at updated_at].each do |ck|
response.first["body"].should == "redis://127.0.0.1:6379/0" response[c].keys.member?(ck).should == true
end
end end
it "named configuration for application should work" do end
get '/configurations/noah/redis' it "named configuration should work" do
get '/configurations/redis_url'
last_response.should be_ok last_response.should be_ok
response = last_response.body response = last_response.should return_json
response.should == "redis://127.0.0.1:6379/0" 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 end
it "named configuration should work with mime-type" do it "named configuration should work with mime-type" do
require 'yaml' require 'yaml'
get '/configurations/myrailsapp1/database.yml' get '/configurations/yaml_config/data'
last_response.should be_ok last_response.should be_ok
last_response.headers["Content-Type"].should == "text/x-yaml;charset=utf-8" last_response.headers["Content-Type"].should == "text/x-yaml;charset=utf-8"
response = YAML.load(last_response.body) response = YAML.load(last_response.body)
...@@ -36,12 +64,12 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_ ...@@ -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"].keys.sort.should == ["adapter", "database", "password", "username"]
response["development"].values.sort.should == ["dev_password", "dev_user", "development_database", "mysql"] response["development"].values.sort.should == ["dev_password", "dev_user", "development_database", "mysql"]
end end
it "invalid application should not work" do it "invalid configuration should not work" do
get '/configurations/badapp' get '/configurations/badconfig'
last_response.should be_missing last_response.should be_missing
end end
it "invalid configuration for application should not work" do it "invalid configuration data should not work" do
get '/configurations/badapp/badconfig' get '/configurations/badconfig/data'
last_response.should be_missing last_response.should be_missing
end end
end end
...@@ -49,60 +77,56 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_ ...@@ -49,60 +77,56 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_
describe "PUT" do describe "PUT" do
it "new configuration should work" do it "new configuration should work" do
config_data = {:format => "string", :body => "sample_config_entry"}.to_json config_data = {:format => "string", :body => "sample_config_entry"}.to_json
put '/configurations/newapp/newconfig', config_data, "CONTENT_TYPE" => "application/json" put '/configurations/newconfig', config_data, "CONTENT_TYPE" => "application/json"
last_response.should be_ok last_response.should be_ok
response = last_response.should return_json response = last_response.should return_json
response["result"].should == "success" response["result"].should == "success"
response["action"].should == "create" response["action"].should == "create"
response["dependencies"].should == "created"
response["application"].should == "newapp"
response["item"].should == "newconfig" response["item"].should == "newconfig"
end end
it "existing configuration should work" do it "existing configuration should work" do
config_data = {:format => "string", :body => "sample_config_entry"}.to_json config_data = {:format => "string", :body => "sample_config_entry"}.to_json
put '/configurations/newconfig', config_data, "CONTENT_TYPE" => "application/json"
sleep 3 sleep 3
put '/configurations/newapp/newconfig', config_data, "CONTENT_TYPE" => "application/json" put '/configurations/newconfig', config_data, "CONTENT_TYPE" => "application/json"
last_response.should be_ok last_response.should be_ok
response = last_response.should return_json response = last_response.should return_json
response["result"].should == "success" response["result"].should == "success"
response["action"].should == "update" response["action"].should == "update"
response["dependencies"].should == "updated"
response["application"].should == "newapp"
response["item"].should == "newconfig" response["item"].should == "newconfig"
end end
it "new configuration with missing format should not work" do it "new configuration with missing format should not work" do
config_data = {:body => "a string"}.to_json 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 last_response.should be_invalid
end end
it "new configuration with missing body should not work" do it "new configuration with missing body should not work" do
config_data = {:body => "a string"}.to_json 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 last_response.should be_invalid
end end
end end
describe "DELETE" do 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 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 last_response.should be_ok
response = last_response.should return_json response = last_response.should return_json
response["result"].should == "success" response["result"].should == "success"
response["id"].should == @c.id response["id"].should == @c.id
response["action"].should == "delete" response["action"].should == "delete"
response["application"].should == @a.name response["affected_applications"].member?(@a.name).should == true
response["item"].should == @c.name response["item"].should == @c.name
end end
it "invalid configuration should not work" do it "invalid configuration should not work" do
delete "/configurations/#{@a.name}/#{@c.name}" delete "/configurations/somethingthatshouldnotexist"
last_response.should be_missing last_response.should be_missing
end end
end end
......
...@@ -20,9 +20,9 @@ puts "Creating Application entry for 'noah'" ...@@ -20,9 +20,9 @@ puts "Creating Application entry for 'noah'"
a = Noah::Application.create(:name => 'noah') a = Noah::Application.create(:name => 'noah')
if a.save if a.save
puts "Creating Configuration entry for 'noah'" puts "Creating Configuration entry for 'noah'"
cr = Noah::Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0') cr = Noah::Configuration.create(:name => 'redis_url', :format => 'string', :body => 'redis://127.0.0.1:6379/0')
ch = Noah::Configuration.create(:name => 'host', :format => 'string', :body => 'localhost') ch = Noah::Configuration.create(:name => 'noah_host', :format => 'string', :body => 'localhost')
cp = Noah::Configuration.create(:name => 'port', :format => 'string', :body => '9292') cp = Noah::Configuration.create(:name => 'noah_port', :format => 'string', :body => '9292')
[cr,ch,cp].each do |c| [cr,ch,cp].each do |c|
a.configurations << c a.configurations << c
end end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment