Skip to content

Commit

Permalink
api: add status parameter to services list (#3093)
Browse files Browse the repository at this point in the history
Signed-off-by: Lorin Bucher <lorin@lbtec.dev>
  • Loading branch information
lorinbucher committed Feb 16, 2023
1 parent e9d4ddf commit 7cd7458
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
10 changes: 9 additions & 1 deletion docker/api/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,16 @@ def remove_service(self, service):
return True

@utils.minimum_version('1.24')
def services(self, filters=None):
def services(self, filters=None, status=None):
"""
List services.
Args:
filters (dict): Filters to process on the nodes list. Valid
filters: ``id``, ``name`` , ``label`` and ``mode``.
Default: ``None``.
status (bool): Include the service task count of running and
desired tasks. Default: ``None``.
Returns:
A list of dictionaries containing data about each service.
Expand All @@ -281,6 +283,12 @@ def services(self, filters=None):
params = {
'filters': utils.convert_filters(filters) if filters else None
}
if status is not None:
if utils.version_lt(self._version, '1.41'):
raise errors.InvalidVersion(
'status is not supported in API version < 1.41'
)
params['status'] = status
url = self._url('/services')
return self._result(self._get(url, params=params), True)

Expand Down
2 changes: 2 additions & 0 deletions docker/models/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ def list(self, **kwargs):
filters (dict): Filters to process on the nodes list. Valid
filters: ``id``, ``name`` , ``label`` and ``mode``.
Default: ``None``.
status (bool): Include the service task count of running and
desired tasks. Default: ``None``.
Returns:
list of :py:class:`Service`: The services.
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/api_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ def test_list_services_filter_by_label(self):
assert len(test_services) == 1
assert test_services[0]['Spec']['Labels']['test_label'] == 'testing'

@requires_api_version('1.41')
def test_list_services_with_status(self):
test_services = self.client.services()
assert len(test_services) == 0
self.create_simple_service()
test_services = self.client.services(
filters={'name': 'dockerpytest_'}, status=False
)
assert 'ServiceStatus' not in test_services[0]
test_services = self.client.services(
filters={'name': 'dockerpytest_'}, status=True
)
assert 'ServiceStatus' in test_services[0]

def test_inspect_service_by_id(self):
svc_name, svc_id = self.create_simple_service()
svc_info = self.client.inspect_service(svc_id)
Expand Down

0 comments on commit 7cd7458

Please sign in to comment.