Module: Fluent::Config::SectionGenerator

Defined in:
lib/fluent/config/section.rb

Class Method Summary collapse

Class Method Details

.check_unused_section(proxy, conf, plugin_class) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/fluent/config/section.rb', line 206

def self.check_unused_section(proxy, conf, plugin_class)
  elems = conf.respond_to?(:elements) ? conf.elements : []
  elems.each { |e|
    next if plugin_class.nil? && Fluent::Config::V1Parser::ELEM_SYMBOLS.include?(e.name) # skip pre-defined non-plugin elements because it doens't have proxy section

    unless proxy.sections.any? { |name, subproxy| e.name == subproxy.name.to_s || e.name == subproxy.alias.to_s }
      parent_name = if conf.arg.empty?
                      conf.name
                    else
                      "#{conf.name} #{conf.arg}"
                    end
      e.unused_in = [parent_name, plugin_class]
    end
  }
end

.generate(proxy, conf, logger, plugin_class, stack = []) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/fluent/config/section.rb', line 109

def self.generate(proxy, conf, logger, plugin_class, stack = [])
  return nil if conf.nil?

  section_stack = ""
  unless stack.empty?
    section_stack = ", in section " + stack.join(" > ")
  end

  section_params = {}

  proxy.defaults.each_pair do |name, defval|
    varname = name.to_sym
    section_params[varname] = (defval.dup rescue defval)
  end

  if proxy.argument
    unless conf.arg.empty?
      key, block, opts = proxy.argument
      section_params[key] = self.instance_exec(conf.arg, opts, name, &block)
    end
    unless section_params.has_key?(proxy.argument.first)
      logger.error "config error in:\n#{conf}" if logger # logger should exist, but somethimes it's nil (e.g, in tests)
      raise ConfigError, "'<#{proxy.name} ARG>' section requires argument" + section_stack
    end
    # argument should NOT be deprecated... (argument always has a value: '')
  end

  proxy.params.each_pair do |name, defval|
    varname = name.to_sym
    block, opts = defval
    if conf.has_key?(name.to_s) || opts[:alias] && conf.has_key?(opts[:alias].to_s)
      val = if conf.has_key?(name.to_s)
              conf[name.to_s]
            else
              conf[opts[:alias].to_s]
            end
      section_params[varname] = self.instance_exec(val, opts, name, &block)

      # Source of definitions of deprecated/obsoleted:
      # https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features
      #
      # Deprecated: These deprecated features can still be used, but should be used with caution
      #             because they are expected to be removed entirely sometime in the future.
      # Obsoleted: These obsolete features have been entirely removed from JavaScript and can no longer be used.
      if opts[:deprecated]
        logger.warn "'#{name}' parameter is deprecated: #{opts[:deprecated]}" if logger
      end
      if opts[:obsoleted]
        logger.error "config error in:\n#{conf}" if logger
        raise ObsoletedParameterError, "'#{name}' parameter is already removed: #{opts[:obsoleted]}" + section_stack
      end
    end
    unless section_params.has_key?(varname)
      logger.error "config error in:\n#{conf}" if logger
      raise ConfigError, "'#{name}' parameter is required" + section_stack
    end
  end

  check_unused_section(proxy, conf, plugin_class)

  proxy.sections.each do |name, subproxy|
    varname = subproxy.variable_name
    elements = (conf.respond_to?(:elements) ? conf.elements : []).select{ |e| e.name == subproxy.name.to_s || e.name == subproxy.alias.to_s }
    if elements.empty? && subproxy.init?
      if subproxy.argument && !subproxy.defaults.has_key?(subproxy.argument.first)
        raise ArgumentError, "#{name}: init is specified, but default value of argument is missing"
      end
      missing_keys = subproxy.params.keys.select{|param_name| !subproxy.defaults.has_key?(param_name)}
      if !missing_keys.empty?
        raise ArgumentError, "#{name}: init is specified, but there're parameters without default values:#{missing_keys.join(',')}"
      end
      elements << Fluent::Config::Element.new(subproxy.name.to_s, '', {}, [])
    end

    # set subproxy for secret option
    elements.each { |element|
      element.corresponding_proxies << subproxy
    }

    if subproxy.required? && elements.size < 1
      logger.error "config error in:\n#{conf}" if logger
      raise ConfigError, "'<#{subproxy.name}>' sections are required" + section_stack
    end
    if subproxy.multi?
      section_params[varname] = elements.map{ |e| generate(subproxy, e, logger, plugin_class, stack + [subproxy.name]) }
    else
      if elements.size > 1
        logger.error "config error in:\n#{conf}" if logger
        raise ConfigError, "'<#{subproxy.name}>' section cannot be written twice or more" + section_stack
      end
      section_params[varname] = generate(subproxy, elements.first, logger, plugin_class, stack + [subproxy.name])
    end
  end

  Section.new(section_params, conf)
end