示例#1
0
// title: service instance update
// path: /services/{service}/instances/{instance}
// method: PUT
// consume: application/x-www-form-urlencoded
// responses:
//   200: Service instance updated
//   400: Invalid data
//   401: Unauthorized
//   404: Service instance not found
func updateServiceInstance(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	serviceName := r.URL.Query().Get(":service")
	instanceName := r.URL.Query().Get(":instance")
	description := r.FormValue("description")
	if description == "" {
		return &errors.HTTP{
			Code:    http.StatusBadRequest,
			Message: "Invalid value for description",
		}
	}
	si, err := getServiceInstanceOrError(serviceName, instanceName)
	if err != nil {
		return err
	}
	allowed := permission.Check(t, permission.PermServiceInstanceUpdateDescription,
		permission.Context(permission.CtxServiceInstance, si.Name),
	)
	if !allowed {
		return permission.ErrUnauthorized
	}
	user, err := t.User()
	if err != nil {
		return err
	}
	rec.Log(user.Email, "update-service-instance", "description="+description)
	si.Description = description
	return service.UpdateService(si)
}
示例#2
0
// title: service instance update
// path: /services/{service}/instances/{instance}
// method: PUT
// consume: application/x-www-form-urlencoded
// responses:
//   200: Service instance updated
//   400: Invalid data
//   401: Unauthorized
//   404: Service instance not found
func updateServiceInstance(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
	serviceName := r.URL.Query().Get(":service")
	instanceName := r.URL.Query().Get(":instance")
	description := r.FormValue("description")
	if description == "" {
		return &tsuruErrors.HTTP{
			Code:    http.StatusBadRequest,
			Message: "Invalid value for description",
		}
	}
	si, err := getServiceInstanceOrError(serviceName, instanceName)
	if err != nil {
		return err
	}
	allowed := permission.Check(t, permission.PermServiceInstanceUpdateDescription,
		contextsForServiceInstance(si, serviceName)...,
	)
	if !allowed {
		return permission.ErrUnauthorized
	}
	evt, err := event.New(&event.Opts{
		Target:     serviceInstanceTarget(serviceName, instanceName),
		Kind:       permission.PermServiceInstanceUpdateDescription,
		Owner:      t,
		CustomData: event.FormToCustomData(r.Form),
		Allowed: event.Allowed(permission.PermServiceInstanceReadEvents,
			contextsForServiceInstance(si, serviceName)...),
	})
	if err != nil {
		return err
	}
	defer func() { evt.Done(err) }()
	si.Description = description
	return service.UpdateService(si)
}
示例#3
0
func updateServiceInstance(w http.ResponseWriter, r *http.Request, t auth.Token) error {
	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}
	var body map[string]string
	err = json.Unmarshal(b, &body)
	if err != nil {
		return err
	}
	serviceName := r.URL.Query().Get(":service")
	instanceName := r.URL.Query().Get(":instance")
	description, ok := body["description"]
	if !ok || description == "" {
		return &errors.HTTP{
			Code:    http.StatusBadRequest,
			Message: "Invalid value for description",
		}
	}
	si, err := getServiceInstanceOrError(serviceName, instanceName)
	if err != nil {
		return err
	}
	allowed := permission.Check(t, permission.PermServiceInstanceUpdateDescription,
		permission.Context(permission.CtxServiceInstance, si.Name),
	)
	if !allowed {
		return permission.ErrUnauthorized
	}
	user, err := t.User()
	if err != nil {
		return err
	}
	rec.Log(user.Email, "update-service-instance", string(b))
	si.Description = description
	return service.UpdateService(si)
}