From bb3bebac13a42de1b8737d64b6df8bebd9b2edc7 Mon Sep 17 00:00:00 2001 From: Tan Jiang Date: Mon, 16 May 2016 19:34:24 +0800 Subject: [PATCH] dao methods for querying policy and jobs --- api/job.go | 87 ----------------------------------- api/{ => jobs}/replication.go | 3 +- dao/dao_test.go | 63 ++++++++++++++++++++++--- dao/replication_job.go | 12 +++++ jobservice/router.go | 2 +- 5 files changed, 72 insertions(+), 95 deletions(-) delete mode 100644 api/job.go rename api/{ => jobs}/replication.go (98%) diff --git a/api/job.go b/api/job.go deleted file mode 100644 index 48930b7..0000000 --- a/api/job.go +++ /dev/null @@ -1,87 +0,0 @@ -package api - -/* -import ( - "encoding/json" - "fmt" - "github.com/vmware/harbor/dao" - "github.com/vmware/harbor/job" - "github.com/vmware/harbor/models" - "github.com/vmware/harbor/utils/log" - "net/http" - "strconv" -) - -type JobAPI struct { - BaseAPI -} - - -func (ja *JobAPI) Post() { - var je models.JobEntry - ja.DecodeJSONReq(&je) - res, err := json.Marshal(je.Options) - if !job.RunnerExists(je.Type) { - log.Errorf("runner for type %s is not registered", je.Type) - ja.RenderError(http.StatusBadRequest, fmt.Sprintf("runner for type %s is not registered", je.Type)) - return - } - je.OptionsStr = string(res) - if err != nil { - log.Warningf("Error marshaling options: %v", err) - } - res, err = json.Marshal(je.Parms) - je.ParmsStr = string(res) - if err != nil { - log.Warningf("Error marshaling parms: %v", err) - } - jobID, err := dao.AddJob(je) - if err != nil { - log.Errorf("Failed to add job to DB, error: %v", err) - ja.RenderError(http.StatusInternalServerError, "Failed to add job") - return - } - je.ID = jobID - log.Debugf("job Id:%d, type: %s", je.ID, je.Type) - job.Schedule(je) -} - -func (ja *JobAPI) Get() { - idStr := ja.Ctx.Input.Param(":id") - if len(idStr) > 0 { - jobID, err := strconv.ParseInt(idStr, 10, 64) - if err != nil { - log.Errorf("Failed to parse job id in url: %s", idStr) - ja.RenderError(http.StatusBadRequest, "invalid job id") - return - } - je, err := dao.GetJob(jobID) - if err != nil { - log.Errorf("Failed to query job from db, error: %v", err) - ja.RenderError(http.StatusInternalServerError, "Failed to query job") - return - } - if je == nil { - log.Errorf("job does not exist, id: %d", jobID) - ja.RenderError(http.StatusNotFound, "") - return - } - logs, err := dao.GetJobLogs(jobID) - if err != nil { - log.Errorf("Failed to get job logs, error: %v", err) - ja.RenderError(http.StatusInternalServerError, "Failed to query job") - return - } - je.Logs = logs - ja.Data["json"] = je - } else { - jobs, err := dao.ListJobs() - if err != nil { - log.Errorf("Failed to list jobs, error:%v", err) - ja.RenderError(http.StatusInternalServerError, "Failed to query job") - } - log.Debugf("jobs: %v", jobs) - ja.Data["json"] = jobs - } - ja.ServeJSON() -}*/ diff --git a/api/replication.go b/api/jobs/replication.go similarity index 98% rename from api/replication.go rename to api/jobs/replication.go index 0c8543a..e85df41 100644 --- a/api/replication.go +++ b/api/jobs/replication.go @@ -3,6 +3,7 @@ package api import ( "encoding/json" "fmt" + "github.com/vmware/harbor/api" "github.com/vmware/harbor/dao" "github.com/vmware/harbor/job" "github.com/vmware/harbor/models" @@ -16,7 +17,7 @@ import ( ) type ReplicationJob struct { - BaseAPI + api.BaseAPI } type ReplicationReq struct { diff --git a/dao/dao_test.go b/dao/dao_test.go index f381e03..de7259a 100644 --- a/dao/dao_test.go +++ b/dao/dao_test.go @@ -734,7 +734,7 @@ func TestDeleteUser(t *testing.T) { } } -var targetID, policyID, jobID int64 +var targetID, policyID, policyID2, policyID3, jobID, jobID2, jobID3 int64 func TestAddRepTarget(t *testing.T) { target := models.RepTarget{ @@ -849,17 +849,17 @@ func TestAddRepPolicy2(t *testing.T) { Description: "whatever", Name: "mypolicy", } - id, err := AddRepPolicy(policy2) - t.Logf("added policy, id: %d", id) + policyID2, err := AddRepPolicy(policy2) + t.Logf("added policy, id: %d", policyID2) if err != nil { t.Errorf("Error occurred in AddRepPolicy: %v", err) } - p, err := GetRepPolicy(id) + p, err := GetRepPolicy(policyID2) if err != nil { - t.Errorf("Error occurred in GetPolicy: %v, id: %d", err, id) + t.Errorf("Error occurred in GetPolicy: %v, id: %d", err, policyID2) } if p == nil { - t.Errorf("Unable to find a policy with id: %d", id) + t.Errorf("Unable to find a policy with id: %d", policyID2) } var tm time.Time if p.StartTime.After(tm) { @@ -910,6 +910,57 @@ func TestUpdateRepJobStatus(t *testing.T) { } } +func TestGetRepPolicyByProject(t *testing.T) { + p1, err := GetRepPolicyByProject(99) + if err != nil { + t.Errorf("Error occured in GetRepPolicyByProject:%v, project ID: %d", err, 99) + return + } + if len(p1) > 0 { + t.Errorf("Unexpected length of policy list, expected: 0, in fact: %d, project id: %d", len(p1), 99) + return + } + + p2, err := GetRepPolicyByProject(1) + if err != nil { + t.Errorf("Error occuered in GetRepPolicyByProject:%v, project ID: %d", err, 2) + return + } + if len(p2) != 1 { + t.Errorf("Unexpected length of policy list, expected: 1, in fact: %d, project id: %d", len(p2), 1) + return + } + if p2[0].ID != policyID { + t.Errorf("Unexpecred policy id in result, expected: %d, in fact: %d", policyID, p2[0].ID) + return + } +} + +func TestGetRepJobByPolicy(t *testing.T) { + jobs, err := GetRepJobByPolicy(999) + if err != nil { + log.Errorf("Error occured in GetRepJobByPolicy: %v, policy ID: %d", err, 999) + return + } + if len(jobs) > 0 { + log.Errorf("Unexpected length of jobs, expected: 0, in fact: %d", len(jobs)) + return + } + jobs, err = GetRepJobByPolicy(policyID) + if err != nil { + log.Errorf("Error occured in GetRepJobByPolicy: %v, policy ID: %d", err, policyID) + return + } + if len(jobs) != 1 { + log.Errorf("Unexpected length of jobs, expected: 1, in fact: %d", len(jobs)) + return + } + if jobs[0].ID != jobID { + log.Errorf("Unexpected job ID in the result, expected: %d, in fact: %d", jobID, jobs[0].ID) + return + } +} + func TestDeleteRepTarget(t *testing.T) { err := DeleteRepTarget(targetID) if err != nil { diff --git a/dao/replication_job.go b/dao/replication_job.go index 6954646..86e08de 100644 --- a/dao/replication_job.go +++ b/dao/replication_job.go @@ -54,6 +54,12 @@ func GetRepPolicy(id int64) (*models.RepPolicy, error) { } return &p, err } +func GetRepPolicyByProject(projectID int64) ([]*models.RepPolicy, error) { + var res []*models.RepPolicy + o := orm.NewOrm() + _, err := o.QueryTable("replication_policy").Filter("project_id", projectID).All(&res) + return res, err +} func DeleteRepPolicy(id int64) error { o := orm.NewOrm() _, err := o.Delete(&models.RepPolicy{ID: id}) @@ -94,6 +100,12 @@ func GetRepJob(id int64) (*models.RepJob, error) { } return &j, err } +func GetRepJobByPolicy(policyID int64) ([]*models.RepJob, error) { + o := orm.NewOrm() + var res []*models.RepJob + _, err := o.QueryTable("replication_job").Filter("policy_id", policyID).All(&res) + return res, err +} func DeleteRepJob(id int64) error { o := orm.NewOrm() _, err := o.Delete(&models.RepJob{ID: id}) diff --git a/jobservice/router.go b/jobservice/router.go index a9c0280..d224d44 100644 --- a/jobservice/router.go +++ b/jobservice/router.go @@ -1,7 +1,7 @@ package main import ( - "github.com/vmware/harbor/api" + api "github.com/vmware/harbor/api/jobs" "github.com/astaxie/beego" )