Skip to content

Commit 9391b2e

Browse files
authored
Merge pull request #104 from rahlvers/master
Add support for $metadata returns (in text/xml) and /redfish/v1/odata
2 parents 60dc913 + 7168ba2 commit 9391b2e

1 file changed

Lines changed: 70 additions & 2 deletions

File tree

emulator.py

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
import xml.etree.ElementTree as ET
1515
import logging
1616
import copy
17-
logging.basicConfig(level=logging.DEBUG)
1817

18+
logging.basicConfig(level=logging.DEBUG)
1919

2020
import g
2121

@@ -32,7 +32,6 @@
3232

3333
from infragen.populate import populate
3434

35-
3635
# Trays to load into the resource manager
3736
TRAYS = None
3837
SPEC = None
@@ -125,6 +124,12 @@ def error_response(msg, status, jsonify=False):
125124
class PathError(Exception):
126125
pass
127126

127+
@g.api.representation('application/xml')
128+
def output_xml(data, code, headers=None):
129+
resp = make_response(data, code)
130+
resp.headers.extend(headers or {})
131+
resp.headers['Content-Type'] = 'text/xml; charset=ISO-8859-1'
132+
return resp
128133

129134
@g.api.representation('application/json')
130135
def output_json(data, code, headers=None):
@@ -135,6 +140,8 @@ def output_json(data, code, headers=None):
135140
resp.headers.extend(headers or {})
136141
return resp
137142

143+
144+
138145
# The following code provides a mechanism for the Redfish client to either
139146
# - Emulator Service Root
140147
# - Control the emulator
@@ -345,6 +352,67 @@ def serviceInfo():
345352
def browse():
346353
return render_template('browse.html')
347354

355+
# Return metadata as type text/xml
356+
@g.app.route('/redfish/v1/$metadata')
357+
def get_metadata():
358+
logging.info ('In get_metadata')
359+
try:
360+
361+
md_xml = ""
362+
363+
if os.path.exists('Resources/$metadata/index.xml'):
364+
# Use dynamic data source
365+
filename = 'Resources/$metadata/index.xml'
366+
else:
367+
# Use static mockup
368+
mockup_path = MOCKUPFOLDERS[0]
369+
filename = os.path.join("api_emulator", mockup_path, 'static', '$metadata', 'index.xml')
370+
371+
with open(filename, 'r') as var:
372+
for line in var:
373+
line = line.rstrip()
374+
md_xml += line
375+
376+
resp = make_response(md_xml, 200)
377+
resp.headers['Content-Type'] = 'text/xml'
378+
return resp
379+
380+
except Exception:
381+
traceback.print_exc()
382+
resp = error_response('Internal Server Error', 500, True)
383+
return resp
384+
385+
# Return odata
386+
@g.app.route('/redfish/v1/odata')
387+
def get_odata():
388+
logging.info ('In get_odata')
389+
try:
390+
391+
odata_json = ""
392+
393+
if os.path.exists('Resources//odata//index.json'):
394+
# Use dynamic data source
395+
filename = 'Resources/odata/index.json'
396+
logging.info ('Resources path exists:', filename)
397+
else:
398+
# Use static mockup
399+
mockup_path = MOCKUPFOLDERS[0]
400+
filename = os.path.join("api_emulator", mockup_path, 'static', 'odata', 'index.json')
401+
402+
with open(filename, 'r') as var:
403+
for line in var:
404+
line = line.rstrip()
405+
odata_json += line
406+
407+
resp = make_response(odata_json, 200)
408+
resp.headers['Content-Type'] = 'application/json'
409+
return resp
410+
411+
except Exception:
412+
traceback.print_exc()
413+
resp = error_response('Internal Server Error', 500, True)
414+
return resp
415+
348416

349417
#
350418
# If any other RESTful request, send to RedfishAPI object for processing. Note: <path:path> specifies any path

0 commit comments

Comments
 (0)