o
    q_a(                     @   sF   d dl mZ d dlmZmZ d dlmZ d dlmZ G dd dZdS )   )InvalidVersion)check_resourceminimum_version)
version_lt)utilsc                   @   s   e Zd ZdddZ				dddZedddd	Zed
dd Zed
dddZ	ed			dddZ
ed	dddZdS )NetworkApiMixinNc                 C   sX   |du ri }|r||d< |r||d< dt |i}| d}| j||d}| j|ddS )	a  
        List networks. Similar to the ``docker network ls`` command.

        Args:
            names (:py:class:`list`): List of names to filter by
            ids (:py:class:`list`): List of ids to filter by
            filters (dict): Filters to be processed on the network list.
                Available filters:
                - ``driver=[<driver-name>]`` Matches a network's driver.
                - ``label=[<key>]``, ``label=[<key>=<value>]`` or a list of
                    such.
                - ``type=["custom"|"builtin"]`` Filters networks by type.

        Returns:
            (dict): List of network objects.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        Nnameidfiltersz	/networksparamsTZjson)r   convert_filters_url_get_result)selfnamesZidsr
   r   urlres r   4/usr/lib/python3/dist-packages/docker/api/network.pynetworks   s   
zNetworkApiMixin.networksFc                 C   s2  |durt |tstd|||||d}|dur0t| jdr#tdt |ts,td||d< |r@t| jdr<tdd	|d
< |rPt| jdrLtdd	|d< |	durbt| jdr^td|	|d< |durtt| jdrptd||d< |
durt| jdrtd|
|d< | d}| j||d}| j|d	dS )a  
        Create a network. Similar to the ``docker network create``.

        Args:
            name (str): Name of the network
            driver (str): Name of the driver used to create the network
            options (dict): Driver options as a key-value dictionary
            ipam (IPAMConfig): Optional custom IP scheme for the network.
            check_duplicate (bool): Request daemon to check for networks with
                same name. Default: ``None``.
            internal (bool): Restrict external access to the network. Default
                ``False``.
            labels (dict): Map of labels to set on the network. Default
                ``None``.
            enable_ipv6 (bool): Enable IPv6 on the network. Default ``False``.
            attachable (bool): If enabled, and the network is in the global
                scope,  non-service containers on worker nodes will be able to
                connect to the network.
            scope (str): Specify the network's scope (``local``, ``global`` or
                ``swarm``)
            ingress (bool): If set, create an ingress network which provides
                the routing-mesh in swarm mode.

        Returns:
            (dict): The created network reference object

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

        Example:
            A network using the bridge driver:

                >>> client.api.create_network("network1", driver="bridge")

            You can also create more advanced networks with custom IPAM
            configurations. For example, setting the subnet to
            ``192.168.52.0/24`` and gateway address to ``192.168.52.254``.

            .. code-block:: python

                >>> ipam_pool = docker.types.IPAMPool(
                    subnet='192.168.52.0/24',
                    gateway='192.168.52.254'
                )
                >>> ipam_config = docker.types.IPAMConfig(
                    pool_configs=[ipam_pool]
                )
                >>> client.api.create_network("network1", driver="bridge",
                                                 ipam=ipam_config)
        Nzoptions must be a dictionary)NameZDriverZOptionsZIPAMZCheckDuplicatez1.23z*network labels were introduced in API 1.23zlabels must be a dictionaryZLabelsz&enable_ipv6 was introduced in API 1.23TZ
EnableIPv61.22z9Internal networks are not supported in API version < 1.22ZInternalz1.24z1attachable is not supported in API version < 1.24Z
Attachablez1.29z.ingress is not supported in API version < 1.29ZIngressz1.30z,scope is not supported in API version < 1.30ZScopez/networks/createdatar   )	
isinstancedict	TypeErrorr   _versionr   r   
_post_jsonr   )r   r   ZdriveroptionsZipamZcheck_duplicateZinternallabelsZenable_ipv6Z
attachablescopeZingressr   r   r   r   r   r   create_network)   s^   7

zNetworkApiMixin.create_networkz1.25c                 C   s6   i }|rt ||d< | d}| | j||ddS )a  
        Delete unused networks

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

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

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        r
   z/networks/pruner   T)r   r   r   r   Z_post)r   r
   r   r   r   r   r   prune_networks   s
   
zNetworkApiMixin.prune_networksnet_idc                 C   s$   |  d|}| |}| | dS )z
        Remove a network. Similar to the ``docker network rm`` command.

        Args:
            net_id (str): The network's id
        /networks/{0}N)r   Z_delete_raise_for_status)r   r'   r   r   r   r   r   remove_network   s   
zNetworkApiMixin.remove_networkc                 C   st   i }|durt | jdrtd||d< |dur&t | jdr"td||d< | d|}| j||d	}| j|d
dS )aD  
        Get detailed information about a network.

        Args:
            net_id (str): ID of network
            verbose (bool): Show the service details across the cluster in
                swarm mode.
            scope (str): Filter the network by scope (``swarm``, ``global``
                or ``local``).
        Nz1.28z"verbose was introduced in API 1.28verbosez1.31z scope was introduced in API 1.31r$   r(   r   Tr   )r   r    r   r   r   r   )r   r'   r+   r$   r   r   r   r   r   r   inspect_network   s   zNetworkApiMixin.inspect_network	containerc	              	   C   sD   || j ||||||dd}	| d|}
| j|
|	d}| | dS )a  
        Connect a container to a network.

        Args:
            container (str): container-id/name to be connected to the network
            net_id (str): network id
            aliases (:py:class:`list`): A list of aliases for this endpoint.
                Names in that list can be used within the network to reach the
                container. Defaults to ``None``.
            links (:py:class:`list`): A list of links for this endpoint.
                Containers declared in this list will be linked to this
                container. Defaults to ``None``.
            ipv4_address (str): The IP address of this container on the
                network, using the IPv4 protocol. Defaults to ``None``.
            ipv6_address (str): The IP address of this container on the
                network, using the IPv6 protocol. Defaults to ``None``.
            link_local_ips (:py:class:`list`): A list of link-local
                (IPv4/IPv6) addresses.
        )aliaseslinksipv4_addressipv6_addresslink_local_ips
driver_opt)	ContainerZEndpointConfigz/networks/{0}/connectr   N)Zcreate_endpoint_configr   r!   r)   )r   r-   r'   r0   r1   r.   r/   r2   r3   r   r   r   r   r   r   connect_container_to_network   s   	z,NetworkApiMixin.connect_container_to_networkc                 C   sP   d|i}|rt | jdrtd||d< | d|}| j||d}| | dS )aA  
        Disconnect a container from a network.

        Args:
            container (str): container ID or name to be disconnected from the
                network
            net_id (str): network ID
            force (bool): Force the container to disconnect from a network.
                Default: ``False``
        r4   r   z,Forced disconnect was introduced in API 1.22ZForcez/networks/{0}/disconnectr   N)r   r    r   r   r!   r)   )r   r-   r'   Zforcer   r   r   r   r   r   !disconnect_container_from_network   s   z1NetworkApiMixin.disconnect_container_from_network)NNN)
NNNNFNFNNN)N)NN)NNNNNN)F)__name__
__module____qualname__r   r%   r   r&   r   r*   r,   r5   r6   r   r   r   r   r      s*    
!
r
$r   N)errorsr   r   r   r   r    r   r   r   r   r   <module>   s
    