require_relative "provider"
require_relative "aws/aws_connector"
require_relative "aws/aws_provider_account"
require_relative "aws/server_category_provider_aws"
require_relative "aws/stack_category_provider_aws"

module Provider
  class Aws < AbstractProvider

    PROVIDER = "aws"

    def name
      PROVIDER
    end

    def create_connector config, account
      options = {
        #aws_ssh_key: account.ssh_key,
        aws_access_key_id:  account.access_key_id,
        aws_secret_access_key: account.secret_access_key,
        aws_use_iam_profile: account.use_iam_profile,
        aws_availability_zone: account.availability_zone,
        storage_bucket_name: account.storage_bucket_name,

        aws_proxy: config[:aws_proxy],
        aws_no_proxy: config[:aws_no_proxy],
        aws_integration_run_list: config[:aws_integration_run_list]
      }
      add_connector(account.account_name, AwsConnector.new(options))
    end

    def create_account hash
      Devops::Model::AwsProviderAccount.new(hash)
    end

    def create_category_provider hash
      category = case hash["type"]
                 when Devops::Model::CategoryProvider::SERVER_TYPE
                   Devops::Model::ServerCategoryProviderAws.new(hash)
                 when Devops::Model::CategoryProvider::STACK_TYPE
                   Devops::Model::StackCategoryProviderAws.new(hash)
                 else
                   raise "Invalid category type: '#{hash['type']}'"
                 end
      if DevopsLogger.logger.debug?
        DevopsLogger.logger.debug("Created aws category provider: #{category.to_hash.inspect}")
      end
      category
    end

    def account name
      Devops::Model::AwsProviderAccount.find(name)
    end

    def accounts
      Devops::Model::AwsProviderAccount.all
    end
  end

  register Aws

end
