#
# Copyright (C) 2014 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

require 'aws-sdk'

module Services
  class NotificationService
    DEFAULT_CONFIG = {
      notification_service_queue_name: 'notification-service'
    }.freeze

    def self.process(global_id, body, type, to)
      self.notification_queue.send_message({
        'global_id' => global_id,
        'type' => type,
        'message' => body,
        'target' => to,
        'request_id' => RequestContextGenerator.request_id
      }.to_json)
    end

    def self.notification_queue
      return @notification_queue if defined?(@notification_queue)
      @config ||= DEFAULT_CONFIG.merge(ConfigFile.load('notification_service').try(:symbolize_keys))
      sqs = AWS::SQS.new(@config)
      @notification_queue = sqs.queues.named(@config[:notification_service_queue_name])
    end
  end
end

#
# Copyright (C) 2014-2016 Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
#

require 'aws-sdk'

module Services
  class NotificationService
    def self.process(global_id, body, type, to)
      return unless notification_queue.present?

      notification_queue.send_message({
        global_id: global_id,
        type: type,
        message: body,
        target: to,
        request_id: RequestContextGenerator.request_id
      }.to_json)
    end

    class << self
      private

      def notification_queue
        return nil if config.blank?

        @notification_queue ||= begin
          queue_name = config['notification_service_queue_name']
          sqs = AWS::SQS.new(config)
          sqs.queues.named(queue_name)
        end
      end

      def config
        ConfigFile.load('notification_service') || {}
      end
    end
  end
end

Fix nil value loading notification service config
Fix nil value loading notification service config

Closes CNVS-27564

Avoids causing a crash when configuration for the notification service
is absent but feature flags are enabled.

Also removes the default config options, meaning that
notification_service_queue_name is now required in the config.

Test plan:
- Remove config/notification_service.yml
- Enable the notification service
- Message delivery should fail silently
- Put in invalid credentials
- It should explode
- Put in correct credentials
- It should work

Change-Id: I68559c17c08f8efdbd9e2bf478ce0b6795f471bf
Reviewed-on: https://gerrit.instructure.com/74621
Tested-by: Jenkins
Reviewed-by: Steven Burnett <a0b450117c5d20a22d5c4d52bb8ae13adee003d8@instructure.com>
Reviewed-by: Christina Wuest <54afda8c1ceb6b42b1ea3027c45a7930c54ff01c@instructure.com>
QA-Review: Gentry Beckmann <f487939f789774cca922ca5b886632de73477ea0@instructure.com>
Product-Review: Gentry Beckmann <f487939f789774cca922ca5b886632de73477ea0@instructure.com>
