portainer/portainer

View on GitHub
api/dataservices/endpointgroup/endpointgroup.go

Summary

Maintainability
A
0 mins
Test Coverage
package endpointgroup

import (
    portainer "github.com/portainer/portainer/api"
    "github.com/portainer/portainer/api/dataservices"
)

const BucketName = "endpoint_groups"

// Service represents a service for managing environment(endpoint) data.
type Service struct {
    dataservices.BaseDataService[portainer.EndpointGroup, portainer.EndpointGroupID]
}

// NewService creates a new instance of a service.
func NewService(connection portainer.Connection) (*Service, error) {
    err := connection.SetServiceName(BucketName)
    if err != nil {
        return nil, err
    }

    return &Service{
        BaseDataService: dataservices.BaseDataService[portainer.EndpointGroup, portainer.EndpointGroupID]{
            Bucket:     BucketName,
            Connection: connection,
        },
    }, nil
}

func (service *Service) Tx(tx portainer.Transaction) ServiceTx {
    return ServiceTx{
        BaseDataServiceTx: dataservices.BaseDataServiceTx[portainer.EndpointGroup, portainer.EndpointGroupID]{
            Bucket:     BucketName,
            Connection: service.Connection,
            Tx:         tx,
        },
    }
}

// CreateEndpointGroup assign an ID to a new environment(endpoint) group and saves it.
func (service *Service) Create(endpointGroup *portainer.EndpointGroup) error {
    return service.Connection.CreateObject(
        BucketName,
        func(id uint64) (int, interface{}) {
            endpointGroup.ID = portainer.EndpointGroupID(id)
            return int(endpointGroup.ID), endpointGroup
        },
    )
}