class Terracop::StateLoader

Loads a Terraform state file and transforms it into a Terracop-friendly list of instances.

Public Class Methods

load(file) click to toggle source
# File lib/terracop/state_loader.rb, line 8
def load(file)
  state = File.read(file)
  load_from_text(state)
end
load_from_text(text) click to toggle source
# File lib/terracop/state_loader.rb, line 13
def load_from_text(text)
  state = JSON.parse(text)

  managed_resources = state['resources'].select do |resource|
    resource['mode'] == 'managed'
  end

  flatten_instances(managed_resources)
end

Private Class Methods

flatten_instances(resources) click to toggle source
# File lib/terracop/state_loader.rb, line 25
def flatten_instances(resources)
  resources.map do |resource|
    resource['instances'].map do |instance|
      {
        type: resource['type'],
        name: resource['name'],
        index: instance['index_key'],
        attributes: instance['attributes']
      }
    end
  end.flatten
end