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

Returns a new instance of Base.



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

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#respObject (readonly)

Returns the value of attribute resp.



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

def resp
  @resp
end

Instance Method Details

#close_outputObject



37
38
39
# File 'lib/wavefront-cli/output/hcl/base.rb', line 37

def close_output
  '}'
end

#handler(key, val) ⇒ String

Format each key-value pair

Parameters:

  • key (String)

    key

  • val (Any)

    value

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/wavefront-cli/output/hcl/base.rb', line 63

def handler(key, val)
  key_handler = :"khandle_#{key}"
  value_handler = :"vhandle_#{key}"
  quote_handler = :"qhandle_#{key}"
  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_fieldsArray

Fields which the provider requires.

Returns:



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

def hcl_fields
  []
end

#open_outputObject



31
32
33
34
35
# 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) ⇒ String

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

Parameters:

  • val (Object)

    value

Returns:



92
93
94
95
96
97
98
99
# 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_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



52
53
54
55
56
# 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_nameObject

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



44
45
46
# File 'lib/wavefront-cli/output/hcl/base.rb', line 44

def resource_name
  options[:class]
end

#runObject



18
19
20
21
22
# 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) ⇒ Array

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

Parameters:

Returns:

  • (Array)

    of soft-quoted tags



82
83
84
85
# 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