diff --git a/core/clients/server.go b/core/clients/server.go index e47ea7d..82f44ea 100644 --- a/core/clients/server.go +++ b/core/clients/server.go @@ -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 @@ -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 } @@ -132,7 +134,7 @@ 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"` @@ -140,8 +142,11 @@ func (c *serverV1) WorkflowList(ctx context.Context, page v1.Pagination) ([]*v1. 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 } @@ -184,7 +189,7 @@ 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"` @@ -192,8 +197,11 @@ func (c *serverV1) BoxList(ctx context.Context, page v1.Pagination) ([]*v1.Box, 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 } diff --git a/core/command/ctl.go b/core/command/ctl.go index b9c938c..da955a0 100644 --- a/core/command/ctl.go +++ b/core/command/ctl.go @@ -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 } @@ -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 } @@ -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 } diff --git a/core/handler/server/box.go b/core/handler/server/box.go index 66f0f50..d5fcd20 100644 --- a/core/handler/server/box.go +++ b/core/handler/server/box.go @@ -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 diff --git a/core/handler/server/route.go b/core/handler/server/route.go index 8b3481e..c9f352a 100644 --- a/core/handler/server/route.go +++ b/core/handler/server/route.go @@ -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) { @@ -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)) @@ -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)) diff --git a/core/handler/server/secret.go b/core/handler/server/secret.go index 5f3e9e4..212a825 100644 --- a/core/handler/server/secret.go +++ b/core/handler/server/secret.go @@ -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 diff --git a/core/handler/server/workflow.go b/core/handler/server/workflow.go index 720b709..55af146 100644 --- a/core/handler/server/workflow.go +++ b/core/handler/server/workflow.go @@ -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 diff --git a/core/service/box/box.go b/core/service/box/box.go index 954458d..b47ec9f 100644 --- a/core/service/box/box.go +++ b/core/service/box/box.go @@ -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 } diff --git a/core/service/secret/secret.go b/core/service/secret/secret.go index 6c5f707..fcaeff5 100644 --- a/core/service/secret/secret.go +++ b/core/service/secret/secret.go @@ -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 } diff --git a/core/service/service.go b/core/service/service.go index 732fbbf..2ffee89 100644 --- a/core/service/service.go +++ b/core/service/service.go @@ -22,7 +22,7 @@ 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 @@ -30,7 +30,7 @@ type ( } 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 diff --git a/core/service/workflow/workflow.go b/core/service/workflow/workflow.go index 3021b0b..779637c 100644 --- a/core/service/workflow/workflow.go +++ b/core/service/workflow/workflow.go @@ -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 } diff --git a/pkg/api/core/v1/meta.go b/pkg/api/core/v1/meta.go index 3233487..65851aa 100644 --- a/pkg/api/core/v1/meta.go +++ b/pkg/api/core/v1/meta.go @@ -19,7 +19,10 @@ import ( "time" ) -const DefaultNamespace = "default" +const ( + DefaultNamespace = "default" + AllNamespace = "" +) const ( KindBox = "Box"