Beispiel #1
0
func putTopology(request *restful.Request, response *restful.Response) {
	topologyName := request.PathParameter("topology")

	topologyInput := &topology.Topology{}
	err := request.ReadEntity(&topologyInput)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Read body failure"
		jsonMap["ErrorMessage"] = err.Error()
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	if topologyName != topologyInput.Name {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Path parameter name is different from name in the body"
		jsonMap["path"] = topologyName
		jsonMap["body"] = topologyInput.Name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	oldTopology, _ := topology.GetStorage().LoadTopology(topologyInput.Name)
	if oldTopology == nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "The topology to update doesn't exist"
		jsonMap["name"] = topologyInput.Name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(404, string(errorMessageByteSlice))
		return
	}

	err = topology.GetStorage().SaveTopology(topologyInput)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Save topology failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["topologyInput"] = topologyInput
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}
}
Beispiel #2
0
func getAllTopology(request *restful.Request, response *restful.Response) {
	topologySlice, err := topology.GetStorage().LoadAllTopology()
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Get all topology failure"
		jsonMap["ErrorMessage"] = err.Error()
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}

	response.WriteJson(topologySlice, "[]Topology")
}
Beispiel #3
0
func postTopology(request *restful.Request, response *restful.Response) {
	topologyInput := &topology.Topology{}
	err := request.ReadEntity(&topologyInput)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Read body failure"
		jsonMap["ErrorMessage"] = err.Error()
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(400, string(errorMessageByteSlice))
		return
	}

	oldTopology, _ := topology.GetStorage().LoadTopology(topologyInput.Name)
	if oldTopology != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "The topology to create already exists"
		jsonMap["name"] = topologyInput.Name
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(409, string(errorMessageByteSlice))
		return
	}

	err = topology.GetStorage().SaveTopology(topologyInput)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Save topology failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["topologyInput"] = topologyInput
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}
}
Beispiel #4
0
func deleteTopology(request *restful.Request, response *restful.Response) {
	topologyName := request.PathParameter("topology")

	err := topology.GetStorage().DeleteTopology(topologyName)
	if err != nil {
		jsonMap := make(map[string]interface{})
		jsonMap["Error"] = "Delete topology failure"
		jsonMap["ErrorMessage"] = err.Error()
		jsonMap["topologyName"] = topologyName
		errorMessageByteSlice, _ := json.Marshal(jsonMap)
		log.Error(jsonMap)
		response.WriteErrorString(422, string(errorMessageByteSlice))
		return
	}
}