-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathholdcloud.js
More file actions
169 lines (157 loc) · 4.07 KB
/
holdcloud.js
File metadata and controls
169 lines (157 loc) · 4.07 KB
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
161
162
163
164
165
166
167
168
169
'use strict';
const _ = require('lodash');
const crypto = require('crypto');
const {request} = require('./request');
class HoldCloud {
/**
* [constructor description]
*
* @param {[String]} username [用户名]
* @param {[String]} password [用户密码]
* @param {[String]} baseUrl [基础 url]
* @param {[Boolean]} noStrictSSL 是否校验证书
*
*/
constructor(username, password, baseUrl, noStrictSSL) {
this.username = username;
this.password = password;
this.BASE_URI = baseUrl;
this.noStrictSSL = noStrictSSL;
}
}
HoldCloud.prototype.requestWithToken = async function (options = {}) {
options = _.cloneDeep(options);
if (!this.token) {
await this.updateToken();
}
_.set(options, 'headers.jweToken', this.token);
let tokenRefreshed = false;
const self = this;
const rp = request(this.BASE_URI, this.noStrictSSL);
return await rp(options).catch((error) => {
if (error.statusCode === 401 && !tokenRefreshed) {
self.updateToken();
tokenRefreshed = true;
self.requestWithToken(options);
} else {
throw error;
}
});
};
HoldCloud.prototype.updateToken = async function () {
const rp = request(this.BASE_URI, this.noStrictSSL);
const result = await rp({
method: 'POST',
url: 'login',
json: {
username: this.username,
password: crypto.createHash('md5').update(this.password, 'utf8').digest('hex'),
},
});
this.token = result.jweToken;
return result.jweToken;
};
/**
* 获取指定项目下的服务列表
*
* @param {String} projectId 服务id
*
* @return {array} 列表
*/
HoldCloud.prototype.listUnionservices = async function (projectId) {
return this.requestWithToken({
method: 'GET',
url: `project/${projectId}/unionservices`,
});
};
/**
* 在指定项目下创建容器服务
*
* @param {String}} projectId 项目 id
* @param {Object} options 服务参数
*
* @return {integer} 服务id
*/
HoldCloud.prototype.createContainerApp = async function (projectId, options) {
return await this.requestWithToken({
method: 'POST',
url: `project/${projectId}/containerapps`,
json: options,
});
};
/**
* 检查服务名称是否重复
*
* @param {string} projectId 项目 Id
* @param {string} name 服务名称
*
* @return {object} {code: 400, id: data[0].id};
*/
HoldCloud.prototype.checkContainerappsName = async function (projectId, name) {
const data = await this.requestWithToken({
method: 'GET',
url: `project/${projectId}/unionservices`,
searchParams: {
itemsPerPage: 10,
page: 1,
filterBy: `name,^${name}$,`,
sortBy: 'a,name,'
}
});
if (data.totalItems !== 0 && data.items[0].name === name) {
return {code: 400, id: data.items[0].id};
}
return {code: 200, id: null};
};
/**
* 创建容器实例
*
* @param {integer} appId 服务 id
* @param {Object} options 实例参数
*
* @return {Object} {}
*/
HoldCloud.prototype.createContainerAppInstances = async function (appId, options) {
return await this.requestWithToken({
method: 'POST',
url: `containerapp/${appId}/instances`,
json: options,
});
};
/**
* 获取容器实例状态
*
* @param {integer} appId 服务 id
*
* @return {Object} {state: "Creating"}
*/
HoldCloud.prototype.getContainerAppState = async function (appId) {
return await this.requestWithToken({
method: 'GET',
url: `containerapp/${appId}/state`,
})
};
/**
* 删除容器服务
*
* @param {Integer} appId [app 编号]
*
* @return {[Object]} [{}]
*/
HoldCloud.prototype.destroyContainerApp = async function (appId) {
return await this.requestWithToken({
method: 'DELETE',
url: `containerapp/${appId}`,
});
};
/**
* 重新启动指定服务
* @param {integer} appId [app 编号]
*/
HoldCloud.prototype.restartContainerApp = async function (appId) {
return await this.requestWithToken({
method: 'POST',
url: `containerapp/${appId}/restart`,
});
}
module.exports = HoldCloud;