2018-06-19 11:15:10 +00:00
|
|
|
package resourcecontrol
|
|
|
|
|
|
|
|
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 = "resource_control"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceControl returns a ResourceControl object by ID
|
|
|
|
func (service *Service) ResourceControl(ID portainer.ResourceControlID) (*portainer.ResourceControl, error) {
|
|
|
|
var resourceControl portainer.ResourceControl
|
|
|
|
identifier := internal.Itob(int(ID))
|
|
|
|
|
2021-04-06 10:08:43 +00:00
|
|
|
err := internal.GetObject(service.connection, BucketName, identifier, &resourceControl)
|
2018-06-19 11:15:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resourceControl, nil
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
// ResourceControlByResourceIDAndType returns a ResourceControl object by checking if the resourceID is equal
|
|
|
|
// to the main ResourceID or in SubResourceIDs. It also performs a check on the resource type. Return nil
|
|
|
|
// if no ResourceControl was found.
|
|
|
|
func (service *Service) ResourceControlByResourceIDAndType(resourceID string, resourceType portainer.ResourceControlType) (*portainer.ResourceControl, error) {
|
2018-06-19 11:15:10 +00:00
|
|
|
var resourceControl *portainer.ResourceControl
|
|
|
|
|
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 rc portainer.ResourceControl
|
|
|
|
err := internal.UnmarshalObject(v, &rc)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-12 23:41:42 +00:00
|
|
|
if rc.ResourceID == resourceID && rc.Type == resourceType {
|
2018-06-19 11:15:10 +00:00
|
|
|
resourceControl = &rc
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, subResourceID := range rc.SubResourceIDs {
|
|
|
|
if subResourceID == resourceID {
|
|
|
|
resourceControl = &rc
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return resourceControl, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResourceControls returns all the ResourceControl objects
|
|
|
|
func (service *Service) ResourceControls() ([]portainer.ResourceControl, error) {
|
|
|
|
var rcs = make([]portainer.ResourceControl, 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 resourceControl portainer.ResourceControl
|
|
|
|
err := internal.UnmarshalObject(v, &resourceControl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rcs = append(rcs, resourceControl)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return rcs, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateResourceControl creates a new ResourceControl object
|
|
|
|
func (service *Service) CreateResourceControl(resourceControl *portainer.ResourceControl) 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()
|
|
|
|
resourceControl.ID = portainer.ResourceControlID(id)
|
|
|
|
|
|
|
|
data, err := internal.MarshalObject(resourceControl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return bucket.Put(internal.Itob(int(resourceControl.ID)), data)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateResourceControl saves a ResourceControl object.
|
|
|
|
func (service *Service) UpdateResourceControl(ID portainer.ResourceControlID, resourceControl *portainer.ResourceControl) error {
|
|
|
|
identifier := internal.Itob(int(ID))
|
2021-04-06 10:08:43 +00:00
|
|
|
return internal.UpdateObject(service.connection, BucketName, identifier, resourceControl)
|
2018-06-19 11:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteResourceControl deletes a ResourceControl object by ID
|
|
|
|
func (service *Service) DeleteResourceControl(ID portainer.ResourceControlID) 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
|
|
|
}
|