Commit 8473d6f4 authored by kaiyou's avatar kaiyou Committed by kaiyou
Browse files

Expose the root pdns api function

parent 8d4c8ff0
Pipeline #7919 passed with stage
in 5 minutes and 47 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)
......
......@@ -28,7 +28,8 @@ from ..decorators import (
import random
import string
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)
......@@ -89,22 +90,27 @@ def get_role_id(role_name, role_id=None):
return role_id
@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
......@@ -114,42 +120,42 @@ def handle_StructuredException(err):
return json.dumps(err.to_dict()), err.status_code
@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
......@@ -167,7 +173,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():
......@@ -220,7 +226,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']:
......@@ -232,7 +238,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):
......@@ -284,7 +290,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()
......@@ -349,8 +355,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 = []
......@@ -390,7 +396,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)
......@@ -427,7 +433,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
......@@ -513,8 +519,8 @@ def api_update_apikey(apikey_id):
return '', 204
@api_bp.route('/pdnsadmin/users', defaults={'username': None})
@api_bp.route('/pdnsadmin/users/<string:username>')
@apiv1_bp.route('/pdnsadmin/users', defaults={'username': None})
@apiv1_bp.route('/pdnsadmin/users/<string:username>')
@api_basic_auth
@api_role_can('list users', allow_self=True)
def api_list_users(username=None):
......@@ -528,7 +534,7 @@ def api_list_users(username=None):
return json.dumps(user_schema.dump(user_list)), 200
@api_bp.route('/pdnsadmin/users', methods=['POST'])
@apiv1_bp.route('/pdnsadmin/users', methods=['POST'])
@api_basic_auth
@api_role_can('create users', allow_self=True)
def api_create_user():
......@@ -601,7 +607,7 @@ def api_create_user():
return json.dumps(user_schema.dump([user])), 201
@api_bp.route('/pdnsadmin/users/<int:user_id>', methods=['PUT'])
@apiv1_bp.route('/pdnsadmin/users/<int:user_id>', methods=['PUT'])
@api_basic_auth
@api_role_can('update users', allow_self=True)
def api_update_user(user_id):
......@@ -670,7 +676,7 @@ def api_update_user(user_id):
return '', 204
@api_bp.route('/pdnsadmin/users/<int:user_id>', methods=['DELETE'])
@apiv1_bp.route('/pdnsadmin/users/<int:user_id>', methods=['DELETE'])
@api_basic_auth
@api_role_can('delete users')
def api_delete_user(user_id):
......@@ -702,8 +708,8 @@ def api_delete_user(user_id):
return '', 204
@api_bp.route('/pdnsadmin/accounts', defaults={'account_name': None})
@api_bp.route('/pdnsadmin/accounts/<string:account_name>')
@apiv1_bp.route('/pdnsadmin/accounts', defaults={'account_name': None})
@apiv1_bp.route('/pdnsadmin/accounts/<string:account_name>')
@api_basic_auth
@api_role_can('list accounts')
def api_list_accounts(account_name):
......@@ -721,7 +727,7 @@ def api_list_accounts(account_name):
return json.dumps(account_schema.dump(account_list)), 200
@api_bp.route('/pdnsadmin/accounts', methods=['POST'])
@apiv1_bp.route('/pdnsadmin/accounts', methods=['POST'])
@api_basic_auth
def api_create_account():
if current_user.role.name not in ['Administrator', 'Operator']:
......@@ -755,7 +761,7 @@ def api_create_account():
return json.dumps(account_schema.dump([account])), 201
@api_bp.route('/pdnsadmin/accounts/<int:account_id>', methods=['PUT'])
@apiv1_bp.route('/pdnsadmin/accounts/<int:account_id>', methods=['PUT'])
@api_basic_auth
@api_role_can('update accounts')
def api_update_account(account_id):
......@@ -795,7 +801,7 @@ def api_update_account(account_id):
return '', 204
@api_bp.route('/pdnsadmin/accounts/<int:account_id>', methods=['DELETE'])
@apiv1_bp.route('/pdnsadmin/accounts/<int:account_id>', methods=['DELETE'])
@api_basic_auth
@api_role_can('delete accounts')
def api_delete_account(account_id):
......@@ -816,7 +822,7 @@ def api_delete_account(account_id):
return '', 204
@api_bp.route('/pdnsadmin/accounts/users/<int:account_id>', methods=['GET'])
@apiv1_bp.route('/pdnsadmin/accounts/users/<int:account_id>', methods=['GET'])
@api_basic_auth
@api_role_can('list account users')
def api_list_account_users(account_id):
......@@ -828,7 +834,7 @@ def api_list_account_users(account_id):
return json.dumps(user_schema.dump(user_list)), 200
@api_bp.route(
@apiv1_bp.route(
'/pdnsadmin/accounts/users/<int:account_id>/<int:user_id>',
methods=['PUT'])
@api_basic_auth
......@@ -852,7 +858,7 @@ def api_add_account_user(account_id, user_id):
return '', 204
@api_bp.route(
@apiv1_bp.route(
'/pdnsadmin/accounts/users/<int:account_id>/<int:user_id>',
methods=['DELETE'])
@api_basic_auth
......@@ -882,7 +888,7 @@ def api_remove_account_user(account_id, user_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
......@@ -892,7 +898,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
......@@ -920,7 +926,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():
......@@ -928,7 +934,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):
......@@ -936,7 +942,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()
......@@ -963,7 +969,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':
......@@ -978,7 +984,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()
......
Supports Markdown
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