Commit b125b598 authored by kaiyou's avatar kaiyou
Browse files

Expose the root pdns api function

parent 5fceb2be
Pipeline #938 passed with stage
in 3 minutes and 15 seconds
......@@ -5,7 +5,7 @@ from .user import user_bp
from .dashboard import dashboard_bp
from .domain import domain_bp
from .admin import admin_bp
from .api import api_bp
from .api import api_bp, apiv1_bp
def init_app(app):
......@@ -17,6 +17,7 @@ def init_app(app):
app.register_blueprint(domain_bp)
app.register_blueprint(admin_bp)
app.register_blueprint(api_bp)
app.register_blueprint(apiv1_bp)
app.register_error_handler(400, handle_bad_request)
app.register_error_handler(401, handle_unauthorized_access)
......
......@@ -10,7 +10,8 @@ from ..lib.schema import ApiKeySchema, DomainSchema, ApiPlainKeySchema
from ..lib.errors import DomainNotExists, DomainAlreadyExists, DomainAccessForbidden, RequestIsNotJSON, ApiKeyCreateFail, ApiKeyNotUsable, NotEnoughPrivileges
from ..decorators import api_basic_auth, api_can_create_domain, is_json, apikey_auth, apikey_is_admin, apikey_can_access_domain
api_bp = Blueprint('api', __name__, url_prefix='/api/v1')
api_bp = Blueprint('api', __name__, url_prefix='/api')
apiv1_bp = Blueprint('apiv1', __name__, url_prefix='/api/v1')
apikey_schema = ApiKeySchema(many=True)
domain_schema = DomainSchema(many=True)
......@@ -53,62 +54,67 @@ def get_user_apikeys(domain_name=None):
return info
@api_bp.errorhandler(400)
@api_bp.route('/', methods=['GET'])
def api_version():
return json.dumps([{"url": "/api/v1", "version": 1}])
@apiv1_bp.errorhandler(400)
def handle_400(err):
return json.dumps({"msg": "Bad Request"}), 400
@api_bp.errorhandler(401)
@apiv1_bp.errorhandler(401)
def handle_401(err):
return json.dumps({"msg": "Unauthorized"}), 401
@api_bp.errorhandler(409)
@apiv1_bp.errorhandler(409)
def handle_409(err):
return json.dumps({"msg": "Conflict"}), 409
@api_bp.errorhandler(500)
@apiv1_bp.errorhandler(500)
def handle_500(err):
return json.dumps({"msg": "Internal Server Error"}), 500
@api_bp.errorhandler(DomainNotExists)
@apiv1_bp.errorhandler(DomainNotExists)
def handle_domain_not_exists(err):
return json.dumps(err.to_dict()), err.status_code
@api_bp.errorhandler(DomainAlreadyExists)
@apiv1_bp.errorhandler(DomainAlreadyExists)
def handle_domain_already_exists(err):
return json.dumps(err.to_dict()), err.status_code
@api_bp.errorhandler(DomainAccessForbidden)
@apiv1_bp.errorhandler(DomainAccessForbidden)
def handle_domain_access_forbidden(err):
return json.dumps(err.to_dict()), err.status_code
@api_bp.errorhandler(ApiKeyCreateFail)
@apiv1_bp.errorhandler(ApiKeyCreateFail)
def handle_apikey_create_fail(err):
return json.dumps(err.to_dict()), err.status_code
@api_bp.errorhandler(ApiKeyNotUsable)
@apiv1_bp.errorhandler(ApiKeyNotUsable)
def handle_apikey_not_usable(err):
return json.dumps(err.to_dict()), err.status_code
@api_bp.errorhandler(NotEnoughPrivileges)
@apiv1_bp.errorhandler(NotEnoughPrivileges)
def handle_not_enough_privileges(err):
return json.dumps(err.to_dict()), err.status_code
@api_bp.errorhandler(RequestIsNotJSON)
@apiv1_bp.errorhandler(RequestIsNotJSON)
def handle_request_is_not_json(err):
return json.dumps(err.to_dict()), err.status_code
@api_bp.before_request
@apiv1_bp.before_request
@is_json
def before_request():
# Check site is in maintenance mode
......@@ -123,7 +129,7 @@ def before_request():
}))
@api_bp.route('/pdnsadmin/zones', methods=['POST'])
@apiv1_bp.route('/pdnsadmin/zones', methods=['POST'])
@api_basic_auth
@api_can_create_domain
def api_login_create_zone():
......@@ -176,7 +182,7 @@ def api_login_create_zone():
return resp.content, resp.status_code, resp.headers.items()
@api_bp.route('/pdnsadmin/zones', methods=['GET'])
@apiv1_bp.route('/pdnsadmin/zones', methods=['GET'])
@api_basic_auth
def api_login_list_zones():
if current_user.role.name not in ['Administrator', 'Operator']:
......@@ -188,7 +194,7 @@ def api_login_list_zones():
return json.dumps(domain_schema.dump(domain_obj_list)), 200
@api_bp.route('/pdnsadmin/zones/<string:domain_name>', methods=['DELETE'])
@apiv1_bp.route('/pdnsadmin/zones/<string:domain_name>', methods=['DELETE'])
@api_basic_auth
@api_can_create_domain
def api_login_delete_zone(domain_name):
......@@ -240,7 +246,7 @@ def api_login_delete_zone(domain_name):
return resp.content, resp.status_code, resp.headers.items()
@api_bp.route('/pdnsadmin/apikeys', methods=['POST'])
@apiv1_bp.route('/pdnsadmin/apikeys', methods=['POST'])
@api_basic_auth
def api_generate_apikey():
data = request.get_json()
......@@ -305,8 +311,8 @@ def api_generate_apikey():
return json.dumps(apikey_plain_schema.dump([apikey])), 201
@api_bp.route('/pdnsadmin/apikeys', defaults={'domain_name': None})
@api_bp.route('/pdnsadmin/apikeys/<string:domain_name>')
@apiv1_bp.route('/pdnsadmin/apikeys', defaults={'domain_name': None})
@apiv1_bp.route('/pdnsadmin/apikeys/<string:domain_name>')
@api_basic_auth
def api_get_apikeys(domain_name):
apikeys = []
......@@ -346,7 +352,7 @@ def api_get_apikeys(domain_name):
return json.dumps(apikey_schema.dump(apikeys)), 200
@api_bp.route('/pdnsadmin/apikeys/<int:apikey_id>', methods=['DELETE'])
@apiv1_bp.route('/pdnsadmin/apikeys/<int:apikey_id>', methods=['DELETE'])
@api_basic_auth
def api_delete_apikey(apikey_id):
apikey = ApiKey.query.get(apikey_id)
......@@ -383,7 +389,7 @@ def api_delete_apikey(apikey_id):
return '', 204
@api_bp.route('/pdnsadmin/apikeys/<int:apikey_id>', methods=['PUT'])
@apiv1_bp.route('/pdnsadmin/apikeys/<int:apikey_id>', methods=['PUT'])
@api_basic_auth
def api_update_apikey(apikey_id):
# if role different and user is allowed to change it, update
......@@ -469,7 +475,7 @@ def api_update_apikey(apikey_id):
return '', 204
@api_bp.route(
@apiv1_bp.route(
'/servers/<string:server_id>/zones/<string:zone_id>/<path:subpath>',
methods=['GET', 'POST', 'PUT', 'PATCH', 'DELETE'])
@apikey_auth
......@@ -479,7 +485,7 @@ def api_zone_subpath_forward(server_id, zone_id, subpath):
return resp.content, resp.status_code, resp.headers.items()
@api_bp.route('/servers/<string:server_id>/zones/<string:zone_id>',
@apiv1_bp.route('/servers/<string:server_id>/zones/<string:zone_id>',
methods=['GET', 'PUT', 'PATCH', 'DELETE'])
@apikey_auth
@apikey_can_access_domain
......@@ -507,7 +513,7 @@ def api_zone_forward(server_id, zone_id):
return resp.content, resp.status_code, resp.headers.items()
@api_bp.route('/servers', methods=['GET'])
@apiv1_bp.route('/servers', methods=['GET'])
@apikey_auth
@apikey_is_admin
def api_server_forward():
......@@ -515,7 +521,7 @@ def api_server_forward():
return resp.content, resp.status_code, resp.headers.items()
@api_bp.route('/servers/<path:subpath>', methods=['GET', 'PUT'])
@apiv1_bp.route('/servers/<path:subpath>', methods=['GET', 'PUT'])
@apikey_auth
@apikey_is_admin
def api_server_sub_forward(subpath):
......@@ -523,7 +529,7 @@ def api_server_sub_forward(subpath):
return resp.content, resp.status_code, resp.headers.items()
@api_bp.route('/servers/<string:server_id>/zones', methods=['POST'])
@apiv1_bp.route('/servers/<string:server_id>/zones', methods=['POST'])
@apikey_auth
def api_create_zone(server_id):
resp = helper.forward_request()
......@@ -550,7 +556,7 @@ def api_create_zone(server_id):
return resp.content, resp.status_code, resp.headers.items()
@api_bp.route('/servers/<string:server_id>/zones', methods=['GET'])
@apiv1_bp.route('/servers/<string:server_id>/zones', methods=['GET'])
@apikey_auth
def api_get_zones(server_id):
if server_id == 'pdnsadmin':
......@@ -565,7 +571,7 @@ def api_get_zones(server_id):
# The endpoint to snychronize Domains in background
@api_bp.route('/sync_domains', methods=['GET'])
@apiv1_bp.route('/sync_domains', methods=['GET'])
@apikey_auth
def sync_domains():
domain = Domain()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment