# 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/autoscalingpolicy_basic_algorithm'
require 'google/dataproc/property/autoscalingpolicy_basic_algorithm_spark_standalone_config'
require 'google/dataproc/property/autoscalingpolicy_basic_algorithm_yarn_config'
require 'google/dataproc/property/autoscalingpolicy_labels'
require 'google/dataproc/property/autoscalingpolicy_secondary_worker_config'
require 'google/dataproc/property/autoscalingpolicy_worker_config'

# A provider to manage Dataproc resources.
class DataprocAutoscalingPolicy < GcpResourceBase
  name 'google_dataproc_autoscaling_policy'
  desc 'AutoscalingPolicy'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :id
  attr_reader :name
  attr_reader :basic_algorithm
  attr_reader :worker_config
  attr_reader :secondary_worker_config
  attr_reader :labels

  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
    @id = @fetched['id']
    @name = @fetched['name']
    @basic_algorithm = GoogleInSpec::Dataproc::Property::AutoscalingPolicyBasicAlgorithm.new(@fetched['basicAlgorithm'], to_s)
    @worker_config = GoogleInSpec::Dataproc::Property::AutoscalingPolicyWorkerConfig.new(@fetched['workerConfig'], to_s)
    @secondary_worker_config = GoogleInSpec::Dataproc::Property::AutoscalingPolicySecondaryWorkerConfig.new(@fetched['secondaryWorkerConfig'], to_s)
    @labels = GoogleInSpec::Dataproc::Property::AutoscalingPolicyLabels.new(@fetched['labels'], to_s)
  end

  def exists?
    !@fetched.nil?
  end

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

  private

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

  def resource_base_url
    '{{name}}'
  end
end
