From 70c17f1b1fd07a5cbd2e95d834eadd898a84909e Mon Sep 17 00:00:00 2001 From: Erika Rosaverde Date: Wed, 1 Nov 2023 10:15:36 +0000 Subject: [PATCH] flask server to test sunfishcore library Signed-off-by: Erika Rosaverde --- .gitignore | 6 ++ README.md | 33 ++++++++- Resources/Systems/1/index.json | 51 +++++++++++++ Resources/Systems/1234/index.json | 58 +++++++++++++++ Resources/Systems/index.json | 14 ++++ Resources/index.json | 10 +++ __init__.py | 0 app.py | 73 +++++++++++++++++++ requirements.txt | 1 + .../test_create_collection_exception.json | 14 ++++ templates/test_create_systems_1.json | 62 ++++++++++++++++ templates/test_create_systems_1234.json | 56 ++++++++++++++ templates/test_patch_systems_1234.json | 18 +++++ templates/test_put_system_1.json | 49 +++++++++++++ templates/test_update_exception.json | 18 +++++ 15 files changed, 461 insertions(+), 2 deletions(-) create mode 100644 .gitignore create mode 100644 Resources/Systems/1/index.json create mode 100644 Resources/Systems/1234/index.json create mode 100644 Resources/Systems/index.json create mode 100644 Resources/index.json create mode 100644 __init__.py create mode 100644 app.py create mode 100644 requirements.txt create mode 100644 templates/test_create_collection_exception.json create mode 100644 templates/test_create_systems_1.json create mode 100644 templates/test_create_systems_1234.json create mode 100644 templates/test_patch_systems_1234.json create mode 100644 templates/test_put_system_1.json create mode 100644 templates/test_update_exception.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dc75c8a --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.DS_Store +venv +.vscode +bin +include +lib \ No newline at end of file diff --git a/README.md b/README.md index a4753ea..172e9f1 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,31 @@ -# sunfish_server -Standalone RESTful API Server for Sunfish API +# Sunfish_flask_test +This application is a basic HTTP REST Server that can be used to test the sunfishcore library. + +## Requirements +The sunfishcore library have to be intalled. To create the .whl installable file of sunfishcore library, follow follow the instructions at: +https://github.com/erika-rosaverde/sunfish_library_reference/blob/main/README.md + + +# Sunfishcore library installation +Follow the following steps to install the sunfishcore library: +> pip3 install [path_to_sunfishcore_library]/dist/sunfishcore-0.1.0-py3-none-any.whl +> +> flask run + + +## Examples of requests: + curl -X GET localhost:5000/redfish/v1/ + + curl -X POST \ + -H "Content-Type: application/json" \ + -d @.json localhost:5000/redfish/v1/ + + curl -X PUT \ + -H "Content-Type: application/json" \ + -d @.json localhost:5000/redfish/v1/ + + curl -X PATCH \ + -H "Content-Type: application/json" \ + -d @.json localhost:5000/redfish/v1/ + + curl -X DELETE localhost:5000/redfish/v1/ diff --git a/Resources/Systems/1/index.json b/Resources/Systems/1/index.json new file mode 100644 index 0000000..6de836b --- /dev/null +++ b/Resources/Systems/1/index.json @@ -0,0 +1,51 @@ +{ + "@odata.id": "/redfish/v1/Systems/1", + "@odata.type": "#ComputerSystem.1.00.0.ComputerSystem", + "Boot": { + "BootSourceOverrideEnabled": "Once", + "BootSourceOverrideSupported": [ + "None", + "Pxe", + "Floppy", + "Cd", + "Usb" + ], + "BootSourceOverrideTarget": "Pxe", + "UefiTargetBootSourceOverride": "uefi device path" + }, + "FabricAdapters": [ + { + "@odata.id": "/redfish/1/Systems/1/FabricAdapters" + } + ], + "Id": "1", + "IndicatorLED": "On", + "Manufacturer": "Manufacturer Name", + "Memory": { + "Status": { + "Health": "OK", + "HealthRollUp": "OK", + "State": "Enabled" + }, + "TotalSystemMemoryGB": 8 + }, + "Model": "Model Name", + "Name": "Computer Node 1", + "Power": "Off", + "Processors": { + "Count": 4, + "Model": "Multi-Core Intel(R) Xeon(R) processor 7xxx Series", + "Status": { + "Health": "OK", + "HealthRollUp": "OK", + "State": "Enabled" + } + }, + "SKU": "", + "Status": { + "Health": "OK", + "HealthRollUp": "OK", + "State": "Enabled" + }, + "SystemType": "Physical" +} \ No newline at end of file diff --git a/Resources/Systems/1234/index.json b/Resources/Systems/1234/index.json new file mode 100644 index 0000000..9462219 --- /dev/null +++ b/Resources/Systems/1234/index.json @@ -0,0 +1,58 @@ +{ + "@odata.id": "/redfish/v1/Systems/1234", + "@odata.type": "#ComputerSystem.1.00.0.ComputerSystem", + "Boot": { + "BootSourceOverrideEnabled": "Once", + "BootSourceOverrideSupported": [ + "None", + "Pxe", + "Floppy", + "Cd", + "Usb" + ], + "BootSourceOverrideTarget": "Pxe", + "UefiTargetBootSourceOverride": "uefi device path" + }, + "FabricAdapters": [ + { + "@odata.id": "/redfish/1/Systems/1234/FabricAdapters" + } + ], + "Id": "1234", + "IndicatorLED": "Off", + "Links": { + "Chassis": [ + { + "@odata.id": "/redfish/v1/Chassis/1" + } + ] + }, + "Manufacturer": "Manufacturer Name", + "Memory": { + "Status": { + "Health": "OK", + "HealthRollUp": "OK", + "State": "Disabled" + }, + "TotalSystemMemoryGB": 12 + }, + "Model": "Model Name", + "Name": "Compute Node 1234", + "Power": "On", + "Processors": { + "Count": 4, + "Model": "Multi-Core Intel(R) Xeon(R) processor 7xxx Series", + "Status": { + "Health": "OK", + "HealthRollUp": "OK", + "State": "Enabled" + } + }, + "SKU": "sku", + "Status": { + "Health": "OK", + "HealthRollUp": "OK", + "State": "Enabled" + }, + "SystemType": "Physical" +} \ No newline at end of file diff --git a/Resources/Systems/index.json b/Resources/Systems/index.json new file mode 100644 index 0000000..546f00b --- /dev/null +++ b/Resources/Systems/index.json @@ -0,0 +1,14 @@ +{ + "@odata.id": "/redfish/v1/Systems", + "@odata.type": "#SystemsCollection.SystemsCollection", + "Members": [ + { + "@odata.id": "/redfish/v1/Systems/1" + }, + { + "@odata.id": "/redfish/v1/Systems/1234" + } + ], + "Members@odata.count": 2, + "Name": "Systems Collection" +} \ No newline at end of file diff --git a/Resources/index.json b/Resources/index.json new file mode 100644 index 0000000..00423f6 --- /dev/null +++ b/Resources/index.json @@ -0,0 +1,10 @@ +{ + "@odata.id": "/redfish/v1", + "@odata.type": "#ServiceRoot.v1_14_0.ServiceRoot", + "Id": "RootService", + "Name": "Root Service", + "RedfishVersion": "1.14.0", + "Systems": { + "@odata.id": "/redfish/v1/Systems" + } +} \ No newline at end of file diff --git a/__init__.py b/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app.py b/app.py new file mode 100644 index 0000000..866e9c1 --- /dev/null +++ b/app.py @@ -0,0 +1,73 @@ +# Copyright IBM Corp. 2023 +# This software is available to you under a BSD 3-Clause License. +# The full license terms are available here: https://github.com/OpenFabrics/sunfish_server_reference/blob/main/LICENSE + +from flask import Flask, request +from sunfishcorelib.core import Core +from sunfishcorelib.exceptions import * + +conf = { + "storage_backend": "FS", + "redfish_root": "/redfish/v1/", + "backend_conf" : { + "fs_root": "Resources" + } +} + +# initialize flask + +app = Flask(__name__) +sunfish_core = Core(conf) + +# Usa codici http +@app.route('/', methods=["GET"]) +def get(resource): + + try: + resp = sunfish_core.get_object(resource) + return resp, 200 + except ResourceNotFound as e: + return e.message, 404 + +@app.route('/', methods=["POST"]) +def post(resource): + try : + resp = sunfish_core.create_object(request.json) + return resp + except CollectionNotSupported as e: + return e.message, 405 # method not allowed + except AlreadyExists as e: + return e.message, 409 # Conflict + +@app.route('/', methods=["PUT"]) +def put(resource): + try: + data = request.json + resp = sunfish_core.replace_object(data) + return resp, 200 + except ResourceNotFound as e: + return e.message, 404 + +@app.route('/', methods=["PATCH"]) +def patch(resource): + try: + data = request.json + resp = sunfish_core.patch_object(data) + return resp, 200 + except ResourceNotFound as e: + return e.message, 404 + +@app.route('/', methods=["DELETE"]) +def delete(resource): + try: + resp = sunfish_core.delete_object(resource) + return resp, 200 + except ResourceNotFound as e: + return e.message, 404 + except ActionNotAllowed as e: + return e.message, 403 # forbidden + except InvalidPath as e: + return e.message, 400 + +# we run app debugging mode +app.run(debug=False) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8ab6294 --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +flask \ No newline at end of file diff --git a/templates/test_create_collection_exception.json b/templates/test_create_collection_exception.json new file mode 100644 index 0000000..52c3158 --- /dev/null +++ b/templates/test_create_collection_exception.json @@ -0,0 +1,14 @@ +{ + "@odata.id": "/Users/erika/Documents/private_sunfish_library_reference/sunfish_library_reference/Resources/Systems", + "@odata.type": "#SystemsCollection.SystemsCollection", + "Members": [ + { + "@odata.id": "/redfish/v1/Systems/1" + }, + { + "@odata.id": "/redfish/v1/Systems/1234" + } + ], + "Members@odata.count": 2, + "Name": "Systems Collection" +} \ No newline at end of file diff --git a/templates/test_create_systems_1.json b/templates/test_create_systems_1.json new file mode 100644 index 0000000..062900a --- /dev/null +++ b/templates/test_create_systems_1.json @@ -0,0 +1,62 @@ +{ + "@odata.id": "/redfish/v1/Systems/1", + "@odata.type": "#ComputerSystem.1.00.0.ComputerSystem", + "Id": "1", + "Name": "Compute Node 1", + "SystemType": "Physical", + "Manufacturer": "Manufacturer Name", + "Model": "Model Name", + "SKU": "", + "Status": { + "State": "Enabled", + "Health": "OK", + "HealthRollUp": "OK" + }, + "IndicatorLED": "Off", + "Power": "On", + "Boot": { + "BootSourceOverrideEnabled": "Once", + "BootSourceOverrideTarget": "Pxe", + "BootSourceOverrideSupported": [ + "None", + "Pxe", + "Floppy", + "Cd", + "Usb", + "Hdd", + "BiosSetup", + "Utilities", + "Diags", + "UefiTarget" + ], + "UefiTargetBootSourceOverride": "uefi device path" + }, + "Processors": { + "Count": 8, + "Model": "Multi-Core Intel(R) Xeon(R) processor 7xxx Series", + "Status": { + "State": "Enabled", + "Health": "OK", + "HealthRollUp": "OK" + } + }, + "Memory": { + "TotalSystemMemoryGB": 16, + "Status": { + "State": "Enabled", + "Health": "OK", + "HealthRollUp": "OK" + } + }, + "FabricAdapters": [ + { "@odata.id": "/redfish/1/Systems/1/FabricAdapters" } + ], + "Links": { + "Chassis": [ + { + "@odata.id": "/redfish/v1/Chassis/1" + } + ] + } + +} \ No newline at end of file diff --git a/templates/test_create_systems_1234.json b/templates/test_create_systems_1234.json new file mode 100644 index 0000000..0c25043 --- /dev/null +++ b/templates/test_create_systems_1234.json @@ -0,0 +1,56 @@ +{ + "@odata.id": "/redfish/v1/Systems/1234", + "@odata.type": "#ComputerSystem.1.00.0.ComputerSystem", + "Id": "1234", + "Name": "Compute Node 1234", + "SystemType": "Physical", + "Manufacturer": "Manufacturer Name", + "Model": "Model Name", + "SKU": "", + "Status": { + "State": "Enabled", + "Health": "OK", + "HealthRollUp": "OK" + }, + "IndicatorLED": "Off", + "Power": "On", + "Boot": { + "BootSourceOverrideEnabled": "Once", + "BootSourceOverrideTarget": "Pxe", + "BootSourceOverrideSupported": [ + "None", + "Pxe", + "Floppy", + "Cd", + "Usb" + ], + "UefiTargetBootSourceOverride": "uefi device path" + }, + "Processors": { + "Count": 4, + "Model": "Multi-Core Intel(R) Xeon(R) processor 7xxx Series", + "Status": { + "State": "Enabled", + "Health": "OK", + "HealthRollUp": "OK" + } + }, + "Memory": { + "TotalSystemMemoryGB": 8, + "Status": { + "State": "Enabled", + "Health": "OK", + "HealthRollUp": "OK" + } + }, + "FabricAdapters": [ + { "@odata.id": "/redfish/1/Systems/1234/FabricAdapters" } + ], + "Links": { + "Chassis": [ + { + "@odata.id": "/redfish/v1/Chassis/1" + } + ] + } +} \ No newline at end of file diff --git a/templates/test_patch_systems_1234.json b/templates/test_patch_systems_1234.json new file mode 100644 index 0000000..94609d5 --- /dev/null +++ b/templates/test_patch_systems_1234.json @@ -0,0 +1,18 @@ +{ + "@odata.id": "/redfish/v1/Systems/1234", + "@odata.type": "#ComputerSystem.1.00.0.ComputerSystem", + "Id": "1234", + "Name": "Compute Node 1234", + "SystemType": "Physical", + "Manufacturer": "Manufacturer Name", + "Model": "Model Name", + "SKU": "sku", + "Memory": { + "TotalSystemMemoryGB": 12, + "Status": { + "State": "Disabled", + "Health": "OK", + "HealthRollUp": "OK" + } + } + } diff --git a/templates/test_put_system_1.json b/templates/test_put_system_1.json new file mode 100644 index 0000000..6a893c9 --- /dev/null +++ b/templates/test_put_system_1.json @@ -0,0 +1,49 @@ +{ + "@odata.id": "/redfish/v1/Systems/1", + "@odata.type": "#ComputerSystem.1.00.0.ComputerSystem", + "Id": "1", + "Name": "Computer Node 1", + "SystemType": "Physical", + "Manufacturer": "Manufacturer Name", + "Model": "Model Name", + "SKU": "", + "Status": { + "State": "Enabled", + "Health": "OK", + "HealthRollUp": "OK" + }, + "IndicatorLED": "On", + "Power": "Off", + "Boot": { + "BootSourceOverrideEnabled": "Once", + "BootSourceOverrideTarget": "Pxe", + "BootSourceOverrideSupported": [ + "None", + "Pxe", + "Floppy", + "Cd", + "Usb" + ], + "UefiTargetBootSourceOverride": "uefi device path" + }, + "Processors": { + "Count": 4, + "Model": "Multi-Core Intel(R) Xeon(R) processor 7xxx Series", + "Status": { + "State": "Enabled", + "Health": "OK", + "HealthRollUp": "OK" + } + }, + "Memory": { + "TotalSystemMemoryGB": 8, + "Status": { + "State": "Enabled", + "Health": "OK", + "HealthRollUp": "OK" + } + }, + "FabricAdapters": [ + { "@odata.id": "/redfish/1/Systems/1/FabricAdapters" } + ] +} diff --git a/templates/test_update_exception.json b/templates/test_update_exception.json new file mode 100644 index 0000000..7cfd14e --- /dev/null +++ b/templates/test_update_exception.json @@ -0,0 +1,18 @@ +{ + "@odata.id": "/redfish/v1/Systems/-1", + "@odata.type": "#ComputerSystem.1.00.0.ComputerSystem", + "Id": "1234", + "Name": "Compute Node 1234", + "SystemType": "Physical", + "Manufacturer": "Manufacturer Name", + "Model": "Model Name", + "SKU": "", + "Memory": { + "TotalSystemMemoryGB": 12, + "Status": { + "State": "Disabled", + "Health": "OK", + "HealthRollUp": "OK" + } + } +} \ No newline at end of file