import fnmatch
import re
from collections import namedtuple
+from functools import wraps
from typing import Optional, Dict, Any, List, Union
import six
raise ServiceSpecValidationError(e)
+def handle_type_error(method):
+ @wraps(method)
+ def inner(cls, *args, **kwargs):
+ try:
+ return method(cls, *args, **kwargs)
+ except (TypeError, AttributeError) as e:
+ error_msg = '{}: {}'.format(cls.__name__, e)
+ raise ServiceSpecValidationError(error_msg)
+ return inner
+
+
class HostPlacementSpec(namedtuple('HostPlacementSpec', ['hostname', 'network', 'name'])):
def __str__(self):
res = ''
return res
@classmethod
+ @handle_type_error
def from_json(cls, data):
return cls(**data)
return "PlacementSpec(%s)" % ', '.join(kv)
@classmethod
+ @handle_type_error
def from_json(cls, data):
c = data.copy()
hosts = c.get('hosts', [])
self.unmanaged = unmanaged
@classmethod
+ @handle_type_error
def from_json(cls, json_spec):
# type: (dict) -> Any
# Python 3:
service_type = json_spec.get('service_type', '')
_cls = cls._cls(service_type)
- return _cls._from_json_impl(json_spec) # type: ignore
+ c = json_spec.copy()
+ if 'status' in c:
+ del c['status'] # kludge to make us compatible to `ServiceDescription.to_json()`
+
+ return _cls._from_json_impl(c) # type: ignore
@classmethod
def _from_json_impl(cls, json_spec):