Fix: delete addon registry (#2594)

* Fix: delete addon registry

* add validate Git should not be empty

* fix token could be nil

* GitSource Repo url is a must
This commit is contained in:
Hongchao Deng
2021-11-02 11:30:30 +08:00
committed by GitHub
parent 90e5fd9ed6
commit 5f09faeff0
4 changed files with 15 additions and 8 deletions
+1 -1
View File
@@ -26,7 +26,7 @@ type AddonRegistry struct {
// GitAddonSource defines the information about the Git as addon source
type GitAddonSource struct {
URL string `json:"url,omitempty"`
URL string `json:"url,omitempty" validate:"required"`
Path string `json:"path,omitempty"`
Token string `json:"token,omitempty"`
}
+1 -1
View File
@@ -51,7 +51,7 @@ type EmptyResponse struct{}
// CreateAddonRegistryRequest defines the format for addon registry create request
type CreateAddonRegistryRequest struct {
Name string `json:"name" validate:"checkname"`
Git *model.GitAddonSource `json:"git,omitempty"`
Git *model.GitAddonSource `json:"git,omitempty" validate:"required"`
}
// AddonRegistryMeta defines the format for a single addon registry
+8 -6
View File
@@ -5,7 +5,6 @@ import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"path"
"sort"
@@ -47,6 +46,7 @@ const (
type AddonUsecase interface {
GetAddonRegistryModel(ctx context.Context, name string) (*model.AddonRegistry, error)
CreateAddonRegistry(ctx context.Context, req apis.CreateAddonRegistryRequest) (*apis.AddonRegistryMeta, error)
DeleteAddonRegistry(ctx context.Context, name string) error
ListAddonRegistries(ctx context.Context) ([]*apis.AddonRegistryMeta, error)
ListAddons(ctx context.Context, detailed bool, query string) ([]*apis.DetailAddonResponse, error)
StatusAddon(name string) (*apis.AddonStatusResponse, error)
@@ -158,6 +158,10 @@ func (u *addonUsecaseImpl) ListAddons(ctx context.Context, detailed bool, query
return addons, nil
}
func (u *addonUsecaseImpl) DeleteAddonRegistry(ctx context.Context, name string) error {
return u.ds.Delete(ctx, &model.AddonRegistry{Name: name})
}
func (u *addonUsecaseImpl) CreateAddonRegistry(ctx context.Context, req apis.CreateAddonRegistryRequest) (*apis.AddonRegistryMeta, error) {
r := addonRegistryModelFromCreateAddonRegistryRequest(req)
@@ -312,13 +316,11 @@ func hasAddon(addons []*apis.DetailAddonResponse, name string) bool {
func getAddonsFromGit(baseURL, dir, token string, detailed bool) ([]*apis.DetailAddonResponse, error) {
addons := []*apis.DetailAddonResponse{}
dec := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme)
var tc *http.Client
var ts oauth2.TokenSource
if token != "" {
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc = oauth2.NewClient(context.Background(), ts)
ts = oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
}
tc := oauth2.NewClient(context.Background(), ts)
tc.Timeout = time.Second * 10
clt := github.NewClient(tc)
// TODO add error handling
@@ -108,6 +108,11 @@ func (s *addonRegistryWebService) deleteAddonRegistry(req *restful.Request, res
bcode.ReturnError(req, res, err)
return
}
err = s.addonUsecase.DeleteAddonRegistry(req.Request.Context(), r.Name)
if err != nil {
bcode.ReturnError(req, res, err)
return
}
if err := res.WriteEntity(*utils.ConvertAddonRegistryModel2AddonRegistryMeta(r)); err != nil {
bcode.ReturnError(req, res, err)