o
    q_a                     @   s*   d dl mZ d dl mZ G dd dZdS )   )errors)utilsc                   @   sJ   e Zd ZdddZ		dddZdd Zeddd	d
ZdddZ	dS )VolumeApiMixinNc                 C   s6   d|rt |ndi}| d}| | j||ddS )a  
        List volumes currently registered by the docker daemon. Similar to the
        ``docker volume ls`` command.

        Args:
            filters (dict): Server-side list filtering options.

        Returns:
            (dict): Dictionary with list of volume objects as value of the
            ``Volumes`` key.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.

        Example:

            >>> client.api.volumes()
            {u'Volumes': [{u'Driver': u'local',
               u'Mountpoint': u'/var/lib/docker/volumes/foobar/_data',
               u'Name': u'foobar'},
              {u'Driver': u'local',
               u'Mountpoint': u'/var/lib/docker/volumes/baz/_data',
               u'Name': u'baz'}]}
        filtersNz/volumesparamsT)r   convert_filters_url_result_getselfr   r   url r   3/usr/lib/python3/dist-packages/docker/api/volume.pyvolumes   s   
zVolumeApiMixin.volumesc                 C   s   |  d}|durt|tstd|||d}|dur7td| jdk r*tdt|ts3td||d	< | 	| j
||d
dS )a  
        Create and register a named volume

        Args:
            name (str): Name of the volume
            driver (str): Name of the driver used to create the volume
            driver_opts (dict): Driver options as a key-value dictionary
            labels (dict): Labels to set on the volume

        Returns:
            (dict): The created volume reference object

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.

        Example:

            >>> volume = client.api.create_volume(name='foobar', driver='local',
                    driver_opts={'foo': 'bar', 'baz': 'false'},
                    labels={"key": "value"})
            >>> print(volume)
            {u'Driver': u'local',
             u'Labels': {u'key': u'value'},
             u'Mountpoint': u'/var/lib/docker/volumes/foobar/_data',
             u'Name': u'foobar',
             u'Scope': u'local'}

        z/volumes/createNz driver_opts must be a dictionary)NameZDriverZ
DriverOptsz1.23    z)volume labels were introduced in API 1.23zlabels must be a dictionaryZLabels)dataT)r	   
isinstancedict	TypeErrorr   Zcompare_version_versionr   InvalidVersionr
   Z
_post_json)r   nameZdriverZdriver_optslabelsr   r   r   r   r   create_volume'   s    

zVolumeApiMixin.create_volumec                 C   s   |  d|}| | |dS )a  
        Retrieve volume info by name.

        Args:
            name (str): volume name

        Returns:
            (dict): Volume information dictionary

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.

        Example:

            >>> client.api.inspect_volume('foobar')
            {u'Driver': u'local',
             u'Mountpoint': u'/var/lib/docker/volumes/foobar/_data',
             u'Name': u'foobar'}

        /volumes/{0}T)r	   r
   r   )r   r   r   r   r   r   inspect_volume[   s   zVolumeApiMixin.inspect_volume1.25c                 C   s6   i }|rt ||d< | d}| | j||ddS )a  
        Delete unused volumes

        Args:
            filters (dict): Filters to process on the prune list.

        Returns:
            (dict): A dict containing a list of deleted volume names and
                the amount of disk space reclaimed in bytes.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        r   z/volumes/pruner   T)r   r   r	   r
   Z_postr   r   r   r   prune_volumest   s
   
zVolumeApiMixin.prune_volumesFc                 C   sP   i }|rt | jdrtdd|i}| jd||d}| |}| | dS )az  
        Remove a volume. Similar to the ``docker volume rm`` command.

        Args:
            name (str): The volume's name
            force (bool): Force removal of volumes that were already removed
                out of band by the volume driver plugin.

        Raises:
            :py:class:`docker.errors.APIError`
                If volume failed to remove.
        r   z(force removal was introduced in API 1.25forcer   r   N)r   Z
version_ltr   r   r   r	   Z_deleteZ_raise_for_status)r   r   r!   r   r   Zrespr   r   r   remove_volume   s   
zVolumeApiMixin.remove_volume)N)NNNN)F)
__name__
__module____qualname__r   r   r   r   Zminimum_versionr    r"   r   r   r   r   r      s    
!
4r   N) r   r   r   r   r   r   r   <module>   s    