class WavefrontHclOutput::Base

Output stuff for Hashicorp Configuration Language

Attributes

options[R]
resp[R]

Public Class Methods

new(resp, options) click to toggle source
# File lib/wavefront-cli/output/hcl/base.rb, line 13
def initialize(resp, options)
  @resp = resp
  @options = options
end

Public Instance Methods

close_output() click to toggle source
# File lib/wavefront-cli/output/hcl/base.rb, line 37
def close_output
  '}'
end
handler(key, val) click to toggle source

Format each key-value pair @param key [String] key @param val [Any] value @return [String]

# File lib/wavefront-cli/output/hcl/base.rb, line 63
def handler(key, val)
  key_handler = "khandle_#{key}".to_sym
  value_handler = "vhandle_#{key}".to_sym
  quote_handler = "qhandle_#{key}".to_sym
  key = send(key_handler) if respond_to?(key_handler)
  val = send(value_handler, val) if respond_to?(value_handler)

  quote_handler = :quote_value unless respond_to?(quote_handler)

  format('  %<key>s = %<value>s',
         key: key.to_snake,
         value: send(quote_handler, val))
end
hcl_fields() click to toggle source

Fields which the provider requires. @return [Array] of strings

# File lib/wavefront-cli/output/hcl/base.rb, line 27
def hcl_fields
  []
end
open_output() click to toggle source
# File lib/wavefront-cli/output/hcl/base.rb, line 31
def open_output
  format('resource "wavefront_%<name>s" "%<uuid>s" {',
         name: resource_name,
         uuid: SecureRandom.uuid)
end
quote_value(val) click to toggle source

Some values need to be quoted, some need to be escaped etc etc. @param val [Object] value @return [String]

# File lib/wavefront-cli/output/hcl/base.rb, line 92
def quote_value(val)
  case val.class.to_s.to_sym
  when :String
    format('"%<value>s"', value: val.gsub(/"/, '\"'))
  else
    val
  end
end
required_fields() click to toggle source

The provider can only handle certain keys. Each class should provide a list of things it knows the provider requires. If it does not, we display everything

# File lib/wavefront-cli/output/hcl/base.rb, line 52
def required_fields
  return resp if hcl_fields.empty?

  resp.select { |k, _v| hcl_fields.include?(k) }
end
resource_name() click to toggle source

Override this if the provider calls a resource something other than the name of the inheriting class

# File lib/wavefront-cli/output/hcl/base.rb, line 44
def resource_name
  options[:class]
end
run() click to toggle source
# File lib/wavefront-cli/output/hcl/base.rb, line 18
def run
  puts open_output
  required_fields.each { |k, v| puts handler(k, v) }
  puts close_output
end
vhandle_tags(val) click to toggle source

Tags need to be in an array. They aren't always called “tags” by the API. @param val [Array,Hash,String] tags @return [Array] of soft-quoted tags

# File lib/wavefront-cli/output/hcl/base.rb, line 82
def vhandle_tags(val)
  val = val.values if val.is_a?(Hash)
  Array(val).flatten
end