mirror of
https://github.com/goharbor/harbor.git
synced 2025-02-10 17:01:27 +01:00
Merge pull request #682 from ywk253100/policy_deletion
Support policy deletion
This commit is contained in:
commit
2cb2175940
@ -110,6 +110,7 @@ create table replication_policy (
|
||||
target_id int NOT NULL,
|
||||
enabled tinyint(1) NOT NULL DEFAULT 1,
|
||||
description text,
|
||||
deleted tinyint (1) DEFAULT 0 NOT NULL,
|
||||
cron_str varchar(256),
|
||||
start_time timestamp NULL,
|
||||
creation_time timestamp default CURRENT_TIMESTAMP,
|
||||
|
@ -349,3 +349,41 @@ func (pa *RepPolicyAPI) UpdateEnablement() {
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
// Delete : policies which are disabled and have no running jobs
|
||||
// can be deleted
|
||||
func (r *RepPolicyAPI) Delete() {
|
||||
id := r.GetIDFromURL()
|
||||
policy, err := dao.GetRepPolicy(id)
|
||||
if err != nil {
|
||||
log.Errorf("failed to get policy %d: %v", id, err)
|
||||
r.CustomAbort(http.StatusInternalServerError, "")
|
||||
}
|
||||
|
||||
if policy == nil || policy.Deleted == 1 {
|
||||
r.CustomAbort(http.StatusNotFound, "")
|
||||
}
|
||||
|
||||
if policy.Enabled == 1 {
|
||||
r.CustomAbort(http.StatusPreconditionFailed, "plicy is enabled, can not be deleted")
|
||||
}
|
||||
|
||||
jobs, err := dao.GetRepJobByPolicy(id)
|
||||
if err != nil {
|
||||
log.Errorf("failed to get jobs of policy %d: %v", id, err)
|
||||
r.CustomAbort(http.StatusInternalServerError, "")
|
||||
}
|
||||
|
||||
for _, job := range jobs {
|
||||
if job.Status == models.JobRunning ||
|
||||
job.Status == models.JobRetrying ||
|
||||
job.Status == models.JobPending {
|
||||
r.CustomAbort(http.StatusPreconditionFailed, "policy has running/retrying/pending jobs, can not be deleted")
|
||||
}
|
||||
}
|
||||
|
||||
if err = dao.DeleteRepPolicy(id); err != nil {
|
||||
log.Errorf("failed to delete policy %d: %v", id, err)
|
||||
r.CustomAbort(http.StatusInternalServerError, "")
|
||||
}
|
||||
}
|
||||
|
@ -155,17 +155,18 @@ func FilterRepPolicies(name string, projectID int64) ([]*models.RepPolicy, error
|
||||
left join project p on rp.project_id=p.project_id
|
||||
left join replication_target rt on rp.target_id=rt.id
|
||||
left join replication_job rj on rp.id=rj.policy_id and (rj.status="error"
|
||||
or rj.status="retrying") `
|
||||
or rj.status="retrying")
|
||||
where rp.delete = 0 `
|
||||
|
||||
if len(name) != 0 && projectID != 0 {
|
||||
sql += `where rp.name like ? and rp.project_id = ? `
|
||||
sql += `and rp.name like ? and rp.project_id = ? `
|
||||
args = append(args, "%"+name+"%")
|
||||
args = append(args, projectID)
|
||||
} else if len(name) != 0 {
|
||||
sql += `where rp.name like ? `
|
||||
sql += `and rp.name like ? `
|
||||
args = append(args, "%"+name+"%")
|
||||
} else if projectID != 0 {
|
||||
sql += `where rp.project_id = ? `
|
||||
sql += `and rp.project_id = ? `
|
||||
args = append(args, projectID)
|
||||
}
|
||||
|
||||
@ -181,7 +182,7 @@ func FilterRepPolicies(name string, projectID int64) ([]*models.RepPolicy, error
|
||||
// GetRepPolicyByName ...
|
||||
func GetRepPolicyByName(name string) (*models.RepPolicy, error) {
|
||||
o := GetOrmer()
|
||||
sql := `select * from replication_policy where name = ?`
|
||||
sql := `select * from replication_policy where deleted = 0 and name = ?`
|
||||
|
||||
var policy models.RepPolicy
|
||||
|
||||
@ -198,7 +199,7 @@ func GetRepPolicyByName(name string) (*models.RepPolicy, error) {
|
||||
// GetRepPolicyByProject ...
|
||||
func GetRepPolicyByProject(projectID int64) ([]*models.RepPolicy, error) {
|
||||
o := GetOrmer()
|
||||
sql := `select * from replication_policy where project_id = ?`
|
||||
sql := `select * from replication_policy where deleted = 0 and project_id = ?`
|
||||
|
||||
var policies []*models.RepPolicy
|
||||
|
||||
@ -212,7 +213,7 @@ func GetRepPolicyByProject(projectID int64) ([]*models.RepPolicy, error) {
|
||||
// GetRepPolicyByTarget ...
|
||||
func GetRepPolicyByTarget(targetID int64) ([]*models.RepPolicy, error) {
|
||||
o := GetOrmer()
|
||||
sql := `select * from replication_policy where target_id = ?`
|
||||
sql := `select * from replication_policy where deleted = 0 and target_id = ?`
|
||||
|
||||
var policies []*models.RepPolicy
|
||||
|
||||
@ -226,7 +227,7 @@ func GetRepPolicyByTarget(targetID int64) ([]*models.RepPolicy, error) {
|
||||
// GetRepPolicyByProjectAndTarget ...
|
||||
func GetRepPolicyByProjectAndTarget(projectID, targetID int64) ([]*models.RepPolicy, error) {
|
||||
o := GetOrmer()
|
||||
sql := `select * from replication_policy where project_id = ? and target_id = ?`
|
||||
sql := `select * from replication_policy where deleted = 0 and project_id = ? and target_id = ?`
|
||||
|
||||
var policies []*models.RepPolicy
|
||||
|
||||
@ -247,7 +248,11 @@ func UpdateRepPolicy(policy *models.RepPolicy) error {
|
||||
// DeleteRepPolicy ...
|
||||
func DeleteRepPolicy(id int64) error {
|
||||
o := GetOrmer()
|
||||
_, err := o.Delete(&models.RepPolicy{ID: id})
|
||||
policy := &models.RepPolicy{
|
||||
ID: id,
|
||||
Deleted: 1,
|
||||
}
|
||||
_, err := o.Update(policy, "Deleted")
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,7 @@ Changelog for harbor database schema
|
||||
- delete data `AMDRWS` from table `role`
|
||||
- delete data `A` from table `access`
|
||||
|
||||
## 0.2.0
|
||||
## 0.3.0
|
||||
|
||||
- create table `replication_policy`
|
||||
- create table `replication_target`
|
||||
@ -25,3 +25,9 @@ Changelog for harbor database schema
|
||||
- add column `repo_tag` to table `access_log`
|
||||
- alter column `repo_name` on table `access_log`
|
||||
- alter column `email` on table `user`
|
||||
|
||||
## TODO
|
||||
|
||||
- add index `pid_optime (project_id, op_time)` on table `access_log`
|
||||
- add index `poid_uptime (policy_id, update_time)` on table `replication_job`
|
||||
- add column `deleted` to table `replication_policy`
|
||||
|
@ -63,6 +63,7 @@ type RepPolicy struct {
|
||||
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 `json:"error_job_count"`
|
||||
Deleted int `orm:"column(deleted)" json:"deleted"`
|
||||
}
|
||||
|
||||
// Valid ...
|
||||
|
@ -78,7 +78,7 @@
|
||||
this.time = 0;
|
||||
this.minimum = 0;
|
||||
this.maximum = 0;
|
||||
}
|
||||
};
|
||||
|
||||
TimeCounter.prototype.setMaximum = function(maximum) {
|
||||
this.maximum = maximum;
|
||||
|
Loading…
Reference in New Issue
Block a user