Skip to content

Commit

Permalink
feature: add rules for type validation
Browse files Browse the repository at this point in the history
Signed-off-by: zhuangqh <[email protected]>
  • Loading branch information
zhuangqh committed Oct 31, 2018
1 parent 143227d commit 504e13b
Show file tree
Hide file tree
Showing 15 changed files with 377 additions and 15 deletions.
6 changes: 0 additions & 6 deletions apis/opts/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,3 @@ func parseLabel(label string) ([]string, error) {
}
return fields, nil
}

// ValidateLabels verifies the correct of labels
func ValidateLabels(map[string]string) error {
// TODO
return nil
}
4 changes: 4 additions & 0 deletions apis/server/container_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,10 @@ func (s *Server) updateContainer(ctx context.Context, rw http.ResponseWriter, re
if err := json.NewDecoder(reader).Decode(config); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}
// validate request body
if err := config.Validate(strfmt.NewFormats()); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

name := mux.Vars(req)["name"]

Expand Down
15 changes: 13 additions & 2 deletions apis/server/system_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/alibaba/pouch/pkg/utils"

"github.com/docker/docker/pkg/ioutils"
"github.com/go-openapi/strfmt"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -41,20 +42,30 @@ func (s *Server) version(ctx context.Context, rw http.ResponseWriter, req *http.

func (s *Server) updateDaemon(ctx context.Context, rw http.ResponseWriter, req *http.Request) (err error) {
cfg := &types.DaemonUpdateConfig{}

// decode request body
if err := json.NewDecoder(req.Body).Decode(cfg); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

// TODO: validate cfg in details
// validate request body
if err := cfg.Validate(strfmt.NewFormats()); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

return s.SystemMgr.UpdateDaemon(cfg)
}

func (s *Server) auth(ctx context.Context, rw http.ResponseWriter, req *http.Request) (err error) {
auth := types.AuthConfig{}

// decode request body
if err := json.NewDecoder(req.Body).Decode(&auth); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}
// validate request body
if err := auth.Validate(strfmt.NewFormats()); err != nil {
return httputils.NewHTTPError(err, http.StatusBadRequest)
}

token, err := s.SystemMgr.Auth(&auth)
if err != nil {
Expand Down
14 changes: 13 additions & 1 deletion apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2179,6 +2179,7 @@ definitions:
type: "object"
additionalProperties:
type: "string"
pattern: ^[a-zA-Z0-9][a-zA-Z0-9_\-.]{0,63}$
StopSignal:
description: "Signal to stop a container as a string or unsigned integer."
type: "string"
Expand All @@ -2187,6 +2188,7 @@ definitions:
StopTimeout:
description: "Timeout to stop a container in seconds."
type: "integer"
minimum: 0
default: 10
Shell:
description: "Shell for when `RUN`, `CMD`, and `ENTRYPOINT` uses a shell."
Expand Down Expand Up @@ -3172,21 +3174,25 @@ definitions:
description: "The new volume's name. If not specified, Pouch generates a name."
type: "string"
x-nullable: false
pattern: ^[a-zA-Z0-9][a-zA-Z0-9_\-.]{0,63}$
Driver:
description: "Name of the volume driver to use."
type: "string"
default: "local"
x-nullable: false
pattern: ^[a-zA-Z0-9][a-zA-Z0-9_\-.]{0,63}$
DriverOpts:
description: "A mapping of driver options and values. These options are passed directly to the driver and are driver specific."
type: "object"
additionalProperties:
type: "string"
pattern: ^[a-zA-Z0-9][a-zA-Z0-9_\-.]{0,127}$
Labels:
description: "User-defined key/value metadata."
type: "object"
additionalProperties:
type: "string"
pattern: ^[a-zA-Z0-9][a-zA-Z0-9_\-.]{0,127}$
example:
Name: "tardis"
Labels:
Expand Down Expand Up @@ -3214,6 +3220,7 @@ definitions:
ExecCreateConfig:
type: "object"
description: is a small subset of the Config struct that holds the configuration.
required: [Cmd]
properties:
User:
type: "string"
Expand Down Expand Up @@ -3909,8 +3916,9 @@ definitions:
type: "string"
example: "127.0.0.1"
HostPort:
description: "Host port number that the container's port is mapped to."
description: "Host port number that the container's port is mapped to. range [0,65535]"
type: "string"
pattern: ^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$
example: "4443"

RestartPolicy:
Expand Down Expand Up @@ -3963,6 +3971,7 @@ definitions:
Driver:
type: "string"
description: "Driver means the network's driver."
pattern: ^[a-zA-Z0-9][a-zA-Z0-9_\-.]{0,63}$
EnableIPv6:
type: "boolean"
IPAM:
Expand All @@ -3979,6 +3988,7 @@ definitions:
type: "object"
additionalProperties:
type: "string"
pattern: ^[a-zA-Z0-9][a-zA-Z0-9_\-.]{0,63}$

NetworkInspectResp:
type: "object"
Expand Down Expand Up @@ -4095,10 +4105,12 @@ definitions:
properties:
Driver:
type: "string"
pattern: ^[a-zA-Z0-9][a-zA-Z0-9_\-.]{0,63}$
Options:
type: "object"
additionalProperties:
type: "string"
pattern: ^[a-zA-Z0-9][a-zA-Z0-9_\-.]{0,63}$
Config:
type: "array"
items:
Expand Down
39 changes: 39 additions & 0 deletions apis/types/container_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions apis/types/exec_create_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions apis/types/ip_a_m.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions apis/types/network_create.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 504e13b

Please sign in to comment.