# 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 ComputeRegionHealthChecks < GcpResourceBase
  name 'google_compute_region_health_checks'
  desc 'RegionHealthCheck plural resource'
  supports platform: 'gcp'

  attr_reader :table

  filter_table_config = FilterTable.create

  filter_table_config.add(:check_interval_secs, field: :check_interval_sec)
  filter_table_config.add(:creation_timestamps, field: :creation_timestamp)
  filter_table_config.add(:descriptions, field: :description)
  filter_table_config.add(:healthy_thresholds, field: :healthy_threshold)
  filter_table_config.add(:ids, field: :id)
  filter_table_config.add(:names, field: :name)
  filter_table_config.add(:unhealthy_thresholds, field: :unhealthy_threshold)
  filter_table_config.add(:timeout_secs, field: :timeout_sec)
  filter_table_config.add(:types, field: :type)
  filter_table_config.add(:http_health_checks, field: :http_health_check)
  filter_table_config.add(:https_health_checks, field: :https_health_check)
  filter_table_config.add(:tcp_health_checks, field: :tcp_health_check)
  filter_table_config.add(:ssl_health_checks, field: :ssl_health_check)
  filter_table_config.add(:http2_health_checks, field: :http2_health_check)
  filter_table_config.add(:grpc_health_checks, field: :grpc_health_check)
  filter_table_config.add(:log_configs, field: :log_config)
  filter_table_config.add(:regions, field: :region)

  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
    {
      'checkIntervalSec' => ->(obj) { [:check_interval_sec, obj['checkIntervalSec']] },
      'creationTimestamp' => ->(obj) { [:creation_timestamp, parse_time_string(obj['creationTimestamp'])] },
      'description' => ->(obj) { [:description, obj['description']] },
      'healthyThreshold' => ->(obj) { [:healthy_threshold, obj['healthyThreshold']] },
      'id' => ->(obj) { [:id, obj['id']] },
      'name' => ->(obj) { [:name, obj['name']] },
      'unhealthyThreshold' => ->(obj) { [:unhealthy_threshold, obj['unhealthyThreshold']] },
      'timeoutSec' => ->(obj) { [:timeout_sec, obj['timeoutSec']] },
      'type' => ->(obj) { [:type, obj['type']] },
      'httpHealthCheck' => ->(obj) { [:http_health_check, GoogleInSpec::Compute::Property::RegionHealthCheckHttpHealthCheck.new(obj['httpHealthCheck'], to_s)] },
      'httpsHealthCheck' => ->(obj) { [:https_health_check, GoogleInSpec::Compute::Property::RegionHealthCheckHttpsHealthCheck.new(obj['httpsHealthCheck'], to_s)] },
      'tcpHealthCheck' => ->(obj) { [:tcp_health_check, GoogleInSpec::Compute::Property::RegionHealthCheckTcpHealthCheck.new(obj['tcpHealthCheck'], to_s)] },
      'sslHealthCheck' => ->(obj) { [:ssl_health_check, GoogleInSpec::Compute::Property::RegionHealthCheckSslHealthCheck.new(obj['sslHealthCheck'], to_s)] },
      'http2HealthCheck' => ->(obj) { [:http2_health_check, GoogleInSpec::Compute::Property::RegionHealthCheckHttp2HealthCheck.new(obj['http2HealthCheck'], to_s)] },
      'grpcHealthCheck' => ->(obj) { [:grpc_health_check, GoogleInSpec::Compute::Property::RegionHealthCheckGrpcHealthCheck.new(obj['grpcHealthCheck'], to_s)] },
      'logConfig' => ->(obj) { [:log_config, GoogleInSpec::Compute::Property::RegionHealthCheckLogConfig.new(obj['logConfig'], to_s)] },
      'region' => ->(obj) { [:region, obj['region']] },
    }
  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://compute.googleapis.com/compute/v1/'
  end

  def resource_base_url
    'projects/{{project}}/regions/{{region}}/healthChecks'
  end
end
