本文整理汇总了Golang中github.com/tsuru/tsuru/event.FormToCustomData函数的典型用法代码示例。如果您正苦于以下问题:Golang FormToCustomData函数的具体用法?Golang FormToCustomData怎么用?Golang FormToCustomData使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了FormToCustomData函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: serviceInstanceRevokeTeam
// title: revoke access to service instance
// path: /services/{service}/instances/permission/{instance}/{team}
// method: DELETE
// responses:
// 200: Access revoked
// 401: Unauthorized
// 404: Service instance not found
func serviceInstanceRevokeTeam(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
r.ParseForm()
instanceName := r.URL.Query().Get(":instance")
serviceName := r.URL.Query().Get(":service")
serviceInstance, err := getServiceInstanceOrError(serviceName, instanceName)
if err != nil {
return err
}
allowed := permission.Check(t, permission.PermServiceInstanceUpdateRevoke,
contextsForServiceInstance(serviceInstance, serviceName)...,
)
if !allowed {
return permission.ErrUnauthorized
}
evt, err := event.New(&event.Opts{
Target: serviceInstanceTarget(serviceName, instanceName),
Kind: permission.PermServiceInstanceUpdateRevoke,
Owner: t,
CustomData: event.FormToCustomData(r.Form),
Allowed: event.Allowed(permission.PermServiceInstanceReadEvents,
contextsForServiceInstance(serviceInstance, serviceName)...),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
teamName := r.URL.Query().Get(":team")
return serviceInstance.Revoke(teamName)
}
示例2: removePlan
// title: remove plan
// path: /plans/{name}
// method: DELETE
// responses:
// 200: Plan removed
// 401: Unauthorized
// 404: Plan not found
func removePlan(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
r.ParseForm()
allowed := permission.Check(t, permission.PermPlanDelete)
if !allowed {
return permission.ErrUnauthorized
}
planName := r.URL.Query().Get(":planname")
evt, err := event.New(&event.Opts{
Target: event.Target{Type: event.TargetTypePlan, Value: planName},
Kind: permission.PermPlanDelete,
Owner: t,
CustomData: event.FormToCustomData(r.Form),
Allowed: event.Allowed(permission.PermPlanReadEvents),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
err = app.PlanRemove(planName)
if err == app.ErrPlanNotFound {
return &errors.HTTP{
Code: http.StatusNotFound,
Message: err.Error(),
}
}
return err
}
示例3: updateServiceInstance
// 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)
}
示例4: regenerateAPIToken
// title: regenerate token
// path: /users/api-key
// method: POST
// produce: application/json
// responses:
// 200: OK
// 401: Unauthorized
// 404: User not found
func regenerateAPIToken(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
r.ParseForm()
email := r.URL.Query().Get("user")
if email == "" {
email = t.GetUserName()
}
allowed := permission.Check(t, permission.PermUserUpdateToken,
permission.Context(permission.CtxUser, email),
)
if !allowed {
return permission.ErrUnauthorized
}
evt, err := event.New(&event.Opts{
Target: userTarget(email),
Kind: permission.PermUserUpdateToken,
Owner: t,
CustomData: event.FormToCustomData(r.Form),
Allowed: event.Allowed(permission.PermUserReadEvents, permission.Context(permission.CtxUser, email)),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
u, err := auth.GetUserByEmail(email)
if err != nil {
return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
}
apiKey, err := u.RegenerateAPIKey()
if err != nil {
return err
}
w.Header().Add("Content-Type", "application/json")
return json.NewEncoder(w).Encode(apiKey)
}
示例5: changeAppQuota
// title: update application quota
// path: /apps/{appname}/quota
// method: PUT
// consume: application/x-www-form-urlencoded
// responses:
// 200: Quota updated
// 400: Invalid data
// 401: Unauthorized
// 404: Application not found
func changeAppQuota(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
r.ParseForm()
appName := r.URL.Query().Get(":appname")
a, err := getAppFromContext(appName, r)
if err != nil {
return err
}
allowed := permission.Check(t, permission.PermAppAdminQuota, contextsForApp(&a)...)
if !allowed {
return permission.ErrUnauthorized
}
evt, err := event.New(&event.Opts{
Target: event.Target{Type: event.TargetTypeApp, Value: appName},
Kind: permission.PermAppAdminQuota,
Owner: t,
CustomData: event.FormToCustomData(r.Form),
Allowed: event.Allowed(permission.PermAppReadEvents, contextsForApp(&a)...),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
limit, err := strconv.Atoi(r.FormValue("limit"))
if err != nil {
return &errors.HTTP{
Code: http.StatusBadRequest,
Message: "Invalid limit",
}
}
return app.ChangeQuota(&a, limit)
}
示例6: serviceProxy
// title: service proxy
// path: /services/proxy/service/{service}
// method: "*"
// responses:
// 401: Unauthorized
// 404: Service not found
func serviceProxy(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
parseFormPreserveBody(r)
serviceName := r.URL.Query().Get(":service")
s, err := getService(serviceName)
if err != nil {
return err
}
allowed := permission.Check(t, permission.PermServiceUpdateProxy,
contextsForServiceProvision(&s)...,
)
if !allowed {
return permission.ErrUnauthorized
}
if r.Method != httpMethodGet && r.Method != httpMethodHead {
evt, err := event.New(&event.Opts{
Target: serviceTarget(s.Name),
Kind: permission.PermServiceUpdateProxy,
Owner: t,
CustomData: append(event.FormToCustomData(r.Form), map[string]interface{}{
"name": "method",
"value": r.Method,
}),
Allowed: event.Allowed(permission.PermServiceReadEvents, contextsForServiceProvision(&s)...),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
}
path := r.URL.Query().Get("callback")
return service.Proxy(&s, path, w, r)
}
示例7: removeTeamToPoolHandler
// title: remove team from pool
// path: /pools/{name}/team
// method: DELETE
// responses:
// 200: Pool updated
// 401: Unauthorized
// 400: Invalid data
// 404: Pool not found
func removeTeamToPoolHandler(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
r.ParseForm()
allowed := permission.Check(t, permission.PermPoolUpdateTeamRemove)
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.PermPoolUpdateTeamRemove,
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) }()
if teams, ok := r.URL.Query()["team"]; ok {
err := provision.RemoveTeamsFromPool(poolName, teams)
if err == provision.ErrPoolNotFound {
return &terrors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
}
return err
}
return &terrors.HTTP{
Code: http.StatusBadRequest,
Message: "You must provide the team",
}
}
示例8: autoScaleSetRule
// title: autoscale set rule
// path: /docker/autoscale/rules
// method: POST
// consume: application/x-www-form-urlencoded
// responses:
// 200: Ok
// 400: Invalid data
// 401: Unauthorized
func autoScaleSetRule(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
allowedSetRule := permission.Check(t, permission.PermNodeAutoscaleUpdate)
if !allowedSetRule {
return permission.ErrUnauthorized
}
err = r.ParseForm()
if err != nil {
return &tsuruErrors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
}
var rule autoScaleRule
dec := form.NewDecoder(nil)
dec.IgnoreUnknownKeys(true)
err = dec.DecodeValues(&rule, r.Form)
if err != nil {
return &tsuruErrors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
}
var ctxs []permission.PermissionContext
if rule.MetadataFilter != "" {
ctxs = append(ctxs, permission.Context(permission.CtxPool, rule.MetadataFilter))
}
evt, err := event.New(&event.Opts{
Target: event.Target{Type: event.TargetTypePool, Value: rule.MetadataFilter},
Kind: permission.PermNodeAutoscaleUpdate,
Owner: t,
CustomData: event.FormToCustomData(r.Form),
Allowed: event.Allowed(permission.PermPoolReadEvents, ctxs...),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
return rule.update()
}
示例9: removeTeam
// title: remove team
// path: /teams/{name}
// method: DELETE
// responses:
// 200: Team removed
// 401: Unauthorized
// 403: Forbidden
// 404: Not found
func removeTeam(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
r.ParseForm()
name := r.URL.Query().Get(":name")
allowed := permission.Check(t, permission.PermTeamDelete,
permission.Context(permission.CtxTeam, name),
)
if !allowed {
return &errors.HTTP{Code: http.StatusNotFound, Message: fmt.Sprintf(`Team "%s" not found.`, name)}
}
evt, err := event.New(&event.Opts{
Target: teamTarget(name),
Kind: permission.PermTeamDelete,
Owner: t,
CustomData: event.FormToCustomData(r.Form),
Allowed: event.Allowed(permission.PermTeamReadEvents, permission.Context(permission.CtxTeam, name)),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
err = auth.RemoveTeam(name)
if err != nil {
if _, ok := err.(*auth.ErrTeamStillUsed); ok {
msg := fmt.Sprintf("This team cannot be removed because there are still references to it:\n%s", err)
return &errors.HTTP{Code: http.StatusForbidden, Message: msg}
}
if err == auth.ErrTeamNotFound {
return &errors.HTTP{Code: http.StatusNotFound, Message: fmt.Sprintf(`Team "%s" not found.`, name)}
}
return err
}
return nil
}
示例10: nodeHealingDelete
// title: remove node healing
// path: /healing/node
// method: DELETE
// produce: application/json
// responses:
// 200: Ok
// 401: Unauthorized
func nodeHealingDelete(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
r.ParseForm()
poolName := r.URL.Query().Get("pool")
var ctxs []permission.PermissionContext
if poolName != "" {
ctxs = append(ctxs, permission.Context(permission.CtxPool, poolName))
}
if !permission.Check(t, permission.PermHealingDelete, ctxs...) {
return permission.ErrUnauthorized
}
evt, err := event.New(&event.Opts{
Target: event.Target{Type: event.TargetTypePool, Value: poolName},
Kind: permission.PermHealingDelete,
Owner: t,
CustomData: event.FormToCustomData(r.Form),
DisableLock: true,
Allowed: event.Allowed(permission.PermPoolReadEvents, ctxs...),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
if len(r.URL.Query()["name"]) == 0 {
return healer.RemoveConfig(poolName, "")
}
for _, v := range r.URL.Query()["name"] {
err := healer.RemoveConfig(poolName, v)
if err != nil {
return err
}
}
return nil
}
示例11: machineDestroy
// title: machine destroy
// path: /iaas/machines/{machine_id}
// method: DELETE
// responses:
// 200: OK
// 400: Invalid data
// 401: Unauthorized
// 404: Not found
func machineDestroy(w http.ResponseWriter, r *http.Request, token auth.Token) (err error) {
r.ParseForm()
machineID := r.URL.Query().Get(":machine_id")
if machineID == "" {
return &errors.HTTP{Code: http.StatusBadRequest, Message: "machine id is required"}
}
m, err := iaas.FindMachineById(machineID)
if err != nil {
if err == mgo.ErrNotFound {
return &errors.HTTP{Code: http.StatusNotFound, Message: "machine not found"}
}
return err
}
iaasCtx := permission.Context(permission.CtxIaaS, m.Iaas)
allowed := permission.Check(token, permission.PermMachineDelete, iaasCtx)
if !allowed {
return permission.ErrUnauthorized
}
evt, err := event.New(&event.Opts{
Target: event.Target{Type: event.TargetTypeIaas, Value: m.Iaas},
Kind: permission.PermMachineDelete,
Owner: token,
CustomData: event.FormToCustomData(r.Form),
Allowed: event.Allowed(permission.PermMachineReadEvents, iaasCtx),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
return m.Destroy()
}
示例12: resetPassword
// title: reset password
// path: /users/{email}/password
// method: POST
// responses:
// 200: Ok
// 400: Invalid data
// 401: Unauthorized
// 403: Forbidden
// 404: Not found
func resetPassword(w http.ResponseWriter, r *http.Request) (err error) {
managed, ok := app.AuthScheme.(auth.ManagedScheme)
if !ok {
return &errors.HTTP{Code: http.StatusBadRequest, Message: nonManagedSchemeMsg}
}
r.ParseForm()
email := r.URL.Query().Get(":email")
token := r.FormValue("token")
evt, err := event.New(&event.Opts{
Target: userTarget(email),
Kind: permission.PermUserUpdateReset,
RawOwner: event.Owner{Type: event.OwnerTypeUser, Name: email},
CustomData: event.FormToCustomData(r.Form),
Allowed: event.Allowed(permission.PermUserReadEvents, permission.Context(permission.CtxUser, email)),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
u, err := auth.GetUserByEmail(email)
if err != nil {
if err == auth.ErrUserNotFound {
return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()}
} else if e, ok := err.(*errors.ValidationError); ok {
return &errors.HTTP{Code: http.StatusBadRequest, Message: e.Error()}
}
return err
}
if token == "" {
return managed.StartPasswordReset(u)
}
return managed.ResetPassword(u, token)
}
示例13: templateCreate
// title: template create
// path: /iaas/templates
// method: POST
// consume: application/x-www-form-urlencoded
// responses:
// 201: Template created
// 400: Invalid data
// 401: Unauthorized
func templateCreate(w http.ResponseWriter, r *http.Request, token auth.Token) (err error) {
err = r.ParseForm()
if err != nil {
return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
}
var paramTemplate iaas.Template
dec := form.NewDecoder(nil)
dec.IgnoreUnknownKeys(true)
err = dec.DecodeValues(¶mTemplate, r.Form)
if err != nil {
return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
}
iaasCtx := permission.Context(permission.CtxIaaS, paramTemplate.IaaSName)
allowed := permission.Check(token, permission.PermMachineTemplateCreate, iaasCtx)
if !allowed {
return permission.ErrUnauthorized
}
evt, err := event.New(&event.Opts{
Target: event.Target{Type: event.TargetTypeIaas, Value: paramTemplate.IaaSName},
Kind: permission.PermMachineTemplateCreate,
Owner: token,
CustomData: event.FormToCustomData(r.Form),
Allowed: event.Allowed(permission.PermMachineReadEvents, iaasCtx),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
err = paramTemplate.Save()
if err != nil {
return err
}
w.WriteHeader(http.StatusCreated)
return nil
}
示例14: templateDestroy
// title: template destroy
// path: /iaas/templates/{template_name}
// method: DELETE
// responses:
// 200: OK
// 401: Unauthorized
// 404: Not found
func templateDestroy(w http.ResponseWriter, r *http.Request, token auth.Token) (err error) {
r.ParseForm()
templateName := r.URL.Query().Get(":template_name")
t, err := iaas.FindTemplate(templateName)
if err != nil {
if err == mgo.ErrNotFound {
return &errors.HTTP{Code: http.StatusNotFound, Message: "template not found"}
}
return err
}
iaasCtx := permission.Context(permission.CtxIaaS, t.IaaSName)
allowed := permission.Check(token, permission.PermMachineTemplateDelete, iaasCtx)
if !allowed {
return permission.ErrUnauthorized
}
evt, err := event.New(&event.Opts{
Target: event.Target{Type: event.TargetTypeIaas, Value: t.IaaSName},
Kind: permission.PermMachineTemplateDelete,
Owner: token,
CustomData: event.FormToCustomData(r.Form),
Allowed: event.Allowed(permission.PermMachineReadEvents, iaasCtx),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
return iaas.DestroyTemplate(templateName)
}
示例15: logsConfigSetHandler
// title: logs config set
// path: /docker/logs
// method: POST
// consume: application/x-www-form-urlencoded
// produce: application/x-json-stream
// responses:
// 200: Ok
// 400: Invalid data
// 401: Unauthorized
func logsConfigSetHandler(w http.ResponseWriter, r *http.Request, t auth.Token) (err error) {
err = r.ParseForm()
if err != nil {
return &tsuruErrors.HTTP{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("unable to parse form values: %s", err),
}
}
pool := r.FormValue("pool")
restart, _ := strconv.ParseBool(r.FormValue("restart"))
delete(r.Form, "pool")
delete(r.Form, "restart")
var conf container.DockerLogConfig
dec := form.NewDecoder(nil)
dec.IgnoreUnknownKeys(true)
err = dec.DecodeValues(&conf, r.Form)
if err != nil {
return &tsuruErrors.HTTP{
Code: http.StatusBadRequest,
Message: fmt.Sprintf("unable to parse fields in docker log config: %s", err),
}
}
var ctxs []permission.PermissionContext
if pool != "" {
ctxs = append(ctxs, permission.Context(permission.CtxPool, pool))
}
hasPermission := permission.Check(t, permission.PermPoolUpdateLogs, ctxs...)
if !hasPermission {
return permission.ErrUnauthorized
}
evt, err := event.New(&event.Opts{
Target: event.Target{Type: event.TargetTypePool, Value: pool},
Kind: permission.PermPoolUpdateLogs,
Owner: t,
CustomData: event.FormToCustomData(r.Form),
DisableLock: true,
Allowed: event.Allowed(permission.PermPoolReadEvents, ctxs...),
})
if err != nil {
return err
}
defer func() { evt.Done(err) }()
err = conf.Save(pool)
if err != nil {
return err
}
w.Header().Set("Content-Type", "application/x-json-stream")
keepAliveWriter := tsuruIo.NewKeepAliveWriter(w, 15*time.Second, "")
defer keepAliveWriter.Stop()
writer := &tsuruIo.SimpleJsonMessageEncoderWriter{Encoder: json.NewEncoder(keepAliveWriter)}
fmt.Fprintln(writer, "Log config successfully updated.")
if restart {
filter := &app.Filter{}
if pool != "" {
filter.Pools = []string{pool}
}
return tryRestartAppsByFilter(filter, writer)
}
return nil
}