mirror of
https://github.com/fluent/fluentd-ui.git
synced 2026-05-04 18:36:13 +02:00
Add JSON API for changing config by section
This commit is contained in:
parent
645c2bbefc
commit
86351718f3
45
app/controllers/api/settings_controller.rb
Normal file
45
app/controllers/api/settings_controller.rb
Normal 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
|
||||
@ -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
|
||||
|
||||
6
app/views/api/settings/_element.json.jbuilder
Normal file
6
app/views/api/settings/_element.json.jbuilder
Normal 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
|
||||
3
app/views/api/settings/index.json.jbuilder
Normal file
3
app/views/api/settings/index.json.jbuilder
Normal file
@ -0,0 +1,3 @@
|
||||
json.array! @config.elements do |elm|
|
||||
json.partial! "api/settings/element", element: elm
|
||||
end
|
||||
1
app/views/api/settings/show.json.jbuilder
Normal file
1
app/views/api/settings/show.json.jbuilder
Normal file
@ -0,0 +1 @@
|
||||
json.partial! "api/settings/element", element: @section
|
||||
@ -19,6 +19,7 @@ require "jquery-rails"
|
||||
require "sucker_punch"
|
||||
require "settingslogic"
|
||||
require "kramdown-haml"
|
||||
require "jbuilder"
|
||||
|
||||
module FluentdUi
|
||||
class Application < Rails::Application
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user