use project manager instead of dao method

This commit is contained in:
Wenkai Yin 2017-06-20 13:19:43 +08:00
parent 6ee631d1eb
commit 0aed9a1171
5 changed files with 28 additions and 59 deletions

View File

@ -684,6 +684,7 @@ func TestCountPull(t *testing.T) {
}
}
/*
func TestProjectExists(t *testing.T) {
var exists bool
var err error
@ -702,7 +703,7 @@ func TestProjectExists(t *testing.T) {
t.Errorf("The project with name: %s, does not exist", currentProject.Name)
}
}
*/
func TestGetProjectById(t *testing.T) {
id := currentProject.ProjectID
p, err := GetProjectByID(id)
@ -768,13 +769,14 @@ func TestToggleProjectPublicity(t *testing.T) {
}
/*
func TestIsProjectPublic(t *testing.T) {
if isPublic := IsProjectPublic(projectName); isPublic {
t.Errorf("project, id: %d, its publicity is not false after turning off", currentProject.ProjectID)
}
}
*/
func TestGetUserProjectRoles(t *testing.T) {
r, err := GetUserProjectRoles(currentUser.UserID, currentProject.ProjectID)
if err != nil {
@ -791,6 +793,7 @@ func TestGetUserProjectRoles(t *testing.T) {
}
}
/*
func TestProjectPermission(t *testing.T) {
roleCode, err := GetPermission(currentUser.Username, currentProject.Name)
if err != nil {
@ -800,7 +803,7 @@ func TestProjectPermission(t *testing.T) {
t.Errorf("The expected role code is MDRWS,but actual: %s", roleCode)
}
}
*/
func TestGetTotalOfProjects(t *testing.T) {
total, err := GetTotalOfProjects(nil)
if err != nil {

View File

@ -21,7 +21,7 @@ import (
"fmt"
"time"
"github.com/vmware/harbor/src/common/utils/log"
//"github.com/vmware/harbor/src/common/utils/log"
)
//TODO:transaction, return err
@ -50,6 +50,7 @@ func AddProject(project models.Project) (int64, error) {
return projectID, err
}
/*
// IsProjectPublic ...
func IsProjectPublic(projectName string) bool {
project, err := GetProjectByName(projectName)
@ -85,7 +86,7 @@ func ProjectExists(nameOrID interface{}) (bool, error) {
return num > 0, nil
}
*/
// GetProjectByID ...
func GetProjectByID(id int64) (*models.Project, error) {
o := GetOrmer()
@ -125,6 +126,7 @@ func GetProjectByName(name string) (*models.Project, error) {
return &p[0], nil
}
/*
// GetPermission gets roles that the user has according to the project.
func GetPermission(username, projectName string) (string, error) {
o := GetOrmer()
@ -147,7 +149,7 @@ func GetPermission(username, projectName string) (string, error) {
return r[0].RoleCode, nil
}
*/
// ToggleProjectPublicity toggles the publicity of the project.
func ToggleProjectPublicity(projectID int64, publicity int) error {
o := GetOrmer()

View File

@ -166,14 +166,14 @@ func (ra *RepositoryAPI) Delete() {
repoName := ra.GetString(":splat")
projectName, _ := utils.ParseRepository(repoName)
exist, err := ra.ProjectMgr.Exist(projectName)
project, err := ra.ProjectMgr.Get(projectName)
if err != nil {
ra.HandleInternalServerError(fmt.Sprintf("failed to check the existence of project %s: %v",
ra.HandleInternalServerError(fmt.Sprintf("failed to get the project %s: %v",
projectName, err))
return
}
if !exist {
if project == nil {
ra.HandleNotFound(fmt.Sprintf("project %s not found", projectName))
return
}
@ -251,21 +251,10 @@ func (ra *RepositoryAPI) Delete() {
ra.CustomAbort(http.StatusInternalServerError, "internal error")
}
log.Infof("delete tag: %s:%s", repoName, t)
go TriggerReplicationByRepository(repoName, []string{t}, models.RepOpDelete)
go TriggerReplicationByRepository(project.ProjectID, repoName, []string{t}, models.RepOpDelete)
go func(tag string) {
project, err := ra.ProjectMgr.Get(projectName)
if err != nil {
log.Errorf("failed to get the project %s: %v",
projectName, err)
return
}
if project == nil {
log.Errorf("project %s not found", projectName)
return
}
if err := dao.AddAccessLog(models.AccessLog{
Username: ra.SecurityCtx.GetUsername(),
ProjectID: project.ProjectID,
@ -279,7 +268,7 @@ func (ra *RepositoryAPI) Delete() {
}(t)
}
exist, err = repositoryExist(repoName, rc)
exist, err := repositoryExist(repoName, rc)
if err != nil {
log.Errorf("failed to check the existence of repository %s: %v", repoName, err)
ra.CustomAbort(http.StatusInternalServerError, "")

View File

@ -98,28 +98,9 @@ func TriggerReplication(policyID int64, repository string,
return requestAsUI("POST", url, bytes.NewBuffer(b), http.StatusOK)
}
// GetPoliciesByRepository returns policies according the repository
func GetPoliciesByRepository(repository string) ([]*models.RepPolicy, error) {
repository = strings.TrimSpace(repository)
repository = strings.TrimRight(repository, "/")
projectName, _ := utils.ParseRepository(repository)
project, err := dao.GetProjectByName(projectName)
if err != nil {
return nil, err
}
policies, err := dao.GetRepPolicyByProject(project.ProjectID)
if err != nil {
return nil, err
}
return policies, nil
}
// TriggerReplicationByRepository triggers the replication according to the repository
func TriggerReplicationByRepository(repository string, tags []string, operation string) {
policies, err := GetPoliciesByRepository(repository)
func TriggerReplicationByRepository(projectID int64, repository string, tags []string, operation string) {
policies, err := dao.GetRepPolicyByProject(projectID)
if err != nil {
log.Errorf("failed to get policies for repository %s: %v", repository, err)
return

View File

@ -25,13 +25,11 @@ import (
"github.com/vmware/harbor/src/common/utils"
"github.com/vmware/harbor/src/common/utils/log"
"github.com/vmware/harbor/src/ui/api"
"github.com/astaxie/beego"
)
// NotificationHandler handles request on /service/notifications/, which listens to registry's events.
type NotificationHandler struct {
beego.Controller
api.BaseController
}
const manifestPattern = `^application/vnd.docker.distribution.manifest.v\d\+(json|prettyjws)`
@ -55,7 +53,6 @@ func (n *NotificationHandler) Post() {
for _, event := range events {
repository := event.Target.Repository
project, _ := utils.ParseRepository(repository)
tag := event.Target.Tag
action := event.Action
@ -65,12 +62,13 @@ func (n *NotificationHandler) Post() {
user = "anonymous"
}
pro, err := n.ProjectMgr.Get(project)
if err != nil {
log.Errorf("failed to get project by name %s: %v", project, err)
return
}
go func() {
pro, err := dao.GetProjectByName(project)
if err != nil {
log.Errorf("failed to get project by name %s: %v", project, err)
return
}
if err := dao.AddAccessLog(models.AccessLog{
Username: user,
ProjectID: pro.ProjectID,
@ -82,6 +80,7 @@ func (n *NotificationHandler) Post() {
log.Errorf("failed to add access log: %v", err)
}
}()
if action == "push" {
go func() {
exist := dao.RepositoryExists(repository)
@ -89,11 +88,6 @@ func (n *NotificationHandler) Post() {
return
}
log.Debugf("Add repository %s into DB.", repository)
pro, err := dao.GetProjectByName(project)
if err != nil {
log.Errorf("failed to get project %s: %v", project, err)
return
}
repoRecord := models.RepoRecord{
Name: repository,
ProjectID: pro.ProjectID,
@ -102,7 +96,7 @@ func (n *NotificationHandler) Post() {
log.Errorf("Error happens when adding repository: %v", err)
}
}()
go api.TriggerReplicationByRepository(repository, []string{tag}, models.RepOpTransfer)
go api.TriggerReplicationByRepository(pro.ProjectID, repository, []string{tag}, models.RepOpTransfer)
}
if action == "pull" {
go func() {