swirl/biz/registry.go

99 lines
2.3 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"
"github.com/cuigh/swirl/model"
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 {
2021-12-16 12:23:08 +00:00
Search() ([]*model.Registry, error)
Find(id string) (*model.Registry, error)
2021-12-06 12:24:22 +00:00
GetAuth(url string) (auth string, err error)
Delete(id, name string, user web.User) (err error)
2021-12-16 12:23:08 +00:00
Create(registry *model.Registry, user web.User) (err error)
Update(registry *model.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
}
2021-12-16 12:23:08 +00:00
func (b *registryBiz) Create(r *model.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
2021-12-06 12:24:22 +00:00
err = b.d.RegistryCreate(context.TODO(), r)
if err == nil {
b.eb.CreateRegistry(EventActionCreate, r.ID, r.Name, user)
}
2017-09-26 12:50:09 +00:00
return
}
2021-12-16 12:23:08 +00:00
func (b *registryBiz) Update(r *model.Registry, user web.User) (err error) {
r.UpdatedAt = now()
r.UpdatedBy = newOperator(user)
2021-12-16 08:11:16 +00:00
err = b.d.RegistryUpdate(context.TODO(), 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
}
2021-12-16 12:23:08 +00:00
func (b *registryBiz) Search() (registries []*model.Registry, err error) {
registries, err = b.d.RegistryGetAll(context.TODO())
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
}
2021-12-16 12:23:08 +00:00
func (b *registryBiz) Find(id string) (registry *model.Registry, err error) {
registry, err = b.d.RegistryGet(context.TODO(), id)
if err == nil {
registry.Password = ""
2021-12-06 12:24:22 +00:00
}
2017-09-26 12:50:09 +00:00
return
}
2021-12-06 12:24:22 +00:00
func (b *registryBiz) GetAuth(url string) (auth string, err error) {
var (
r *model.Registry
buf []byte
)
if r, err = b.d.RegistryGetByURL(context.TODO(), url); err == nil && r != nil {
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
}
func (b *registryBiz) Delete(id, name string, user web.User) (err error) {
err = b.d.RegistryDelete(context.TODO(), id)
if err == nil {
b.eb.CreateRegistry(EventActionDelete, id, name, user)
}
2017-09-26 12:50:09 +00:00
return
}