UNPKG

8.69 kBPlain TextView Raw
1'use strict'
2
3/**
4 * Class reprensenting a plugin
5 */
6class Plugin {
7
8 modem: any
9 id: any
10
11 /**
12 * Creates a new plugin
13 * @param {Modem} modem Modem to connect to the remote service
14 * @param {string} id Id of the plugin (optional)
15 */
16 constructor (modem, id?) {
17 this.modem = modem
18 this.id = id
19 }
20
21 /**
22 * Get the list of plugins
23 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/list-plugins
24 * @param {Object} opts Query params in the request (optional)
25 * @return {Promise} Promise returning the result as a list of plugins
26 */
27 list (opts) {
28 const call = {
29 path: '/plugins?',
30 method: 'GET',
31 options: opts,
32 statusCodes: {
33 200: true,
34 500: 'server error'
35 }
36 }
37
38 return new Promise((resolve, reject) => {
39 this.modem.dial(call, (err, plugins) => {
40 if (err) return reject(err)
41 if (!plugins || !plugins.length) return resolve([])
42 resolve(plugins.map((conf) => {
43 const plugin = new Plugin(this.modem, conf.Id)
44 return Object.assign(plugin, conf)
45 }))
46 })
47 })
48 }
49
50 /**
51 * upgrade a plugin
52 * https://docs.docker.com/engine/api/v1.26/#operation/PluginUpgrade
53 * @param {Object} opts Query params in the request (optional)
54 * @return {Promise} Promise return the new plugin
55 */
56 upgrade (opts) {
57 let id
58 [ opts, id ] = this.__processArguments(opts)
59
60 const call = {
61 path: `/plugins/${id}/upgrade?`,
62 method: 'POST',
63 options: opts,
64 statusCodes: {
65 204: true,
66 404: 'plugin not installed',
67 500: 'server error'
68 }
69 }
70
71 return new Promise((resolve, reject) => {
72 this.modem.dial(call, (err, conf) => {
73 if (err) return reject(err)
74 const plugin = new Plugin(this.modem, opts.name)
75 resolve(plugin)
76 })
77 })
78 }
79
80 /**
81 * Create a plugin
82 * https://docs.docker.com/engine/api/v1.25/#operation/PluginCreate
83 * @param {Object} opts Query params in the request (optional)
84 * @return {Promise} Promise return the new plugin
85 */
86 create (opts) {
87 const call = {
88 path: '/plugins/create?',
89 method: 'POST',
90 options: opts,
91 statusCodes: {
92 204: true,
93 500: 'server error'
94 }
95 }
96
97 return new Promise((resolve, reject) => {
98 this.modem.dial(call, (err, conf) => {
99 if (err) return reject(err)
100 const plugin = new Plugin(this.modem, opts.name)
101 resolve(plugin)
102 })
103 })
104 }
105
106 /**
107 * install a plugin
108 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/install-a-plugin
109 * @param {Object} opts Query params in the request (optional)
110 * @return {Promise} Promise return the new plugin
111 */
112 install (opts) {
113 const call = {
114 path: '/plugins/pull?',
115 method: 'POST',
116 options: opts,
117 statusCodes: {
118 200: true,
119 500: 'server error'
120 }
121 }
122
123 return new Promise((resolve, reject) => {
124 this.modem.dial(call, (err, conf) => {
125 if (err) return reject(err)
126 const plugin = new Plugin(this.modem, opts.name)
127 resolve(plugin)
128 })
129 })
130 }
131
132 /**
133 * Get low-level information on a plugin
134 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/inspect-a-plugin
135 * The reason why this module isn't called inspect is because that interferes with the inspect utility of node.
136 * @param {Object} opts Query params in the request (optional)
137 * @param {String} id ID of the plugin to inspect, if it's not set, use the id of the object (optional)
138 * @return {Promise} Promise return the plugin
139 */
140 status (opts, id) {
141 [ opts, id ] = this.__processArguments(opts, id)
142
143 const call = {
144 path: `/plugins/${id}?`,
145 method: 'GET',
146 options: opts,
147 statusCodes: {
148 200: true,
149 404: 'no such plugin',
150 500: 'server error'
151 }
152 }
153
154 return new Promise((resolve, reject) => {
155 this.modem.dial(call, (err, conf) => {
156 if (err) return reject(err)
157 const plugin = new Plugin(this.modem, id)
158 resolve(Object.assign(plugin, conf))
159 })
160 })
161 }
162
163 /**
164 * Remove a plugin
165 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/remove-a-plugin
166 * @param {Object} opts Query params in the request (optional)
167 * @param {String} id ID of the plugin to inspect, if it's not set, use the id of the object (optional)
168 * @return {Promise} Promise return the result
169 */
170 remove (opts, id) {
171 [ opts, id ] = this.__processArguments(opts, id)
172 const call = {
173 path: `/plugins/${id}?`,
174 method: 'DELETE',
175 options: opts,
176 statusCodes: {
177 200: true,
178 404: 'no such plugin',
179 500: 'server error'
180 }
181 }
182
183 return new Promise((resolve, reject) => {
184 this.modem.dial(call, (err, res) => {
185 if (err) return reject(err)
186 resolve(res)
187 })
188 })
189 }
190
191 /**
192 * push a plugin
193 * https://docs.docker.com/engine/api/v1.26/#operation/PluginPush
194 * @param {Object} opts Query params in the request (optional)
195 * @param {String} id ID of the plugin, if it's not set, use the id of the object (optional)
196 * @return {Promise} Promise return the plugin
197 */
198 push (opts, id) {
199 [ opts, id ] = this.__processArguments(opts, id)
200
201 const call = {
202 path: `/plugins/${id}/push?`,
203 method: 'POST',
204 options: opts,
205 statusCodes: {
206 200: true,
207 404: 'plugin not found',
208 500: 'server error'
209 }
210 }
211
212 return new Promise((resolve, reject) => {
213 this.modem.dial(call, (err, conf) => {
214 if (err) return reject(err)
215 const plugin = new Plugin(this.modem, id)
216 resolve(plugin)
217 })
218 })
219 }
220
221 /**
222 * Set a plugin configuration
223 * https://docs.docker.com/engine/api/v1.25/#operation/PluginSet
224 * @param {Object} opts Query params in the request (optional)
225 * @param {String} id ID of the plugin, if it's not set, use the id of the object (optional)
226 * @return {Promise} Promise return the plugin
227 */
228 set (opts, id) {
229 [ opts, id ] = this.__processArguments(opts, id)
230
231 const call = {
232 path: `/plugins/${id}/set?`,
233 method: 'POST',
234 options: opts,
235 statusCodes: {
236 204: true,
237 404: 'plugin not found',
238 500: 'server error'
239 }
240 }
241
242 return new Promise((resolve, reject) => {
243 this.modem.dial(call, (err, conf) => {
244 if (err) return reject(err)
245 const plugin = new Plugin(this.modem, id)
246 resolve(plugin)
247 })
248 })
249 }
250
251 /**
252 * Enable a plugin
253 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/enable-a-plugin
254 * @param {Object} opts Query params in the request (optional)
255 * @param {String} id ID of the plugin, if it's not set, use the id of the object (optional)
256 * @return {Promise} Promise return the plugin
257 */
258 enable (opts, id) {
259 [ opts, id ] = this.__processArguments(opts, id)
260
261 const call = {
262 path: `/plugins/${id}/enable?`,
263 method: 'POST',
264 options: opts,
265 statusCodes: {
266 200: true,
267 500: 'server error'
268 }
269 }
270
271 return new Promise((resolve, reject) => {
272 this.modem.dial(call, (err, conf) => {
273 if (err) return reject(err)
274 const plugin = new Plugin(this.modem, id)
275 resolve(plugin)
276 })
277 })
278 }
279
280 /**
281 * Disable a plugin
282 * https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/disable-a-plugin
283 * @param {Object} opts Query params in the request (optional)
284 * @param {String} id ID of the plugin, if it's not set, use the id of the object (optional)
285 * @return {Promise} Promise return the plugin
286 */
287 disable (opts, id) {
288 [ opts, id ] = this.__processArguments(opts, id)
289
290 const call = {
291 path: `/plugins/${id}/disable?`,
292 method: 'POST',
293 options: opts,
294 statusCodes: {
295 200: true,
296 500: 'server error'
297 }
298 }
299
300 return new Promise((resolve, reject) => {
301 this.modem.dial(call, (err, conf) => {
302 if (err) return reject(err)
303 const plugin = new Plugin(this.modem, id)
304 resolve(plugin)
305 })
306 })
307 }
308
309 __processArguments (opts, id?) {
310 if (typeof opts === 'string' && !id) {
311 id = opts
312 }
313 if (!id && this.id) {
314 id = this.id
315 }
316 if (!opts) opts = {}
317 return [ opts, id ]
318 }
319}
320
321export default Plugin