Skip to content

Commit

Permalink
support namespace filter
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 committed Dec 18, 2023
1 parent 5d40fcb commit 6ee5e9e
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 24 deletions.
32 changes: 20 additions & 12 deletions core/clients/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ type Server interface {
}

type ServerV1 interface {
SecretList(ctx context.Context) ([]*v1.Secret, error)
SecretList(ctx context.Context, namespace string) ([]*v1.Secret, error)
SecretInfo(ctx context.Context, namespace, name string) (*v1.Secret, error)
SecretCreate(ctx context.Context, data *v1.Secret) error
SecretUpdate(ctx context.Context, data *v1.Secret) error
SecretDelete(ctx context.Context, namespace, name string) error

WorkflowList(ctx context.Context, page v1.Pagination) ([]*v1.Workflow, *v1.Pagination, error)
WorkflowList(ctx context.Context, namespace string, page v1.Pagination) ([]*v1.Workflow, *v1.Pagination, error)
WorkflowInfo(ctx context.Context, namespace, name string) (*v1.Workflow, error)
WorkflowCreate(ctx context.Context, data *v1.Workflow) error
WorkflowUpdate(ctx context.Context, data *v1.Workflow) error
WorkflowDelete(ctx context.Context, namespace, name string) error

BoxList(ctx context.Context, page v1.Pagination) ([]*v1.Box, *v1.Pagination, error)
BoxList(ctx context.Context, namespace string, page v1.Pagination) ([]*v1.Box, *v1.Pagination, error)
BoxInfo(ctx context.Context, namespace, name string) (*v1.Box, error)
BoxCreate(ctx context.Context, data *v1.Box) error
BoxUpdate(ctx context.Context, data *v1.Box) error
Expand Down Expand Up @@ -86,10 +86,12 @@ func (c *serverV1) R(ctx context.Context) *resty.Request {
return c.rc.R().SetContext(ctx)
}

func (c *serverV1) SecretList(ctx context.Context) ([]*v1.Secret, error) {
func (c *serverV1) SecretList(ctx context.Context, namespace string) ([]*v1.Secret, error) {
var result []*v1.Secret
req := c.R(ctx).SetResult(&result)
resp, err := req.Get("/secret")
req := c.R(ctx).
SetPathParam("namespace", namespace).
SetResult(&result)
resp, err := req.Get("/secret/{namespace}")
if err := handleClientError(resp, err); err != nil {
return nil, err
}
Expand Down Expand Up @@ -132,16 +134,19 @@ func (c *serverV1) SecretDelete(ctx context.Context, namespace, name string) err
return handleClientError(resp, err)
}

func (c *serverV1) WorkflowList(ctx context.Context, page v1.Pagination) ([]*v1.Workflow, *v1.Pagination, error) {
func (c *serverV1) WorkflowList(ctx context.Context, namespace string, page v1.Pagination) ([]*v1.Workflow, *v1.Pagination, error) {
type resultT struct {
v1.Pagination
Items []*v1.Workflow `json:"items"`
}

var result resultT
vs := page.ToValues()
req := c.R(ctx).SetQueryParamsFromValues(vs).SetResult(&result)
resp, err := req.Get("/workflow")
req := c.R(ctx).
SetQueryParamsFromValues(vs).
SetPathParam("namespace", namespace).
SetResult(&result)
resp, err := req.Get("/workflow/{namespace}")
if err := handleClientError(resp, err); err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -184,16 +189,19 @@ func (c *serverV1) WorkflowDelete(ctx context.Context, namespace, name string) e
return handleClientError(resp, err)
}

func (c *serverV1) BoxList(ctx context.Context, page v1.Pagination) ([]*v1.Box, *v1.Pagination, error) {
func (c *serverV1) BoxList(ctx context.Context, namespace string, page v1.Pagination) ([]*v1.Box, *v1.Pagination, error) {
type resultT struct {
v1.Pagination
Items []*v1.Box `json:"items"`
}

var result resultT
vs := page.ToValues()
req := c.R(ctx).SetQueryParamsFromValues(vs).SetResult(&result)
resp, err := req.Get("/box")
req := c.R(ctx).
SetQueryParamsFromValues(vs).
SetPathParam("namespace", namespace).
SetResult(&result)
resp, err := req.Get("/box/{namespace}")
if err := handleClientError(resp, err); err != nil {
return nil, nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions core/command/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func secretList(cmd *cobra.Command, _ []string) error {
if err != nil {
return err
}
result, err := sc.SecretList(context.Background())
result, err := sc.SecretList(context.Background(), v1.AllNamespace)
if err != nil {
return err
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func workflowList(cmd *cobra.Command, _ []string) error {
if err != nil {
return err
}
result, _, err := sc.WorkflowList(context.Background(), *getPage(cmd))
result, _, err := sc.WorkflowList(context.Background(), v1.AllNamespace, *getPage(cmd))
if err != nil {
return err
}
Expand Down Expand Up @@ -209,7 +209,7 @@ func boxList(cmd *cobra.Command, _ []string) error {
if err != nil {
return err
}
result, _, err := sc.BoxList(context.Background(), *getPage(cmd))
result, _, err := sc.BoxList(context.Background(), v1.AllNamespace, *getPage(cmd))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion core/handler/server/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ import (

func boxList(boxSrv service.Box) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
namespace := wrapper.URLParam(r, "namespace")
page := v1.GetPagination(r)

result, err := boxSrv.List(r.Context(), page)
result, err := boxSrv.List(r.Context(), namespace, page)
if err != nil {
wrapper.InternalError(w, err)
return
Expand Down
3 changes: 3 additions & 0 deletions core/handler/server/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func Handler(middlewares chi.Middlewares) http.Handler {

r.Route("/box", func(r chi.Router) {
r.Get("/", boxList(boxSrv))
r.Get("/{namespace}", boxList(boxSrv))
r.Post("/", boxCreate(boxSrv))

r.Route("/{namespace}/{name}", func(r chi.Router) {
Expand All @@ -59,6 +60,7 @@ func Handler(middlewares chi.Middlewares) http.Handler {

r.Route("/workflow", func(r chi.Router) {
r.Get("/", workflowList(workflowSrv))
r.Get("/{namespace}", workflowList(workflowSrv))
r.Post("/", workflowCreate(workflowSrv))
r.Route("/{namespace}/{name}", func(r chi.Router) {
r.Get("/", workflowInfo(workflowSrv))
Expand All @@ -69,6 +71,7 @@ func Handler(middlewares chi.Middlewares) http.Handler {

r.Route("/secret", func(r chi.Router) {
r.Get("/", secretList(secretSrv))
r.Get("/{namespace}", secretList(secretSrv))
r.Post("/", secretCreate(secretSrv))
r.Route("/{namespace}/{name}", func(r chi.Router) {
r.Get("/", secretInfo(secretSrv))
Expand Down
3 changes: 2 additions & 1 deletion core/handler/server/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import (

func secretList(secretSrv service.Secret) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
result, err := secretSrv.List(r.Context(), "")
namespace := wrapper.URLParam(r, "namespace")
result, err := secretSrv.List(r.Context(), namespace)
if err != nil {
wrapper.InternalError(w, err)
return
Expand Down
3 changes: 2 additions & 1 deletion core/handler/server/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ import (

func workflowList(workflowSrv service.Workflow) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
namespace := wrapper.URLParam(r, "namespace")
page := v1.GetPagination(r)
result, err := workflowSrv.List(r.Context(), page)
result, err := workflowSrv.List(r.Context(), namespace, page)
if err != nil {
wrapper.InternalError(w, err)
return
Expand Down
5 changes: 4 additions & 1 deletion core/service/box/box.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ func New() service.Box {

type srv struct{}

func (s *srv) List(ctx context.Context, page *v1.Pagination) ([]*v1.Box, error) {
func (s *srv) List(ctx context.Context, namespace string, page *v1.Pagination) ([]*v1.Box, error) {
db := database.FromContext(ctx)

var (
list []storageV1.Box
total int64
)
if len(namespace) > 0 {
db = db.Where("namespace = ?", namespace)
}
if err := db.Model(&storageV1.Box{}).Count(&total).Error; err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion core/service/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ func New() service.Secret {

type srv struct{}

func (s *srv) List(ctx context.Context, _ string) ([]*v1.Secret, error) {
func (s *srv) List(ctx context.Context, namespace string) ([]*v1.Secret, error) {
db := database.FromContext(ctx)

var list []storageV1.Secret
if len(namespace) > 0 {
db = db.Where("namespace = ?", namespace)
}
if err := db.Find(&list).Error; err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions core/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ import (

type (
Workflow interface {
List(ctx context.Context, page *v1.Pagination) ([]*v1.Workflow, error)
List(ctx context.Context, namespace string, page *v1.Pagination) ([]*v1.Workflow, error)
Info(ctx context.Context, namespace, name string) (*v1.Workflow, error)
Create(ctx context.Context, data *v1.Workflow) error
Update(ctx context.Context, data *v1.Workflow) error
Delete(ctx context.Context, namespace, name string) error
}

Box interface {
List(ctx context.Context, page *v1.Pagination) ([]*v1.Box, error)
List(ctx context.Context, namespace string, page *v1.Pagination) ([]*v1.Box, error)
Info(ctx context.Context, namespace, name string) (*v1.Box, error)
Create(ctx context.Context, data *v1.Box) error
Update(ctx context.Context, data *v1.Box) error
Expand Down
5 changes: 4 additions & 1 deletion core/service/workflow/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ func New() service.Workflow {

type srv struct{}

func (s *srv) List(ctx context.Context, page *v1.Pagination) ([]*v1.Workflow, error) {
func (s *srv) List(ctx context.Context, namespace string, page *v1.Pagination) ([]*v1.Workflow, error) {
db := database.FromContext(ctx)

var (
list []storageV1.Workflow
total int64
)
if len(namespace) > 0 {
db = db.Where("namespace = ?", namespace)
}
if err := db.Model(&storageV1.Workflow{}).Count(&total).Error; err != nil {
return nil, err
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/api/core/v1/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import (
"time"
)

const DefaultNamespace = "default"
const (
DefaultNamespace = "default"
AllNamespace = ""
)

const (
KindBox = "Box"
Expand Down

0 comments on commit 6ee5e9e

Please sign in to comment.