module Dalmatian
  class Infrastructure
    def initialize(configuration = ConfigurationReader.new.infrastructures)
      @clusters = configuration.map { |description| Cluster.new(description) }
    end

    BOOTSTRAP_PATH = "terraform/account_bootstrap".freeze
    PATH = "terraform/infrastructures".freeze
    APP_ROOT = File.expand_path(File.dirname("."))

    def fetch(infrastructure_name: "all")
      clusters.each do |cluster|
        cluster.fetch(infrastructure_name: infrastructure_name)
      end
    end

    def test(environment_name: "all", infrastructure_name: "all", service_name: "all", skip_deployments: "")
      deploy(environment_name: environment_name, infrastructure_name: infrastructure_name, service_name: service_name, skip_deployments: skip_deployments, test: true, auto_approve: false, plan: true, destroy: false)
    end

    def deploy(environment_name: "all", infrastructure_name: "all", service_name: "all", skip_deployments: "", test: false, auto_approve: false, plan: false, destroy: false, verbose: false)
      clusters.each do |cluster|
        next if cluster.id != infrastructure_name && infrastructure_name != "all"
        cluster.deploy(environment_name: environment_name, service_name: service_name, skip_deployments: skip_deployments, test: test, auto_approve: auto_approve, plan: plan, destroy: destroy, verbose: verbose)
      end
    end

    def remove(environment_name: "all", infrastructure_name: "all", service_name: "all", skip_deployments: "")
      deploy(environment_name: environment_name, infrastructure_name: infrastructure_name, service_name: service_name, skip_deployments: skip_deployments, test: false, auto_approve: false, plan: false, destroy: true)
    end

    attr_reader :clusters
  end
end
