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.")
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])
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])
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])
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])