# frozen_string_literal: false

# ----------------------------------------------------------------------------
#
#     ***     AUTO GENERATED CODE    ***    AUTO GENERATED CODE     ***
#
# ----------------------------------------------------------------------------
#
#     This file is automatically generated by Magic Modules and manual
#     changes will be clobbered when the file is regenerated.
#
#     Please read more about how to change this file in README.md and
#     CONTRIBUTING.md located at the root of this package.
#
# ----------------------------------------------------------------------------
require 'gcp_backend'
class IAMServiceAccountKeys < GcpResourceBase
  name 'google_service_account_keys'
  desc 'ServiceAccountKey plural resource'
  supports platform: 'gcp'

  attr_reader :table

  filter_table_config = FilterTable.create

  filter_table_config.add(:key_names, field: :key_name)
  filter_table_config.add(:private_key_types, field: :private_key_type)
  filter_table_config.add(:key_algorithms, field: :key_algorithm)
  filter_table_config.add(:private_key_data, field: :private_key_data)
  filter_table_config.add(:public_key_data, field: :public_key_data)
  filter_table_config.add(:valid_after_times, field: :valid_after_time)
  filter_table_config.add(:valid_before_times, field: :valid_before_time)
  filter_table_config.add(:key_types, field: :key_type)
  filter_table_config.add(:service_accounts, field: :service_account)
  filter_table_config.add(:paths, field: :path)
  filter_table_config.add(:key_origins, field: :key_origin)
  filter_table_config.add(:disableds, field: :disabled)

  filter_table_config.connect(self, :table)

  def initialize(params = {})
    super(params.merge({ use_http_transport: true }))
    @params = params
    @table = fetch_wrapped_resource('keys')
  end

  def fetch_wrapped_resource(wrap_path)
    # fetch_resource returns an array of responses (to handle pagination)
    result = @connection.fetch_all(product_url, resource_base_url, @params, 'Get')
    return if result.nil?

    # Conversion of string -> object hash to symbol -> object hash that InSpec needs
    converted = []
    result.each do |response|
      next if response.nil? || !response.key?(wrap_path)
      response[wrap_path].each do |hash|
        hash_with_symbols = {}
        hash.each_key do |key|
          name, value = transform(key, hash)
          hash_with_symbols[name] = value
        end
        converted.push(hash_with_symbols)
      end
    end

    converted
  end

  def transform(key, value)
    return transformers[key].call(value) if transformers.key?(key)

    [key.to_sym, value]
  end

  def transformers
    {
      'name' => ->(obj) { [:key_name, obj['name']] },
      'privateKeyType' => ->(obj) { [:private_key_type, obj['privateKeyType']] },
      'keyAlgorithm' => ->(obj) { [:key_algorithm, obj['keyAlgorithm']] },
      'privateKeyData' => ->(obj) { [:private_key_data, obj['privateKeyData']] },
      'publicKeyData' => ->(obj) { [:public_key_data, obj['publicKeyData']] },
      'validAfterTime' => ->(obj) { [:valid_after_time, parse_time_string(obj['validAfterTime'])] },
      'validBeforeTime' => ->(obj) { [:valid_before_time, parse_time_string(obj['validBeforeTime'])] },
      'keyType' => ->(obj) { [:key_type, obj['keyType']] },
      'serviceAccount' => ->(obj) { [:service_account, obj['serviceAccount']] },
      'path' => ->(obj) { [:path, obj['path']] },
      'keyOrigin' => ->(obj) { [:key_origin, obj['keyOrigin']] },
      'disabled' => ->(obj) { [:disabled, obj['disabled']] },
    }
  end

  # Handles parsing RFC3339 time string
  def parse_time_string(time_string)
    time_string ? Time.parse(time_string) : nil
  end

  private

  def product_url(_ = nil)
    'https://iam.googleapis.com/v1/'
  end

  def resource_base_url
    'projects/{{project}}/serviceAccounts/{{service_account}}/keys'
  end
end
