# 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 ComputeReservations < GcpResourceBase
  name 'google_compute_reservations'
  desc 'Reservation plural resource'
  supports platform: 'gcp'

  attr_reader :table

  filter_table_config = FilterTable.create

  filter_table_config.add(:kinds, field: :kind)
  filter_table_config.add(:ids, field: :id)
  filter_table_config.add(:creation_timestamps, field: :creation_timestamp)
  filter_table_config.add(:self_links, field: :self_link)
  filter_table_config.add(:self_link_with_ids, field: :self_link_with_id)
  filter_table_config.add(:zones, field: :zone)
  filter_table_config.add(:descriptions, field: :description)
  filter_table_config.add(:names, field: :name)
  filter_table_config.add(:specific_reservations, field: :specific_reservation)
  filter_table_config.add(:aggregate_reservations, field: :aggregate_reservation)
  filter_table_config.add(:commitments, field: :commitment)
  filter_table_config.add(:specific_reservation_requireds, field: :specific_reservation_required)
  filter_table_config.add(:statuses, field: :status)
  filter_table_config.add(:share_settings, field: :share_settings)
  filter_table_config.add(:satisfies_pzs, field: :satisfies_pzs)
  filter_table_config.add(:resource_policies, field: :resource_policies)
  filter_table_config.add(:resource_statuses, field: :resource_status)
  filter_table_config.add(:delete_at_times, field: :delete_at_time)
  filter_table_config.add(:delete_after_durations, field: :delete_after_duration)

  filter_table_config.connect(self, :table)

  def initialize(params = {})
    super(params.merge({ use_http_transport: true }))
    @params = params
    @table = fetch_wrapped_resource('items')
  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
    {
      'kind' => ->(obj) { [:kind, obj['kind']] },
      'id' => ->(obj) { [:id, obj['id']] },
      'creationTimestamp' => ->(obj) { [:creation_timestamp, obj['creationTimestamp']] },
      'selfLink' => ->(obj) { [:self_link, obj['selfLink']] },
      'selfLinkWithId' => ->(obj) { [:self_link_with_id, obj['selfLinkWithId']] },
      'zone' => ->(obj) { [:zone, obj['zone']] },
      'description' => ->(obj) { [:description, obj['description']] },
      'name' => ->(obj) { [:name, obj['name']] },
      'specificReservation' => ->(obj) { [:specific_reservation, GoogleInSpec::Compute::Property::ReservationSpecificReservation.new(obj['specificReservation'], to_s)] },
      'aggregateReservation' => ->(obj) { [:aggregate_reservation, GoogleInSpec::Compute::Property::ReservationAggregateReservation.new(obj['aggregateReservation'], to_s)] },
      'commitment' => ->(obj) { [:commitment, obj['commitment']] },
      'specificReservationRequired' => ->(obj) { [:specific_reservation_required, obj['specificReservationRequired']] },
      'status' => ->(obj) { [:status, obj['status']] },
      'shareSettings' => ->(obj) { [:share_settings, GoogleInSpec::Compute::Property::ReservationShareSettings.new(obj['shareSettings'], to_s)] },
      'satisfiesPzs' => ->(obj) { [:satisfies_pzs, obj['satisfiesPzs']] },
      'resourcePolicies' => ->(obj) { [:resource_policies, GoogleInSpec::Compute::Property::ReservationResourcePolicies.new(obj['resourcePolicies'], to_s)] },
      'resourceStatus' => ->(obj) { [:resource_status, GoogleInSpec::Compute::Property::ReservationResourceStatus.new(obj['resourceStatus'], to_s)] },
      'deleteAtTime' => ->(obj) { [:delete_at_time, obj['deleteAtTime']] },
      'deleteAfterDuration' => ->(obj) { [:delete_after_duration, GoogleInSpec::Compute::Property::ReservationDeleteAfterDuration.new(obj['deleteAfterDuration'], to_s)] },
    }
  end

  private

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

  def resource_base_url
    'projects/{{project}}/zones/{{zone}}/reservations'
  end
end
