From 46eb23b4f3fb0bf03a06b64e2872703659b0c006 Mon Sep 17 00:00:00 2001 From: Joffrey F Date: Thu, 10 Sep 2015 17:22:57 -0700 Subject: [PATCH] Basic volume API implementation. Signed-off-by: Joffrey F --- docker/api/__init__.py | 1 + docker/api/volume.py | 48 ++++++++++++++++++++++++++++++++++++++++++ docker/client.py | 3 ++- 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 docker/api/volume.py diff --git a/docker/api/__init__.py b/docker/api/__init__.py index 836f07e3..3fb81203 100644 --- a/docker/api/__init__.py +++ b/docker/api/__init__.py @@ -4,3 +4,4 @@ from .container import ContainerApiMixin from .daemon import DaemonApiMixin from .exec_api import ExecApiMixin from .image import ImageApiMixin +from .volume import VolumeApiMixin \ No newline at end of file diff --git a/docker/api/volume.py b/docker/api/volume.py new file mode 100644 index 00000000..4e005b82 --- /dev/null +++ b/docker/api/volume.py @@ -0,0 +1,48 @@ +import functools + +from .. import errors +from ..utils import utils + + +def check_api_version(f): + @functools.wraps(f) + def wrapped(self, *args, **kwargs): + if utils.compare_version('1.21', self._version) < 0: + raise errors.InvalidVersion( + 'The volume API is not available for API version < 1.21' + ) + return f(self, *args, **kwargs) + return wrapped + + +class VolumeApiMixin(object): + @check_api_version + def volumes(self, filters=None): + params = { + 'filter': utils.convert_filters(filters) if filters else None + } + url = self._url('/volumes') + return self._result(self._get(url, params=params), True) + + @check_api_version + def create_volume(self, name, driver=None, driver_opts=None): + url = self._url('/volumes') + if not isinstance(driver_opts, dict): + raise TypeError('driver_opts must be a dictionary') + + data = { + 'Name': name, + 'Driver': driver, + 'DriverOpts': driver_opts, + } + return self._result(self._post(url, data=data), True) + + @check_api_version + def inspect_volume(self, name): + url = self._url('/volumes/{0}', name) + return self._result(self._get(url), True) + + @check_api_version + def remove_volume(self, name): + url = self._url('/volumes/{0}', name) + return self._result(self._delete(url), True) diff --git a/docker/client.py b/docker/client.py index 2eb859cf..58d0496f 100644 --- a/docker/client.py +++ b/docker/client.py @@ -38,7 +38,8 @@ class Client( api.ContainerApiMixin, api.DaemonApiMixin, api.ExecApiMixin, - api.ImageApiMixin): + api.ImageApiMixin, + api.VolumeApiMixin): def __init__(self, base_url=None, version=None, timeout=constants.DEFAULT_TIMEOUT_SECONDS, tls=False): super(Client, self).__init__()