# frozen_string_literal: false

# ----------------------------------------------------------------------------
#
#     ***     AUTO GENERATED CODE    ***    Type: MMv1     ***
#
# ----------------------------------------------------------------------------
#
#     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 KMSKeyRingImportJobs < GcpResourceBase
  name 'google_kms_key_ring_import_jobs'
  desc 'KeyRingImportJob plural resource'
  supports platform: 'gcp'

  attr_reader :table

  filter_table_config = FilterTable.create

  filter_table_config.add(:names, field: :name)
  filter_table_config.add(:import_methods, field: :import_method)
  filter_table_config.add(:protection_levels, field: :protection_level)
  filter_table_config.add(:create_times, field: :create_time)
  filter_table_config.add(:generate_times, field: :generate_time)
  filter_table_config.add(:expire_times, field: :expire_time)
  filter_table_config.add(:expire_event_times, field: :expire_event_time)
  filter_table_config.add(:states, field: :state)
  filter_table_config.add(:public_keys, field: :public_key)
  filter_table_config.add(:attestations, field: :attestation)
  filter_table_config.add(:key_rings, field: :key_ring)
  filter_table_config.add(:import_job_ids, field: :import_job_id)

  filter_table_config.connect(self, :table)

  def initialize(params = {})
    super(params.merge({ use_http_transport: true }))
    @params = params
    @table = fetch_wrapped_resource('keyRingImportJobs')
  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) { [:name, obj['name']] },
      'importMethod' => ->(obj) { [:import_method, obj['importMethod']] },
      'protectionLevel' => ->(obj) { [:protection_level, obj['protectionLevel']] },
      'createTime' => ->(obj) { [:create_time, parse_time_string(obj['createTime'])] },
      'generateTime' => ->(obj) { [:generate_time, parse_time_string(obj['generateTime'])] },
      'expireTime' => ->(obj) { [:expire_time, parse_time_string(obj['expireTime'])] },
      'expireEventTime' => ->(obj) { [:expire_event_time, parse_time_string(obj['expireEventTime'])] },
      'state' => ->(obj) { [:state, obj['state']] },
      'publicKey' => ->(obj) { [:public_key, GoogleInSpec::KMS::Property::KeyRingImportJobPublicKey.new(obj['publicKey'], to_s)] },
      'attestation' => ->(obj) { [:attestation, GoogleInSpec::KMS::Property::KeyRingImportJobAttestation.new(obj['attestation'], to_s)] },
      'keyRing' => ->(obj) { [:key_ring, obj['keyRing']] },
      'importJobId' => ->(obj) { [:import_job_id, obj['importJobId']] },
    }
  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://cloudkms.googleapis.com/v1/'
  end

  def resource_base_url
    '{{key_ring}}/importJobs'
  end
end
