/
databases.js
190 lines (166 loc) · 5.91 KB
/
databases.js
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* database.js: Database methods for working with databases from MongoLab
*
* (C) 2012 Charlie Robbins, Ken Perkins, Ross Kukulinski & the Contributors.
*
*/
var errs = require('errs'),
url = require('url');
// Function formatResponse
// This function parse the response from the provider and return an object
// with the correct keys and values.
// ### @response {Object} The body response from the provider api
function formatResponse(response) {
var info, user, dbname, auth;
info = url.parse(response.uri);
auth = encodeURIComponent(info.auth);
user = auth.replace(/%3A/i, ':').split(':');
dbname = response.name;
var database = {
id: dbname,
host: info.hostname,
port: Number(info.port),
uri: 'mongodb://' + info.auth + '@' + info.host,
username: decodeURIComponent(user[0]),
password: decodeURIComponent(user[1]),
dbname: dbname
};
return database;
}
// Create Database
// ### @options {Object} Set of options can be
// #### options['name'] {String} Name of database (required)
// #### options['owner'] {String} Name of the user owner the database (required)
// #### options['plan'] {String} Name of plan according to the MongoLab plans (Default: 'free')
// ### @callback {Function} Continuation to respond to when complete.
exports.create = function create(options, callback) {
// Check for options
if (typeof options === 'function') {
return errs.handle(errs.create({
message: 'Options required for create a database.'
}), options);
}
if (!options['name']) {
return errs.handle(errs.create({
message: 'options. Name is a required argument'
}), callback);
}
if (!options['owner']) {
return errs.handle(errs.create({
message: 'options. Owner is a required argument'
}), callback);
}
if (!options['plan']) {
options['plan'] = 'free';
}
// We have to setup the correct prefix for database name
// for the moment we use the 'owner' field because we expect the correct prefix there.
var databaseName = [options['owner'], options['name']].join('_');
// Setup the account name according mongolab API.
// @todo We need a helper function for add the prefix if its necesary
//var account = [this.config.username, options['owner']].join('_');
// at the moment we need provide the username with the prefix (partner name)
var account = options['owner'];
var createOptions = {
method: 'POST',
path: 'accounts/' + account + '/databases',
body: {
name: databaseName,
plan: options['plan'],
username: options['owner'],
// In future we will have to change this for support multiples clouds and user-selected cloud.
cloud: this.config.cloud
}
};
this._request(createOptions, function (err, body) {
return err
? callback(err)
: callback(null, formatResponse(body));
});
};
// Lists all databases by user account
// ### @owner {String} Username for list their databases
// ### @callback {Function} Continuation to respond to when complete.
exports.getDatabases = function getDatabases(owner, callback) {
// Check for options
if (typeof owner === 'function') {
return errs.handle(errs.create({
message: 'Name required for delete an account.'
}), owner);
}
this._request({ path: 'accounts/' + owner + '/databases' }, function (err, body) {
return err
? callback(err)
: callback(null, body);
});
};
// View one database with details
// NOT USE THIS METHOD YET
// The principal idea of this method is for view details like username and
// password and the hostname and port, but for now MongoLab just answer with the name.
// The behavior I describe its according the parters documentation.
// https://objectlabs.jira.com/wiki/display/partners/MongoLab+Partner+Integration+API#MongoLabPartnerIntegrationAPI-Viewdatabase
// ### @options {Object} Set of options can be
// #### options['name'] {String} Name of the database to view (required)
// #### options['owner'] {String} Username of the database owner (required)
// ### @callback {Function} Continuation to respond to when complete.
exports.getDatabase = function getDatabase(options, callback) {
// Check for options
if (typeof options === 'function') {
return errs.handle(errs.create({
message: 'Options required for view a database.'
}), options);
}
// Check for name
if (!options['name']) {
return errs.handle(errs.create({
message: 'options. Name is a required argument.'
}), callback);
}
// Check for owner
if (!options['owner']) {
return errs.handle(errs.create({
message: 'options. Username of owner is a required argument.'
}), callback);
}
var path = ['accounts', options['owner'], 'databases', options['name']].join('/');
this._request({ path: path }, function (err, body) {
return err
? callback(err)
: callback(null, body);
});
};
// Delete a database
// ### @options {Object} Set of options can be
// #### options['name'] {String} Name of the database to view (required)
// #### options['owner'] {String} Username of the database owner (required)
// ### @callback {Function} Continuation to respond to when complete.
exports.remove = function remove(options, callback) {
// Check for options
if (typeof options === 'function') {
return errs.handle(errs.create({
message: 'Options required for delete a database.'
}), options);
}
// Check for name
if (!options['name']) {
return errs.handle(errs.create({
message: 'options. Name is a required argument.'
}), callback);
}
// Check for owner
if (!options['owner']) {
return errs.handle(errs.create({
message: 'options. Username of owner is a required argument.'
}), callback);
}
var deleteOptions = {
method: 'DELETE',
path: ['accounts', options['owner'], 'databases', options['name']].join('/')
};
this._request(deleteOptions, function (err) {
return err
? callback(err)
: callback(null);
});
};