diff --git a/api/replication_policy.go b/api/replication_policy.go index 419e95bf8..d0e9946cb 100644 --- a/api/replication_policy.go +++ b/api/replication_policy.go @@ -19,7 +19,6 @@ type RepPolicyAPI struct { } // Prepare validates whether the user has system admin role -// and parsed the policy ID if it exists func (pa *RepPolicyAPI) Prepare() { uid := pa.ValidateUser() var err error @@ -129,12 +128,85 @@ func (pa *RepPolicyAPI) Post() { pa.Redirect(http.StatusCreated, strconv.FormatInt(pid, 10)) } +// Put modifies name and description of policy +func (pa *RepPolicyAPI) Put() { + id := pa.GetIDFromURL() + originalPolicy, err := dao.GetRepPolicy(id) + if err != nil { + log.Errorf("failed to get policy %d: %v", id, err) + pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) + } + + if originalPolicy == nil { + pa.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound)) + } + + var policy *models.RepPolicy + pa.DecodeJSONReq(policy) + policy.ProjectID = originalPolicy.ProjectID + policy.TargetID = originalPolicy.TargetID + pa.Validate(policy) + + if policy.Name != originalPolicy.Name { + po, err := dao.GetRepPolicyByName(policy.Name) + if err != nil { + log.Errorf("failed to get policy %s: %v", policy.Name, err) + pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) + } + + if po != nil { + pa.CustomAbort(http.StatusConflict, "name is already used") + } + } + + policy.ID = id + + if err = dao.UpdateRepPolicy(policy); err != nil { + log.Errorf("failed to update policy %d: %v", id, err) + pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) + } + + if policy.Enabled == originalPolicy.Enabled { + return + } + + //enablement has been modified + if policy.Enabled == 1 { + go func() { + if err := TriggerReplication(id, "", nil, models.RepOpTransfer); err != nil { + log.Errorf("failed to trigger replication of %d: %v", id, err) + } else { + log.Infof("replication of %d triggered", id) + } + }() + } else { + go func() { + if err := postReplicationAction(id, "stop"); err != nil { + log.Errorf("failed to stop replication of %d: %v", id, err) + } else { + log.Infof("try to stop replication of %d", id) + } + }() + } +} + type enablementReq struct { Enabled int `json:"enabled"` } // UpdateEnablement changes the enablement of the policy func (pa *RepPolicyAPI) UpdateEnablement() { + id := pa.GetIDFromURL() + policy, err := dao.GetRepPolicy(id) + if err != nil { + log.Errorf("failed to get policy %d: %v", id, err) + pa.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) + } + + if policy == nil { + pa.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound)) + } + e := enablementReq{} pa.DecodeJSONReq(&e) if e.Enabled != 0 && e.Enabled != 1 { diff --git a/api/target.go b/api/target.go index eee639b32..7d94e12f1 100644 --- a/api/target.go +++ b/api/target.go @@ -207,20 +207,20 @@ func (t *TargetAPI) Post() { func (t *TargetAPI) Put() { id := t.GetIDFromURL() - originTarget, err := dao.GetRepTarget(id) + originalTarget, err := dao.GetRepTarget(id) if err != nil { log.Errorf("failed to get target %d: %v", id, err) t.CustomAbort(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError)) } - if originTarget == nil { + if originalTarget == nil { t.CustomAbort(http.StatusNotFound, http.StatusText(http.StatusNotFound)) } target := &models.RepTarget{} t.DecodeJSONReqAndValidate(target) - if target.Name != originTarget.Name { + if target.Name != originalTarget.Name { ta, err := dao.GetRepTargetByName(target.Name) if err != nil { log.Errorf("failed to get target %s: %v", target.Name, err) diff --git a/dao/dao_test.go b/dao/dao_test.go index f8758ac4b..343c38b57 100644 --- a/dao/dao_test.go +++ b/dao/dao_test.go @@ -1149,6 +1149,23 @@ func TestDeleteRepTarget(t *testing.T) { } } +func TestFilterRepPolicies(t *testing.T) { + _, err := FilterRepPolicies("name", 0) + if err != nil { + t.Fatalf("failed to filter policy") + } +} + +func TestUpdateRepPolicy(t *testing.T) { + policy := &models.RepPolicy{ + ID: policyID, + Name: "new_policy_name", + } + if err := UpdateRepPolicy(policy); err != nil { + t.Fatalf("failed to update policy") + } +} + func TestDeleteRepPolicy(t *testing.T) { err := DeleteRepPolicy(policyID) if err != nil { diff --git a/dao/replication_job.go b/dao/replication_job.go index 1df9657a9..5c8e11055 100644 --- a/dao/replication_job.go +++ b/dao/replication_job.go @@ -171,6 +171,13 @@ func GetRepPolicyByProject(projectID int64) ([]*models.RepPolicy, error) { return policies, nil } +// UpdateRepPolicy ... +func UpdateRepPolicy(policy *models.RepPolicy) error { + o := GetOrmer() + _, err := o.Update(policy, "Name", "Enabled", "Description", "CronStr") + return err +} + // DeleteRepPolicy ... func DeleteRepPolicy(id int64) error { o := GetOrmer()