Skip to content

Commit

Permalink
* update the push implementation and signature allowing
Browse files Browse the repository at this point in the history
  users to pass only the changes that they want to apply to the Redfish object.
 * update the tests to adhere to the change in signatures.

Signed-off-by: MICHELE GAZZETTI <[email protected]>
  • Loading branch information
mgazz committed Feb 2, 2024
1 parent 998796a commit 2297caf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
14 changes: 8 additions & 6 deletions sunfish/lib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from sunfish.lib.object_handler import ObjectHandler
from sunfish.storage.backend_FS import BackendFS
from sunfish.lib.exceptions import CollectionNotSupported
from sunfish.lib.exceptions import CollectionNotSupported,PropertyNotFound
from sunfish.events.redfish_event_handler import RedfishEventHandler
from sunfish.events.redfish_subscription_handler import RedfishSubscriptionHandler

Expand Down Expand Up @@ -75,7 +75,7 @@ def create_object(self, path, payload:dict):
}
payload.update(to_add)


type = self.__check_type(payload)

if "Collection" in type:
Expand Down Expand Up @@ -115,7 +115,7 @@ def replace_object(self, payload):
# Call function from backend
return self.storage_backend.replace(payload)

def patch_object(self, payload):
def patch_object(self, path,payload):
"""Calls the correspondent patch function from the backend implementation.
Args:
Expand All @@ -124,8 +124,8 @@ def patch_object(self, payload):
Returns:
str|exception: return the updated resource or an exception in case of fault.
"""
## controlla odata.type
type = self.__check_type(payload)
obj = self.get_object(path)
type =obj["@odata.type"]
if "Collection" in type:
raise CollectionNotSupported()
elif type == "EventDestination":
Expand All @@ -135,7 +135,7 @@ def patch_object(self, payload):
return resp

# call function from backend
return self.storage_backend.patch(payload)
return self.storage_backend.patch(path,payload)

def delete_object(self, path):
"""Calls the correspondent remove function from the backend implementation. Checks that the path is valid.
Expand Down Expand Up @@ -166,6 +166,8 @@ def handle_event(self, payload):

def __check_type(self, payload):
## controlla odata.type
if "@odata.type" not in payload:
raise PropertyNotFound("@odata.type")
type = payload["@odata.type"]
type = type.split('.')[0]
return type.replace("#", "")
6 changes: 4 additions & 2 deletions sunfish/storage/backend_FS.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ def replace(self, payload:json):
except ResourceNotFound as e:
raise ResourceNotFound(e.resource_id)

def patch(self, payload:json):
def patch(self, path: str, payload:json):
object = self.read(path)
object.update(payload)
try:
return self._update_object(payload, False)
return self._update_object(object, False)
except ResourceNotFound as e:
raise ResourceNotFound(e.resource_id)

Expand Down
18 changes: 10 additions & 8 deletions tests/test_sunfishcore_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,21 @@ def test_put_exception(self):
# Patch
def test_patch(self):
id = test_utils.get_id(self.conf["backend_conf"]["fs_root"], 'Systems')
object_path = os.path.join(self.conf["redfish_root"], 'Systems', id)
object_to_update = self.core.get_object(object_path)

payload = tests_template.test_patch
id_properties = {
"@odata.id": os.path.join(self.conf["redfish_root"], 'Systems', id),
"Id": id
}
payload.update(id_properties)
assert self.core.patch_object(payload) == self.core.get_object(payload['@odata.id'])

self.core.patch_object(object_path, payload)

object_to_update.update(payload)

assert object_to_update == self.core.get_object(object_path)

# Exception patch element that doesnt exists
def test_patch_exception(self):
payload = tests_template.test_update_exception
with pytest.raises(ResourceNotFound):
self.core.patch_object(payload)
self.core.patch_object('/redfish/v1/Systems/-1', payload)

# EVENTING and SUBSCRIPTIONS
def test_subscription(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/tests_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
test_patch = {
"@odata.type": "#ComputerSystem.1.0.0.ComputerSystem",
"Status": {
"State": "Disabled",
"State": "Enabled",
"Health": "OK"
}
}
Expand Down

0 comments on commit 2297caf

Please sign in to comment.