harbor/api/search.go

126 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/service/cache"
2016-02-01 12:59:10 +01:00
"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() {
userID, ok := s.GetSession("userId").(int)
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 {
2016-05-17 11:03:40 +02:00
projects, err = dao.GetAllProjects("")
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)
}
}
repositories, err2 := cache.GetRepoFromCache()
2016-02-01 12:59:10 +01:00
if err2 != nil {
2016-03-28 02:50:09 +02:00
log.Errorf("Failed to get repos from cache, error: %v", err2)
2016-05-09 10:48:59 +02:00
s.CustomAbort(http.StatusInternalServerError, "Failed to get repositories search result")
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) {
r := &utils.Repository{Name: repositories[i]}
d := strings.Compare(r.GetProject(), projects[j].Name)
if d < 0 {
i++
continue
} else if d == 0 {
i++
if len(keyword) != 0 && !strings.Contains(r.Name, keyword) {
continue
}
entry := make(map[string]interface{})
entry["repository_name"] = r.Name
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
}