# 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'

# A provider to manage Compute Engine resources.
class ComputeForwardingRule < GcpResourceBase
  name 'google_compute_forwarding_rule'
  desc 'ForwardingRule'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :creation_timestamp
  attr_reader :is_mirroring_collector
  attr_reader :description
  attr_reader :id
  attr_reader :ip_address
  attr_reader :ip_protocol
  attr_reader :backend_service
  attr_reader :load_balancing_scheme
  attr_reader :name
  attr_reader :network
  attr_reader :port_range
  attr_reader :ports
  attr_reader :subnetwork
  attr_reader :target
  attr_reader :allow_global_access
  attr_reader :labels
  attr_reader :label_fingerprint
  attr_reader :all_ports
  attr_reader :network_tier
  attr_reader :service_label
  attr_reader :service_name
  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
    @creation_timestamp = parse_time_string(@fetched['creationTimestamp'])
    @is_mirroring_collector = @fetched['isMirroringCollector']
    @description = @fetched['description']
    @id = @fetched['id']
    @ip_address = @fetched['IPAddress']
    @ip_protocol = @fetched['IPProtocol']
    @backend_service = @fetched['backendService']
    @load_balancing_scheme = @fetched['loadBalancingScheme']
    @name = @fetched['name']
    @network = name_from_self_link(@fetched['network'])
    @port_range = @fetched['portRange']
    @ports = @fetched['ports']
    @subnetwork = @fetched['subnetwork']
    @target = @fetched['target']
    @allow_global_access = @fetched['allowGlobalAccess']
    @labels = @fetched['labels']
    @label_fingerprint = @fetched['labelFingerprint']
    @all_ports = @fetched['allPorts']
    @network_tier = @fetched['networkTier']
    @service_label = @fetched['serviceLabel']
    @service_name = @fetched['serviceName']
    @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
    "ForwardingRule #{@params[:name]}"
  end

  private

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

  def resource_base_url
    'projects/{{project}}/regions/{{region}}/forwardingRules/{{name}}'
  end
end
