From c6ce9cc722868f5666b0f91f283c9a7d30f82fc4 Mon Sep 17 00:00:00 2001 From: Lena Garber Date: Tue, 25 Feb 2025 11:51:05 -0500 Subject: [PATCH 1/5] Drop addresses key from ACL if None --- linode_api4/objects/lke.py | 3 ++- test/unit/objects/lke_test.py | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/linode_api4/objects/lke.py b/linode_api4/objects/lke.py index e675eae8e..d201483c9 100644 --- a/linode_api4/objects/lke.py +++ b/linode_api4/objects/lke.py @@ -14,6 +14,7 @@ Region, Type, ) +from linode_api4.util import drop_null_keys class LKEType(Base): @@ -574,7 +575,7 @@ def control_plane_acl_update( result = self._client.put( f"{LKECluster.api_endpoint}/control_plane_acl", model=self, - data={"acl": acl}, + data={"acl": drop_null_keys(acl)}, ) acl = result.get("acl") diff --git a/test/unit/objects/lke_test.py b/test/unit/objects/lke_test.py index 1a39b69bc..79d15970e 100644 --- a/test/unit/objects/lke_test.py +++ b/test/unit/objects/lke_test.py @@ -498,3 +498,18 @@ def test_populate_with_mixed_types(self): assert self.pool.nodes[0].id == "node7" assert self.pool.nodes[1].id == "node8" assert self.pool.nodes[2].id == "node9" + + def test_cluster_update_null_addresses(self): + cluster = LKECluster(self.client, 18881) + + with self.mock_put("lke/clusters/18881/control_plane_acl") as m: + cluster.control_plane_acl_update( + LKEClusterControlPlaneACLOptions( + enabled=True, + addresses=None, + ) + ) + + # Addresses should not be included in the API request if it's null + # See: TPT-3489 + assert m.call_data == {"acl": {"enabled": True}} From 330bc367bece7bdc2d555a307e6feeaf3dd3e8d8 Mon Sep 17 00:00:00 2001 From: Lena Garber Date: Tue, 25 Feb 2025 12:15:19 -0500 Subject: [PATCH 2/5] Fix create --- linode_api4/groups/lke.py | 2 +- test/integration/models/lke/test_lke.py | 14 ++++++++++++++ test/unit/objects/lke_test.py | 8 ++++---- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/linode_api4/groups/lke.py b/linode_api4/groups/lke.py index d64d45536..4d13bb650 100644 --- a/linode_api4/groups/lke.py +++ b/linode_api4/groups/lke.py @@ -131,7 +131,7 @@ def cluster_create( result = self.client.post( "/lke/clusters", - data=_flatten_request_body_recursive(drop_null_keys(params)), + data=drop_null_keys(_flatten_request_body_recursive(params)), ) if "id" not in result: diff --git a/test/integration/models/lke/test_lke.py b/test/integration/models/lke/test_lke.py index 7355ca40b..5a5ef7cf4 100644 --- a/test/integration/models/lke/test_lke.py +++ b/test/integration/models/lke/test_lke.py @@ -298,6 +298,20 @@ def test_lke_cluster_acl(lke_cluster_with_acl): assert acl.addresses.ipv4 == ["10.0.0.2/32"] +def test_lke_cluster_update_acl_null_addresses(lke_cluster_with_acl): + cluster = lke_cluster_with_acl + + # Addresses should not be included in the request if it's null, + # else an error will be returned by the API. + # See: TPT-3489 + acl = cluster.control_plane_acl_update( + {"enabled": False, "addresses": None} + ) + + assert acl == cluster.control_plane_acl + assert acl.addresses.ipv4 == ["10.0.0.1/32"] + + def test_lke_cluster_disable_acl(lke_cluster_with_acl): cluster = lke_cluster_with_acl diff --git a/test/unit/objects/lke_test.py b/test/unit/objects/lke_test.py index 79d15970e..60db1ef60 100644 --- a/test/unit/objects/lke_test.py +++ b/test/unit/objects/lke_test.py @@ -504,10 +504,10 @@ def test_cluster_update_null_addresses(self): with self.mock_put("lke/clusters/18881/control_plane_acl") as m: cluster.control_plane_acl_update( - LKEClusterControlPlaneACLOptions( - enabled=True, - addresses=None, - ) + { + "enabled": True, + "addresses": None, + } ) # Addresses should not be included in the API request if it's null From dc71975ec0e81b11303e2dc818bd30fa09e2f499 Mon Sep 17 00:00:00 2001 From: Lena Garber Date: Tue, 25 Feb 2025 12:20:36 -0500 Subject: [PATCH 3/5] Add creation unit test --- test/unit/objects/lke_test.py | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/test/unit/objects/lke_test.py b/test/unit/objects/lke_test.py index 60db1ef60..1f397afac 100644 --- a/test/unit/objects/lke_test.py +++ b/test/unit/objects/lke_test.py @@ -499,7 +499,30 @@ def test_populate_with_mixed_types(self): assert self.pool.nodes[1].id == "node8" assert self.pool.nodes[2].id == "node9" - def test_cluster_update_null_addresses(self): + def test_cluster_create_acl_null_addresses(self): + with self.mock_post("lke/clusters") as m: + self.client.lke.cluster_create( + region="us-mia", + label="foobar", + kube_version="1.32", + node_pools=[self.client.lke.node_pool("g6-standard-1", 3)], + control_plane={ + "acl": { + "enabled": False, + "addresses": None, + } + }, + ) + + # Addresses should not be included in the API request if it's null + # See: TPT-3489 + assert m.call_data["control_plane"] == { + "acl": { + "enabled": False, + } + } + + def test_cluster_update_acl_null_addresses(self): cluster = LKECluster(self.client, 18881) with self.mock_put("lke/clusters/18881/control_plane_acl") as m: From cfc0252209b38324384fa2242e842b2d291ead36 Mon Sep 17 00:00:00 2001 From: Lena Garber Date: Thu, 27 Feb 2025 11:53:45 -0500 Subject: [PATCH 4/5] Account for LKE cluster ACL API change --- test/integration/models/lke/test_lke.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/integration/models/lke/test_lke.py b/test/integration/models/lke/test_lke.py index 5a5ef7cf4..8d8d0cf77 100644 --- a/test/integration/models/lke/test_lke.py +++ b/test/integration/models/lke/test_lke.py @@ -45,7 +45,7 @@ def lke_cluster(test_linode_client): cluster.delete() -@pytest.fixture(scope="session") +@pytest.fixture(scope="function") def lke_cluster_with_acl(test_linode_client): node_type = test_linode_client.linode.types()[1] # g6-standard-1 version = test_linode_client.lke.versions()[0] @@ -288,6 +288,7 @@ def test_lke_cluster_acl(lke_cluster_with_acl): acl = cluster.control_plane_acl_update( LKEClusterControlPlaneACLOptions( + enabled=True, addresses=LKEClusterControlPlaneACLAddressesOptions( ipv4=["10.0.0.2/32"] ) @@ -309,7 +310,7 @@ def test_lke_cluster_update_acl_null_addresses(lke_cluster_with_acl): ) assert acl == cluster.control_plane_acl - assert acl.addresses.ipv4 == ["10.0.0.1/32"] + assert acl.addresses.ipv4 == [] def test_lke_cluster_disable_acl(lke_cluster_with_acl): @@ -325,7 +326,7 @@ def test_lke_cluster_disable_acl(lke_cluster_with_acl): assert acl.enabled is False assert acl == cluster.control_plane_acl - assert acl.addresses.ipv4 == ["10.0.0.2/32"] + assert acl.addresses.ipv4 == [] cluster.control_plane_acl_delete() From 7fce67c8e0ac91908025346d0b4b9b8e3a020464 Mon Sep 17 00:00:00 2001 From: Lena Garber Date: Thu, 27 Feb 2025 13:06:24 -0500 Subject: [PATCH 5/5] make format --- test/integration/models/lke/test_lke.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration/models/lke/test_lke.py b/test/integration/models/lke/test_lke.py index 8d8d0cf77..794bc3203 100644 --- a/test/integration/models/lke/test_lke.py +++ b/test/integration/models/lke/test_lke.py @@ -291,7 +291,7 @@ def test_lke_cluster_acl(lke_cluster_with_acl): enabled=True, addresses=LKEClusterControlPlaneACLAddressesOptions( ipv4=["10.0.0.2/32"] - ) + ), ) )