Class: WavefrontHclOutput::Base
- Inherits:
-
Object
- Object
- WavefrontHclOutput::Base
- Defined in:
- lib/wavefront-cli/output/hcl/base.rb
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(k, v) ⇒ 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(v) ⇒ 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(v) ⇒ Array
Tags need to be in an array.
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, ) @resp = resp @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/wavefront-cli/output/hcl/base.rb', line 6 def @options end |
#resp ⇒ Object (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_output ⇒ Object
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
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_fields ⇒ Array
Fields which the provider requires.
22 23 24 |
# File 'lib/wavefront-cli/output/hcl/base.rb', line 22 def hcl_fields [] end |
#open_output ⇒ Object
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.
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_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
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_name ⇒ Object
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 [:class] end |
#run ⇒ Object
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 |