mirror of
https://github.com/fluent/fluentd-ui.git
synced 2025-08-12 01:07:09 +02:00
Remove unused module
Signed-off-by: Kenji Okimoto <okimoto@clear-code.com>
This commit is contained in:
parent
c1398a3acd
commit
a849fdeda3
@ -1,185 +0,0 @@
|
||||
class Fluentd
|
||||
module Setting
|
||||
module Common
|
||||
extend ActiveSupport::Concern
|
||||
include ActiveModel::Model
|
||||
|
||||
module ClassMethods
|
||||
attr_accessor :values, :types, :children, :hidden_values
|
||||
|
||||
def choice(key, values)
|
||||
@values ||= {}
|
||||
@values[key] = values
|
||||
set_type(:choice, [key])
|
||||
end
|
||||
|
||||
def hidden(key)
|
||||
set_type(:hidden, [key])
|
||||
end
|
||||
|
||||
def nested(key, klass, options = {})
|
||||
# e.g.:
|
||||
# <match>
|
||||
# type forward
|
||||
# <server>
|
||||
# ..
|
||||
# </server>
|
||||
# </match>
|
||||
@children ||= {}
|
||||
@children[key] = {
|
||||
class: klass,
|
||||
options: options,
|
||||
}
|
||||
set_type(:nested, [key])
|
||||
end
|
||||
|
||||
def booleans(*keys)
|
||||
# e.g.:
|
||||
# use_ssl true
|
||||
# include_time_key false
|
||||
set_type(:boolean, keys)
|
||||
end
|
||||
|
||||
def flags(*keys)
|
||||
# e.g.:
|
||||
# tag_mapped
|
||||
# utc
|
||||
set_type(:flag, keys)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_type(type, keys)
|
||||
@types ||= {}
|
||||
keys.each do |key|
|
||||
@types[key] = type
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def children_of(key)
|
||||
meta = self.class.children[key]
|
||||
return unless meta
|
||||
klass = meta[:class]
|
||||
data = send(key) || {"0" => {}}
|
||||
children = []
|
||||
data.each_pair do |index, attrs|
|
||||
children << klass.new(attrs)
|
||||
end
|
||||
children
|
||||
end
|
||||
|
||||
def child_class(key)
|
||||
self.class.children[key][:class]
|
||||
end
|
||||
|
||||
def values_of(key)
|
||||
self.class.values.try(:[], key) || []
|
||||
end
|
||||
|
||||
def column_type(key)
|
||||
self.class.types.try(:[], key) || "string"
|
||||
end
|
||||
|
||||
def conf(key)
|
||||
case column_type(key)
|
||||
when :boolean
|
||||
boolenan(key)
|
||||
when :flag
|
||||
flag(key)
|
||||
when :nested
|
||||
return "" unless send(key)
|
||||
klass = child_class(key)
|
||||
send(key).map do |(_, child)|
|
||||
# send("servers")
|
||||
#
|
||||
# "servers" => {
|
||||
# "0" => {
|
||||
# "name" => "foo",
|
||||
# "host" => "bar",
|
||||
# ..
|
||||
# },
|
||||
# "1" => {
|
||||
# ..
|
||||
# }
|
||||
# }
|
||||
child_instance = klass.new(child)
|
||||
unless child_instance.empty_value?
|
||||
"\n" + child_instance.to_config(key).gsub(/^/m, " ")
|
||||
end
|
||||
end.join
|
||||
else # including :hidden
|
||||
print_if_present(key)
|
||||
end
|
||||
end
|
||||
|
||||
def plugin_type_name
|
||||
# Fluentd::Setting::OutS3 -> s3
|
||||
# Override this method if not above style
|
||||
try(:plugin_name) || self.class.to_s.split("::").last.sub(/(In|Out)/, "").downcase
|
||||
end
|
||||
|
||||
def print_if_present(key)
|
||||
# e.g.:
|
||||
# path /var/log/td/aaa
|
||||
# user nobody
|
||||
# retry_limit 3
|
||||
send(key).present? ? "#{key} #{send(key)}" : ""
|
||||
end
|
||||
|
||||
def boolenan(key)
|
||||
send(key).presence == "true" ? "#{key} true" : "#{key} false"
|
||||
end
|
||||
|
||||
def flag(key)
|
||||
send(key).presence == "true" ? key.to_s : ""
|
||||
end
|
||||
|
||||
def empty_value?
|
||||
config = ""
|
||||
self.class.const_get(:KEYS).each do |key|
|
||||
config << conf(key)
|
||||
end
|
||||
config.empty?
|
||||
end
|
||||
|
||||
def input_plugin?
|
||||
self.class.to_s.match(/::In|^In/)
|
||||
end
|
||||
|
||||
def output_plugin?
|
||||
not input_plugin?
|
||||
end
|
||||
|
||||
def to_config(elm_name = nil)
|
||||
indent = " "
|
||||
if elm_name
|
||||
config = "<#{elm_name}>\n"
|
||||
else
|
||||
if input_plugin?
|
||||
config = "<source>\n"
|
||||
else
|
||||
config = "<match #{match}>\n"
|
||||
end
|
||||
config << "#{indent}type #{plugin_type_name}\n"
|
||||
end
|
||||
self.class.const_get(:KEYS).each do |key|
|
||||
next if key == :match
|
||||
config << indent
|
||||
config << conf(key)
|
||||
config << "\n"
|
||||
end
|
||||
if elm_name
|
||||
config << "</#{elm_name}>\n"
|
||||
else
|
||||
if input_plugin?
|
||||
config << "</source>\n"
|
||||
else
|
||||
config << "</match>\n"
|
||||
end
|
||||
end
|
||||
config.gsub(/^[ ]*\n/m, "")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
@ -1,178 +0,0 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Fluentd::Setting::Common do
|
||||
let(:klass) { Fluentd::Setting::Common }
|
||||
|
||||
describe "class methods" do
|
||||
describe "#booleans" do
|
||||
subject do
|
||||
Class.new do
|
||||
include Fluentd::Setting::Common
|
||||
attr_accessor :bool1, :bool2, :foo
|
||||
booleans :bool1, :bool2
|
||||
end.new
|
||||
end
|
||||
|
||||
it { subject.column_type(:bool1).should == :boolean }
|
||||
it { subject.column_type(:bool2).should == :boolean }
|
||||
it { subject.column_type(:foo).should_not == :boolean }
|
||||
end
|
||||
|
||||
describe "#flags" do
|
||||
subject do
|
||||
Class.new do
|
||||
include Fluentd::Setting::Common
|
||||
attr_accessor :flag1, :flag2, :foo
|
||||
flags :flag1, :flag2
|
||||
end.new
|
||||
end
|
||||
|
||||
it { subject.column_type(:flag1).should == :flag }
|
||||
it { subject.column_type(:flag2).should == :flag }
|
||||
it { subject.column_type(:foo).should_not == :flag }
|
||||
end
|
||||
|
||||
describe "#hidden" do
|
||||
subject do
|
||||
Class.new do
|
||||
include Fluentd::Setting::Common
|
||||
attr_accessor :hide
|
||||
hidden :hide
|
||||
end.new
|
||||
end
|
||||
|
||||
it { subject.column_type(:hide).should == :hidden }
|
||||
end
|
||||
|
||||
describe "#choice" do
|
||||
subject do
|
||||
Class.new do
|
||||
include Fluentd::Setting::Common
|
||||
attr_accessor :choice, :foo
|
||||
choice :choice, %w(a b c)
|
||||
end.new
|
||||
end
|
||||
|
||||
it { subject.column_type(:choice).should == :choice }
|
||||
it { subject.values_of(:choice).should == %w(a b c) }
|
||||
it { subject.column_type(:foo).should_not == :choice }
|
||||
end
|
||||
|
||||
describe "#nested" do
|
||||
before do
|
||||
@child_class = Class.new do
|
||||
include Fluentd::Setting::Common
|
||||
end
|
||||
end
|
||||
|
||||
subject do
|
||||
child = @child_class
|
||||
Class.new do
|
||||
include Fluentd::Setting::Common
|
||||
attr_accessor :child, :foo
|
||||
nested :child, child
|
||||
end.new
|
||||
end
|
||||
|
||||
it { subject.column_type(:child).should == :nested }
|
||||
it { subject.child_class(:child).should == @child_class }
|
||||
it { subject.column_type(:foo).should_not == :nested }
|
||||
end
|
||||
end
|
||||
|
||||
describe "instance methods" do
|
||||
describe "plugin name" do
|
||||
before do
|
||||
klass = Class.new do
|
||||
include Fluentd::Setting::Common
|
||||
end
|
||||
Object.const_set(class_name, klass)
|
||||
end
|
||||
after { Object.send(:remove_const, class_name) }
|
||||
subject { Object.const_get(class_name).new }
|
||||
|
||||
context "InFoo" do
|
||||
let(:class_name) { "InFoo" }
|
||||
it "plugin_type_name == foo" do
|
||||
subject.plugin_type_name.should == "foo"
|
||||
end
|
||||
it "should be input_plugin" do
|
||||
subject.should be_input_plugin
|
||||
end
|
||||
end
|
||||
|
||||
context "OutBar" do
|
||||
let(:class_name) { "OutBar" }
|
||||
it "plugin_type_name == bar" do
|
||||
subject.plugin_type_name.should == "bar"
|
||||
end
|
||||
it "should be output_plugin" do
|
||||
subject.should be_output_plugin
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "generate config file" do
|
||||
before do
|
||||
class Child
|
||||
include Fluentd::Setting::Common
|
||||
KEYS = [:child_foo]
|
||||
attr_accessor(*KEYS)
|
||||
end
|
||||
@klass = Class.new do
|
||||
include Fluentd::Setting::Common
|
||||
const_set(:KEYS, [:key1, :key2, :flag1, :hide, :ch, :child, :string])
|
||||
attr_accessor(*const_get(:KEYS))
|
||||
booleans :key1, :key2
|
||||
flags :flag1
|
||||
hidden :hide
|
||||
choice :ch, %w(foo bar)
|
||||
nested :child, Child
|
||||
end
|
||||
end
|
||||
after { Object.send(:remove_const, :Child) }
|
||||
|
||||
subject { @klass.new(params).to_config("dummy") }
|
||||
|
||||
describe "boolean" do
|
||||
# NOTE: "true" and "false" are the string because the are given by HTTP request
|
||||
let(:params) { {key1: "true", key2: "false"} }
|
||||
|
||||
it { should include("key1 true\n") }
|
||||
it { should include("key2 false\n") }
|
||||
end
|
||||
|
||||
describe "flag" do
|
||||
context "true" do
|
||||
let(:params) { {flag1: "true"} }
|
||||
it { should include("flag1\n") }
|
||||
end
|
||||
context "false" do
|
||||
let(:params) { {flag1: "false"} }
|
||||
it { should_not include("flag1\n") }
|
||||
end
|
||||
end
|
||||
|
||||
describe "hidden" do
|
||||
let(:params) { {hide: "foo"} }
|
||||
it { should include("hide foo\n") }
|
||||
end
|
||||
|
||||
describe "choice" do
|
||||
let(:params) { {ch: "foo"} }
|
||||
it { should include("ch foo\n") }
|
||||
end
|
||||
|
||||
describe "string" do
|
||||
let(:params) { {string: "foobar"} }
|
||||
it { should include("string foobar\n") }
|
||||
end
|
||||
|
||||
describe "nested" do
|
||||
let(:params) { {child: {"0" => { child_foo: "hi" }}} }
|
||||
it { should match(%r!<child>\n\s+child_foo hi\n\s+</child>!) }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user