Sha256: d5c79c817d64a6def870434ee2501fde3efda331f93243b2a0c89f57d175db63

Contents?: true

Size: 1 KB

Versions: 1

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

module Terracop
  # Loads a Terraform plan file and transforms it into a Terracop-friendly list
  # of instances.
  class PlanLoader
    class << self
      def load(file)
        plan = decode(file)

        changed_resources = plan['resource_changes'].reject! do |resource|
          resource['change']['actions'] == ['no-op'] ||
            resource['change']['actions'] == ['delete']
        end

        restruct_resources(changed_resources)
      end

      private

      # :nocov:
      def decode(file)
        JSON.parse(`terraform show -json #{file}`)
      rescue JSON::ParserError
        puts 'Terraform failed to decode the plan file.'
        exit
      end
      # :nocov:

      def restruct_resources(resources)
        resources.map do |resource|
          {
            type: resource['type'],
            name: resource['name'],
            index: resource['index'],
            attributes: resource['change']['after']
          }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
terracop-0.2.0 lib/terracop/plan_loader.rb