mirror of https://github.com/portainer/portainer
feat(roles): add transactions support EE-5390 (#8878)
parent
757461d58b
commit
745bbb7d79
|
@ -8,10 +8,8 @@ import (
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
// BucketName represents the name of the bucket where this service stores data.
|
||||||
// BucketName represents the name of the bucket where this service stores data.
|
const BucketName = "roles"
|
||||||
BucketName = "roles"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Service represents a service for managing environment(endpoint) data.
|
// Service represents a service for managing environment(endpoint) data.
|
||||||
type Service struct {
|
type Service struct {
|
||||||
|
@ -34,6 +32,13 @@ func NewService(connection portainer.Connection) (*Service, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (service *Service) Tx(tx portainer.Transaction) ServiceTx {
|
||||||
|
return ServiceTx{
|
||||||
|
service: service,
|
||||||
|
tx: tx,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Role returns a Role by ID
|
// Role returns a Role by ID
|
||||||
func (service *Service) Role(ID portainer.RoleID) (*portainer.Role, error) {
|
func (service *Service) Role(ID portainer.RoleID) (*portainer.Role, error) {
|
||||||
var set portainer.Role
|
var set portainer.Role
|
||||||
|
@ -47,7 +52,7 @@ func (service *Service) Role(ID portainer.RoleID) (*portainer.Role, error) {
|
||||||
return &set, nil
|
return &set, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Roles return an array containing all the sets.
|
// Roles returns an array containing all the sets.
|
||||||
func (service *Service) Roles() ([]portainer.Role, error) {
|
func (service *Service) Roles() ([]portainer.Role, error) {
|
||||||
var sets = make([]portainer.Role, 0)
|
var sets = make([]portainer.Role, 0)
|
||||||
|
|
||||||
|
@ -58,7 +63,7 @@ func (service *Service) Roles() ([]portainer.Role, error) {
|
||||||
set, ok := obj.(*portainer.Role)
|
set, ok := obj.(*portainer.Role)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Debug().Str("obj", fmt.Sprintf("%#v", obj)).Msg("failed to convert to Role object")
|
log.Debug().Str("obj", fmt.Sprintf("%#v", obj)).Msg("failed to convert to Role object")
|
||||||
return nil, fmt.Errorf("Failed to convert to Role object: %s", obj)
|
return nil, fmt.Errorf("failed to convert to Role object: %s", obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
sets = append(sets, *set)
|
sets = append(sets, *set)
|
||||||
|
|
|
@ -0,0 +1,71 @@
|
||||||
|
package role
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
portainer "github.com/portainer/portainer/api"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Service represents a service for managing environment(endpoint) data.
|
||||||
|
type ServiceTx struct {
|
||||||
|
service *Service
|
||||||
|
tx portainer.Transaction
|
||||||
|
}
|
||||||
|
|
||||||
|
func (service ServiceTx) BucketName() string {
|
||||||
|
return BucketName
|
||||||
|
}
|
||||||
|
|
||||||
|
// Role returns a Role by ID
|
||||||
|
func (service ServiceTx) Role(ID portainer.RoleID) (*portainer.Role, error) {
|
||||||
|
var set portainer.Role
|
||||||
|
identifier := service.service.connection.ConvertToKey(int(ID))
|
||||||
|
|
||||||
|
err := service.tx.GetObject(BucketName, identifier, &set)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &set, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Roles returns an array containing all the sets.
|
||||||
|
func (service ServiceTx) Roles() ([]portainer.Role, error) {
|
||||||
|
var sets = make([]portainer.Role, 0)
|
||||||
|
|
||||||
|
err := service.tx.GetAll(
|
||||||
|
BucketName,
|
||||||
|
&portainer.Role{},
|
||||||
|
func(obj interface{}) (interface{}, error) {
|
||||||
|
set, ok := obj.(*portainer.Role)
|
||||||
|
if !ok {
|
||||||
|
log.Debug().Str("obj", fmt.Sprintf("%#v", obj)).Msg("failed to convert to Role object")
|
||||||
|
return nil, fmt.Errorf("failed to convert to Role object: %s", obj)
|
||||||
|
}
|
||||||
|
|
||||||
|
sets = append(sets, *set)
|
||||||
|
|
||||||
|
return &portainer.Role{}, nil
|
||||||
|
})
|
||||||
|
|
||||||
|
return sets, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateRole creates a new Role.
|
||||||
|
func (service ServiceTx) Create(role *portainer.Role) error {
|
||||||
|
return service.tx.CreateObject(
|
||||||
|
BucketName,
|
||||||
|
func(id uint64) (int, interface{}) {
|
||||||
|
role.ID = portainer.RoleID(id)
|
||||||
|
return int(role.ID), role
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// UpdateRole updates a role.
|
||||||
|
func (service ServiceTx) UpdateRole(ID portainer.RoleID, role *portainer.Role) error {
|
||||||
|
identifier := service.service.connection.ConvertToKey(int(ID))
|
||||||
|
return service.tx.UpdateObject(BucketName, identifier, role)
|
||||||
|
}
|
|
@ -48,7 +48,11 @@ func (tx *StoreTx) Registry() dataservices.RegistryService {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tx *StoreTx) ResourceControl() dataservices.ResourceControlService { return nil }
|
func (tx *StoreTx) ResourceControl() dataservices.ResourceControlService { return nil }
|
||||||
func (tx *StoreTx) Role() dataservices.RoleService { return nil }
|
|
||||||
|
func (tx *StoreTx) Role() dataservices.RoleService {
|
||||||
|
return tx.store.RoleService.Tx(tx.tx)
|
||||||
|
}
|
||||||
|
|
||||||
func (tx *StoreTx) APIKeyRepository() dataservices.APIKeyRepository { return nil }
|
func (tx *StoreTx) APIKeyRepository() dataservices.APIKeyRepository { return nil }
|
||||||
func (tx *StoreTx) Settings() dataservices.SettingsService { return nil }
|
func (tx *StoreTx) Settings() dataservices.SettingsService { return nil }
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue