Ejemplo n.º 1
0
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))
Ejemplo n.º 2
0
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