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
Returns a new instance of Base.
13 14 15 16 |
# File 'lib/wavefront-cli/output/hcl/base.rb', line 13 def initialize(resp, ) @resp = resp @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
11 12 13 |
# File 'lib/wavefront-cli/output/hcl/base.rb', line 11 def @options end |
#resp ⇒ Object (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_output ⇒ Object
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
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_fields ⇒ Array
Fields which the provider requires.
27 28 29 |
# File 'lib/wavefront-cli/output/hcl/base.rb', line 27 def hcl_fields [] end |
#open_output ⇒ Object
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.
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_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
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_name ⇒ Object
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 [:class] end |
#run ⇒ Object
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 |