Skip to content

new: Support deleting VLAN in networking group #500

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

Merged
merged 2 commits into from
Jan 30, 2025
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
27 changes: 27 additions & 0 deletions linode_api4/groups/networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,3 +367,30 @@ def transfer_prices(self, *filters):
return self.client._get_and_filter(
NetworkTransferPrice, *filters, endpoint="/network-transfer/prices"
)

def delete_vlan(self, vlan, region):
"""
This operation deletes a VLAN.
You can't delete a VLAN if it's still attached to a Linode.

API Documentation: https://techdocs.akamai.com/linode-api/reference/delete-vlan

:param vlan: The label of the VLAN to be deleted.
:type vlan: str or VLAN
:param region: The VLAN's region.
:type region: str or Region
"""
if isinstance(region, Region):
region = region.id

if isinstance(vlan, VLAN):
vlan = vlan.label
resp = self.client.delete(
"/networking/vlans/{}/{}".format(region, vlan),
model=self,
)

if "error" in resp:
return False

return True
19 changes: 19 additions & 0 deletions test/integration/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,22 @@ def pytest_configure(config):
"markers",
"smoke: mark test as part of smoke test suite",
)


@pytest.fixture(scope="session")
def linode_for_vlan_tests(test_linode_client, e2e_test_firewall):
client = test_linode_client
region = get_region(client, {"Linodes", "Vlans"}, site_type="core")
label = get_test_label(length=8)

linode_instance, password = client.linode.instance_create(
"g6-nanode-1",
region,
image="linode/debian12",
label=label,
firewall=e2e_test_firewall,
)

yield linode_instance

linode_instance.delete()
50 changes: 48 additions & 2 deletions test/integration/models/networking/test_networking.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
get_region,
get_token,
)
from test.integration.helpers import get_test_label
from test.integration.helpers import (
get_test_label,
retry_sending_request,
wait_for_condition,
)

import pytest

from linode_api4 import LinodeClient
from linode_api4 import Instance, LinodeClient
from linode_api4.objects import Config, ConfigInterfaceIPv4, Firewall, IPAddress
from linode_api4.objects.networking import NetworkTransferPrice, Price

Expand Down Expand Up @@ -163,3 +167,45 @@ def test_allocate_and_delete_ip(test_linode_client, create_linode):
is_deleted = ip.delete()

assert is_deleted is True


def get_status(linode: Instance, status: str):
return linode.status == status


def test_create_and_delete_vlan(test_linode_client, linode_for_vlan_tests):
linode = linode_for_vlan_tests

config: Config = linode.configs[0]

config.interfaces = []
config.save()

vlan_label = "testvlan"
interface = config.interface_create_vlan(
label=vlan_label, ipam_address="10.0.0.2/32"
)

config.invalidate()

assert interface.id == config.interfaces[0].id
assert interface.purpose == "vlan"
assert interface.label == vlan_label

# Remove the VLAN interface and reboot Linode
config.interfaces = []
config.save()

wait_for_condition(3, 100, get_status, linode, "running")

retry_sending_request(3, linode.reboot)

wait_for_condition(3, 100, get_status, linode, "rebooting")
assert linode.status == "rebooting"

# Delete the VLAN
is_deleted = test_linode_client.networking.delete_vlan(
vlan_label, linode.region
)

assert is_deleted is True
16 changes: 15 additions & 1 deletion test/unit/objects/networking_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from test.unit.base import ClientBaseCase

from linode_api4 import ExplicitNullValue, Instance
from linode_api4 import VLAN, ExplicitNullValue, Instance, Region
from linode_api4.objects import Firewall, IPAddress, IPv6Range


Expand Down Expand Up @@ -94,3 +94,17 @@ def test_delete_ip(self):
ip.delete()

self.assertEqual(m.call_url, "/linode/instances/123/ips/127.0.0.1")

def test_delete_vlan(self):
"""
Tests that deleting a VLAN creates the correct api request
"""
with self.mock_delete() as m:
self.client.networking.delete_vlan(
VLAN(self.client, "vlan-test"),
Region(self.client, "us-southeast"),
)

self.assertEqual(
m.call_url, "/networking/vlans/us-southeast/vlan-test"
)
Loading