diff --git a/docker/types/services.py b/docker/types/services.py index 29498e9..1dec455 100644 --- a/docker/types/services.py +++ b/docker/types/services.py @@ -567,29 +567,39 @@ class ServiceMode(dict): replicas (int): Number of replicas. For replicated services only. """ def __init__(self, mode, replicas=None): - if mode not in ('replicated', 'global'): + if mode not in ('replicated', 'global', 'replicated-job', 'global-job'): raise errors.InvalidArgument( - 'mode must be either "replicated" or "global"' + 'mode must be either "replicated", "replicated-job", "global" or "global-job"' ) - if mode != 'replicated' and replicas is not None: + if mode not in ('replicated', 'replicated-job') and replicas is not None: raise errors.InvalidArgument( - 'replicas can only be used for replicated mode' + 'replicas can only be used for replicated or replicated-job mode' ) - self[mode] = {} - if replicas is not None: - self[mode]['Replicas'] = replicas + new_mode = self._convert_mode(mode) + self[new_mode] = {} - @property - def mode(self): - if 'global' in self: - return 'global' - return 'replicated' + if replicas is not None and mode == 'replicated': + self[new_mode]['Replicas'] = replicas + if replicas is not None and mode == 'replicated-job': + self[new_mode]['TotalCompletions'] = replicas + self[new_mode]['MaxConcurrent'] = 1 + + @staticmethod + def _convert_mode(original_mode): + if original_mode in ('global', 'replicated'): + return original_mode + + if original_mode == 'global-job': + return 'GlobalJob' + + if original_mode == 'replicated-job': + return 'ReplicatedJob' @property def replicas(self): - if self.mode != 'replicated': + if not set(self).intersection(('replicated', 'ReplicatedJob')): return None - return self['replicated'].get('Replicas') + return self['replicated'].get('Replicas') or self['ReplicatedJob'].get('TotalCompletions') class SecretReference(dict):