Beispiel #1
0
func poolUpdateHandler(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	allowed := permission.Check(t, permission.PermPoolUpdate)
	if !allowed {
		return permission.ErrUnauthorized
	}
	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}
	var params map[string]*bool
	err = json.Unmarshal(b, &params)
	if err != nil {
		return err
	}
	query := bson.M{}
	for k, v := range params {
		if v != nil {
			query[k] = *v
		}
	}
	poolName := r.URL.Query().Get(":name")
	forceDefault, _ := strconv.ParseBool(r.URL.Query().Get("force"))
	err = provision.PoolUpdate(poolName, query, forceDefault)
	if err != nil {
		if err == provision.ErrDefaultPoolAlreadyExists {
			return &terrors.HTTP{
				Code:    http.StatusPreconditionFailed,
				Message: "Default pool already exists.",
			}
		}
		return err
	}
	return nil
}
Beispiel #2
0
func poolUpdateHandler(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}
	var params provision.PoolUpdateOptions
	err = json.Unmarshal(b, &params)
	if err != nil {
		return err
	}
	params.Name = r.URL.Query().Get(":name")
	return provision.PoolUpdate(params)
}
Beispiel #3
0
// title: pool update
// path: /pools/{name}
// method: PUT
// consume: application/x-www-form-urlencoded
// responses:
//   200: Pool updated
//   401: Unauthorized
//   404: Pool not found
//   409: Default pool already defined
func poolUpdateHandler(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
	r.ParseForm()
	allowed := permission.Check(t, permission.PermPoolUpdate)
	if !allowed {
		return permission.ErrUnauthorized
	}
	poolName := r.URL.Query().Get(":name")
	evt, err := event.New(&event.Opts{
		Target:     event.Target{Type: event.TargetTypePool, Value: poolName},
		Kind:       permission.PermPoolUpdate,
		Owner:      t,
		CustomData: event.FormToCustomData(r.Form),
		Allowed:    event.Allowed(permission.PermPoolReadEvents, permission.Context(permission.CtxPool, poolName)),
	})
	if err != nil {
		return err
	}
	defer func() { evt.Done(err) }()
	dec := form.NewDecoder(nil)
	dec.IgnoreCase(true)
	dec.IgnoreUnknownKeys(true)
	var updateOpts provision.UpdatePoolOptions
	err = dec.DecodeValues(&updateOpts, r.Form)
	if err != nil {
		return &terrors.HTTP{
			Code:    http.StatusBadRequest,
			Message: err.Error(),
		}
	}
	err = provision.PoolUpdate(poolName, updateOpts)
	if err == provision.ErrPoolNotFound {
		return &terrors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
	}
	if err == provision.ErrDefaultPoolAlreadyExists {
		return &terrors.HTTP{
			Code:    http.StatusConflict,
			Message: err.Error(),
		}
	}
	return err
}