Class: WavefrontHclOutput::Dashboard

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

Overview

This is a rather kludgy class which generates HCL output suitable for the Wavefront Terraform provider. It has to work round a number of inconsistencies and omissions in said provider, and will have to change as the provider improves.

It works, manually, down the hierarchy described in github.com/spaceapegames/terraform-provider-wavefront/blob/master/wavefront/resource_dashboard.go

Instance Attribute Summary

Attributes inherited from Base

#options, #resp

Instance Method Summary collapse

Methods inherited from Base

#close_output, #handler, #initialize, #open_output, #required_fields, #resource_name, #run, #vhandle_tags

Constructor Details

This class inherits a constructor from WavefrontHclOutput::Base

Instance Method Details

#handle_chart(chart) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/wavefront-cli/output/hcl/dashboard.rb', line 51

def handle_chart(chart)
  fields = %w[units name description]

  lines = chart.each_with_object([]) do |(k, v), a|
    a.<< format('%s = %s', k, quote_value(v)) if fields.include?(k)
  end

  lines.<< "source = #{handle_sources(chart[:sources])}"
  lines.to_hcl_obj(10)
end

#handle_charts(charts) ⇒ Object



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

def handle_charts(charts)
  listmaker(charts, :handle_chart)
end

#handle_rows(rows) ⇒ Object



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

def handle_rows(rows)
  rows.each_with_object([]) do |row, a|
    a.<< ("chart = " + handle_charts(row[:charts]).to_s).braced(8)
  end.to_hcl_list
end

#handle_source(source) ⇒ Object



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

def handle_source(source)
  fields = %w[name query disabled scatterPlotSource querybuilderEnabled
              sourceDescription]

  source.each_with_object([]) do |(k, v), a|
    if fields.include?(k)
      k = 'queryBuilderEnabled' if k == 'querybuilderEnabled'
      a.<< format('%s = %s', k.to_snake, quote_value(v))
    end
  end.to_hcl_obj(14)
end

#handle_sources(sources) ⇒ Object



62
63
64
# File 'lib/wavefront-cli/output/hcl/dashboard.rb', line 62

def handle_sources(sources)
  listmaker(sources, :handle_source)
end

#hcl_fieldsObject

Top-level fields



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

def hcl_fields
  %w[name description url sections parameter_details tags]
end

#khandle_sectionsObject



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

def khandle_sections
  'section'
end

#listmaker(vals, fn) ⇒ String

Returns HCL list of vals.

Parameters:

  • vals (Array)

    an array of objects

  • fn (Symbol)

    a method which knows how to deal with one of the objects in vals

Returns:

  • (String)

    HCL list of vals



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

def listmaker(vals, fn)
  vals.each_with_object([]) { |v, a| a.<< send(fn, v) }.to_hcl_list
end

#qhandle_sections(v) ⇒ Object



78
79
80
# File 'lib/wavefront-cli/output/hcl/dashboard.rb', line 78

def qhandle_sections(v)
  v
end

#quote_value(v) ⇒ Object



82
83
84
85
# File 'lib/wavefront-cli/output/hcl/dashboard.rb', line 82

def quote_value(v)
  v.gsub!(/\$/, '$$') if v.is_a?(String)
  super
end

#vhandle_sections(v) ⇒ Object



34
35
36
37
38
39
# File 'lib/wavefront-cli/output/hcl/dashboard.rb', line 34

def vhandle_sections(v)
  v.each_with_object([]) do |section, a|
    a.<< ("name = \"#{section[:name]}\"\n      row = " +
          handle_rows(section[:rows])).braced(4)
  end.to_hcl_list
end