-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.es6
162 lines (128 loc) · 3.82 KB
/
client.es6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import {Client} from 'node-rest-client';
export default class OpenstackClient{
/**
* This gets the ball rolling with authentication with OpenStack
* @param authEndpoint {String} The endpoint of the keystone token manager (ex: "http://10.0.0.10:5000")
* @param username {String,{}} Either the username of the user to login as (ex: "admin") OR the complete object you wish to authenticate with.
* @param password {String} The password you wish to authenticate with (ex: "admin")
* @param project {String} The project/tenant name you want to use for authentication (ex: "ethode")
*/
constructor(authEndpoint,username,password,project){
if(!authEndpoint || !username){
throw new Exception("You must specify an authentication endpoint and authentication data!");
}
if(typeof username == "string"){
this.authData = {
"passwordCredentials": {
"username": username,
"password": password
},
"tenantName": project
}
}else{
this.authData = username;
}
this.authEndpoint = authEndpoint;
this.client = new Client();
this.token = false;
this.waiting = [];
this._initToken();
}
/**
* This get's our token and will run any pre-requested code
* @param cb {Function} The function to execute upon completion
* @private
*/
_initToken(cb){
var self = this;
this.client.post(this.authEndpoint + "/v2.0/tokens", {
data: {
"auth": this.authData
},
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
}
}, function (data) {
self.token = data.access.token.id;
if(cb) cb(self.token);
console.info("Got Token: " + self.token);
self.waiting.forEach(function(item){
self._do(item.type, item.url, item.data, item.cb);
});
});
}
/**
* Performs a GET request on the url given
* @param url {String} The URL to hit for your request
* @param cb {Function} The function to call when request succeeds (ex: function(data, response){})
* @returns {*} This/Myself
*/
get(url,cb){
return this._do("get",url,{},cb);
}
/**
* Performs a POST request on the url given
* @param url {String} The URL to hit for your request
* @param data {*} Whatever you want to send in your request to the server
* @param cb {Function} The function to call when request succeeds (ex: function(data, response){})
* @returns {*} This/Myself
*/
post(url,data,cb){
return this._do("post",url,data,cb);
}
/**
* Performs a PUT request on the url given
* @param url {String} The URL to hit for your request
* @param data {*} Whatever you want to send in your request to the server
* @param cb {Function} The function to call when request succeeds (ex: function(data, response){})
* @returns {*} This/Myself
*/
put(url,data,cb){
return this._do("put",url,data,cb);
}
/**
* Will call the function passed into this function when a token has been successfully obtained
* @param cb {Function} The function to call when request succeeds (ex: function(data, response){})
* @returns {*} This/Myself
*/
onReady(cb){
return this._do("onReady", false, false, cb);
}
/**
* Actually does the requests
* @param type {String} post/get/put/etc
* @param url The URL we want to hit
* @param data The data to send, this must NEVER be NULL or everything iwll break
* @param cb
* @returns {*}
* @private
*/
_do(type,url,data,cb){
if(!this.token){
this.waiting.push({
type:type,
url:url,
data:data,
cb:cb
});
return this;
}
if(type=="onReady"){
if(cb) cb();
return;
}
console.info("Running Request (" + type + "): " + url);
var args = {
data: data,
headers: {
"Accept": "application/json",
"Content-Type": "application/json",
"X-Auth-Token": this.token,
"X-Auth-Project-Id": this.authData.tenantName
}
};
this.client[type](url,args,cb);
return this;
}
}