Skip to content
Snippets Groups Projects
default.py 2.65 KiB
"""
Health-related Endpoints
=========================

These endpoints are intended for checking the status of the service.

.. contents:: :local:

/version
---------------------

.. autofunction:: inventory_provider.routes.default.version

/ping
-----------------------------

.. autofunction:: inventory_provider.routes.default.ping


"""
import pkg_resources

from flask import Blueprint, jsonify, current_app
from inventory_provider.routes import common
from inventory_provider.tasks.common import get_current_redis, get_latch

routes = Blueprint('inventory-data-default-routes', __name__)

API_VERSION = '0.1'

VERSION_SCHEMA = {
    '$schema': 'https://json-schema.org/draft-07/schema#',

    'definitions': {
        'latch': {
            'type': 'object',
            'properties': {
                'current': {'type': 'integer'},
                'next': {'type': 'integer'},
                'this': {'type': 'integer'},
                'failure': {'type': 'boolean'},
                'pending': {'type': 'boolean'},
                'timestamp': {'type': 'number'},
                'update-started': {'type': 'number'}
            },
            'required': ['current', 'next', 'this', 'pending', 'failure'],
            'additionalProperties': False
        }
    },

    'type': 'object',
    'properties': {
        'api': {
            'type': 'string',
            'pattern': r'\d+\.\d+'
        },
        'module': {
            'type': 'string',
            'pattern': r'\d+\.\d+'
        },
        'latch': {'$ref': '#/definitions/latch'}
    },
    'required': ['api', 'module'],
    'additionalProperties': False
}


@routes.after_request
def after_request(resp):
    return common.after_request(resp)


@routes.route('/version', methods=['GET'])
@common.require_accepts_json
def version():
    """
    Returns a json object with information about the module version.

    The response will be formatted according to the following schema:

    .. asjson:: inventory_provider.routes.default.VERSION_SCHEMA

    :return:
    """
    config = current_app.config["INVENTORY_PROVIDER_CONFIG"]
    version_params = {
        'api': API_VERSION,
        'module':
            pkg_resources.get_distribution('inventory_provider').version
    }
    latch = get_latch(get_current_redis(config))
    if latch:
        version_params['latch'] = latch
    return jsonify(version_params)


@routes.route('/ping', methods=['GET'])
def ping():
    """
    This method can be used to verify the web service is alive
    and processing requests.  This endpoint is used by haproxy
    for backend http checks.

    There's no response payload, only 204 (empty content) will be returned.
    """
    return '', 204