Skip to content

Commit 8dc1d84

Browse files
committed
merge
2 parents aeaf94c + af6acd2 commit 8dc1d84

File tree

14 files changed

+431
-374
lines changed

14 files changed

+431
-374
lines changed

internal/api/v1/chart/chart.go

+40-95
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package chart
22

33
import (
4-
"errors"
5-
"github.com/KubeOperator/kubepi/internal/api/v1/session"
6-
"github.com/KubeOperator/kubepi/internal/server"
74
"github.com/KubeOperator/kubepi/internal/service/v1/chart"
8-
"github.com/KubeOperator/kubepi/internal/service/v1/common"
95
pkgV1 "github.com/KubeOperator/kubepi/pkg/api/v1"
10-
"github.com/asdine/storm/v3"
116
"github.com/kataras/iris/v12"
127
"github.com/kataras/iris/v12/context"
8+
"strings"
139
)
1410

1511
type Handler struct {
@@ -22,109 +18,32 @@ func NewHandler() *Handler {
2218
}
2319
}
2420

25-
func (h *Handler) SearchCharts() iris.Handler {
21+
func (h *Handler) DeleteRepo() iris.Handler {
2622
return func(ctx *context.Context) {
27-
pageNum, _ := ctx.Values().GetInt(pkgV1.PageNum)
28-
pageSize, _ := ctx.Values().GetInt(pkgV1.PageSize)
29-
pattern := ctx.URLParam("pattern")
3023
cluster := ctx.URLParam("cluster")
31-
charts, total, err := h.chartService.Search(pageNum, pageSize, cluster, pattern, common.DBOptions{})
32-
if err != nil {
33-
if !errors.Is(err, storm.ErrNotFound) {
34-
ctx.StatusCode(iris.StatusInternalServerError)
35-
ctx.Values().Set("message", err.Error())
36-
return
37-
}
38-
}
39-
cs := make([]Chart, 0)
40-
for i := range charts {
41-
cs = append(cs, Chart{
42-
Chart: charts[i],
43-
})
44-
}
45-
ctx.Values().Set("data", pkgV1.Page{Items: cs, Total: total})
46-
}
47-
}
48-
49-
func (h *Handler) CreateChart() iris.Handler {
50-
return func(ctx *context.Context) {
51-
var req Chart
52-
if err := ctx.ReadJSON(&req); err != nil {
53-
ctx.StatusCode(iris.StatusBadRequest)
54-
ctx.Values().Set("message", err.Error())
55-
return
56-
}
57-
u := ctx.Values().Get("profile")
58-
profile := u.(session.UserProfile)
59-
//tx
60-
tx, err := server.DB().Begin(true)
61-
if err != nil {
62-
ctx.StatusCode(iris.StatusInternalServerError)
63-
ctx.Values().Set("message", err.Error())
64-
return
65-
}
66-
req.CreatedBy = profile.Name
67-
if err := h.chartService.Create(&req.Chart, common.DBOptions{DB: tx}); err != nil {
68-
_ = tx.Rollback()
69-
ctx.StatusCode(iris.StatusInternalServerError)
70-
ctx.Values().Set("message", err.Error())
71-
return
72-
}
73-
_ = tx.Commit()
74-
ctx.Values().Set("data", req)
75-
}
76-
}
77-
78-
func (h *Handler) DeleteChart() iris.Handler {
79-
return func(ctx *context.Context) {
80-
tx, _ := server.DB().Begin(true)
81-
txOptions := common.DBOptions{DB: tx}
82-
8324
name := ctx.Params().GetString("name")
84-
if err := h.chartService.Delete(name, txOptions); err != nil {
85-
_ = tx.Rollback()
25+
if err := h.chartService.RemoveRepo(cluster, name); err != nil {
8626
ctx.StatusCode(iris.StatusInternalServerError)
8727
ctx.Values().Set("message", err.Error())
8828
return
8929
}
90-
_ = tx.Commit()
9130
}
9231
}
9332

9433
func (h *Handler) GetChart() iris.Handler {
9534
return func(ctx *context.Context) {
96-
name := ctx.Params().GetString("name")
97-
c, err := h.chartService.GetByName(name, common.DBOptions{})
98-
if err != nil {
99-
ctx.StatusCode(iris.StatusInternalServerError)
100-
ctx.Values().Set("message", err.Error())
101-
return
102-
}
103-
ctx.Values().Set("data", &Chart{Chart: *c})
35+
//name := ctx.Params().GetString("name")
36+
//c, err := h.chartService.GetByName(name, common.DBOptions{})
37+
//if err != nil {
38+
// ctx.StatusCode(iris.StatusInternalServerError)
39+
// ctx.Values().Set("message", err.Error())
40+
// return
41+
//}
42+
//ctx.Values().Set("data", &Chart{Chart: *c})
10443
}
10544
}
10645
func (h *Handler) UpdateChart() iris.Handler {
10746
return func(ctx *context.Context) {
108-
name := ctx.Params().GetString("name")
109-
var req Chart
110-
if err := ctx.ReadJSON(&req); err != nil {
111-
ctx.StatusCode(iris.StatusBadRequest)
112-
ctx.Values().Set("message", err.Error())
113-
}
114-
tx, err := server.DB().Begin(true)
115-
if err != nil {
116-
ctx.StatusCode(iris.StatusInternalServerError)
117-
ctx.Values().Set("message", err.Error())
118-
return
119-
}
120-
if err := h.chartService.Update(name, &req.Chart, common.DBOptions{DB: tx}); err != nil {
121-
_ = tx.Rollback()
122-
ctx.StatusCode(iris.StatusInternalServerError)
123-
ctx.Values().Set("message", err.Error())
124-
return
125-
}
126-
_ = tx.Commit()
127-
ctx.Values().Set("data", &req)
12847
}
12948
}
13049

@@ -167,14 +86,40 @@ func (h *Handler) AddRepo() iris.Handler {
16786
}
16887
}
16988

89+
func (h *Handler) ListCharts() iris.Handler {
90+
return func(ctx *context.Context) {
91+
pageNum, _ := ctx.Values().GetInt(pkgV1.PageNum)
92+
pageSize, _ := ctx.Values().GetInt(pkgV1.PageSize)
93+
pattern := ctx.URLParam("pattern")
94+
cluster := ctx.URLParam("cluster")
95+
repo := ctx.URLParam("repo")
96+
charts, total, err := h.chartService.ListCharts(cluster, repo, pageNum, pageSize, pattern)
97+
if err != nil {
98+
ctx.StatusCode(iris.StatusInternalServerError)
99+
ctx.Values().Set("message", err.Error())
100+
return
101+
}
102+
chartArray := make([]*Chart, len(charts))
103+
for i, ch := range charts {
104+
names := strings.Split(ch.Name, "/")
105+
chartArray[i] = &Chart{
106+
Name: ch.Chart.Metadata.Name,
107+
Description: ch.Chart.Metadata.Description,
108+
Icon: ch.Chart.Icon,
109+
Repo: names[0],
110+
}
111+
}
112+
ctx.Values().Set("data", pkgV1.Page{Items: chartArray, Total: total})
113+
}
114+
}
115+
170116
func Install(parent iris.Party) {
171117
handler := NewHandler()
172118
sp := parent.Party("/charts")
173-
sp.Post("/search", handler.SearchCharts())
174-
sp.Post("/", handler.CreateChart())
175-
sp.Delete("/:name", handler.DeleteChart())
176119
sp.Put("/:name", handler.UpdateChart())
177120
sp.Get("/:name", handler.GetChart())
178121
sp.Get("/repos", handler.ListRepo())
179122
sp.Post("/repos", handler.AddRepo())
123+
sp.Delete("/repos/:name", handler.DeleteRepo())
124+
sp.Post("/search", handler.ListCharts())
180125
}

internal/api/v1/chart/types.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import (
44
v1Chart "github.com/KubeOperator/kubepi/internal/model/v1/chart"
55
)
66

7-
type Chart struct {
8-
v1Chart.Chart
9-
}
10-
117
type Repo struct {
128
Url string `json:"url"`
139
Name string `json:"name"`
@@ -16,3 +12,10 @@ type Repo struct {
1612
type RepoCreate struct {
1713
v1Chart.RepoCreate
1814
}
15+
16+
type Chart struct {
17+
Name string `json:"name"`
18+
Repo string `json:"repo"`
19+
Icon string `json:"icon"`
20+
Description string `json:"description"`
21+
}

internal/model/v1/chart/chart.go

-34
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,5 @@
11
package chart
22

3-
import (
4-
v1 "github.com/KubeOperator/kubepi/internal/model/v1"
5-
)
6-
7-
type Chart struct {
8-
v1.BaseModel `storm:"inline"`
9-
v1.Metadata `storm:"inline"`
10-
Type string `json:"type"`
11-
Cluster string `json:"cluster"`
12-
Spec Spec `json:"spec"`
13-
Status Status `json:"status"`
14-
}
15-
16-
type Spec struct {
17-
GitBranch string `json:"gitBranch"`
18-
GitRepo string `json:"gitRepo"`
19-
Url string `json:"url"`
20-
Authentication Authentication `json:"authentication"`
21-
}
22-
23-
type Authentication struct {
24-
Type string `json:"type"`
25-
Username string `json:"username"`
26-
Password string `json:"password"`
27-
PublicKey string `json:"publicKey"`
28-
PrivateKey string `json:"privateKey"`
29-
}
30-
31-
type Status struct {
32-
Version string `json:"version"`
33-
Phase string `json:"phase"`
34-
Message string `json:"message"`
35-
}
36-
373
type RepoCreate struct {
384
Url string `json:"url"`
395
Name string `json:"name"`

internal/server/server.go

+5-8
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ func (e *KubePiSerer) setUpDB() {
7575

7676
func (e *KubePiSerer) setUpStaticFile() {
7777
spaOption := iris.DirOptions{SPA: true, IndexName: "index.html"}
78-
7978
party := e.Party("/")
8079
party.Get("/", func(ctx *context.Context) {
8180
ctx.Redirect("/kubepi")
@@ -92,7 +91,6 @@ func (e *KubePiSerer) setUpStaticFile() {
9291
kubePiFS := iris.PrefixDir("web/kubepi", http.FS(EmbedWebKubePi))
9392
party.RegisterView(view.HTML(kubePiFS, ".html"))
9493
party.HandleDir("/kubepi/", kubePiFS, spaOption)
95-
9694
}
9795

9896
func (e *KubePiSerer) setUpSession() {
@@ -154,15 +152,14 @@ func (e *KubePiSerer) setUpErrHandler() {
154152
originMessage string
155153
)
156154

157-
switch message.(type) {
155+
switch value := message.(type) {
158156
case string:
159157
originMessage = message.(string)
160-
translateMessage, err = i18n.Translate(lang, message.(string))
158+
translateMessage, err = i18n.Translate(lang, value)
161159
case []string:
162-
ms := message.([]string)
163-
originMessage = strings.Join(ms, ",")
164-
if len(ms) > 0 {
165-
translateMessage, err = i18n.Translate(lang, ms[0], ms[1:])
160+
originMessage = strings.Join(value, ",")
161+
if len(value) > 0 {
162+
translateMessage, err = i18n.Translate(lang, value[0], value[1:])
166163
}
167164
}
168165
msg := translateMessage

0 commit comments

Comments
 (0)