require "providers/provider_account"

module Devops
  module Model
    class AwsProviderAccount < ProviderAccount

      field :access_key_id, type: String
      field :secret_access_key, type: String
      field :use_iam_profile, type: Boolean
      field :storage_bucket_name, type: String


      default_scope ->{ where(provider: ::Provider::AwsConnector::PROVIDER) }

      validates_presence_of :access_key_id, message: "'access_key_id' is undefined"
      validates_length_of :access_key_id, minimum: 16, maximum: 32
      validates_format_of :access_key_id, with: /\A[\w]+\z/, message: "invalid access_key_id"

      validates_presence_of :secret_access_key, message: "'secret_access_key' is undefined"
      validates_length_of :secret_access_key, maximum: 100

      validates_presence_of :use_iam_profile, message: "'use_iam_profile' is undefined"

      validates_presence_of :storage_bucket_name

      attr_accessor :availability_zone

      def to_hash
        hash = super.to_hash
        unless self.use_iam_profile
          hash["access_key_id"] = "***"
          hash["secret_access_key"] = "***"
        end
        hash
      end

    end
  end
end

