# 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 CloudFunctionsCloudFunctions < GcpResourceBase
  name 'google_cloudfunctions_cloud_functions'
  desc 'CloudFunction plural resource'
  supports platform: 'gcp'

  attr_reader :table

  filter_table_config = FilterTable.create

  filter_table_config.add(:names, field: :name)
  filter_table_config.add(:descriptions, field: :description)
  filter_table_config.add(:statuses, field: :status)
  filter_table_config.add(:entry_points, field: :entry_point)
  filter_table_config.add(:runtimes, field: :runtime)
  filter_table_config.add(:timeouts, field: :timeout)
  filter_table_config.add(:available_memory_mbs, field: :available_memory_mb)
  filter_table_config.add(:service_account_emails, field: :service_account_email)
  filter_table_config.add(:update_times, field: :update_time)
  filter_table_config.add(:version_ids, field: :version_id)
  filter_table_config.add(:labels, field: :labels)
  filter_table_config.add(:environment_variables, field: :environment_variables)
  filter_table_config.add(:source_archive_urls, field: :source_archive_url)
  filter_table_config.add(:source_upload_urls, field: :source_upload_url)
  filter_table_config.add(:source_repositories, field: :source_repository)
  filter_table_config.add(:https_triggers, field: :https_trigger)
  filter_table_config.add(:event_triggers, field: :event_trigger)
  filter_table_config.add(:locations, field: :location)

  filter_table_config.connect(self, :table)

  def initialize(params = {})
    super(params.merge({ use_http_transport: true }))
    @params = params
    @table = fetch_wrapped_resource('functions')
  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']] },
      'description' => ->(obj) { [:description, obj['description']] },
      'status' => ->(obj) { [:status, obj['status']] },
      'entryPoint' => ->(obj) { [:entry_point, obj['entryPoint']] },
      'runtime' => ->(obj) { [:runtime, obj['runtime']] },
      'timeout' => ->(obj) { [:timeout, obj['timeout']] },
      'availableMemoryMb' => ->(obj) { [:available_memory_mb, obj['availableMemoryMb']] },
      'serviceAccountEmail' => ->(obj) { [:service_account_email, obj['serviceAccountEmail']] },
      'updateTime' => ->(obj) { [:update_time, obj['updateTime']] },
      'versionId' => ->(obj) { [:version_id, obj['versionId']] },
      'labels' => ->(obj) { [:labels, obj['labels']] },
      'environmentVariables' => ->(obj) { [:environment_variables, obj['environmentVariables']] },
      'sourceArchiveUrl' => ->(obj) { [:source_archive_url, obj['sourceArchiveUrl']] },
      'sourceUploadUrl' => ->(obj) { [:source_upload_url, obj['sourceUploadUrl']] },
      'sourceRepository' => ->(obj) { [:source_repository, GoogleInSpec::CloudFunctions::Property::CloudFunctionSourceRepository.new(obj['sourceRepository'], to_s)] },
      'httpsTrigger' => ->(obj) { [:https_trigger, GoogleInSpec::CloudFunctions::Property::CloudFunctionHttpsTrigger.new(obj['httpsTrigger'], to_s)] },
      'eventTrigger' => ->(obj) { [:event_trigger, GoogleInSpec::CloudFunctions::Property::CloudFunctionEventTrigger.new(obj['eventTrigger'], to_s)] },
      'location' => ->(obj) { [:location, obj['location']] },
    }
  end

  private

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

  def resource_base_url
    'projects/{{project}}/locations/{{location}}/functions'
  end
end
