Ejemplo n.º 1
0
    def is_valid(self, req, data):
        """
        Args:
          req (WSGIRequest): Request from Resource
          data (dict): Dict that represents the json sent via the request
        Returns:
          bool: True if passes json schema and IP validation
        """
        valid = False
        try:
            #jsonschema.validate(data, inventory_schema)
            valid = True
        except:
            file_name = 'failures/%s.log' % datetime.now()
            logging.info('Failed validation see file "%s"' % file_name)
            log_failed_data(file_name, data)

        return valid
Ejemplo n.º 2
0
    def on_post(self, req, resp):
        """ Handle API Posts to /api/inventory
        Uses a request handler to perform data validation and preparation for
        elastic database.

        If any exceptions are raised by the Performance Model during saving. Check the logs.
        Log file is saved based on value of settings.LOG_FILE

        Args:
            req: WSGI Request
            resp: WSGI Response
        Raises:
            falcon.HTTPError: For database level errors
                returns a 202 to the user..this is to prevent a 500 Status
        """
        data = get_json(req)
        handler = RequestHandler()
        if handler.is_valid(req, data):
            data = handler.prepare(data, req)
            # Save Data
            try:
                es = Elasticsearch([settings.ELASTIC_URL], port=settings.ELASTIC_PORT)
                model = InventoryModel(es)
                model.save(data)
            except Exception as e:
                if 'timestamp' in data:
                    data['timestamp'] = str(data['timestamp'])
                file_name = 'failures/%s.log' % datetime.now()
                logging.error('Failed to save to database see file "%s due to %s"' % (file_name, e.message))
                log_failed_data(file_name, data)
                raise falcon.HTTPError(falcon.HTTP_400, 'error', 'saving')

            resp.status = falcon.HTTP_201
            resp.body = '{"title": "created"}'
        else:
            raise falcon.HTTPError(falcon.HTTP_400, 'Error', 'Invalid request')