Commit 4d691003 authored by John E. Vincent's avatar John E. Vincent

refactor of config/app models. linking support wip

parent 0c7922b2
...@@ -5,7 +5,7 @@ class Noah::App ...@@ -5,7 +5,7 @@ class Noah::App
if app.nil? if app.nil?
halt 404 halt 404
else else
c = Noah::Configuration.find(:name => config, :application_id => app.id).first c = app.configurations.find(:name => config).first
c.to_json c.to_json
end end
end end
...@@ -50,10 +50,12 @@ class Noah::App ...@@ -50,10 +50,12 @@ class Noah::App
if app.nil? if app.nil?
halt 404 halt 404
else else
configurations = [] # configs are no longer tied to apps. remove the cascade delete
Noah::Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0 # configurations = []
# Noah::Configuration.find(:application_id => app.id).sort.each {|x| configurations << x; x.delete} if app.configurations.size > 0
app.delete app.delete
r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}", "configurations" => "#{configurations.size}"} # r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}", "configurations" => "#{configurations.size}"}
r = {"result" => "success", "action" => "delete", "id" => "#{app.id}", "name" => "#{appname}"}
r.to_json r.to_json
end end
end end
......
...@@ -11,7 +11,7 @@ class Noah::App ...@@ -11,7 +11,7 @@ class Noah::App
if a.nil? if a.nil?
halt 404 halt 404
else else
c = Noah::Configuration.find(:name => element, :application_id => a.id).first c = a.configurations.find(:name => element).first
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]
c.body c.body
end end
...@@ -23,7 +23,7 @@ class Noah::App ...@@ -23,7 +23,7 @@ class Noah::App
if a.nil? if a.nil?
halt 404 halt 404
else else
Noah::Configuration.find(:application_id => a.id).sort.each {|c| config << c.to_hash} a.configurations.sort.each {|c| config << c.to_hash}
config.to_json config.to_json
end end
end end
...@@ -48,14 +48,16 @@ class Noah::App ...@@ -48,14 +48,16 @@ class Noah::App
put '/configurations/:appname/:element?' do |appname, element| put '/configurations/:appname/:element?' do |appname, element|
app = Noah::Application.find_or_create(:name => appname) app = Noah::Application.find_or_create(:name => appname)
config = Noah::Configuration.find_or_create(:name => element, :application_id => app.id) dependency_action = app.is_new? ? "created" : "updated"
config = Noah::Configuration.find_or_create(:name => element)
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.format = data["format"]; config.body = data["body"]) : (raise "Missing Parameters")
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"
dependency_action = app.is_new? ? "created" : "updated"
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, "dependencies" => dependency_action, "application" => app.name, "item" => config.name}
r.to_json r.to_json
else else
......
...@@ -51,13 +51,12 @@ module Noah ...@@ -51,13 +51,12 @@ module Noah
end end
end end
def watch!(opts={:endpoint => nil, :pattern => nil}) def watch!(opts={:endpoint => nil})
base_pattern = "#{self.patternize_me}" base_pattern = "#{self.patternize_me}"
opts[:endpoint].nil? ? (raise ArgumentError, "Need an endpoint") : endpoint=opts[:endpoint] opts[:endpoint].nil? ? (raise ArgumentError, "Need an endpoint") : endpoint=opts[:endpoint]
opts[:pattern].nil? ? pattern=base_pattern : pattern=opts[:pattern]
begin begin
w = Watcher.new :pattern => pattern, :endpoint => endpoint w = Watcher.new :pattern => base_pattern, :endpoint => endpoint
w.valid? ? w.save : (raise "#{w.errors}") w.valid? ? w.save : (raise "#{w.errors}")
w.name w.name
rescue Exception => e rescue Exception => e
......
...@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), 'configurations') ...@@ -2,7 +2,7 @@ require File.join(File.dirname(__FILE__), 'configurations')
module Noah module Noah
class Application < Model class Application < Model
attribute :name attribute :name
collection :configurations, Configuration set :configurations, Configuration
index :name index :name
...@@ -14,8 +14,9 @@ module Noah ...@@ -14,8 +14,9 @@ module Noah
def to_hash def to_hash
arr = [] arr = []
configurations.sort.each {|c| arr << c.to_hash} self.configurations.sort.each {|c| arr << c.to_hash} if self.configurations.size != 0
super.merge(:name => name, :configuration => arr, :created_at => created_at, :updated_at => updated_at) super.merge(:name => name, :configurations => arr, :created_at => created_at, :updated_at => updated_at)
#super.merge(:name => name, :created_at => created_at, :updated_at => updated_at)
end end
class << self class << self
......
require File.join(File.dirname(__FILE__), 'applications')
module Noah module Noah
class Configuration < Model class Configuration < Model
attribute :name attribute :name
attribute :format attribute :format
attribute :body attribute :body
attribute :new_record
reference :application, Application
index :name index :name
index :format index :format
...@@ -16,13 +15,11 @@ module Noah ...@@ -16,13 +15,11 @@ module Noah
assert_present :name assert_present :name
assert_present :format assert_present :format
assert_present :body assert_present :body
assert_present :application_id assert_unique :name
assert_unique [:name, :application_id]
end end
def to_hash def to_hash
Application[application_id].nil? ? app_name=nil : app_name=Application[application_id].name 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, :application => app_name)
end end
class << self class << self
......
require File.join(File.dirname(__FILE__), 'link_member')
module Noah module Noah
class Link < Model class Link < Model
attribute :path attribute :path
list :nodes, LinkMember attribute :nodes
index :path index :path
index :nodes
def nodes
arr = []
self.key[:nodes].smembers.each do |node|
arr << node
end
arr
end
def nodes=(node)
case node.class.to_s
when "Array"
node.each do |n|
self.key[:nodes].sadd(n.key)
end
else
self.key[:nodes].sadd(node.key)
end
end
def validate def validate
super super
assert_present :path assert_present :path
end end
# Nothing to see yet.
# This will be for creating "overlays" or "link" relationships def to_hash
# between arbitrary objects or modeling your data the way you want. n = Array.new
# nodes.each {|node| n << node_to_class(node).to_hash} if nodes.size > 0
# Example: h = {:name => name, :nodes => n, :created_at => created_at, :updated_at => updated_at}
# path = "/myservers" super.merge(h)
# path.nodes = ["/applications/myapp","/some/ephemeral/path", "sometag"] end
#
# would result in a structure like:
# path/
# applications/
# myapp
# some/ephemeral/path/
# child1
# child2
# child3
# sometag/
# tagged_item1
# tagged_item2
# tagged_item4
# tagged_item5
#
# The idea is to create a blended view across opinionated, tagged and
# ephemeral nodes.
#
# Almost a "choose your own model" thing.
def name def name
@name = path @name = path
end end
private
def node_to_class(node)
node.match(/^Noah::(.*):(\d+)$/)
Noah.const_get($1).send(:[], $2)
end
end end
end end
module Noah
class LinkMember
class <<self
def create(path)
path_to_instance(path)
end
protected
def path_to_instance(path)
p = path.split('/')
model, name = p[1], p[2]
x = instance_eval(Noah::PATH_MAPPING[model])
x.find(:name => name).first
end
end
end
end
...@@ -9,6 +9,13 @@ module Noah ...@@ -9,6 +9,13 @@ module Noah
def self.[](id) def self.[](id)
super(encode(id)) || create(:id => encode(id)) super(encode(id)) || create(:id => encode(id))
end end
def tagged
end
def all
end
end end
end end
...@@ -23,7 +23,7 @@ describe "Using the Application Model", :reset_redis => true do ...@@ -23,7 +23,7 @@ describe "Using the Application Model", :reset_redis => true do
end end
it "create a new Noah::Application with Configurations" do it "create a new Noah::Application with Configurations" do
a = Noah::Application.create(@appdata1) a = Noah::Application.create(@appdata1)
a.configurations << Noah::Configuration.create(@appconf_string.merge({:application => a})) a.configurations << Noah::Configuration.create(@appconf_string)
a.valid?.should == true a.valid?.should == true
a.is_new?.should == true a.is_new?.should == true
a.save a.save
......
...@@ -3,13 +3,11 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') ...@@ -3,13 +3,11 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Configuration Model", :reset_redis => true do describe "Using the Configuration Model", :reset_redis => true do
before(:each) do before(:each) do
Ohm.redis.flushdb Ohm.redis.flushdb
app = Noah::Application.create :name => "my_application" @appconf_string = {:name => "mystringconf", :format => "string", :body => "some_var"}
@appconf_string = {:name => "mystringconf", :format => "string", :body => "some_var", :application_id => app.id} @appconf_json = {:name => "myjsonconf", :format => "json", :body => @appconf_string.to_json}
@appconf_json = {:name => "myjsonconf", :format => "json", :body => @appconf_string.to_json, :application_id => app.id}
@appconf_missing_name = @appconf_string.reject {|k, v| k == :name} @appconf_missing_name = @appconf_string.reject {|k, v| k == :name}
@appconf_missing_format = @appconf_string.reject {|k, v| k == :format} @appconf_missing_format = @appconf_string.reject {|k, v| k == :format}
@appconf_missing_body = @appconf_string.reject {|k, v| k == :body} @appconf_missing_body = @appconf_string.reject {|k, v| k == :body}
@appconf_missing_application = @appconf_string.reject {|k, v| k == :application_id}
end end
after(:each) do after(:each) do
Ohm.redis.flushdb Ohm.redis.flushdb
...@@ -74,15 +72,10 @@ describe "Using the Configuration Model", :reset_redis => true do ...@@ -74,15 +72,10 @@ describe "Using the Configuration Model", :reset_redis => true do
a.valid?.should == false a.valid?.should == false
a.errors.should == [[:body, :not_present]] a.errors.should == [[:body, :not_present]]
end end
it "create a new Confguration without an application" do
a = Noah::Configuration.create(@appconf_missing_application)
a.valid?.should == false
a.errors.should == [[:application_id, :not_present]]
end
it "create a duplicate Configuration" do it "create a duplicate Configuration" do
a = Noah::Configuration.create(@appconf_string) a = Noah::Configuration.create(@appconf_string)
b = Noah::Configuration.create(@appconf_string) b = Noah::Configuration.create(@appconf_string)
b.errors.should == [[[:name, :application_id], :not_unique]] b.errors.should == [[:name, :not_unique]]
end end
end end
......
...@@ -3,9 +3,9 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') ...@@ -3,9 +3,9 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe "Using the Application API", :reset_redis => false do describe "Using the Application API", :reset_redis => false do
before(:all) do before(:all) do
@a = Noah::Application.create(:name => 'rspec_sample_app') @a = Noah::Application.create(:name => 'rspec_sample_app')
@a.configurations << Noah::Configuration.create(:name => 'rspec_config', :format => 'string', :body => 'rspec is great', :application => @a) @c = Noah::Configuration.create(:name => 'rspec_config', :format => 'string', :body => 'rspec is great')
@a.configurations << @c
@a.save @a.save
@c = @a.configurations.first
end end
describe "calling" do describe "calling" do
...@@ -38,7 +38,6 @@ describe "Using the Application API", :reset_redis => false do ...@@ -38,7 +38,6 @@ describe "Using the Application API", :reset_redis => false do
response["name"].should == @c.name response["name"].should == @c.name
response["format"].should == @c.format response["format"].should == @c.format
response["body"].should == @c.body response["body"].should == @c.body
response["application"].should == @a.name
end end
it "invalid application should not work" do it "invalid application should not work" do
get "/applications/should_not_exist" get "/applications/should_not_exist"
...@@ -96,7 +95,6 @@ describe "Using the Application API", :reset_redis => false do ...@@ -96,7 +95,6 @@ describe "Using the Application API", :reset_redis => false do
response["action"].should == "delete" response["action"].should == "delete"
response["id"].nil?.should == false response["id"].nil?.should == false
response["name"].should == @appdata[:name] response["name"].should == @appdata[:name]
response["configurations"].should == "0"
end end
it "invalid application should not work" do it "invalid application should not work" do
delete "/applications/should_not_work" delete "/applications/should_not_work"
......
...@@ -18,7 +18,6 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_ ...@@ -18,7 +18,6 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_
response.first["name"].should == "redis" response.first["name"].should == "redis"
response.first["format"].should == "string" response.first["format"].should == "string"
response.first["body"].should == "redis://127.0.0.1:6379/0" response.first["body"].should == "redis://127.0.0.1:6379/0"
response.first["application"].should == "noah"
end end
it "named configuration for application should work" do it "named configuration for application should work" do
get '/configurations/noah/redis' get '/configurations/noah/redis'
...@@ -86,7 +85,7 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_ ...@@ -86,7 +85,7 @@ describe "Using the Configuration API", :reset_redis => false, :populate_sample_
describe "DELETE" do describe "DELETE" do
before(:all) do before(:all) do
@a = Noah::Application.create(:name => 'delete_test_app') @a = Noah::Application.create(:name => 'delete_test_app')
cparms = {:name => 'a', :format => 'string', :body => 'asdf', :application_id => @a.id} cparms = {:name => 'a', :format => 'string', :body => 'asdf'}
@a.configurations << Noah::Configuration.create(cparms) @a.configurations << Noah::Configuration.create(cparms)
@a.save @a.save
@c = @a.configurations.first @c = @a.configurations.first
......
...@@ -29,9 +29,9 @@ RSpec.configure do |config| ...@@ -29,9 +29,9 @@ RSpec.configure do |config|
a = Noah::Application.create(:name => 'noah') a = Noah::Application.create(:name => 'noah')
if a.save if a.save
cr = Noah::Configuration.create(:name => 'redis', :format => 'string', :body => 'redis://127.0.0.1:6379/0', :application => a) 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', :application => a) ch = Noah::Configuration.create(:name => 'host', :format => 'string', :body => 'localhost')
cp = Noah::Configuration.create(:name => 'port', :format => 'string', :body => '9292', :application => a) cp = Noah::Configuration.create(:name => 'port', :format => 'string', :body => '9292')
%w[cr ch cp].each do |c| %w[cr ch cp].each do |c|
a.configurations << eval(c) a.configurations << eval(c)
end end
...@@ -53,13 +53,13 @@ EOJ ...@@ -53,13 +53,13 @@ EOJ
a1 = Noah::Application.create(:name => 'myrailsapp1') a1 = Noah::Application.create(:name => 'myrailsapp1')
if a1.save if a1.save
c1 = Noah::Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml, :application => a1) c1 = Noah::Configuration.create(:name => 'database.yml', :format => 'yaml', :body => my_yaml)
a1.configurations << c1 a1.configurations << c1
end end
a2 = Noah::Application.create(:name => 'myrestapp1') a2 = Noah::Application.create(:name => 'myrestapp1')
if a2.save if a2.save
c2 = Noah::Configuration.create(:name => 'config.json', :format => 'json', :body => my_json, :application => a2) c2 = Noah::Configuration.create(:name => 'config.json', :format => 'json', :body => my_json)
a2.configurations << c2 a2.configurations << c2
end end
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