2016-02-01 12:59:10 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2016-02-24 07:31:52 +01:00
|
|
|
"net/http"
|
2016-02-01 12:59:10 +01:00
|
|
|
|
|
|
|
"github.com/vmware/harbor/dao"
|
|
|
|
"github.com/vmware/harbor/models"
|
|
|
|
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProjectAPI struct {
|
|
|
|
BaseAPI
|
|
|
|
userId int
|
|
|
|
projectId int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type projectReq struct {
|
|
|
|
ProjectName string `json:"project_name"`
|
|
|
|
Public bool `json:"public"`
|
|
|
|
}
|
|
|
|
|
2016-02-17 07:29:05 +01:00
|
|
|
const PROJECT_NAME_MAX_LEN int = 30
|
|
|
|
|
2016-02-01 12:59:10 +01:00
|
|
|
func (p *ProjectAPI) Prepare() {
|
|
|
|
p.userId = p.ValidateUser()
|
|
|
|
id_str := p.Ctx.Input.Param(":id")
|
|
|
|
if len(id_str) > 0 {
|
|
|
|
var err error
|
|
|
|
p.projectId, err = strconv.ParseInt(id_str, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error parsing project id: %s, error: %v", id_str, err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.CustomAbort(http.StatusBadRequest, "invalid project id")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-02-17 07:29:05 +01:00
|
|
|
exist, err := dao.ProjectExists(p.projectId)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-02-17 07:29:05 +01:00
|
|
|
log.Printf("Error occurred in ProjectExists: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-02-17 07:29:05 +01:00
|
|
|
if !exist {
|
2016-02-24 07:31:52 +01:00
|
|
|
p.CustomAbort(http.StatusNotFound, fmt.Sprintf("project does not exist, id: %v", p.projectId))
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectAPI) Post() {
|
|
|
|
var req projectReq
|
|
|
|
var public int
|
|
|
|
p.DecodeJsonReq(&req)
|
|
|
|
if req.Public {
|
|
|
|
public = 1
|
|
|
|
}
|
2016-02-17 07:29:05 +01:00
|
|
|
err := validateProjectReq(req)
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("Invalid project request, error: ", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusBadRequest, "Invalid request for creating project")
|
2016-02-17 07:29:05 +01:00
|
|
|
return
|
|
|
|
}
|
2016-02-01 12:59:10 +01:00
|
|
|
projectName := req.ProjectName
|
|
|
|
exist, err := dao.ProjectExists(projectName)
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("Error happened checking project existence in db:", err, ", project name:", projectName)
|
|
|
|
}
|
|
|
|
if exist {
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusConflict, "")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
project := models.Project{OwnerId: p.userId, Name: projectName, CreationTime: time.Now(), Public: public}
|
2016-02-17 07:29:05 +01:00
|
|
|
err = dao.AddProject(project)
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("Failed to add project, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusInternalServerError, "Failed to add project")
|
2016-02-17 07:29:05 +01:00
|
|
|
}
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectAPI) Head() {
|
|
|
|
projectName := p.GetString("project_name")
|
|
|
|
result, err := dao.ProjectExists(projectName)
|
|
|
|
if err != nil {
|
2016-02-17 07:29:05 +01:00
|
|
|
beego.Error("Error while communicating with DB: ", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusInternalServerError, "Error while communicating with DB")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !result {
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusNotFound, "")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectAPI) Get() {
|
|
|
|
queryProject := models.Project{UserId: p.userId}
|
|
|
|
projectName := p.GetString("project_name")
|
|
|
|
if len(projectName) > 0 {
|
|
|
|
queryProject.Name = "%" + projectName + "%"
|
|
|
|
}
|
|
|
|
public, _ := p.GetInt("is_public")
|
|
|
|
queryProject.Public = public
|
|
|
|
|
|
|
|
projectList, err := dao.QueryProject(queryProject)
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("Error occurred in QueryProject:", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
for i := 0; i < len(projectList); i++ {
|
|
|
|
if isProjectAdmin(p.userId, projectList[i].ProjectId) {
|
|
|
|
projectList[i].Togglable = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p.Data["json"] = projectList
|
|
|
|
p.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectAPI) Put() {
|
|
|
|
var req projectReq
|
|
|
|
var public int
|
|
|
|
|
|
|
|
projectId, err := strconv.ParseInt(p.Ctx.Input.Param(":id"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("Error parsing project id:", projectId, ", error: ", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusBadRequest, "invalid project id")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.DecodeJsonReq(&req)
|
|
|
|
if req.Public {
|
|
|
|
public = 1
|
|
|
|
}
|
|
|
|
if !isProjectAdmin(p.userId, projectId) {
|
|
|
|
beego.Warning("Current user, id:", p.userId, ", does not have project admin role for project, id:", projectId)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusForbidden, "")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
err = dao.ToggleProjectPublicity(p.projectId, public)
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("Error while updating project, project id:", projectId, ", error:", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.RenderError(http.StatusInternalServerError, "Failed to update project")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *ProjectAPI) FilterAccessLog() {
|
|
|
|
|
|
|
|
var filter models.AccessLog
|
|
|
|
p.DecodeJsonReq(&filter)
|
|
|
|
|
|
|
|
username := filter.Username
|
|
|
|
keywords := filter.Keywords
|
|
|
|
|
2016-02-19 05:34:18 +01:00
|
|
|
beginTime := time.Unix(filter.BeginTimestamp, 0)
|
|
|
|
endTime := time.Unix(filter.EndTimestamp, 0)
|
|
|
|
|
|
|
|
query := models.AccessLog{ProjectId: p.projectId, Username: "%" + username + "%", Keywords: keywords, BeginTime: beginTime, BeginTimestamp: filter.BeginTimestamp, EndTime: endTime, EndTimestamp: filter.EndTimestamp}
|
|
|
|
|
|
|
|
log.Printf("Query AccessLog: begin: %v, end: %v, keywords: %s", query.BeginTime, query.EndTime, query.Keywords)
|
|
|
|
|
2016-02-01 12:59:10 +01:00
|
|
|
accessLogList, err := dao.GetAccessLogs(query)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error occurred in GetAccessLogs: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
p.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
p.Data["json"] = accessLogList
|
|
|
|
p.ServeJSON()
|
|
|
|
}
|
|
|
|
|
|
|
|
func isProjectAdmin(userId int, pid int64) bool {
|
|
|
|
userQuery := models.User{UserId: userId, RoleId: models.PROJECTADMIN}
|
|
|
|
rolelist, err := dao.GetUserProjectRoles(userQuery, pid)
|
|
|
|
if err != nil {
|
|
|
|
beego.Error("Error occurred in GetUserProjectRoles:", err, ", returning false")
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return len(rolelist) > 0
|
|
|
|
}
|
2016-02-17 07:29:05 +01:00
|
|
|
|
|
|
|
func validateProjectReq(req projectReq) error {
|
|
|
|
pn := req.ProjectName
|
|
|
|
if len(pn) == 0 {
|
|
|
|
return fmt.Errorf("Project name can not be empty")
|
|
|
|
}
|
|
|
|
if len(pn) > PROJECT_NAME_MAX_LEN {
|
|
|
|
return fmt.Errorf("Project name is too long")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|