Exemple #1
0
    def wrapper(*args, **kwargs):
        try:
            script = request.json['script']
        except KeyError:
            raise HTTPError(message="`state` is not provided in request",
                            status_code=400)

        if not isinstance(script, str):
            raise HTTPError(message="Script is malformed", status_code=400)
        return f(*args, **kwargs)
Exemple #2
0
 def wrapper(*args, **kwargs):
     try:
         # If the application content type is not set then
         # request.json is None
         if not request.json:
             raise HTTPError('JSON request or mimetype is missing',
                             status_code=400)
     except ValueError:
         body = request.body.read()
         log.debug('JSON request is malformed: {}'.format(body))
         raise HTTPError('JSON request is malformed', status_code=400)
     return f(*args, **kwargs)
Exemple #3
0
    def wrapper(*args, **kwargs):
        try:
            state = request.json['state']
        except KeyError:
            raise HTTPError(message="`state` is not provided in request",
                            status_code=400)

        one_of = ['agent', 'local', 'rescue']
        if state not in one_of:
            print('here')
            raise HTTPError(message='State must be one of {}'.format(
                ','.join(one_of)),
                            status_code=400)
        return f(*args, **kwargs)
Exemple #4
0
def create_job():
    """
    Creates a job with the given instructions.

    :return: The created job id.
    """
    instruction = request.json.get('instruction')
    if not isinstance(instruction, dict):
        raise HTTPError('Command is missing from request or is malformed',
                        status_code=400)
    query = request.json.get('query')
    if not isinstance(query, dict):
        raise HTTPError('Query is missing from request or is malformed',
                        status_code=400)
    job_id = rpc_client.create_job(query, instruction)

    if not job_id:
        raise HTTPError('Query did not match any active agents',
                        status_code=404)
    return jsonify(job_id)
Exemple #5
0
def get_task(task_id):
    """
    Query the RPC service for a task record with a given task_id.

    :param task_id: RPC task id.
    :return: Sinle RPC task object. 
    """
    task = rpc_client.get_task(task_id)
    if not task:
        raise HTTPError('Task not found', status_code=404)
    return jsonify(task)
Exemple #6
0
def get_job_status(job_id):
    """
    Get one Job by job_id.

    :param job_id: RPC job id.
    :return: Job status dictionary. 
    """
    job = rpc_client.get_job_status(job_id)
    if not job:
        raise HTTPError('Job {} does not exist'.format(job_id),
                        status_code=404)
    return jsonify(job)
Exemple #7
0
def get_job_task(job_id):
    """
    Query the RPC service for tasks associated to a given job_id.

    :param job_id: RPC job id.
    :return: List of task objects. 
    """
    projection = get_projection_from_qsa()
    tasks = rpc_client.get_job_tasks(job_id, projection)
    if tasks['count'] == 0:
        raise HTTPError('No tasks exist for job {}'.format(job_id),
                        status_code=404)
    return jsonify(tasks)
Exemple #8
0
def get_job(job_id):
    """
    Get the job status by job_id.

    :param job_id: RPC job id, default is None.
    :return: Job object. 
    """
    projection = get_projection_from_qsa()
    data = rpc_client.get_job(job_id, projection)
    if not data:
        raise HTTPError('Job {} does not exist'.format(job_id),
                        status_code=404)

    return jsonify(data)
Exemple #9
0
def get_inventory(mercury_id):
    """
    Get one inventory object by mercury_id.

    :param mercury_id: Device mercury id.
    :return: Inventory object.
    """
    projection = get_projection_from_qsa()
    data = inventory_client.get_one(mercury_id, projection=projection)
    if not data:
        message = 'mercury_id {} does not exist in inventory'
        raise HTTPError(message.format(mercury_id), status_code=404)

    return jsonify(data)
Exemple #10
0
 def wrapper(*args, **kwargs):
     if not isinstance(request.json.get('query'), dict):
         raise HTTPError('JSON request is malformed', status_code=400)
     return f(*args, **kwargs)