Skip to content

Commit

Permalink
Merge pull request #5945 from steven-zou/provide_total_count_of_charts
Browse files Browse the repository at this point in the history
Return the total count of charts under the project in project API
  • Loading branch information
Daniel Jiang committed Sep 25, 2018
2 parents 86cddf3 + 8b538cb commit 8e438d8
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3031,6 +3031,9 @@ definitions:
repo_count:
type: integer
description: The number of the repositories under this project.
chart_count:
type: integer
description: The total number of charts under this project.
metadata:
description: The metadata of the project.
$ref: '#/definitions/ProjectMetadata'
Expand Down
8 changes: 8 additions & 0 deletions src/chartserver/handler_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ type ServiceHandler interface {
// If succeed, a nil error will be returned;
// otherwise, a non-nil error will be got.
DeleteChart(namespace, chartName string) error

// GetCountOfCharts calculates and returns the total count of charts under the specified namespaces.
//
// namespaces []string : the namespaces to count charts
//
// If succeed, a unsigned integer with nil error will be returned;
// otherwise, a non-nil error will be got.
GetCountOfCharts(namespaces []string) (uint64, error)
}

// ProxyTrafficHandler defines the handler methods to handle the proxy traffic.
Expand Down
15 changes: 15 additions & 0 deletions src/chartserver/handler_utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ const (
maxDeletionThreads = 10
)

// GetCountOfCharts calculates and returns the total count of charts under the specified namespaces.
// See @ServiceHandler.GetCountOfCharts
func (c *Controller) GetCountOfCharts(namespaces []string) (uint64, error) {
if namespaces == nil || len(namespaces) == 0 {
return 0, nil // Directly return 0 instead of non-nil error
}

indexFile, err := c.getIndexYaml(namespaces)
if err != nil {
return 0, err
}

return (uint64)(len(indexFile.Entries)), nil
}

// DeleteChart deletes all the chart versions of the specified chart under the namespace.
// See @ServiceHandler.DeleteChart
func (c *Controller) DeleteChart(namespace, chartName string) error {
Expand Down
32 changes: 32 additions & 0 deletions src/chartserver/handler_utility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,38 @@ import (
"testing"
)

// Test the function GetCountOfCharts
func TestGetCountOfCharts(t *testing.T) {
s, c, err := createMockObjects()
if err != nil {
t.Fatal(err)
}
defer s.Close()

count, err := c.GetCountOfCharts([]string{})
if err != nil {
t.Fatalf("expect nil error but got %s", err)
}
if count != 0 {
t.Fatalf("expect 0 but got %d", count)
}

namespaces := []string{"repo1", "repo2"}
count, err = c.GetCountOfCharts(namespaces)
if err != nil {
t.Fatalf("expect nil error but got %s", err)
}

if count != 5 {
t.Fatalf("expect 5 but got %d", count)
}

_, err = c.GetCountOfCharts([]string{"not-existing-ns"})
if err == nil {
t.Fatal("expect non-nil error but got nil one")
}
}

// Test the function DeleteChart
func TestDeleteChart(t *testing.T) {
s, c, err := createMockObjects()
Expand Down
1 change: 1 addition & 0 deletions src/common/models/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Project struct {
Togglable bool `orm:"-" json:"togglable"`
Role int `orm:"-" json:"current_user_role_id"`
RepoCount int64 `orm:"-" json:"repo_count"`
ChartCount uint64 `orm:"-" json:"chart_count"`
Metadata map[string]string `orm:"-" json:"metadata"`
}

Expand Down
9 changes: 9 additions & 0 deletions src/core/api/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,15 @@ func (p *ProjectAPI) populateProperties(project *models.Project) {
}

project.RepoCount = total

// Populate chart count property
count, err := chartController.GetCountOfCharts([]string{project.Name})
if err != nil {
log.Errorf("Failed to get total of charts under project %s: %v", project.Name, err)
p.CustomAbort(http.StatusInternalServerError, "")
}

project.ChartCount = count
}

// Put ...
Expand Down
18 changes: 18 additions & 0 deletions src/core/api/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ func TestListProjects(t *testing.T) {
apiTest := newHarborAPI()
var result []apilib.Project

cMockServer, oldCtrl, err := mockChartController()
if err != nil {
t.Fatal(err)
}
defer func() {
cMockServer.Close()
chartController = oldCtrl
}()

// ----------------------------case 1 : Response Code=200----------------------------//
fmt.Println("case 1: respose code:200")
httpStatusCode, result, err := apiTest.ProjectsGet(
Expand Down Expand Up @@ -183,6 +192,15 @@ func TestProGetByID(t *testing.T) {
var result apilib.Project
projectID := strconv.Itoa(addPID)

cMockServer, oldCtrl, err := mockChartController()
if err != nil {
t.Fatal(err)
}
defer func() {
cMockServer.Close()
chartController = oldCtrl
}()

// ----------------------------case 1 : Response Code=200----------------------------//
fmt.Println("case 1: respose code:200")
httpStatusCode, result, err := apiTest.ProjectsGetByPID(projectID)
Expand Down
5 changes: 5 additions & 0 deletions src/testing/chart_utility.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ var MockChartRepoHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.
w.Write([]byte(repo2IndexYaml))
return
}
case "/not-existing-ns/index.yaml":
if r.Method == http.MethodGet {
w.WriteHeader(http.StatusNotFound)
return
}
case "/repo1/charts/harbor-0.2.0.tgz",
"/library/charts/harbor-0.2.0.tgz":
if r.Method == http.MethodGet {
Expand Down

0 comments on commit 8e438d8

Please sign in to comment.