# 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/cloudscheduler/property/job_app_engine_http_target'
require 'google/cloudscheduler/property/job_app_engine_http_target_app_engine_routing'
require 'google/cloudscheduler/property/job_http_target'
require 'google/cloudscheduler/property/job_http_target_oauth_token'
require 'google/cloudscheduler/property/job_http_target_oidc_token'
require 'google/cloudscheduler/property/job_pubsub_target'
require 'google/cloudscheduler/property/job_retry_config'

# A provider to manage Cloud Scheduler resources.
class CloudSchedulerJob < GcpResourceBase
  name 'google_cloud_scheduler_job'
  desc 'Job'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :name
  attr_reader :description
  attr_reader :schedule
  attr_reader :time_zone
  attr_reader :state
  attr_reader :attempt_deadline
  attr_reader :retry_config
  attr_reader :pubsub_target
  attr_reader :app_engine_http_target
  attr_reader :http_target
  attr_reader :region

  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']
    @description = @fetched['description']
    @schedule = @fetched['schedule']
    @time_zone = @fetched['timeZone']
    @state = @fetched['state']
    @attempt_deadline = @fetched['attemptDeadline']
    @retry_config = GoogleInSpec::CloudScheduler::Property::JobRetryConfig.new(@fetched['retryConfig'], to_s)
    @pubsub_target = GoogleInSpec::CloudScheduler::Property::JobPubsubTarget.new(@fetched['pubsubTarget'], to_s)
    @app_engine_http_target = GoogleInSpec::CloudScheduler::Property::JobAppEngineHttpTarget.new(@fetched['appEngineHttpTarget'], to_s)
    @http_target = GoogleInSpec::CloudScheduler::Property::JobHttpTarget.new(@fetched['httpTarget'], to_s)
    @region = @fetched['region']
  end

  def exists?
    !@fetched.nil?
  end

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

  private

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

  def resource_base_url
    'projects/{{project}}/locations/{{region}}/jobs/{{name}}'
  end
end
