2018-06-19 11:15:10 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2021-04-06 10:08:43 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api/bolt/internal"
|
2018-06-19 11:15:10 +00:00
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// BucketName represents the name of the bucket where this service stores data.
|
|
|
|
BucketName = "registries"
|
|
|
|
)
|
|
|
|
|
2021-09-20 00:14:22 +00:00
|
|
|
// Service represents a service for managing environment(endpoint) data.
|
2018-06-19 11:15:10 +00:00
|
|
|
type Service struct {
|
2021-04-06 10:08:43 +00:00
|
|
|
connection *internal.DbConnection
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewService creates a new instance of a service.
|
2021-04-06 10:08:43 +00:00
|
|
|
func NewService(connection *internal.DbConnection) (*Service, error) {
|
|
|
|
err := internal.CreateBucket(connection, BucketName)
|
2018-06-19 11:15:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Service{
|
2021-04-06 10:08:43 +00:00
|
|
|
connection: connection,
|
2018-06-19 11:15:10 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Registry returns an registry by ID.
|
|
|
|
func (service *Service) Registry(ID portainer.RegistryID) (*portainer.Registry, error) {
|
|
|
|
var registry portainer.Registry
|
|
|
|
identifier := internal.Itob(int(ID))
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := internal.GetObject(service.connection, BucketName, identifier, ®istry)
|
2018-06-19 11:15:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return ®istry, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Registries returns an array containing all the registries.
|
|
|
|
func (service *Service) Registries() ([]portainer.Registry, error) {
|
|
|
|
var registries = make([]portainer.Registry, 0)
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := service.connection.View(func(tx *bolt.Tx) error {
|
2018-06-19 11:15:10 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
cursor := bucket.Cursor()
|
|
|
|
for k, v := cursor.First(); k != nil; k, v = cursor.Next() {
|
|
|
|
var registry portainer.Registry
|
|
|
|
err := internal.UnmarshalObject(v, ®istry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
registries = append(registries, registry)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return registries, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateRegistry creates a new registry.
|
|
|
|
func (service *Service) CreateRegistry(registry *portainer.Registry) error {
|
2021-04-06 10:08:43 +00:00
|
|
|
return service.connection.Update(func(tx *bolt.Tx) error {
|
2018-06-19 11:15:10 +00:00
|
|
|
bucket := tx.Bucket([]byte(BucketName))
|
|
|
|
|
|
|
|
id, _ := bucket.NextSequence()
|
|
|
|
registry.ID = portainer.RegistryID(id)
|
|
|
|
|
|
|
|
data, err := internal.MarshalObject(registry)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bucket.Put(internal.Itob(int(registry.ID)), data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateRegistry updates an registry.
|
|
|
|
func (service *Service) UpdateRegistry(ID portainer.RegistryID, registry *portainer.Registry) error {
|
|
|
|
identifier := internal.Itob(int(ID))
|
2021-04-06 10:08:43 +00:00
|
|
|
return internal.UpdateObject(service.connection, BucketName, identifier, registry)
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteRegistry deletes an registry.
|
|
|
|
func (service *Service) DeleteRegistry(ID portainer.RegistryID) error {
|
|
|
|
identifier := internal.Itob(int(ID))
|
2021-04-06 10:08:43 +00:00
|
|
|
return internal.DeleteObject(service.connection, BucketName, identifier)
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|