# 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/redis/property/instance_server_ca_certs'

# A provider to manage Memorystore (Redis) resources.
class RedisInstance < GcpResourceBase
  name 'google_redis_instance'
  desc 'Instance'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :alternative_location_id
  attr_reader :auth_enabled
  attr_reader :authorized_network
  attr_reader :connect_mode
  attr_reader :create_time
  attr_reader :current_location_id
  attr_reader :display_name
  attr_reader :host
  attr_reader :labels
  attr_reader :redis_configs
  attr_reader :location_id
  attr_reader :name
  attr_reader :memory_size_gb
  attr_reader :port
  attr_reader :persistence_iam_identity
  attr_reader :redis_version
  attr_reader :reserved_ip_range
  attr_reader :tier
  attr_reader :transit_encryption_mode
  attr_reader :server_ca_certs
  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
    @alternative_location_id = @fetched['alternativeLocationId']
    @auth_enabled = @fetched['authEnabled']
    @authorized_network = @fetched['authorizedNetwork']
    @connect_mode = @fetched['connectMode']
    @create_time = parse_time_string(@fetched['createTime'])
    @current_location_id = @fetched['currentLocationId']
    @display_name = @fetched['displayName']
    @host = @fetched['host']
    @labels = @fetched['labels']
    @redis_configs = @fetched['redisConfigs']
    @location_id = @fetched['locationId']
    @name = @fetched['name']
    @memory_size_gb = @fetched['memorySizeGb']
    @port = @fetched['port']
    @persistence_iam_identity = @fetched['persistenceIamIdentity']
    @redis_version = @fetched['redisVersion']
    @reserved_ip_range = @fetched['reservedIpRange']
    @tier = @fetched['tier']
    @transit_encryption_mode = @fetched['transitEncryptionMode']
    @server_ca_certs = GoogleInSpec::Redis::Property::InstanceServerCaCertsArray.parse(@fetched['serverCaCerts'], to_s)
    @region = @fetched['region']
  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
    "Instance #{@params[:name]}"
  end

  private

  def product_url(beta = false)
    if beta
      'https://redis.googleapis.com/v1beta1/'
    else
      'https://redis.googleapis.com/v1/'
    end
  end

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