Matteo Scandolo | ecf088a | 2016-10-20 10:25:34 +0200 | [diff] [blame] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Visit http://guide.xosproject.org/devguide/addview/ for more information |
| 5 | * |
| 6 | * Created by teone on 10/20/16. |
| 7 | */ |
| 8 | |
| 9 | (function () { |
| 10 | 'use strict'; |
| 11 | angular.module('xos.globalXos') |
| 12 | .service('Controllers', function($resource){ |
| 13 | return $resource('/api/core/controllers/:id', {id: '@id'}); |
| 14 | }) |
| 15 | .service('GXOS', function($q, $resource, _, LXOS){ |
| 16 | this.attachXosToItem = (items, target = LXOS) => { |
| 17 | return _.map(items, (lxos, i) => { |
| 18 | let currentXos = target[i]; |
| 19 | return _.map(lxos, (item) => { |
| 20 | item.xos = currentXos; |
| 21 | return item; |
| 22 | }); |
| 23 | }) |
| 24 | }; |
| 25 | |
| 26 | this.buildQueryEndpoint = (baseUrl, target = LXOS) => { |
| 27 | return () => { |
| 28 | const d = $q.defer(); |
| 29 | |
| 30 | // store generated $resource for each L-XOS |
| 31 | let r = []; |
| 32 | |
| 33 | let p = []; |
| 34 | _.forEach(target, (xos, i) => { |
| 35 | let resource = $resource(`${xos.auth_url}${baseUrl}`, {id: '@id'}, { |
| 36 | query: { |
| 37 | isArray: true, |
| 38 | headers: { |
| 39 | Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}` |
| 40 | } |
| 41 | } |
| 42 | }); |
| 43 | r.push(resource); |
| 44 | p.push(r[i].query().$promise); |
| 45 | }); |
| 46 | |
| 47 | $q.all(p) |
| 48 | .then(res => { |
| 49 | res = this.attachXosToItem(res, target); |
| 50 | d.resolve(_.flatten(res)); |
| 51 | }) |
| 52 | .catch(d.reject); |
| 53 | |
| 54 | return {$promise: d.promise}; |
| 55 | }; |
| 56 | }; |
| 57 | |
| 58 | // TODO evaluate |
| 59 | this.buildLocalResource = (baseUrl, xos) => { |
| 60 | const headers = { |
| 61 | Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}` |
| 62 | }; |
| 63 | const resource = $resource(`${xos.auth_url}${baseUrl}`, {id: '@id'}, { |
| 64 | query: { |
| 65 | isArray: true, |
| 66 | headers: headers |
| 67 | } |
| 68 | }); |
| 69 | return resource; |
| 70 | } |
| 71 | }) |
| 72 | .service('LocalAuth', function ($q, $http, _, LXOS) { |
| 73 | const baseUrl = `api/utility/login/`; |
| 74 | this.login = () => { |
| 75 | const d = $q.defer(); |
| 76 | |
| 77 | let p = []; |
| 78 | _.forEach(LXOS, (xos, i) => { |
| 79 | let loginRequest = $http.post(`${xos.auth_url}${baseUrl}`, {username: xos.admin_user, password: xos.admin_password}); |
| 80 | p.push(loginRequest); |
| 81 | }); |
| 82 | |
| 83 | $q.all(p) |
| 84 | .then(auths => { |
| 85 | _.forEach(auths, (auth, i) => { |
| 86 | LXOS[i].xoscsrftoken = auth.data.xoscsrftoken; |
| 87 | LXOS[i].xossessionid = auth.data.xossessionid; |
| 88 | LXOS[i].user = JSON.parse(auth.data.user); |
| 89 | }); |
| 90 | d.resolve(); |
| 91 | }) |
| 92 | .catch(e => { |
| 93 | d.reject(e); |
| 94 | }); |
| 95 | |
| 96 | return d.promise; |
Matteo Scandolo | a93248a | 2016-11-15 17:03:11 -0800 | [diff] [blame] | 97 | }; |
| 98 | |
| 99 | this.getUserByName = (xos, username) => { |
| 100 | const d = $q.defer(); |
| 101 | console.log(username); |
| 102 | $http.get(`${xos.auth_url}api/core/users/`, { |
| 103 | params: { |
| 104 | username: username |
| 105 | }, |
| 106 | headers: { |
| 107 | Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}` |
| 108 | } |
| 109 | }) |
| 110 | .then((res) => { |
| 111 | d.resolve(res.data[0]); |
| 112 | }) |
| 113 | .catch(e => { |
| 114 | d.reject(e); |
| 115 | }); |
| 116 | |
| 117 | return d.promise; |
Matteo Scandolo | ecf088a | 2016-10-20 10:25:34 +0200 | [diff] [blame] | 118 | } |
| 119 | }) |
Matteo Scandolo | a93248a | 2016-11-15 17:03:11 -0800 | [diff] [blame] | 120 | .service('LocalSlices', function($q, $http, GXOS){ |
Matteo Scandolo | ecf088a | 2016-10-20 10:25:34 +0200 | [diff] [blame] | 121 | const baseUrl = `api/utility/slicesplus/`; |
| 122 | |
| 123 | // TODO build a global resource |
| 124 | this.queryFromAll = (targets) => { |
| 125 | return GXOS.buildQueryEndpoint(baseUrl, targets)(); |
| 126 | }; |
Matteo Scandolo | a93248a | 2016-11-15 17:03:11 -0800 | [diff] [blame] | 127 | |
| 128 | this.getLocalByName = (xos, sliceName) => { |
| 129 | const d = $q.defer(); |
| 130 | |
| 131 | $http.get(`${xos.auth_url}${baseUrl}`, { |
| 132 | // params: params, |
| 133 | headers: { |
| 134 | Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}` |
| 135 | } |
| 136 | }) |
| 137 | .then((res) => { |
| 138 | const slice = _.filter(res.data, (s) => { |
| 139 | return s.name.indexOf(sliceName) > -1; |
| 140 | }); |
| 141 | d.resolve(slice[0]); |
| 142 | }) |
| 143 | .catch(e => { |
| 144 | d.reject(e); |
| 145 | }); |
| 146 | |
| 147 | return d.promise; |
| 148 | }; |
Matteo Scandolo | ecf088a | 2016-10-20 10:25:34 +0200 | [diff] [blame] | 149 | }) |
| 150 | .service('LocalUsers', function(GXOS){ |
| 151 | const baseUrl = `api/core/users/`; |
| 152 | |
| 153 | // TODO build a global resource |
| 154 | this.queryFromAll = GXOS.buildQueryEndpoint(baseUrl); |
| 155 | }) |
| 156 | .service('LocalInstances', function($q, $http, GXOS, LocalDeployments, LocalImages, LocalFlavor, LocalNode){ |
| 157 | const baseUrl = `api/core/instances/`; |
| 158 | |
| 159 | // NOTE Evaluate to dinamically create a resource targeted to a L-XOS |
| 160 | |
| 161 | this.queryFromAll = (targets) => { |
| 162 | return GXOS.buildQueryEndpoint(baseUrl, targets)(); |
| 163 | }; |
| 164 | this.createOnLocal = (instance) => { |
| 165 | const d = $q.defer(); |
| 166 | const xos = instance.xos; |
| 167 | delete instance.xos; |
| 168 | $http.post(`${xos.auth_url}${baseUrl}`, instance, { |
| 169 | headers: { |
| 170 | Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}` |
| 171 | } |
| 172 | }) |
| 173 | .then((inst) => { |
| 174 | d.resolve(inst); |
| 175 | }) |
| 176 | .catch(e => { |
| 177 | d.reject(e); |
| 178 | }); |
| 179 | |
| 180 | return d.promise; |
| 181 | }; |
| 182 | |
| 183 | this.getFromLocal = (xos, params) => { |
| 184 | const d = $q.defer(); |
| 185 | $http.get(`${xos.auth_url}${baseUrl}`, { |
| 186 | params: params, |
| 187 | headers: { |
| 188 | Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}` |
| 189 | } |
| 190 | }) |
| 191 | .then((inst) => { |
| 192 | d.resolve(inst.data); |
| 193 | }) |
| 194 | .catch(e => { |
| 195 | d.reject(e); |
| 196 | }); |
| 197 | |
| 198 | return d.promise; |
| 199 | }; |
| 200 | |
Matteo Scandolo | a93248a | 2016-11-15 17:03:11 -0800 | [diff] [blame] | 201 | this.deleteFromLocal = (instance) => { |
| 202 | console.log('deleteFromLocal'); |
| 203 | const d = $q.defer(); |
| 204 | $http.delete(`${instance.xos.auth_url}${baseUrl}${instance.id}/`, { |
| 205 | headers: { |
| 206 | Authorization: `Basic ${btoa(instance.xos.admin_user + ':' + instance.xos.admin_password)}` |
| 207 | } |
| 208 | }) |
| 209 | .then((inst) => { |
| 210 | d.resolve(inst.data); |
| 211 | }) |
| 212 | .catch(e => { |
| 213 | d.reject(e); |
| 214 | }); |
| 215 | |
| 216 | return d.promise; |
| 217 | }; |
| 218 | |
Matteo Scandolo | ecf088a | 2016-10-20 10:25:34 +0200 | [diff] [blame] | 219 | this.getLocalInfo = (xos) => { |
| 220 | const d = $q.defer(); |
| 221 | $q.all([ |
| 222 | LocalDeployments.queryFromLocal(xos), |
| 223 | LocalImages.queryFromLocal(xos), |
| 224 | LocalFlavor.queryFromLocal(xos), |
| 225 | LocalNode.queryFromLocal(xos) |
| 226 | ]) |
| 227 | .then((res) => { |
| 228 | res = _.map(res, collection => { |
| 229 | return _.map(collection, item => { |
| 230 | return {id: item.id, label: item.name} |
| 231 | }); |
| 232 | }); |
| 233 | d.resolve(res); |
| 234 | }) |
| 235 | .catch(d.reject); |
| 236 | return d.promise; |
| 237 | }; |
| 238 | |
| 239 | }) |
| 240 | .service('LocalDeployments', function($q, $http){ |
| 241 | |
| 242 | const baseUrl = `api/core/deployments/`; |
| 243 | |
| 244 | this.queryFromLocal = (xos) => { |
| 245 | const d = $q.defer(); |
| 246 | |
| 247 | $http.get(`${xos.auth_url}${baseUrl}`, { |
| 248 | headers: { |
| 249 | Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}` |
| 250 | } |
| 251 | }) |
| 252 | .then((res) => { |
| 253 | d.resolve(res.data); |
| 254 | }) |
| 255 | .catch(e => { |
| 256 | d.reject(e); |
| 257 | }); |
| 258 | |
| 259 | return d.promise; |
| 260 | } |
| 261 | }) |
| 262 | .service('LocalImages', function($q, $http){ |
| 263 | |
| 264 | const baseUrl = `api/core/images/`; |
| 265 | |
| 266 | this.queryFromLocal = (xos) => { |
| 267 | const d = $q.defer(); |
| 268 | |
| 269 | $http.get(`${xos.auth_url}${baseUrl}`, { |
| 270 | headers: { |
| 271 | Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}` |
| 272 | } |
| 273 | }) |
| 274 | .then((res) => { |
| 275 | d.resolve(res.data); |
| 276 | }) |
| 277 | .catch(e => { |
| 278 | d.reject(e); |
| 279 | }); |
| 280 | |
| 281 | return d.promise; |
| 282 | } |
| 283 | }) |
| 284 | .service('LocalFlavor', function($q, $http){ |
| 285 | |
| 286 | const baseUrl = `api/core/flavors/`; |
| 287 | |
| 288 | this.queryFromLocal = (xos) => { |
| 289 | const d = $q.defer(); |
| 290 | |
| 291 | $http.get(`${xos.auth_url}${baseUrl}`, { |
| 292 | headers: { |
| 293 | Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}` |
| 294 | } |
| 295 | }) |
| 296 | .then((res) => { |
| 297 | d.resolve(res.data); |
| 298 | }) |
| 299 | .catch(e => { |
| 300 | d.reject(e); |
| 301 | }); |
| 302 | |
| 303 | return d.promise; |
| 304 | } |
| 305 | }) |
| 306 | .service('LocalNode', function($q, $http){ |
| 307 | |
| 308 | const baseUrl = `api/core/nodes/`; |
| 309 | |
| 310 | this.queryFromLocal = (xos) => { |
| 311 | const d = $q.defer(); |
| 312 | |
| 313 | $http.get(`${xos.auth_url}${baseUrl}`, { |
| 314 | headers: { |
| 315 | Authorization: `Basic ${btoa(xos.admin_user + ':' + xos.admin_password)}` |
| 316 | } |
| 317 | }) |
| 318 | .then((res) => { |
| 319 | d.resolve(res.data); |
| 320 | }) |
| 321 | .catch(e => { |
| 322 | d.reject(e); |
| 323 | }); |
| 324 | |
| 325 | return d.promise; |
| 326 | } |
| 327 | }); |
| 328 | })(); |
| 329 | |