def datastore_eval(TE, step): # TE.get_command_params returns the VALUES of each provided property # step.command is the raw XML of the command code = TE.get_command_params(step.command, "code")[0] target_var = TE.get_command_params(step.command, "variable")[0] # TE.replace_variables will look on the runtime variable stack, and replace any [[vars]] # defined in your property code = TE.replace_variables(code) try: """ PyMongo provides an 'eval' function for processing complex javascript operations directly on the database. """ from catocommon import catocommon db = catocommon.new_mongo_conn() result = db.eval(code) TE.logger.debug(result) TE.rt.set(target_var, result) # we should put the response in a variable catocommon.mongo_disconnect(db) except Exception as ex: # write the exception to the logfile TE.logger.critical("Exception in 'datastore_eval.\n%s" % (ex))
def __init__(self, cfilter=""): """ Instantiates a collections object containing all collections satisfying a given filter. Example usage: >>> # return all collections having the string 'test' in the name >>> colls = Collections("test") :param cfilter: collection filter to apply to collections to retrieve from the database Two attributes are created: _names: a list of just the collection names (for REST API output) _collections: a list of Mongo collection objects. """ try: db = catocommon.new_mongo_conn() names = db.collection_names() # filter by sfilter self._names = filter(lambda x: x.find(cfilter) >= 0, names) # strip out the Mongo "system." collections self._names = [x for x in self._names if "system." not in x] #print self._names self._collections = [] # create collections for n in self._names: self._collections.append(db[n]) except Exception as ex: raise DatastoreError(ex)
def find_document(_id=None, query={}, collection="default"): if _id: query['_id'] = _id if "_id" in query: _id = query["_id"] from bson.objectid import ObjectId query["_id"] = ObjectId(query["_id"]) if not collection: collection = "default" try: # connection should be later cleaned up: db = catocommon.new_mongo_conn() coll = db[collection] doc = coll.find_one(query) if doc: rdoc = Document() # convert from ObjectId rdoc.ID = str(doc['_id']) rdoc._collection_obj = coll rdoc.doc = doc else: raise DocumentNotFoundError('Document with id %s not found' % id) except Exception as ex: raise DatastoreError(ex) finally: # TODO: cleanup #db.close() pass return rdoc
def create_document(template, collection="default"): """ Creates a new Document based on the given the parameters. :type template: dict :param template: any JSON document :type template: string :param collection: collection name to use for creating the document. :rtype: Document Note: there is no enforcement of cato_object_id being unique. This is by design, as some documents may have the same cato_object_id and their uniqueness would be defined by additional arguments. raises DatastoreError """ if not collection: collection = "default" jsondoc = {} if template: # verify the template is a valid document try: jsondoc = json.loads(template) except ValueError: return None, "Template is not a valid JSON document." except TypeError: # NOTE: we're expecting template to be a JSON STRING # but it's possible it could also be a python dict in which case # a TypeError will occur. jsondoc = template db = catocommon.new_mongo_conn() coll = db[collection] docid = coll.insert(jsondoc) catocommon.mongo_disconnect(db) # needed to add a incremental backoff retry loop because of mongo's eventual consistancy ii = 1 while True: doc = Document({'_id': docid}, collection) if doc.ID: break elif ii == 5: break else: del(doc) sleep(ii * .1) if doc.ID: return doc, None else: return None, "Document was not created or could not be found by _id=%s." % docid
def __init__(self, collection=None, query={}): """Instantiates a set of documents in a collection satisfying a given filter. Note: The current implementation may results in memory problems for huge collections, if filter is such that it returns many documents. Example usage: >>> query = {'guid': '3678df6e-120d-11e2-a0f5-58b035f767f3'} >>> docs = Documents(query) :type collection: string :param collection: collection name to use for creating the document. :type query: dict :param query: document filter to apply when retrieving documents from collection. The filter is in the native mongodb format. """ if not collection: collection = "default" self.collection = collection self._collection_obj = None if not query: query = {} try: # _id must be manipulated from a string into an ObjectId _id = query.get("_id") if _id: from bson.objectid import ObjectId query["_id"] = ObjectId(_id) db = catocommon.new_mongo_conn() coll = db[self.collection] cursor = coll.find(query) self.documents = [] for d in cursor: #d['_id'] = str(d['_id']) self.documents.append(d) except Exception as ex: raise DatastoreError(ex.__str__())
def __init__(self, query={}, collection="default", projection=None): self.ID = None self.collection = collection self._collection_obj = None # support empty query temporarily for find_document if not query: return # if the query includes _id, cast it to an ObjectId not a string. if "_id" in query: from bson.objectid import ObjectId query["_id"] = ObjectId(query["_id"]) if not collection: collection = "default" self.doc = None try: # connection should be later cleaned up: db = catocommon.new_mongo_conn() coll = db[collection] self._collection_obj = coll doc = coll.find_one(query, projection) if doc is not None: # convert from ObjectId self.ID = str(doc['_id']) self.doc = doc else: logger.critical("Unable to find Datastore document using query %s." % query) self.doc = {} except Exception as ex: raise DatastoreError(ex) finally: # TODO: cleanup #db.close() pass