Class: Fluent::EC2Metadata::PlaceholderExpander

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/ec2_metadata.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log) ⇒ PlaceholderExpander



126
127
128
# File 'lib/fluent/plugin/ec2_metadata.rb', line 126

def initialize(log)
  @log = log
end

Instance Attribute Details

#placeholdersObject (readonly)



132
133
134
# File 'lib/fluent/plugin/ec2_metadata.rb', line 132

def placeholders
  @placeholders
end

Instance Method Details

#expand(str, placeholders) ⇒ Object



152
153
154
155
156
157
# File 'lib/fluent/plugin/ec2_metadata.rb', line 152

def expand(str, placeholders)
  str.gsub(/(\${[a-z_:\-]+(\[-?[0-9]+\])?}|__[A-Z_]+__)/) {
    @log.warn "ec2-metadata: unknown placeholder `#{$1}` found in a tag `#{placeholders['${tag}']}`" unless placeholders.include?($1)
    placeholders[$1]
  }
end

#prepare_placeholders(_record, tag, tag_parts, ec2_metadata) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/fluent/plugin/ec2_metadata.rb', line 134

def prepare_placeholders(_record, tag, tag_parts, )
  placeholders = {
    '${tag}' => tag,
  }

  size = tag_parts.size
  tag_parts.each_with_index { |t, idx|
    placeholders.store("${tag_parts[#{idx}]}", t)
    placeholders.store("${tag_parts[#{idx-size}]}", t) # support tag_parts[-1]
  }

  .each { |k, v|
    placeholders.store("${#{k}}", v)
  }

  placeholders
end