mirror of
https://github.com/goharbor/harbor.git
synced 2025-02-23 23:31:33 +01:00
Refactor the version to variable
Signed-off-by: DQ <dengq@vmware.com>
This commit is contained in:
parent
a7e5873f46
commit
ff0c8b382c
@ -21,6 +21,7 @@ import (
|
||||
"strconv"
|
||||
|
||||
"errors"
|
||||
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/validation"
|
||||
commonhttp "github.com/goharbor/harbor/src/common/http"
|
||||
@ -31,6 +32,9 @@ import (
|
||||
const (
|
||||
defaultPageSize int64 = 500
|
||||
maxPageSize int64 = 500
|
||||
|
||||
// APIVersion is the current core api version
|
||||
APIVersion = "v2.0"
|
||||
)
|
||||
|
||||
// BaseAPI wraps common methods for controllers to host API
|
||||
|
@ -17,6 +17,11 @@ package harbor
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/goharbor/harbor/src/common/api"
|
||||
common_http "github.com/goharbor/harbor/src/common/http"
|
||||
"github.com/goharbor/harbor/src/common/http/modifier"
|
||||
common_http_auth "github.com/goharbor/harbor/src/common/http/modifier/auth"
|
||||
@ -26,9 +31,6 @@ import (
|
||||
"github.com/goharbor/harbor/src/replication/adapter/native"
|
||||
"github.com/goharbor/harbor/src/replication/model"
|
||||
"github.com/goharbor/harbor/src/replication/util"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -117,7 +119,7 @@ func (a *adapter) Info() (*model.RegistryInfo, error) {
|
||||
sys := &struct {
|
||||
ChartRegistryEnabled bool `json:"with_chartmuseum"`
|
||||
}{}
|
||||
if err := a.client.Get(a.getURL()+"/api/v2.0/systeminfo", sys); err != nil {
|
||||
if err := a.client.Get(fmt.Sprintf("%s/api/%s/systeminfo", a.getURL(), api.APIVersion), sys); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if sys.ChartRegistryEnabled {
|
||||
@ -127,7 +129,7 @@ func (a *adapter) Info() (*model.RegistryInfo, error) {
|
||||
Name string `json:"name"`
|
||||
}{}
|
||||
// label isn't supported in some previous version of Harbor
|
||||
if err := a.client.Get(a.getURL()+"/api/v2.0/labels?scope=g", &labels); err != nil {
|
||||
if err := a.client.Get(fmt.Sprintf("%s/api/%s/labels?scope=g", a.getURL(), api.APIVersion), &labels); err != nil {
|
||||
if e, ok := err.(*common_http.Error); !ok || e.Code != http.StatusNotFound {
|
||||
return nil, err
|
||||
}
|
||||
@ -183,7 +185,7 @@ func (a *adapter) PrepareForPush(resources []*model.Resource) error {
|
||||
Name: project.Name,
|
||||
Metadata: project.Metadata,
|
||||
}
|
||||
err := a.client.Post(a.getURL()+"/api/v2.0/projects", pro)
|
||||
err := a.client.Post(fmt.Sprintf("%s/api/%s/projects", a.getURL(), api.APIVersion), pro)
|
||||
if err != nil {
|
||||
if httpErr, ok := err.(*common_http.Error); ok && httpErr.Code == http.StatusConflict {
|
||||
log.Debugf("got 409 when trying to create project %s", project.Name)
|
||||
@ -249,7 +251,7 @@ type project struct {
|
||||
|
||||
func (a *adapter) getProjects(name string) ([]*project, error) {
|
||||
projects := []*project{}
|
||||
url := fmt.Sprintf("%s/api/v2.0/projects?name=%s&page=1&page_size=500", a.getURL(), name)
|
||||
url := fmt.Sprintf("%s/api/%s/projects?name=%s&page=1&page_size=500", a.getURL(), api.APIVersion, name)
|
||||
if err := a.client.GetAndIteratePagination(url, &projects); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -284,7 +286,7 @@ func (a *adapter) getProject(name string) (*project, error) {
|
||||
|
||||
func (a *adapter) getRepositories(projectID int64) ([]*adp.Repository, error) {
|
||||
repositories := []*adp.Repository{}
|
||||
url := fmt.Sprintf("%s/api/v2.0/repositories?project_id=%d&page=1&page_size=500", a.getURL(), projectID)
|
||||
url := fmt.Sprintf("%s/api/%s/repositories?project_id=%d&page=1&page_size=500", a.getURL(), api.APIVersion, projectID)
|
||||
if err := a.client.GetAndIteratePagination(url, &repositories); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -23,6 +23,7 @@ import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/goharbor/harbor/src/common/api"
|
||||
common_http "github.com/goharbor/harbor/src/common/http"
|
||||
adp "github.com/goharbor/harbor/src/replication/adapter"
|
||||
"github.com/goharbor/harbor/src/replication/model"
|
||||
@ -52,7 +53,7 @@ func (a *adapter) FetchCharts(filters []*model.Filter) ([]*model.Resource, error
|
||||
}
|
||||
resources := []*model.Resource{}
|
||||
for _, project := range projects {
|
||||
url := fmt.Sprintf("%s/api/v2.0/chartrepo/%s/charts", a.getURL(), project.Name)
|
||||
url := fmt.Sprintf("%s/api/%s/chartrepo/%s/charts", a.getURL(), api.APIVersion, project.Name)
|
||||
repositories := []*adp.Repository{}
|
||||
if err := a.client.Get(url, &repositories); err != nil {
|
||||
return nil, err
|
||||
@ -71,7 +72,7 @@ func (a *adapter) FetchCharts(filters []*model.Filter) ([]*model.Resource, error
|
||||
}
|
||||
for _, repository := range repositories {
|
||||
name := strings.SplitN(repository.Name, "/", 2)[1]
|
||||
url := fmt.Sprintf("%s/api/v2.0/chartrepo/%s/charts/%s", a.getURL(), project.Name, name)
|
||||
url := fmt.Sprintf("%s/api/%s/chartrepo/%s/charts/%s", a.getURL(), api.APIVersion, project.Name, name)
|
||||
versions := []*chartVersion{}
|
||||
if err := a.client.Get(url, &versions); err != nil {
|
||||
return nil, err
|
||||
@ -131,7 +132,7 @@ func (a *adapter) getChartInfo(name, version string) (*chartVersionDetail, error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
url := fmt.Sprintf("%s/api/v2.0/chartrepo/%s/charts/%s/%s", a.url, project, name, version)
|
||||
url := fmt.Sprintf("%s/api/%s/chartrepo/%s/charts/%s/%s", a.url, api.APIVersion, project, name, version)
|
||||
info := &chartVersionDetail{}
|
||||
if err = a.client.Get(url, info); err != nil {
|
||||
return nil, err
|
||||
@ -191,7 +192,7 @@ func (a *adapter) UploadChart(name, version string, chart io.Reader) error {
|
||||
}
|
||||
w.Close()
|
||||
|
||||
url := fmt.Sprintf("%s/api/v2.0/chartrepo/%s/charts", a.url, project)
|
||||
url := fmt.Sprintf("%s/api/%s/chartrepo/%s/charts", a.url, api.APIVersion, project)
|
||||
|
||||
req, err := http.NewRequest(http.MethodPost, url, buf)
|
||||
if err != nil {
|
||||
@ -222,7 +223,7 @@ func (a *adapter) DeleteChart(name, version string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
url := fmt.Sprintf("%s/api/v2.0/chartrepo/%s/charts/%s/%s", a.url, project, name, version)
|
||||
url := fmt.Sprintf("%s/api/%s/chartrepo/%s/charts/%s/%s", a.url, api.APIVersion, project, name, version)
|
||||
return a.client.Delete(url)
|
||||
}
|
||||
|
||||
|
@ -16,9 +16,11 @@ package harbor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/goharbor/harbor/src/common/api"
|
||||
"github.com/goharbor/harbor/src/common/utils/test"
|
||||
"github.com/goharbor/harbor/src/replication/model"
|
||||
"github.com/stretchr/testify/assert"
|
||||
@ -29,7 +31,7 @@ func TestFetchCharts(t *testing.T) {
|
||||
server := test.NewServer([]*test.RequestHandlerMapping{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Pattern: "/api/v2.0/projects",
|
||||
Pattern: fmt.Sprintf("/api/%s/projects", api.APIVersion),
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
data := `[{
|
||||
"name": "library",
|
||||
@ -40,7 +42,7 @@ func TestFetchCharts(t *testing.T) {
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Pattern: "/api/v2.0/chartrepo/library/charts/harbor",
|
||||
Pattern: fmt.Sprintf("/api/%s/chartrepo/library/charts/harbor", api.APIVersion),
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
data := `[{
|
||||
"name": "harbor",
|
||||
@ -54,7 +56,7 @@ func TestFetchCharts(t *testing.T) {
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Pattern: "/api/v2.0/chartrepo/library/charts",
|
||||
Pattern: fmt.Sprintf("/api/%s/chartrepo/library/charts", api.APIVersion),
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
data := `[{
|
||||
"name": "harbor"
|
||||
@ -100,7 +102,7 @@ func TestFetchCharts(t *testing.T) {
|
||||
func TestChartExist(t *testing.T) {
|
||||
server := test.NewServer(&test.RequestHandlerMapping{
|
||||
Method: http.MethodGet,
|
||||
Pattern: "/api/v2.0/chartrepo/library/charts/harbor/1.0",
|
||||
Pattern: fmt.Sprintf("/api/%s/chartrepo/library/charts/harbor/1.0", api.APIVersion),
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
data := `{
|
||||
"metadata": {
|
||||
@ -125,7 +127,7 @@ func TestDownloadChart(t *testing.T) {
|
||||
server := test.NewServer([]*test.RequestHandlerMapping{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Pattern: "/api/v2.0/chartrepo/library/charts/harbor/1.0",
|
||||
Pattern: fmt.Sprintf("/api/%s/chartrepo/library/charts/harbor/1.0", api.APIVersion),
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
data := `{
|
||||
"metadata": {
|
||||
@ -156,7 +158,7 @@ func TestDownloadChart(t *testing.T) {
|
||||
func TestUploadChart(t *testing.T) {
|
||||
server := test.NewServer(&test.RequestHandlerMapping{
|
||||
Method: http.MethodPost,
|
||||
Pattern: "/api/v2.0/chartrepo/library/charts",
|
||||
Pattern: fmt.Sprintf("/api/%s/chartrepo/library/charts", api.APIVersion),
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
},
|
||||
@ -174,7 +176,7 @@ func TestUploadChart(t *testing.T) {
|
||||
func TestDeleteChart(t *testing.T) {
|
||||
server := test.NewServer(&test.RequestHandlerMapping{
|
||||
Method: http.MethodDelete,
|
||||
Pattern: "/api/v2.0/chartrepo/library/charts/harbor/1.0",
|
||||
Pattern: fmt.Sprintf("/api/%s/chartrepo/library/charts/harbor/1.0", api.APIVersion),
|
||||
Handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user