mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-26 12:15:20 +01:00
feat(replication) refactor replication filter Validate()
Signed-off-by: Ziming Zhang <zziming@vmware.com>
This commit is contained in:
parent
698c336421
commit
62808bf014
@ -79,48 +79,9 @@ func (p *Policy) Validate() error {
|
||||
}
|
||||
|
||||
// valid the filters
|
||||
for _, filter := range p.Filters {
|
||||
switch filter.Type {
|
||||
case model.FilterTypeResource, model.FilterTypeName, model.FilterTypeTag:
|
||||
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)
|
||||
for _, f := range p.Filters {
|
||||
if err := f.Validate(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,8 @@
|
||||
|
||||
package model
|
||||
|
||||
import "github.com/goharbor/harbor/src/lib/errors"
|
||||
|
||||
// const definition
|
||||
const (
|
||||
FilterTypeResource = "resource"
|
||||
@ -38,6 +40,53 @@ type Filter struct {
|
||||
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
|
||||
type Trigger struct {
|
||||
Type string `json:"type"`
|
||||
|
Loading…
Reference in New Issue
Block a user