# 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/kms/property/ekmconnection_service_resolvers'
require 'google/kms/property/ekmconnection_service_resolvers_server_certificates'

# A provider to manage Cloud Key Management Service resources.
class KMSEkmConnection < GcpResourceBase
  name 'google_kms_ekm_connection'
  desc 'EkmConnection'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :name
  attr_reader :create_time
  attr_reader :service_resolvers
  attr_reader :location

  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']
    @create_time = parse_time_string(@fetched['createTime'])
    @service_resolvers = GoogleInSpec::KMS::Property::EkmConnectionServiceResolvers.new(@fetched['serviceResolvers'], to_s)
    @location = @fetched['location']
  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
    "EkmConnection #{@params[:name]}"
  end

  private

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

  def resource_base_url
    'projects/{{project}}/locations/{{location}}/ekmConnections/{{name}}'
  end
end
