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/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/integration/models/lke/test_lke.py b/test/integration/models/lke/test_lke.py index 7355ca40b..794bc3203 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,9 +288,10 @@ 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"] - ) + ), ) ) @@ -298,6 +299,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 == [] + + def test_lke_cluster_disable_acl(lke_cluster_with_acl): cluster = lke_cluster_with_acl @@ -311,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() diff --git a/test/unit/objects/lke_test.py b/test/unit/objects/lke_test.py index 1a39b69bc..1f397afac 100644 --- a/test/unit/objects/lke_test.py +++ b/test/unit/objects/lke_test.py @@ -498,3 +498,41 @@ 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_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: + cluster.control_plane_acl_update( + { + "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}}