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

finished app/config refactor

parent dd40590f
......@@ -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
......
......@@ -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
......
......@@ -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
......@@ -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
......
......@@ -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)
......
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 application should not work" do
get '/configurations/badapp'
it "invalid configuration should not work" do
get '/configurations/badconfig'
last_response.should be_missing
end
it "invalid configuration for application should not work" do
get '/configurations/badapp/badconfig'
it "invalid configuration data should not work" do
get '/configurations/badconfig/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/newapp/newconfig', 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/newapp/newconfig', 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["application"].should == @a.name
response["affected_applications"].member?(@a.name).should == true
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
......
......@@ -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
......
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