Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Connect with mac address #2481

Merged
merged 2 commits into from
Aug 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions docker/api/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def inspect_network(self, net_id, verbose=None, scope=None):
def connect_container_to_network(self, container, net_id,
ipv4_address=None, ipv6_address=None,
aliases=None, links=None,
link_local_ips=None):
link_local_ips=None, mac_address=None):
"""
Connect a container to a network.

Expand All @@ -235,12 +235,15 @@ def connect_container_to_network(self, container, net_id,
network, using the IPv6 protocol. Defaults to ``None``.
link_local_ips (:py:class:`list`): A list of link-local
(IPv4/IPv6) addresses.
mac_address (str): The MAC address of this container on the
network. Defaults to ``None``.
"""
data = {
"Container": container,
"EndpointConfig": self.create_endpoint_config(
aliases=aliases, links=links, ipv4_address=ipv4_address,
ipv6_address=ipv6_address, link_local_ips=link_local_ips
ipv6_address=ipv6_address, link_local_ips=link_local_ips,
mac_address=mac_address
),
}

Expand Down
9 changes: 8 additions & 1 deletion docker/types/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class EndpointConfig(dict):
def __init__(self, version, aliases=None, links=None, ipv4_address=None,
ipv6_address=None, link_local_ips=None):
ipv6_address=None, link_local_ips=None, mac_address=None):
if version_lt(version, '1.22'):
raise errors.InvalidVersion(
'Endpoint config is not supported for API version < 1.22'
Expand All @@ -23,6 +23,13 @@ def __init__(self, version, aliases=None, links=None, ipv4_address=None,
if ipv6_address:
ipam_config['IPv6Address'] = ipv6_address

if mac_address:
if version_lt(version, '1.25'):
raise errors.InvalidVersion(
'mac_address is not supported for API version < 1.25'
)
self['MacAddress'] = mac_address

if link_local_ips is not None:
if version_lt(version, '1.24'):
raise errors.InvalidVersion(
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/api_network_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,22 @@ def test_connect_with_ipv6_address(self):
net_data = container_data['NetworkSettings']['Networks'][net_name]
assert net_data['IPAMConfig']['IPv6Address'] == '2001:389::f00d'

@requires_api_version('1.25')
def test_connect_with_mac_address(self):
net_name, net_id = self.create_network()

container = self.client.create_container(TEST_IMG, 'top')
self.tmp_containers.append(container)

self.client.connect_container_to_network(
container, net_name, mac_address='02:42:ac:11:00:02'
)

container_data = self.client.inspect_container(container)

net_data = container_data['NetworkSettings']['Networks'][net_name]
assert net_data['MacAddress'] == '02:42:ac:11:00:02'

@requires_api_version('1.23')
def test_create_internal_networks(self):
_, net_id = self.create_network(internal=True)
Expand Down