1 | using HeuristicLab.Clients.Access;
|
---|
2 | using HeuristicLab.Clients.Access.Administration;
|
---|
3 | using HeuristicLab.Clients.Hive.WebJobManager.Services;
|
---|
4 | using Microsoft.AspNet.Hosting;
|
---|
5 | using Microsoft.AspNet.Http;
|
---|
6 | using Microsoft.AspNet.Mvc;
|
---|
7 | using System;
|
---|
8 | using System.Collections.Generic;
|
---|
9 | using System.Linq;
|
---|
10 | using System.ServiceModel.Security;
|
---|
11 | using System.Threading;
|
---|
12 | using System.Threading.Tasks;
|
---|
13 |
|
---|
14 | namespace HeuristicLab.Clients.Hive.WebJobManager.Controllers
|
---|
15 | {
|
---|
16 | /// <summary>
|
---|
17 | /// Controller for the resources. Only handles requests that refresh the page.
|
---|
18 | /// </summary>
|
---|
19 | public class ResourceController : Controller
|
---|
20 | {
|
---|
21 | private WebLoginService weblog;
|
---|
22 | private HiveServiceLocatorWeb serviceLocator;
|
---|
23 | private AccessAdministrationClient accessClient;
|
---|
24 | private HiveAdminClientWeb adminClient;
|
---|
25 | private Guid userId;
|
---|
26 |
|
---|
27 | private IHostingEnvironment _environment;
|
---|
28 |
|
---|
29 | public ResourceController(IHostingEnvironment env)
|
---|
30 | {
|
---|
31 | weblog = WebLoginService.Instance;
|
---|
32 | _environment = env;
|
---|
33 | }
|
---|
34 | /// <summary>
|
---|
35 | /// Initialize and check the login. Gets all the required services for the user.
|
---|
36 | /// </summary>
|
---|
37 | /// <returns></returns>
|
---|
38 | private bool init()
|
---|
39 | {
|
---|
40 | var u = HttpContext.Session.GetString("UserId");
|
---|
41 | if (u == null || u == "" || Guid.Parse(u) == Guid.Empty)
|
---|
42 | {
|
---|
43 | return false;
|
---|
44 | }
|
---|
45 | else
|
---|
46 | {
|
---|
47 | userId = Guid.Parse(u);
|
---|
48 | serviceLocator = weblog.getServiceLocator(userId);
|
---|
49 | adminClient = weblog.getAdminClient(userId);
|
---|
50 | accessClient = weblog.getAccessAdminClient(userId);
|
---|
51 | return serviceLocator.CheckLogin();
|
---|
52 |
|
---|
53 | }
|
---|
54 | }
|
---|
55 | /// <summary>
|
---|
56 | /// Loads the main page for the resources
|
---|
57 | /// </summary>
|
---|
58 | /// <returns></returns>
|
---|
59 | public IActionResult Index()
|
---|
60 | {
|
---|
61 |
|
---|
62 | if (init())
|
---|
63 | {
|
---|
64 | ViewBag.SessionId = HttpContext.Session.GetString("UserId");
|
---|
65 | ViewBag.Title = "Resources";
|
---|
66 | return View("Index");
|
---|
67 | }
|
---|
68 | else
|
---|
69 | {
|
---|
70 | return RedirectToAction("Index", "Home");
|
---|
71 | }
|
---|
72 | }
|
---|
73 | /// <summary>
|
---|
74 | /// Changes the parent for a specfic resource
|
---|
75 | /// </summary>
|
---|
76 | /// <param name="inpid">Resource to edit</param>
|
---|
77 | /// <param name="inpidpar">New parent resource</param>
|
---|
78 | /// <returns></returns>
|
---|
79 | [HttpPost]
|
---|
80 | public IActionResult changeParent(string inpid, string inpidpar)
|
---|
81 | {
|
---|
82 | if (init())
|
---|
83 | {
|
---|
84 | var gid = Guid.Parse(inpid);
|
---|
85 |
|
---|
86 |
|
---|
87 | adminClient.Refresh();
|
---|
88 | var tochange = adminClient.Resources.ToList().Find(x => x.Id == gid);
|
---|
89 | //gets current resource
|
---|
90 | if (inpidpar != null && inpidpar != "") {
|
---|
91 | Guid tempid = Guid.Parse(inpidpar);
|
---|
92 | while (tempid != null && tempid != Guid.Empty)
|
---|
93 | {//Check if a loop could occur (some parent of the parent being the parent)
|
---|
94 | if (tempid != gid)
|
---|
95 | {//Not equal so move up a level
|
---|
96 | var t = adminClient.Resources.ToList().Find(x => x.Id == tempid);
|
---|
97 | if (t.ParentResourceId == null)
|
---|
98 | tempid = Guid.Empty;
|
---|
99 | else
|
---|
100 | tempid = (Guid)t.ParentResourceId;
|
---|
101 |
|
---|
102 | }
|
---|
103 | else
|
---|
104 | break; //breaks loop if equal
|
---|
105 | }
|
---|
106 | if(tempid == null || tempid == Guid.Empty)//Broken loop => the tempid is not empty so no change
|
---|
107 | tochange.ParentResourceId = Guid.Parse(inpidpar);
|
---|
108 | }
|
---|
109 | else
|
---|
110 | tochange.ParentResourceId = null;
|
---|
111 | adminClient.Store(tochange, CancellationToken.None);
|
---|
112 | //Save to server
|
---|
113 |
|
---|
114 | return RedirectToAction("Index", "Resource");
|
---|
115 | }
|
---|
116 | else
|
---|
117 | {
|
---|
118 | return RedirectToAction("Index", "Home");
|
---|
119 | }
|
---|
120 | }
|
---|
121 | /// <summary>
|
---|
122 | /// Adds recources to a resource group
|
---|
123 | /// </summary>
|
---|
124 | /// <param name="addres">Array containing all the resources ID's from the group</param>
|
---|
125 | /// <param name="groupid">Group id</param>
|
---|
126 | /// <returns></returns>
|
---|
127 | [HttpPost]
|
---|
128 | public IActionResult addResourcesToGroup(string[] addres, string groupid)
|
---|
129 | {
|
---|
130 | if (init())
|
---|
131 | {
|
---|
132 | Guid gid = Guid.Parse(groupid);
|
---|
133 | adminClient.Refresh();
|
---|
134 | var exist = adminClient.Resources.ToList().FindAll(x => x.ParentResourceId == gid);
|
---|
135 | //Current existing member of the group
|
---|
136 |
|
---|
137 | foreach(var a in addres)
|
---|
138 | {//loop on each ID
|
---|
139 | Guid tempid = Guid.Parse(a);
|
---|
140 | var elemid = exist.FindIndex(x => x.Id == tempid);
|
---|
141 | if (elemid != -1)
|
---|
142 | {//If element already is a member of the group: scramble temporary ID (for future reference)
|
---|
143 | exist[elemid].Id = Guid.Empty;
|
---|
144 | }
|
---|
145 | else
|
---|
146 | {//If element is not yet in list => change parentresourceid from that element
|
---|
147 | var obj = adminClient.Resources.ToList().Find(x => x.Id == tempid);
|
---|
148 | obj.ParentResourceId = gid;
|
---|
149 | adminClient.Store(obj, CancellationToken.None);
|
---|
150 | }
|
---|
151 | }
|
---|
152 | foreach(var e in exist)
|
---|
153 | {//Loop on existing and check the ID's
|
---|
154 | if(e.Id != Guid.Empty)
|
---|
155 | {//ID != Empty means that element should not be a member of the list anymore
|
---|
156 | e.ParentResourceId = null; //remove ParentResourceID from element
|
---|
157 | adminClient.Store(e, CancellationToken.None);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | return RedirectToAction("Index", "Resource");
|
---|
162 |
|
---|
163 | }
|
---|
164 | else
|
---|
165 | {
|
---|
166 | return RedirectToAction("Index", "Home");
|
---|
167 | }
|
---|
168 | }
|
---|
169 | /// <summary>
|
---|
170 | /// Delete a client group (relinks the parent from every member to possible grandparent)
|
---|
171 | /// </summary>
|
---|
172 | /// <param name="inpid">Group ID to delete</param>
|
---|
173 | /// <returns></returns>
|
---|
174 | [HttpPost]
|
---|
175 | public IActionResult deleteClientGroup(string inpid)
|
---|
176 | {
|
---|
177 | if (init())
|
---|
178 | {
|
---|
179 | var gid = Guid.Parse(inpid);
|
---|
180 |
|
---|
181 | adminClient.Refresh();
|
---|
182 | var group = adminClient.Resources.ToList().Find(x => x.Id == gid);
|
---|
183 | //Group to delete
|
---|
184 | var list = adminClient.Resources.ToList().FindAll(x => x.ParentResourceId == gid);
|
---|
185 | //All members from the group
|
---|
186 | foreach(var cl in list)
|
---|
187 | {//Links parent from every child to parent from the group to delete.
|
---|
188 | cl.ParentResourceId = group.ParentResourceId;
|
---|
189 | adminClient.Store(cl, CancellationToken.None);
|
---|
190 | }
|
---|
191 | adminClient.Delete(group);
|
---|
192 |
|
---|
193 | return RedirectToAction("Index", "Resource");
|
---|
194 | }
|
---|
195 | else
|
---|
196 | {
|
---|
197 | return RedirectToAction("Index", "Home");
|
---|
198 | }
|
---|
199 | }
|
---|
200 | /// <summary>
|
---|
201 | /// Create a new Client Group
|
---|
202 | /// </summary>
|
---|
203 | /// <param name="inpname">Name for the group</param>
|
---|
204 | /// <param name="inpheart">Heartbeat</param>
|
---|
205 | /// <param name="inpparent">Possible parent for the group</param>
|
---|
206 | /// <param name="clientstoadd">Clients to be added to the group</param>
|
---|
207 | /// <returns></returns>
|
---|
208 | [HttpPost]
|
---|
209 | public IActionResult newClientGroup(string inpname, int inpheart, string inpparent, string[] clientstoadd)
|
---|
210 | {
|
---|
211 | if (init()) {
|
---|
212 | adminClient.Refresh();
|
---|
213 |
|
---|
214 | SlaveGroup sg = new SlaveGroup();
|
---|
215 | //init
|
---|
216 | sg.Id = Guid.Empty;
|
---|
217 | sg.Name = inpname;
|
---|
218 | sg.HbInterval = inpheart;
|
---|
219 | if (inpparent != null)
|
---|
220 | sg.ParentResourceId = Guid.Parse(inpparent);
|
---|
221 |
|
---|
222 | sg.OwnerUserId = serviceLocator.getHiveServiceClient().GetUserIdByUsername(serviceLocator.getHiveServiceClient().ClientCredentials.UserName.UserName);
|
---|
223 | adminClient.Store(sg, CancellationToken.None);
|
---|
224 | adminClient.Refresh();
|
---|
225 | //Reloads the resources
|
---|
226 | var id = adminClient.Resources.ToList().Find(x => x.Name == inpname).Id;
|
---|
227 | foreach(var s in clientstoadd)
|
---|
228 | {//Loop so each client can be added to the group
|
---|
229 | var cid = Guid.Parse(s);
|
---|
230 | var client = adminClient.Resources.ToList().Find(x => x.Id == cid);
|
---|
231 | client.ParentResourceId = id;
|
---|
232 | adminClient.Store(client, CancellationToken.None);
|
---|
233 | }
|
---|
234 |
|
---|
235 | return RedirectToAction("Index", "Resource");
|
---|
236 | }
|
---|
237 | else
|
---|
238 | {
|
---|
239 | return RedirectToAction("Index", "Home");
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|
243 | }
|
---|