# 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 DNSManagedZones < GcpResourceBase
  name 'google_dns_managed_zones'
  desc 'ManagedZone plural resource'
  supports platform: 'gcp'

  attr_reader :table

  filter_table_config = FilterTable.create

  filter_table_config.add(:descriptions, field: :description)
  filter_table_config.add(:zone_dns_names, field: :zone_dns_name)
  filter_table_config.add(:dnssec_configs, field: :dnssec_config)
  filter_table_config.add(:zone_ids, field: :zone_id)
  filter_table_config.add(:zone_names, field: :zone_name)
  filter_table_config.add(:name_servers, field: :name_servers)
  filter_table_config.add(:name_server_sets, field: :name_server_set)
  filter_table_config.add(:creation_times, field: :creation_time)
  filter_table_config.add(:labels, field: :labels)
  filter_table_config.add(:visibilities, field: :visibility)
  filter_table_config.add(:private_visibility_configs, field: :private_visibility_config)
  filter_table_config.add(:forwarding_configs, field: :forwarding_config)
  filter_table_config.add(:peering_configs, field: :peering_config)
  filter_table_config.add(:reverse_lookups, field: :reverse_lookup)
  filter_table_config.add(:service_directory_configs, field: :service_directory_config)
  filter_table_config.add(:dnssec_config_states, field: :dnssec_config_state)

  filter_table_config.connect(self, :table)

  def initialize(params = {})
    super(params.merge({ use_http_transport: true }))
    @params = params
    @table = fetch_wrapped_resource('managedZones')
  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
        hash_with_symbols[:dnssec_config_state] = hash.dig('dnssecConfig', 'state')&.downcase == 'on'
        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
    {
      'description' => ->(obj) { [:description, obj['description']] },
      'dnsName' => ->(obj) { [:zone_dns_name, obj['dnsName']] },
      'dnssecConfig' => ->(obj) { [:dnssec_config, GoogleInSpec::DNS::Property::ManagedZoneDnssecConfig.new(obj['dnssecConfig'], to_s)] },
      'id' => ->(obj) { [:zone_id, obj['id']] },
      'name' => ->(obj) { [:zone_name, obj['name']] },
      'nameServers' => ->(obj) { [:name_servers, obj['nameServers']] },
      'nameServerSet' => ->(obj) { [:name_server_set, obj['nameServerSet']] },
      'creationTime' => ->(obj) { [:creation_time, parse_time_string(obj['creationTime'])] },
      'labels' => ->(obj) { [:labels, obj['labels']] },
      'visibility' => ->(obj) { [:visibility, obj['visibility']] },
      'privateVisibilityConfig' => ->(obj) { [:private_visibility_config, GoogleInSpec::DNS::Property::ManagedZonePrivateVisibilityConfig.new(obj['privateVisibilityConfig'], to_s)] },
      'forwardingConfig' => ->(obj) { [:forwarding_config, GoogleInSpec::DNS::Property::ManagedZoneForwardingConfig.new(obj['forwardingConfig'], to_s)] },
      'peeringConfig' => ->(obj) { [:peering_config, GoogleInSpec::DNS::Property::ManagedZonePeeringConfig.new(obj['peeringConfig'], to_s)] },
      'reverseLookupConfig' => ->(obj) { [:reverse_lookup, obj['reverseLookupConfig']] },
      'serviceDirectoryConfig' => ->(obj) { [:service_directory_config, GoogleInSpec::DNS::Property::ManagedZoneServiceDirectoryConfig.new(obj['serviceDirectoryConfig'], to_s)] },
    }
  end

  # Handles parsing RFC3339 time string
  def parse_time_string(time_string)
    time_string ? Time.parse(time_string) : nil
  end

  private

  def product_url(beta = false)
    if beta
      'https://dns.googleapis.com/dns/v1beta2/'
    else
      'https://dns.googleapis.com/dns/v1/'
    end
  end

  def resource_base_url
    'projects/{{project}}/managedZones'
  end
end
