Class: Bolt::Plugin::Terraform
- Inherits:
-
Object
- Object
- Bolt::Plugin::Terraform
- Defined in:
- lib/bolt/plugin/terraform.rb
Instance Method Summary collapse
- #hooks ⇒ Object
-
#initialize ⇒ Terraform
constructor
A new instance of Terraform.
- #load_statefile(opts) ⇒ Object
- #lookup_targets(opts) ⇒ Object
- #name ⇒ Object
- #resolve_config(name, resource, config_template) ⇒ Object
- #warn_missing_property(name, property) ⇒ Object
Constructor Details
#initialize ⇒ Terraform
Returns a new instance of Terraform.
8 9 10 |
# File 'lib/bolt/plugin/terraform.rb', line 8 def initialize @logger = Logging.logger[self] end |
Instance Method Details
#hooks ⇒ Object
16 17 18 |
# File 'lib/bolt/plugin/terraform.rb', line 16 def hooks ['lookup_targets'] end |
#load_statefile(opts) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/bolt/plugin/terraform.rb', line 58 def load_statefile(opts) dir = opts['dir'] filename = opts.fetch('statefile', 'terraform.tfstate') statefile = File.(File.join(dir, filename)) JSON.parse(File.read(statefile)) rescue StandardError => e raise Bolt::FileError.new("Could not load Terraform state file #{filename}: #{e}", filename) end |
#lookup_targets(opts) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/bolt/plugin/terraform.rb', line 24 def lookup_targets(opts) state = load_statefile(opts) resources = state.fetch('modules', {}).flat_map do |mod| mod.fetch('resources', {}).map do |name, resource| [name, resource.dig('primary', 'attributes')] end end regex = Regexp.new(opts['resource_type']) resources.select do |name, _resource| name.match?(regex) end.map do |name, resource| unless resource.key?(opts['uri']) warn_missing_property(name, opts['uri']) next end target = { 'uri' => resource[opts['uri']] } if opts.key?('name') if resource.key?(opts['name']) target['name'] = resource[opts['name']] else warn_missing_property(name, opts['name']) end end if opts.key?('config') target['config'] = resolve_config(name, resource, opts['config']) end target end.compact end |
#name ⇒ Object
12 13 14 |
# File 'lib/bolt/plugin/terraform.rb', line 12 def name 'terraform' end |
#resolve_config(name, resource, config_template) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/bolt/plugin/terraform.rb', line 68 def resolve_config(name, resource, config_template) Bolt::Util.walk_vals(config_template) do |value| if value.is_a?(String) if resource.key?(value) resource[value] else warn_missing_property(name, value) nil end else value end end end |
#warn_missing_property(name, property) ⇒ Object
20 21 22 |
# File 'lib/bolt/plugin/terraform.rb', line 20 def warn_missing_property(name, property) @logger.warn("Could not find property #{property} of terraform resource #{name}") end |