示例#1
0
func ExampleSet() {
	err := quota.Create("*****@*****.**", 3)
	if err != nil {
		panic(err)
	}
	quota.Reserve("*****@*****.**", "me/0")
	quota.Reserve("*****@*****.**", "me/1")
	quota.Reserve("*****@*****.**", "me/2")
	quota.Set("*****@*****.**", 2)
	quota.Reserve("*****@*****.**", "me/3") // ErrQuotaExceeded
	quota.Release("*****@*****.**", "me/2")
	quota.Reserve("*****@*****.**", "me/3") // ErrQuotaExceeded
	quota.Release("*****@*****.**", "me/1")
	quota.Reserve("*****@*****.**", "me/3") // Everything is ok now
}
示例#2
0
文件: app.go 项目: richardjoo/tsuru
// RemoveUnits removes n units from the app. It's a process composed of x
// steps:
//
//     1. Remove units from the provisioner
//     2. Unbind units from service instances bound to the app
//     3. Remove units from the app list
//     4. Update the app in the database
func (app *App) RemoveUnits(n uint) error {
	if n == 0 {
		return stderr.New("Cannot remove zero units.")
	} else if l := uint(len(app.Units)); l == n {
		return stderr.New("Cannot remove all units from an app.")
	} else if n > l {
		return fmt.Errorf("Cannot remove %d units from this app, it has only %d units.", n, l)
	}
	var (
		removed []int
		err     error
	)
	units := UnitSlice(app.Units)
	sort.Sort(units)
	items := make([]string, int(n))
	for i := 0; i < int(n); i++ {
		err = Provisioner.RemoveUnit(app, units[i].GetName())
		if err == nil {
			removed = append(removed, i)
		}
		app.unbindUnit(&units[i])
		items[i] = units[i].QuotaItem
	}
	if len(removed) == 0 {
		return err
	}
	conn, err := db.Conn()
	if err != nil {
		return err
	}
	defer conn.Close()
	app.removeUnits(removed)
	dbErr := conn.Apps().Update(
		bson.M{"name": app.Name},
		bson.M{"$set": bson.M{"units": app.Units}},
	)
	quota.Release(app.Name, items...)
	if err == nil {
		return dbErr
	}
	return err
}
示例#3
0
func (s *S) TestReserveUserAppForwardAppNotPointer(c *gocheck.C) {
	user := auth.User{Email: "*****@*****.**"}
	err := quota.Create(user.Email, 1)
	c.Assert(err, gocheck.IsNil)
	defer quota.Delete(user.Email)
	app := App{
		Name:     "clap",
		Platform: "django",
	}
	expected := map[string]string{"user": user.Email, "app": app.Name}
	previous, err := reserveUserApp.Forward(action.FWContext{Params: []interface{}{app, user}})
	c.Assert(err, gocheck.IsNil)
	c.Assert(previous, gocheck.DeepEquals, expected)
	err = quota.Reserve(user.Email, "another-app")
	_, ok := err.(*quota.QuotaExceededError)
	c.Assert(ok, gocheck.Equals, true)
	err = quota.Release(user.Email, app.Name)
	c.Assert(err, gocheck.IsNil)
	err = quota.Reserve(user.Email, "another-app")
	c.Assert(err, gocheck.IsNil)
}
示例#4
0
文件: app.go 项目: nemx/tsuru
// Delete deletes an app.
//
// Delete an app is a process composed of four steps:
//
//       1. Destroy the bucket and S3 credentials (if bucket-support is
//       enabled).
//       2. Destroy the app unit using juju
//       3. Unbind all service instances from the app
//       4. Remove the app from the database
func Delete(app *App) error {
	gURL := repository.ServerURL()
	(&gandalf.Client{Endpoint: gURL}).RemoveRepository(app.Name)
	useS3, _ := config.GetBool("bucket-support")
	if useS3 {
		destroyBucket(app)
	}
	if len(app.Units) > 0 {
		Provisioner.Destroy(app)
		app.unbind()
	}
	token := app.Env["TSURU_APP_TOKEN"].Value
	auth.DeleteToken(token)
	quota.Release(app.Owner, app.Name)
	conn, err := db.Conn()
	if err != nil {
		return err
	}
	defer conn.Close()
	quota.Delete(app.Name)
	return conn.Apps().Remove(bson.M{"name": app.Name})
}
示例#5
0
		switch ctx.Params[1].(type) {
		case auth.User:
			user = ctx.Params[1].(auth.User)
		case *auth.User:
			user = *ctx.Params[1].(*auth.User)
		default:
			return nil, errors.New("Third parameter must be auth.User or *auth.User.")
		}
		if err := quota.Reserve(user.Email, app.Name); err != nil && err != quota.ErrQuotaNotFound {
			return nil, err
		}
		return map[string]string{"app": app.Name, "user": user.Email}, nil
	},
	Backward: func(ctx action.BWContext) {
		m := ctx.FWResult.(map[string]string)
		quota.Release(m["user"], m["app"])
	},
	MinParams: 2,
}

var createAppQuota = action.Action{
	Name: "create-app-quota",
	Forward: func(ctx action.FWContext) (action.Result, error) {
		var app App
		switch ctx.Params[0].(type) {
		case App:
			app = ctx.Params[0].(App)
		case *App:
			app = *ctx.Params[0].(*App)
		default:
			return nil, errors.New("First parameter must be App or *App.")