Exemple #1
0
func getConfigurationHandlerFunc(w http.ResponseWriter, r *http.Request) {
	handler := "resourcifier: get configuration"
	util.LogHandlerEntry(handler, r)
	rtype, err := getPathVariable(w, r, "type", handler)
	if err != nil {
		return
	}

	rname, err := getPathVariable(w, r, "name", handler)
	if err != nil {
		return
	}

	c := &common.Configuration{
		Resources: []*common.Resource{
			{Name: rname, Type: rtype},
		},
	}

	output, err := backend.Configure(c, configurator.GetOperation)
	if err != nil {
		util.LogAndReturnError(handler, http.StatusBadRequest, err, w)
		return
	}

	util.LogHandlerExit(handler, http.StatusOK, output, w)
	util.WriteYAML(handler, w, []byte(output), http.StatusOK)
}
Exemple #2
0
func getPathVariable(w http.ResponseWriter, r *http.Request, variable, handler string) (string, error) {
	vars := mux.Vars(r)
	escaped, ok := vars[variable]
	if !ok {
		e := fmt.Errorf("%s name not found in URL", variable)
		util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
		return "", e
	}

	unescaped, err := url.QueryUnescape(escaped)
	if err != nil {
		e := fmt.Errorf("cannot decode name (%v)", variable)
		util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
		return "", e
	}

	return unescaped, nil
}
Exemple #3
0
func getConfiguration(w http.ResponseWriter, r *http.Request, handler string) *common.Configuration {
	b := io.LimitReader(r.Body, *maxLength*1024)
	y, err := ioutil.ReadAll(b)
	if err != nil {
		util.LogAndReturnError(handler, http.StatusBadRequest, err, w)
		return nil
	}

	// Reject the input if it exceeded the length limit,
	// since we may not have read all of it into the buffer.
	if _, err = b.Read(make([]byte, 0, 1)); err != io.EOF {
		e := fmt.Errorf("configuration exceeds maximum length of %d KB", *maxLength)
		util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
		return nil
	}

	j, err := yaml.YAMLToJSON(y)
	if err != nil {
		e := errors.New(err.Error() + "\n" + string(y))
		util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
		return nil
	}

	c := &common.Configuration{}
	if err := json.Unmarshal(j, c); err != nil {
		e := errors.New(err.Error() + "\n" + string(j))
		util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
		return nil
	}

	if len(c.Resources) < 1 {
		e := fmt.Errorf("configuration is empty")
		util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
		return nil
	}

	return c
}
Exemple #4
0
func listDeploymentsHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error {
	handler := "manager: list deployments"
	util.LogHandlerEntry(handler, r)
	l, err := c.Manager.ListDeployments()
	if err != nil {
		util.LogAndReturnError(handler, http.StatusInternalServerError, err, w)
		return nil
	}
	var names []string
	for _, d := range l {
		names = append(names, d.Name)
	}

	util.LogHandlerExitWithJSON(handler, w, names, http.StatusOK)
	return nil
}
Exemple #5
0
func getDeploymentHandlerFunc(w http.ResponseWriter, r *http.Request, c *router.Context) error {
	handler := "manager: get deployment"
	util.LogHandlerEntry(handler, r)
	name, err := pos(w, r, 2)
	if err != nil {
		return nil
	}

	d, err := c.Manager.GetDeployment(name)
	if err != nil {
		util.LogAndReturnError(handler, http.StatusBadRequest, err, w)
		return nil
	}

	util.LogHandlerExitWithJSON(handler, w, d, http.StatusOK)
	return nil
}
Exemple #6
0
func putConfigurationHandlerFunc(w http.ResponseWriter, r *http.Request) {
	handler := "resourcifier: update configuration"
	util.LogHandlerEntry(handler, r)
	defer r.Body.Close()
	c := getConfiguration(w, r, handler)
	if c != nil {
		if _, err := backend.Configure(c, configurator.ReplaceOperation); err != nil {
			e := errors.New("cannot replace configuration: " + err.Error() + "\n")
			util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
			return
		}

		util.LogHandlerExitWithYAML(handler, w, c, http.StatusCreated)
		return
	}

	util.LogHandlerExit(handler, http.StatusOK, "OK", w)
}
Exemple #7
0
func createConfigurationHandlerFunc(w http.ResponseWriter, r *http.Request) {
	handler := "resourcifier: create configuration"
	util.LogHandlerEntry(handler, r)
	defer r.Body.Close()
	c := getConfiguration(w, r, handler)
	if c != nil {
		_, err := backend.Configure(c, configurator.CreateOperation)
		if err != nil {
			util.LogAndReturnError(handler, http.StatusBadRequest, err, w)
			return
		}

		util.LogHandlerExitWithYAML(handler, w, c, http.StatusCreated)
		return
	}

	util.LogHandlerExit(handler, http.StatusOK, "OK", w)
}
Exemple #8
0
func deleteConfigurationHandlerFunc(w http.ResponseWriter, r *http.Request) {
	handler := "resourcifier: delete configuration"
	util.LogHandlerEntry(handler, r)
	defer r.Body.Close()
	c := getConfiguration(w, r, handler)
	if c != nil {
		if _, err := backend.Configure(c, configurator.DeleteOperation); err != nil {
			e := errors.New("cannot delete configuration: " + err.Error() + "\n")
			util.LogAndReturnError(handler, http.StatusBadRequest, e, w)
			return
		}

		w.WriteHeader(http.StatusNoContent)
		util.LogHandlerExit(handler, http.StatusNoContent, "No Content", w)
		return
	}

	util.LogHandlerExit(handler, http.StatusOK, "OK", w)
}