add tags in db, and support tags in dao and job service api

This commit is contained in:
Tan Jiang 2016-05-25 16:33:45 +08:00
parent b80e727b7f
commit a2d2314708
6 changed files with 38 additions and 15 deletions

View File

@ -134,6 +134,7 @@ create table replication_job (
policy_id int NOT NULL, policy_id int NOT NULL,
repository varchar(256) NOT NULL, repository varchar(256) NOT NULL,
operation varchar(64) NOT NULL, operation varchar(64) NOT NULL,
tags varchar(16384),
creation_time timestamp default CURRENT_TIMESTAMP, creation_time timestamp default CURRENT_TIMESTAMP,
update_time timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, update_time timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY (id) PRIMARY KEY (id)

View File

@ -24,6 +24,7 @@ type ReplicationReq struct {
PolicyID int64 `json:"policy_id"` PolicyID int64 `json:"policy_id"`
Repo string `json:"repository"` Repo string `json:"repository"`
Operation string `json:"operation"` Operation string `json:"operation"`
TagList []string `json:"tags"`
} }
func (rj *ReplicationJob) Post() { func (rj *ReplicationJob) Post() {
@ -64,7 +65,7 @@ func (rj *ReplicationJob) Post() {
} else { } else {
op = models.RepOpTransfer op = models.RepOpTransfer
} }
err := rj.addJob(data.Repo, data.PolicyID, op) err := rj.addJob(data.Repo, data.PolicyID, op, data.TagList...)
if err != nil { if err != nil {
log.Errorf("Failed to insert job record, error: %v", err) log.Errorf("Failed to insert job record, error: %v", err)
rj.RenderError(http.StatusInternalServerError, err.Error()) rj.RenderError(http.StatusInternalServerError, err.Error())
@ -73,11 +74,12 @@ func (rj *ReplicationJob) Post() {
} }
} }
func (rj *ReplicationJob) addJob(repo string, policyID int64, operation string) error { func (rj *ReplicationJob) addJob(repo string, policyID int64, operation string, tags ...string) error {
j := models.RepJob{ j := models.RepJob{
Repository: repo, Repository: repo,
PolicyID: policyID, PolicyID: policyID,
Operation: operation, Operation: operation,
TagList: tags,
} }
log.Debugf("Creating job for repo: %s, policy: %d", repo, policyID) log.Debugf("Creating job for repo: %s, policy: %d", repo, policyID)
id, err := dao.AddRepJob(j) id, err := dao.AddRepJob(j)

View File

@ -865,6 +865,7 @@ func TestAddRepJob(t *testing.T) {
Repository: "library/ubuntu", Repository: "library/ubuntu",
PolicyID: policyID, PolicyID: policyID,
Operation: "transfer", Operation: "transfer",
TagList: []string{"12.01", "14.04", "latest"},
} }
id, err := AddRepJob(job) id, err := AddRepJob(job)
if err != nil { if err != nil {
@ -882,9 +883,9 @@ func TestAddRepJob(t *testing.T) {
t.Errorf("Unable to find a job with id: %d", id) t.Errorf("Unable to find a job with id: %d", id)
return return
} }
if j.Status != models.JobPending || j.Repository != "library/ubuntu" || j.PolicyID != policyID || j.Operation != "transfer" { if j.Status != models.JobPending || j.Repository != "library/ubuntu" || j.PolicyID != policyID || j.Operation != "transfer" || len(j.TagList) != 3 {
t.Errorf("Expected data of job, id: %d, Status: %s, Repository: library/ubuntu, PolicyID: %d, Operation: transfer, "+ t.Errorf("Expected data of job, id: %d, Status: %s, Repository: library/ubuntu, PolicyID: %d, Operation: transfer, taglist length 3"+
"but in returned data:, Status: %s, Repository: %s, Operation: %s, PolicyID: %d", id, models.JobPending, policyID, j.Status, j.Repository, j.Operation, j.PolicyID) "but in returned data:, Status: %s, Repository: %s, Operation: %s, PolicyID: %d, TagList: %v", id, models.JobPending, policyID, j.Status, j.Repository, j.Operation, j.PolicyID, j.TagList)
return return
} }
} }

