本文整理汇总了Golang中github.com/tsuru/tsuru/app.Plan类的典型用法代码示例。如果您正苦于以下问题:Golang Plan类的具体用法?Golang Plan怎么用?Golang Plan使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Plan类的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: addPlan
func addPlan(w http.ResponseWriter, r *http.Request, t auth.Token) error {
var plan app.Plan
err := json.NewDecoder(r.Body).Decode(&plan)
if err != nil {
return &errors.HTTP{
Code: http.StatusBadRequest,
Message: "unable to parse request body",
}
}
err = plan.Save()
if _, ok := err.(app.PlanValidationError); ok {
return &errors.HTTP{
Code: http.StatusBadRequest,
Message: err.Error(),
}
}
if err == app.ErrPlanAlreadyExists {
return &errors.HTTP{
Code: http.StatusConflict,
Message: err.Error(),
}
}
if err == nil {
w.WriteHeader(http.StatusCreated)
}
return err
}
示例2: SetUpTest
func (s *S) SetUpTest(c *check.C) {
routertest.FakeRouter.Reset()
rand.Seed(0)
config.Set("swarm:swarm-port", 0)
err := dbtest.ClearAllCollections(s.conn.Apps().Database)
c.Assert(err, check.IsNil)
err = provision.AddPool(provision.AddPoolOptions{Name: "bonehunters", Default: true, Provisioner: "swarm"})
c.Assert(err, check.IsNil)
p := app.Plan{
Name: "default",
Router: "fake",
Default: true,
CpuShare: 100,
}
err = p.Save()
c.Assert(err, check.IsNil)
s.p = &swarmProvisioner{}
err = s.p.Initialize()
c.Assert(err, check.IsNil)
s.user = &auth.User{Email: "[email protected]", Password: "123456", Quota: quota.Unlimited}
nativeScheme := auth.ManagedScheme(native.NativeScheme{})
app.AuthScheme = nativeScheme
_, err = nativeScheme.Create(s.user)
c.Assert(err, check.IsNil)
s.team = &auth.Team{Name: "admin"}
c.Assert(err, check.IsNil)
err = s.conn.Teams().Insert(s.team)
c.Assert(err, check.IsNil)
s.token, err = nativeScheme.Login(map[string]string{"email": s.user.Email, "password": "123456"})
c.Assert(err, check.IsNil)
}
示例3: addPlan
// title: plan create
// path: /plans
// method: POST
// consume: application/x-www-form-urlencoded
// responses:
// 201: Plan created
// 400: Invalid data
// 401: Unauthorized
// 409: Plan already exists
func addPlan(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
cpuShare, _ := strconv.Atoi(r.FormValue("cpushare"))
isDefault, _ := strconv.ParseBool(r.FormValue("default"))
memory := getSize(r.FormValue("memory"))
swap := getSize(r.FormValue("swap"))
plan := app.Plan{
Name: r.FormValue("name"),
Memory: memory,
Swap: swap,
CpuShare: cpuShare,
Default: isDefault,
Router: r.FormValue("router"),
}
allowed := permission.Check(t, permission.PermPlanCreate)
if !allowed {
return permission.ErrUnauthorized
}
evt, err := event.New(&event.Opts{
Target: event.Target{Type: event.TargetTypePlan, Value: plan.Name},
Kind: permission.PermPlanCreate,
Owner: t,
CustomData: event.FormToCustomData(r.Form),
Allowed: event.Allowed(permission.PermPlanReadEvents),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
err = plan.Save()
if _, ok := err.(app.PlanValidationError); ok {
return &errors.HTTP{
Code: http.StatusBadRequest,
Message: err.Error(),
}
}
if err == app.ErrPlanAlreadyExists {
return &errors.HTTP{
Code: http.StatusConflict,
Message: err.Error(),
}
}
if err == app.ErrLimitOfMemory || err == app.ErrLimitOfCpuShare {
return &errors.HTTP{
Code: http.StatusBadRequest,
Message: err.Error(),
}
}
if err == nil {
w.WriteHeader(http.StatusCreated)
}
return err
}
示例4: TestChangePlanNotFound
func (s *S) TestChangePlanNotFound(c *check.C) {
plan := app.Plan{Name: "superplan", Memory: 268435456, Swap: 268435456, CpuShare: 100}
err := plan.Save()
c.Assert(err, check.IsNil)
defer app.PlanRemove(plan.Name)
a := app.App{Name: "someapp", Platform: "zend", Teams: []string{s.team.Name}, Plan: plan}
err = app.CreateApp(&a, s.user)
c.Assert(err, check.IsNil)
defer s.deleteApp(&a)
defer s.logConn.Logs(a.Name).DropCollection()
request, err := http.NewRequest("POST", "/apps/someapp/plan", strings.NewReader(`{"name":"hiperplan"}`))
c.Assert(err, check.IsNil)
request.Header.Set("Authorization", "bearer "+s.token.GetValue())
recorder := httptest.NewRecorder()
m := RunServer(true)
m.ServeHTTP(recorder, request)
c.Check(recorder.Code, check.Equals, http.StatusNotFound)
c.Check(recorder.Body.String(), check.Equals, app.ErrPlanNotFound.Error()+"\n")
}
示例5: addPlan
func addPlan(w http.ResponseWriter, r *http.Request, t auth.Token) error {
cpuShare, _ := strconv.Atoi(r.FormValue("cpushare"))
isDefault, _ := strconv.ParseBool(r.FormValue("default"))
memory := getSize(r.FormValue("memory"))
swap := getSize(r.FormValue("swap"))
plan := app.Plan{
Name: r.FormValue("name"),
Memory: memory,
Swap: swap,
CpuShare: cpuShare,
Default: isDefault,
Router: r.FormValue("router"),
}
allowed := permission.Check(t, permission.PermPlanCreate)
if !allowed {
return permission.ErrUnauthorized
}
err := plan.Save()
if _, ok := err.(app.PlanValidationError); ok {
return &errors.HTTP{
Code: http.StatusBadRequest,
Message: err.Error(),
}
}
if err == app.ErrPlanAlreadyExists {
return &errors.HTTP{
Code: http.StatusConflict,
Message: err.Error(),
}
}
if err == app.ErrLimitOfMemory || err == app.ErrLimitOfCpuShare {
return &errors.HTTP{
Code: http.StatusBadRequest,
Message: err.Error(),
}
}
if err == nil {
w.WriteHeader(http.StatusCreated)
}
return err
}
示例6: addPlan
func addPlan(w http.ResponseWriter, r *http.Request, t auth.Token) error {
var plan app.Plan
err := json.NewDecoder(r.Body).Decode(&plan)
if err != nil {
return &errors.HTTP{
Code: http.StatusBadRequest,
Message: "unable to parse request body",
}
}
allowed := permission.Check(t, permission.PermPlanCreate)
if !allowed {
return permission.ErrUnauthorized
}
err = plan.Save()
if _, ok := err.(app.PlanValidationError); ok {
return &errors.HTTP{
Code: http.StatusBadRequest,
Message: err.Error(),
}
}
if err == app.ErrPlanAlreadyExists {
return &errors.HTTP{
Code: http.StatusConflict,
Message: err.Error(),
}
}
if err == app.ErrLimitOfMemory || err == app.ErrLimitOfCpuShare {
return &errors.HTTP{
Code: http.StatusBadRequest,
Message: err.Error(),
}
}
if err == nil {
w.WriteHeader(http.StatusCreated)
}
return err
}