# 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'
class ApigeeOrganizations < GcpResourceBase
  name 'google_apigee_organizations'
  desc 'Organization plural resource'
  supports platform: 'gcp'

  attr_reader :table

  filter_table_config = FilterTable.create

  filter_table_config.add(:api_consumer_data_encryption_key_names, field: :api_consumer_data_encryption_key_name)
  filter_table_config.add(:runtime_database_encryption_key_names, field: :runtime_database_encryption_key_name)
  filter_table_config.add(:environments, field: :environments)
  filter_table_config.add(:runtime_types, field: :runtime_type)
  filter_table_config.add(:types, field: :type)
  filter_table_config.add(:portal_disableds, field: :portal_disabled)
  filter_table_config.add(:authorized_networks, field: :authorized_network)
  filter_table_config.add(:project_ids, field: :project_id)
  filter_table_config.add(:descriptions, field: :description)
  filter_table_config.add(:ca_certificates, field: :ca_certificate)
  filter_table_config.add(:subscription_types, field: :subscription_type)
  filter_table_config.add(:addons_configs, field: :addons_config)
  filter_table_config.add(:customer_names, field: :customer_name)
  filter_table_config.add(:created_ats, field: :created_at)
  filter_table_config.add(:last_modified_ats, field: :last_modified_at)
  filter_table_config.add(:subscription_plans, field: :subscription_plan)
  filter_table_config.add(:properties, field: :properties)
  filter_table_config.add(:states, field: :state)
  filter_table_config.add(:names, field: :name)
  filter_table_config.add(:disable_vpc_peerings, field: :disable_vpc_peering)
  filter_table_config.add(:control_plane_encryption_key_names, field: :control_plane_encryption_key_name)
  filter_table_config.add(:analytics_regions, field: :analytics_region)
  filter_table_config.add(:api_consumer_data_locations, field: :api_consumer_data_location)
  filter_table_config.add(:display_names, field: :display_name)
  filter_table_config.add(:apigee_project_ids, field: :apigee_project_id)
  filter_table_config.add(:expires_ats, field: :expires_at)
  filter_table_config.add(:attributes, field: :attributes)
  filter_table_config.add(:billing_types, field: :billing_type)

  filter_table_config.connect(self, :table)

  def initialize(params = {})
    super(params.merge({ use_http_transport: true }))
    @params = params
    @table = fetch_wrapped_resource('organizations')
  end

  def fetch_wrapped_resource(wrap_path)
    # fetch_resource returns an array of responses (to handle pagination)
    result = @connection.fetch_all(product_url, resource_base_url, @params, 'Get')
    return if result.nil?

    # Conversion of string -> object hash to symbol -> object hash that InSpec needs
    converted = []
    result.each do |response|
      next if response.nil? || !response.key?(wrap_path)
      response[wrap_path].each do |hash|
        hash_with_symbols = {}
        hash.each_key do |key|
          name, value = transform(key, hash)
          hash_with_symbols[name] = value
        end
        converted.push(hash_with_symbols)
      end
    end

    converted
  end

  def transform(key, value)
    return transformers[key].call(value) if transformers.key?(key)

    [key.to_sym, value]
  end

  def transformers
    {
      'apiConsumerDataEncryptionKeyName' => ->(obj) { [:api_consumer_data_encryption_key_name, obj['apiConsumerDataEncryptionKeyName']] },
      'runtimeDatabaseEncryptionKeyName' => ->(obj) { [:runtime_database_encryption_key_name, obj['runtimeDatabaseEncryptionKeyName']] },
      'environments' => ->(obj) { [:environments, obj['environments']] },
      'runtimeType' => ->(obj) { [:runtime_type, obj['runtimeType']] },
      'type' => ->(obj) { [:type, obj['type']] },
      'portalDisabled' => ->(obj) { [:portal_disabled, obj['portalDisabled']] },
      'authorizedNetwork' => ->(obj) { [:authorized_network, obj['authorizedNetwork']] },
      'projectId' => ->(obj) { [:project_id, obj['projectId']] },
      'description' => ->(obj) { [:description, obj['description']] },
      'caCertificate' => ->(obj) { [:ca_certificate, obj['caCertificate']] },
      'subscriptionType' => ->(obj) { [:subscription_type, obj['subscriptionType']] },
      'addonsConfig' => ->(obj) { [:addons_config, GoogleInSpec::Apigee::Property::OrganizationAddonsConfig.new(obj['addonsConfig'], to_s)] },
      'customerName' => ->(obj) { [:customer_name, obj['customerName']] },
      'createdAt' => ->(obj) { [:created_at, obj['createdAt']] },
      'lastModifiedAt' => ->(obj) { [:last_modified_at, obj['lastModifiedAt']] },
      'subscriptionPlan' => ->(obj) { [:subscription_plan, obj['subscriptionPlan']] },
      'properties' => ->(obj) { [:properties, GoogleInSpec::Apigee::Property::OrganizationProperties.new(obj['properties'], to_s)] },
      'state' => ->(obj) { [:state, obj['state']] },
      'name' => ->(obj) { [:name, obj['name']] },
      'disableVpcPeering' => ->(obj) { [:disable_vpc_peering, obj['disableVpcPeering']] },
      'controlPlaneEncryptionKeyName' => ->(obj) { [:control_plane_encryption_key_name, obj['controlPlaneEncryptionKeyName']] },
      'analyticsRegion' => ->(obj) { [:analytics_region, obj['analyticsRegion']] },
      'apiConsumerDataLocation' => ->(obj) { [:api_consumer_data_location, obj['apiConsumerDataLocation']] },
      'displayName' => ->(obj) { [:display_name, obj['displayName']] },
      'apigeeProjectId' => ->(obj) { [:apigee_project_id, obj['apigeeProjectId']] },
      'expiresAt' => ->(obj) { [:expires_at, obj['expiresAt']] },
      'attributes' => ->(obj) { [:attributes, obj['attributes']] },
      'billingType' => ->(obj) { [:billing_type, obj['billingType']] },
    }
  end

  private

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

  def resource_base_url
    '{{parent}}'
  end
end
