Skip to content

Commit 2d68b90

Browse files
authored
Merge pull request #109 from mgazz/fix-composed
Improve handling of composition requests
2 parents 4475cae + 612284c commit 2d68b90

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

Diff for: api_emulator/redfish/ComputerSystem_api.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,16 @@ def get(self, ident):
7272
logging.info('ComputerSystemAPI GET called')
7373
try:
7474
# Find the entry with the correct value for Id
75-
resp = 404
7675
if ident in members:
7776
conf= members[ident]
7877
conf['ProcessorSummary']=self.processor_summary(ident)
7978
conf['MemorySummary']=self.memory_summary(ident)
8079
resp = conf, 200
80+
else:
81+
resp = "System " + ident + " not found" , 404
8182
except Exception:
8283
traceback.print_exc()
83-
resp = INTERNAL_ERROR
84+
resp = "Internal Server Error", INTERNAL_ERROR
8485
return resp
8586

8687
# HTTP PUT
@@ -127,19 +128,20 @@ def patch(self, ident):
127128
def delete(self, ident):
128129
logging.info('ComputerSystemAPI DELETE called')
129130
try:
130-
resp = 404
131131
if ident in members:
132-
if members[ident]['SystemType'] == 'Composed':
132+
if 'SystemType' in members[ident] and members[ident]['SystemType'] == 'Composed':
133133
# Delete a composed system
134134
resp = DeleteComposedSystem(ident)
135135
resp = 200
136136
else:
137137
# Delete a physical system
138138
del(members[ident])
139139
resp = 200
140+
else:
141+
resp = "System " + ident + " not found", 404
140142
except Exception:
141143
traceback.print_exc()
142-
resp = INTERNAL_ERROR
144+
resp = "Internal Server Error", INTERNAL_ERROR
143145
return resp
144146

145147

@@ -180,6 +182,12 @@ def put(self):
180182

181183
def verify(self, config):
182184
#TODO: Implement a method to verify that the POST body is valid
185+
if 'Id' not in config:
186+
return False, "Missing attribute: Id"
187+
if 'Links' not in config:
188+
return False, "Missing attribute: Links"
189+
if 'ResourceBlocks' not in config['Links']:
190+
return False, "Missing array: Links['ResourceBlocks']"
183191
return True,{}
184192

185193
# HTTP POST
@@ -193,13 +201,15 @@ def post(self):
193201
config = request.get_json(force=True)
194202
ok, msg = self.verify(config)
195203
if ok:
204+
config["@odata.id"]= "/redfish/v1/Systems/"+ config['Id']
196205
members[config['Id']] = config
197206
resp = config, 201
198207
else:
199208
resp = msg, 400
200209
except Exception:
201210
traceback.print_exc()
202-
resp = INTERNAL_ERROR
211+
resp = "Internal Server Error", INTERNAL_ERROR
212+
203213
return resp
204214

205215
'''

Diff for: test-composed-with-id.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"Id":"Composed-1",
3+
"Name": "Composed Computer System",
4+
"Links": {
5+
"ResourceBlocks": [
6+
{
7+
"@odata.id": "/redfish/v1/CompositionService/ResourceBlocks/Block-1"
8+
},
9+
{
10+
"@odata.id": "/redfish/v1/CompositionService/ResourceBlocks/Block-2"
11+
}
12+
]
13+
}
14+
}

Diff for: unittests.py

+37-2
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,50 @@ def test_redfish_get_system(self):
133133

134134
self.do_gets(params, logger)
135135

136+
def test_redfish_create_system_status_codes(self):
137+
"""
138+
Unit test to get resource of a system instance
139+
140+
NOTE: The emulator must be in the redfish mode to run this test
141+
"""
142+
self.log_file = 'test-create-system-status-codes.log'
143+
logger = self.get_logger(
144+
'test-create-system-status-codes',
145+
self.log_file)
146+
136147
with open('test-composed.json') as payload:
137148
headers = {'Content-Type': 'application/json'}
138149
r = requests.post(self.url('Systems'), data=payload, headers=headers)
139-
self.do_gets([(self.url('Systems/Composed-1'), 'ComposedSystem member')], logger)
150+
self.assert_status(r, 400, logger)
151+
152+
r = requests.get(self.url('Systems/Composed-1'))
153+
self.assert_status(r, 404, logger)
140154

141155
# Testing deleting the system instance (expect to fail with 404)
156+
r = requests.delete(self.url('Systems/Composed-1'))
157+
self.assert_status(r, 404, logger)
158+
159+
def test_redfish_create_system(self):
160+
"""
161+
Unit test to get resource of a system instance
162+
163+
NOTE: The emulator must be in the redfish mode to run this test
164+
"""
165+
self.log_file = 'test-create-system-status-codes.log'
166+
logger = self.get_logger(
167+
'test-create-system-status-codes',
168+
self.log_file)
169+
170+
with open('test-composed-with-id.json') as payload:
171+
headers = {'Content-Type': 'application/json'}
172+
r = requests.post(self.url('Systems'), data=payload, headers=headers)
173+
self.assert_status(r, 201, logger)
174+
175+
r = requests.get(self.url('Systems/Composed-1'))
176+
self.assert_status(r, 200, logger)
177+
142178
r = requests.delete(self.url('Systems/Composed-1'))
143179
self.assert_status(r, 200, logger)
144-
#logger.info('PASS: Unable to delete system instance')
145180

146181
if __name__ == '__main__':
147182
#main(sys.argv[2:])

0 commit comments

Comments
 (0)