[13860] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
[13847] | 22 | using HeuristicLab.Clients.Hive.WebJobManager.Models;
|
---|
[13754] | 23 | using HeuristicLab.Clients.Hive.WebJobManager.Services;
|
---|
[13862] | 24 | using HeuristicLab.Clients.Hive.WebJobManager.Services.Imports;
|
---|
[13860] | 25 | using Microsoft.AspNetCore.Hosting;
|
---|
| 26 | using Microsoft.AspNetCore.Http;
|
---|
| 27 | using Microsoft.AspNetCore.Mvc;
|
---|
[13689] | 28 | using System;
|
---|
| 29 | using System.Linq;
|
---|
[13782] | 30 | using System.Threading;
|
---|
[13689] | 31 |
|
---|
| 32 | namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
|
---|
| 33 | {
|
---|
[13805] | 34 | /// <summary>
|
---|
| 35 | /// Controller for the resources. Only handles requests that refresh the page.
|
---|
| 36 | /// </summary>
|
---|
[13689] | 37 | public class ResourceController : Controller
|
---|
| 38 | {
|
---|
[13739] | 39 | private WebLoginService weblog;
|
---|
| 40 | private HiveServiceLocatorWeb serviceLocator;
|
---|
[13754] | 41 | private AccessAdministrationClient accessClient;
|
---|
[13782] | 42 | private HiveAdminClientWeb adminClient;
|
---|
[13739] | 43 | private Guid userId;
|
---|
[13847] | 44 | private HiveWebUser currentUser;
|
---|
[13739] | 45 |
|
---|
[13689] | 46 | private IHostingEnvironment _environment;
|
---|
| 47 |
|
---|
| 48 | public ResourceController(IHostingEnvironment env)
|
---|
| 49 | {
|
---|
[13739] | 50 | weblog = WebLoginService.Instance;
|
---|
[13740] | 51 | _environment = env;
|
---|
| 52 | }
|
---|
[13805] | 53 | /// <summary>
|
---|
| 54 | /// Initialize and check the login. Gets all the required services for the user.
|
---|
| 55 | /// </summary>
|
---|
| 56 | /// <returns></returns>
|
---|
[13754] | 57 | private bool init()
|
---|
[13740] | 58 | {
|
---|
[13739] | 59 | var u = HttpContext.Session.GetString("UserId");
|
---|
| 60 | if (u == null || u == "" || Guid.Parse(u) == Guid.Empty)
|
---|
| 61 | {
|
---|
[13754] | 62 | return false;
|
---|
[13739] | 63 | }
|
---|
[13740] | 64 | else
|
---|
| 65 | {
|
---|
[13854] | 66 | try {
|
---|
[13739] | 67 | userId = Guid.Parse(u);
|
---|
| 68 | serviceLocator = weblog.getServiceLocator(userId);
|
---|
[13782] | 69 | adminClient = weblog.getAdminClient(userId);
|
---|
[13754] | 70 | accessClient = weblog.getAccessAdminClient(userId);
|
---|
[13847] | 71 | currentUser = weblog.getCurrentUser(userId);
|
---|
| 72 | if (currentUser.hasResourceAdminAccess())
|
---|
| 73 | return serviceLocator.CheckLogin();
|
---|
| 74 | else
|
---|
| 75 | return false;
|
---|
[13854] | 76 | }
|
---|
| 77 | catch (NullReferenceException)
|
---|
| 78 | {
|
---|
| 79 | return false;
|
---|
| 80 | }
|
---|
[13739] | 81 | }
|
---|
[13689] | 82 | }
|
---|
[13805] | 83 | /// <summary>
|
---|
| 84 | /// Loads the main page for the resources
|
---|
| 85 | /// </summary>
|
---|
| 86 | /// <returns></returns>
|
---|
[13689] | 87 | public IActionResult Index()
|
---|
| 88 | {
|
---|
[13754] | 89 |
|
---|
| 90 | if (init())
|
---|
[13689] | 91 | {
|
---|
[13754] | 92 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
[13689] | 93 | ViewBag.Title = "Resources";
|
---|
[13847] | 94 | return View("Index", currentUser);
|
---|
[13689] | 95 | }
|
---|
| 96 | else
|
---|
| 97 | {
|
---|
[13847] | 98 | return RedirectToAction("Index", "Job");
|
---|
[13689] | 99 | }
|
---|
| 100 | }
|
---|
[13805] | 101 | /// <summary>
|
---|
| 102 | /// Changes the parent for a specfic resource
|
---|
| 103 | /// </summary>
|
---|
| 104 | /// <param name="inpid">Resource to edit</param>
|
---|
| 105 | /// <param name="inpidpar">New parent resource</param>
|
---|
| 106 | /// <returns></returns>
|
---|
[13782] | 107 | [HttpPost]
|
---|
| 108 | public IActionResult changeParent(string inpid, string inpidpar)
|
---|
| 109 | {
|
---|
| 110 | if (init())
|
---|
| 111 | {
|
---|
| 112 | var gid = Guid.Parse(inpid);
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | adminClient.Refresh();
|
---|
| 116 | var tochange = adminClient.Resources.ToList().Find(x => x.Id == gid);
|
---|
[13805] | 117 | //gets current resource
|
---|
[13782] | 118 | if (inpidpar != null && inpidpar != "") {
|
---|
| 119 | Guid tempid = Guid.Parse(inpidpar);
|
---|
[13805] | 120 | while (tempid != null && tempid != Guid.Empty)
|
---|
| 121 | {//Check if a loop could occur (some parent of the parent being the parent)
|
---|
[13782] | 122 | if (tempid != gid)
|
---|
[13805] | 123 | {//Not equal so move up a level
|
---|
| 124 | var t = adminClient.Resources.ToList().Find(x => x.Id == tempid);
|
---|
| 125 | if (t.ParentResourceId == null)
|
---|
| 126 | tempid = Guid.Empty;
|
---|
| 127 | else
|
---|
| 128 | tempid = (Guid)t.ParentResourceId;
|
---|
| 129 |
|
---|
[13782] | 130 | }
|
---|
| 131 | else
|
---|
[13805] | 132 | break; //breaks loop if equal
|
---|
[13782] | 133 | }
|
---|
[13805] | 134 | if(tempid == null || tempid == Guid.Empty)//Broken loop => the tempid is not empty so no change
|
---|
[13782] | 135 | tochange.ParentResourceId = Guid.Parse(inpidpar);
|
---|
| 136 | }
|
---|
| 137 | else
|
---|
| 138 | tochange.ParentResourceId = null;
|
---|
| 139 | adminClient.Store(tochange, CancellationToken.None);
|
---|
[13805] | 140 | //Save to server
|
---|
[13782] | 141 |
|
---|
| 142 | return RedirectToAction("Index", "Resource");
|
---|
| 143 | }
|
---|
| 144 | else
|
---|
| 145 | {
|
---|
[13847] | 146 | return RedirectToAction("Index", "Job");
|
---|
[13782] | 147 | }
|
---|
| 148 | }
|
---|
[13805] | 149 | /// <summary>
|
---|
| 150 | /// Adds recources to a resource group
|
---|
| 151 | /// </summary>
|
---|
| 152 | /// <param name="addres">Array containing all the resources ID's from the group</param>
|
---|
| 153 | /// <param name="groupid">Group id</param>
|
---|
| 154 | /// <returns></returns>
|
---|
[13782] | 155 | [HttpPost]
|
---|
[13805] | 156 | public IActionResult addResourcesToGroup(string[] addres, string groupid)
|
---|
| 157 | {
|
---|
| 158 | if (init())
|
---|
| 159 | {
|
---|
| 160 | Guid gid = Guid.Parse(groupid);
|
---|
| 161 | adminClient.Refresh();
|
---|
| 162 | var exist = adminClient.Resources.ToList().FindAll(x => x.ParentResourceId == gid);
|
---|
| 163 | //Current existing member of the group
|
---|
| 164 |
|
---|
| 165 | foreach(var a in addres)
|
---|
| 166 | {//loop on each ID
|
---|
| 167 | Guid tempid = Guid.Parse(a);
|
---|
| 168 | var elemid = exist.FindIndex(x => x.Id == tempid);
|
---|
| 169 | if (elemid != -1)
|
---|
| 170 | {//If element already is a member of the group: scramble temporary ID (for future reference)
|
---|
| 171 | exist[elemid].Id = Guid.Empty;
|
---|
| 172 | }
|
---|
| 173 | else
|
---|
| 174 | {//If element is not yet in list => change parentresourceid from that element
|
---|
| 175 | var obj = adminClient.Resources.ToList().Find(x => x.Id == tempid);
|
---|
| 176 | obj.ParentResourceId = gid;
|
---|
| 177 | adminClient.Store(obj, CancellationToken.None);
|
---|
| 178 | }
|
---|
| 179 | }
|
---|
| 180 | foreach(var e in exist)
|
---|
| 181 | {//Loop on existing and check the ID's
|
---|
| 182 | if(e.Id != Guid.Empty)
|
---|
| 183 | {//ID != Empty means that element should not be a member of the list anymore
|
---|
| 184 | e.ParentResourceId = null; //remove ParentResourceID from element
|
---|
| 185 | adminClient.Store(e, CancellationToken.None);
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | return RedirectToAction("Index", "Resource");
|
---|
| 190 |
|
---|
| 191 | }
|
---|
| 192 | else
|
---|
| 193 | {
|
---|
[13847] | 194 | return RedirectToAction("Index", "Job");
|
---|
[13805] | 195 | }
|
---|
| 196 | }
|
---|
| 197 | /// <summary>
|
---|
| 198 | /// Delete a client group (relinks the parent from every member to possible grandparent)
|
---|
| 199 | /// </summary>
|
---|
| 200 | /// <param name="inpid">Group ID to delete</param>
|
---|
| 201 | /// <returns></returns>
|
---|
| 202 | [HttpPost]
|
---|
[13782] | 203 | public IActionResult deleteClientGroup(string inpid)
|
---|
| 204 | {
|
---|
| 205 | if (init())
|
---|
| 206 | {
|
---|
| 207 | var gid = Guid.Parse(inpid);
|
---|
| 208 |
|
---|
| 209 | adminClient.Refresh();
|
---|
| 210 | var group = adminClient.Resources.ToList().Find(x => x.Id == gid);
|
---|
[13805] | 211 | //Group to delete
|
---|
[13782] | 212 | var list = adminClient.Resources.ToList().FindAll(x => x.ParentResourceId == gid);
|
---|
[13805] | 213 | //All members from the group
|
---|
[13782] | 214 | foreach(var cl in list)
|
---|
[13805] | 215 | {//Links parent from every child to parent from the group to delete.
|
---|
[13782] | 216 | cl.ParentResourceId = group.ParentResourceId;
|
---|
| 217 | adminClient.Store(cl, CancellationToken.None);
|
---|
| 218 | }
|
---|
| 219 | adminClient.Delete(group);
|
---|
| 220 |
|
---|
| 221 | return RedirectToAction("Index", "Resource");
|
---|
| 222 | }
|
---|
| 223 | else
|
---|
| 224 | {
|
---|
[13847] | 225 | return RedirectToAction("Index", "Job");
|
---|
[13782] | 226 | }
|
---|
| 227 | }
|
---|
[13805] | 228 | /// <summary>
|
---|
| 229 | /// Create a new Client Group
|
---|
| 230 | /// </summary>
|
---|
| 231 | /// <param name="inpname">Name for the group</param>
|
---|
| 232 | /// <param name="inpheart">Heartbeat</param>
|
---|
| 233 | /// <param name="inpparent">Possible parent for the group</param>
|
---|
| 234 | /// <param name="clientstoadd">Clients to be added to the group</param>
|
---|
| 235 | /// <returns></returns>
|
---|
[13782] | 236 | [HttpPost]
|
---|
| 237 | public IActionResult newClientGroup(string inpname, int inpheart, string inpparent, string[] clientstoadd)
|
---|
| 238 | {
|
---|
| 239 | if (init()) {
|
---|
| 240 | adminClient.Refresh();
|
---|
| 241 |
|
---|
| 242 | SlaveGroup sg = new SlaveGroup();
|
---|
[13805] | 243 | //init
|
---|
[13782] | 244 | sg.Id = Guid.Empty;
|
---|
| 245 | sg.Name = inpname;
|
---|
| 246 | sg.HbInterval = inpheart;
|
---|
[13805] | 247 | if (inpparent != null)
|
---|
[13782] | 248 | sg.ParentResourceId = Guid.Parse(inpparent);
|
---|
[13805] | 249 |
|
---|
[13782] | 250 | sg.OwnerUserId = serviceLocator.getHiveServiceClient().GetUserIdByUsername(serviceLocator.getHiveServiceClient().ClientCredentials.UserName.UserName);
|
---|
| 251 | adminClient.Store(sg, CancellationToken.None);
|
---|
| 252 | adminClient.Refresh();
|
---|
[13805] | 253 | //Reloads the resources
|
---|
[13782] | 254 | var id = adminClient.Resources.ToList().Find(x => x.Name == inpname).Id;
|
---|
| 255 | foreach(var s in clientstoadd)
|
---|
[13805] | 256 | {//Loop so each client can be added to the group
|
---|
[13782] | 257 | var cid = Guid.Parse(s);
|
---|
| 258 | var client = adminClient.Resources.ToList().Find(x => x.Id == cid);
|
---|
| 259 | client.ParentResourceId = id;
|
---|
| 260 | adminClient.Store(client, CancellationToken.None);
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | return RedirectToAction("Index", "Resource");
|
---|
| 264 | }
|
---|
| 265 | else
|
---|
| 266 | {
|
---|
[13847] | 267 | return RedirectToAction("Index", "Job");
|
---|
[13782] | 268 | }
|
---|
| 269 | }
|
---|
[13689] | 270 | }
|
---|
| 271 | }
|
---|