# 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/orgpolicy/property/policy_alternate'
require 'google/orgpolicy/property/policy_alternate_spec'
require 'google/orgpolicy/property/policy_alternate_spec_rules'
require 'google/orgpolicy/property/policy_dry_run_spec'
require 'google/orgpolicy/property/policy_dry_run_spec_rules'
require 'google/orgpolicy/property/policy_spec'
require 'google/orgpolicy/property/policy_spec_rules'

# A provider to manage orgpolicy resources.
class OrgpolicyOrganizationPolicy < GcpResourceBase
  name 'google_orgpolicy_organization_policy'
  desc 'OrganizationPolicy'
  supports platform: 'gcp'

  attr_reader :params
  attr_reader :dry_run_spec
  attr_reader :spec
  attr_reader :name
  attr_reader :alternate

  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
    @dry_run_spec = GoogleInSpec::Orgpolicy::Property::PolicyDryRunSpec.new(@fetched['dryRunSpec'], to_s)
    @spec = GoogleInSpec::Orgpolicy::Property::PolicySpec.new(@fetched['spec'], to_s)
    @name = @fetched['name']
    @alternate = GoogleInSpec::Orgpolicy::Property::PolicyAlternate.new(@fetched['alternate'], to_s)
  end

  def exists?
    !@fetched.nil?
  end

  def to_s
    "OrganizationPolicy #{@params[:name]}"
  end

  private

  def product_url(_ = nil)
    'https://orgpolicy.googleapis.com/v2/'
  end

  def resource_base_url
    '{{parent}}/policies/{{name}}'
  end
end
