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 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 ®istryBiz{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
|
2021-12-16 08:11:16 +00:00
|
|
|
r.CreatedBy.ID = user.ID()
|
|
|
|
r.CreatedBy.Name = user.Name()
|
|
|
|
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-06 12:24:22 +00:00
|
|
|
func (b *registryBiz) Update(registry *Registry, user web.User) (err error) {
|
2021-12-16 08:11:16 +00:00
|
|
|
r := registry.Convert()
|
|
|
|
r.UpdatedAt = time.Now()
|
|
|
|
r.UpdatedBy.ID = user.ID()
|
|
|
|
r.UpdatedBy.Name = user.Name()
|
|
|
|
err = b.d.RegistryUpdate(context.TODO(), r)
|
2021-12-06 12:24:22 +00:00
|
|
|
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
|
2021-12-16 08:11:16 +00:00
|
|
|
if list, err = b.d.RegistryGetAll(context.TODO()); err == nil {
|
2021-12-06 12:24:22 +00:00
|
|
|
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
|
|
|
|
}
|
2021-12-16 08:11:16 +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" copier:"-"`
|
|
|
|
CreatedAt string `json:"createdAt,omitempty" copier:"-"`
|
|
|
|
UpdatedAt string `json:"updatedAt,omitempty" copier:"-"`
|
|
|
|
CreatedBy model.Operator `json:"createdBy" bson:"created_by"`
|
|
|
|
UpdatedBy model.Operator `json:"updatedBy" bson:"updated_by"`
|
|
|
|
}
|
|
|
|
|
|
|
|
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),
|
|
|
|
CreatedBy: r.CreatedBy,
|
|
|
|
UpdatedBy: r.UpdatedBy,
|
|
|
|
//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,
|
|
|
|
}
|
|
|
|
}
|