api/service_instance.go
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")
	updateData := struct {
		Description string
		Plan        string
		TeamOwner   string
		Tags        []string
	}{}
	err = ParseInput(r, &updateData)
	if err != nil {
		return err
	}
	tags, _ := InputValues(r, "tag")
	updateData.Tags = append(updateData.Tags, tags...) // for compatibility
	srv, err := getService(serviceName)
	if err != nil {
		return err
	}
	si, err := getServiceInstanceOrError(serviceName, instanceName)
	if err != nil {
		return err
	}
	var wantedPerms []*permission.PermissionScheme
	if updateData.Description != "" {
		wantedPerms = append(wantedPerms, permission.PermServiceInstanceUpdateDescription)
		si.Description = updateData.Description
	}
	if updateData.TeamOwner != "" {
		wantedPerms = append(wantedPerms, permission.PermServiceInstanceUpdateTeamowner)
		si.TeamOwner = updateData.TeamOwner
	}
	if updateData.Tags != nil {
		wantedPerms = append(wantedPerms, permission.PermServiceInstanceUpdateTags)
		si.Tags = updateData.Tags
	}
	if updateData.Plan != "" {
		wantedPerms = append(wantedPerms, permission.PermServiceInstanceUpdatePlan)
		si.PlanName = updateData.Plan
	}
	if len(wantedPerms) == 0 {
		return &tsuruErrors.HTTP{
			Code:    http.StatusBadRequest,
			Message: "Neither the description, team owner, tags or plan were set. You must define at least one.",
		}
	}
	for _, perm := range wantedPerms {
		allowed := permission.Check(t, perm,
			contextsForServiceInstance(si, serviceName)...,
		)
		if !allowed {
			return permission.ErrUnauthorized
		}
	}
	evt, err := event.New(&event.Opts{
		Target:     serviceInstanceTarget(serviceName, instanceName),
		Kind:       permission.PermServiceInstanceUpdate,
		Owner:      t,
		CustomData: event.FormToCustomData(InputFields(r)),
		Allowed: event.Allowed(permission.PermServiceInstanceReadEvents,
			contextsForServiceInstance(si, serviceName)...),
	})
	if err != nil {
		return err
	}
	defer func() { evt.Done(err) }()
	requestID := requestIDHeader(r)
	return si.Update(srv, *si, evt, requestID)
}
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")
	updateData := struct {
		Description string
		Plan        string
		TeamOwner   string
		Tags        []string
	}{}
	err = ParseInput(r, &updateData)
	if err != nil {
		return err
	}
	tags, _ := InputValues(r, "tag")
	updateData.Tags = append(updateData.Tags, tags...) // for compatibility
	srv, err := getService(serviceName)
	if err != nil {
		return err
	}
	si, err := getServiceInstanceOrError(serviceName, instanceName)
	if err != nil {
		return err
	}
	var wantedPerms []*permission.PermissionScheme
	if updateData.Description != "" {
		wantedPerms = append(wantedPerms, permission.PermServiceInstanceUpdateDescription)
		si.Description = updateData.Description
	}
	if updateData.TeamOwner != "" {
		wantedPerms = append(wantedPerms, permission.PermServiceInstanceUpdateTeamowner)
		si.TeamOwner = updateData.TeamOwner
	}
	if updateData.Tags != nil {
		wantedPerms = append(wantedPerms, permission.PermServiceInstanceUpdateTags)
		si.Tags = updateData.Tags
	}
	if updateData.Plan != "" {
		wantedPerms = append(wantedPerms, permission.PermServiceInstanceUpdatePlan)
		si.PlanName = updateData.Plan
	}
	if len(wantedPerms) == 0 {
		return &tsuruErrors.HTTP{
			Code:    http.StatusBadRequest,
			Message: "Neither the description, team owner, tags or plan were set. You must define at least one.",
		}
	}
	for _, perm := range wantedPerms {
		allowed := permission.Check(t, perm,
			contextsForServiceInstance(si, serviceName)...,
		)
		if !allowed {
			return permission.ErrUnauthorized
		}
	}
	evt, err := event.New(&event.Opts{
		Target:     serviceInstanceTarget(serviceName, instanceName),
		Kind:       permission.PermServiceInstanceUpdate,
		Owner:      t,
		CustomData: event.FormToCustomData(InputFields(r)),
		Allowed: event.Allowed(permission.PermServiceInstanceReadEvents,
			contextsForServiceInstance(si, serviceName)...),
	})
	if err != nil {
		return err
	}
	defer func() { evt.Done(err) }()
	requestID := requestIDHeader(r)
	return si.Update(srv, *si, evt, requestID)
}
// 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