From 5e9f1437ad40b924faa585a3088cf6a146f5c154 Mon Sep 17 00:00:00 2001 From: kevgliss Date: Tue, 10 May 2016 13:16:33 -0700 Subject: [PATCH] Marsmallowing sources (#310) --- lemur/sources/schemas.py | 32 ++++++ lemur/sources/service.py | 11 +-- lemur/sources/views.py | 45 +++------ lemur/static/app/index.html | 4 +- lemur/tests/test_sources.py | 190 ++++++++++++++++-------------------- 5 files changed, 131 insertions(+), 151 deletions(-) diff --git a/lemur/sources/schemas.py b/lemur/sources/schemas.py index e69de29b..9309bccb 100644 --- a/lemur/sources/schemas.py +++ b/lemur/sources/schemas.py @@ -0,0 +1,32 @@ +""" +.. module: lemur.sources.schemas + :platform: unix + :copyright: (c) 2015 by Netflix Inc., see AUTHORS for more + :license: Apache, see LICENSE for more details. +.. moduleauthor:: Kevin Glisson +""" +from marshmallow import fields + +from lemur.schemas import PluginSchema +from lemur.common.schema import LemurInputSchema, LemurOutputSchema + + +class SourceInputSchema(LemurInputSchema): + label = fields.String(required=True) + options = fields.Dict(load_from='sourceOptions', required=True) + description = fields.String() + plugin = fields.Nested(PluginSchema) + active = fields.Boolean() + + +class SourceOutputSchema(LemurOutputSchema): + label = fields.String() + options = fields.Dict(dump_to='sourceOptions') + description = fields.String() + plugin = fields.Nested(PluginSchema) + fields.Boolean() + + +source_input_schema = SourceInputSchema() +sources_output_schema = SourceOutputSchema(many=True) +source_output_schema = SourceOutputSchema() diff --git a/lemur/sources/service.py b/lemur/sources/service.py index d108f0ae..49fda560 100644 --- a/lemur/sources/service.py +++ b/lemur/sources/service.py @@ -174,10 +174,6 @@ def get_all(): def render(args): - sort_by = args.pop('sort_by') - sort_dir = args.pop('sort_dir') - page = args.pop('page') - count = args.pop('count') filt = args.pop('filter') certificate_id = args.pop('certificate_id', None) @@ -191,9 +187,4 @@ def render(args): terms = filt.split(';') query = database.filter(query, Source, terms) - query = database.find_all(query, Source, args) - - if sort_by and sort_dir: - query = database.sort(query, Source, sort_by, sort_dir) - - return database.paginate(query, page, count) + return database.sort_and_page(query, Source, args) diff --git a/lemur/sources/views.py b/lemur/sources/views.py index 6a94bf2f..6cdcef72 100644 --- a/lemur/sources/views.py +++ b/lemur/sources/views.py @@ -7,35 +7,28 @@ .. moduleauthor:: Kevin Glisson """ from flask import Blueprint -from flask.ext.restful import Api, reqparse, fields +from flask.ext.restful import Api, reqparse from lemur.sources import service +from lemur.common.schema import validate_schema +from lemur.sources.schemas import source_input_schema, source_output_schema, sources_output_schema + from lemur.auth.service import AuthenticatedResource from lemur.auth.permissions import admin_permission -from lemur.common.utils import paginated_parser, marshal_items +from lemur.common.utils import paginated_parser mod = Blueprint('sources', __name__) api = Api(mod) -FIELDS = { - 'description': fields.String, - 'sourceOptions': fields.Raw(attribute='options'), - 'pluginName': fields.String(attribute='plugin_name'), - 'lastRun': fields.DateTime(attribute='last_run', dt_format='iso8061'), - 'label': fields.String, - 'id': fields.Integer, -} - - class SourcesList(AuthenticatedResource): """ Defines the 'sources' endpoint """ def __init__(self): self.reqparse = reqparse.RequestParser() super(SourcesList, self).__init__() - @marshal_items(FIELDS) + @validate_schema(None, sources_output_schema) def get(self): """ .. http:get:: /sources @@ -94,8 +87,8 @@ class SourcesList(AuthenticatedResource): return service.render(args) @admin_permission.require(http_exception=403) - @marshal_items(FIELDS) - def post(self): + @validate_schema(source_input_schema, source_output_schema) + def post(self, data=None): """ .. http:post:: /sources @@ -158,12 +151,7 @@ class SourcesList(AuthenticatedResource): :reqheader Authorization: OAuth token to authenticate :statuscode 200: no error """ - self.reqparse.add_argument('label', type=str, location='json', required=True) - self.reqparse.add_argument('plugin', type=dict, location='json', required=True) - self.reqparse.add_argument('description', type=str, location='json') - - args = self.reqparse.parse_args() - return service.create(args['label'], args['plugin']['slug'], args['plugin']['pluginOptions'], args['description']) + return service.create(data['label'], data['plugin']['slug'], data['plugin']['pluginOptions'], data['description']) class Sources(AuthenticatedResource): @@ -171,7 +159,7 @@ class Sources(AuthenticatedResource): self.reqparse = reqparse.RequestParser() super(Sources, self).__init__() - @marshal_items(FIELDS) + @validate_schema(None, source_output_schema) def get(self, source_id): """ .. http:get:: /sources/1 @@ -218,8 +206,8 @@ class Sources(AuthenticatedResource): return service.get(source_id) @admin_permission.require(http_exception=403) - @marshal_items(FIELDS) - def put(self, source_id): + @validate_schema(source_input_schema, source_output_schema) + def put(self, source_id, data=None): """ .. http:put:: /sources/1 @@ -283,12 +271,7 @@ class Sources(AuthenticatedResource): :reqheader Authorization: OAuth token to authenticate :statuscode 200: no error """ - self.reqparse.add_argument('label', type=str, location='json', required=True) - self.reqparse.add_argument('plugin', type=dict, location='json', required=True) - self.reqparse.add_argument('description', type=str, location='json') - - args = self.reqparse.parse_args() - return service.update(source_id, args['label'], args['plugin']['pluginOptions'], args['description']) + return service.update(source_id, data['label'], data['plugin']['pluginOptions'], data['description']) @admin_permission.require(http_exception=403) def delete(self, source_id): @@ -301,7 +284,7 @@ class CertificateSources(AuthenticatedResource): def __init__(self): super(CertificateSources, self).__init__() - @marshal_items(FIELDS) + @validate_schema(None, sources_output_schema) def get(self, certificate_id): """ .. http:get:: /certificates/1/sources diff --git a/lemur/static/app/index.html b/lemur/static/app/index.html index 34f6ce79..de089c6a 100644 --- a/lemur/static/app/index.html +++ b/lemur/static/app/index.html @@ -56,8 +56,8 @@
  • Destinations
  • Sources
  • -