# 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'
require 'google/dataproc/property/batch_environment_config'
require 'google/dataproc/property/batch_environment_config_execution_config'
require 'google/dataproc/property/batch_environment_config_peripherals_config'
require 'google/dataproc/property/batch_environment_config_peripherals_config_spark_history_server_config'
require 'google/dataproc/property/batch_labels'
require 'google/dataproc/property/batch_pyspark_batch'
require 'google/dataproc/property/batch_runtime_config'
require 'google/dataproc/property/batch_runtime_config_properties'
require 'google/dataproc/property/batch_runtime_config_repository_config'
require 'google/dataproc/property/batch_runtime_config_repository_config_pypi_repository_config'
require 'google/dataproc/property/batch_runtime_info'
require 'google/dataproc/property/batch_runtime_info_approximate_usage'
require 'google/dataproc/property/batch_runtime_info_current_usage'
require 'google/dataproc/property/batch_runtime_info_endpoints'
require 'google/dataproc/property/batch_spark_batch'
require 'google/dataproc/property/batch_spark_r_batch'
require 'google/dataproc/property/batch_spark_sql_batch'
require 'google/dataproc/property/batch_spark_sql_batch_query_variables'
require 'google/dataproc/property/batch_state_history'

# A provider to manage Dataproc resources.
class DataprocBatch < GcpResourceBase
  name 'google_dataproc_batch'
  desc 'Batch'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :name
  attr_reader :uuid
  attr_reader :create_time
  attr_reader :pyspark_batch
  attr_reader :spark_batch
  attr_reader :spark_r_batch
  attr_reader :spark_sql_batch
  attr_reader :runtime_info
  attr_reader :state
  attr_reader :state_message
  attr_reader :state_time
  attr_reader :creator
  attr_reader :labels
  attr_reader :runtime_config
  attr_reader :environment_config
  attr_reader :operation
  attr_reader :state_history

  def initialize(params)
    super(params.merge({ use_http_transport: true }))
    @params = params
    @fetched = @connection.fetch(product_url(params[:beta]), resource_base_url, params, 'Get')
    parse unless @fetched.nil?
  end

  def parse
    @name = @fetched['name']
    @uuid = @fetched['uuid']
    @create_time = @fetched['createTime']
    @pyspark_batch = GoogleInSpec::Dataproc::Property::BatchPysparkBatch.new(@fetched['pysparkBatch'], to_s)
    @spark_batch = GoogleInSpec::Dataproc::Property::BatchSparkBatch.new(@fetched['sparkBatch'], to_s)
    @spark_r_batch = GoogleInSpec::Dataproc::Property::BatchSparkRBatch.new(@fetched['sparkRBatch'], to_s)
    @spark_sql_batch = GoogleInSpec::Dataproc::Property::BatchSparkSqlBatch.new(@fetched['sparkSqlBatch'], to_s)
    @runtime_info = GoogleInSpec::Dataproc::Property::BatchRuntimeInfo.new(@fetched['runtimeInfo'], to_s)
    @state = @fetched['state']
    @state_message = @fetched['stateMessage']
    @state_time = @fetched['stateTime']
    @creator = @fetched['creator']
    @labels = GoogleInSpec::Dataproc::Property::BatchLabels.new(@fetched['labels'], to_s)
    @runtime_config = GoogleInSpec::Dataproc::Property::BatchRuntimeConfig.new(@fetched['runtimeConfig'], to_s)
    @environment_config = GoogleInSpec::Dataproc::Property::BatchEnvironmentConfig.new(@fetched['environmentConfig'], to_s)
    @operation = @fetched['operation']
    @state_history = GoogleInSpec::Dataproc::Property::BatchStateHistoryArray.parse(@fetched['stateHistory'], to_s)
  end

  def exists?
    !@fetched.nil?
  end

  def to_s
    "Batch #{@params[:name]}"
  end

  private

  def product_url(_ = nil)
    'https://dataproc.googleapis.com/v1/'
  end

  def resource_base_url
    '{{name}}'
  end
end
