33from ..types import ServiceMode
44
55
6+ def _check_api_features (version , task_template , update_config ):
7+ if update_config is not None :
8+ if utils .version_lt (version , '1.25' ):
9+ if 'MaxFailureRatio' in update_config :
10+ raise errors .InvalidVersion (
11+ 'UpdateConfig.max_failure_ratio is not supported in'
12+ ' API version < 1.25'
13+ )
14+ if 'Monitor' in update_config :
15+ raise errors .InvalidVersion (
16+ 'UpdateConfig.monitor is not supported in'
17+ ' API version < 1.25'
18+ )
19+
20+ if task_template is not None :
21+ if 'ForceUpdate' in task_template and utils .version_lt (
22+ version , '1.25' ):
23+ raise errors .InvalidVersion (
24+ 'force_update is not supported in API version < 1.25'
25+ )
26+
27+ if task_template .get ('Placement' ):
28+ if utils .version_lt (version , '1.30' ):
29+ if task_template ['Placement' ].get ('Platforms' ):
30+ raise errors .InvalidVersion (
31+ 'Placement.platforms is not supported in'
32+ ' API version < 1.30'
33+ )
34+
35+ if utils .version_lt (version , '1.27' ):
36+ if task_template ['Placement' ].get ('Preferences' ):
37+ raise errors .InvalidVersion (
38+ 'Placement.preferences is not supported in'
39+ ' API version < 1.27'
40+ )
41+
42+
643class ServiceApiMixin (object ):
744 @utils .minimum_version ('1.24' )
845 def create_service (
@@ -43,6 +80,8 @@ def create_service(
4380 )
4481 endpoint_spec = endpoint_config
4582
83+ _check_api_features (self ._version , task_template , update_config )
84+
4685 url = self ._url ('/services/create' )
4786 headers = {}
4887 image = task_template .get ('ContainerSpec' , {}).get ('Image' , None )
@@ -67,17 +106,6 @@ def create_service(
67106 }
68107
69108 if update_config is not None :
70- if utils .version_lt (self ._version , '1.25' ):
71- if 'MaxFailureRatio' in update_config :
72- raise errors .InvalidVersion (
73- 'UpdateConfig.max_failure_ratio is not supported in'
74- ' API version < 1.25'
75- )
76- if 'Monitor' in update_config :
77- raise errors .InvalidVersion (
78- 'UpdateConfig.monitor is not supported in'
79- ' API version < 1.25'
80- )
81109 data ['UpdateConfig' ] = update_config
82110
83111 return self ._result (
@@ -282,6 +310,8 @@ def update_service(self, service, version, task_template=None, name=None,
282310 )
283311 endpoint_spec = endpoint_config
284312
313+ _check_api_features (self ._version , task_template , update_config )
314+
285315 url = self ._url ('/services/{0}/update' , service )
286316 data = {}
287317 headers = {}
@@ -294,12 +324,6 @@ def update_service(self, service, version, task_template=None, name=None,
294324 mode = ServiceMode (mode )
295325 data ['Mode' ] = mode
296326 if task_template is not None :
297- if 'ForceUpdate' in task_template and utils .version_lt (
298- self ._version , '1.25' ):
299- raise errors .InvalidVersion (
300- 'force_update is not supported in API version < 1.25'
301- )
302-
303327 image = task_template .get ('ContainerSpec' , {}).get ('Image' , None )
304328 if image is not None :
305329 registry , repo_name = auth .resolve_repository_name (image )
@@ -308,17 +332,6 @@ def update_service(self, service, version, task_template=None, name=None,
308332 headers ['X-Registry-Auth' ] = auth_header
309333 data ['TaskTemplate' ] = task_template
310334 if update_config is not None :
311- if utils .version_lt (self ._version , '1.25' ):
312- if 'MaxFailureRatio' in update_config :
313- raise errors .InvalidVersion (
314- 'UpdateConfig.max_failure_ratio is not supported in'
315- ' API version < 1.25'
316- )
317- if 'Monitor' in update_config :
318- raise errors .InvalidVersion (
319- 'UpdateConfig.monitor is not supported in'
320- ' API version < 1.25'
321- )
322335 data ['UpdateConfig' ] = update_config
323336
324337 if networks is not None :
0 commit comments