Skip to content

Commit

Permalink
update swagger autogen to include field types
Browse files Browse the repository at this point in the history
  • Loading branch information
rmb938 committed Jun 25, 2018
1 parent 88c90a9 commit 38657bc
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 21 deletions.
4 changes: 2 additions & 2 deletions deli/counter/http/mounts/root/routes/compute/v1/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from deli.kubernetes.resources.const import REGION_LABEL
from deli.kubernetes.resources.model import ResourceState
from deli.kubernetes.resources.project import Project
from deli.kubernetes.resources.v1alpha1.image.model import Image, ImageVisibility
from deli.kubernetes.resources.v1alpha1.image.model import Image
from deli.kubernetes.resources.v1alpha1.region.model import Region


Expand Down Expand Up @@ -87,7 +87,7 @@ def get(self, **_):
@cherrypy.tools.model_params(cls=ParamsListImage)
@cherrypy.tools.model_out_pagination(cls=ResponseImage)
@cherrypy.tools.enforce_permission(permission_name="images:list")
def list(self, region_name, visibility: ImageVisibility, limit: int, marker: uuid.UUID):
def list(self, region_name, limit: int, marker: uuid.UUID):
"""List images
---
get:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
from schematics.types import UUIDType, StringType, IntType

from deli.kubernetes.resources.model import ResourceState
from deli.kubernetes.resources.v1alpha1.image.model import Image, ImageVisibility
from deli.kubernetes.resources.v1alpha1.image.model import Image


class ParamsImage(Model):
image_name = StringType(required=True)


class ParamsListImage(Model):
visibility = EnumType(ImageVisibility, default=ImageVisibility.PRIVATE)
region_name = StringType()
limit = IntType(default=100, max_value=100, min_value=1)
marker = UUIDType()
Expand Down
4 changes: 2 additions & 2 deletions deli/counter/http/mounts/root/routes/iam/v1/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def set(self):
raise cherrypy.HTTPError(404, 'Unknown Group ' + email)

if kind == 'serviceAccount':
_, project, *__ = domain.split('.')
project = domain.split('.')[1]
if project != 'system':
raise cherrypy.HTTPError(400, 'Can only add system service accounts to a system policy.')

Expand Down Expand Up @@ -163,7 +163,7 @@ def set(self):
raise cherrypy.HTTPError(404, 'Unknown Group ' + email)

if kind == 'serviceAccount':
_, sa_project_name, *__ = domain.split('.')
sa_project_name = domain.split('.')[1]

if sa_project_name == 'system':
sa = SystemServiceAccount.get(user)
Expand Down
8 changes: 4 additions & 4 deletions deli/counter/http/mounts/root/routes/iam/v1/role.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def list(self, limit, marker):
description: List system roles
tags:
- iam
- image
- role
responses:
200:
description: List of system roles
Expand Down Expand Up @@ -181,7 +181,7 @@ def delete(self, **_):
description: Delete a system role
tags:
- iam
- image
- role
responses:
204:
description: Role deleted
Expand Down Expand Up @@ -243,7 +243,7 @@ def list(self, limit, marker):
description: List project roles
tags:
- iam
- image
- role
responses:
200:
description: List of project roles
Expand Down Expand Up @@ -282,7 +282,7 @@ def delete(self, **_):
description: Delete a project role
tags:
- iam
- image
- role
responses:
204:
description: Role deleted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def validate_member(self, value, context=None):
if not self.EMAIL_REGEX.match(email):
raise ValidationError(self.MESSAGES['email'])

_, domain = email.split('@')
domain = email.split('@')[1]

if kind == 'user':
if email.endswith('sandwich.local'):
Expand Down
72 changes: 62 additions & 10 deletions deli/counter/http/spec/plugins/docstring.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from apispec import Path
from apispec.utils import load_operations_from_docstring
from ingredients_http.schematics.types import KubeName, ArrowType, IPv4AddressType, IPv4NetworkType, EnumType, \
KubeString
from schematics.models import FieldDescriptor
from schematics.types import IntType, StringType, BooleanType, UUIDType, EmailType, ListType, DictType, ModelType

from deli.counter.http.mounts.root.routes.iam.v1.validation_models.policy import BindingMemberType
from deli.counter.http.mounts.root.routes.iam.v1.validation_models.projects import ProjectName
from deli.counter.http.router import SandwichProjectRouter


Expand All @@ -20,7 +25,7 @@ def docstring_path_helper(spec, path, router, func, **kwargs):

if 'tools.model_in.cls' in cp_config:
model_cls = cp_config['tools.model_in.cls']
spec.definition(model_cls.__name__, **parse_model(model_cls))
spec.definition(model_cls.__name__, **parse_model(spec, model_cls))

data['requestBody']['required'] = True
data['requestBody']['content'] = {
Expand All @@ -43,14 +48,12 @@ def docstring_path_helper(spec, path, router, func, **kwargs):
'name': key,
'in': inn,
'required': model_cls._fields[key].required,
'schema': {
'type': 'string'
}
'schema': parse_model_type(spec, model_cls._fields[key])
})

if 'tools.model_out.cls' in cp_config:
model_cls = cp_config['tools.model_out.cls']
spec.definition(model_cls.__name__, **parse_model(model_cls))
spec.definition(model_cls.__name__, **parse_model(spec, model_cls))
data['responses'][200]['content'] = {
'application/json': {
'schema': {'$ref': '#/components/schemas/' + model_cls.__name__}
Expand All @@ -59,7 +62,7 @@ def docstring_path_helper(spec, path, router, func, **kwargs):

if 'tools.model_out_pagination.cls' in cp_config:
model_cls = cp_config['tools.model_out_pagination.cls']
spec.definition(model_cls.__name__, **parse_model(model_cls))
spec.definition(model_cls.__name__, **parse_model(spec, model_cls))
data['responses'][200]['content'] = {
'application/json': {
'schema': {
Expand Down Expand Up @@ -90,7 +93,7 @@ def setup(spec):
spec.register_path_helper(docstring_path_helper)


def parse_model(model_cls):
def parse_model(spec, model_cls):
kwargs = {
'properties': {},
'extra_fields': {
Expand All @@ -100,13 +103,62 @@ def parse_model(model_cls):
}
for key, obj in model_cls.__dict__.items():
if isinstance(obj, FieldDescriptor):
kwargs['properties'][key] = {
"type": "string"
}
kwargs['properties'][key] = parse_model_type(spec, model_cls._fields[key])
if model_cls._fields[key].required:
kwargs['extra_fields']['required'].append(key)

if len(kwargs['extra_fields']['required']) == 0:
del kwargs['extra_fields']['required']

return kwargs


def parse_model_type(spec, model_type):
swagger_types = {
StringType: 'string',
KubeName: 'string',
KubeString: 'string',
ProjectName: 'string',
UUIDType: 'string',
EmailType: 'string',
EnumType: 'string',
IPv4AddressType: 'string',
IPv4NetworkType: 'string',
ArrowType: 'string',
BindingMemberType: 'string',
IntType: 'integer',
BooleanType: 'boolean',
ListType: 'array',
DictType: 'object',
ModelType: 'object',
}

data = {
# Find the swagger type, if not found default to string
# It would be nice to have complex types like uuid, emails, ect...
# But swagger doesn't support it
"type": swagger_types.get(model_type.__class__, "string")
}

if model_type.__class__ == EnumType:
data['enum'] = [x.value for x in model_type.enum_class]

if model_type.__class__ == ListType:
if model_type.field.__class__ == ModelType:
spec.definition(model_type.field.model_class.__name__, **parse_model(spec, model_type.field.model_class))
data['items'] = {
'$ref': '#/components/schemas/' + model_type.field.model_class.__name__
}
else:
data['items'] = parse_model_type(spec, model_type.field)

if model_type.__class__ == DictType:
data['additionalProperties'] = parse_model_type(spec, model_type.field)

if model_type.__class__ == ModelType:
spec.definition(model_type.model_class.__name__, **parse_model(spec, model_type.model_class))
data['additionalProperties'] = {
'$ref': '#/components/schemas/' + model_type.model_class.__name__
}

return data

0 comments on commit 38657bc

Please sign in to comment.