Commit 3792d7a0 authored by John E. Vincent's avatar John E. Vincent

PUT api for configurations working. more helpers in models

parent 74dfc492
...@@ -127,19 +127,18 @@ end ...@@ -127,19 +127,18 @@ end
namespace '/c' do namespace '/c' do
# Need to move this out to configuration.
# Maybe bootstrap them from itself?
content_type_mapping = {
:yaml => "text/x-yaml",
:json => "application/json",
:xml => "text/xml"
}
get '/:appname/:element/?' do get '/:appname/:element/?' do
a = Application.find(:name => params[:appname]).first a = Application.find(:name => params[:appname]).first
c = Configuration.find(:name => params[:element], :application_id => a.id).first c = Configuration.find(:name => params[:element], :application_id => a.id).first
case c.format content_type content_type_mapping[c.format.to_sym] if content_type_mapping[c.format.to_sym]
when "json"
content_type 'application/json'
when "xml"
content_type 'text/xml'
when "yaml"
content_type 'text/x-yaml'
else
content_type 'text/plain'
end
"#{c.body}" "#{c.body}"
end end
...@@ -156,6 +155,30 @@ namespace '/c' do ...@@ -156,6 +155,30 @@ namespace '/c' do
"#{configs.to_json}" "#{configs.to_json}"
end end
put '/:appname/:element?' do |appname, item|
begin
app = Application.find_or_create(:name => appname)
config = Configuration.find_or_create(:name => item, :application_id => app.id)
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")
if config.valid?
config.save
status 200
r = {"result" => "success","id" => "#{config.id}"}
r.to_json
else
status 500
r = {"result" => "failure","error" => "#{config.errors}"}
r.to_json
end
rescue Exception => e
status 500
r = {"result" => "failure","error" => "#{e.message}"}
r.to_json
end
end
end end
# A route for adding a new host # A route for adding a new host
......
...@@ -68,6 +68,16 @@ class Configuration < Ohm::Model ...@@ -68,6 +68,16 @@ class Configuration < Ohm::Model
def to_hash def to_hash
super.merge(:name => name, :format => format, :body => body, :update_at => updated_at, :application => Application[application_id].name) super.merge(:name => name, :format => format, :body => body, :update_at => updated_at, :application => Application[application_id].name)
end end
class << self
def find_or_create(opts={})
begin
find(opts).first.nil? ? (conf = create(opts)) : (conf = find(opts).first)
rescue Exception => e
e.message
end
end
end
end end
class Application < Ohm::Model class Application < Ohm::Model
...@@ -90,6 +100,22 @@ class Application < Ohm::Model ...@@ -90,6 +100,22 @@ class Application < Ohm::Model
configurations.sort.each {|c| arr << c.to_hash} configurations.sort.each {|c| arr << c.to_hash}
super.merge(:name => name, :updated_at => updated_at, :configurations => arr) super.merge(:name => name, :updated_at => updated_at, :configurations => arr)
end end
class << self
def find_or_create(opts = {})
begin
find(opts).first.nil? ? (app = create(opts)) : (app = find(opts).first)
if app.valid?
app.save
app
else
raise app.errors
end
rescue Exception => e
e.message
end
end
end
end end
class Watcher < Ohm::Model #NYI class Watcher < Ohm::Model #NYI
...@@ -111,7 +137,7 @@ class Watcher < Ohm::Model #NYI ...@@ -111,7 +137,7 @@ class Watcher < Ohm::Model #NYI
end end
end end
# Some pluralized helper objects # Some pluralized helper models
class Hosts class Hosts
def self.all(options = {}) def self.all(options = {})
options.empty? ? Host.all.sort : Host.find(options).sort options.empty? ? Host.all.sort : Host.find(options).sort
......
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