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