harbor/api/search.go

132 lines
3.5 KiB
Go
Raw Normal View History

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.
*/
2016-02-26 11:54:14 +01:00
2016-02-01 12:59:10 +01:00
package api
import (
"net/http"
2016-02-01 12:59:10 +01:00
"sort"
"strings"
"github.com/vmware/harbor/dao"
"github.com/vmware/harbor/models"
"github.com/vmware/harbor/utils"
2016-03-28 02:50:09 +02:00
"github.com/vmware/harbor/utils/log"
2016-02-01 12:59:10 +01:00
)
2016-02-26 11:35:55 +01:00
// SearchAPI handles requesst to /api/search
2016-02-01 12:59:10 +01:00
type SearchAPI struct {
BaseAPI
}
2016-02-26 11:35:55 +01:00
type searchResult struct {
2016-02-01 12:59:10 +01:00
Project []map[string]interface{} `json:"project"`
Repository []map[string]interface{} `json:"repository"`
}
2016-02-26 11:35:55 +01:00
// Get ...
2016-05-09 10:48:59 +02:00
func (s *SearchAPI) Get() {
2016-08-11 10:35:18 +02:00
userID, _, ok := s.GetUserIDForRequest()
2016-02-01 12:59:10 +01:00
if !ok {
2016-02-26 04:26:54 +01:00
userID = dao.NonExistUserID
2016-02-01 12:59:10 +01:00
}
2016-05-09 10:48:59 +02:00
keyword := s.GetString("q")
isSysAdmin, err := dao.IsAdminRole(userID)
2016-02-01 12:59:10 +01:00
if err != nil {
2016-05-09 10:48:59 +02:00
log.Errorf("failed to check whether the user %d is system admin: %v", userID, err)
s.CustomAbort(http.StatusInternalServerError, "internal error")
}
var projects []models.Project
if isSysAdmin {
projects, err = dao.GetProjects("")
2016-05-09 10:48:59 +02:00
if err != nil {
log.Errorf("failed to get all projects: %v", err)
s.CustomAbort(http.StatusInternalServerError, "internal error")
}
} else {
projects, err = dao.SearchProjects(userID)
2016-05-09 10:48:59 +02:00
if err != nil {
log.Errorf("failed to get user %d 's relevant projects: %v", userID, err)
s.CustomAbort(http.StatusInternalServerError, "internal error")
}
2016-02-01 12:59:10 +01:00
}
2016-05-09 10:48:59 +02:00
projectSorter := &models.ProjectSorter{Projects: projects}
2016-02-01 12:59:10 +01:00
sort.Sort(projectSorter)
projectResult := []map[string]interface{}{}
for _, p := range projects {
match := true
if len(keyword) > 0 && !strings.Contains(p.Name, keyword) {
match = false
}
if match {
entry := make(map[string]interface{})
2016-02-26 03:15:01 +01:00
entry["id"] = p.ProjectID
2016-02-01 12:59:10 +01:00
entry["name"] = p.Name
entry["public"] = p.Public
projectResult = append(projectResult, entry)
}
}
repos, err := dao.GetAllRepositories()
if err != nil {
log.Errorf("failed to list repositories: %v", err)
s.CustomAbort(http.StatusInternalServerError, "")
}
repositories := []string{}
for _, repo := range repos {
repositories = append(repositories, repo.Name)
2016-02-01 12:59:10 +01:00
}
2016-02-01 12:59:10 +01:00
sort.Strings(repositories)
repositoryResult := filterRepositories(repositories, projects, keyword)
2016-02-26 11:35:55 +01:00
result := &searchResult{Project: projectResult, Repository: repositoryResult}
2016-05-09 10:48:59 +02:00
s.Data["json"] = result
s.ServeJSON()
2016-02-01 12:59:10 +01:00
}
func filterRepositories(repositories []string, projects []models.Project, keyword string) []map[string]interface{} {
2016-05-09 10:48:59 +02:00
i, j := 0, 0
2016-02-01 12:59:10 +01:00
result := []map[string]interface{}{}
for i < len(repositories) && j < len(projects) {
2016-08-10 04:48:58 +02:00
r := repositories[i]
p, _ := utils.ParseRepository(r)
d := strings.Compare(p, projects[j].Name)
2016-02-01 12:59:10 +01:00
if d < 0 {
i++
continue
} else if d == 0 {
i++
2016-08-10 04:48:58 +02:00
if len(keyword) != 0 && !strings.Contains(r, keyword) {
2016-02-01 12:59:10 +01:00
continue
}
entry := make(map[string]interface{})
2016-08-10 04:48:58 +02:00
entry["repository_name"] = r
2016-02-01 12:59:10 +01:00
entry["project_name"] = projects[j].Name
2016-02-26 03:15:01 +01:00
entry["project_id"] = projects[j].ProjectID
2016-02-01 12:59:10 +01:00
entry["project_public"] = projects[j].Public
result = append(result, entry)
} else {
j++
}
}
return result
}