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