Add JSON API for changing config by section

This commit is contained in:
uu59 2014-11-25 17:51:57 +09:00 committed by uu59
parent 645c2bbefc
commit 86351718f3
7 changed files with 67 additions and 1 deletions

View File

@ -0,0 +1,45 @@
class Api::SettingsController < ApplicationController
before_action :login_required
before_action :find_fluentd
before_action :set_config
before_action :set_section, only: [:show, :update, :destroy]
helper_method :element_id
respond_to :json
def index
end
def update
coming = Fluent::Config::V1Parser.parse(params[:body], @fluentd.config_file)
current = @section
index = @config.elements.index current
@config.elements[index] = coming.elements.first
@config.write_to_file
head :no_content # 204
end
def destroy
@config.elements.delete @section
@config.write_to_file
head :no_content # 204
end
private
def set_config
# TODO: not found
@config = Fluentd::Setting::Config.new(@fluentd.config_file)
end
def set_section
# TODO: not found
@section = @config.elements.find do |elm|
element_id(elm) == params[:id]
end
end
def element_id(element)
index = @config.elements.index(element) # TODO: not found
"#{"%06d" % index}#{Digest::MD5.hexdigest(element.to_s)}"
end
end

View File

@ -4,7 +4,7 @@ class Fluentd
module Setting
class Config
attr_reader :fl_config, :file
delegate :elements, :to_s, to: :fl_config
delegate :elements, to: :fl_config
def initialize(config_file)
@fl_config = Fluent::Config.parse(IO.read(config_file), config_file, nil, true)
@ -26,6 +26,14 @@ class Fluentd
elm.name == "match"
end
end
def write_to_file
File.open(file, "w"){|f| f.write formatted }
end
def formatted
fl_config.to_s.gsub(/<\/?ROOT>/, "").strip_heredoc.gsub(%r|^</.*?>$|, "\\0\n")
end
end
end
end

View File

@ -0,0 +1,6 @@
json.id element_id(element)
json.name element.name
json.type element["type"]
json.arg element.arg
json.settings element
json.content element.to_s

View File

@ -0,0 +1,3 @@
json.array! @config.elements do |elm|
json.partial! "api/settings/element", element: elm
end

View File

@ -0,0 +1 @@
json.partial! "api/settings/element", element: @section

View File

@ -19,6 +19,7 @@ require "jquery-rails"
require "sucker_punch"
require "settingslogic"
require "kramdown-haml"
require "jbuilder"
module FluentdUi
class Application < Rails::Application

View File

@ -93,5 +93,7 @@ Rails.application.routes.draw do
get "file_preview"
post "regexp_preview"
post "grok_to_regexp"
resources :settings, only: [:index, :show, :update, :destroy], defaults: { format: "json" }
end
end