swirl/biz/registry.go

133 lines
3.1 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
"time"
"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 Registry struct {
ID string `json:"id,omitempty"`
Name string `json:"name" valid:"required"`
URL string `json:"url" valid:"required,url"`
Username string `json:"username" valid:"required"`
Password string `json:"password"`
CreatedAt string `json:"createdAt,omitempty"`
UpdatedAt string `json:"updatedAt,omitempty"`
}
func newRegistry(r *model.Registry) *Registry {
if r == nil {
return nil
}
return &Registry{
ID: r.ID,
Name: r.Name,
URL: r.URL,
Username: r.Username,
CreatedAt: formatTime(r.CreatedAt),
UpdatedAt: formatTime(r.UpdatedAt),
//Password: r.Password, // omit password
}
}
func (r *Registry) Convert() *model.Registry {
return &model.Registry{
ID: r.ID,
Name: r.Name,
URL: r.URL,
Username: r.Username,
Password: r.Password,
}
}
type RegistryBiz interface {
Search() ([]*Registry, error)
Find(id string) (*Registry, error)
GetAuth(url string) (auth string, err error)
Delete(id, name string, user web.User) (err error)
Create(registry *Registry, user web.User) (err error)
Update(registry *Registry, user web.User) (err error)
}
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-06 12:24:22 +00:00
func (b *registryBiz) Create(registry *Registry, user web.User) (err error) {
r := registry.Convert()
r.ID = createId()
r.CreatedAt = time.Now()
r.UpdatedAt = r.CreatedAt
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-06 12:24:22 +00:00
func (b *registryBiz) Update(registry *Registry, user web.User) (err error) {
err = b.d.RegistryUpdate(context.TODO(), registry.Convert())
if err == nil {
b.eb.CreateRegistry(EventActionUpdate, registry.ID, registry.Name, user)
}
2017-09-26 12:50:09 +00:00
return
}
2021-12-06 12:24:22 +00:00
func (b *registryBiz) Search() (registries []*Registry, err error) {
var list []*model.Registry
if list, err = b.d.RegistryList(context.TODO()); err == nil {
for _, r := range list {
registries = append(registries, newRegistry(r))
}
}
2017-09-26 12:50:09 +00:00
return
}
2021-12-06 12:24:22 +00:00
func (b *registryBiz) Find(id string) (registry *Registry, err error) {
var r *model.Registry
if r, err = b.d.RegistryGet(context.TODO(), id); err == nil {
registry = newRegistry(r)
}
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
}