8000 XML-RPC API: Fix complex objects are return value by SchoolGuy · Pull Request #3172 · cobbler/cobbler · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

XML-RPC API: Fix complex objects are return value #3172

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
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

8000 Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cobbler/items/distro.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class Distro(item.Item):
A Cobbler distribution object
"""

# Constants
TYPE_NAME = "distro"
COLLECTION_TYPE = "distro"

def __init__(self, api, *args, **kwargs):
Expand Down
2 changes: 2 additions & 0 deletions cobbler/items/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,8 @@ class System(Item):
A Cobbler system object.
"""

# Constants
TYPE_NAME = "system"
COLLECTION_TYPE = "system"

def __init__(self, api, *args, **kwargs):
Expand Down
34 changes: 33 additions & 1 deletion cobbler/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from typing import Dict, List, Optional, Union
from xmlrpc.server import SimpleXMLRPCRequestHandler

from cobbler import enums
from cobbler import autoinstall_manager
from cobbler import configgen
from cobbler.items import (
Expand Down Expand Up @@ -790,7 +791,38 @@ def get_item_resolved_value(self, item_uuid: str, attribute: str):
.. seealso:: Logically identical to :func:`~cobbler.api.CobblerAPI.get_item_resolved_value`
"""
self._log("get_item_resolved_value(%s)" % item_uuid, attribute=attribute)
return self.api.get_item_resolved_value(item_uuid, attribute)
return_value = self.api.get_item_resolved_value(item_uuid, attribute)
if return_value is None:
self._log(
"get_item_resolved_value(%s): returned None" % item_uuid,
attribute=attribute,
)
raise ValueError(
'None is not a valid value for the resolved attribute "%s". Please fix the item(s) '
'starting at uuid "%s"' % (attribute, item_uuid)
)
elif isinstance(return_value, enums.ConvertableEnum):
return return_value.value
elif isinstance(
return_value,
(enums.DHCP, enums.NetworkInterfaceType, enums.BaudRates, item.Item),
):
return return_value.name
elif isinstance(return_value, dict):
return self.xmlrpc_hacks(return_value)

if not isinstance(
return_value, (str, int, float, bool, tuple, bytes, bytearray, dict, list)
):
self._log(
"get_item_resolved_value(%s): Cannot return XML-RPC compliant type. Please add a case to convert"
' type "%s" to an XML-RPC compliant type!'
% (item_uuid, type(return_value))
)
raise ValueError(
"Cannot return XML-RPC compliant type. See logs for more information!"
)
return return_value

def set_item_resolved_value(
self, item_uuid: str, attribute: str, value, token=None
Expand Down
39 changes: 33 additions & 6 deletions tests/xmlrpcapi/non_object_calls_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import time
import re

from tests.conftest import does_not_raise

TEST_POWER_MANAGEMENT = True
TEST_SYSTEM = ""

Expand Down Expand Up @@ -190,8 +192,26 @@ def test_get_random_mac(remote, token):
assert match_obj


@pytest.mark.parametrize(
"input_attribute,checked_object,expected_result,expected_exception",
[
("kernel_options", "system", {"a": "1", "b": "2", "d": "~"}, does_not_raise()),
("arch", "distro", "x86_64", does_not_raise()),
("distro", "profile", "testdistro_item_resolved_value", does_not_raise()),
("profile", "system", "testprofile_item_resolved_value", does_not_raise()),
],
)
def test_get_item_resolved_value(
remote, token, create_distro, create_profile, create_system, create_kernel_initrd
remote,
token,
create_distro,
create_profile,
create_system,
create_kernel_initrd,
input_attribute,
checked_object,
expected_result,
expected_exception,
):
# Arrange
fk_kernel = "vmlinuz1"
Expand All @@ -207,11 +227,18 @@ def test_get_item_resolved_value(
create_profile(name_profile, name_distro, "a=1 b=2 c=3 c=4 c=5 d e")
test_system_handle = create_system(name_system, name_profile)
remote.modify_system(test_system_handle, "kernel_options", "!c !e", token=token)
test_system = remote.get_system(name_system, token=token)
expected_result = {"a": "1", "b": "2", "d": None}
if checked_object == "distro":
test_item = remote.get_distro(name_distro, token=token)
elif checked_object == "profile":
test_item = remote.get_profile(name_profile, token=token)
elif checked_object == "system":
test_item = remote.get_system(name_system, token=token)
else:
raise ValueError("checked_object has wrong value")

# Act
result = remote.get_item_resolved_value(test_system.get("uid"), "kernel_options")
with expected_exception:
result = remote.get_item_resolved_value(test_item.get("uid"), input_attribute)

# Assert
assert expected_result == result
# Assert
assert expected_result == result
0