forked from p15670423/monkey
Island: Refactor RemoteRun.post() results encoding
This commit is contained in:
parent
43d8fd87ed
commit
ae0e8ddb8e
|
@ -51,27 +51,26 @@ class RemoteRun(flask_restful.Resource):
|
||||||
@jwt_required
|
@jwt_required
|
||||||
def post(self):
|
def post(self):
|
||||||
body = json.loads(request.data)
|
body = json.loads(request.data)
|
||||||
resp = {}
|
|
||||||
if body.get("type") == "aws":
|
if body.get("type") == "aws":
|
||||||
result = self.run_aws_monkeys(body)
|
results = self.run_aws_monkeys(body)
|
||||||
result = self._get_encodable_results(result)
|
return RemoteRun._encode_results(results)
|
||||||
resp["result"] = result
|
|
||||||
return jsonify(resp)
|
|
||||||
|
|
||||||
# default action
|
# default action
|
||||||
return make_response({"error": "Invalid action"}, 500)
|
return make_response({"error": "Invalid action"}, 500)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _get_encodable_results(results: Sequence[AWSCommandResults]) -> str:
|
def _encode_results(results: Sequence[AWSCommandResults]):
|
||||||
results_copy = []
|
result = list(map(RemoteRun._aws_command_results_to_encodable_dict, results))
|
||||||
for result in results:
|
response = {"result": result}
|
||||||
results_copy.append(
|
|
||||||
AWSCommandResults(
|
return jsonify(response)
|
||||||
result.instance_id,
|
|
||||||
result.response_code,
|
@staticmethod
|
||||||
result.stdout,
|
def _aws_command_results_to_encodable_dict(aws_command_results: AWSCommandResults):
|
||||||
result.stderr,
|
return {
|
||||||
result.status.name.lower(),
|
"instance_id": aws_command_results.instance_id,
|
||||||
)
|
"response_code": aws_command_results.response_code,
|
||||||
)
|
"stdout": aws_command_results.stdout,
|
||||||
return results_copy
|
"stderr": aws_command_results.stderr,
|
||||||
|
"status": aws_command_results.status.name.lower(),
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue