Class: WavefrontHclOutput::Base

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

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.



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

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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/wavefront-cli/output/hcl/base.rb', line 6

def options
  @options
end

#respObject (readonly)

Returns the value of attribute resp.



6
7
8
# File 'lib/wavefront-cli/output/hcl/base.rb', line 6

def resp
  @resp
end

Instance Method Details

#close_outputObject



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

def close_output
  '}'
end

#handler(k, v) ⇒ String

Format each key-value pair

Parameters:

  • k (String)

    key

  • v (Any)

    value

Returns:



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/wavefront-cli/output/hcl/base.rb', line 56

def handler(k, v)
  key_handler = "khandle_#{k}".to_sym
  value_handler = "vhandle_#{k}".to_sym
  quote_handler = "qhandle_#{k}".to_sym
  k = send(key_handler) if respond_to?(key_handler)
  v = send(value_handler, v) if respond_to?(value_handler)

  quote_handler = :quote_value unless respond_to?(quote_handler)

  format('  %s = %s', k.to_snake, send(quote_handler, v))
end

#hcl_fieldsArray

Fields which the provider requires.

Returns:



22
23
24
# File 'lib/wavefront-cli/output/hcl/base.rb', line 22

def hcl_fields
  []
end

#open_outputObject



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

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

#quote_value(v) ⇒ String

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

Parameters:

  • v (Object)

    value

Returns:



83
84
85
86
87
88
89
90
# File 'lib/wavefront-cli/output/hcl/base.rb', line 83

def quote_value(v)
  case v.class.to_s.to_sym
  when :String
    format('"%s"', v.gsub(/\"/, '\"'))
  else
    v
  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



46
47
48
49
# File 'lib/wavefront-cli/output/hcl/base.rb', line 46

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



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

def resource_name
  options[:class]
end

#runObject



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

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

#vhandle_tags(v) ⇒ Array

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

Parameters:

Returns:

  • (Array)

    of soft-quoted tags



73
74
75
76
# File 'lib/wavefront-cli/output/hcl/base.rb', line 73

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