# frozen_string_literal: false

# ----------------------------------------------------------------------------
#
#     ***     AUTO GENERATED CODE    ***    AUTO GENERATED CODE     ***
#
# ----------------------------------------------------------------------------
#
#     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/kms/property/cryptokey_version_template'

# A provider to manage Cloud Key Management Service resources.
class KMSCryptoKey < GcpResourceBase
  name 'google_kms_crypto_key'
  desc 'CryptoKey'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :crypto_key_name
  attr_reader :create_time
  attr_reader :labels
  attr_reader :purpose
  attr_reader :rotation_period
  attr_reader :version_template
  attr_reader :next_rotation_time
  attr_reader :key_ring
  attr_reader :skip_initial_version_creation

  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
    @crypto_key_name = name_from_self_link(@fetched['name'])
    @create_time = parse_time_string(@fetched['createTime'])
    @labels = @fetched['labels']
    @purpose = @fetched['purpose']
    @rotation_period = @fetched['rotationPeriod']
    @version_template = GoogleInSpec::KMS::Property::CryptoKeyVersionTemplate.new(@fetched['versionTemplate'], to_s)
    @next_rotation_time = parse_time_string(@fetched['nextRotationTime'])
    @key_ring = @fetched['keyRing']
    @skip_initial_version_creation = @fetched['skipInitialVersionCreation']
  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
    "CryptoKey #{@params[:name]}"
  end

  def primary_state
    return if @fetched.nil? || @fetched.empty?
    @fetched.dig('primary', 'state')
  end

  private

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

  def resource_base_url
    'projects/{{project}}/locations/{{location}}/keyRings/{{key_ring_name}}/cryptoKeys/{{name}}'
  end
end
