Ejemplo n.º 1
0
def list_allocations():
    """
    Lists allocations.
    ---
    tags:
      - allocation
    parameters:
      - name: show
        in: query
        type: string
        description: Show different allocations
        default: current
        enum: [all, current, finished]
    responses:
      200:
        description: Allocations of Packet Tracer instances
        schema:
            properties:
                allocations:
                    type: array
                    items:
                      $ref: '#/definitions/allocate_instance_post_Allocation'
    """
    show_param = request.args.get("show")
    if show_param is None or show_param == "current":  # default option
        return get_json_allocations(Allocation.get_current())
    else:
        if show_param not in ("all", "current", "finished"):
            return BadRequest("The 'show' parameter must contain one of the following values: all, running or finished.")

        if show_param == "all":
            return get_json_allocations(Allocation.get_all())  # .limit(10)
        else:  # show_param is "finished":
            return get_json_allocations(Allocation.get_finished())
Ejemplo n.º 2
0
def show_allocation_details(allocation_id):
    """
    Shows the details of a Packet Tracer instance allocation.
    ---
    tags:
      - allocation
    parameters:
      - name: allocation_id
        in: path
        type: integer
        description: allocation identifier
        required: true
    responses:
      200:
        description: Details of the instance allocation.
        schema:
            $ref: '#/definitions/allocate_instance_post_Allocation'
      404:
        description: There is not an allocation for the given allocation_id.
        schema:
            $ref: '#/definitions/allocate_instance_post_Error'
    """
    allocation = Allocation.get(allocation_id)
    if allocation:
        return jsonify(allocation.serialize(request.base_url, get_host()))
    return not_found(error="The allocation does not exist.")
Ejemplo n.º 3
0
def allocate_instance():
    """
    Allocates a Packet Tracer instance.
    ---
    tags:
        - allocation
    responses:
        201:
            description: Packet Tracer instance allocated (i.e., allocation created)
            schema:
                id: Allocation
                properties:
                    id:
                        type: integer
                        description: Identifier of the allocation
                    url:
                        type: string
                        description: URL to handle the allocation
                    packetTracer:
                        type: string
                        description: Host and port where the Packet Tracer instance can be contacted (through IPC)
                    createdAt:
                        type: string
                        format: date-time
                        description: When was the allocation created?
                    deletedAt:
                        type: string
                        format: date-time
                        description: When was the allocation removed/stopped?
        500:
            description: The instance could not be allocated, there was an error.
            schema:
                id: Error
                properties:
                    status:
                        type: integer
                        description: HTTP status code.
                    message:
                        type: string
                        description: Description for the error.
        503:
            description: At the moment the server cannot allocate more instances.
            schema:
                $ref: '#/definitions/allocate_instance_post_Error'
    """
    try:
        result = tasks.allocate_instance.apply_async()
        allocation_id = result.get()
        if allocation_id:
            allocation = Allocation.get(allocation_id)
            return jsonify(
                allocation.serialize(
                    "%s/%d" % (request.base_url, allocation.id), get_host()))
        return unavailable()
    except TaskRevokedError:
        return unavailable('timeout got during instance allocation')
    except InsufficientResourcesError as ire:
        return unavailable(ire.args[0])
    except DockerContainerError as e:
        return internal_error(e.args[0])
Ejemplo n.º 4
0
def show_allocation_details(allocation_id):
    """
    Shows the details of a Packet Tracer instance allocation.
    ---
    tags:
      - allocation
    parameters:
      - name: allocation_id
        in: path
        type: integer
        description: allocation identifier
        required: true
    responses:
      200:
        description: Details of the instance allocation.
        schema:
            $ref: '#/definitions/allocate_instance_post_Allocation'
      404:
        description: There is not an allocation for the given allocation_id.
        schema:
            $ref: '#/definitions/allocate_instance_post_Error'
    """
    allocation = Allocation.get(allocation_id)
    if allocation:
        return jsonify(allocation.serialize(request.base_url, get_host()))
    return not_found(error="The allocation does not exist.")
