# 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/run/property/job_annotations'
require 'google/run/property/job_binary_authorization'
require 'google/run/property/job_conditions'
require 'google/run/property/job_labels'
require 'google/run/property/job_latest_created_execution'
require 'google/run/property/job_template'
require 'google/run/property/job_template_annotations'
require 'google/run/property/job_template_labels'
require 'google/run/property/job_template_template'
require 'google/run/property/job_template_template_containers'
require 'google/run/property/job_template_template_volumes'
require 'google/run/property/job_template_template_vpc_access'
require 'google/run/property/job_template_template_vpc_access_network_interfaces'
require 'google/run/property/job_terminal_condition'

# A provider to manage run resources.
class RunJob < GcpResourceBase
  name 'google_run_job'
  desc 'Job'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :name
  attr_reader :uid
  attr_reader :generation
  attr_reader :labels
  attr_reader :annotations
  attr_reader :create_time
  attr_reader :update_time
  attr_reader :delete_time
  attr_reader :expire_time
  attr_reader :creator
  attr_reader :last_modifier
  attr_reader :client
  attr_reader :client_version
  attr_reader :launch_stage
  attr_reader :binary_authorization
  attr_reader :template
  attr_reader :observed_generation
  attr_reader :terminal_condition
  attr_reader :conditions
  attr_reader :execution_count
  attr_reader :latest_created_execution
  attr_reader :reconciling
  attr_reader :satisfies_pzs
  attr_reader :start_execution_token
  attr_reader :run_execution_token
  attr_reader :etag

  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']
    @uid = @fetched['uid']
    @generation = @fetched['generation']
    @labels = GoogleInSpec::Run::Property::JobLabels.new(@fetched['labels'], to_s)
    @annotations = GoogleInSpec::Run::Property::JobAnnotations.new(@fetched['annotations'], to_s)
    @create_time = @fetched['createTime']
    @update_time = @fetched['updateTime']
    @delete_time = @fetched['deleteTime']
    @expire_time = @fetched['expireTime']
    @creator = @fetched['creator']
    @last_modifier = @fetched['lastModifier']
    @client = @fetched['client']
    @client_version = @fetched['clientVersion']
    @launch_stage = @fetched['launchStage']
    @binary_authorization = GoogleInSpec::Run::Property::JobBinaryAuthorization.new(@fetched['binaryAuthorization'], to_s)
    @template = GoogleInSpec::Run::Property::JobTemplate.new(@fetched['template'], to_s)
    @observed_generation = @fetched['observedGeneration']
    @terminal_condition = GoogleInSpec::Run::Property::JobTerminalCondition.new(@fetched['terminalCondition'], to_s)
    @conditions = GoogleInSpec::Run::Property::JobConditionsArray.parse(@fetched['conditions'], to_s)
    @execution_count = @fetched['executionCount']
    @latest_created_execution = GoogleInSpec::Run::Property::JobLatestCreatedExecution.new(@fetched['latestCreatedExecution'], to_s)
    @reconciling = @fetched['reconciling']
    @satisfies_pzs = @fetched['satisfiesPzs']
    @start_execution_token = @fetched['startExecutionToken']
    @run_execution_token = @fetched['runExecutionToken']
    @etag = @fetched['etag']
  end

  def exists?
    !@fetched.nil?
  end

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

  private

  def product_url(_ = nil)
    'https://run.googleapis.com/v2/'
  end

  def resource_base_url
    '{{name}}'
  end
end
