mirror of
https://github.com/portainer/portainer.git
synced 2025-07-24 07:49:41 +02:00
feat(dataservices): unify access methods and abstract away redundant code [EE-5628] (#9115)
This commit is contained in:
parent
4c6bbe9a2f
commit
4cc96b4b30
171 changed files with 714 additions and 1590 deletions
|
@ -13,11 +13,7 @@ const BucketName = "webhooks"
|
|||
|
||||
// Service represents a service for managing webhook data.
|
||||
type Service struct {
|
||||
connection portainer.Connection
|
||||
}
|
||||
|
||||
func (service *Service) BucketName() string {
|
||||
return BucketName
|
||||
dataservices.BaseDataService[portainer.Webhook, portainer.WebhookID]
|
||||
}
|
||||
|
||||
// NewService creates a new instance of a service.
|
||||
|
@ -28,39 +24,18 @@ func NewService(connection portainer.Connection) (*Service, error) {
|
|||
}
|
||||
|
||||
return &Service{
|
||||
connection: connection,
|
||||
BaseDataService: dataservices.BaseDataService[portainer.Webhook, portainer.WebhookID]{
|
||||
Bucket: BucketName,
|
||||
Connection: connection,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Webhooks returns an array of all webhooks
|
||||
func (service *Service) Webhooks() ([]portainer.Webhook, error) {
|
||||
var webhooks = make([]portainer.Webhook, 0)
|
||||
|
||||
return webhooks, service.connection.GetAll(
|
||||
BucketName,
|
||||
&portainer.Webhook{},
|
||||
dataservices.AppendFn(&webhooks),
|
||||
)
|
||||
}
|
||||
|
||||
// Webhook returns a webhook by ID.
|
||||
func (service *Service) Webhook(ID portainer.WebhookID) (*portainer.Webhook, error) {
|
||||
var webhook portainer.Webhook
|
||||
identifier := service.connection.ConvertToKey(int(ID))
|
||||
|
||||
err := service.connection.GetObject(BucketName, identifier, &webhook)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &webhook, nil
|
||||
}
|
||||
|
||||
// WebhookByResourceID returns a webhook by the ResourceID it is associated with.
|
||||
func (service *Service) WebhookByResourceID(ID string) (*portainer.Webhook, error) {
|
||||
var w portainer.Webhook
|
||||
|
||||
err := service.connection.GetAll(
|
||||
err := service.Connection.GetAll(
|
||||
BucketName,
|
||||
&portainer.Webhook{},
|
||||
dataservices.FirstFn(&w, func(e portainer.Webhook) bool {
|
||||
|
@ -83,7 +58,7 @@ func (service *Service) WebhookByResourceID(ID string) (*portainer.Webhook, erro
|
|||
func (service *Service) WebhookByToken(token string) (*portainer.Webhook, error) {
|
||||
var w portainer.Webhook
|
||||
|
||||
err := service.connection.GetAll(
|
||||
err := service.Connection.GetAll(
|
||||
BucketName,
|
||||
&portainer.Webhook{},
|
||||
dataservices.FirstFn(&w, func(e portainer.Webhook) bool {
|
||||
|
@ -102,15 +77,9 @@ func (service *Service) WebhookByToken(token string) (*portainer.Webhook, error)
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// DeleteWebhook deletes a webhook.
|
||||
func (service *Service) DeleteWebhook(ID portainer.WebhookID) error {
|
||||
identifier := service.connection.ConvertToKey(int(ID))
|
||||
return service.connection.DeleteObject(BucketName, identifier)
|
||||
}
|
||||
|
||||
// CreateWebhook assign an ID to a new webhook and saves it.
|
||||
func (service *Service) Create(webhook *portainer.Webhook) error {
|
||||
return service.connection.CreateObject(
|
||||
return service.Connection.CreateObject(
|
||||
BucketName,
|
||||
func(id uint64) (int, interface{}) {
|
||||
webhook.ID = portainer.WebhookID(id)
|
||||
|
@ -118,9 +87,3 @@ func (service *Service) Create(webhook *portainer.Webhook) error {
|
|||
},
|
||||
)
|
||||
}
|
||||
|
||||
// UpdateWebhook update a webhook.
|
||||
func (service *Service) UpdateWebhook(ID portainer.WebhookID, webhook *portainer.Webhook) error {
|
||||
identifier := service.connection.ConvertToKey(int(ID))
|
||||
return service.connection.UpdateObject(BucketName, identifier, webhook)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue