Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add status parameter to services list API #3093

Merged
merged 1 commit into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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