# 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 DataprocMetastoreServices < GcpResourceBase
  name 'google_dataproc_metastore_services'
  desc 'Service plural resource'
  supports platform: 'gcp'

  attr_reader :table

  filter_table_config = FilterTable.create

  filter_table_config.add(:hive_metastore_configs, field: :hive_metastore_config)
  filter_table_config.add(:names, field: :name)
  filter_table_config.add(:create_times, field: :create_time)
  filter_table_config.add(:update_times, field: :update_time)
  filter_table_config.add(:labels, field: :labels)
  filter_table_config.add(:networks, field: :network)
  filter_table_config.add(:endpoint_uris, field: :endpoint_uri)
  filter_table_config.add(:ports, field: :port)
  filter_table_config.add(:states, field: :state)
  filter_table_config.add(:state_messages, field: :state_message)
  filter_table_config.add(:artifact_gcs_uris, field: :artifact_gcs_uri)
  filter_table_config.add(:tiers, field: :tier)
  filter_table_config.add(:metadata_integrations, field: :metadata_integration)
  filter_table_config.add(:maintenance_windows, field: :maintenance_window)
  filter_table_config.add(:uids, field: :uid)
  filter_table_config.add(:metadata_management_activities, field: :metadata_management_activity)
  filter_table_config.add(:release_channels, field: :release_channel)
  filter_table_config.add(:encryption_configs, field: :encryption_config)
  filter_table_config.add(:network_configs, field: :network_config)
  filter_table_config.add(:database_types, field: :database_type)
  filter_table_config.add(:telemetry_configs, field: :telemetry_config)
  filter_table_config.add(:scaling_configs, field: :scaling_config)
  filter_table_config.add(:scheduled_backups, field: :scheduled_backup)
  filter_table_config.add(:deletion_protections, field: :deletion_protection)

  filter_table_config.connect(self, :table)

  def initialize(params = {})
    super(params.merge({ use_http_transport: true }))
    @params = params
    @table = fetch_wrapped_resource('services')
  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
    {
      'hiveMetastoreConfig' => ->(obj) { [:hive_metastore_config, GoogleInSpec::DataprocMetastore::Property::ServiceHiveMetastoreConfig.new(obj['hiveMetastoreConfig'], to_s)] },
      'name' => ->(obj) { [:name, obj['name']] },
      'createTime' => ->(obj) { [:create_time, obj['createTime']] },
      'updateTime' => ->(obj) { [:update_time, obj['updateTime']] },
      'labels' => ->(obj) { [:labels, GoogleInSpec::DataprocMetastore::Property::ServiceLabels.new(obj['labels'], to_s)] },
      'network' => ->(obj) { [:network, obj['network']] },
      'endpointUri' => ->(obj) { [:endpoint_uri, obj['endpointUri']] },
      'port' => ->(obj) { [:port, obj['port']] },
      'state' => ->(obj) { [:state, obj['state']] },
      'stateMessage' => ->(obj) { [:state_message, obj['stateMessage']] },
      'artifactGcsUri' => ->(obj) { [:artifact_gcs_uri, obj['artifactGcsUri']] },
      'tier' => ->(obj) { [:tier, obj['tier']] },
      'metadataIntegration' => ->(obj) { [:metadata_integration, GoogleInSpec::DataprocMetastore::Property::ServiceMetadataIntegration.new(obj['metadataIntegration'], to_s)] },
      'maintenanceWindow' => ->(obj) { [:maintenance_window, GoogleInSpec::DataprocMetastore::Property::ServiceMaintenanceWindow.new(obj['maintenanceWindow'], to_s)] },
      'uid' => ->(obj) { [:uid, obj['uid']] },
      'metadataManagementActivity' => ->(obj) { [:metadata_management_activity, GoogleInSpec::DataprocMetastore::Property::ServiceMetadataManagementActivity.new(obj['metadataManagementActivity'], to_s)] },
      'releaseChannel' => ->(obj) { [:release_channel, obj['releaseChannel']] },
      'encryptionConfig' => ->(obj) { [:encryption_config, GoogleInSpec::DataprocMetastore::Property::ServiceEncryptionConfig.new(obj['encryptionConfig'], to_s)] },
      'networkConfig' => ->(obj) { [:network_config, GoogleInSpec::DataprocMetastore::Property::ServiceNetworkConfig.new(obj['networkConfig'], to_s)] },
      'databaseType' => ->(obj) { [:database_type, obj['databaseType']] },
      'telemetryConfig' => ->(obj) { [:telemetry_config, GoogleInSpec::DataprocMetastore::Property::ServiceTelemetryConfig.new(obj['telemetryConfig'], to_s)] },
      'scalingConfig' => ->(obj) { [:scaling_config, GoogleInSpec::DataprocMetastore::Property::ServiceScalingConfig.new(obj['scalingConfig'], to_s)] },
      'scheduledBackup' => ->(obj) { [:scheduled_backup, GoogleInSpec::DataprocMetastore::Property::ServiceScheduledBackup.new(obj['scheduledBackup'], to_s)] },
      'deletionProtection' => ->(obj) { [:deletion_protection, obj['deletionProtection']] },
    }
  end

  private

  def product_url(_ = nil)
    'https://metastore.googleapis.com/v1beta/'
  end

  def resource_base_url
    '{{parent}}/services'
  end
end
