Class: WavefrontHclOutput::Base
- Inherits:
-
Object
- Object
- WavefrontHclOutput::Base
- Defined in:
- lib/wavefront-cli/output/hcl/base.rb
Overview
Output stuff for Hashicorp Configuration Language
Direct Known Subclasses
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#resp ⇒ Object
readonly
Returns the value of attribute resp.
Instance Method Summary collapse
- #close_output ⇒ Object
-
#handler(key, val) ⇒ String
Format each key-value pair.
-
#hcl_fields ⇒ Array
Fields which the provider requires.
-
#initialize(resp, options) ⇒ Base
constructor
A new instance of Base.
- #open_output ⇒ Object
-
#quote_value(val) ⇒ String
Some values need to be quoted, some need to be escaped etc etc.
-
#required_fields ⇒ Object
The provider can only handle certain keys.
-
#resource_name ⇒ Object
Override this if the provider calls a resource something other than the name of the inheriting class.
- #run ⇒ Object
-
#vhandle_tags(val) ⇒ Array
Tags need to be in an array.
Constructor Details
#initialize(resp, options) ⇒ Base
11 12 13 14 |
# File 'lib/wavefront-cli/output/hcl/base.rb', line 11 def initialize(resp, ) @resp = resp = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
9 10 11 |
# File 'lib/wavefront-cli/output/hcl/base.rb', line 9 def end |
#resp ⇒ Object (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_output ⇒ Object
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_fields ⇒ Array
Fields which the provider requires.
25 26 27 |
# File 'lib/wavefront-cli/output/hcl/base.rb', line 25 def hcl_fields [] end |
#open_output ⇒ Object
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_fields ⇒ Object
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_name ⇒ Object
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 [:class] end |
#run ⇒ Object
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 (val) val = val.values if val.is_a?(Hash) Array(val).flatten end |