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



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

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

  lines = chart.each_with_object([]) do |(k, v), a|
    next unless fields.include?(k)

    a << format('%<key>s = %<value>s', key: k, value: quote_value(v))
  end

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

#handle_charts(charts) ⇒ Object



51
52
53
# File 'lib/wavefront-cli/output/hcl/dashboard.rb', line 51

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

#handle_rows(rows) ⇒ Object



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

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

#handle_source(source) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/wavefront-cli/output/hcl/dashboard.rb', line 72

def handle_source(source)
  source.each_with_object([]) do |(k, v), a|
    next unless source_fields.include?(k)

    k = 'queryBuilderEnabled' if k == 'querybuilderEnabled'

    a << format('%<key>s = %<value>s',
                key: k.to_snake,
                value: quote_value(v))
  end.to_hcl_obj(14)
end

#handle_sources(sources) ⇒ Object



68
69
70
# File 'lib/wavefront-cli/output/hcl/dashboard.rb', line 68

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

#hcl_fieldsObject

Top-level fields



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

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

#khandle_sectionsObject



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

def khandle_sections
  'section'
end

#listmaker(vals, method) ⇒ String

Returns HCL list of vals.

Parameters:

  • vals (Array)

    an array of objects

  • method (Symbol)

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

Returns:

  • (String)

    HCL list of vals



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

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

#qhandle_sections(val) ⇒ Object



89
90
91
# File 'lib/wavefront-cli/output/hcl/dashboard.rb', line 89

def qhandle_sections(val)
  val
end

#quote_value(val) ⇒ Object



93
94
95
96
# File 'lib/wavefront-cli/output/hcl/dashboard.rb', line 93

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

#source_fieldsObject



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

def source_fields
  %w[name query disabled scatterPlotSource querybuilderEnabled
     sourceDescription]
end

#vhandle_sections(vals) ⇒ Object



38
39
40
41
42
43
# File 'lib/wavefront-cli/output/hcl/dashboard.rb', line 38

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