mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-21 23:21:26 +01:00
update according to the comments
This commit is contained in:
parent
51d5df0849
commit
5cef58baa1
@ -2398,17 +2398,21 @@ definitions:
|
||||
type: string
|
||||
description: The description of the policy.
|
||||
trigger:
|
||||
type: string
|
||||
type: object
|
||||
description: The trigger for schedule job.
|
||||
items:
|
||||
$ref: '#/definitions/RepTrigger'
|
||||
filters:
|
||||
type: array
|
||||
description: >-
|
||||
The replication policy filter array.
|
||||
description: The replication policy filter array.
|
||||
items:
|
||||
$ref: '#/definitions/RepFilter'
|
||||
replicate_existing_image_now:
|
||||
type: string
|
||||
description: Whether to replicate the existing images now.
|
||||
replicate_deletion:
|
||||
type: string
|
||||
description: Whether replication deletion operation.
|
||||
description: Whether to replicate the deletion operation.
|
||||
start_time:
|
||||
type: string
|
||||
description: The start time of the policy.
|
||||
@ -2438,14 +2442,18 @@ definitions:
|
||||
type: string
|
||||
description: The policy name.
|
||||
trigger:
|
||||
type: string
|
||||
type: object
|
||||
description: The trigger for schedule job.
|
||||
items:
|
||||
$ref: '#/definitions/RepTrigger'
|
||||
filters:
|
||||
type: array
|
||||
description: >-
|
||||
The replication policy filter array.
|
||||
description: The replication policy filter array.
|
||||
items:
|
||||
$ref: '#/definitions/RepFilter'
|
||||
replicate_existing_image_now:
|
||||
type: string
|
||||
description: Whether to replicate the existing images now.
|
||||
replicate_deletion:
|
||||
type: string
|
||||
description: Whether replication deletion operation.
|
||||
@ -2471,16 +2479,30 @@ definitions:
|
||||
type: string
|
||||
description: The description of the policy.
|
||||
trigger:
|
||||
type: string
|
||||
description: The cron string for schedule job.
|
||||
type: object
|
||||
description: The trigger for schedule job.
|
||||
items:
|
||||
$ref: '#/definitions/RepTrigger'
|
||||
filters:
|
||||
type: array
|
||||
description: The replication policy filter array.
|
||||
items:
|
||||
$ref: '#/definitions/RepFilter'
|
||||
replicate_existing_image_now:
|
||||
type: string
|
||||
description: Whether to replicate the existing images now.
|
||||
replicate_deletion:
|
||||
type: string
|
||||
description: Whether replication deletion operation.
|
||||
RepTrigger:
|
||||
type: object
|
||||
properties:
|
||||
type:
|
||||
type: string
|
||||
description: The replication policy trigger type.
|
||||
params:
|
||||
type: object
|
||||
description: The map is the replication policy trigger parameters.
|
||||
RepFilter:
|
||||
type: object
|
||||
properties:
|
||||
|
@ -104,7 +104,7 @@ func FilterRepTargets(name string) ([]*models.RepTarget, error) {
|
||||
|
||||
// AddRepPolicy ...
|
||||
func AddRepPolicy(policy models.RepPolicy) (int64, error) {
|
||||
if err := policy.MarshalFilter(); err != nil {
|
||||
if err := policy.Marshal(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
now := time.Now()
|
||||
@ -132,7 +132,7 @@ func GetRepPolicy(id int64) (*models.RepPolicy, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := policy.UnmarshalFilter(); err != nil {
|
||||
if err := policy.Unmarshal(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -176,7 +176,7 @@ func FilterRepPolicies(name string, projectID int64) ([]*models.RepPolicy, error
|
||||
}
|
||||
|
||||
for _, policy := range policies {
|
||||
if err := policy.UnmarshalFilter(); err != nil {
|
||||
if err := policy.Unmarshal(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@ -198,7 +198,7 @@ func GetRepPolicyByName(name string) (*models.RepPolicy, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := policy.UnmarshalFilter(); err != nil {
|
||||
if err := policy.Unmarshal(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@ -217,7 +217,7 @@ func GetRepPolicyByProject(projectID int64) ([]*models.RepPolicy, error) {
|
||||
}
|
||||
|
||||
for _, policy := range policies {
|
||||
if err := policy.UnmarshalFilter(); err != nil {
|
||||
if err := policy.Unmarshal(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@ -237,7 +237,7 @@ func GetRepPolicyByTarget(targetID int64) ([]*models.RepPolicy, error) {
|
||||
}
|
||||
|
||||
for _, policy := range policies {
|
||||
if err := policy.UnmarshalFilter(); err != nil {
|
||||
if err := policy.Unmarshal(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@ -257,7 +257,7 @@ func GetRepPolicyByProjectAndTarget(projectID, targetID int64) ([]*models.RepPol
|
||||
}
|
||||
|
||||
for _, policy := range policies {
|
||||
if err := policy.UnmarshalFilter(); err != nil {
|
||||
if err := policy.Unmarshal(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
@ -267,13 +267,13 @@ func GetRepPolicyByProjectAndTarget(projectID, targetID int64) ([]*models.RepPol
|
||||
|
||||
// UpdateRepPolicy ...
|
||||
func UpdateRepPolicy(policy *models.RepPolicy) error {
|
||||
if err := policy.MarshalFilter(); err != nil {
|
||||
if err := policy.Marshal(); err != nil {
|
||||
return err
|
||||
}
|
||||
o := GetOrmer()
|
||||
policy.UpdateTime = time.Now()
|
||||
_, err := o.Update(policy, "TargetID", "Name", "Enabled", "Description",
|
||||
"Trigger", "FiltersInDB", "ReplicateDeletion", "UpdateTime")
|
||||
"TriggerInDB", "FiltersInDB", "ReplicateDeletion", "UpdateTime")
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -41,23 +41,25 @@ const (
|
||||
|
||||
// RepPolicy is the model for a replication policy, which associate to a project and a target (destination)
|
||||
type RepPolicy struct {
|
||||
ID int64 `orm:"pk;auto;column(id)" json:"id"`
|
||||
ProjectID int64 `orm:"column(project_id)" json:"project_id"`
|
||||
ProjectName string `orm:"-" json:"project_name,omitempty"`
|
||||
TargetID int64 `orm:"column(target_id)" json:"target_id"`
|
||||
TargetName string `orm:"-" json:"target_name,omitempty"`
|
||||
Name string `orm:"column(name)" json:"name"`
|
||||
Enabled int `orm:"column(enabled)" json:"enabled"`
|
||||
Description string `orm:"column(description)" json:"description"`
|
||||
Trigger string `orm:"column(cron_str)" json:"trigger"`
|
||||
Filters []*RepFilter `orm:"-" json:"filters"`
|
||||
FiltersInDB string `orm:"column(filters)" json:"-"`
|
||||
ReplicateDeletion bool `orm:"column(replicate_deletion)" json:"replicate_deletion"`
|
||||
StartTime time.Time `orm:"column(start_time)" json:"start_time"`
|
||||
CreationTime time.Time `orm:"column(creation_time);auto_now_add" json:"creation_time"`
|
||||
UpdateTime time.Time `orm:"column(update_time);auto_now" json:"update_time"`
|
||||
ErrorJobCount int `orm:"-" json:"error_job_count"`
|
||||
Deleted int `orm:"column(deleted)" json:"deleted"`
|
||||
ID int64 `orm:"pk;auto;column(id)" json:"id"`
|
||||
ProjectID int64 `orm:"column(project_id)" json:"project_id"`
|
||||
ProjectName string `orm:"-" json:"project_name,omitempty"`
|
||||
TargetID int64 `orm:"column(target_id)" json:"target_id"`
|
||||
TargetName string `orm:"-" json:"target_name,omitempty"`
|
||||
Name string `orm:"column(name)" json:"name"`
|
||||
Enabled int `orm:"column(enabled)" json:"enabled"`
|
||||
Description string `orm:"column(description)" json:"description"`
|
||||
Trigger *RepTrigger `orm:"-" json:"trigger"`
|
||||
TriggerInDB string `orm:"column(cron_str)" json:"-"`
|
||||
Filters []*RepFilter `orm:"-" json:"filters"`
|
||||
FiltersInDB string `orm:"column(filters)" json:"-"`
|
||||
ReplicateExistingImageNow bool `orm:"-" json:"replicate_existing_image_now"`
|
||||
ReplicateDeletion bool `orm:"column(replicate_deletion)" json:"replicate_deletion"`
|
||||
StartTime time.Time `orm:"column(start_time)" json:"start_time"`
|
||||
CreationTime time.Time `orm:"column(creation_time);auto_now_add" json:"creation_time"`
|
||||
UpdateTime time.Time `orm:"column(update_time);auto_now" json:"update_time"`
|
||||
ErrorJobCount int `orm:"-" json:"error_job_count"`
|
||||
Deleted int `orm:"column(deleted)" json:"deleted"`
|
||||
}
|
||||
|
||||
// Valid ...
|
||||
@ -82,24 +84,37 @@ func (r *RepPolicy) Valid(v *validation.Validation) {
|
||||
v.SetError("enabled", "must be 0 or 1")
|
||||
}
|
||||
|
||||
if len(r.Trigger) > 256 {
|
||||
v.SetError("trigger", "max length is 256")
|
||||
if r.Trigger != nil {
|
||||
r.Trigger.Valid(v)
|
||||
}
|
||||
|
||||
for _, filter := range r.Filters {
|
||||
filter.Valid(v)
|
||||
}
|
||||
|
||||
if err := r.MarshalFilter(); err != nil {
|
||||
v.SetError("filters", err.Error())
|
||||
if err := r.Marshal(); err != nil {
|
||||
v.SetError("trigger or filters", err.Error())
|
||||
}
|
||||
if len(r.Filters) > 1024 {
|
||||
|
||||
if len(r.TriggerInDB) > 256 {
|
||||
v.SetError("trigger", "max length is 256")
|
||||
}
|
||||
|
||||
if len(r.FiltersInDB) > 1024 {
|
||||
v.SetError("filters", "max length is 1024")
|
||||
}
|
||||
}
|
||||
|
||||
// MarshalFilter marshal RepFilter array to json string
|
||||
func (r *RepPolicy) MarshalFilter() error {
|
||||
// Marshal marshal RepTrigger and RepFilter array to json string
|
||||
func (r *RepPolicy) Marshal() error {
|
||||
if r.Trigger != nil {
|
||||
b, err := json.Marshal(r.Trigger)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
r.TriggerInDB = string(b)
|
||||
}
|
||||
|
||||
if r.Filters != nil {
|
||||
b, err := json.Marshal(r.Filters)
|
||||
if err != nil {
|
||||
@ -110,8 +125,16 @@ func (r *RepPolicy) MarshalFilter() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// UnmarshalFilter unmarshal json string to RepFilter array
|
||||
func (r *RepPolicy) UnmarshalFilter() error {
|
||||
// Unmarshal unmarshal json string to RepTrigger and RepFilter array
|
||||
func (r *RepPolicy) Unmarshal() error {
|
||||
if len(r.TriggerInDB) > 0 {
|
||||
trigger := &RepTrigger{}
|
||||
if err := json.Unmarshal([]byte(r.TriggerInDB), &trigger); err != nil {
|
||||
return err
|
||||
}
|
||||
r.Trigger = trigger
|
||||
}
|
||||
|
||||
if len(r.FiltersInDB) > 0 {
|
||||
filter := []*RepFilter{}
|
||||
if err := json.Unmarshal([]byte(r.FiltersInDB), &filter); err != nil {
|
||||
@ -141,6 +164,21 @@ func (r *RepFilter) Valid(v *validation.Validation) {
|
||||
}
|
||||
}
|
||||
|
||||
// RepTrigger holds information for the replication policy trigger
|
||||
type RepTrigger struct {
|
||||
Type string `json:"type"`
|
||||
Params map[string]interface{} `json:"params"`
|
||||
}
|
||||
|
||||
// Valid ...
|
||||
func (r *RepTrigger) Valid(v *validation.Validation) {
|
||||
if !(r.Type == replication.TriggerKindManually ||
|
||||
r.Type == replication.TriggerKindSchedule ||
|
||||
r.Type == replication.TriggerKindImmediately) {
|
||||
v.SetError("trigger.type", fmt.Sprintf("invalid trigger type: %s", r.Type))
|
||||
}
|
||||
}
|
||||
|
||||
// RepJob is the model for a replication job, which is the execution unit on job service, currently it is used to transfer/remove
|
||||
// a repository to/from a remote registry instance.
|
||||
type RepJob struct {
|
||||
|
@ -21,7 +21,11 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMarshalAndUnmarshalFilter(t *testing.T) {
|
||||
func TestMarshalAndUnmarshal(t *testing.T) {
|
||||
trigger := &RepTrigger{
|
||||
Type: "schedule",
|
||||
Params: map[string]interface{}{"date": "2:00"},
|
||||
}
|
||||
filters := []*RepFilter{
|
||||
&RepFilter{
|
||||
Type: "repository",
|
||||
@ -29,15 +33,18 @@ func TestMarshalAndUnmarshalFilter(t *testing.T) {
|
||||
},
|
||||
}
|
||||
policy := &RepPolicy{
|
||||
Trigger: trigger,
|
||||
Filters: filters,
|
||||
}
|
||||
|
||||
err := policy.MarshalFilter()
|
||||
err := policy.Marshal()
|
||||
require.Nil(t, err)
|
||||
|
||||
policy.Trigger = nil
|
||||
policy.Filters = nil
|
||||
err = policy.UnmarshalFilter()
|
||||
err = policy.Unmarshal()
|
||||
require.Nil(t, err)
|
||||
|
||||
assert.EqualValues(t, filters, policy.Filters)
|
||||
assert.EqualValues(t, trigger, policy.Trigger)
|
||||
}
|
@ -7,4 +7,10 @@ const (
|
||||
FilterItemKindRepository = "repository"
|
||||
//FilterItemKindTag : Kind of filter item is 'tag'
|
||||
FilterItemKindTag = "tag"
|
||||
//TriggerKindManually : kind of trigger is 'manully'
|
||||
TriggerKindManually = "manually"
|
||||
//TriggerKindSchedule : kind of trigger is 'schedule'
|
||||
TriggerKindSchedule = "schedule"
|
||||
//TriggerKindImmediately : kind of trigger is 'immediately'
|
||||
TriggerKindImmediately = "immediately"
|
||||
)
|
||||
|
@ -41,12 +41,19 @@ func TestPoliciesPost(t *testing.T) {
|
||||
//add target
|
||||
CommonAddTarget()
|
||||
targetID := int64(CommonGetTarget())
|
||||
repPolicy := &apilib.RepPolicyPost{int64(1), targetID, addPolicyName, []*models.RepFilter{
|
||||
&models.RepFilter{
|
||||
Type: replication.FilterItemKindRepository,
|
||||
Value: "library/ubuntu*",
|
||||
repPolicy := &apilib.RepPolicyPost{int64(1), targetID, addPolicyName,
|
||||
&models.RepTrigger{
|
||||
Type: replication.TriggerKindSchedule,
|
||||
Params: map[string]interface{}{
|
||||
"date": "2:00",
|
||||
},
|
||||
},
|
||||
}}
|
||||
[]*models.RepFilter{
|
||||
&models.RepFilter{
|
||||
Type: replication.FilterItemKindRepository,
|
||||
Value: "library/ubuntu*",
|
||||
},
|
||||
}}
|
||||
|
||||
fmt.Println("Testing Policies Post API")
|
||||
|
||||
@ -129,12 +136,16 @@ func TestPoliciesPost(t *testing.T) {
|
||||
}
|
||||
|
||||
fmt.Println("case 8 : response code = 400: invalid filter")
|
||||
repPolicy = &apilib.RepPolicyPost{int64(1), targetID, addPolicyName, []*models.RepFilter{
|
||||
&models.RepFilter{
|
||||
Type: "replication",
|
||||
Value: "",
|
||||
repPolicy = &apilib.RepPolicyPost{int64(1), targetID, addPolicyName,
|
||||
&models.RepTrigger{
|
||||
Type: replication.TriggerKindManually,
|
||||
},
|
||||
}}
|
||||
[]*models.RepFilter{
|
||||
&models.RepFilter{
|
||||
Type: "replication",
|
||||
Value: "",
|
||||
},
|
||||
}}
|
||||
httpStatusCode, err = apiTest.AddPolicy(*admin, *repPolicy)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(int(400), httpStatusCode, "httpStatusCode should be 400")
|
||||
|
@ -37,6 +37,9 @@ type RepPolicyPost struct {
|
||||
// The policy name.
|
||||
Name string `json:"name,omitempty"`
|
||||
|
||||
// Trigger
|
||||
Trigger *models.RepTrigger `json:"trigger"`
|
||||
|
||||
// Filters
|
||||
Filters []*models.RepFilter `json:"filters"`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user