From b537225f1648912d5e519155c160c49de3d5c4cc Mon Sep 17 00:00:00 2001 From: Tan Jiang Date: Tue, 24 May 2016 13:15:27 +0800 Subject: [PATCH] /replicationJobs api will handle creating a job for a single repository --- api/jobs/replication.go | 59 +++++++++++++------ jobservice/router.go | 6 +- jobservice/{newtest.json => sync_policy.json} | 0 jobservice/sync_repo.json | 1 + 4 files changed, 46 insertions(+), 20 deletions(-) rename jobservice/{newtest.json => sync_policy.json} (100%) create mode 100644 jobservice/sync_repo.json diff --git a/api/jobs/replication.go b/api/jobs/replication.go index 3b9146813..e9a7c4afe 100644 --- a/api/jobs/replication.go +++ b/api/jobs/replication.go @@ -22,7 +22,9 @@ type ReplicationJob struct { } type ReplicationReq struct { - PolicyID int64 `json:"policy_id"` + PolicyID int64 `json:"policy_id"` + Repo string `json:"repository"` + Operation string `json:"operation"` } func (rj *ReplicationJob) Post() { @@ -40,31 +42,54 @@ func (rj *ReplicationJob) Post() { rj.RenderError(http.StatusNotFound, fmt.Sprintf("Policy not found, id: %d", data.PolicyID)) return } - repoList, err := getRepoList(p.ProjectID) - if err != nil { - log.Errorf("Failed to get repository list, project id: %d, error: %v", p.ProjectID, err) - rj.RenderError(http.StatusInternalServerError, err.Error()) - return - } - log.Debugf("repo list: %v", repoList) - for _, repo := range repoList { - j := models.RepJob{ - Repository: repo, - PolicyID: data.PolicyID, - Operation: models.RepOpTransfer, + if len(data.Repo) == 0 { // sync all repositories + repoList, err := getRepoList(p.ProjectID) + if err != nil { + log.Errorf("Failed to get repository list, project id: %d, error: %v", p.ProjectID, err) + rj.RenderError(http.StatusInternalServerError, err.Error()) + return } - log.Debugf("Creating job for repo: %s, policy: %d", repo, data.PolicyID) - id, err := dao.AddRepJob(j) + log.Debugf("repo list: %v", repoList) + for _, repo := range repoList { + err := rj.addJob(repo, data.PolicyID, models.RepOpTransfer) + if err != nil { + log.Errorf("Failed to insert job record, error: %v", err) + rj.RenderError(http.StatusInternalServerError, err.Error()) + return + } + } + } else { // sync a single repository + var op string + if len(data.Operation) > 0 { + op = data.Operation + } else { + op = models.RepOpTransfer + } + err := rj.addJob(data.Repo, data.PolicyID, op) if err != nil { log.Errorf("Failed to insert job record, error: %v", err) rj.RenderError(http.StatusInternalServerError, err.Error()) return } - log.Debugf("Send job to scheduler, job id: %d", id) - job.Schedule(id) } } +func (rj *ReplicationJob) addJob(repo string, policyID int64, operation string) error { + j := models.RepJob{ + Repository: repo, + PolicyID: policyID, + Operation: operation, + } + log.Debugf("Creating job for repo: %s, policy: %d", repo, policyID) + id, err := dao.AddRepJob(j) + if err != nil { + return err + } + log.Debugf("Send job to scheduler, job id: %d", id) + job.Schedule(id) + return nil +} + type RepActionReq struct { PolicyID int64 `json:"policy_id"` Action string `json:"action"` diff --git a/jobservice/router.go b/jobservice/router.go index e917fcc90..6a87bd995 100644 --- a/jobservice/router.go +++ b/jobservice/router.go @@ -7,7 +7,7 @@ import ( ) func initRouters() { - beego.Router("/api/jobs/replication", &api.ReplicationJob{}) - beego.Router("/api/jobs/replication/:id/log", &api.ReplicationJob{}, "get:GetLog") - beego.Router("/api/jobs/replication/actions", &api.ReplicationJob{}, "post:HandleAction") + beego.Router("/api/replicationJobs", &api.ReplicationJob{}) + beego.Router("/api/replicationJobs/:id/log", &api.ReplicationJob{}, "get:GetLog") + beego.Router("/api/replicationJobs/actions", &api.ReplicationJob{}, "post:HandleAction") } diff --git a/jobservice/newtest.json b/jobservice/sync_policy.json similarity index 100% rename from jobservice/newtest.json rename to jobservice/sync_policy.json diff --git a/jobservice/sync_repo.json b/jobservice/sync_repo.json new file mode 100644 index 000000000..550c7ddc4 --- /dev/null +++ b/jobservice/sync_repo.json @@ -0,0 +1 @@ +{"policy_id": 1, "repository":"library/ubuntu"}