Skip to content

Commit

Permalink
flask server to test sunfishcore library
Browse files Browse the repository at this point in the history
Signed-off-by: Erika Rosaverde <erika.rosaverde@ibm.com>
  • Loading branch information
erika-rosaverde committed Nov 1, 2023
1 parent 738128a commit 70c17f1
Show file tree
Hide file tree
Showing 15 changed files with 461 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
venv
.vscode
bin
include
lib
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
> <source flask-test/bin/activate]>
> flask run

## Examples of requests:
curl -X GET localhost:5000/redfish/v1/<resource path>

curl -X POST \
-H "Content-Type: application/json" \
-d @<filename>.json localhost:5000/redfish/v1/<resource path>

curl -X PUT \
-H "Content-Type: application/json" \
-d @<filename>.json localhost:5000/redfish/v1/<resource path>

curl -X PATCH \
-H "Content-Type: application/json" \
-d @<filename>.json localhost:5000/redfish/v1/<resource path>

curl -X DELETE localhost:5000/redfish/v1/<resource path>
51 changes: 51 additions & 0 deletions Resources/Systems/1/index.json
Original file line number Diff line number Diff line change
@@ -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"
}
58 changes: 58 additions & 0 deletions Resources/Systems/1234/index.json
Original file line number Diff line number Diff line change
@@ -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"
}
14 changes: 14 additions & 0 deletions Resources/Systems/index.json
Original file line number Diff line number Diff line change
@@ -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"
}
10 changes: 10 additions & 0 deletions Resources/index.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
Empty file added __init__.py
Empty file.
73 changes: 73 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -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('/<path:resource>', 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('/<path:resource>', 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('/<path:resource>', 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('/<path:resource>', 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('/<path:resource>', 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)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
flask
14 changes: 14 additions & 0 deletions templates/test_create_collection_exception.json
Original file line number Diff line number Diff line change
@@ -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"
}
62 changes: 62 additions & 0 deletions templates/test_create_systems_1.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}

}
56 changes: 56 additions & 0 deletions templates/test_create_systems_1234.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
Loading

0 comments on commit 70c17f1

Please sign in to comment.