Skip to content

Commit

Permalink
secret support paginate
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 committed Dec 22, 2023
1 parent aa9bb86 commit 7880104
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
50 changes: 31 additions & 19 deletions core/clients/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Server interface {
}

type ServerV1 interface {
SecretList(ctx context.Context, namespace string) ([]*v1.Secret, error)
SecretList(ctx context.Context, namespace string, opt v1.ListOption) ([]*v1.Secret, *v1.Pagination, 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
Expand Down Expand Up @@ -86,16 +86,24 @@ func (c *serverV1) R(ctx context.Context) *resty.Request {
return c.rc.R().SetContext(ctx)
}

func (c *serverV1) SecretList(ctx context.Context, namespace string) ([]*v1.Secret, error) {
var result []*v1.Secret
req := c.R(ctx).
SetPathParam("namespace", namespace).
SetResult(&result)
resp, err := req.Get("/secret/{namespace}")
func (c *serverV1) SecretList(ctx context.Context, namespace string, opt v1.ListOption) ([]*v1.Secret, *v1.Pagination, error) {
type resultT struct {
v1.Pagination
Items []*v1.Secret `json:"items"`
}

var result resultT
uri := "/secret"
req := c.R(ctx).SetResult(&result).SetQueryParamsFromValues(opt.ToValues())
if len(namespace) > 0 {
req.SetPathParam("namespace", namespace)
uri += "/{namespace}"

Check failure on line 100 in core/clients/server.go

View workflow job for this annotation

GitHub Actions / lint

string `/{namespace}` has 3 occurrences, make it a constant (goconst)
}
resp, err := req.Get(uri)
if err := handleClientError(resp, err); err != nil {
return nil, err
return nil, nil, err
}
return result, nil
return result.Items, &result.Pagination, nil
}

func (c *serverV1) SecretInfo(ctx context.Context, namespace, name string) (*v1.Secret, error) {
Expand Down Expand Up @@ -141,11 +149,13 @@ func (c *serverV1) WorkflowList(ctx context.Context, namespace string, opt v1.Li
}

var result resultT
req := c.R(ctx).
SetQueryParamsFromValues(opt.ToValues()).
SetPathParam("namespace", namespace).
SetResult(&result)
resp, err := req.Get("/workflow/{namespace}")
uri := "/workflow"
req := c.R(ctx).SetResult(&result).SetQueryParamsFromValues(opt.ToValues())
if len(namespace) > 0 {
req.SetPathParam("namespace", namespace)
uri += "/{namespace}"
}
resp, err := req.Get(uri)
if err := handleClientError(resp, err); err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -195,11 +205,13 @@ func (c *serverV1) BoxList(ctx context.Context, namespace string, opt v1.ListOpt
}

var result resultT
req := c.R(ctx).
SetQueryParamsFromValues(opt.ToValues()).
SetPathParam("namespace", namespace).
SetResult(&result)
resp, err := req.Get("/box/{namespace}")
uri := "/box"
req := c.R(ctx).SetResult(&result).SetQueryParamsFromValues(opt.ToValues())
if len(namespace) > 0 {
req.SetPathParam("namespace", namespace)
uri += "/{namespace}"
}
resp, err := req.Get(uri)
if err := handleClientError(resp, err); err != nil {
return nil, nil, err
}
Expand Down
23 changes: 20 additions & 3 deletions core/service/secret/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package secret
import (
"context"

"github.com/zc2638/ink/core/service/common"

Check failure on line 20 in core/service/secret/secret.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/zc2638/ink) -s blank --custom-order (gci)

"github.com/zc2638/ink/core/constant"
"github.com/zc2638/ink/core/service"

Check failure on line 23 in core/service/secret/secret.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s prefix(github.com/zc2638/ink) -s blank --custom-order (gci)
v1 "github.com/zc2638/ink/pkg/api/core/v1"
Expand All @@ -30,14 +32,29 @@ func New() service.Secret {

type srv struct{}

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

var list []storageV1.Secret
labels := opt.Labels()
if len(labels) > 0 {
names, err := common.SelectNamesByLabels(ctx, v1.KindBox, namespace, labels)
if err != nil {
return nil, err
}
if len(names) == 0 {
return nil, nil
}
db = db.Where("name in (?)", names)
}
if len(namespace) > 0 {
db = db.Where("namespace = ?", namespace)
}
if err := db.Find(&list).Error; err != nil {
if err := db.Model(&storageV1.Box{}).Count(&opt.Pagination.Total).Error; err != nil {
return nil, err
}

var list []storageV1.Secret
if err := db.Scopes(opt.Pagination.Scope).Find(&list).Error; err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion core/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type (
}

Secret interface {
List(ctx context.Context, namespace string) ([]*v1.Secret, error)
List(ctx context.Context, namespace string, opt v1.ListOption) ([]*v1.Secret, error)
Info(ctx context.Context, namespace, name string) (*v1.Secret, error)
Create(ctx context.Context, data *v1.Secret) error
Update(ctx context.Context, data *v1.Secret) error
Expand Down

0 comments on commit 7880104

Please sign in to comment.