dao methods for querying policy and jobs

This commit is contained in:
Tan Jiang 2016-05-16 19:34:24 +08:00
parent dd4c346826
commit bb3bebac13
5 changed files with 72 additions and 95 deletions

View File

@ -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()
}*/

View File

@ -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 {

View File

@ -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 {

View File

@ -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})

View File

@ -1,7 +1,7 @@
package main
import (
"github.com/vmware/harbor/api"
api "github.com/vmware/harbor/api/jobs"
"github.com/astaxie/beego"
)