2021-12-06 12:24:22 +00:00
|
|
|
package biz
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/cuigh/auxo/data"
|
|
|
|
"github.com/cuigh/auxo/net/web"
|
|
|
|
"github.com/cuigh/swirl/docker"
|
|
|
|
"github.com/docker/docker/api/types/swarm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ConfigBiz interface {
|
2022-01-06 08:54:14 +00:00
|
|
|
Search(ctx context.Context, name string, pageIndex, pageSize int) (configs []*Config, total int, err error)
|
|
|
|
Find(ctx context.Context, id string) (config *Config, raw string, err error)
|
|
|
|
Delete(ctx context.Context, id, name string, user web.User) (err error)
|
|
|
|
Create(ctx context.Context, c *Config, user web.User) (err error)
|
|
|
|
Update(ctx context.Context, c *Config, user web.User) (err error)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewConfig(d *docker.Docker, eb EventBiz) ConfigBiz {
|
|
|
|
return &configBiz{d: d, eb: eb}
|
|
|
|
}
|
|
|
|
|
|
|
|
type configBiz struct {
|
|
|
|
d *docker.Docker
|
|
|
|
eb EventBiz
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
func (b *configBiz) Find(ctx context.Context, id string) (config *Config, raw string, err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
var (
|
|
|
|
c swarm.Config
|
|
|
|
r []byte
|
|
|
|
)
|
2022-01-06 08:54:14 +00:00
|
|
|
c, r, err = b.d.ConfigInspect(ctx, id)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err == nil {
|
|
|
|
raw, err = indentJSON(r)
|
|
|
|
}
|
|
|
|
if err == nil {
|
|
|
|
config = newConfig(&c)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
func (b *configBiz) Search(ctx context.Context, name string, pageIndex, pageSize int) ([]*Config, int, error) {
|
|
|
|
list, total, err := b.d.ConfigList(ctx, name, pageIndex, pageSize)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
configs := make([]*Config, len(list))
|
|
|
|
for i, n := range list {
|
|
|
|
configs[i] = newConfig(&n)
|
|
|
|
}
|
|
|
|
return configs, total, nil
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
func (b *configBiz) Delete(ctx context.Context, id, name string, user web.User) (err error) {
|
|
|
|
err = b.d.ConfigRemove(ctx, id)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err == nil {
|
|
|
|
b.eb.CreateConfig(EventActionDelete, id, name, user)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
func (b *configBiz) Create(ctx context.Context, c *Config, user web.User) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
spec := swarm.ConfigSpec{
|
|
|
|
Data: []byte(c.Data),
|
|
|
|
}
|
|
|
|
spec.Name = c.Name
|
|
|
|
spec.Labels = toMap(c.Labels)
|
|
|
|
if c.Templating.Name != "none" {
|
|
|
|
spec.Templating = &swarm.Driver{
|
|
|
|
Name: c.Templating.Name,
|
|
|
|
Options: toMap(c.Templating.Options),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var id string
|
2022-01-06 08:54:14 +00:00
|
|
|
id, err = b.d.ConfigCreate(ctx, &spec)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err == nil {
|
|
|
|
b.eb.CreateConfig(EventActionCreate, id, c.Name, user)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
func (b *configBiz) Update(ctx context.Context, c *Config, user web.User) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
spec := &swarm.ConfigSpec{
|
|
|
|
Data: []byte(c.Data),
|
|
|
|
}
|
|
|
|
spec.Name = c.Name
|
|
|
|
spec.Labels = toMap(c.Labels)
|
|
|
|
if c.Templating.Name != "" {
|
|
|
|
spec.Templating = &swarm.Driver{
|
|
|
|
Name: c.Templating.Name,
|
|
|
|
Options: toMap(c.Templating.Options),
|
|
|
|
}
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
err = b.d.ConfigUpdate(ctx, c.ID, c.Version, spec)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err == nil {
|
|
|
|
b.eb.CreateConfig(EventActionUpdate, c.ID, c.Name, user)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2021-12-16 12:23:08 +00:00
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Version uint64 `json:"version"`
|
|
|
|
Data string `json:"data"`
|
|
|
|
Labels data.Options `json:"labels,omitempty"`
|
|
|
|
Templating Driver `json:"templating"`
|
|
|
|
CreatedAt string `json:"createdAt"`
|
|
|
|
UpdatedAt string `json:"updatedAt"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Driver struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Options data.Options `json:"options,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func newConfig(c *swarm.Config) *Config {
|
|
|
|
config := &Config{
|
|
|
|
ID: c.ID,
|
|
|
|
Name: c.Spec.Name,
|
|
|
|
Version: c.Version.Index,
|
|
|
|
Data: string(c.Spec.Data),
|
|
|
|
Labels: mapToOptions(c.Spec.Labels),
|
|
|
|
CreatedAt: formatTime(c.CreatedAt),
|
|
|
|
UpdatedAt: formatTime(c.UpdatedAt),
|
|
|
|
}
|
|
|
|
if c.Spec.Templating != nil {
|
|
|
|
config.Templating.Name = c.Spec.Templating.Name
|
|
|
|
config.Templating.Options = mapToOptions(c.Spec.Templating.Options)
|
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|