feat(replication) refactor replication filter Validate()

Signed-off-by: Ziming Zhang <zziming@vmware.com>
This commit is contained in:
Ziming Zhang 2021-08-17 14:18:55 +08:00
parent 698c336421
commit 62808bf014
2 changed files with 52 additions and 42 deletions

View File

@ -79,48 +79,9 @@ func (p *Policy) Validate() error {
} }
// valid the filters // valid the filters
for _, filter := range p.Filters { for _, f := range p.Filters {
switch filter.Type { if err := f.Validate(); err != nil {
case model.FilterTypeResource, model.FilterTypeName, model.FilterTypeTag: return err
value, ok := filter.Value.(string)
if !ok {
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("the type of filter value isn't string")
}
if filter.Type == model.FilterTypeResource {
rt := value
if !(rt == model.ResourceTypeArtifact || rt == model.ResourceTypeImage || rt == model.ResourceTypeChart) {
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("invalid resource filter: %s", value)
}
}
if filter.Type == model.FilterTypeName || filter.Type == model.FilterTypeResource {
if filter.Decoration != "" {
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("only tag and label filter support decoration")
}
}
case model.FilterTypeLabel:
labels, ok := filter.Value.([]interface{})
if !ok {
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("the type of label filter value isn't string slice")
}
for _, label := range labels {
_, ok := label.(string)
if !ok {
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("the type of label filter value isn't string slice")
}
}
default:
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("invalid filter type")
}
if filter.Decoration != "" && filter.Decoration != model.Matches && filter.Decoration != model.Excludes {
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("invalid filter decoration, :%s", filter.Decoration)
} }
} }

View File

@ -14,6 +14,8 @@
package model package model
import "github.com/goharbor/harbor/src/lib/errors"
// const definition // const definition
const ( const (
FilterTypeResource = "resource" FilterTypeResource = "resource"
@ -38,6 +40,53 @@ type Filter struct {
Decoration string `json:"decoration,omitempty"` Decoration string `json:"decoration,omitempty"`
} }
func (f *Filter) Validate() error {
switch f.Type {
case FilterTypeResource, FilterTypeName, FilterTypeTag:
value, ok := f.Value.(string)
if !ok {
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("the type of filter value isn't string")
}
if f.Type == FilterTypeResource {
rt := value
if !(rt == ResourceTypeArtifact || rt == ResourceTypeImage || rt == ResourceTypeChart) {
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("invalid resource filter: %s", value)
}
}
if f.Type == FilterTypeName || f.Type == FilterTypeResource {
if f.Decoration != "" {
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("only tag and label filter support decoration")
}
}
case FilterTypeLabel:
labels, ok := f.Value.([]interface{})
if !ok {
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("the type of label filter value isn't string slice")
}
for _, label := range labels {
_, ok := label.(string)
if !ok {
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("the type of label filter value isn't string slice")
}
}
default:
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("invalid filter type")
}
if f.Decoration != "" && f.Decoration != Matches && f.Decoration != Excludes {
return errors.New(nil).WithCode(errors.BadRequestCode).
WithMessage("invalid filter decoration, :%s", f.Decoration)
}
return nil
}
// Trigger holds info for a trigger // Trigger holds info for a trigger
type Trigger struct { type Trigger struct {
Type string `json:"type"` Type string `json:"type"`