mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-21 23:21:26 +01:00
1. Define ng persist replication policy model.
2. Add ng replication policy CURD methods. 3. Implement ng policy manger. Signed-off-by: Frank Kung <kfanjian@gmail.com> Signed-off-by: 慕薇疯魔 <kfanjian@gmail.com>
This commit is contained in:
parent
83aca2885e
commit
5bd5d59a4f
@ -23,6 +23,7 @@ import (
|
||||
func init() {
|
||||
orm.RegisterModel(
|
||||
new(models.Registry),
|
||||
new(models.RepPolicy),
|
||||
new(RepPolicy),
|
||||
new(RepJob),
|
||||
new(User),
|
||||
|
27
src/replication/ng/dao/models/policy.go
Normal file
27
src/replication/ng/dao/models/policy.go
Normal file
@ -0,0 +1,27 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
// RepPolicy is the model for a ng replication policy.
|
||||
type RepPolicy struct {
|
||||
ID int64 `orm:"pk;auto;column(id)" json:"id"`
|
||||
Name string `orm:"column(name)" json:"name"`
|
||||
Description string `orm:"column(description)" json:"description"`
|
||||
Creator string `orm:"column(creator)" json:"creator"`
|
||||
SrcRegistryID int64 `orm:"column(src_registry_id)" json:"src_registry_id"`
|
||||
SrcNamespaces string `orm:"column(src_namespaces)" json:"src_namespaces"`
|
||||
DestRegistryID int64 `orm:"column(dest_registry_id)" json:"dest_registry_id"`
|
||||
DestNamespace string `orm:"column(dest_namespace)" json:"dest_namespace"`
|
||||
Override bool `orm:"column(override)" json:"override"`
|
||||
Enabled bool `orm:"column(enabled)" json:"enabled"`
|
||||
Trigger string `orm:"column(cron_str)" json:"trigger"`
|
||||
Filters string `orm:"column(filters)" json:"filters"`
|
||||
ReplicateDeletion bool `orm:"column(replicate_deletion)" json:"replicate_deletion"`
|
||||
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"`
|
||||
}
|
||||
|
||||
// TableName set table name for ORM. TODO: replace _ng suffix.
|
||||
func (r *RepPolicy) TableName() string {
|
||||
return "replication_policy_ng"
|
||||
}
|
88
src/replication/ng/dao/policy.go
Normal file
88
src/replication/ng/dao/policy.go
Normal file
@ -0,0 +1,88 @@
|
||||
package dao
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/astaxie/beego/orm"
|
||||
common_dao "github.com/goharbor/harbor/src/common/dao"
|
||||
"github.com/goharbor/harbor/src/replication/ng/dao/models"
|
||||
)
|
||||
|
||||
// AddRepPolicy insert new policy to DB.
|
||||
func AddRepPolicy(policy *models.RepPolicy) (int64, error) {
|
||||
o := common_dao.GetOrmer()
|
||||
now := time.Now()
|
||||
policy.CreationTime = now
|
||||
policy.UpdateTime = now
|
||||
|
||||
return o.Insert(policy)
|
||||
}
|
||||
|
||||
func filteredRepPolicyQuerySeter(name, namespace string) orm.QuerySeter {
|
||||
var qs = common_dao.GetOrmer().QueryTable(new(models.RepPolicy))
|
||||
|
||||
// TODO: just filter polices by name now, and need consider how to filter namespace.
|
||||
qs = qs.Filter("name__icontains", name)
|
||||
|
||||
return qs
|
||||
}
|
||||
|
||||
// GetTotalOfRepPolicies returns the total count of replication policies
|
||||
func GetTotalOfRepPolicies(name, namespace string) (int64, error) {
|
||||
var qs = filteredRepPolicyQuerySeter(name, namespace)
|
||||
return qs.Count()
|
||||
}
|
||||
|
||||
// GetPolicies filter policies and pagination.
|
||||
func GetPolicies(name, namespace string, page, pageSize int64) (policies []*models.RepPolicy, err error) {
|
||||
var qs = filteredRepPolicyQuerySeter(name, namespace)
|
||||
|
||||
// Paginate
|
||||
if page > 0 && pageSize > 0 {
|
||||
qs = qs.Limit(pageSize, (page-1)*pageSize)
|
||||
}
|
||||
|
||||
_, err = qs.All(&policies)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetRepPolicy return special policy by id.
|
||||
func GetRepPolicy(id int64) (policy *models.RepPolicy, err error) {
|
||||
policy = new(models.RepPolicy)
|
||||
err = common_dao.GetOrmer().QueryTable(policy).
|
||||
Filter("id", id).One(policy)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetRepPolicyByName return special policy by name.
|
||||
func GetRepPolicyByName(name string) (policy *models.RepPolicy, err error) {
|
||||
policy = new(models.RepPolicy)
|
||||
err = common_dao.GetOrmer().QueryTable(policy).
|
||||
Filter("name", name).One(policy)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// UpdateRepPolicy update fields by props
|
||||
func UpdateRepPolicy(policy *models.RepPolicy, props ...string) (err error) {
|
||||
var o = common_dao.GetOrmer()
|
||||
|
||||
if policy != nil {
|
||||
_, err = o.Update(policy, props...)
|
||||
} else {
|
||||
err = errors.New("Nil policy")
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteRepPolicy will hard delete database item
|
||||
func DeleteRepPolicy(id int64) error {
|
||||
o := common_dao.GetOrmer()
|
||||
|
||||
_, err := o.Delete(&models.RepPolicy{ID: id})
|
||||
return err
|
||||
}
|
@ -15,9 +15,104 @@
|
||||
package policy
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/goharbor/harbor/src/replication/ng/dao"
|
||||
persist_models "github.com/goharbor/harbor/src/replication/ng/dao/models"
|
||||
"github.com/goharbor/harbor/src/replication/ng/model"
|
||||
)
|
||||
|
||||
var errNilPolicyModel = errors.New("nil policy model")
|
||||
|
||||
func convertFromPersistModel(policy *persist_models.RepPolicy) (*model.Policy, error) {
|
||||
if policy == nil {
|
||||
return &model.Policy{}, nil
|
||||
}
|
||||
|
||||
ply := model.Policy{
|
||||
ID: policy.ID,
|
||||
Name: policy.Name,
|
||||
Description: policy.Description,
|
||||
Creator: policy.Creator,
|
||||
SrcRegistryID: policy.SrcRegistryID,
|
||||
DestRegistryID: policy.DestRegistryID,
|
||||
DestNamespace: policy.DestNamespace,
|
||||
Deletion: policy.ReplicateDeletion,
|
||||
Override: policy.Override,
|
||||
Enabled: policy.Enabled,
|
||||
CreationTime: policy.CreationTime,
|
||||
UpdateTime: policy.UpdateTime,
|
||||
}
|
||||
|
||||
// 1. parse SrcNamespaces to array
|
||||
if len(policy.SrcNamespaces) > 0 {
|
||||
ply.SrcNamespaces = strings.Split(policy.SrcNamespaces, ",")
|
||||
}
|
||||
|
||||
// 2. parse Filters
|
||||
if len(policy.Filters) > 0 {
|
||||
filters := []*model.Filter{}
|
||||
if err := json.Unmarshal([]byte(policy.Filters), &filters); err != nil {
|
||||
return &model.Policy{}, err
|
||||
}
|
||||
ply.Filters = filters
|
||||
}
|
||||
|
||||
// 3. parse Trigger
|
||||
if len(policy.Trigger) > 0 {
|
||||
trigger := &model.Trigger{}
|
||||
if err := json.Unmarshal([]byte(policy.Trigger), trigger); err != nil {
|
||||
return &model.Policy{}, err
|
||||
}
|
||||
ply.Trigger = trigger
|
||||
}
|
||||
|
||||
return &ply, nil
|
||||
}
|
||||
|
||||
func convertToPersistModel(policy *model.Policy) (*persist_models.RepPolicy, error) {
|
||||
if policy == nil {
|
||||
return nil, errNilPolicyModel
|
||||
}
|
||||
|
||||
ply := &persist_models.RepPolicy{
|
||||
ID: policy.ID,
|
||||
Name: policy.Name,
|
||||
Description: policy.Description,
|
||||
Creator: policy.Creator,
|
||||
SrcRegistryID: policy.SrcRegistryID,
|
||||
SrcNamespaces: strings.Join(policy.SrcNamespaces, ","),
|
||||
DestRegistryID: policy.DestRegistryID,
|
||||
DestNamespace: policy.DestNamespace,
|
||||
Override: policy.Override,
|
||||
Enabled: policy.Enabled,
|
||||
ReplicateDeletion: policy.Deletion,
|
||||
CreationTime: policy.CreationTime,
|
||||
UpdateTime: time.Now(),
|
||||
}
|
||||
|
||||
if policy.Trigger != nil {
|
||||
trigger, err := json.Marshal(policy.Trigger)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ply.Trigger = string(trigger)
|
||||
}
|
||||
|
||||
if len(policy.Filters) > 0 {
|
||||
filters, err := json.Marshal(policy.Filters)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ply.Filters = string(filters)
|
||||
}
|
||||
|
||||
return ply, nil
|
||||
}
|
||||
|
||||
// Manager manages replication policies
|
||||
type Manager interface {
|
||||
// Create new policy
|
||||
@ -32,3 +127,86 @@ type Manager interface {
|
||||
// Remove the specified policy
|
||||
Remove(int64) error
|
||||
}
|
||||
|
||||
// DefaultManager provides replication policy CURD capabilities.
|
||||
type DefaultManager struct{}
|
||||
|
||||
var _ Manager = &DefaultManager{}
|
||||
|
||||
// NewDefaultManager is the constructor of DefaultManager.
|
||||
func NewDefaultManager() *DefaultManager {
|
||||
return &DefaultManager{}
|
||||
}
|
||||
|
||||
// Create creates a new policy with the provided data;
|
||||
// If creating failed, error will be returned;
|
||||
// If creating succeed, ID of the new created policy will be returned.
|
||||
func (m *DefaultManager) Create(policy *model.Policy) (int64, error) {
|
||||
ply, err := convertToPersistModel(policy)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return dao.AddRepPolicy(ply)
|
||||
}
|
||||
|
||||
// List returns all the policies
|
||||
func (m *DefaultManager) List(queries ...*model.PolicyQuery) (total int64, polices []*model.Policy, err error) {
|
||||
// default query parameters
|
||||
var name = ""
|
||||
var namespace = ""
|
||||
var page int64 = 1
|
||||
var pageSize int64 = 15
|
||||
|
||||
if len(queries) > 0 {
|
||||
name = queries[0].Name
|
||||
namespace = queries[0].Namespace
|
||||
page = queries[0].Pagination.Page
|
||||
pageSize = queries[0].Pagination.Size
|
||||
}
|
||||
|
||||
var persistPolicies []*persist_models.RepPolicy
|
||||
persistPolicies, err = dao.GetPolicies(name, namespace, page, pageSize)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
total, err = dao.GetTotalOfRepPolicies(name, namespace)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, policy := range persistPolicies {
|
||||
ply, err := convertFromPersistModel(policy)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
|
||||
polices = append(polices, ply)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Get returns the policy with the specified ID
|
||||
func (m *DefaultManager) Get(policyID int64) (*model.Policy, error) {
|
||||
policy, err := dao.GetRepPolicy(policyID)
|
||||
if err != nil {
|
||||
return &model.Policy{}, err
|
||||
}
|
||||
|
||||
return convertFromPersistModel(policy)
|
||||
}
|
||||
|
||||
// Update Update the specified policy
|
||||
func (m *DefaultManager) Update(policy *model.Policy, props ...string) error {
|
||||
updatePolicy, err := convertToPersistModel(policy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return dao.UpdateRepPolicy(updatePolicy, props...)
|
||||
}
|
||||
|
||||
// Remove Remove the specified policy
|
||||
func (m *DefaultManager) Remove(policyID int64) error {
|
||||
return dao.DeleteRepPolicy(policyID)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user