require "azure_generic_resources"

class AzureSqlDatabaseServerVulnerabilityAssessments< AzureGenericResources
  name "azure_sql_database_server_vulnerability_assessments"
  desc "Verifies all settings of Azure SQL Database Server Vulnerability Assessment"
  example <<-EXAMPLE
    describe azure_sql_database_server_vulnerability_assessments(resource_group: 'RESOURCE_GROUP_NAME', server_name: 'SERVER_NAME') do
      it { should exist }
    end
  EXAMPLE

  def initialize(opts = {})
    raise ArgumentError, "Parameters must be provided in an Hash object." unless opts.is_a?(Hash)

    opts[:resource_provider] = specific_resource_constraint("Microsoft.Sql/servers", opts)
    opts[:required_parameters] = %i(server_name)
    opts[:resource_path] = [opts[:server_name], "vulnerabilityAssessments"].join("/")

    super(opts, true)

    return if failed_resource?
    # Define the column and field names for FilterTable.
    # In most cases, the `column` should be the pluralized form of the `field`.
    # @see https://github.com/inspec/inspec/blob/master/docs/dev/filtertable-usage.md

    @table.map do |row|
      props = row[:properties][:recurringScans]
      row[:isEnabled] = props[:isEnabled]
      row[:emailSubscriptionAdmins] = props[:emailSubscriptionAdmins]
      row[:emails] = props[:emails]
    end

    table_schema = [
      { column: :ids, field: :id },
      { column: :names, field: :name },
      { column: :types, field: :type },
      { column: :properties, field: :properties },
      { column: :isEnabled, field: :isEnabled },
      { column: :emailSubscriptionAdmins, field: :emailSubscriptionAdmins },
      { column: :emails, field: :emails },
    ]
    AzureGenericResources.populate_filter_table(:table, table_schema)
  end

  def to_s
    super(AzureSqlDatabaseServerVulnerabilityAssessments)
  end
end
