# 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 DataflowProjectLocationJobs < GcpResourceBase
  name 'google_dataflow_project_location_jobs'
  desc 'ProjectLocationJob plural resource'
  supports platform: 'gcp'

  attr_reader :table

  filter_table_config = FilterTable.create

  filter_table_config.add(:ids, field: :id)
  filter_table_config.add(:project_ids, field: :project_id)
  filter_table_config.add(:names, field: :name)
  filter_table_config.add(:types, field: :type)
  filter_table_config.add(:environments, field: :environment)
  filter_table_config.add(:steps, field: :steps)
  filter_table_config.add(:steps_locations, field: :steps_location)
  filter_table_config.add(:current_states, field: :current_state)
  filter_table_config.add(:current_state_times, field: :current_state_time)
  filter_table_config.add(:requested_states, field: :requested_state)
  filter_table_config.add(:execution_infos, field: :execution_info)
  filter_table_config.add(:create_times, field: :create_time)
  filter_table_config.add(:replace_job_ids, field: :replace_job_id)
  filter_table_config.add(:transform_name_mappings, field: :transform_name_mapping)
  filter_table_config.add(:client_request_ids, field: :client_request_id)
  filter_table_config.add(:replaced_by_job_ids, field: :replaced_by_job_id)
  filter_table_config.add(:temp_files, field: :temp_files)
  filter_table_config.add(:labels, field: :labels)
  filter_table_config.add(:locations, field: :location)
  filter_table_config.add(:pipeline_descriptions, field: :pipeline_description)
  filter_table_config.add(:stage_states, field: :stage_states)
  filter_table_config.add(:job_metadata, field: :job_metadata)
  filter_table_config.add(:start_times, field: :start_time)
  filter_table_config.add(:created_from_snapshot_ids, field: :created_from_snapshot_id)
  filter_table_config.add(:satisfies_pzs, field: :satisfies_pzs)
  filter_table_config.add(:runtime_updatable_params, field: :runtime_updatable_params)
  filter_table_config.add(:satisfies_pzis, field: :satisfies_pzi)

  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
    {
      'id' => ->(obj) { [:id, obj['id']] },
      'projectId' => ->(obj) { [:project_id, obj['projectId']] },
      'name' => ->(obj) { [:name, obj['name']] },
      'type' => ->(obj) { [:type, obj['type']] },
      'environment' => ->(obj) { [:environment, GoogleInSpec::Dataflow::Property::ProjectLocationJobEnvironment.new(obj['environment'], to_s)] },
      'steps' => ->(obj) { [:steps, GoogleInSpec::Dataflow::Property::ProjectLocationJobStepsArray.parse(obj['steps'], to_s)] },
      'stepsLocation' => ->(obj) { [:steps_location, obj['stepsLocation']] },
      'currentState' => ->(obj) { [:current_state, obj['currentState']] },
      'currentStateTime' => ->(obj) { [:current_state_time, obj['currentStateTime']] },
      'requestedState' => ->(obj) { [:requested_state, obj['requestedState']] },
      'executionInfo' => ->(obj) { [:execution_info, GoogleInSpec::Dataflow::Property::ProjectLocationJobExecutionInfo.new(obj['executionInfo'], to_s)] },
      'createTime' => ->(obj) { [:create_time, obj['createTime']] },
      'replaceJobId' => ->(obj) { [:replace_job_id, obj['replaceJobId']] },
      'transformNameMapping' => ->(obj) { [:transform_name_mapping, GoogleInSpec::Dataflow::Property::ProjectLocationJobTransformNameMapping.new(obj['transformNameMapping'], to_s)] },
      'clientRequestId' => ->(obj) { [:client_request_id, obj['clientRequestId']] },
      'replacedByJobId' => ->(obj) { [:replaced_by_job_id, obj['replacedByJobId']] },
      'tempFiles' => ->(obj) { [:temp_files, obj['tempFiles']] },
      'labels' => ->(obj) { [:labels, GoogleInSpec::Dataflow::Property::ProjectLocationJobLabels.new(obj['labels'], to_s)] },
      'location' => ->(obj) { [:location, obj['location']] },
      'pipelineDescription' => ->(obj) { [:pipeline_description, GoogleInSpec::Dataflow::Property::ProjectLocationJobPipelineDescription.new(obj['pipelineDescription'], to_s)] },
      'stageStates' => ->(obj) { [:stage_states, GoogleInSpec::Dataflow::Property::ProjectLocationJobStageStatesArray.parse(obj['stageStates'], to_s)] },
      'jobMetadata' => ->(obj) { [:job_metadata, GoogleInSpec::Dataflow::Property::ProjectLocationJobJobMetadata.new(obj['jobMetadata'], to_s)] },
      'startTime' => ->(obj) { [:start_time, obj['startTime']] },
      'createdFromSnapshotId' => ->(obj) { [:created_from_snapshot_id, obj['createdFromSnapshotId']] },
      'satisfiesPzs' => ->(obj) { [:satisfies_pzs, obj['satisfiesPzs']] },
      'runtimeUpdatableParams' => ->(obj) { [:runtime_updatable_params, GoogleInSpec::Dataflow::Property::ProjectLocationJobRuntimeUpdatableParams.new(obj['runtimeUpdatableParams'], to_s)] },
      'satisfiesPzi' => ->(obj) { [:satisfies_pzi, obj['satisfiesPzi']] },
    }
  end

  private

  def product_url(_ = nil)
    'https://dataflow.googleapis.com/v1b3/'
  end

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