Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

network.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. from ..errors import InvalidVersion
  2. from ..utils import check_resource, minimum_version
  3. from ..utils import version_lt
  4. from .. import utils
  5. class NetworkApiMixin:
  6. def networks(self, names=None, ids=None, filters=None):
  7. """
  8. List networks. Similar to the ``docker network ls`` command.
  9. Args:
  10. names (:py:class:`list`): List of names to filter by
  11. ids (:py:class:`list`): List of ids to filter by
  12. filters (dict): Filters to be processed on the network list.
  13. Available filters:
  14. - ``driver=[<driver-name>]`` Matches a network's driver.
  15. - ``label=[<key>]``, ``label=[<key>=<value>]`` or a list of
  16. such.
  17. - ``type=["custom"|"builtin"]`` Filters networks by type.
  18. Returns:
  19. (dict): List of network objects.
  20. Raises:
  21. :py:class:`docker.errors.APIError`
  22. If the server returns an error.
  23. """
  24. if filters is None:
  25. filters = {}
  26. if names:
  27. filters['name'] = names
  28. if ids:
  29. filters['id'] = ids
  30. params = {'filters': utils.convert_filters(filters)}
  31. url = self._url("/networks")
  32. res = self._get(url, params=params)
  33. return self._result(res, json=True)
  34. def create_network(self, name, driver=None, options=None, ipam=None,
  35. check_duplicate=None, internal=False, labels=None,
  36. enable_ipv6=False, attachable=None, scope=None,
  37. ingress=None):
  38. """
  39. Create a network. Similar to the ``docker network create``.
  40. Args:
  41. name (str): Name of the network
  42. driver (str): Name of the driver used to create the network
  43. options (dict): Driver options as a key-value dictionary
  44. ipam (IPAMConfig): Optional custom IP scheme for the network.
  45. check_duplicate (bool): Request daemon to check for networks with
  46. same name. Default: ``None``.
  47. internal (bool): Restrict external access to the network. Default
  48. ``False``.
  49. labels (dict): Map of labels to set on the network. Default
  50. ``None``.
  51. enable_ipv6 (bool): Enable IPv6 on the network. Default ``False``.
  52. attachable (bool): If enabled, and the network is in the global
  53. scope, non-service containers on worker nodes will be able to
  54. connect to the network.
  55. scope (str): Specify the network's scope (``local``, ``global`` or
  56. ``swarm``)
  57. ingress (bool): If set, create an ingress network which provides
  58. the routing-mesh in swarm mode.
  59. Returns:
  60. (dict): The created network reference object
  61. Raises:
  62. :py:class:`docker.errors.APIError`
  63. If the server returns an error.
  64. Example:
  65. A network using the bridge driver:
  66. >>> client.api.create_network("network1", driver="bridge")
  67. You can also create more advanced networks with custom IPAM
  68. configurations. For example, setting the subnet to
  69. ``192.168.52.0/24`` and gateway address to ``192.168.52.254``.
  70. .. code-block:: python
  71. >>> ipam_pool = docker.types.IPAMPool(
  72. subnet='192.168.52.0/24',
  73. gateway='192.168.52.254'
  74. )
  75. >>> ipam_config = docker.types.IPAMConfig(
  76. pool_configs=[ipam_pool]
  77. )
  78. >>> client.api.create_network("network1", driver="bridge",
  79. ipam=ipam_config)
  80. """
  81. if options is not None and not isinstance(options, dict):
  82. raise TypeError('options must be a dictionary')
  83. data = {
  84. 'Name': name,
  85. 'Driver': driver,
  86. 'Options': options,
  87. 'IPAM': ipam,
  88. 'CheckDuplicate': check_duplicate,
  89. }
  90. if labels is not None:
  91. if version_lt(self._version, '1.23'):
  92. raise InvalidVersion(
  93. 'network labels were introduced in API 1.23'
  94. )
  95. if not isinstance(labels, dict):
  96. raise TypeError('labels must be a dictionary')
  97. data["Labels"] = labels
  98. if enable_ipv6:
  99. if version_lt(self._version, '1.23'):
  100. raise InvalidVersion(
  101. 'enable_ipv6 was introduced in API 1.23'
  102. )
  103. data['EnableIPv6'] = True
  104. if internal:
  105. if version_lt(self._version, '1.22'):
  106. raise InvalidVersion('Internal networks are not '
  107. 'supported in API version < 1.22')
  108. data['Internal'] = True
  109. if attachable is not None:
  110. if version_lt(self._version, '1.24'):
  111. raise InvalidVersion(
  112. 'attachable is not supported in API version < 1.24'
  113. )
  114. data['Attachable'] = attachable
  115. if ingress is not None:
  116. if version_lt(self._version, '1.29'):
  117. raise InvalidVersion(
  118. 'ingress is not supported in API version < 1.29'
  119. )
  120. data['Ingress'] = ingress
  121. if scope is not None:
  122. if version_lt(self._version, '1.30'):
  123. raise InvalidVersion(
  124. 'scope is not supported in API version < 1.30'
  125. )
  126. data['Scope'] = scope
  127. url = self._url("/networks/create")
  128. res = self._post_json(url, data=data)
  129. return self._result(res, json=True)
  130. @minimum_version('1.25')
  131. def prune_networks(self, filters=None):
  132. """
  133. Delete unused networks
  134. Args:
  135. filters (dict): Filters to process on the prune list.
  136. Returns:
  137. (dict): A dict containing a list of deleted network names and
  138. the amount of disk space reclaimed in bytes.
  139. Raises:
  140. :py:class:`docker.errors.APIError`
  141. If the server returns an error.
  142. """
  143. params = {}
  144. if filters:
  145. params['filters'] = utils.convert_filters(filters)
  146. url = self._url('/networks/prune')
  147. return self._result(self._post(url, params=params), True)
  148. @check_resource('net_id')
  149. def remove_network(self, net_id):
  150. """
  151. Remove a network. Similar to the ``docker network rm`` command.
  152. Args:
  153. net_id (str): The network's id
  154. """
  155. url = self._url("/networks/{0}", net_id)
  156. res = self._delete(url)
  157. self._raise_for_status(res)
  158. @check_resource('net_id')
  159. def inspect_network(self, net_id, verbose=None, scope=None):
  160. """
  161. Get detailed information about a network.
  162. Args:
  163. net_id (str): ID of network
  164. verbose (bool): Show the service details across the cluster in
  165. swarm mode.
  166. scope (str): Filter the network by scope (``swarm``, ``global``
  167. or ``local``).
  168. """
  169. params = {}
  170. if verbose is not None:
  171. if version_lt(self._version, '1.28'):
  172. raise InvalidVersion('verbose was introduced in API 1.28')
  173. params['verbose'] = verbose
  174. if scope is not None:
  175. if version_lt(self._version, '1.31'):
  176. raise InvalidVersion('scope was introduced in API 1.31')
  177. params['scope'] = scope
  178. url = self._url("/networks/{0}", net_id)
  179. res = self._get(url, params=params)
  180. return self._result(res, json=True)
  181. @check_resource('container')
  182. def connect_container_to_network(self, container, net_id,
  183. ipv4_address=None, ipv6_address=None,
  184. aliases=None, links=None,
  185. link_local_ips=None, driver_opt=None,
  186. mac_address=None):
  187. """
  188. Connect a container to a network.
  189. Args:
  190. container (str): container-id/name to be connected to the network
  191. net_id (str): network id
  192. aliases (:py:class:`list`): A list of aliases for this endpoint.
  193. Names in that list can be used within the network to reach the
  194. container. Defaults to ``None``.
  195. links (:py:class:`list`): A list of links for this endpoint.
  196. Containers declared in this list will be linked to this
  197. container. Defaults to ``None``.
  198. ipv4_address (str): The IP address of this container on the
  199. network, using the IPv4 protocol. Defaults to ``None``.
  200. ipv6_address (str): The IP address of this container on the
  201. network, using the IPv6 protocol. Defaults to ``None``.
  202. link_local_ips (:py:class:`list`): A list of link-local
  203. (IPv4/IPv6) addresses.
  204. mac_address (str): The MAC address of this container on the
  205. network. Defaults to ``None``.
  206. """
  207. data = {
  208. "Container": container,
  209. "EndpointConfig": self.create_endpoint_config(
  210. aliases=aliases, links=links, ipv4_address=ipv4_address,
  211. ipv6_address=ipv6_address, link_local_ips=link_local_ips,
  212. driver_opt=driver_opt,
  213. mac_address=mac_address
  214. ),
  215. }
  216. url = self._url("/networks/{0}/connect", net_id)
  217. res = self._post_json(url, data=data)
  218. self._raise_for_status(res)
  219. @check_resource('container')
  220. def disconnect_container_from_network(self, container, net_id,
  221. force=False):
  222. """
  223. Disconnect a container from a network.
  224. Args:
  225. container (str): container ID or name to be disconnected from the
  226. network
  227. net_id (str): network ID
  228. force (bool): Force the container to disconnect from a network.
  229. Default: ``False``
  230. """
  231. data = {"Container": container}
  232. if force:
  233. if version_lt(self._version, '1.22'):
  234. raise InvalidVersion(
  235. 'Forced disconnect was introduced in API 1.22'
  236. )
  237. data['Force'] = force
  238. url = self._url("/networks/{0}/disconnect", net_id)
  239. res = self._post_json(url, data=data)
  240. self._raise_for_status(res)