Skip to content

Commit

Permalink
Improved agents management
Browse files Browse the repository at this point in the history
Various changes to utils.py and agents_management.py to improve the way how agents are linked with resources.

Signed-off-by: Christian Pinto <christian.pinto@ibm.com>
  • Loading branch information
christian-pinto committed Nov 1, 2023
1 parent a899dd1 commit 90c415c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 26 deletions.
23 changes: 17 additions & 6 deletions api_emulator/agents_management.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@
import api_emulator.utils as utils
import requests

request_headers = {
'Content-Type': 'application/json'
}

# This function checks if an object is managed by a Sunfish agent and returns
# the Agent @odata.id or None if the object is not managed by an Agent
# We are assuming that the resource_path we receive is always that of a collection because according to the RedFish
# specification (v1.18.0 at the time of writing this comment): "To create a resource, services shall support the POST
# method on resource collections."
def isAgentManaged(resource_path):
resource = utils.get_object(resource_path)
if "oem" in resource and "Sunfish_RM" in resource["oem"] and "ManagingAgent" in resource["oem"]["Sunfish_RM"]:
return resource["oem"]["Sunfish_RM"]["ManagingAgent"]["@odata.id"]
if "Oem" in resource and "Sunfish_RM" in resource["Oem"] and "ManagingAgent" in resource["Oem"]["Sunfish_RM"]:
return resource["Oem"]["Sunfish_RM"]["ManagingAgent"]["@odata.id"]

return None

Expand All @@ -27,12 +31,19 @@ def requestToAgent(method, agent_id, resource_path, payload):
if payload is None:
# we only check if there is a payload and we assume that is correct
raise Exception("Error: Missing payload")
headers = {
'Content-Type': 'application/json'
}
r = requests.post(resource_uri, headers=headers, data=json.dumps(payload))
r = requests.post(resource_uri, headers=request_headers, data=json.dumps(payload))
elif method == "PATCH":
if payload is None:
# we only check if there is a payload and we assume that is correct
raise Exception("Error: Missing payload")
r = requests.patch(resource_uri, headers=request_headers, data=json.dumps(payload))
elif method == "DELETE":
r = requests.delete(resource_uri)
elif method == "PUT":
if payload is None:
# we only check if there is a payload and we assume that is correct
raise Exception("Error: Missing payload")
r = requests.put(resource_uri, headers=request_headers, data=json.dumps(payload))
else:
raise Exception("Invalid method for requestToAgent")

Expand Down
58 changes: 38 additions & 20 deletions api_emulator/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ def get_json_data(path):
# return jsonify(data)
return data

def create_object (config, members, member_ids, path, agent = None):
def create_object (config, members, member_ids, path, agent=None):
# For POST Singleton API:

if agent is not None:
Expand All @@ -189,13 +189,13 @@ def create_object (config, members, member_ids, path, agent = None):
"@odata.id": agent
}
}
if "oem" not in config:
config["oem"] = {}
if "Sunfish_RM" not in config["oem"]:
config["oem"]["Sunfish_RM"] = oem
if "Oem" not in config:
config["Oem"] = {}
if "Sunfish_RM" not in config["Oem"]:
config["Oem"]["Sunfish_RM"] = oem

members.append(config)
member_ids.append(config['@odata.id'].split("/")[-1])
member_ids.append(config['@odata.id'])

# Create instances of subordinate resources, then call put operation
# not implemented yet
Expand Down Expand Up @@ -227,9 +227,10 @@ def create_and_patch_object (config, members, member_ids, path, collection_path,

def delete_object(path, base_path, members=None, member_ids=None):

delPath = path.replace(PATHS['Root'],'/redfish/v1/').replace("\\","/")
path2 = create_path(base_path, 'index.json').replace("\\","/")
object_id = path.split('/')[-1]
delPath = path.replace(PATHS['Root'],'/redfish/v1/').replace("//","/")
path2 = create_path(base_path, 'index.json').replace("//","/")
logging.debug(f"Object path: {path}")
object_id = delPath
logging.debug(f"path2: {path2}")
try:
with open(path2,"r") as pdata:
Expand All @@ -252,12 +253,15 @@ def delete_object(path, base_path, members=None, member_ids=None):
logging.debug(json.dumps(pdata, indent=2))
if members and member_ids:
object_index = member_ids.index(object_id)
logging.debug(f"Object index in members: {object_index}")
member_ids.pop(object_index)
members.pop(object_index)

with open(path2,"w") as jdata:
json.dump(pdata, jdata, indent=4, sort_keys=True)

logging.debug("Data dumped")

except Exception as e:
return {"error": "Unable to read file because of the following error::{}".format(e)}, 404

Expand Down Expand Up @@ -309,34 +313,48 @@ def patch_object(path):

# Write the updated json to file.
with open(path, 'w') as f:
json.dump(data, f)
json.dump(data, f, indent=4)
f.close()

except Exception as e:
return {"error": "Unable to read file because of the following error:{}".format(e)}, 404

return 200

def put_object(path):
def put_object(path, agent=None):
if not os.path.exists(path):
return {"error": "The requested object does not exist.:{}"}, 404
try:
config = request.json
if agent is not None:
# This means this object is being created in response to a new object being advertised by an agent
# We mark the boject as belonging to an agent through the oem field.
oem = {
"@odata.type": "#SunfishExtensions.v1_0_0.ResourceExtensions",
"ManagingAgent": {
"@odata.id": agent
}
}
if "Oem" not in config:
config["Oem"] = {}
if "Sunfish_RM" not in config["Oem"]:
config["Oem"]["Sunfish_RM"] = oem
# Read json from file.
# with open(path, 'r') as data_json:
# data = json.load(data_json)
# data_json.close()
data = {}
path = path.replace("\\","/")
# If input body data, then update properties
if request.data:
request_data = json.loads(request.data)
# Update the keys of payload in json file.
for key, value in request_data.items():
data[key] = value
# data = {}
# path = path.replace("\\","/")
# # If input body data, then update properties
# if request.data:
# request_data = json.loads(request.data)
# # Update the keys of payload in json file.
# for key, value in request_data.items():
# data[key] = value

# Write the updated json to file.
with open(path, 'w') as f:
json.dump(data, f)
json.dump(config, f, indent=4, sort_keys=True)
f.close()

except Exception as e:
Expand Down

0 comments on commit 90c415c

Please sign in to comment.