Skip to content

Commit 10ad316

Browse files
authored
new: Allow defining interfaces on Instance creation (#372)
* enable interfaces * simplify code
1 parent 0bbd8cc commit 10ad316

3 files changed

Lines changed: 71 additions & 1 deletion

File tree

linode_api4/groups/linode.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ def instance_create(
265265
:type metadata: dict
266266
:param firewall: The firewall to attach this Linode to.
267267
:type firewall: int or Firewall
268+
:param interfaces: An array of Network Interfaces to add to this Linode’s Configuration Profile.
269+
At least one and up to three Interface objects can exist in this array.
270+
:type interfaces: list[ConfigInterface] or list[dict[str, Any]]
268271
269272
:returns: A new Instance object, or a tuple containing the new Instance and
270273
the generated password.
@@ -303,6 +306,12 @@ def instance_create(
303306
fw = kwargs.pop("firewall")
304307
kwargs["firewall_id"] = fw.id if isinstance(fw, Firewall) else fw
305308

309+
if "interfaces" in kwargs:
310+
kwargs["interfaces"] = [
311+
i._serialize() if isinstance(i, ConfigInterface) else i
312+
for i in kwargs["interfaces"]
313+
]
314+
306315
params = {
307316
"type": ltype.id if issubclass(type(ltype), Base) else ltype,
308317
"region": region.id if issubclass(type(region), Base) else region,

test/integration/linode_client/test_linode_client.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import pytest
66

77
from linode_api4 import ApiError, LinodeClient
8-
from linode_api4.objects import ObjectStorageKeys
8+
from linode_api4.objects import ConfigInterface, ObjectStorageKeys
99

1010

1111
@pytest.fixture(scope="session", autouse=True)
@@ -231,6 +231,38 @@ def test_create_linode_instance_with_image(setup_client_and_linode):
231231
assert re.search("linode/debian10", str(linode.image))
232232

233233

234+
def test_create_linode_with_interfaces(test_linode_client):
235+
client = test_linode_client
236+
available_regions = client.regions()
237+
chosen_region = available_regions[4]
238+
label = get_test_label()
239+
240+
linode_instance, password = client.linode.instance_create(
241+
"g6-nanode-1",
242+
chosen_region,
243+
label=label,
244+
image="linode/debian10",
245+
interfaces=[
246+
{"purpose": "public"},
247+
ConfigInterface(
248+
purpose="vlan", label="cool-vlan", ipam_address="10.0.0.4/32"
249+
),
250+
],
251+
)
252+
253+
assert len(linode_instance.configs[0].interfaces) == 2
254+
assert linode_instance.configs[0].interfaces[0].purpose == "public"
255+
assert linode_instance.configs[0].interfaces[1].purpose == "vlan"
256+
assert linode_instance.configs[0].interfaces[1].label == "cool-vlan"
257+
assert (
258+
linode_instance.configs[0].interfaces[1].ipam_address == "10.0.0.4/32"
259+
)
260+
261+
res = linode_instance.delete()
262+
263+
assert res
264+
265+
234266
# LongviewGroupTests
235267
def test_get_longview_clients(test_linode_client, test_longview_client):
236268
client = test_linode_client

test/unit/objects/linode_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,35 @@ def test_instance_create_with_user_data(self):
421421
},
422422
)
423423

424+
def test_instance_create_with_interfaces(self):
425+
"""
426+
Tests that user can pass a list of interfaces on Linode create.
427+
"""
428+
interfaces = [
429+
{"purpose": "public"},
430+
ConfigInterface(
431+
purpose="vlan", label="cool-vlan", ipam_address="10.0.0.4/32"
432+
),
433+
]
434+
with self.mock_post("linode/instances/123") as m:
435+
self.client.linode.instance_create(
436+
"us-southeast",
437+
"g6-nanode-1",
438+
interfaces=interfaces,
439+
)
440+
441+
self.assertEqual(
442+
m.call_data["interfaces"],
443+
[
444+
{"purpose": "public"},
445+
{
446+
"purpose": "vlan",
447+
"label": "cool-vlan",
448+
"ipam_address": "10.0.0.4/32",
449+
},
450+
],
451+
)
452+
424453
def test_build_instance_metadata(self):
425454
"""
426455
Tests that the metadata field is built correctly.

0 commit comments

Comments
 (0)