View File

@ -5,6 +5,7 @@ import (
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
"github.com/vmware/harbor/models" "github.com/vmware/harbor/models"
"strings"
) )
func AddRepTarget(target models.RepTarget) (int64, error) { func AddRepTarget(target models.RepTarget) (int64, error) {
@ -104,8 +105,12 @@ func AddRepJob(job models.RepJob) (int64, error) {
if len(job.Status) == 0 { if len(job.Status) == 0 {
job.Status = models.JobPending job.Status = models.JobPending
} }
if len(job.TagList) > 0 {
job.Tags = strings.Join(job.TagList, ",")
}
return o.Insert(&job) return o.Insert(&job)
} }
func GetRepJob(id int64) (*models.RepJob, error) { func GetRepJob(id int64) (*models.RepJob, error) {
o := orm.NewOrm() o := orm.NewOrm()
j := models.RepJob{ID: id} j := models.RepJob{ID: id}
@ -113,11 +118,14 @@ func GetRepJob(id int64) (*models.RepJob, error) {
if err == orm.ErrNoRows { if err == orm.ErrNoRows {
return nil, nil return nil, nil
} }
return &j, err genTagListForJob(&j)
return &j, nil
} }
func GetRepJobByPolicy(policyID int64) ([]*models.RepJob, error) { func GetRepJobByPolicy(policyID int64) ([]*models.RepJob, error) {
var res []*models.RepJob var res []*models.RepJob
_, err := repJobPolicyIDQs(policyID).All(&res) _, err := repJobPolicyIDQs(policyID).All(&res)
genTagListForJob(res...)
return res, err return res, err
} }
@ -125,6 +133,7 @@ func GetRepJobByPolicy(policyID int64) ([]*models.RepJob, error) {
func GetRepJobToStop(policyID int64) ([]*models.RepJob, error) { func GetRepJobToStop(policyID int64) ([]*models.RepJob, error) {
var res []*models.RepJob var res []*models.RepJob
_, err := repJobPolicyIDQs(policyID).Filter("status__in", models.JobPending, models.JobRunning).All(&res) _, err := repJobPolicyIDQs(policyID).Filter("status__in", models.JobPending, models.JobRunning).All(&res)
genTagListForJob(res...)
return res, err return res, err
} }
@ -150,3 +159,11 @@ func UpdateRepJobStatus(id int64, status string) error {
} }
return err return err
} }
func genTagListForJob(jobs ...*models.RepJob) {
for _, j := range jobs {
if len(j.Tags) > 0 {
j.TagList = strings.Split(j.Tags, ",")
}
}
}

View File

@ -1 +1 @@
{"policy_id": 1, "repository":"library/ubuntu"} {"policy_id": 1, "repository":"library/ubuntu", "tags":["12.04","11.11"]}

View File

@ -39,6 +39,8 @@ type RepJob struct {
Repository string `orm:"column(repository)" json:"repository"` Repository string `orm:"column(repository)" json:"repository"`
PolicyID int64 `orm:"column(policy_id)" json:"policy_id"` PolicyID int64 `orm:"column(policy_id)" json:"policy_id"`
Operation string `orm:"column(operation)" json:"operation"` Operation string `orm:"column(operation)" json:"operation"`
Tags string `orm:"column(tags)" json:"-"`
TagList []string `orm:"-" json:"tags"`
// Policy RepPolicy `orm:"-" json:"policy"` // Policy RepPolicy `orm:"-" json:"policy"`
CreationTime time.Time `orm:"column(creation_time);auto_now_add" json:"creation_time"` 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"` UpdateTime time.Time `orm:"column(update_time);auto_now" json:"update_time"`