import logging
import json

import features

from app import secscan_notification_queue
from flask import request, make_response, Blueprint
from endpoints.common import route_show_if

logger = logging.getLogger(__name__)
secscan = Blueprint('secscan', __name__)

@route_show_if(features.SECURITY_SCANNER)
@secscan.route('/notification', methods=['POST'])
def secscan_notification():
  data = request.get_json()
  logger.debug('Got notification from Clair: %s', data)

  content = data['Content']
  layer_ids = content.get('NewIntroducingLayersIDs', content.get('IntroducingLayersIDs', []))
  if not layer_ids:
    return make_response('Okay')

  secscan_notification_queue.put(['notification', data['Name']], json.dumps(data))
  return make_response('Okay')