12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- from pymongo import Connection , ASCENDING , DESCENDING
15
+ from pymongo import connection , Connection , ASCENDING , DESCENDING
16
16
from pymongo .son import SON
17
- from pymongo .errors import ConnectionFailure , OperationFailure , AutoReconnect
17
+ from pymongo .errors import ConnectionFailure , ConfigurationError , OperationFailure , AutoReconnect
18
18
from bson import json_util
19
19
20
20
import re
@@ -41,25 +41,21 @@ def __init__(self, mongos):
41
41
42
42
self ._connect (args , out .ostream , name = name )
43
43
44
- def _get_connection (self , name = None , host = None , port = None ):
44
+ def _get_connection (self , name = None , uri = 'mongodb://localhost:27017' ):
45
45
if name == None :
46
46
name = "default"
47
47
48
48
if name in self .connections :
49
49
return self .connections [name ]
50
-
51
- if port == None :
52
- port = 27107
53
-
50
+
54
51
try :
55
- connection = Connection (host = host , port = port , network_timeout = 2 )
56
- except ConnectionFailure :
52
+ connection = Connection (uri , network_timeout = 2 )
53
+ except ( ConnectionFailure , ConfigurationError ) :
57
54
return None
58
55
59
56
self .connections [name ] = connection
60
57
return connection
61
58
62
-
63
59
def _get_host_and_port (self , server ):
64
60
host = "localhost"
65
61
port = 27017
@@ -146,7 +142,7 @@ def _status(self, args, out, name = None, db = None, collection = None):
146
142
result ['connections' ][name ] = "%s:%d" % (conn .host , conn .port )
147
143
148
144
out (json .dumps (result ))
149
-
145
+
150
146
def _connect (self , args , out , name = None , db = None , collection = None ):
151
147
"""
152
148
connect to a mongod
@@ -156,21 +152,56 @@ def _connect(self, args, out, name = None, db = None, collection = None):
156
152
out ('{"ok" : 0, "errmsg" : "_connect must be a POST request"}' )
157
153
return
158
154
159
- host = "localhost"
160
- port = 27017
161
155
if "server" in args :
162
- (host , port ) = self ._get_host_and_port (args .getvalue ('server' ))
156
+ try :
157
+ uri = args .getvalue ('server' )
158
+ info = connection ._parse_uri (uri )
159
+ except Exception , e :
160
+ print uri
161
+ print e
162
+ out ('{"ok" : 0, "errmsg" : "invalid server uri given", "server" : "%s"}' % uri )
163
+ return
164
+ else :
165
+ uri = 'mongodb://localhost:27017'
163
166
164
167
if name == None :
165
168
name = "default"
166
169
167
- conn = self ._get_connection (name , host , port )
170
+ conn = self ._get_connection (name , uri )
168
171
if conn != None :
169
- out ('{"ok" : 1, "host " : "%s", "port" : %d, " name" : "%s"}' % (host , port , name ))
172
+ out ('{"ok" : 1, "server " : "%s", "name" : "%s"}' % (uri , name ))
170
173
else :
171
- out ('{"ok" : 0, "errmsg" : "could not connect", "host" : "%s", "port" : %d, "name" : "%s"}' % (host , port , name ))
174
+ out ('{"ok" : 0, "errmsg" : "could not connect", "server" : "%s", "name" : "%s"}' % (uri , name ))
175
+
176
+ def _authenticate (self , args , out , name = None , db = None , collection = None ):
177
+ """
178
+ authenticate to the database.
179
+ """
180
+
181
+ if type (args ).__name__ == 'dict' :
182
+ out ('{"ok" : 0, "errmsg" : "_find must be a POST request"}' )
183
+ return
172
184
185
+ conn = self ._get_connection (name )
186
+ if conn == None :
187
+ out ('{"ok" : 0, "errmsg" : "couldn\' t get connection to mongo"}' )
188
+ return
173
189
190
+ if db == None :
191
+ out ('{"ok" : 0, "errmsg" : "db must be defined"}' )
192
+ return
193
+
194
+ if not 'username' in args :
195
+ out ('{"ok" : 0, "errmsg" : "username must be defined"}' )
196
+
197
+ if not 'password' in args :
198
+ out ('{"ok" : 0, "errmsg" : "password must be defined"}' )
199
+
200
+ if not conn [db ].authenticate (args .getvalue ('username' ), args .getvalue ('password' )):
201
+ out ('{"ok" : 0, "errmsg" : "authentication failed"}' )
202
+ else :
203
+ out ('{"ok" : 1}' )
204
+
174
205
def _find (self , args , out , name = None , db = None , collection = None ):
175
206
"""
176
207
query the database.
0 commit comments