swirl/biz/registry.go

98 lines
2.4 KiB
Go
Raw Normal View History

2017-09-26 12:50:09 +00:00
package biz
import (
2021-12-06 12:24:22 +00:00
"context"
"encoding/base64"
"encoding/json"
2017-09-26 12:50:09 +00:00
"github.com/cuigh/auxo/net/web"
"github.com/cuigh/swirl/dao"
2021-12-06 12:24:22 +00:00
"github.com/docker/docker/api/types"
2017-09-26 12:50:09 +00:00
)
2021-12-06 12:24:22 +00:00
type RegistryBiz interface {
2022-01-06 08:54:14 +00:00
Search(ctx context.Context) ([]*dao.Registry, error)
Find(ctx context.Context, id string) (*dao.Registry, error)
GetAuth(ctx context.Context, url string) (auth string, err error)
Delete(ctx context.Context, id, name string, user web.User) (err error)
Create(ctx context.Context, registry *dao.Registry, user web.User) (err error)
Update(ctx context.Context, registry *dao.Registry, user web.User) (err error)
2021-12-06 12:24:22 +00:00
}
func NewRegistry(d dao.Interface, eb EventBiz) RegistryBiz {
return &registryBiz{d: d, eb: eb}
}
2017-09-26 12:50:09 +00:00
type registryBiz struct {
2021-12-06 12:24:22 +00:00
d dao.Interface
eb EventBiz
2017-09-26 12:50:09 +00:00
}
2022-01-06 08:54:14 +00:00
func (b *registryBiz) Create(ctx context.Context, r *dao.Registry, user web.User) (err error) {
2021-12-06 12:24:22 +00:00
r.ID = createId()
2021-12-16 12:23:08 +00:00
r.CreatedAt = now()
2021-12-06 12:24:22 +00:00
r.UpdatedAt = r.CreatedAt
2021-12-16 12:23:08 +00:00
r.CreatedBy = newOperator(user)
2021-12-16 08:11:16 +00:00
r.UpdatedBy = r.CreatedBy
2017-09-26 12:50:09 +00:00
2022-01-06 08:54:14 +00:00
err = b.d.RegistryCreate(ctx, r)
2021-12-06 12:24:22 +00:00
if err == nil {
b.eb.CreateRegistry(EventActionCreate, r.ID, r.Name, user)
}
2017-09-26 12:50:09 +00:00
return
}
2022-01-06 08:54:14 +00:00
func (b *registryBiz) Update(ctx context.Context, r *dao.Registry, user web.User) (err error) {
2021-12-16 12:23:08 +00:00
r.UpdatedAt = now()
r.UpdatedBy = newOperator(user)
2022-01-06 08:54:14 +00:00
err = b.d.RegistryUpdate(ctx, r)
2021-12-06 12:24:22 +00:00
if err == nil {
2021-12-16 12:23:08 +00:00
b.eb.CreateRegistry(EventActionUpdate, r.ID, r.Name, user)
2021-12-06 12:24:22 +00:00
}
2017-09-26 12:50:09 +00:00
return
}
2022-01-06 08:54:14 +00:00
func (b *registryBiz) Search(ctx context.Context) (registries []*dao.Registry, err error) {
registries, err = b.d.RegistryGetAll(ctx)
2021-12-16 12:23:08 +00:00
if err == nil {
for _, r := range registries {
r.Password = ""
2021-12-06 12:24:22 +00:00
}
}
2017-09-26 12:50:09 +00:00
return
}
2022-01-06 08:54:14 +00:00
func (b *registryBiz) Find(ctx context.Context, id string) (registry *dao.Registry, err error) {
registry, err = b.d.RegistryGet(ctx, id)
2021-12-16 12:23:08 +00:00
if err == nil {
registry.Password = ""
2021-12-06 12:24:22 +00:00
}
2017-09-26 12:50:09 +00:00
return
}
2022-01-06 08:54:14 +00:00
func (b *registryBiz) GetAuth(ctx context.Context, url string) (auth string, err error) {
2021-12-06 12:24:22 +00:00
var (
2021-12-23 11:28:31 +00:00
r *dao.Registry
2021-12-06 12:24:22 +00:00
buf []byte
)
2022-01-06 08:54:14 +00:00
if r, err = b.d.RegistryGetByURL(ctx, url); err == nil && r != nil {
2021-12-06 12:24:22 +00:00
cfg := &types.AuthConfig{
ServerAddress: r.URL,
Username: r.Username,
Password: r.Password,
2017-09-26 12:50:09 +00:00
}
2021-12-06 12:24:22 +00:00
if buf, err = json.Marshal(cfg); err == nil {
auth = base64.URLEncoding.EncodeToString(buf)
2017-09-26 12:50:09 +00:00
}
2021-12-06 12:24:22 +00:00
}
return
}
2022-01-06 08:54:14 +00:00
func (b *registryBiz) Delete(ctx context.Context, id, name string, user web.User) (err error) {
err = b.d.RegistryDelete(ctx, id)
2021-12-06 12:24:22 +00:00
if err == nil {
b.eb.CreateRegistry(EventActionDelete, id, name, user)
}
2017-09-26 12:50:09 +00:00
return
}