本文整理匯總了Golang中github.com/flynn/flynn/pkg/httphelper.ValidationError函數的典型用法代碼示例。如果您正苦於以下問題:Golang ValidationError函數的具體用法?Golang ValidationError怎麽用?Golang ValidationError使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了ValidationError函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: CreateProvider
func (api *HTTPAPI) CreateProvider(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
pspec := &volume.ProviderSpec{}
if err := httphelper.DecodeJSON(r, &pspec); err != nil {
httphelper.Error(w, err)
return
}
if pspec.ID == "" {
pspec.ID = random.UUID()
}
if pspec.Kind == "" {
httphelper.ValidationError(w, "kind", "must not be blank")
return
}
var provider volume.Provider
provider, err := volumemanager.NewProvider(pspec)
if err == volume.UnknownProviderKind {
httphelper.ValidationError(w, "kind", fmt.Sprintf("%q is not known", pspec.Kind))
return
}
if err := api.vman.AddProvider(pspec.ID, provider); err != nil {
switch err {
case volumemanager.ErrProviderExists:
httphelper.ObjectExistsError(w, fmt.Sprintf("provider %q already exists", pspec.ID))
return
default:
httphelper.Error(w, err)
return
}
}
httphelper.JSON(w, 200, pspec)
}
示例2: GetLog
func (a *aggregatorAPI) GetLog(ctx context.Context, w http.ResponseWriter, req *http.Request) {
ctx, cancel := context.WithCancel(ctx)
if cn, ok := w.(http.CloseNotifier); ok {
ch := cn.CloseNotify()
go func() {
select {
case <-ch:
cancel()
case <-ctx.Done():
}
}()
}
defer cancel()
params, _ := ctxhelper.ParamsFromContext(ctx)
follow := false
if strFollow := req.FormValue("follow"); strFollow == "true" {
follow = true
}
var (
backlog bool
lines int
err error
)
if strLines := req.FormValue("lines"); strLines != "" {
if lines, err = strconv.Atoi(strLines); err != nil {
httphelper.ValidationError(w, "lines", err.Error())
return
}
if lines < 0 || lines > 10000 {
httphelper.ValidationError(w, "lines", "lines must be an integer between 0 and 10000")
return
}
backlog = lines > 0
}
filters := make(filterSlice, 0)
if jobID := req.FormValue("job_id"); jobID != "" {
filters = append(filters, filterJobID(jobID))
}
if processTypeVals, ok := req.Form["process_type"]; ok && len(processTypeVals) > 0 {
val := processTypeVals[len(processTypeVals)-1]
filters = append(filters, filterProcessType(val))
}
iter := &Iterator{
id: params.ByName("channel_id"),
follow: follow,
backlog: backlog,
lines: lines,
filter: filters,
donec: ctx.Done(),
}
writeMessages(ctx, w, iter.Scan(a.agg))
}
示例3: KillJob
func (c *controllerAPI) KillJob(ctx context.Context, w http.ResponseWriter, req *http.Request) {
params, _ := ctxhelper.ParamsFromContext(ctx)
job, err := c.jobRepo.Get(params.ByName("jobs_id"))
if err != nil {
respondWithError(w, err)
return
} else if job.HostID == "" {
httphelper.ValidationError(w, "", "cannot kill a job which has not been placed on a host")
return
}
client, err := c.clusterClient.Host(job.HostID)
if err != nil {
respondWithError(w, err)
return
}
if err = client.StopJob(job.ID); err != nil {
if _, ok := err.(ct.NotFoundError); ok {
err = ErrNotFound
}
respondWithError(w, err)
return
}
}
示例4: GetCloudRegions
func (api *httpAPI) GetCloudRegions(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
params := req.URL.Query()
cloud := params.Get("cloud")
if cloud != "digital_ocean" && cloud != "azure" {
httphelper.ObjectNotFoundError(w, "")
return
}
credentialID := params.Get("credential_id")
creds, err := api.Installer.FindCredentials(credentialID)
if err != nil {
httphelper.ValidationError(w, "credential_id", "Invalid credential id")
return
}
var res interface{}
switch cloud {
case "digital_ocean":
res, err = api.Installer.ListDigitalOceanRegions(creds)
case "azure":
res, err = api.Installer.ListAzureRegions(creds)
}
if err != nil {
httphelper.Error(w, err)
return
}
httphelper.JSON(w, 200, res)
}
示例5: Send
func (api *HTTPAPI) Send(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
volumeID := ps.ByName("volume_id")
if !strings.Contains(r.Header.Get("Accept"), snapshotContentType) {
httphelper.ValidationError(w, "", fmt.Sprintf("must be prepared to accept a content type of %q", snapshotContentType))
return
}
w.Header().Set("Content-Type", snapshotContentType)
var haves []json.RawMessage
if err := httphelper.DecodeJSON(r, &haves); err != nil {
httphelper.Error(w, err)
return
}
err := api.vman.SendSnapshot(volumeID, haves, w)
if err != nil {
switch err {
case volume.ErrNoSuchVolume:
httphelper.ObjectNotFoundError(w, fmt.Sprintf("no volume with id %q", volumeID))
return
default:
httphelper.Error(w, err)
return
}
}
}
示例6: servePutInstance
// servePutInstance adds an instance to a service.
func (h *Handler) servePutInstance(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
// Read path parameter.
service := params.ByName("service")
// Read instance from request.
inst := &discoverd.Instance{}
if err := json.NewDecoder(r.Body).Decode(inst); err != nil {
hh.Error(w, err)
return
}
// Ensure instance is valid.
if err := inst.Valid(); err != nil {
hh.ValidationError(w, "", err.Error())
return
}
// Add instance to service in the store.
if err := h.Store.AddInstance(service, inst); err == ErrNotLeader {
h.redirectToLeader(w, r)
return
} else if IsNotFound(err) {
hh.ObjectNotFoundError(w, err.Error())
return
} else if err != nil {
hh.Error(w, err)
return
}
}
示例7: dropDatabase
func (a *API) dropDatabase(ctx context.Context, w http.ResponseWriter, req *http.Request) {
id := strings.SplitN(strings.TrimPrefix(req.FormValue("id"), "/databases/"), ":", 2)
if len(id) != 2 || id[1] == "" {
httphelper.ValidationError(w, "id", "is invalid")
return
}
db, err := a.connect()
if err != nil {
httphelper.Error(w, err)
return
}
defer db.Close()
if _, err := db.Exec(fmt.Sprintf("DROP DATABASE `%s`", id[1])); err != nil {
httphelper.Error(w, err)
return
}
if _, err := db.Exec(fmt.Sprintf("DROP USER '%s'", id[0])); err != nil {
httphelper.Error(w, err)
return
}
w.WriteHeader(200)
}
示例8: dropDatabase
func (p *pgAPI) dropDatabase(ctx context.Context, w http.ResponseWriter, req *http.Request) {
id := strings.SplitN(strings.TrimPrefix(req.FormValue("id"), "/databases/"), ":", 2)
if len(id) != 2 || id[1] == "" {
httphelper.ValidationError(w, "id", "is invalid")
return
}
// disable new connections to the target database
if err := p.db.Exec(disallowConns, id[1]); err != nil {
httphelper.Error(w, err)
return
}
// terminate current connections
if err := p.db.Exec(disconnectConns, id[1]); err != nil {
httphelper.Error(w, err)
return
}
if err := p.db.Exec(fmt.Sprintf(`DROP DATABASE "%s"`, id[1])); err != nil {
httphelper.Error(w, err)
return
}
if err := p.db.Exec(fmt.Sprintf(`DROP USER "%s"`, id[0])); err != nil {
httphelper.Error(w, err)
return
}
w.WriteHeader(200)
}
示例9: UpdateRoute
func (api *API) UpdateRoute(ctx context.Context, w http.ResponseWriter, req *http.Request) {
log, _ := ctxhelper.LoggerFromContext(ctx)
params, _ := ctxhelper.ParamsFromContext(ctx)
var route *router.Route
if err := json.NewDecoder(req.Body).Decode(&route); err != nil {
log.Error(err.Error())
httphelper.Error(w, err)
return
}
route.Type = params.ByName("route_type")
route.ID = params.ByName("id")
l := api.router.ListenerFor(route.Type)
if l == nil {
httphelper.ValidationError(w, "type", "Invalid route type")
return
}
if err := l.UpdateRoute(route); err != nil {
if err == ErrNotFound {
w.WriteHeader(404)
return
}
log.Error(err.Error())
httphelper.Error(w, err)
return
}
httphelper.JSON(w, 200, route)
}
示例10: dropDatabase
func (a *API) dropDatabase(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
id := strings.SplitN(strings.TrimPrefix(req.FormValue("id"), "/databases/"), ":", 2)
if len(id) != 2 || id[1] == "" {
httphelper.ValidationError(w, "id", "is invalid")
return
}
user, database := id[0], id[1]
session, err := mgo.DialWithInfo(&mgo.DialInfo{
Addrs: []string{net.JoinHostPort(serviceHost, "27017")},
Username: "flynn",
Password: os.Getenv("MONGO_PWD"),
Database: "admin",
})
if err != nil {
httphelper.Error(w, err)
return
}
defer session.Close()
// Delete user.
if err := session.DB(database).Run(bson.D{{"dropUser", user}}, nil); err != nil {
httphelper.Error(w, err)
return
}
// Delete database.
if err := session.DB(database).Run(bson.D{{"dropDatabase", 1}}, nil); err != nil {
httphelper.Error(w, err)
return
}
w.WriteHeader(200)
}
示例11: servePutLeader
// servePutLeader sets the leader for a service.
func (h *Handler) servePutLeader(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
// Retrieve path parameters.
service := params.ByName("service")
// Check if the service allows manual leader election.
config := h.Store.Config(service)
if config == nil || config.LeaderType != discoverd.LeaderTypeManual {
hh.ValidationError(w, "", "service leader election type is not manual")
return
}
// Read instance from the request.
inst := &discoverd.Instance{}
if err := hh.DecodeJSON(r, inst); err != nil {
hh.Error(w, err)
return
}
// Manually set the leader on the service.
if err := h.Store.SetServiceLeader(service, inst.ID); err == ErrNotLeader {
h.redirectToLeader(w, r)
return
} else if err != nil {
hh.Error(w, err)
return
}
}
示例12: servePutService
// servePutService creates a service.
func (h *Handler) servePutService(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
// Retrieve the path parameter.
service := params.ByName("service")
if err := ValidServiceName(service); err != nil {
hh.ValidationError(w, "", err.Error())
return
}
// Read config from the request.
config := &discoverd.ServiceConfig{}
if err := hh.DecodeJSON(r, config); err != nil {
hh.Error(w, err)
return
}
// Add the service to the store.
if err := h.Store.AddService(service, config); err == ErrNotLeader {
h.redirectToLeader(w, r)
return
} else if IsServiceExists(err) {
hh.ObjectExistsError(w, err.Error())
return
} else if err != nil {
hh.Error(w, err)
return
}
}
示例13: SignalJob
func (h *jobAPI) SignalJob(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
sig := ps.ByName("signal")
if sig == "" {
httphelper.ValidationError(w, "sig", "must not be empty")
return
}
sigInt, err := strconv.Atoi(sig)
if err != nil {
httphelper.ValidationError(w, "sig", "must be an integer")
return
}
id := ps.ByName("id")
if err := h.host.SignalJob(id, sigInt); err != nil {
httphelper.Error(w, err)
return
}
w.WriteHeader(200)
}
示例14: respondWithError
// NOTE: this is temporary until httphelper supports custom errors
func respondWithError(w http.ResponseWriter, err error) {
switch v := err.(type) {
case ct.ValidationError:
httphelper.ValidationError(w, v.Field, v.Message)
default:
if err == ErrNotFound {
w.WriteHeader(404)
return
}
httphelper.Error(w, err)
}
}
示例15: InstallHandler
func (api *httpAPI) InstallHandler(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
var input *jsonInput
if err := httphelper.DecodeJSON(req, &input); err != nil {
httphelper.Error(w, err)
return
}
api.InstallerStackMtx.Lock()
defer api.InstallerStackMtx.Unlock()
if len(api.InstallerStacks) > 0 {
httphelper.ObjectExistsError(w, "install already started")
return
}
var id = random.Hex(16)
var creds aws.CredentialsProvider
if input.Creds.AccessKeyID != "" && input.Creds.SecretAccessKey != "" {
creds = aws.Creds(input.Creds.AccessKeyID, input.Creds.SecretAccessKey, "")
} else {
var err error
creds, err = aws.EnvCreds()
if err != nil {
httphelper.ValidationError(w, "", err.Error())
return
}
}
s := &httpInstaller{
ID: id,
PromptOutChan: make(chan *httpPrompt),
PromptInChan: make(chan *httpPrompt),
logger: log.New(),
api: api,
}
s.Stack = &Stack{
Creds: creds,
Region: input.Region,
InstanceType: input.InstanceType,
NumInstances: input.NumInstances,
VpcCidr: input.VpcCidr,
SubnetCidr: input.SubnetCidr,
PromptInput: s.PromptInput,
YesNoPrompt: s.YesNoPrompt,
}
if err := s.Stack.RunAWS(); err != nil {
httphelper.Error(w, err)
return
}
api.InstallerStacks[id] = s
go s.handleEvents()
httphelper.JSON(w, 200, s)
}