o
    h_                     @   sX   d dl mZ d dlmZ ddlmZ ddlmZmZ G dd deZ	G dd	 d	eZ
d
S )   )	APIClient)version_gte   )	Container)Model
Collectionc                   @   s@   e Zd ZdZedd Zedd Zdd Zdd	 Zd
d Z	dS )Networkz
    A Docker network.
    c                 C   s   | j dS )z*
        The name of the network.
        Name)attrsgetself r   8/usr/lib/python3/dist-packages/docker/models/networks.pyname   s   zNetwork.namec                    s"    fdd j dpi  D S )z
        The containers that are connected to the network, as a list of
        :py:class:`~docker.models.containers.Container` objects.
        c                    s   g | ]	} j j|qS r   )client
containersr   ).0Zcidr   r   r   
<listcomp>   s    z&Network.containers.<locals>.<listcomp>Z
Containers)r
   r   keysr   r   r   r   r      s   
zNetwork.containersc                 O   0   t |tr|j}| jjj|| jg|R i |S )a  
        Connect a container to this network.

        Args:
            container (str): Container to connect to this network, as either
                an ID, name, or :py:class:`~docker.models.containers.Container`
                object.
            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 linkedto 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.
            driver_opt (dict): A dictionary of options to provide to the
                network driver. Defaults to ``None``.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        )
isinstancer   idr   apiZconnect_container_to_networkr   	containerargskwargsr   r   r   connect   s   
zNetwork.connectc                 O   r   )a  
        Disconnect a container from this network.

        Args:
            container (str): Container to disconnect from this network, as
                either an ID, name, or
                :py:class:`~docker.models.containers.Container` object.
            force (bool): Force the container to disconnect from a network.
                Default: ``False``

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        )r   r   r   r   r   Z!disconnect_container_from_networkr   r   r   r   
disconnect>   s   
zNetwork.disconnectc                 C   s   | j j| jS )z
        Remove this network.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        )r   r   Zremove_networkr   r   r   r   r   removeS   s   zNetwork.removeN)
__name__
__module____qualname____doc__propertyr   r   r   r   r    r   r   r   r   r      s    


!r   c                   @   s@   e Zd ZdZeZdd Zdd Zdd Zdd	d
Z	e
jje	_dS )NetworkCollectionz(
    Networks on the Docker server.
    c                 O   s*   | j jj|g|R i |}| |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:
            (:py:class:`Network`): The network that was created.

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

        Example:
            A network using the bridge driver:

                >>> client.networks.create("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.networks.create(
                    "network1",
                    driver="bridge",
                    ipam=ipam_config
                )

        ZId)r   r   Zcreate_networkr   )r   r   r   r   respr   r   r   created   s   8zNetworkCollection.createc                 O   s"   |  | jjj|g|R i |S )aU  
        Get a network by its ID.

        Args:
            network_id (str): The ID of the network.
            verbose (bool): Retrieve the service details across the cluster in
                swarm mode.
            scope (str): Filter the network by scope (``swarm``, ``global``
                or ``local``).

        Returns:
            (:py:class:`Network`) The network.

        Raises:
            :py:class:`docker.errors.NotFound`
                If the network does not exist.

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

        )prepare_modelr   r   Zinspect_network)r   Z
network_idr   r   r   r   r   r      s   zNetworkCollection.getc                    s\   | dd} jjj|i |} fdd|D }|r,t jjjdr,|D ]}|  q%|S )a  
        List networks. Similar to the ``docker networks 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` (str|list): format either ``"key"``, ``"key=value"``
                    or a list of such.
                - ``type=["custom"|"builtin"]`` Filters networks by type.
            greedy (bool): Fetch more details for each network individually.
                You might want this to get the containers attached to them.

        Returns:
            (list of :py:class:`Network`) The networks on the server.

        Raises:
            :py:class:`docker.errors.APIError`
                If the server returns an error.
        greedyFc                    s   g | ]}  |qS r   )r)   )r   itemr   r   r   r      s    z*NetworkCollection.list.<locals>.<listcomp>z1.28)popr   r   networksr   Z_versionreload)r   r   r   r*   r'   r-   Znetr   r   r   list   s   
zNetworkCollection.listNc                 C   s   | j jj|dS )N)filters)r   r   prune_networks)r   r0   r   r   r   prune   s   zNetworkCollection.prune)N)r!   r"   r#   r$   r   Zmodelr(   r   r/   r2   r   r1   r   r   r   r   r&   ^   s    ;
r&   N)r   r   Zutilsr   r   r   Zresourcer   r   r   r&   r   r   r   r   <module>   s    W