Exemplo n.º 1
0
// @app.route(api_root + '/events/<entity_id>/<event_uuid>', methods=['POST'])
func eventsOnPost(w http.ResponseWriter, r *http.Request) {
	// get entity structure
	entity, _, err := entityFromHttpRequest(r)
	if err != nil {
		restutil.WriteError(w, err)
		return
	}
	// instantiate event
	event, err := newEventFromHttpRequest(r)
	if err != nil {
		restutil.WriteError(w, err)
		return
	}
	eventUUID := mux.Vars(r)["event_uuid"]
	if eventUUID != event.ID() {
		err := fmt.Errorf("uuid mismatch: %s vs %s", eventUUID, event.ID())
		restutil.WriteError(w, err)
		return
	}
	// send event to orchestrator main
	go sendEventToMain(entity, event)
	// return empty json
	if err = restutil.WriteJSON(w, map[string]interface{}{}); err != nil {
		restutil.WriteError(w, err)
	}
}
Exemplo n.º 2
0
// @app.route(api_root + '/actions/<entity_id>/<action_uuid>', methods=['DELETE'])
//
// NOTE: DELETE must be idempotent (RFC 7231)
func actionsOnDelete(w http.ResponseWriter, r *http.Request) {
	var err error
	// get entity structure
	_, queue, err := entityFromHttpRequest(r)
	if err != nil {
		restutil.WriteError(w, err)
		return
	}
	// delete the action
	actionUUID := mux.Vars(r)["action_uuid"]
	queue.Delete(actionUUID)
	// return empty json
	if err = restutil.WriteJSON(w, map[string]interface{}{}); err != nil {
		restutil.WriteError(w, err)
	}
}
Exemplo n.º 3
0
// @app.route(api_root + '/actions/<entity_id>', methods=['GET'])
//
// NOTE: an inspector has responsibility to DELETE the action, because GET must be idempotent (RFC 7231)
//
// NOTE: there should not be any concurrent GETs for an entity_id
func actionsOnGet(w http.ResponseWriter, r *http.Request) {
	var err error
	// get entity structure
	_, queue, err := entityFromHttpRequest(r)
	if err != nil {
		restutil.WriteError(w, err)
		return
	}

	// NOTE: if NumGoroutine() increases rapidly, something is going wrong.
	log.Debugf("runtime.NumGoroutine()=%d", runtime.NumGoroutine())

	// get action (this can take a while, depending on the exploration policy)
	action := queue.Peek()
	if action == nil {
		restutil.WriteError(w, fmt.Errorf("could not get action"))
		return
	}
	if err = restutil.WriteJSON(w, action.JSONMap()); err != nil {
		restutil.WriteError(w, err)
	}
}