# 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/filestore/property/instance_file_shares'
require 'google/filestore/property/instance_networks'

# A provider to manage Filestore resources.
class FilestoreInstance < GcpResourceBase
  name 'google_filestore_instance'
  desc 'Instance'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :name
  attr_reader :description
  attr_reader :create_time
  attr_reader :tier
  attr_reader :labels
  attr_reader :file_shares
  attr_reader :networks
  attr_reader :etag
  attr_reader :zone

  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']
    @create_time = parse_time_string(@fetched['createTime'])
    @tier = @fetched['tier']
    @labels = @fetched['labels']
    @file_shares = GoogleInSpec::Filestore::Property::InstanceFileSharesArray.parse(@fetched['fileShares'], to_s)
    @networks = GoogleInSpec::Filestore::Property::InstanceNetworksArray.parse(@fetched['networks'], to_s)
    @etag = @fetched['etag']
    @zone = @fetched['zone']
  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://file.googleapis.com/v1beta1/'
    else
      'https://file.googleapis.com/v1/'
    end
  end

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