Class: WavefrontHclOutput::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront-cli/output/hcl/base.rb

Overview

Output stuff for Hashicorp Configuration Language

Direct Known Subclasses

Alert, Dashboard, Notificant

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resp, options) ⇒ Base



11
12
13
14
# File 'lib/wavefront-cli/output/hcl/base.rb', line 11

def initialize(resp, options)
  @resp = resp
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



9
10
11
# File 'lib/wavefront-cli/output/hcl/base.rb', line 9

def options
  @options
end

#respObject (readonly)

Returns the value of attribute resp.



9
10
11
# File 'lib/wavefront-cli/output/hcl/base.rb', line 9

def resp
  @resp
end

Instance Method Details

#close_outputObject



34
35
36
# File 'lib/wavefront-cli/output/hcl/base.rb', line 34

def close_output
  '}'
end

#handler(key, val) ⇒ String

Format each key-value pair



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/wavefront-cli/output/hcl/base.rb', line 59

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('  %s = %s', key.to_snake, send(quote_handler, val))
end

#hcl_fieldsArray

Fields which the provider requires.



25
26
27
# File 'lib/wavefront-cli/output/hcl/base.rb', line 25

def hcl_fields
  []
end

#open_outputObject



29
30
31
32
# File 'lib/wavefront-cli/output/hcl/base.rb', line 29

def open_output
  format('resource "wavefront_%s" "%s" {', resource_name,
         SecureRandom.uuid)
end

#quote_value(val) ⇒ String

Some values need to be quoted, some need to be escaped etc etc.



86
87
88
89
90
91
92
93
# File 'lib/wavefront-cli/output/hcl/base.rb', line 86

def quote_value(val)
  case val.class.to_s.to_sym
  when :String
    format('"%s"', val.gsub(/\"/, '\"'))
  else
    val
  end
end

#required_fieldsObject

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



49
50
51
52
# File 'lib/wavefront-cli/output/hcl/base.rb', line 49

def required_fields
  return resp if hcl_fields.empty?
  resp.select { |k, _v| hcl_fields.include?(k) }
end

#resource_nameObject

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



41
42
43
# File 'lib/wavefront-cli/output/hcl/base.rb', line 41

def resource_name
  options[:class]
end

#runObject



16
17
18
19
20
# File 'lib/wavefront-cli/output/hcl/base.rb', line 16

def run
  puts open_output
  required_fields.each { |k, v| puts handler(k, v) }
  puts close_output
end

#vhandle_tags(val) ⇒ Array

Tags need to be in an array. They aren’t always called “tags” by the API.



76
77
78
79
# File 'lib/wavefront-cli/output/hcl/base.rb', line 76

def vhandle_tags(val)
  val = val.values if val.is_a?(Hash)
  Array(val).flatten
end