UNPKG

2.81 kBJavaScriptView Raw
1var util = require('./util');
2
3/**
4 * Represents a config
5 * @param {Object} modem docker-modem
6 * @param {String} id Config's id
7 */
8var Config = function(modem, id) {
9 this.modem = modem;
10 this.id = id;
11};
12
13Config.prototype[require('util').inspect.custom] = function() { return this; };
14
15/**
16 * Inspect
17 * @param {Function} callback Callback, if specified Docker will be queried.
18 * @return {Object} Name only if callback isn't specified.
19 */
20Config.prototype.inspect = function(callback) {
21 var self = this;
22
23 var optsf = {
24 path: '/configs/' + this.id,
25 method: 'GET',
26 statusCodes: {
27 200: true,
28 404: 'config not found',
29 500: 'server error',
30 503: 'node is not part of a swarm'
31 }
32 };
33
34 if(callback === undefined) {
35 return new this.modem.Promise(function(resolve, reject) {
36 self.modem.dial(optsf, function(err, data) {
37 if (err) {
38 return reject(err);
39 }
40 resolve(data);
41 });
42 });
43 } else {
44 this.modem.dial(optsf, function(err, data) {
45 callback(err, data);
46 });
47 }
48};
49
50/**
51 * Update a config.
52 *
53 * @param {object} opts
54 * @param {function} callback
55 */
56Config.prototype.update = function(opts, callback) {
57 var self = this;
58 if (!callback && typeof opts === 'function') {
59 callback = opts;
60 }
61
62 var optsf = {
63 path: '/configs/' + this.id + '/update?',
64 method: 'POST',
65 statusCodes: {
66 200: true,
67 404: 'config not found',
68 500: 'server error',
69 503: 'node is not part of a swarm'
70 },
71 options: opts
72 };
73
74 if(callback === undefined) {
75 return new this.modem.Promise(function(resolve, reject) {
76 self.modem.dial(optsf, function(err, data) {
77 if (err) {
78 return reject(err);
79 }
80 resolve(data);
81 });
82 });
83 } else {
84 this.modem.dial(optsf, function(err, data) {
85 callback(err, data);
86 });
87 }
88};
89
90
91/**
92 * Removes the config
93 * @param {[Object]} opts Remove options (optional)
94 * @param {Function} callback Callback
95 */
96Config.prototype.remove = function(opts, callback) {
97 var self = this;
98 var args = util.processArgs(opts, callback);
99
100 var optsf = {
101 path: '/configs/' + this.id,
102 method: 'DELETE',
103 statusCodes: {
104 200: true,
105 204: true,
106 404: 'config not found',
107 500: 'server error',
108 503: 'node is not part of a swarm'
109 },
110 options: args.opts
111 };
112
113 if(args.callback === undefined) {
114 return new this.modem.Promise(function(resolve, reject) {
115 self.modem.dial(optsf, function(err, data) {
116 if (err) {
117 return reject(err);
118 }
119 resolve(data);
120 });
121 });
122 } else {
123 this.modem.dial(optsf, function(err, data) {
124 args.callback(err, data);
125 });
126 }
127};
128
129
130
131module.exports = Config;