# 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 ComputeMachineImages < GcpResourceBase
  name 'google_compute_machine_images'
  desc 'MachineImage 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(:names, field: :name)
  filter_table_config.add(:descriptions, field: :description)
  filter_table_config.add(:self_links, field: :self_link)
  filter_table_config.add(:source_instances, field: :source_instance)
  filter_table_config.add(:statuses, field: :status)
  filter_table_config.add(:source_instance_properties, field: :source_instance_properties)
  filter_table_config.add(:instance_properties, field: :instance_properties)
  filter_table_config.add(:saved_disks, field: :saved_disks)
  filter_table_config.add(:storage_locations, field: :storage_locations)
  filter_table_config.add(:machine_image_encryption_keys, field: :machine_image_encryption_key)
  filter_table_config.add(:guest_flushes, field: :guest_flush)
  filter_table_config.add(:source_disk_encryption_keys, field: :source_disk_encryption_keys)
  filter_table_config.add(:total_storage_bytes, field: :total_storage_bytes)
  filter_table_config.add(:satisfies_pzs, field: :satisfies_pzs)

  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']] },
      'name' => ->(obj) { [:name, obj['name']] },
      'description' => ->(obj) { [:description, obj['description']] },
      'selfLink' => ->(obj) { [:self_link, obj['selfLink']] },
      'sourceInstance' => ->(obj) { [:source_instance, obj['sourceInstance']] },
      'status' => ->(obj) { [:status, obj['status']] },
      'sourceInstanceProperties' => ->(obj) { [:source_instance_properties, GoogleInSpec::Compute::Property::MachineImageSourceInstanceProperties.new(obj['sourceInstanceProperties'], to_s)] },
      'instanceProperties' => ->(obj) { [:instance_properties, GoogleInSpec::Compute::Property::MachineImageInstanceProperties.new(obj['instanceProperties'], to_s)] },
      'savedDisks' => ->(obj) { [:saved_disks, GoogleInSpec::Compute::Property::MachineImageSavedDisksArray.parse(obj['savedDisks'], to_s)] },
      'storageLocations' => ->(obj) { [:storage_locations, obj['storageLocations']] },
      'machineImageEncryptionKey' => ->(obj) { [:machine_image_encryption_key, GoogleInSpec::Compute::Property::MachineImageMachineImageEncryptionKey.new(obj['machineImageEncryptionKey'], to_s)] },
      'guestFlush' => ->(obj) { [:guest_flush, obj['guestFlush']] },
      'sourceDiskEncryptionKeys' => ->(obj) { [:source_disk_encryption_keys, GoogleInSpec::Compute::Property::MachineImageSourceDiskEncryptionKeysArray.parse(obj['sourceDiskEncryptionKeys'], to_s)] },
      'totalStorageBytes' => ->(obj) { [:total_storage_bytes, obj['totalStorageBytes']] },
      'satisfiesPzs' => ->(obj) { [:satisfies_pzs, obj['satisfiesPzs']] },
    }
  end

  private

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

  def resource_base_url
    'projects/{{project}}/global/machineImages'
  end
end
