# 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/session_environment_config'
require 'google/dataproc/property/session_environment_config_execution_config'
require 'google/dataproc/property/session_environment_config_peripherals_config'
require 'google/dataproc/property/session_environment_config_peripherals_config_spark_history_server_config'
require 'google/dataproc/property/session_jupyter_session'
require 'google/dataproc/property/session_labels'
require 'google/dataproc/property/session_runtime_config'
require 'google/dataproc/property/session_runtime_config_properties'
require 'google/dataproc/property/session_runtime_config_repository_config'
require 'google/dataproc/property/session_runtime_config_repository_config_pypi_repository_config'
require 'google/dataproc/property/session_runtime_info'
require 'google/dataproc/property/session_runtime_info_approximate_usage'
require 'google/dataproc/property/session_runtime_info_current_usage'
require 'google/dataproc/property/session_runtime_info_endpoints'
require 'google/dataproc/property/session_state_history'

# A provider to manage Dataproc resources.
class DataprocSession < GcpResourceBase
  name 'google_dataproc_session'
  desc 'Session'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :name
  attr_reader :uuid
  attr_reader :create_time
  attr_reader :jupyter_session
  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 :user
  attr_reader :state_history
  attr_reader :session_template

  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']
    @jupyter_session = GoogleInSpec::Dataproc::Property::SessionJupyterSession.new(@fetched['jupyterSession'], to_s)
    @runtime_info = GoogleInSpec::Dataproc::Property::SessionRuntimeInfo.new(@fetched['runtimeInfo'], to_s)
    @state = @fetched['state']
    @state_message = @fetched['stateMessage']
    @state_time = @fetched['stateTime']
    @creator = @fetched['creator']
    @labels = GoogleInSpec::Dataproc::Property::SessionLabels.new(@fetched['labels'], to_s)
    @runtime_config = GoogleInSpec::Dataproc::Property::SessionRuntimeConfig.new(@fetched['runtimeConfig'], to_s)
    @environment_config = GoogleInSpec::Dataproc::Property::SessionEnvironmentConfig.new(@fetched['environmentConfig'], to_s)
    @user = @fetched['user']
    @state_history = GoogleInSpec::Dataproc::Property::SessionStateHistoryArray.parse(@fetched['stateHistory'], to_s)
    @session_template = @fetched['sessionTemplate']
  end

  def exists?
    !@fetched.nil?
  end

  def to_s
    "Session #{@params}"
  end

  private

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

  def resource_base_url
    '{{name}}'
  end
end
