# 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/sql/property/databaseinstance_disk_encryption_configuration'
require 'google/sql/property/databaseinstance_disk_encryption_status'
require 'google/sql/property/databaseinstance_failover_replica'
require 'google/sql/property/databaseinstance_ip_addresses'
require 'google/sql/property/databaseinstance_replica_configuration'
require 'google/sql/property/databaseinstance_replica_configuration_mysql_replica_configuration'
require 'google/sql/property/databaseinstance_server_ca_cert'
require 'google/sql/property/databaseinstance_settings'
require 'google/sql/property/databaseinstance_settings_backup_configuration'
require 'google/sql/property/databaseinstance_settings_database_flags'
require 'google/sql/property/databaseinstance_settings_ip_configuration'
require 'google/sql/property/databaseinstance_settings_ip_configuration_authorized_networks'

# A provider to manage Cloud SQL resources.
class SQLDatabaseInstance < GcpResourceBase
  name 'google_sql_database_instance'
  desc 'DatabaseInstance'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :backend_type
  attr_reader :kind
  attr_reader :connection_name
  attr_reader :database_version
  attr_reader :failover_replica
  attr_reader :instance_type
  attr_reader :ip_addresses
  attr_reader :ipv6_address
  attr_reader :master_instance_name
  attr_reader :max_disk_size
  attr_reader :name
  attr_reader :region
  attr_reader :replica_configuration
  attr_reader :settings
  attr_reader :gce_zone
  attr_reader :state
  attr_reader :disk_encryption_configuration
  attr_reader :disk_encryption_status
  attr_reader :server_ca_cert

  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
    @backend_type = @fetched['backendType']
    @kind = @fetched['kind']
    @connection_name = @fetched['connectionName']
    @database_version = @fetched['databaseVersion']
    @failover_replica = GoogleInSpec::SQL::Property::DatabaseInstanceFailoverReplica.new(@fetched['failoverReplica'], to_s)
    @instance_type = @fetched['instanceType']
    @ip_addresses = GoogleInSpec::SQL::Property::DatabaseInstanceIpAddressesArray.parse(@fetched['ipAddresses'], to_s)
    @ipv6_address = @fetched['ipv6Address']
    @master_instance_name = @fetched['masterInstanceName']
    @max_disk_size = @fetched['maxDiskSize']
    @name = @fetched['name']
    @region = @fetched['region']
    @replica_configuration = GoogleInSpec::SQL::Property::DatabaseInstanceReplicaConfiguration.new(@fetched['replicaConfiguration'], to_s)
    @settings = GoogleInSpec::SQL::Property::DatabaseInstanceSettings.new(@fetched['settings'], to_s)
    @gce_zone = @fetched['gceZone']
    @state = @fetched['state']
    @disk_encryption_configuration = GoogleInSpec::SQL::Property::DatabaseInstanceDiskEncryptionConfiguration.new(@fetched['diskEncryptionConfiguration'], to_s)
    @disk_encryption_status = GoogleInSpec::SQL::Property::DatabaseInstanceDiskEncryptionStatus.new(@fetched['diskEncryptionStatus'], to_s)
    @server_ca_cert = GoogleInSpec::SQL::Property::DatabaseInstanceServerCaCert.new(@fetched['serverCaCert'], to_s)
  end

  def exists?
    !@fetched.nil?
  end

  def to_s
    "DatabaseInstance #{@params[:instance]}"
  end

  def has_ip_configuration_require_ssl?
    return false if @settings&.ip_configuration&.require_ssl.nil?
    return true if @settings.ip_configuration.require_ssl.to_s.casecmp('true').zero?
    false
  end

  def authorized_networks
    return [] if @settings&.ip_configuration&.authorized_networks.nil?
    @database.settings.ip_configuration.authorized_networks.map(&:value)
  end

  def primary_ip_address
    return '' if !@ip_addresses.empty? || @ip_addresses[0].ip_address.nil?
    @ip_addresses[0].ip_address
  end

  private

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

  def resource_base_url
    'projects/{{project}}/instances/{{instance}}'
  end
end
