/** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { session_start(); if (!isset($_SESSION['AUTH']) || $_SESSION['AUTH'] == false) { \App::abort(500, 'User not authenticated'); } $i = instanceUser::find($id); try { //emit request to make db $redis = \Redis::connection(); // Using the Redis extension provided client $redis->publish('demeter', json_encode(array('command' => 'deleteInstanceUser', 'vm' => $i->instance->vmId, 'instanceId' => $i->instance->id, 'instanceName' => $i->instance->name, 'username' => $i->name, 'netId' => $_SESSION['AUTH_USER']))); if ($i->delete()) { echo "success"; } else { \App::abort(500, 'User could not be deleted, please contact an Administrator'); } } catch (Exception $e) { \App::abort(500, 'User could not be deleted, please contact an Administrator'); } }
/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { session_start(); if (!isset($_SESSION['AUTH']) || $_SESSION['AUTH'] == false) { \App::abort(500, 'User not authenticated'); } $post = file_get_contents('php://input'); $data = json_decode($post, true); if ($data['name'] != null && $data['organization'] != null && $data['maxSize'] != null && $data['description'] != null && $data['type'] != null && $data['username'] != null && $data['password'] != null) { //check if instance exists with the same name // if(instance::where('name', $data['name'])->exists()) // die("Database name in use"); $user = demeterUser::where('netId', $_SESSION['AUTH_USER'])->first(); //create a new instance (db). expects name, type, ownerId, organization, maxSize, and description $i = new instance(); $i->id = \Uuid::generate(4); $i->name = $data['name']; $i->type = $data['type']; $i->ownerId = $user->id; $i->organization = $data['organization']; $i->maxSize = $data['maxSize']; $i->description = $data['description']; /////FOR DEMO if ($data['name'] == 'map') { //$i->port = 3306; //$i->ipAddr = "54.213.216.81"; } ////END //determine the VM for this instance $vms = vm::where("type", "LIKE", "%" . $i->type . "%")->get(); foreach ($vms as $vm) { if ($i->maxSize == $i->maxSize) { $i->vmId = $vm->id; break; } } if (!isset($i->vmId)) { echo "No VM available"; } else { try { //emit request to make db $redis = \Redis::connection(); // Using the Redis extension provided client $redis->publish('demeter', json_encode(array('command' => 'createInstance', 'vm' => $i->vmId, 'instanceId' => $i->id->string, 'instanceName' => $i->name, 'type' => $i->type, 'maxSize' => $i->maxSize, 'username' => $data['username'], 'password' => $data['password'], 'netId' => $_SESSION['AUTH_USER']))); $i->inUse = 0; $iu = new instanceUser(); $iu->id = \Uuid::generate(4); $iu->name = $data['username']; $iu->instanceId = $i->id->string; $id = $i->id->string; $i->save(); $inst = instance::find($id); $iu->save(); $inst->users()->save($user); echo "success"; //else // echo "fail"; } catch (Exception $e) { \App::abort(500, 'Database could not be created, please contact an Administrator'); } } } }