Skip to content

Commit baee4dc

Browse files
Drop the LKE ACL addresses field when its value is null (#514)
* Drop addresses key from ACL if None * Fix create * Add creation unit test * Account for LKE cluster ACL API change * make format
1 parent e538377 commit baee4dc

File tree

4 files changed

+59
-5
lines changed

4 files changed

+59
-5
lines changed

linode_api4/groups/lke.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def cluster_create(
131131

132132
result = self.client.post(
133133
"/lke/clusters",
134-
data=_flatten_request_body_recursive(drop_null_keys(params)),
134+
data=drop_null_keys(_flatten_request_body_recursive(params)),
135135
)
136136

137137
if "id" not in result:

linode_api4/objects/lke.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
Region,
1515
Type,
1616
)
17+
from linode_api4.util import drop_null_keys
1718

1819

1920
class LKEType(Base):
@@ -566,7 +567,7 @@ def control_plane_acl_update(
566567
result = self._client.put(
567568
f"{LKECluster.api_endpoint}/control_plane_acl",
568569
model=self,
569-
data={"acl": acl},
570+
data={"acl": drop_null_keys(acl)},
570571
)
571572

572573
acl = result.get("acl")

test/integration/models/lke/test_lke.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def lke_cluster(test_linode_client):
4545
cluster.delete()
4646

4747

48-
@pytest.fixture(scope="session")
48+
@pytest.fixture(scope="function")
4949
def lke_cluster_with_acl(test_linode_client):
5050
node_type = test_linode_client.linode.types()[1] # g6-standard-1
5151
version = test_linode_client.lke.versions()[0]
@@ -288,16 +288,31 @@ def test_lke_cluster_acl(lke_cluster_with_acl):
288288

289289
acl = cluster.control_plane_acl_update(
290290
LKEClusterControlPlaneACLOptions(
291+
enabled=True,
291292
addresses=LKEClusterControlPlaneACLAddressesOptions(
292293
ipv4=["10.0.0.2/32"]
293-
)
294+
),
294295
)
295296
)
296297

297298
assert acl == cluster.control_plane_acl
298299
assert acl.addresses.ipv4 == ["10.0.0.2/32"]
299300

300301

302+
def test_lke_cluster_update_acl_null_addresses(lke_cluster_with_acl):
303+
cluster = lke_cluster_with_acl
304+
305+
# Addresses should not be included in the request if it's null,
306+
# else an error will be returned by the API.
307+
# See: TPT-3489
308+
acl = cluster.control_plane_acl_update(
309+
{"enabled": False, "addresses": None}
310+
)
311+
312+
assert acl == cluster.control_plane_acl
313+
assert acl.addresses.ipv4 == []
314+
315+
301316
def test_lke_cluster_disable_acl(lke_cluster_with_acl):
302317
cluster = lke_cluster_with_acl
303318

@@ -311,7 +326,7 @@ def test_lke_cluster_disable_acl(lke_cluster_with_acl):
311326

312327
assert acl.enabled is False
313328
assert acl == cluster.control_plane_acl
314-
assert acl.addresses.ipv4 == ["10.0.0.2/32"]
329+
assert acl.addresses.ipv4 == []
315330

316331
cluster.control_plane_acl_delete()
317332

test/unit/objects/lke_test.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,41 @@ def test_populate_with_mixed_types(self):
498498
assert self.pool.nodes[0].id == "node7"
499499
assert self.pool.nodes[1].id == "node8"
500500
assert self.pool.nodes[2].id == "node9"
501+
502+
def test_cluster_create_acl_null_addresses(self):
503+
with self.mock_post("lke/clusters") as m:
504+
self.client.lke.cluster_create(
505+
region="us-mia",
506+
label="foobar",
507+
kube_version="1.32",
508+
node_pools=[self.client.lke.node_pool("g6-standard-1", 3)],
509+
control_plane={
510+
"acl": {
511+
"enabled": False,
512+
"addresses": None,
513+
}
514+
},
515+
)
516+
517+
# Addresses should not be included in the API request if it's null
518+
# See: TPT-3489
519+
assert m.call_data["control_plane"] == {
520+
"acl": {
521+
"enabled": False,
522+
}
523+
}
524+
525+
def test_cluster_update_acl_null_addresses(self):
526+
cluster = LKECluster(self.client, 18881)
527+
528+
with self.mock_put("lke/clusters/18881/control_plane_acl") as m:
529+
cluster.control_plane_acl_update(
530+
{
531+
"enabled": True,
532+
"addresses": None,
533+
}
534+
)
535+
536+
# Addresses should not be included in the API request if it's null
537+
# See: TPT-3489
538+
assert m.call_data == {"acl": {"enabled": True}}

0 commit comments

Comments
 (0)