Ejemplo n.º 5
0
def allocate_instance():
    """
    Allocates a Packet Tracer instance.
    ---
    tags:
        - allocation
    responses:
        201:
            description: Packet Tracer instance allocated (i.e., allocation created)
            schema:
                id: Allocation
                properties:
                    id:
                        type: integer
                        description: Identifier of the allocation
                    url:
                        type: string
                        description: URL to handle the allocation
                    packetTracer:
                        type: string
                        description: Host and port where the Packet Tracer instance can be contacted (through IPC)
                    createdAt:
                        type: string
                        format: date-time
                        description: When was the allocation created?
                    deletedAt:
                        type: string
                        format: date-time
                        description: When was the allocation removed/stopped?
        500:
            description: The instance could not be allocated, there was an error.
            schema:
                id: Error
                properties:
                    status:
                        type: integer
                        description: HTTP status code.
                    message:
                        type: string
                        description: Description for the error.
        503:
            description: At the moment the server cannot allocate more instances.
            schema:
                $ref: '#/definitions/allocate_instance_post_Error'
    """
    try:
        result = tasks.allocate_instance.apply_async()
        allocation_id = result.get()
        if allocation_id:
            allocation = Allocation.get(allocation_id)
            return jsonify(allocation.serialize("%s/%d" % (request.base_url, allocation.id), get_host()))
        return unavailable()
    except TaskRevokedError:
        return unavailable('timeout got during instance allocation')
    except InsufficientResourcesError as ire:
        return unavailable(ire.args[0])
    except DockerContainerError as e:
        return internal_error(e.args[0])
Ejemplo n.º 6
0
def list_allocations():
    """
    Lists allocations.
    ---
    tags:
      - allocation
    parameters:
      - name: show
        in: query
        type: string
        description: Show different allocations
        default: current
        enum: [all, current, finished]
    responses:
      200:
        description: Allocations of Packet Tracer instances
        schema:
            properties:
                allocations:
                    type: array
                    items:
                      $ref: '#/definitions/allocate_instance_post_Allocation'
    """
    show_param = request.args.get("show")
    if show_param is None or show_param == "current":  # default option
        return get_json_allocations(Allocation.get_current())
    else:
        if show_param not in ("all", "current", "finished"):
            return BadRequest(
                "The 'show' parameter must contain one of the following values: all, running or finished."
            )

        if show_param == "all":
            return get_json_allocations(Allocation.get_all())  # .limit(10)
        else:  # show_param is "finished":
            return get_json_allocations(Allocation.get_finished())
Ejemplo n.º 7
0
def deallocate_instance(allocation_id):
    """
    Stops a running Packet Tracer instance.
    ---
    tags:
      - allocation
    parameters:
      - name: allocation_id
        in: path
        type: integer
        description: allocation identifier
        required: true
    responses:
      200:
          description: Allocation removed
          schema:
              $ref: '#/definitions/allocate_instance_post_Allocation'
      404:
          description: There is not an allocation for the given allocation_id.
          schema:
              $ref: '#/definitions/allocate_instance_post_Error'
    """
    instance = Instance.get_by_allocation_id(allocation_id)
    if not instance:
        return not_found(error="The allocation does not exist.")

    try:
        allocation_id = instance.allocated_by
        result = tasks.deallocate_instance.apply_async(args=(instance.id, ))
        result.get()
        allocation = Allocation.get(allocation_id)
        if allocation:
            # TODO update instance object as status has changed
            return jsonify(allocation.serialize(request.base_url, get_host()))
        # else
        return not_found(error="The allocation does not exist.")
    except Exception as e:
        return internal_error(e.args[0])
Ejemplo n.º 8
0
def deallocate_instance(allocation_id):
    """
    Stops a running Packet Tracer instance.
    ---
    tags:
      - allocation
    parameters:
      - name: allocation_id
        in: path
        type: integer
        description: allocation identifier
        required: true
    responses:
      200:
          description: Allocation removed
          schema:
              $ref: '#/definitions/allocate_instance_post_Allocation'
      404:
          description: There is not an allocation for the given allocation_id.
          schema:
              $ref: '#/definitions/allocate_instance_post_Error'
    """
    instance = Instance.get_by_allocation_id(allocation_id)
    if not instance:
        return not_found(error="The allocation does not exist.")

    try:
        allocation_id = instance.allocated_by
        result = tasks.deallocate_instance.apply_async(args=(instance.id,))
        result.get()
        allocation = Allocation.get(allocation_id)
        if allocation:
            # TODO update instance object as status has changed
            return jsonify(allocation.serialize(request.base_url, get_host()))
        # else
        return not_found(error="The allocation does not exist.")
    except Exception as e:
        return internal_error(e.args[0])