# 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 RunJobs < GcpResourceBase
  name 'google_run_jobs'
  desc 'Job plural resource'
  supports platform: 'gcp'

  attr_reader :table

  filter_table_config = FilterTable.create

  filter_table_config.add(:names, field: :name)
  filter_table_config.add(:uids, field: :uid)
  filter_table_config.add(:generations, field: :generation)
  filter_table_config.add(:labels, field: :labels)
  filter_table_config.add(:annotations, field: :annotations)
  filter_table_config.add(:create_times, field: :create_time)
  filter_table_config.add(:update_times, field: :update_time)
  filter_table_config.add(:delete_times, field: :delete_time)
  filter_table_config.add(:expire_times, field: :expire_time)
  filter_table_config.add(:creators, field: :creator)
  filter_table_config.add(:last_modifiers, field: :last_modifier)
  filter_table_config.add(:clients, field: :client)
  filter_table_config.add(:client_versions, field: :client_version)
  filter_table_config.add(:launch_stages, field: :launch_stage)
  filter_table_config.add(:binary_authorizations, field: :binary_authorization)
  filter_table_config.add(:templates, field: :template)
  filter_table_config.add(:observed_generations, field: :observed_generation)
  filter_table_config.add(:terminal_conditions, field: :terminal_condition)
  filter_table_config.add(:conditions, field: :conditions)
  filter_table_config.add(:execution_counts, field: :execution_count)
  filter_table_config.add(:latest_created_executions, field: :latest_created_execution)
  filter_table_config.add(:reconcilings, field: :reconciling)
  filter_table_config.add(:satisfies_pzs, field: :satisfies_pzs)
  filter_table_config.add(:start_execution_tokens, field: :start_execution_token)
  filter_table_config.add(:run_execution_tokens, field: :run_execution_token)
  filter_table_config.add(:etags, field: :etag)

  filter_table_config.connect(self, :table)

  def initialize(params = {})
    super(params.merge({ use_http_transport: true }))
    @params = params
    @table = fetch_wrapped_resource('jobs')
  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']] },
      'uid' => ->(obj) { [:uid, obj['uid']] },
      'generation' => ->(obj) { [:generation, obj['generation']] },
      'labels' => ->(obj) { [:labels, GoogleInSpec::Run::Property::JobLabels.new(obj['labels'], to_s)] },
      'annotations' => ->(obj) { [:annotations, GoogleInSpec::Run::Property::JobAnnotations.new(obj['annotations'], to_s)] },
      'createTime' => ->(obj) { [:create_time, obj['createTime']] },
      'updateTime' => ->(obj) { [:update_time, obj['updateTime']] },
      'deleteTime' => ->(obj) { [:delete_time, obj['deleteTime']] },
      'expireTime' => ->(obj) { [:expire_time, obj['expireTime']] },
      'creator' => ->(obj) { [:creator, obj['creator']] },
      'lastModifier' => ->(obj) { [:last_modifier, obj['lastModifier']] },
      'client' => ->(obj) { [:client, obj['client']] },
      'clientVersion' => ->(obj) { [:client_version, obj['clientVersion']] },
      'launchStage' => ->(obj) { [:launch_stage, obj['launchStage']] },
      'binaryAuthorization' => ->(obj) { [:binary_authorization, GoogleInSpec::Run::Property::JobBinaryAuthorization.new(obj['binaryAuthorization'], to_s)] },
      'template' => ->(obj) { [:template, GoogleInSpec::Run::Property::JobTemplate.new(obj['template'], to_s)] },
      'observedGeneration' => ->(obj) { [:observed_generation, obj['observedGeneration']] },
      'terminalCondition' => ->(obj) { [:terminal_condition, GoogleInSpec::Run::Property::JobTerminalCondition.new(obj['terminalCondition'], to_s)] },
      'conditions' => ->(obj) { [:conditions, GoogleInSpec::Run::Property::JobConditionsArray.parse(obj['conditions'], to_s)] },
      'executionCount' => ->(obj) { [:execution_count, obj['executionCount']] },
      'latestCreatedExecution' => ->(obj) { [:latest_created_execution, GoogleInSpec::Run::Property::JobLatestCreatedExecution.new(obj['latestCreatedExecution'], to_s)] },
      'reconciling' => ->(obj) { [:reconciling, obj['reconciling']] },
      'satisfiesPzs' => ->(obj) { [:satisfies_pzs, obj['satisfiesPzs']] },
      'startExecutionToken' => ->(obj) { [:start_execution_token, obj['startExecutionToken']] },
      'runExecutionToken' => ->(obj) { [:run_execution_token, obj['runExecutionToken']] },
      'etag' => ->(obj) { [:etag, obj['etag']] },
    }
  end

  private

  def product_url(_ = nil)
    'https://run.googleapis.com/v2/'
  end

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