Matteo Scandolo | f4f6955 | 2017-08-24 17:26:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | const request = require('request-promise'); |
| 18 | const config = require('../test_helpers/config'); |
| 19 | const user = require('../test_helpers/user'); |
| 20 | const P = require('bluebird'); |
| 21 | const baseUrl = config.url.replace('/xos/#', ''); |
| 22 | |
| 23 | const auth = { |
| 24 | 'auth': { |
| 25 | 'user': user.username, |
| 26 | 'pass': user.pwd.replace('\n', ''), |
| 27 | } |
| 28 | }; |
| 29 | |
| 30 | const _getIds = (list) => { |
| 31 | return list.map(i => i.id); |
| 32 | } |
| 33 | |
| 34 | const _parseRes = (res) => JSON.parse(res); |
| 35 | |
| 36 | const getModels = P.promisify((url, done) => { |
| 37 | request.get(`${ baseUrl + url }`, auth) |
| 38 | .then(res => { |
| 39 | return done(null, _parseRes(res)); |
| 40 | }) |
| 41 | .catch(e => { |
| 42 | return done(e); |
| 43 | }); |
| 44 | }); |
| 45 | |
| 46 | const deleteModel = P.promisify((url, id, done) => { |
| 47 | request.delete(`${ baseUrl +url }/${id}`, auth) |
| 48 | .then(res => { |
| 49 | return done(null, res); |
| 50 | }) |
| 51 | .catch(e => { |
| 52 | return done(e); |
| 53 | }); |
| 54 | }); |
| 55 | |
| 56 | const deleteAllModels = P.promisify((url, done) => { |
| 57 | getModels(url) |
| 58 | .then(res => { |
| 59 | if (res.items.length === 0) { |
| 60 | return done(null); |
| 61 | } |
| 62 | const ids = _getIds(res.items); |
| 63 | const promises = []; |
| 64 | ids.forEach(id => { |
| 65 | promises.push(deleteModel(url, id)); |
| 66 | }) |
| 67 | |
| 68 | return P.all(promises) |
| 69 | }) |
| 70 | .then(res => { |
| 71 | done(null, res); |
| 72 | }) |
| 73 | .catch(e => { |
| 74 | return done(e); |
| 75 | }); |
| 76 | }); |
| 77 | |
| 78 | const createModel = P.promisify((url, data, done) => { |
| 79 | request({ |
| 80 | uri: baseUrl + url, |
| 81 | method: 'POST', |
| 82 | json: data, |
| 83 | auth: auth.auth |
| 84 | }) |
| 85 | .then(res => { |
| 86 | return done(null, res); |
| 87 | }) |
| 88 | .catch(err => { |
| 89 | return done(err); |
| 90 | }) |
| 91 | }) |
| 92 | |
| 93 | // getModels(`/xosapi/v1/core/nodes`).then(console.log); |
| 94 | |
| 95 | // createModel(`/xosapi/v1/core/nodes`, {name: 'test-p', site_deployment_id: 1}); |
| 96 | |
| 97 | module.exports = { |
| 98 | getModels: getModels, |
| 99 | deleteAllModels: deleteAllModels, |
| 100 | createModel: createModel, |
| 101 | deleteModel: deleteModel |
| 102 | } |