fixing get inconsistency and adding vm_state_command

This commit is contained in:
2021-02-16 21:13:51 -06:00
parent 2e265703bd
commit e8348052a8
6 changed files with 87 additions and 14 deletions

View File

@ -52,6 +52,7 @@ def operation_impl(operation_id: int):
"list_ids": handle_list_ids,
"create": handle_create,
"destroy": handle_destroy,
"vm_state_command": handle_vm_state_command,
}
error_message = ""
@ -90,11 +91,8 @@ def handle_get(operation_id, request_body):
return abort(400, f"bad request; id is required for get")
vm = current_app.config['SPOKE_MODEL'].get(request_body['id'], request_body['get_ssh_host_keys'])
# TODO vm can be None when the capsul exists on this host but has no IP address yet
# when this happens it logs an "error reading assignment_status"
# To fix this we need to
# 1. modify shell script to return does it exist on this host + if it does whats its ip
# 2. if exists but no ip, return assigned with no ip
if vm is None:
return jsonify(dict(assignment_status="assigned"))
return jsonify(dict(assignment_status="assigned", id=vm.id, host=vm.host, ipv4=vm.ipv4, ipv6=vm.ipv6, ssh_host_keys=vm.ssh_host_keys))
@ -167,4 +165,26 @@ def handle_destroy(operation_id, request_body):
current_app.logger.error(f"current_app.config['SPOKE_MODEL'].destroy(id='{request_body['id']}', email='{request_body['email']}') failed: {error_message}")
return jsonify(dict(assignment_status="assigned", status="error", error_message=error_message))
return jsonify(dict(assignment_status="assigned", status="success"))
def handle_vm_state_command(operation_id, request_body):
required_properties = ['id', 'email', 'command']
for required_property in required_properties:
if required_property not in request_body:
current_app.logger.error(f"/hosts/operation returned 400: {required_property} is required for vm_state_command")
return abort(400, f"bad request; {required_property} is required for vm_state_command")
if request_body['command'] not in ["stop", "force-stop", "start", "restart"]:
current_app.logger.error(f"/hosts/operation returned 400: command ({request_body['command']}) must be one of stop, force-stop, start, or restart")
return abort(400, f"bad request; command ({request_body['command']}) must be one of stop, force-stop, start, or restart")
try:
current_app.config['SPOKE_MODEL'].vm_state_command(id=request_body['id'], email=request_body['email'], command=request_body['command'])
except:
error_message = my_exec_info_message(sys.exc_info())
current_app.logger.error(f"current_app.config['SPOKE_MODEL'].vm_state_command(id='{request_body['id']}', email='{request_body['email']}, command='{request_body['command']}') failed: {error_message}")
return jsonify(dict(assignment_status="assigned", status="error", error_message=error_message))
return jsonify(dict(assignment_status="assigned", status="success"))