Ejemplo n.º 1
0
// handleAdd parses the json in the request body and creates a configuration with the fields
// indicated in the json. If successful it sends a 200 code. If two configurations
//have the same name then it sends a 409 code with the configuration in the body of
// the response.
func (ch Handler) handleAdd(w http.ResponseWriter, r *http.Request) {
	config := configuration.Configuration{}
	err := json.NewDecoder(r.Body).Decode(&config)
	if err != nil {
		http.Error(w, "Bad Format", http.StatusBadRequest)
		return
	}

	configs, err := ch.Add(config)
	if configErr, ok := err.(configuration.Error); ok && configErr.Err == configuration.DuplicateConfigErr {
		response.WriteJson(w, http.StatusConflict, configuration.Configurations{[]configuration.Configuration{configErr.Configuration}})
		return
	}

	if err != nil {
		response.ServerError(w)
	}

	response.WriteJson(w, http.StatusOK, configuration.Configurations{configs})

}
Ejemplo n.º 2
0
// handleGet sends a list of configurations containing only one configuration
// whose name matches the name specified in the url with a 200 code. If no such
// configuration can be found sends a 404 code.
func (ch Handler) handleGet(w http.ResponseWriter, r *http.Request, configName string) {
	configs, err := ch.Get(configName)
	if err == configuration.DoesNotExistErr {
		http.Error(w, "", http.StatusNotFound)
		return
	}
	if err != nil {
		response.ServerError(w)
		return
	}

	response.WriteJson(w, http.StatusOK, configuration.Configurations{configs})
}
Ejemplo n.º 3
0
// handleGetAll sends a list of all the configurations with a 200 code
func (ch Handler) handleGetAll(w http.ResponseWriter, r *http.Request) {
	configs, err := ch.GetAll()
	if err != nil {
		response.ServerError(w)
		return
	}

	configs, err = handleParameters(w, r, configs)
	if err != nil {
		http.Error(w, "Bad Query String", http.StatusBadRequest)
	}

	response.WriteJson(w, http.StatusOK, configuration.Configurations{configs})
}
Ejemplo n.º 4
0
// handleModify modifies the configuration whose name matches the name specified
// in the url. If no such configuration exists sends a 404 code. If the modification
// would cause two configurations to have the same name then sends a 409 code with
// the configuration in the body of the response. If successful sends a 200 code/
func (ch Handler) handleModify(w http.ResponseWriter, r *http.Request, configName string) {
	config := configuration.Configuration{}
	err := json.NewDecoder(r.Body).Decode(&config)
	if err != nil {
		http.Error(w, "Bad Format", http.StatusBadRequest)
		return
	}

	config, err = ch.Modify(configName, config)

	if err == configuration.DoesNotExistErr {
		http.Error(w, "", http.StatusNotFound)
		return
	} else if confErr, ok := err.(configuration.Error); ok && confErr.Err == configuration.DuplicateConfigErr {
		response.WriteJson(w, http.StatusConflict, configuration.Configurations{[]configuration.Configuration{confErr.Configuration}})
		return
	} else if err != nil {
		response.ServerError(w)
		return
	}

	response.WriteJson(w, http.StatusOK, configuration.Configurations{[]configuration.Configuration{config}})
}