blob: 5a57b89828cafc671753860c468a323d9f52c2d1 [file] [log] [blame]
Matteo Scandolof4f69552017-08-24 17:26:25 -07001/*
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
17const request = require('request-promise');
18const config = require('../test_helpers/config');
19const user = require('../test_helpers/user');
20const P = require('bluebird');
21const baseUrl = config.url.replace('/xos/#', '');
22
23const auth = {
24 'auth': {
25 'user': user.username,
26 'pass': user.pwd.replace('\n', ''),
27 }
28};
29
30const _getIds = (list) => {
31 return list.map(i => i.id);
32}
33
34const _parseRes = (res) => JSON.parse(res);
35
36const 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
46const 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
56const 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
78const 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
97module.exports = {
98 getModels: getModels,
99 deleteAllModels: deleteAllModels,
100 createModel: createModel,
101 deleteModel: deleteModel
102}