# 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/dlp/property/jobtrigger_inspect_job'
require 'google/dlp/property/jobtrigger_inspect_job_actions'
require 'google/dlp/property/jobtrigger_inspect_job_storage_config'
require 'google/dlp/property/jobtrigger_inspect_job_storage_config_big_query_options'
require 'google/dlp/property/jobtrigger_inspect_job_storage_config_big_query_options_table_reference'
require 'google/dlp/property/jobtrigger_inspect_job_storage_config_cloud_storage_options'
require 'google/dlp/property/jobtrigger_inspect_job_storage_config_cloud_storage_options_file_set'
require 'google/dlp/property/jobtrigger_inspect_job_storage_config_cloud_storage_options_file_set_regex_file_set'
require 'google/dlp/property/jobtrigger_inspect_job_storage_config_datastore_options'
require 'google/dlp/property/jobtrigger_inspect_job_storage_config_datastore_options_kind'
require 'google/dlp/property/jobtrigger_inspect_job_storage_config_datastore_options_partition_id'
require 'google/dlp/property/jobtrigger_inspect_job_storage_config_timespan_config'
require 'google/dlp/property/jobtrigger_inspect_job_storage_config_timespan_config_timestamp_field'
require 'google/dlp/property/jobtrigger_triggers'

# A provider to manage Dlp resources.
class DLPJobTrigger < GcpResourceBase
  name 'google_dlp_job_trigger'
  desc 'JobTrigger'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :name
  attr_reader :description
  attr_reader :display_name
  attr_reader :last_run_time
  attr_reader :status
  attr_reader :triggers
  attr_reader :inspect_job
  attr_reader :parent

  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']
    @display_name = @fetched['displayName']
    @last_run_time = parse_time_string(@fetched['lastRunTime'])
    @status = @fetched['status']
    @triggers = GoogleInSpec::DLP::Property::JobTriggerTriggersArray.parse(@fetched['triggers'], to_s)
    @inspect_job = GoogleInSpec::DLP::Property::JobTriggerInspectJob.new(@fetched['inspectJob'], to_s)
    @parent = @fetched['parent']
  end

  # Handles parsing RFC3339 time string
  def parse_time_string(time_string)
    time_string ? Time.parse(time_string) : nil
  end

  def exists?
    !@fetched.nil?
  end

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

  private

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

  def resource_base_url
    '{{parent}}/jobTriggers/{{name}}'
  end
end
