From ff348be04852ed6afdb7f501e6ea9e43f226d675 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Thu, 11 Aug 2016 14:02:53 +0800 Subject: [PATCH] 1. support policy deletion 2. bug fix: missing semicolon in js --- Deploy/db/registry.sql | 1 + api/replication_policy.go | 38 +++++++++++++++++++ dao/replication_job.go | 23 ++++++----- migration/changelog.md | 8 +++- models/replication_job.go | 1 + .../paginator/paginator.directive.js | 2 +- 6 files changed, 62 insertions(+), 11 deletions(-) diff --git a/Deploy/db/registry.sql b/Deploy/db/registry.sql index 121e27aae..aa7494155 100644 --- a/Deploy/db/registry.sql +++ b/Deploy/db/registry.sql @@ -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, diff --git a/api/replication_policy.go b/api/replication_policy.go index be24aa0e9..633cb9356 100644 --- a/api/replication_policy.go +++ b/api/replication_policy.go @@ -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, "") + } +} diff --git a/dao/replication_job.go b/dao/replication_job.go index 5e9f8324e..3d0f46c40 100644 --- a/dao/replication_job.go +++ b/dao/replication_job.go @@ -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 } diff --git a/migration/changelog.md b/migration/changelog.md index 888aeb5a6..b1f86f04f 100644 --- a/migration/changelog.md +++ b/migration/changelog.md @@ -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` diff --git a/models/replication_job.go b/models/replication_job.go index 8d847f9b0..6a76dcd03 100644 --- a/models/replication_job.go +++ b/models/replication_job.go @@ -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 ... diff --git a/static/resources/js/components/paginator/paginator.directive.js b/static/resources/js/components/paginator/paginator.directive.js index 7a091978a..d9e08be0b 100644 --- a/static/resources/js/components/paginator/paginator.directive.js +++ b/static/resources/js/components/paginator/paginator.directive.js @@ -78,7 +78,7 @@ this.time = 0; this.minimum = 0; this.maximum = 0; - } + }; TimeCounter.prototype.setMaximum = function(maximum) { this.maximum = maximum;