Merge pull request #2019 from mrfuxi/less-verbose-extends

Allow to extend service using shorthand notation. Closes #1989
This commit is contained in:
mnowster 2015-10-14 17:13:09 +01:00
commit 6b68a84b9b
5 changed files with 82 additions and 14 deletions

View file

@ -269,15 +269,19 @@ class ServiceLoader(object):
self.service_dict['environment'] = env
def validate_and_construct_extends(self):
extends = self.service_dict['extends']
if not isinstance(extends, dict):
extends = {'service': extends}
validate_extends_file_path(
self.service_name,
self.service_dict['extends'],
extends,
self.filename
)
self.extended_config_path = self.get_extended_config_path(
self.service_dict['extends']
extends
)
self.extended_service_name = self.service_dict['extends']['service']
self.extended_service_name = extends['service']
full_extended_config = pre_process_config(
load_yaml(self.extended_config_path)

View file

@ -58,14 +58,21 @@
},
"extends": {
"type": "object",
"oneOf": [
{
"type": "string"
},
{
"type": "object",
"properties": {
"service": {"type": "string"},
"file": {"type": "string"}
},
"required": ["service"],
"additionalProperties": false
"properties": {
"service": {"type": "string"},
"file": {"type": "string"}
},
"required": ["service"],
"additionalProperties": false
}
]
},
"extra_hosts": {"$ref": "#/definitions/list_or_dict"},

View file

@ -4,6 +4,7 @@ import os
import sys
from functools import wraps
import six
from docker.utils.ports import split_port
from jsonschema import Draft4Validator
from jsonschema import FormatChecker
@ -162,10 +163,25 @@ def process_errors(errors, service_name=None):
Inspecting the context value of a ValidationError gives us information about
which sub schema failed and which kind of error it is.
"""
required = [context for context in error.context if context.validator == 'required']
if required:
return required[0].message
additionalProperties = [context for context in error.context if context.validator == 'additionalProperties']
if additionalProperties:
invalid_config_key = _parse_key_from_error_msg(additionalProperties[0])
return "contains unsupported option: '{}'".format(invalid_config_key)
constraint = [context for context in error.context if len(context.path) > 0]
if constraint:
valid_types = _parse_valid_types_from_validator(constraint[0].validator_value)
msg = "contains {}, which is an invalid type, it should be {}".format(
invalid_config_key = "".join(
"'{}' ".format(fragment) for fragment in constraint[0].path
if isinstance(fragment, six.string_types)
)
msg = "{}contains {}, which is an invalid type, it should be {}".format(
invalid_config_key,
constraint[0].instance,
valid_types
)