From 1598ec47fffeef97bf81549b4dd0409dfea9bb9c Mon Sep 17 00:00:00 2001 From: Sven Dowideit Date: Wed, 23 Feb 2022 15:57:06 +1000 Subject: [PATCH] move Connection interface to api/database, and edgejob type definitions to api/dataservices/edgejobs to reduce the number of deps on portainer., and make deps on specific structures more obvious Signed-off-by: Sven Dowideit --- api/chisel/schedules.go | 8 +- api/chisel/service.go | 11 +- api/chisel/tunnel.go | 12 +- api/{ => database}/connection.go | 2 +- api/database/database.go | 3 +- api/database/types.go | 6 + .../apikeyrepository/apikeyrepository.go | 5 +- .../customtemplate/customtemplate.go | 5 +- api/dataservices/dockerhub/dockerhub.go | 2 +- api/dataservices/edgegroup/edgegroup.go | 5 +- api/dataservices/edgejob/edgejob.go | 54 +++++--- api/dataservices/edgestack/edgestack.go | 5 +- api/dataservices/endpoint/endpoint.go | 14 +-- .../endpointgroup/endpointgroup.go | 5 +- .../endpointrelation/endpointrelation.go | 12 +- api/dataservices/extension/extension.go | 5 +- api/dataservices/fdoprofile/fdoprofile.go | 5 +- .../helmuserrepository/helmuserrepository.go | 5 +- api/dataservices/interface.go | 24 ++-- api/dataservices/registry/registry.go | 5 +- .../resourcecontrol/resourcecontrol.go | 5 +- api/dataservices/role/role.go | 5 +- api/dataservices/schedule/schedule.go | 5 +- api/dataservices/settings/settings.go | 5 +- api/dataservices/ssl/ssl.go | 5 +- api/dataservices/stack/stack.go | 5 +- api/dataservices/tag/tag.go | 5 +- api/dataservices/team/team.go | 5 +- .../teammembership/teammembership.go | 5 +- api/dataservices/tunnelserver/tunnelserver.go | 5 +- api/dataservices/user/user.go | 5 +- api/dataservices/version/version.go | 5 +- api/dataservices/webhook/webhook.go | 5 +- api/datastore/datastore.go | 3 +- api/datastore/datastore_test.go | 9 +- api/datastore/migrate_dbversion29_test.go | 2 +- api/datastore/migrator/migrate_dbversion22.go | 7 +- api/datastore/services.go | 5 +- api/datastore/teststore.go | 2 +- api/http/handler/backup/restore_test.go | 5 +- .../edgegroups/associated_endpoints.go | 9 +- .../handler/edgegroups/edgegroup_create.go | 7 +- api/http/handler/edgegroups/edgegroup_list.go | 3 +- .../handler/edgegroups/edgegroup_list_test.go | 25 ++-- .../handler/edgegroups/edgegroup_update.go | 7 +- api/http/handler/edgejobs/edgejob_create.go | 28 +++-- api/http/handler/edgejobs/edgejob_delete.go | 4 +- api/http/handler/edgejobs/edgejob_file.go | 4 +- api/http/handler/edgejobs/edgejob_inspect.go | 11 +- .../edgejobs/edgejob_tasklogs_clear.go | 6 +- .../edgejobs/edgejob_tasklogs_collect.go | 6 +- .../handler/edgejobs/edgejob_tasks_list.go | 11 +- api/http/handler/edgejobs/edgejob_update.go | 12 +- .../handler/edgestacks/edgestack_create.go | 9 +- .../edgestacks/edgestack_create_test.go | 5 +- .../edgestacks/edgestack_status_update.go | 5 +- .../handler/edgestacks/edgestack_update.go | 11 +- api/http/handler/edgestacks/endpoints.go | 7 +- api/http/handler/edgestacks/endpoints_test.go | 49 ++++---- api/http/handler/edgestacks/handler.go | 3 +- .../endpointedge/endpoint_edgejob_logs.go | 6 +- .../endpoint_edgestack_inspect.go | 3 +- .../endpointgroups/endpointgroup_create.go | 3 +- .../endpointgroup_endpoint_add.go | 3 +- .../endpointgroup_endpoint_delete.go | 3 +- api/http/handler/endpointproxy/proxy_azure.go | 5 +- .../handler/endpointproxy/proxy_docker.go | 3 +- .../handler/endpointproxy/proxy_kubernetes.go | 3 +- .../endpoints/endpoint_association_delete.go | 5 +- api/http/handler/endpoints/endpoint_delete.go | 10 +- .../endpoints/endpoint_dockerhub_status.go | 3 +- .../handler/endpoints/endpoint_inspect.go | 4 +- api/http/handler/endpoints/endpoint_list.go | 7 +- .../endpoints/endpoint_registries_inspect.go | 3 +- .../endpoints/endpoint_registries_list.go | 5 +- .../endpoints/endpoint_registry_access.go | 5 +- .../endpoints/endpoint_settings_update.go | 6 +- .../handler/endpoints/endpoint_snapshot.go | 3 +- .../endpoints/endpoint_status_inspect.go | 6 +- api/http/handler/endpoints/endpoint_update.go | 3 +- .../hostmanagement/openamt/amtactivation.go | 4 +- .../hostmanagement/openamt/amtdevices.go | 3 +- .../handler/hostmanagement/openamt/amtrpc.go | 19 +-- .../handler/kubernetes/kubernetes_config.go | 5 +- .../kubernetes/kubernetes_nodes_limits.go | 4 +- api/http/handler/stacks/handler.go | 7 +- api/http/handler/stacks/stack_associate.go | 3 +- api/http/handler/stacks/stack_create.go | 5 +- api/http/handler/stacks/stack_delete.go | 7 +- api/http/handler/stacks/stack_list.go | 3 +- api/http/handler/stacks/stack_migrate.go | 7 +- api/http/handler/stacks/stack_update.go | 3 +- api/http/handler/stacks/stack_update_git.go | 3 +- .../stacks/stack_update_git_redeploy.go | 3 +- api/http/handler/tags/tag_create.go | 3 +- api/http/handler/webhooks/webhook_create.go | 3 +- api/http/handler/webhooks/webhook_execute.go | 3 +- api/http/handler/webhooks/webhook_list.go | 3 +- api/http/handler/websocket/attach.go | 3 +- api/http/handler/websocket/exec.go | 3 +- api/http/handler/websocket/pod.go | 3 +- api/http/handler/websocket/shell_pod.go | 3 +- api/http/middlewares/endpoint.go | 3 +- api/http/proxy/factory/agent/transport.go | 3 +- .../proxy/factory/docker/access_control.go | 3 +- api/http/proxy/factory/docker/configs.go | 3 +- api/http/proxy/factory/docker/containers.go | 3 +- api/http/proxy/factory/docker/networks.go | 3 +- api/http/proxy/factory/docker/registry.go | 3 +- api/http/proxy/factory/docker/secrets.go | 3 +- api/http/proxy/factory/docker/services.go | 3 +- api/http/proxy/factory/docker/transport.go | 3 +- api/http/proxy/factory/docker/volumes.go | 3 +- api/http/proxy/factory/kubernetes/token.go | 3 +- api/http/proxy/manager.go | 3 +- api/http/security/authorization.go | 3 +- api/http/security/filter.go | 3 +- api/internal/authorization/authorizations.go | 3 +- api/internal/edge/edgegroup.go | 5 +- api/internal/edge/edgestack.go | 5 +- api/internal/endpointutils/endpoint_test.go | 35 +++--- api/internal/endpointutils/endpointutils.go | 5 +- api/internal/registryutils/access/access.go | 5 +- api/internal/registryutils/ecr_kube_secret.go | 3 +- api/internal/stackutils/stackutils.go | 3 +- api/internal/testhelpers/datastore.go | 32 ++--- .../testhelpers/reverse_tunnel_service.go | 18 +-- api/kubernetes/cli/client.go | 3 +- api/kubernetes/kubeconfig_service.go | 6 +- api/portainer.go | 115 +++++++----------- api/stacks/deploy.go | 3 +- api/stacks/deploy_test.go | 11 +- 132 files changed, 565 insertions(+), 434 deletions(-) rename api/{ => database}/connection.go (98%) create mode 100644 api/database/types.go diff --git a/api/chisel/schedules.go b/api/chisel/schedules.go index 6bcba574d..00f1468c4 100644 --- a/api/chisel/schedules.go +++ b/api/chisel/schedules.go @@ -1,13 +1,15 @@ package chisel import ( + "github.com/portainer/portainer/api/database" + "github.com/portainer/portainer/api/dataservices/edgejob" "strconv" portainer "github.com/portainer/portainer/api" ) // AddEdgeJob register an EdgeJob inside the tunnel details associated to an environment(endpoint). -func (service *Service) AddEdgeJob(endpointID portainer.EndpointID, edgeJob *portainer.EdgeJob) { +func (service *Service) AddEdgeJob(endpointID database.EndpointID, edgeJob *edgejob.EdgeJob) { tunnel := service.GetTunnelDetails(endpointID) existingJobIndex := -1 @@ -29,11 +31,11 @@ func (service *Service) AddEdgeJob(endpointID portainer.EndpointID, edgeJob *por } // RemoveEdgeJob will remove the specified Edge job from each tunnel it was registered with. -func (service *Service) RemoveEdgeJob(edgeJobID portainer.EdgeJobID) { +func (service *Service) RemoveEdgeJob(edgeJobID edgejob.EdgeJobID) { for item := range service.tunnelDetailsMap.IterBuffered() { tunnelDetails := item.Val.(*portainer.TunnelDetails) - updatedJobs := make([]portainer.EdgeJob, 0) + updatedJobs := make([]edgejob.EdgeJob, 0) for _, edgeJob := range tunnelDetails.Jobs { if edgeJob.ID == edgeJobID { continue diff --git a/api/chisel/service.go b/api/chisel/service.go index b2813bc7c..5c9bd5c65 100644 --- a/api/chisel/service.go +++ b/api/chisel/service.go @@ -3,6 +3,7 @@ package chisel import ( "context" "fmt" + "github.com/portainer/portainer/api/database" "github.com/portainer/portainer/api/http/proxy" "log" "net/http" @@ -46,7 +47,7 @@ func NewService(dataStore dataservices.DataStore, shutdownCtx context.Context) * } // pingAgent ping the given agent so that the agent can keep the tunnel alive -func (service *Service) pingAgent(endpointID portainer.EndpointID) error { +func (service *Service) pingAgent(endpointID database.EndpointID) error { tunnel := service.GetTunnelDetails(endpointID) requestURL := fmt.Sprintf("http://127.0.0.1:%d/ping", tunnel.Port) req, err := http.NewRequest(http.MethodHead, requestURL, nil) @@ -66,7 +67,7 @@ func (service *Service) pingAgent(endpointID portainer.EndpointID) error { } // KeepTunnelAlive keeps the tunnel of the given environment for maxAlive duration, or until ctx is done -func (service *Service) KeepTunnelAlive(endpointID portainer.EndpointID, ctx context.Context, maxAlive time.Duration) { +func (service *Service) KeepTunnelAlive(endpointID database.EndpointID, ctx context.Context, maxAlive time.Duration) { go func() { log.Printf("[DEBUG] [chisel,KeepTunnelAlive] [endpoint_id: %d] [message: start for %.0f minutes]\n", endpointID, maxAlive.Minutes()) maxAliveTicker := time.NewTicker(maxAlive) @@ -211,7 +212,7 @@ func (service *Service) checkTunnels() { log.Printf("[ERROR] [chisel,snapshot,conversion] Invalid environment identifier (id: %s): %s", item.Key, err) } - err = service.snapshotEnvironment(portainer.EndpointID(endpointID), tunnel.Port) + err = service.snapshotEnvironment(database.EndpointID(endpointID), tunnel.Port) if err != nil { log.Printf("[ERROR] [snapshot] Unable to snapshot Edge environment (id: %s): %s", item.Key, err) } @@ -223,11 +224,11 @@ func (service *Service) checkTunnels() { continue } - service.SetTunnelStatusToIdle(portainer.EndpointID(endpointID)) + service.SetTunnelStatusToIdle(database.EndpointID(endpointID)) } } -func (service *Service) snapshotEnvironment(endpointID portainer.EndpointID, tunnelPort int) error { +func (service *Service) snapshotEnvironment(endpointID database.EndpointID, tunnelPort int) error { endpoint, err := service.dataStore.Endpoint().Endpoint(endpointID) if err != nil { return err diff --git a/api/chisel/tunnel.go b/api/chisel/tunnel.go index 884c08baa..a028e6c34 100644 --- a/api/chisel/tunnel.go +++ b/api/chisel/tunnel.go @@ -3,6 +3,8 @@ package chisel import ( "encoding/base64" "fmt" + "github.com/portainer/portainer/api/database" + "github.com/portainer/portainer/api/dataservices/edgejob" "math/rand" "strconv" "strings" @@ -39,7 +41,7 @@ func randomInt(min, max int) int { } // GetTunnelDetails returns information about the tunnel associated to an environment(endpoint). -func (service *Service) GetTunnelDetails(endpointID portainer.EndpointID) *portainer.TunnelDetails { +func (service *Service) GetTunnelDetails(endpointID database.EndpointID) *portainer.TunnelDetails { key := strconv.Itoa(int(endpointID)) if item, ok := service.tunnelDetailsMap.Get(key); ok { @@ -47,7 +49,7 @@ func (service *Service) GetTunnelDetails(endpointID portainer.EndpointID) *porta return tunnelDetails } - jobs := make([]portainer.EdgeJob, 0) + jobs := make([]edgejob.EdgeJob, 0) return &portainer.TunnelDetails{ Status: portainer.EdgeAgentIdle, Port: 0, @@ -90,7 +92,7 @@ func (service *Service) GetActiveTunnel(endpoint *portainer.Endpoint) (*portaine // SetTunnelStatusToActive update the status of the tunnel associated to the specified environment(endpoint). // It sets the status to ACTIVE. -func (service *Service) SetTunnelStatusToActive(endpointID portainer.EndpointID) { +func (service *Service) SetTunnelStatusToActive(endpointID database.EndpointID) { tunnel := service.GetTunnelDetails(endpointID) tunnel.Status = portainer.EdgeAgentActive tunnel.Credentials = "" @@ -103,7 +105,7 @@ func (service *Service) SetTunnelStatusToActive(endpointID portainer.EndpointID) // SetTunnelStatusToIdle update the status of the tunnel associated to the specified environment(endpoint). // It sets the status to IDLE. // It removes any existing credentials associated to the tunnel. -func (service *Service) SetTunnelStatusToIdle(endpointID portainer.EndpointID) { +func (service *Service) SetTunnelStatusToIdle(endpointID database.EndpointID) { tunnel := service.GetTunnelDetails(endpointID) tunnel.Status = portainer.EdgeAgentIdle @@ -127,7 +129,7 @@ func (service *Service) SetTunnelStatusToIdle(endpointID portainer.EndpointID) { // If no port is currently associated to the tunnel, it will associate a random unused port to the tunnel // and generate temporary credentials that can be used to establish a reverse tunnel on that port. // Credentials are encrypted using the Edge ID associated to the environment(endpoint). -func (service *Service) SetTunnelStatusToRequired(endpointID portainer.EndpointID) error { +func (service *Service) SetTunnelStatusToRequired(endpointID database.EndpointID) error { tunnel := service.GetTunnelDetails(endpointID) if tunnel.Port == 0 { diff --git a/api/connection.go b/api/database/connection.go similarity index 98% rename from api/connection.go rename to api/database/connection.go index 0d5caffe0..d043e8c71 100644 --- a/api/connection.go +++ b/api/database/connection.go @@ -1,4 +1,4 @@ -package portainer +package database import ( "io" diff --git a/api/database/database.go b/api/database/database.go index 03feea8e9..bdc5b89fb 100644 --- a/api/database/database.go +++ b/api/database/database.go @@ -3,12 +3,11 @@ package database import ( "fmt" - portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/database/boltdb" ) // NewDatabase should use config options to return a connection to the requested database -func NewDatabase(storeType, storePath string, encryptionKey []byte) (connection portainer.Connection, err error) { +func NewDatabase(storeType, storePath string, encryptionKey []byte) (connection Connection, err error) { switch storeType { case "boltdb": return &boltdb.DbConnection{ diff --git a/api/database/types.go b/api/database/types.go new file mode 100644 index 000000000..e010f8eeb --- /dev/null +++ b/api/database/types.go @@ -0,0 +1,6 @@ +package database + +//TODO: as the dependencies get simpler, these should move to their respective dataservices + +// EndpointID represents an environment(endpoint) identifier +type EndpointID int diff --git a/api/dataservices/apikeyrepository/apikeyrepository.go b/api/dataservices/apikeyrepository/apikeyrepository.go index 1207b3deb..dbcb2707b 100644 --- a/api/dataservices/apikeyrepository/apikeyrepository.go +++ b/api/dataservices/apikeyrepository/apikeyrepository.go @@ -3,6 +3,7 @@ package apikeyrepository import ( "bytes" "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices/errors" @@ -16,11 +17,11 @@ const ( // Service represents a service for managing api-key data. type Service struct { - connection portainer.Connection + connection database.Connection } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/customtemplate/customtemplate.go b/api/dataservices/customtemplate/customtemplate.go index 2f27f54ea..0e2dd7fac 100644 --- a/api/dataservices/customtemplate/customtemplate.go +++ b/api/dataservices/customtemplate/customtemplate.go @@ -2,6 +2,7 @@ package customtemplate import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" @@ -14,7 +15,7 @@ const ( // Service represents a service for managing custom template data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +23,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/dockerhub/dockerhub.go b/api/dataservices/dockerhub/dockerhub.go index 61d81c2ca..26f09f6d1 100644 --- a/api/dataservices/dockerhub/dockerhub.go +++ b/api/dataservices/dockerhub/dockerhub.go @@ -1,7 +1,7 @@ package dockerhub import ( - portainer "github.com/portainer/portainer/api" + portainer "github.com/portainer/portainer/api/database" ) const ( diff --git a/api/dataservices/edgegroup/edgegroup.go b/api/dataservices/edgegroup/edgegroup.go index 3370970fe..0d9d4d60c 100644 --- a/api/dataservices/edgegroup/edgegroup.go +++ b/api/dataservices/edgegroup/edgegroup.go @@ -2,6 +2,7 @@ package edgegroup import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" @@ -14,7 +15,7 @@ const ( // Service represents a service for managing Edge group data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +23,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/edgejob/edgejob.go b/api/dataservices/edgejob/edgejob.go index edbb27736..e459bcb37 100644 --- a/api/dataservices/edgejob/edgejob.go +++ b/api/dataservices/edgejob/edgejob.go @@ -2,8 +2,7 @@ package edgejob import ( "fmt" - - portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" "github.com/sirupsen/logrus" ) @@ -14,7 +13,7 @@ const ( // Service represents a service for managing edge jobs data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +21,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err @@ -34,29 +33,29 @@ func NewService(connection portainer.Connection) (*Service, error) { } // EdgeJobs returns a list of Edge jobs -func (service *Service) EdgeJobs() ([]portainer.EdgeJob, error) { - var edgeJobs = make([]portainer.EdgeJob, 0) +func (service *Service) EdgeJobs() ([]EdgeJob, error) { + var edgeJobs = make([]EdgeJob, 0) err := service.connection.GetAll( BucketName, - &portainer.EdgeJob{}, + &EdgeJob{}, func(obj interface{}) (interface{}, error) { //var tag portainer.Tag - job, ok := obj.(*portainer.EdgeJob) + job, ok := obj.(*EdgeJob) if !ok { logrus.WithField("obj", obj).Errorf("Failed to convert to EdgeJob object") return nil, fmt.Errorf("Failed to convert to EdgeJob object: %s", obj) } edgeJobs = append(edgeJobs, *job) - return &portainer.EdgeJob{}, nil + return &EdgeJob{}, nil }) return edgeJobs, err } // EdgeJob returns an Edge job by ID -func (service *Service) EdgeJob(ID portainer.EdgeJobID) (*portainer.EdgeJob, error) { - var edgeJob portainer.EdgeJob +func (service *Service) EdgeJob(ID EdgeJobID) (*EdgeJob, error) { + var edgeJob EdgeJob identifier := service.connection.ConvertToKey(int(ID)) err := service.connection.GetObject(BucketName, identifier, &edgeJob) @@ -68,24 +67,24 @@ func (service *Service) EdgeJob(ID portainer.EdgeJobID) (*portainer.EdgeJob, err } // CreateEdgeJob creates a new Edge job -func (service *Service) Create(edgeJob *portainer.EdgeJob) error { +func (service *Service) Create(edgeJob *EdgeJob) error { return service.connection.CreateObject( BucketName, func(id uint64) (int, interface{}) { - edgeJob.ID = portainer.EdgeJobID(id) + edgeJob.ID = EdgeJobID(id) return int(edgeJob.ID), edgeJob }, ) } // UpdateEdgeJob updates an Edge job by ID -func (service *Service) UpdateEdgeJob(ID portainer.EdgeJobID, edgeJob *portainer.EdgeJob) error { +func (service *Service) UpdateEdgeJob(ID EdgeJobID, edgeJob *EdgeJob) error { identifier := service.connection.ConvertToKey(int(ID)) return service.connection.UpdateObject(BucketName, identifier, edgeJob) } // DeleteEdgeJob deletes an Edge job -func (service *Service) DeleteEdgeJob(ID portainer.EdgeJobID) error { +func (service *Service) DeleteEdgeJob(ID EdgeJobID) error { identifier := service.connection.ConvertToKey(int(ID)) return service.connection.DeleteObject(BucketName, identifier) } @@ -94,3 +93,28 @@ func (service *Service) DeleteEdgeJob(ID portainer.EdgeJobID) error { func (service *Service) GetNextIdentifier() int { return service.connection.GetNextIdentifier(BucketName) } + +// EdgeJob represents a job that can run on Edge environments(endpoints). +type EdgeJob struct { + // EdgeJob Identifier + ID EdgeJobID `json:"Id" example:"1"` + Created int64 `json:"Created"` + CronExpression string `json:"CronExpression"` + Endpoints map[database.EndpointID]EdgeJobEndpointMeta `json:"Endpoints"` + Name string `json:"Name"` + ScriptPath string `json:"ScriptPath"` + Recurring bool `json:"Recurring"` + Version int `json:"Version"` +} + +// EdgeJobEndpointMeta represents a meta data object for an Edge job and Environment(Endpoint) relation +type EdgeJobEndpointMeta struct { + LogsStatus EdgeJobLogsStatus + CollectLogs bool +} + +// EdgeJobID represents an Edge job identifier +type EdgeJobID int + +// EdgeJobLogsStatus represent status of logs collection job +type EdgeJobLogsStatus int diff --git a/api/dataservices/edgestack/edgestack.go b/api/dataservices/edgestack/edgestack.go index 78b7a4f4b..dec7f4486 100644 --- a/api/dataservices/edgestack/edgestack.go +++ b/api/dataservices/edgestack/edgestack.go @@ -2,6 +2,7 @@ package edgestack import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" @@ -14,7 +15,7 @@ const ( // Service represents a service for managing Edge stack data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +23,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/endpoint/endpoint.go b/api/dataservices/endpoint/endpoint.go index 52a6a9bc0..6e237b382 100644 --- a/api/dataservices/endpoint/endpoint.go +++ b/api/dataservices/endpoint/endpoint.go @@ -2,8 +2,8 @@ package endpoint import ( "fmt" - portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" "github.com/sirupsen/logrus" ) @@ -14,7 +14,7 @@ const ( // Service represents a service for managing environment(endpoint) data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +22,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err @@ -34,7 +34,7 @@ func NewService(connection portainer.Connection) (*Service, error) { } // Endpoint returns an environment(endpoint) by ID. -func (service *Service) Endpoint(ID portainer.EndpointID) (*portainer.Endpoint, error) { +func (service *Service) Endpoint(ID database.EndpointID) (*portainer.Endpoint, error) { var endpoint portainer.Endpoint identifier := service.connection.ConvertToKey(int(ID)) @@ -47,13 +47,13 @@ func (service *Service) Endpoint(ID portainer.EndpointID) (*portainer.Endpoint, } // UpdateEndpoint updates an environment(endpoint). -func (service *Service) UpdateEndpoint(ID portainer.EndpointID, endpoint *portainer.Endpoint) error { +func (service *Service) UpdateEndpoint(ID database.EndpointID, endpoint *portainer.Endpoint) error { identifier := service.connection.ConvertToKey(int(ID)) return service.connection.UpdateObject(BucketName, identifier, endpoint) } // DeleteEndpoint deletes an environment(endpoint). -func (service *Service) DeleteEndpoint(ID portainer.EndpointID) error { +func (service *Service) DeleteEndpoint(ID database.EndpointID) error { identifier := service.connection.ConvertToKey(int(ID)) return service.connection.DeleteObject(BucketName, identifier) } @@ -82,7 +82,7 @@ func (service *Service) Endpoints() ([]portainer.Endpoint, error) { func (service *Service) Create(endpoint *portainer.Endpoint) error { if int(endpoint.ID) == 0 { // TODO: hopefully this can become the only path - endpoint.ID = portainer.EndpointID(service.getNextIdentifier()) + endpoint.ID = database.EndpointID(service.getNextIdentifier()) } return service.connection.CreateObjectWithSetSequence(BucketName, int(endpoint.ID), endpoint) } diff --git a/api/dataservices/endpointgroup/endpointgroup.go b/api/dataservices/endpointgroup/endpointgroup.go index dc9f57ef0..d7216793d 100644 --- a/api/dataservices/endpointgroup/endpointgroup.go +++ b/api/dataservices/endpointgroup/endpointgroup.go @@ -2,6 +2,7 @@ package endpointgroup import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" @@ -14,7 +15,7 @@ const ( // Service represents a service for managing environment(endpoint) data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +23,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/endpointrelation/endpointrelation.go b/api/dataservices/endpointrelation/endpointrelation.go index 30e63354b..3ef5c6b10 100644 --- a/api/dataservices/endpointrelation/endpointrelation.go +++ b/api/dataservices/endpointrelation/endpointrelation.go @@ -2,8 +2,8 @@ package endpointrelation import ( "fmt" - portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" "github.com/sirupsen/logrus" ) @@ -14,7 +14,7 @@ const ( // Service represents a service for managing environment(endpoint) relation data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +22,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err @@ -54,7 +54,7 @@ func (service *Service) EndpointRelations() ([]portainer.EndpointRelation, error } // EndpointRelation returns a Environment(Endpoint) relation object by EndpointID -func (service *Service) EndpointRelation(endpointID portainer.EndpointID) (*portainer.EndpointRelation, error) { +func (service *Service) EndpointRelation(endpointID database.EndpointID) (*portainer.EndpointRelation, error) { var endpointRelation portainer.EndpointRelation identifier := service.connection.ConvertToKey(int(endpointID)) @@ -72,13 +72,13 @@ func (service *Service) Create(endpointRelation *portainer.EndpointRelation) err } // UpdateEndpointRelation updates an Environment(Endpoint) relation object -func (service *Service) UpdateEndpointRelation(EndpointID portainer.EndpointID, endpointRelation *portainer.EndpointRelation) error { +func (service *Service) UpdateEndpointRelation(EndpointID database.EndpointID, endpointRelation *portainer.EndpointRelation) error { identifier := service.connection.ConvertToKey(int(EndpointID)) return service.connection.UpdateObject(BucketName, identifier, endpointRelation) } // DeleteEndpointRelation deletes an Environment(Endpoint) relation object -func (service *Service) DeleteEndpointRelation(EndpointID portainer.EndpointID) error { +func (service *Service) DeleteEndpointRelation(EndpointID database.EndpointID) error { identifier := service.connection.ConvertToKey(int(EndpointID)) return service.connection.DeleteObject(BucketName, identifier) } diff --git a/api/dataservices/extension/extension.go b/api/dataservices/extension/extension.go index 9b501c0f7..4dd398d60 100644 --- a/api/dataservices/extension/extension.go +++ b/api/dataservices/extension/extension.go @@ -2,6 +2,7 @@ package extension import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" @@ -14,7 +15,7 @@ const ( // Service represents a service for managing environment(endpoint) data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +23,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/fdoprofile/fdoprofile.go b/api/dataservices/fdoprofile/fdoprofile.go index 4cb25e929..ec86327a3 100644 --- a/api/dataservices/fdoprofile/fdoprofile.go +++ b/api/dataservices/fdoprofile/fdoprofile.go @@ -2,6 +2,7 @@ package fdoprofile import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" @@ -14,7 +15,7 @@ const ( // Service represents a service for managingFDO Profiles data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +23,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/helmuserrepository/helmuserrepository.go b/api/dataservices/helmuserrepository/helmuserrepository.go index 0ae3a3564..a09b586f6 100644 --- a/api/dataservices/helmuserrepository/helmuserrepository.go +++ b/api/dataservices/helmuserrepository/helmuserrepository.go @@ -2,6 +2,7 @@ package helmuserrepository import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" @@ -14,7 +15,7 @@ const ( // Service represents a service for managing environment(endpoint) data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +23,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/interface.go b/api/dataservices/interface.go index 5a806f85e..446603956 100644 --- a/api/dataservices/interface.go +++ b/api/dataservices/interface.go @@ -3,6 +3,8 @@ package dataservices // "github.com/portainer/portainer/api/dataservices" import ( + "github.com/portainer/portainer/api/database" + "github.com/portainer/portainer/api/dataservices/edgejob" "io" "time" @@ -72,11 +74,11 @@ type ( // EdgeJobService represents a service to manage Edge jobs EdgeJobService interface { - EdgeJobs() ([]portainer.EdgeJob, error) - EdgeJob(ID portainer.EdgeJobID) (*portainer.EdgeJob, error) - Create(edgeJob *portainer.EdgeJob) error - UpdateEdgeJob(ID portainer.EdgeJobID, edgeJob *portainer.EdgeJob) error - DeleteEdgeJob(ID portainer.EdgeJobID) error + EdgeJobs() ([]edgejob.EdgeJob, error) + EdgeJob(ID edgejob.EdgeJobID) (*edgejob.EdgeJob, error) + Create(edgeJob *edgejob.EdgeJob) error + UpdateEdgeJob(ID edgejob.EdgeJobID, edgeJob *edgejob.EdgeJob) error + DeleteEdgeJob(ID edgejob.EdgeJobID) error GetNextIdentifier() int BucketName() string } @@ -94,11 +96,11 @@ type ( // EndpointService represents a service for managing environment(endpoint) data EndpointService interface { - Endpoint(ID portainer.EndpointID) (*portainer.Endpoint, error) + Endpoint(ID database.EndpointID) (*portainer.Endpoint, error) Endpoints() ([]portainer.Endpoint, error) Create(endpoint *portainer.Endpoint) error - UpdateEndpoint(ID portainer.EndpointID, endpoint *portainer.Endpoint) error - DeleteEndpoint(ID portainer.EndpointID) error + UpdateEndpoint(ID database.EndpointID, endpoint *portainer.Endpoint) error + DeleteEndpoint(ID database.EndpointID) error //GetNextIdentifier() int NewDefault() *portainer.Endpoint BucketName() string @@ -117,10 +119,10 @@ type ( // EndpointRelationService represents a service for managing environment(endpoint) relations data EndpointRelationService interface { EndpointRelations() ([]portainer.EndpointRelation, error) - EndpointRelation(EndpointID portainer.EndpointID) (*portainer.EndpointRelation, error) + EndpointRelation(EndpointID database.EndpointID) (*portainer.EndpointRelation, error) Create(endpointRelation *portainer.EndpointRelation) error - UpdateEndpointRelation(EndpointID portainer.EndpointID, endpointRelation *portainer.EndpointRelation) error - DeleteEndpointRelation(EndpointID portainer.EndpointID) error + UpdateEndpointRelation(EndpointID database.EndpointID, endpointRelation *portainer.EndpointRelation) error + DeleteEndpointRelation(EndpointID database.EndpointID) error BucketName() string } diff --git a/api/dataservices/registry/registry.go b/api/dataservices/registry/registry.go index 0fc5e3340..8ba6f53c9 100644 --- a/api/dataservices/registry/registry.go +++ b/api/dataservices/registry/registry.go @@ -2,6 +2,7 @@ package registry import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" @@ -14,7 +15,7 @@ const ( // Service represents a service for managing environment(endpoint) data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +23,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/resourcecontrol/resourcecontrol.go b/api/dataservices/resourcecontrol/resourcecontrol.go index 3d0486d0f..da89661fe 100644 --- a/api/dataservices/resourcecontrol/resourcecontrol.go +++ b/api/dataservices/resourcecontrol/resourcecontrol.go @@ -2,6 +2,7 @@ package resourcecontrol import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" @@ -14,7 +15,7 @@ const ( // Service represents a service for managing environment(endpoint) data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +23,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/role/role.go b/api/dataservices/role/role.go index 907004dae..0afa8cd59 100644 --- a/api/dataservices/role/role.go +++ b/api/dataservices/role/role.go @@ -2,6 +2,7 @@ package role import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" @@ -14,7 +15,7 @@ const ( // Service represents a service for managing environment(endpoint) data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +23,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/schedule/schedule.go b/api/dataservices/schedule/schedule.go index d6aac2713..7d1171a36 100644 --- a/api/dataservices/schedule/schedule.go +++ b/api/dataservices/schedule/schedule.go @@ -2,6 +2,7 @@ package schedule import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" @@ -14,7 +15,7 @@ const ( // Service represents a service for managing schedule data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +23,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/settings/settings.go b/api/dataservices/settings/settings.go index d5db9c7fe..08a82c643 100644 --- a/api/dataservices/settings/settings.go +++ b/api/dataservices/settings/settings.go @@ -2,6 +2,7 @@ package settings import ( portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" ) const ( @@ -12,7 +13,7 @@ const ( // Service represents a service for managing environment(endpoint) data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -20,7 +21,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/ssl/ssl.go b/api/dataservices/ssl/ssl.go index 58b5e3876..036d94fe6 100644 --- a/api/dataservices/ssl/ssl.go +++ b/api/dataservices/ssl/ssl.go @@ -2,6 +2,7 @@ package ssl import ( portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" ) const ( @@ -12,7 +13,7 @@ const ( // Service represents a service for managing ssl data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -20,7 +21,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/stack/stack.go b/api/dataservices/stack/stack.go index 7abd102a0..059800428 100644 --- a/api/dataservices/stack/stack.go +++ b/api/dataservices/stack/stack.go @@ -2,6 +2,7 @@ package stack import ( "fmt" + "github.com/portainer/portainer/api/database" "strings" "github.com/sirupsen/logrus" @@ -17,7 +18,7 @@ const ( // Service represents a service for managing environment(endpoint) data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -25,7 +26,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/tag/tag.go b/api/dataservices/tag/tag.go index b62c2eb76..a3499baae 100644 --- a/api/dataservices/tag/tag.go +++ b/api/dataservices/tag/tag.go @@ -2,6 +2,7 @@ package tag import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" @@ -14,7 +15,7 @@ const ( // Service represents a service for managing environment(endpoint) data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +23,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/team/team.go b/api/dataservices/team/team.go index 14aa1c131..60b817ce6 100644 --- a/api/dataservices/team/team.go +++ b/api/dataservices/team/team.go @@ -2,6 +2,7 @@ package team import ( "fmt" + "github.com/portainer/portainer/api/database" "strings" "github.com/portainer/portainer/api/dataservices/errors" @@ -17,7 +18,7 @@ const ( // Service represents a service for managing environment(endpoint) data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -25,7 +26,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/teammembership/teammembership.go b/api/dataservices/teammembership/teammembership.go index d80d95a37..9c6fb4794 100644 --- a/api/dataservices/teammembership/teammembership.go +++ b/api/dataservices/teammembership/teammembership.go @@ -2,6 +2,7 @@ package teammembership import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/sirupsen/logrus" @@ -14,7 +15,7 @@ const ( // Service represents a service for managing environment(endpoint) data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -22,7 +23,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/tunnelserver/tunnelserver.go b/api/dataservices/tunnelserver/tunnelserver.go index ea573e71d..195144b5c 100644 --- a/api/dataservices/tunnelserver/tunnelserver.go +++ b/api/dataservices/tunnelserver/tunnelserver.go @@ -2,6 +2,7 @@ package tunnelserver import ( portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" ) const ( @@ -12,7 +13,7 @@ const ( // Service represents a service for managing environment(endpoint) data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -20,7 +21,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/user/user.go b/api/dataservices/user/user.go index e508da851..7a8663eac 100644 --- a/api/dataservices/user/user.go +++ b/api/dataservices/user/user.go @@ -2,6 +2,7 @@ package user import ( "fmt" + "github.com/portainer/portainer/api/database" "strings" "github.com/portainer/portainer/api/dataservices/errors" @@ -17,7 +18,7 @@ const ( // Service represents a service for managing environment(endpoint) data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -25,7 +26,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/version/version.go b/api/dataservices/version/version.go index 0848c15ca..46492a7c2 100644 --- a/api/dataservices/version/version.go +++ b/api/dataservices/version/version.go @@ -1,6 +1,7 @@ package version import ( + "github.com/portainer/portainer/api/database" "strconv" portainer "github.com/portainer/portainer/api" @@ -17,7 +18,7 @@ const ( // Service represents a service to manage stored versions. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -25,7 +26,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/dataservices/webhook/webhook.go b/api/dataservices/webhook/webhook.go index ae16ec014..f1d8e84e2 100644 --- a/api/dataservices/webhook/webhook.go +++ b/api/dataservices/webhook/webhook.go @@ -2,6 +2,7 @@ package webhook import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices/errors" @@ -15,7 +16,7 @@ const ( // Service represents a service for managing webhook data. type Service struct { - connection portainer.Connection + connection database.Connection } func (service *Service) BucketName() string { @@ -23,7 +24,7 @@ func (service *Service) BucketName() string { } // NewService creates a new instance of a service. -func NewService(connection portainer.Connection) (*Service, error) { +func NewService(connection database.Connection) (*Service, error) { err := connection.SetServiceName(BucketName) if err != nil { return nil, err diff --git a/api/datastore/datastore.go b/api/datastore/datastore.go index b0530937c..56f9db45c 100644 --- a/api/datastore/datastore.go +++ b/api/datastore/datastore.go @@ -2,6 +2,7 @@ package datastore import ( "fmt" + "github.com/portainer/portainer/api/database" "io" "os" "path" @@ -29,7 +30,7 @@ func (store *Store) edition() portainer.SoftwareEdition { } // NewStore initializes a new Store and the associated services -func NewStore(storePath string, fileService portainer.FileService, connection portainer.Connection) *Store { +func NewStore(storePath string, fileService portainer.FileService, connection database.Connection) *Store { return &Store{ fileService: fileService, connection: connection, diff --git a/api/datastore/datastore_test.go b/api/datastore/datastore_test.go index 3a504965c..269bcfada 100644 --- a/api/datastore/datastore_test.go +++ b/api/datastore/datastore_test.go @@ -2,6 +2,7 @@ package datastore import ( "fmt" + "github.com/portainer/portainer/api/database" "runtime" "strings" "testing" @@ -83,7 +84,7 @@ func (store *Store) testEnvironments(t *testing.T) { store.CreateEndpointRelation(id) } -func newEndpoint(endpointType portainer.EndpointType, id portainer.EndpointID, name, URL string, TLS bool) *portainer.Endpoint { +func newEndpoint(endpointType portainer.EndpointType, id database.EndpointID, name, URL string, TLS bool) *portainer.Endpoint { endpoint := &portainer.Endpoint{ ID: id, Name: name, @@ -127,11 +128,11 @@ func setEndpointAuthorizations(endpoint *portainer.Endpoint) { } } -func (store *Store) CreateEndpoint(t *testing.T, name string, endpointType portainer.EndpointType, URL string, tls bool) portainer.EndpointID { +func (store *Store) CreateEndpoint(t *testing.T, name string, endpointType portainer.EndpointType, URL string, tls bool) database.EndpointID { is := assert.New(t) var expectedEndpoint *portainer.Endpoint - id := portainer.EndpointID(store.Endpoint().GetNextIdentifier()) + id := database.EndpointID(store.Endpoint().GetNextIdentifier()) switch endpointType { case portainer.DockerEnvironment: @@ -174,7 +175,7 @@ func (store *Store) CreateEndpoint(t *testing.T, name string, endpointType porta return endpoint.ID } -func (store *Store) CreateEndpointRelation(id portainer.EndpointID) { +func (store *Store) CreateEndpointRelation(id database.EndpointID) { relation := &portainer.EndpointRelation{ EndpointID: id, EdgeStacks: map[portainer.EdgeStackID]bool{}, diff --git a/api/datastore/migrate_dbversion29_test.go b/api/datastore/migrate_dbversion29_test.go index 914bffc74..3523fdf16 100644 --- a/api/datastore/migrate_dbversion29_test.go +++ b/api/datastore/migrate_dbversion29_test.go @@ -1,9 +1,9 @@ package datastore import ( + portainer "github.com/portainer/portainer/api/database" "testing" - portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/datastore/migrator" "github.com/portainer/portainer/api/internal/authorization" ) diff --git a/api/datastore/migrator/migrate_dbversion22.go b/api/datastore/migrator/migrate_dbversion22.go index b5466fe6a..b64d365f3 100644 --- a/api/datastore/migrator/migrate_dbversion22.go +++ b/api/datastore/migrator/migrate_dbversion22.go @@ -1,6 +1,9 @@ package migrator -import portainer "github.com/portainer/portainer/api" +import ( + portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" +) func (m *Migrator) updateTagsToDBVersion23() error { migrateLog.Info("Updating tags") @@ -11,7 +14,7 @@ func (m *Migrator) updateTagsToDBVersion23() error { for _, tag := range tags { tag.EndpointGroups = make(map[portainer.EndpointGroupID]bool) - tag.Endpoints = make(map[portainer.EndpointID]bool) + tag.Endpoints = make(map[database.EndpointID]bool) err = m.tagService.UpdateTag(tag.ID, &tag) if err != nil { return err diff --git a/api/datastore/services.go b/api/datastore/services.go index 26da1ef19..59bfefddb 100644 --- a/api/datastore/services.go +++ b/api/datastore/services.go @@ -2,6 +2,7 @@ package datastore import ( "encoding/json" + "github.com/portainer/portainer/api/database" "io/ioutil" "strconv" @@ -39,7 +40,7 @@ import ( // Store defines the implementation of portainer.DataStore using // BoltDB as the storage system. type Store struct { - connection portainer.Connection + connection database.Connection fileService portainer.FileService CustomTemplateService *customtemplate.Service @@ -348,7 +349,7 @@ func (store *Store) Webhook() dataservices.WebhookService { type storeExport struct { CustomTemplate []portainer.CustomTemplate `json:"customtemplates,omitempty"` EdgeGroup []portainer.EdgeGroup `json:"edgegroups,omitempty"` - EdgeJob []portainer.EdgeJob `json:"edgejobs,omitempty"` + EdgeJob []edgejob.EdgeJob `json:"edgejobs,omitempty"` EdgeStack []portainer.EdgeStack `json:"edge_stack,omitempty"` Endpoint []portainer.Endpoint `json:"endpoints,omitempty"` EndpointGroup []portainer.EndpointGroup `json:"endpoint_groups,omitempty"` diff --git a/api/datastore/teststore.go b/api/datastore/teststore.go index 7402365d4..78b8bf605 100644 --- a/api/datastore/teststore.go +++ b/api/datastore/teststore.go @@ -14,7 +14,7 @@ import ( var errTempDir = errors.New("can't create a temp dir") -func (store *Store) GetConnection() portainer.Connection { +func (store *Store) GetConnection() database.Connection { return store.connection } diff --git a/api/http/handler/backup/restore_test.go b/api/http/handler/backup/restore_test.go index bf9617248..19f18e29b 100644 --- a/api/http/handler/backup/restore_test.go +++ b/api/http/handler/backup/restore_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "fmt" + "github.com/portainer/portainer/api/dataservices/edgejob" "io" "mime/multipart" "net/http" @@ -48,7 +49,7 @@ func Test_restoreArchive_usingCombinationOfPasswords(t *testing.T) { for _, test := range tests { t.Run(test.name, func(t *testing.T) { - datastore := i.NewDatastore(i.WithUsers([]portainer.User{}), i.WithEdgeJobs([]portainer.EdgeJob{})) + datastore := i.NewDatastore(i.WithUsers([]portainer.User{}), i.WithEdgeJobs([]edgejob.EdgeJob{})) adminMonitor := adminmonitor.New(time.Hour, datastore, context.Background()) h := NewHandler(nil, datastore, offlinegate.NewOfflineGate(), "./test_assets/handler_test", func() {}, adminMonitor) @@ -71,7 +72,7 @@ func Test_restoreArchive_shouldFailIfSystemWasAlreadyInitialized(t *testing.T) { admin := portainer.User{ Role: portainer.AdministratorRole, } - datastore := i.NewDatastore(i.WithUsers([]portainer.User{admin}), i.WithEdgeJobs([]portainer.EdgeJob{})) + datastore := i.NewDatastore(i.WithUsers([]portainer.User{admin}), i.WithEdgeJobs([]edgejob.EdgeJob{})) adminMonitor := adminmonitor.New(time.Hour, datastore, context.Background()) h := NewHandler(nil, datastore, offlinegate.NewOfflineGate(), "./test_assets/handler_test", func() {}, adminMonitor) diff --git a/api/http/handler/edgegroups/associated_endpoints.go b/api/http/handler/edgegroups/associated_endpoints.go index b34711eda..d94f00a4a 100644 --- a/api/http/handler/edgegroups/associated_endpoints.go +++ b/api/http/handler/edgegroups/associated_endpoints.go @@ -2,13 +2,14 @@ package edgegroups import ( "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" ) -type endpointSetType map[portainer.EndpointID]bool +type endpointSetType map[database.EndpointID]bool -func (handler *Handler) getEndpointsByTags(tagIDs []portainer.TagID, partialMatch bool) ([]portainer.EndpointID, error) { +func (handler *Handler) getEndpointsByTags(tagIDs []portainer.TagID, partialMatch bool) ([]database.EndpointID, error) { if len(tagIDs) == 0 { - return []portainer.EndpointID{}, nil + return []database.EndpointID{}, nil } endpoints, err := handler.DataStore.Endpoint().Endpoints() @@ -36,7 +37,7 @@ func (handler *Handler) getEndpointsByTags(tagIDs []portainer.TagID, partialMatc endpointSet = setsIntersection(setsOfEndpoints) } - results := []portainer.EndpointID{} + results := []database.EndpointID{} for _, endpoint := range endpoints { if _, ok := endpointSet[endpoint.ID]; ok && (endpoint.Type == portainer.EdgeAgentOnDockerEnvironment || endpoint.Type == portainer.EdgeAgentOnKubernetesEnvironment) { results = append(results, endpoint.ID) diff --git a/api/http/handler/edgegroups/edgegroup_create.go b/api/http/handler/edgegroups/edgegroup_create.go index 26c2c3836..1225389dc 100644 --- a/api/http/handler/edgegroups/edgegroup_create.go +++ b/api/http/handler/edgegroups/edgegroup_create.go @@ -2,6 +2,7 @@ package edgegroups import ( "errors" + "github.com/portainer/portainer/api/database" "net/http" "github.com/asaskevich/govalidator" @@ -15,7 +16,7 @@ type edgeGroupCreatePayload struct { Name string Dynamic bool TagIDs []portainer.TagID - Endpoints []portainer.EndpointID + Endpoints []database.EndpointID PartialMatch bool } @@ -67,14 +68,14 @@ func (handler *Handler) edgeGroupCreate(w http.ResponseWriter, r *http.Request) Name: payload.Name, Dynamic: payload.Dynamic, TagIDs: []portainer.TagID{}, - Endpoints: []portainer.EndpointID{}, + Endpoints: []database.EndpointID{}, PartialMatch: payload.PartialMatch, } if edgeGroup.Dynamic { edgeGroup.TagIDs = payload.TagIDs } else { - endpointIDs := []portainer.EndpointID{} + endpointIDs := []database.EndpointID{} for _, endpointID := range payload.Endpoints { endpoint, err := handler.DataStore.Endpoint().Endpoint(endpointID) if err != nil { diff --git a/api/http/handler/edgegroups/edgegroup_list.go b/api/http/handler/edgegroups/edgegroup_list.go index 270c07356..f450071ae 100644 --- a/api/http/handler/edgegroups/edgegroup_list.go +++ b/api/http/handler/edgegroups/edgegroup_list.go @@ -2,6 +2,7 @@ package edgegroups import ( "fmt" + "github.com/portainer/portainer/api/database" "net/http" httperror "github.com/portainer/libhttp/error" @@ -76,7 +77,7 @@ func (handler *Handler) edgeGroupList(w http.ResponseWriter, r *http.Request) *h return response.JSON(w, decoratedEdgeGroups) } -func getEndpointTypes(endpointService dataservices.EndpointService, endpointIds []portainer.EndpointID) ([]portainer.EndpointType, error) { +func getEndpointTypes(endpointService dataservices.EndpointService, endpointIds []database.EndpointID) ([]portainer.EndpointType, error) { typeSet := map[portainer.EndpointType]bool{} for _, endpointID := range endpointIds { endpoint, err := endpointService.Endpoint(endpointID) diff --git a/api/http/handler/edgegroups/edgegroup_list_test.go b/api/http/handler/edgegroups/edgegroup_list_test.go index 36816fea9..1fe362105 100644 --- a/api/http/handler/edgegroups/edgegroup_list_test.go +++ b/api/http/handler/edgegroups/edgegroup_list_test.go @@ -1,6 +1,7 @@ package edgegroups import ( + "github.com/portainer/portainer/api/database" "testing" portainer "github.com/portainer/portainer/api" @@ -22,19 +23,19 @@ func Test_getEndpointTypes(t *testing.T) { datastore := testhelpers.NewDatastore(testhelpers.WithEndpoints(endpoints)) tests := []struct { - endpointIds []portainer.EndpointID + endpointIds []database.EndpointID expected []portainer.EndpointType }{ - {endpointIds: []portainer.EndpointID{1}, expected: []portainer.EndpointType{portainer.DockerEnvironment}}, - {endpointIds: []portainer.EndpointID{2}, expected: []portainer.EndpointType{portainer.AgentOnDockerEnvironment}}, - {endpointIds: []portainer.EndpointID{3}, expected: []portainer.EndpointType{portainer.AzureEnvironment}}, - {endpointIds: []portainer.EndpointID{4}, expected: []portainer.EndpointType{portainer.EdgeAgentOnDockerEnvironment}}, - {endpointIds: []portainer.EndpointID{5}, expected: []portainer.EndpointType{portainer.KubernetesLocalEnvironment}}, - {endpointIds: []portainer.EndpointID{6}, expected: []portainer.EndpointType{portainer.AgentOnKubernetesEnvironment}}, - {endpointIds: []portainer.EndpointID{7}, expected: []portainer.EndpointType{portainer.EdgeAgentOnKubernetesEnvironment}}, - {endpointIds: []portainer.EndpointID{7, 2}, expected: []portainer.EndpointType{portainer.EdgeAgentOnKubernetesEnvironment, portainer.AgentOnDockerEnvironment}}, - {endpointIds: []portainer.EndpointID{6, 4, 1}, expected: []portainer.EndpointType{portainer.AgentOnKubernetesEnvironment, portainer.EdgeAgentOnDockerEnvironment, portainer.DockerEnvironment}}, - {endpointIds: []portainer.EndpointID{1, 2, 3}, expected: []portainer.EndpointType{portainer.DockerEnvironment, portainer.AgentOnDockerEnvironment, portainer.AzureEnvironment}}, + {endpointIds: []database.EndpointID{1}, expected: []portainer.EndpointType{portainer.DockerEnvironment}}, + {endpointIds: []database.EndpointID{2}, expected: []portainer.EndpointType{portainer.AgentOnDockerEnvironment}}, + {endpointIds: []database.EndpointID{3}, expected: []portainer.EndpointType{portainer.AzureEnvironment}}, + {endpointIds: []database.EndpointID{4}, expected: []portainer.EndpointType{portainer.EdgeAgentOnDockerEnvironment}}, + {endpointIds: []database.EndpointID{5}, expected: []portainer.EndpointType{portainer.KubernetesLocalEnvironment}}, + {endpointIds: []database.EndpointID{6}, expected: []portainer.EndpointType{portainer.AgentOnKubernetesEnvironment}}, + {endpointIds: []database.EndpointID{7}, expected: []portainer.EndpointType{portainer.EdgeAgentOnKubernetesEnvironment}}, + {endpointIds: []database.EndpointID{7, 2}, expected: []portainer.EndpointType{portainer.EdgeAgentOnKubernetesEnvironment, portainer.AgentOnDockerEnvironment}}, + {endpointIds: []database.EndpointID{6, 4, 1}, expected: []portainer.EndpointType{portainer.AgentOnKubernetesEnvironment, portainer.EdgeAgentOnDockerEnvironment, portainer.DockerEnvironment}}, + {endpointIds: []database.EndpointID{1, 2, 3}, expected: []portainer.EndpointType{portainer.DockerEnvironment, portainer.AgentOnDockerEnvironment, portainer.AzureEnvironment}}, } for _, test := range tests { @@ -48,6 +49,6 @@ func Test_getEndpointTypes(t *testing.T) { func Test_getEndpointTypes_failWhenEndpointDontExist(t *testing.T) { datastore := testhelpers.NewDatastore(testhelpers.WithEndpoints([]portainer.Endpoint{})) - _, err := getEndpointTypes(datastore.Endpoint(), []portainer.EndpointID{1}) + _, err := getEndpointTypes(datastore.Endpoint(), []database.EndpointID{1}) assert.Error(t, err, "getEndpointTypes should fail") } diff --git a/api/http/handler/edgegroups/edgegroup_update.go b/api/http/handler/edgegroups/edgegroup_update.go index 8ca3ef187..28981b84e 100644 --- a/api/http/handler/edgegroups/edgegroup_update.go +++ b/api/http/handler/edgegroups/edgegroup_update.go @@ -2,6 +2,7 @@ package edgegroups import ( "errors" + "github.com/portainer/portainer/api/database" "net/http" "github.com/asaskevich/govalidator" @@ -16,7 +17,7 @@ type edgeGroupUpdatePayload struct { Name string Dynamic bool TagIDs []portainer.TagID - Endpoints []portainer.EndpointID + Endpoints []database.EndpointID PartialMatch *bool } @@ -95,7 +96,7 @@ func (handler *Handler) edgeGroupUpdate(w http.ResponseWriter, r *http.Request) if edgeGroup.Dynamic { edgeGroup.TagIDs = payload.TagIDs } else { - endpointIDs := []portainer.EndpointID{} + endpointIDs := []database.EndpointID{} for _, endpointID := range payload.Endpoints { endpoint, err := handler.DataStore.Endpoint().Endpoint(endpointID) if err != nil { @@ -131,7 +132,7 @@ func (handler *Handler) edgeGroupUpdate(w http.ResponseWriter, r *http.Request) return response.JSON(w, edgeGroup) } -func (handler *Handler) updateEndpoint(endpointID portainer.EndpointID) error { +func (handler *Handler) updateEndpoint(endpointID database.EndpointID) error { relation, err := handler.DataStore.EndpointRelation().EndpointRelation(endpointID) if err != nil { return err diff --git a/api/http/handler/edgejobs/edgejob_create.go b/api/http/handler/edgejobs/edgejob_create.go index bc4116b0c..42d5278e6 100644 --- a/api/http/handler/edgejobs/edgejob_create.go +++ b/api/http/handler/edgejobs/edgejob_create.go @@ -2,6 +2,8 @@ package edgejobs import ( "errors" + "github.com/portainer/portainer/api/database" + "github.com/portainer/portainer/api/dataservices/edgejob" "net/http" "strconv" "strings" @@ -48,7 +50,7 @@ type edgeJobCreateFromFileContentPayload struct { Name string CronExpression string Recurring bool - Endpoints []portainer.EndpointID + Endpoints []database.EndpointID FileContent string } @@ -97,7 +99,7 @@ type edgeJobCreateFromFilePayload struct { Name string CronExpression string Recurring bool - Endpoints []portainer.EndpointID + Endpoints []database.EndpointID File []byte } @@ -118,7 +120,7 @@ func (payload *edgeJobCreateFromFilePayload) Validate(r *http.Request) error { } payload.CronExpression = cronExpression - var endpoints []portainer.EndpointID + var endpoints []database.EndpointID err = request.RetrieveMultiPartFormJSONValue(r, "Environments", &endpoints, false) if err != nil { return errors.New("Invalid environments") @@ -151,12 +153,12 @@ func (handler *Handler) createEdgeJobFromFile(w http.ResponseWriter, r *http.Req return response.JSON(w, edgeJob) } -func (handler *Handler) createEdgeJobObjectFromFilePayload(payload *edgeJobCreateFromFilePayload) *portainer.EdgeJob { - edgeJobIdentifier := portainer.EdgeJobID(handler.DataStore.EdgeJob().GetNextIdentifier()) +func (handler *Handler) createEdgeJobObjectFromFilePayload(payload *edgeJobCreateFromFilePayload) *edgejob.EdgeJob { + edgeJobIdentifier := edgejob.EdgeJobID(handler.DataStore.EdgeJob().GetNextIdentifier()) endpoints := convertEndpointsToMetaObject(payload.Endpoints) - edgeJob := &portainer.EdgeJob{ + edgeJob := &edgejob.EdgeJob{ ID: edgeJobIdentifier, Name: payload.Name, CronExpression: payload.CronExpression, @@ -169,12 +171,12 @@ func (handler *Handler) createEdgeJobObjectFromFilePayload(payload *edgeJobCreat return edgeJob } -func (handler *Handler) createEdgeJobObjectFromFileContentPayload(payload *edgeJobCreateFromFileContentPayload) *portainer.EdgeJob { - edgeJobIdentifier := portainer.EdgeJobID(handler.DataStore.EdgeJob().GetNextIdentifier()) +func (handler *Handler) createEdgeJobObjectFromFileContentPayload(payload *edgeJobCreateFromFileContentPayload) *edgejob.EdgeJob { + edgeJobIdentifier := edgejob.EdgeJobID(handler.DataStore.EdgeJob().GetNextIdentifier()) endpoints := convertEndpointsToMetaObject(payload.Endpoints) - edgeJob := &portainer.EdgeJob{ + edgeJob := &edgejob.EdgeJob{ ID: edgeJobIdentifier, Name: payload.Name, CronExpression: payload.CronExpression, @@ -187,7 +189,7 @@ func (handler *Handler) createEdgeJobObjectFromFileContentPayload(payload *edgeJ return edgeJob } -func (handler *Handler) addAndPersistEdgeJob(edgeJob *portainer.EdgeJob, file []byte) error { +func (handler *Handler) addAndPersistEdgeJob(edgeJob *edgejob.EdgeJob, file []byte) error { edgeCronExpression := strings.Split(edgeJob.CronExpression, " ") if len(edgeCronExpression) == 6 { edgeCronExpression = edgeCronExpression[1:] @@ -222,11 +224,11 @@ func (handler *Handler) addAndPersistEdgeJob(edgeJob *portainer.EdgeJob, file [] return handler.DataStore.EdgeJob().Create(edgeJob) } -func convertEndpointsToMetaObject(endpoints []portainer.EndpointID) map[portainer.EndpointID]portainer.EdgeJobEndpointMeta { - endpointsMap := map[portainer.EndpointID]portainer.EdgeJobEndpointMeta{} +func convertEndpointsToMetaObject(endpoints []database.EndpointID) map[database.EndpointID]edgejob.EdgeJobEndpointMeta { + endpointsMap := map[database.EndpointID]edgejob.EdgeJobEndpointMeta{} for _, endpointID := range endpoints { - endpointsMap[endpointID] = portainer.EdgeJobEndpointMeta{} + endpointsMap[endpointID] = edgejob.EdgeJobEndpointMeta{} } return endpointsMap diff --git a/api/http/handler/edgejobs/edgejob_delete.go b/api/http/handler/edgejobs/edgejob_delete.go index 4397f7105..5ebf267ea 100644 --- a/api/http/handler/edgejobs/edgejob_delete.go +++ b/api/http/handler/edgejobs/edgejob_delete.go @@ -1,13 +1,13 @@ package edgejobs import ( + "github.com/portainer/portainer/api/dataservices/edgejob" "net/http" "strconv" httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" - portainer "github.com/portainer/portainer/api" ) // @id EdgeJobDelete @@ -28,7 +28,7 @@ func (handler *Handler) edgeJobDelete(w http.ResponseWriter, r *http.Request) *h return &httperror.HandlerError{http.StatusBadRequest, "Invalid Edge job identifier route variable", err} } - edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(portainer.EdgeJobID(edgeJobID)) + edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(edgejob.EdgeJobID(edgeJobID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an Edge job with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/edgejobs/edgejob_file.go b/api/http/handler/edgejobs/edgejob_file.go index 402b7dfc0..947da9827 100644 --- a/api/http/handler/edgejobs/edgejob_file.go +++ b/api/http/handler/edgejobs/edgejob_file.go @@ -1,12 +1,12 @@ package edgejobs import ( + "github.com/portainer/portainer/api/dataservices/edgejob" "net/http" httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" - portainer "github.com/portainer/portainer/api" ) type edgeJobFileResponse struct { @@ -32,7 +32,7 @@ func (handler *Handler) edgeJobFile(w http.ResponseWriter, r *http.Request) *htt return &httperror.HandlerError{http.StatusBadRequest, "Invalid Edge job identifier route variable", err} } - edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(portainer.EdgeJobID(edgeJobID)) + edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(edgejob.EdgeJobID(edgeJobID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an Edge job with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/edgejobs/edgejob_inspect.go b/api/http/handler/edgejobs/edgejob_inspect.go index 22a48cb78..aeb2a9803 100644 --- a/api/http/handler/edgejobs/edgejob_inspect.go +++ b/api/http/handler/edgejobs/edgejob_inspect.go @@ -1,17 +1,18 @@ package edgejobs import ( + "github.com/portainer/portainer/api/database" + "github.com/portainer/portainer/api/dataservices/edgejob" "net/http" httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" - portainer "github.com/portainer/portainer/api" ) type edgeJobInspectResponse struct { - *portainer.EdgeJob - Endpoints []portainer.EndpointID + *edgejob.EdgeJob + Endpoints []database.EndpointID } // @id EdgeJobInspect @@ -33,14 +34,14 @@ func (handler *Handler) edgeJobInspect(w http.ResponseWriter, r *http.Request) * return &httperror.HandlerError{http.StatusBadRequest, "Invalid Edge job identifier route variable", err} } - edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(portainer.EdgeJobID(edgeJobID)) + edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(edgejob.EdgeJobID(edgeJobID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an Edge job with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an Edge job with the specified identifier inside the database", err} } - endpointIDs := []portainer.EndpointID{} + endpointIDs := []database.EndpointID{} for endpointID := range edgeJob.Endpoints { endpointIDs = append(endpointIDs, endpointID) diff --git a/api/http/handler/edgejobs/edgejob_tasklogs_clear.go b/api/http/handler/edgejobs/edgejob_tasklogs_clear.go index 9f73a7462..dadf508b6 100644 --- a/api/http/handler/edgejobs/edgejob_tasklogs_clear.go +++ b/api/http/handler/edgejobs/edgejob_tasklogs_clear.go @@ -1,6 +1,8 @@ package edgejobs import ( + "github.com/portainer/portainer/api/database" + "github.com/portainer/portainer/api/dataservices/edgejob" "net/http" "strconv" @@ -35,14 +37,14 @@ func (handler *Handler) edgeJobTasksClear(w http.ResponseWriter, r *http.Request return &httperror.HandlerError{http.StatusBadRequest, "Invalid Task identifier route variable", err} } - edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(portainer.EdgeJobID(edgeJobID)) + edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(edgejob.EdgeJobID(edgeJobID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an Edge job with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an Edge job with the specified identifier inside the database", err} } - endpointID := portainer.EndpointID(taskID) + endpointID := database.EndpointID(taskID) meta := edgeJob.Endpoints[endpointID] meta.CollectLogs = false diff --git a/api/http/handler/edgejobs/edgejob_tasklogs_collect.go b/api/http/handler/edgejobs/edgejob_tasklogs_collect.go index 18fb3c6d5..eb73cc03c 100644 --- a/api/http/handler/edgejobs/edgejob_tasklogs_collect.go +++ b/api/http/handler/edgejobs/edgejob_tasklogs_collect.go @@ -1,6 +1,8 @@ package edgejobs import ( + "github.com/portainer/portainer/api/database" + "github.com/portainer/portainer/api/dataservices/edgejob" "net/http" httperror "github.com/portainer/libhttp/error" @@ -34,14 +36,14 @@ func (handler *Handler) edgeJobTasksCollect(w http.ResponseWriter, r *http.Reque return &httperror.HandlerError{http.StatusBadRequest, "Invalid Task identifier route variable", err} } - edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(portainer.EdgeJobID(edgeJobID)) + edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(edgejob.EdgeJobID(edgeJobID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an Edge job with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an Edge job with the specified identifier inside the database", err} } - endpointID := portainer.EndpointID(taskID) + endpointID := database.EndpointID(taskID) meta := edgeJob.Endpoints[endpointID] meta.CollectLogs = true diff --git a/api/http/handler/edgejobs/edgejob_tasks_list.go b/api/http/handler/edgejobs/edgejob_tasks_list.go index caefb8806..6a5a832d9 100644 --- a/api/http/handler/edgejobs/edgejob_tasks_list.go +++ b/api/http/handler/edgejobs/edgejob_tasks_list.go @@ -2,18 +2,19 @@ package edgejobs import ( "fmt" + "github.com/portainer/portainer/api/database" + "github.com/portainer/portainer/api/dataservices/edgejob" "net/http" httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" - portainer "github.com/portainer/portainer/api" ) type taskContainer struct { - ID string `json:"Id"` - EndpointID portainer.EndpointID `json:"EndpointId"` - LogsStatus portainer.EdgeJobLogsStatus `json:"LogsStatus"` + ID string `json:"Id"` + EndpointID database.EndpointID `json:"EndpointId"` + LogsStatus edgejob.EdgeJobLogsStatus `json:"LogsStatus"` } // @id EdgeJobTasksList @@ -35,7 +36,7 @@ func (handler *Handler) edgeJobTasksList(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid Edge job identifier route variable", err} } - edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(portainer.EdgeJobID(edgeJobID)) + edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(edgejob.EdgeJobID(edgeJobID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an Edge job with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/edgejobs/edgejob_update.go b/api/http/handler/edgejobs/edgejob_update.go index 3c734c36e..af8d77bb6 100644 --- a/api/http/handler/edgejobs/edgejob_update.go +++ b/api/http/handler/edgejobs/edgejob_update.go @@ -2,6 +2,8 @@ package edgejobs import ( "errors" + "github.com/portainer/portainer/api/database" + "github.com/portainer/portainer/api/dataservices/edgejob" "net/http" "strconv" @@ -16,7 +18,7 @@ type edgeJobUpdatePayload struct { Name *string CronExpression *string Recurring *bool - Endpoints []portainer.EndpointID + Endpoints []database.EndpointID FileContent *string } @@ -54,7 +56,7 @@ func (handler *Handler) edgeJobUpdate(w http.ResponseWriter, r *http.Request) *h return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(portainer.EdgeJobID(edgeJobID)) + edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(edgejob.EdgeJobID(edgeJobID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an Edge job with the specified identifier inside the database", err} } else if err != nil { @@ -74,13 +76,13 @@ func (handler *Handler) edgeJobUpdate(w http.ResponseWriter, r *http.Request) *h return response.JSON(w, edgeJob) } -func (handler *Handler) updateEdgeSchedule(edgeJob *portainer.EdgeJob, payload *edgeJobUpdatePayload) error { +func (handler *Handler) updateEdgeSchedule(edgeJob *edgejob.EdgeJob, payload *edgeJobUpdatePayload) error { if payload.Name != nil { edgeJob.Name = *payload.Name } if payload.Endpoints != nil { - endpointsMap := map[portainer.EndpointID]portainer.EdgeJobEndpointMeta{} + endpointsMap := map[database.EndpointID]edgejob.EdgeJobEndpointMeta{} for _, endpointID := range payload.Endpoints { endpoint, err := handler.DataStore.Endpoint().Endpoint(endpointID) @@ -95,7 +97,7 @@ func (handler *Handler) updateEdgeSchedule(edgeJob *portainer.EdgeJob, payload * if meta, ok := edgeJob.Endpoints[endpointID]; ok { endpointsMap[endpointID] = meta } else { - endpointsMap[endpointID] = portainer.EdgeJobEndpointMeta{} + endpointsMap[endpointID] = edgejob.EdgeJobEndpointMeta{} } } diff --git a/api/http/handler/edgestacks/edgestack_create.go b/api/http/handler/edgestacks/edgestack_create.go index 9c7344d77..62e6e52a1 100644 --- a/api/http/handler/edgestacks/edgestack_create.go +++ b/api/http/handler/edgestacks/edgestack_create.go @@ -3,6 +3,7 @@ package edgestacks import ( "errors" "fmt" + "github.com/portainer/portainer/api/database" "net/http" "strconv" "strings" @@ -105,7 +106,7 @@ func (handler *Handler) createSwarmStackFromFileContent(r *http.Request) (*porta DeploymentType: payload.DeploymentType, CreationDate: time.Now().Unix(), EdgeGroups: payload.EdgeGroups, - Status: make(map[portainer.EndpointID]portainer.EdgeStackStatus), + Status: make(map[database.EndpointID]portainer.EdgeStackStatus), Version: 1, } @@ -228,7 +229,7 @@ func (handler *Handler) createSwarmStackFromGitRepository(r *http.Request) (*por Name: payload.Name, CreationDate: time.Now().Unix(), EdgeGroups: payload.EdgeGroups, - Status: make(map[portainer.EndpointID]portainer.EdgeStackStatus), + Status: make(map[database.EndpointID]portainer.EdgeStackStatus), DeploymentType: payload.DeploymentType, Version: 1, } @@ -337,7 +338,7 @@ func (handler *Handler) createSwarmStackFromFileUpload(r *http.Request) (*portai DeploymentType: payload.DeploymentType, CreationDate: time.Now().Unix(), EdgeGroups: payload.EdgeGroups, - Status: make(map[portainer.EndpointID]portainer.EdgeStackStatus), + Status: make(map[database.EndpointID]portainer.EdgeStackStatus), Version: 1, } @@ -404,7 +405,7 @@ func (handler *Handler) validateUniqueName(name string) error { } // updateEndpointRelations adds a relation between the Edge Stack to the related environments(endpoints) -func updateEndpointRelations(endpointRelationService dataservices.EndpointRelationService, edgeStackID portainer.EdgeStackID, relatedEndpointIds []portainer.EndpointID) error { +func updateEndpointRelations(endpointRelationService dataservices.EndpointRelationService, edgeStackID portainer.EdgeStackID, relatedEndpointIds []database.EndpointID) error { for _, endpointID := range relatedEndpointIds { relation, err := endpointRelationService.EndpointRelation(endpointID) if err != nil { diff --git a/api/http/handler/edgestacks/edgestack_create_test.go b/api/http/handler/edgestacks/edgestack_create_test.go index 8e1144e4a..d0dfe9806 100644 --- a/api/http/handler/edgestacks/edgestack_create_test.go +++ b/api/http/handler/edgestacks/edgestack_create_test.go @@ -1,6 +1,7 @@ package edgestacks import ( + "github.com/portainer/portainer/api/database" "testing" portainer "github.com/portainer/portainer/api" @@ -18,7 +19,7 @@ func Test_updateEndpointRelation_successfulRuns(t *testing.T) { {EndpointID: 5, EdgeStacks: map[portainer.EdgeStackID]bool{}}, } - relatedIds := []portainer.EndpointID{2, 3} + relatedIds := []database.EndpointID{2, 3} dataStore := testhelpers.NewDatastore(testhelpers.WithEndpointRelations(endpointRelations)) @@ -26,7 +27,7 @@ func Test_updateEndpointRelation_successfulRuns(t *testing.T) { assert.NoError(t, err, "updateEndpointRelations should not fail") - relatedSet := map[portainer.EndpointID]bool{} + relatedSet := map[database.EndpointID]bool{} for _, relationID := range relatedIds { relatedSet[relationID] = true } diff --git a/api/http/handler/edgestacks/edgestack_status_update.go b/api/http/handler/edgestacks/edgestack_status_update.go index 41bf05f21..7fffe63c3 100644 --- a/api/http/handler/edgestacks/edgestack_status_update.go +++ b/api/http/handler/edgestacks/edgestack_status_update.go @@ -2,6 +2,7 @@ package edgestacks import ( "errors" + "github.com/portainer/portainer/api/database" "net/http" "github.com/asaskevich/govalidator" @@ -14,7 +15,7 @@ import ( type updateStatusPayload struct { Error string Status *portainer.EdgeStackStatusType - EndpointID *portainer.EndpointID + EndpointID *database.EndpointID } func (payload *updateStatusPayload) Validate(r *http.Request) error { @@ -62,7 +63,7 @@ func (handler *Handler) edgeStackStatusUpdate(w http.ResponseWriter, r *http.Req return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(*payload.EndpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(*payload.EndpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/edgestacks/edgestack_update.go b/api/http/handler/edgestacks/edgestack_update.go index 8a0efc070..8af41e4aa 100644 --- a/api/http/handler/edgestacks/edgestack_update.go +++ b/api/http/handler/edgestacks/edgestack_update.go @@ -2,6 +2,7 @@ package edgestacks import ( "errors" + "github.com/portainer/portainer/api/database" "net/http" "strconv" @@ -83,7 +84,7 @@ func (handler *Handler) edgeStackUpdate(w http.ResponseWriter, r *http.Request) oldRelatedSet := EndpointSet(relatedEndpointIds) newRelatedSet := EndpointSet(newRelated) - endpointsToRemove := map[portainer.EndpointID]bool{} + endpointsToRemove := map[database.EndpointID]bool{} for endpointID := range oldRelatedSet { if !newRelatedSet[endpointID] { endpointsToRemove[endpointID] = true @@ -104,7 +105,7 @@ func (handler *Handler) edgeStackUpdate(w http.ResponseWriter, r *http.Request) } } - endpointsToAdd := map[portainer.EndpointID]bool{} + endpointsToAdd := map[database.EndpointID]bool{} for endpointID := range newRelatedSet { if !oldRelatedSet[endpointID] { endpointsToAdd[endpointID] = true @@ -179,7 +180,7 @@ func (handler *Handler) edgeStackUpdate(w http.ResponseWriter, r *http.Request) if payload.Version != nil && *payload.Version != stack.Version { stack.Version = *payload.Version - stack.Status = map[portainer.EndpointID]portainer.EdgeStackStatus{} + stack.Status = map[database.EndpointID]portainer.EdgeStackStatus{} } err = handler.DataStore.EdgeStack().UpdateEdgeStack(stack.ID, stack) @@ -190,8 +191,8 @@ func (handler *Handler) edgeStackUpdate(w http.ResponseWriter, r *http.Request) return response.JSON(w, stack) } -func EndpointSet(endpointIDs []portainer.EndpointID) map[portainer.EndpointID]bool { - set := map[portainer.EndpointID]bool{} +func EndpointSet(endpointIDs []database.EndpointID) map[database.EndpointID]bool { + set := map[database.EndpointID]bool{} for _, endpointID := range endpointIDs { set[endpointID] = true diff --git a/api/http/handler/edgestacks/endpoints.go b/api/http/handler/edgestacks/endpoints.go index c202ff963..ea84ff9bf 100644 --- a/api/http/handler/edgestacks/endpoints.go +++ b/api/http/handler/edgestacks/endpoints.go @@ -2,21 +2,22 @@ package edgestacks import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/internal/endpointutils" ) -func hasKubeEndpoint(endpointService dataservices.EndpointService, endpointIDs []portainer.EndpointID) (bool, error) { +func hasKubeEndpoint(endpointService dataservices.EndpointService, endpointIDs []database.EndpointID) (bool, error) { return hasEndpointPredicate(endpointService, endpointIDs, endpointutils.IsKubernetesEndpoint) } -func hasDockerEndpoint(endpointService dataservices.EndpointService, endpointIDs []portainer.EndpointID) (bool, error) { +func hasDockerEndpoint(endpointService dataservices.EndpointService, endpointIDs []database.EndpointID) (bool, error) { return hasEndpointPredicate(endpointService, endpointIDs, endpointutils.IsDockerEndpoint) } -func hasEndpointPredicate(endpointService dataservices.EndpointService, endpointIDs []portainer.EndpointID, predicate func(*portainer.Endpoint) bool) (bool, error) { +func hasEndpointPredicate(endpointService dataservices.EndpointService, endpointIDs []database.EndpointID, predicate func(*portainer.Endpoint) bool) (bool, error) { for _, endpointID := range endpointIDs { endpoint, err := endpointService.Endpoint(endpointID) if err != nil { diff --git a/api/http/handler/edgestacks/endpoints_test.go b/api/http/handler/edgestacks/endpoints_test.go index 6cc94b17c..8c19a0a6d 100644 --- a/api/http/handler/edgestacks/endpoints_test.go +++ b/api/http/handler/edgestacks/endpoints_test.go @@ -1,6 +1,7 @@ package edgestacks import ( + "github.com/portainer/portainer/api/database" "testing" portainer "github.com/portainer/portainer/api" @@ -22,19 +23,19 @@ func Test_hasKubeEndpoint(t *testing.T) { datastore := testhelpers.NewDatastore(testhelpers.WithEndpoints(endpoints)) tests := []struct { - endpointIds []portainer.EndpointID + endpointIds []database.EndpointID expected bool }{ - {endpointIds: []portainer.EndpointID{1}, expected: false}, - {endpointIds: []portainer.EndpointID{2}, expected: false}, - {endpointIds: []portainer.EndpointID{3}, expected: false}, - {endpointIds: []portainer.EndpointID{4}, expected: false}, - {endpointIds: []portainer.EndpointID{5}, expected: true}, - {endpointIds: []portainer.EndpointID{6}, expected: true}, - {endpointIds: []portainer.EndpointID{7}, expected: true}, - {endpointIds: []portainer.EndpointID{7, 2}, expected: true}, - {endpointIds: []portainer.EndpointID{6, 4, 1}, expected: true}, - {endpointIds: []portainer.EndpointID{1, 2, 3}, expected: false}, + {endpointIds: []database.EndpointID{1}, expected: false}, + {endpointIds: []database.EndpointID{2}, expected: false}, + {endpointIds: []database.EndpointID{3}, expected: false}, + {endpointIds: []database.EndpointID{4}, expected: false}, + {endpointIds: []database.EndpointID{5}, expected: true}, + {endpointIds: []database.EndpointID{6}, expected: true}, + {endpointIds: []database.EndpointID{7}, expected: true}, + {endpointIds: []database.EndpointID{7, 2}, expected: true}, + {endpointIds: []database.EndpointID{6, 4, 1}, expected: true}, + {endpointIds: []database.EndpointID{1, 2, 3}, expected: false}, } for _, test := range tests { @@ -49,7 +50,7 @@ func Test_hasKubeEndpoint(t *testing.T) { func Test_hasKubeEndpoint_failWhenEndpointDontExist(t *testing.T) { datastore := testhelpers.NewDatastore(testhelpers.WithEndpoints([]portainer.Endpoint{})) - _, err := hasKubeEndpoint(datastore.Endpoint(), []portainer.EndpointID{1}) + _, err := hasKubeEndpoint(datastore.Endpoint(), []database.EndpointID{1}) assert.Error(t, err, "hasKubeEndpoint should fail") } @@ -67,19 +68,19 @@ func Test_hasDockerEndpoint(t *testing.T) { datastore := testhelpers.NewDatastore(testhelpers.WithEndpoints(endpoints)) tests := []struct { - endpointIds []portainer.EndpointID + endpointIds []database.EndpointID expected bool }{ - {endpointIds: []portainer.EndpointID{1}, expected: true}, - {endpointIds: []portainer.EndpointID{2}, expected: true}, - {endpointIds: []portainer.EndpointID{3}, expected: false}, - {endpointIds: []portainer.EndpointID{4}, expected: true}, - {endpointIds: []portainer.EndpointID{5}, expected: false}, - {endpointIds: []portainer.EndpointID{6}, expected: false}, - {endpointIds: []portainer.EndpointID{7}, expected: false}, - {endpointIds: []portainer.EndpointID{7, 2}, expected: true}, - {endpointIds: []portainer.EndpointID{6, 4, 1}, expected: true}, - {endpointIds: []portainer.EndpointID{1, 2, 3}, expected: true}, + {endpointIds: []database.EndpointID{1}, expected: true}, + {endpointIds: []database.EndpointID{2}, expected: true}, + {endpointIds: []database.EndpointID{3}, expected: false}, + {endpointIds: []database.EndpointID{4}, expected: true}, + {endpointIds: []database.EndpointID{5}, expected: false}, + {endpointIds: []database.EndpointID{6}, expected: false}, + {endpointIds: []database.EndpointID{7}, expected: false}, + {endpointIds: []database.EndpointID{7, 2}, expected: true}, + {endpointIds: []database.EndpointID{6, 4, 1}, expected: true}, + {endpointIds: []database.EndpointID{1, 2, 3}, expected: true}, } for _, test := range tests { @@ -94,6 +95,6 @@ func Test_hasDockerEndpoint(t *testing.T) { func Test_hasDockerEndpoint_failWhenEndpointDontExist(t *testing.T) { datastore := testhelpers.NewDatastore(testhelpers.WithEndpoints([]portainer.Endpoint{})) - _, err := hasDockerEndpoint(datastore.Endpoint(), []portainer.EndpointID{1}) + _, err := hasDockerEndpoint(datastore.Endpoint(), []database.EndpointID{1}) assert.Error(t, err, "hasDockerEndpoint should fail") } diff --git a/api/http/handler/edgestacks/handler.go b/api/http/handler/edgestacks/handler.go index e51c3dc94..47c9d0426 100644 --- a/api/http/handler/edgestacks/handler.go +++ b/api/http/handler/edgestacks/handler.go @@ -2,6 +2,7 @@ package edgestacks import ( "fmt" + "github.com/portainer/portainer/api/database" "net/http" "strconv" @@ -46,7 +47,7 @@ func NewHandler(bouncer *security.RequestBouncer) *Handler { return h } -func (handler *Handler) convertAndStoreKubeManifestIfNeeded(edgeStack *portainer.EdgeStack, relatedEndpointIds []portainer.EndpointID) error { +func (handler *Handler) convertAndStoreKubeManifestIfNeeded(edgeStack *portainer.EdgeStack, relatedEndpointIds []database.EndpointID) error { hasKubeEndpoint, err := hasKubeEndpoint(handler.DataStore.Endpoint(), relatedEndpointIds) if err != nil { return fmt.Errorf("unable to check if edge stack has kube environments: %w", err) diff --git a/api/http/handler/endpointedge/endpoint_edgejob_logs.go b/api/http/handler/endpointedge/endpoint_edgejob_logs.go index a1d8c1091..996fb90dd 100644 --- a/api/http/handler/endpointedge/endpoint_edgejob_logs.go +++ b/api/http/handler/endpointedge/endpoint_edgejob_logs.go @@ -1,6 +1,8 @@ package endpointedge import ( + "github.com/portainer/portainer/api/database" + "github.com/portainer/portainer/api/dataservices/edgejob" "net/http" "strconv" @@ -36,7 +38,7 @@ func (handler *Handler) endpointEdgeJobsLogs(w http.ResponseWriter, r *http.Requ return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { @@ -59,7 +61,7 @@ func (handler *Handler) endpointEdgeJobsLogs(w http.ResponseWriter, r *http.Requ return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(portainer.EdgeJobID(edgeJobID)) + edgeJob, err := handler.DataStore.EdgeJob().EdgeJob(edgejob.EdgeJobID(edgeJobID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an edge job with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpointedge/endpoint_edgestack_inspect.go b/api/http/handler/endpointedge/endpoint_edgestack_inspect.go index 14eff89ac..ca59c70ba 100644 --- a/api/http/handler/endpointedge/endpoint_edgestack_inspect.go +++ b/api/http/handler/endpointedge/endpoint_edgestack_inspect.go @@ -2,6 +2,7 @@ package endpointedge import ( "errors" + "github.com/portainer/portainer/api/database" "net/http" httperror "github.com/portainer/libhttp/error" @@ -34,7 +35,7 @@ func (handler *Handler) endpointEdgeStackInspect(w http.ResponseWriter, r *http. return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpointgroups/endpointgroup_create.go b/api/http/handler/endpointgroups/endpointgroup_create.go index dfa243263..f6917bd68 100644 --- a/api/http/handler/endpointgroups/endpointgroup_create.go +++ b/api/http/handler/endpointgroups/endpointgroup_create.go @@ -2,6 +2,7 @@ package endpointgroups import ( "errors" + "github.com/portainer/portainer/api/database" "net/http" "github.com/asaskevich/govalidator" @@ -17,7 +18,7 @@ type endpointGroupCreatePayload struct { // Environment(Endpoint) group description Description string `example:"description"` // List of environment(endpoint) identifiers that will be part of this group - AssociatedEndpoints []portainer.EndpointID `example:"1,3"` + AssociatedEndpoints []database.EndpointID `example:"1,3"` // List of tag identifiers to which this environment(endpoint) group is associated TagIDs []portainer.TagID `example:"1,2"` } diff --git a/api/http/handler/endpointgroups/endpointgroup_endpoint_add.go b/api/http/handler/endpointgroups/endpointgroup_endpoint_add.go index 49ee2607e..061698524 100644 --- a/api/http/handler/endpointgroups/endpointgroup_endpoint_add.go +++ b/api/http/handler/endpointgroups/endpointgroup_endpoint_add.go @@ -1,6 +1,7 @@ package endpointgroups import ( + "github.com/portainer/portainer/api/database" "net/http" httperror "github.com/portainer/libhttp/error" @@ -41,7 +42,7 @@ func (handler *Handler) endpointGroupAddEndpoint(w http.ResponseWriter, r *http. return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an environment group with the specified identifier inside the database", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpointgroups/endpointgroup_endpoint_delete.go b/api/http/handler/endpointgroups/endpointgroup_endpoint_delete.go index df3de59b2..9b804e328 100644 --- a/api/http/handler/endpointgroups/endpointgroup_endpoint_delete.go +++ b/api/http/handler/endpointgroups/endpointgroup_endpoint_delete.go @@ -1,6 +1,7 @@ package endpointgroups import ( + "github.com/portainer/portainer/api/database" "net/http" httperror "github.com/portainer/libhttp/error" @@ -40,7 +41,7 @@ func (handler *Handler) endpointGroupDeleteEndpoint(w http.ResponseWriter, r *ht return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an environment group with the specified identifier inside the database", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpointproxy/proxy_azure.go b/api/http/handler/endpointproxy/proxy_azure.go index 3211a1b58..24c741444 100644 --- a/api/http/handler/endpointproxy/proxy_azure.go +++ b/api/http/handler/endpointproxy/proxy_azure.go @@ -1,12 +1,11 @@ package endpointproxy import ( + "github.com/portainer/portainer/api/database" "strconv" httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" - portainer "github.com/portainer/portainer/api" - "net/http" ) @@ -16,7 +15,7 @@ func (handler *Handler) proxyRequestsToAzureAPI(w http.ResponseWriter, r *http.R return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpointproxy/proxy_docker.go b/api/http/handler/endpointproxy/proxy_docker.go index 96a539ddb..fddbe3e70 100644 --- a/api/http/handler/endpointproxy/proxy_docker.go +++ b/api/http/handler/endpointproxy/proxy_docker.go @@ -5,6 +5,7 @@ import ( httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" "strconv" "strings" @@ -17,7 +18,7 @@ func (handler *Handler) proxyRequestsToDockerAPI(w http.ResponseWriter, r *http. return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpointproxy/proxy_kubernetes.go b/api/http/handler/endpointproxy/proxy_kubernetes.go index b3508b624..d7c2d389a 100644 --- a/api/http/handler/endpointproxy/proxy_kubernetes.go +++ b/api/http/handler/endpointproxy/proxy_kubernetes.go @@ -6,6 +6,7 @@ import ( httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" "strings" "net/http" @@ -17,7 +18,7 @@ func (handler *Handler) proxyRequestsToKubernetesAPI(w http.ResponseWriter, r *h return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpoints/endpoint_association_delete.go b/api/http/handler/endpoints/endpoint_association_delete.go index e8385b278..1013022d1 100644 --- a/api/http/handler/endpoints/endpoint_association_delete.go +++ b/api/http/handler/endpoints/endpoint_association_delete.go @@ -4,6 +4,7 @@ import ( "encoding/base64" "errors" "fmt" + "github.com/portainer/portainer/api/database" "net/http" "regexp" "strings" @@ -34,7 +35,7 @@ func (handler *Handler) endpointAssociationDelete(w http.ResponseWriter, r *http return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { @@ -54,7 +55,7 @@ func (handler *Handler) endpointAssociationDelete(w http.ResponseWriter, r *http return &httperror.HandlerError{http.StatusInternalServerError, "Invalid EdgeKey", err} } - err = handler.DataStore.Endpoint().UpdateEndpoint(portainer.EndpointID(endpointID), endpoint) + err = handler.DataStore.Endpoint().UpdateEndpoint(database.EndpointID(endpointID), endpoint) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Failed persisting environment in database", err} } diff --git a/api/http/handler/endpoints/endpoint_delete.go b/api/http/handler/endpoints/endpoint_delete.go index da87d9d3c..60bd7f4b2 100644 --- a/api/http/handler/endpoints/endpoint_delete.go +++ b/api/http/handler/endpoints/endpoint_delete.go @@ -1,13 +1,13 @@ package endpoints import ( + "github.com/portainer/portainer/api/database" "net/http" "strconv" httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" - portainer "github.com/portainer/portainer/api" ) // @id EndpointDelete @@ -29,7 +29,7 @@ func (handler *Handler) endpointDelete(w http.ResponseWriter, r *http.Request) * return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { @@ -44,7 +44,7 @@ func (handler *Handler) endpointDelete(w http.ResponseWriter, r *http.Request) * } } - err = handler.DataStore.Endpoint().DeleteEndpoint(portainer.EndpointID(endpointID)) + err = handler.DataStore.Endpoint().DeleteEndpoint(database.EndpointID(endpointID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove environment from the database", err} } @@ -122,7 +122,7 @@ func (handler *Handler) endpointDelete(w http.ResponseWriter, r *http.Request) * return response.Empty(w) } -func findEndpointIndex(tags []portainer.EndpointID, searchEndpointID portainer.EndpointID) int { +func findEndpointIndex(tags []database.EndpointID, searchEndpointID database.EndpointID) int { for idx, tagID := range tags { if searchEndpointID == tagID { return idx @@ -131,7 +131,7 @@ func findEndpointIndex(tags []portainer.EndpointID, searchEndpointID portainer.E return -1 } -func removeElement(arr []portainer.EndpointID, index int) []portainer.EndpointID { +func removeElement(arr []database.EndpointID, index int) []database.EndpointID { if index < 0 { return arr } diff --git a/api/http/handler/endpoints/endpoint_dockerhub_status.go b/api/http/handler/endpoints/endpoint_dockerhub_status.go index 479d0df6e..d22ae7f6e 100644 --- a/api/http/handler/endpoints/endpoint_dockerhub_status.go +++ b/api/http/handler/endpoints/endpoint_dockerhub_status.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/portainer/portainer/api/database" "net/http" "strconv" "strings" @@ -45,7 +46,7 @@ func (handler *Handler) endpointDockerhubStatus(w http.ResponseWriter, r *http.R return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpoints/endpoint_inspect.go b/api/http/handler/endpoints/endpoint_inspect.go index 55e3ed588..b32fd3d48 100644 --- a/api/http/handler/endpoints/endpoint_inspect.go +++ b/api/http/handler/endpoints/endpoint_inspect.go @@ -1,12 +1,12 @@ package endpoints import ( + "github.com/portainer/portainer/api/database" "net/http" httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" - portainer "github.com/portainer/portainer/api" ) // @id EndpointInspect @@ -29,7 +29,7 @@ func (handler *Handler) endpointInspect(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpoints/endpoint_list.go b/api/http/handler/endpoints/endpoint_list.go index 00107278c..0a04baa88 100644 --- a/api/http/handler/endpoints/endpoint_list.go +++ b/api/http/handler/endpoints/endpoint_list.go @@ -1,6 +1,7 @@ package endpoints import ( + "github.com/portainer/portainer/api/database" "net/http" "strconv" "strings" @@ -56,7 +57,7 @@ func (handler *Handler) endpointList(w http.ResponseWriter, r *http.Request) *ht tagsPartialMatch, _ := request.RetrieveBooleanQueryParameter(r, "tagsPartialMatch", true) - var endpointIDs []portainer.EndpointID + var endpointIDs []database.EndpointID request.RetrieveJSONQueryParameter(r, "endpointIds", &endpointIDs, true) endpointGroups, err := handler.DataStore.EndpointGroup().EndpointGroups() @@ -321,10 +322,10 @@ func endpointFullMatchTags(endpoint portainer.Endpoint, endpointGroup portainer. return len(missingTags) == 0 } -func filteredEndpointsByIds(endpoints []portainer.Endpoint, ids []portainer.EndpointID) []portainer.Endpoint { +func filteredEndpointsByIds(endpoints []portainer.Endpoint, ids []database.EndpointID) []portainer.Endpoint { filteredEndpoints := make([]portainer.Endpoint, 0) - idsSet := make(map[portainer.EndpointID]bool) + idsSet := make(map[database.EndpointID]bool) for _, id := range ids { idsSet[id] = true } diff --git a/api/http/handler/endpoints/endpoint_registries_inspect.go b/api/http/handler/endpoints/endpoint_registries_inspect.go index 50a2a1641..5fc4b0e4e 100644 --- a/api/http/handler/endpoints/endpoint_registries_inspect.go +++ b/api/http/handler/endpoints/endpoint_registries_inspect.go @@ -1,6 +1,7 @@ package endpoints import ( + "github.com/portainer/portainer/api/database" "net/http" httperror "github.com/portainer/libhttp/error" @@ -54,7 +55,7 @@ func (handler *Handler) endpointRegistryInspect(w http.ResponseWriter, r *http.R return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to retrieve user from the database", Err: err} } - if !security.AuthorizedRegistryAccess(registry, user, securityContext.UserMemberships, portainer.EndpointID(endpointID)) { + if !security.AuthorizedRegistryAccess(registry, user, securityContext.UserMemberships, database.EndpointID(endpointID)) { return &httperror.HandlerError{StatusCode: http.StatusForbidden, Message: "Access denied to resource", Err: httperrors.ErrResourceAccessDenied} } diff --git a/api/http/handler/endpoints/endpoint_registries_list.go b/api/http/handler/endpoints/endpoint_registries_list.go index 049e4aa94..4dc68e45e 100644 --- a/api/http/handler/endpoints/endpoint_registries_list.go +++ b/api/http/handler/endpoints/endpoint_registries_list.go @@ -1,6 +1,7 @@ package endpoints import ( + "github.com/portainer/portainer/api/database" "net/http" "github.com/pkg/errors" @@ -41,7 +42,7 @@ func (handler *Handler) endpointRegistriesList(w http.ResponseWriter, r *http.Re return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid environment identifier route variable", Err: err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { @@ -114,7 +115,7 @@ func (handler *Handler) isNamespaceAuthorized(endpoint *portainer.Endpoint, name return security.AuthorizedAccess(userId, memberships, namespacePolicy.UserAccessPolicies, namespacePolicy.TeamAccessPolicies), nil } -func filterRegistriesByNamespace(registries []portainer.Registry, endpointId portainer.EndpointID, namespace string) []portainer.Registry { +func filterRegistriesByNamespace(registries []portainer.Registry, endpointId database.EndpointID, namespace string) []portainer.Registry { if namespace == "" { return registries } diff --git a/api/http/handler/endpoints/endpoint_registry_access.go b/api/http/handler/endpoints/endpoint_registry_access.go index 05af612ef..9004066b5 100644 --- a/api/http/handler/endpoints/endpoint_registry_access.go +++ b/api/http/handler/endpoints/endpoint_registry_access.go @@ -1,6 +1,7 @@ package endpoints import ( + "github.com/portainer/portainer/api/database" "net/http" httperror "github.com/portainer/libhttp/error" @@ -48,7 +49,7 @@ func (handler *Handler) endpointRegistryAccess(w http.ResponseWriter, r *http.Re return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid registry identifier route variable", Err: err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{StatusCode: http.StatusNotFound, Message: "Unable to find an environment with the specified identifier inside the database", Err: err} } else if err != nil { @@ -104,7 +105,7 @@ func (handler *Handler) endpointRegistryAccess(w http.ResponseWriter, r *http.Re registryAccess.TeamAccessPolicies = payload.TeamAccessPolicies } - registry.RegistryAccesses[portainer.EndpointID(endpointID)] = registryAccess + registry.RegistryAccesses[database.EndpointID(endpointID)] = registryAccess handler.DataStore.Registry().UpdateRegistry(registry.ID, registry) diff --git a/api/http/handler/endpoints/endpoint_settings_update.go b/api/http/handler/endpoints/endpoint_settings_update.go index 441a66eb4..a867f78f8 100644 --- a/api/http/handler/endpoints/endpoint_settings_update.go +++ b/api/http/handler/endpoints/endpoint_settings_update.go @@ -1,12 +1,12 @@ package endpoints import ( + "github.com/portainer/portainer/api/database" "net/http" httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" - portainer "github.com/portainer/portainer/api" ) type endpointSettingsUpdatePayload struct { @@ -62,7 +62,7 @@ func (handler *Handler) endpointSettingsUpdate(w http.ResponseWriter, r *http.Re return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { @@ -109,7 +109,7 @@ func (handler *Handler) endpointSettingsUpdate(w http.ResponseWriter, r *http.Re endpoint.SecuritySettings = securitySettings - err = handler.DataStore.Endpoint().UpdateEndpoint(portainer.EndpointID(endpointID), endpoint) + err = handler.DataStore.Endpoint().UpdateEndpoint(database.EndpointID(endpointID), endpoint) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Failed persisting environment in database", err} } diff --git a/api/http/handler/endpoints/endpoint_snapshot.go b/api/http/handler/endpoints/endpoint_snapshot.go index e4db58616..34dc31f52 100644 --- a/api/http/handler/endpoints/endpoint_snapshot.go +++ b/api/http/handler/endpoints/endpoint_snapshot.go @@ -2,6 +2,7 @@ package endpoints import ( "errors" + "github.com/portainer/portainer/api/database" "net/http" httperror "github.com/portainer/libhttp/error" @@ -30,7 +31,7 @@ func (handler *Handler) endpointSnapshot(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpoints/endpoint_status_inspect.go b/api/http/handler/endpoints/endpoint_status_inspect.go index afefd9f25..47160ec6e 100644 --- a/api/http/handler/endpoints/endpoint_status_inspect.go +++ b/api/http/handler/endpoints/endpoint_status_inspect.go @@ -3,6 +3,8 @@ package endpoints import ( "encoding/base64" "errors" + "github.com/portainer/portainer/api/database" + "github.com/portainer/portainer/api/dataservices/edgejob" "net/http" "strconv" "time" @@ -22,7 +24,7 @@ type stackStatusResponse struct { type edgeJobResponse struct { // EdgeJob Identifier - ID portainer.EdgeJobID `json:"Id" example:"2"` + ID edgejob.EdgeJobID `json:"Id" example:"2"` // Whether to collect logs CollectLogs bool `json:"CollectLogs" example:"true"` // A cron expression to schedule this job @@ -68,7 +70,7 @@ func (handler *Handler) endpointStatusInspect(w http.ResponseWriter, r *http.Req return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpoints/endpoint_update.go b/api/http/handler/endpoints/endpoint_update.go index b8364b4a2..044b348fc 100644 --- a/api/http/handler/endpoints/endpoint_update.go +++ b/api/http/handler/endpoints/endpoint_update.go @@ -1,6 +1,7 @@ package endpoints import ( + "github.com/portainer/portainer/api/database" "net/http" "reflect" "strconv" @@ -82,7 +83,7 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) * return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/hostmanagement/openamt/amtactivation.go b/api/http/handler/hostmanagement/openamt/amtactivation.go index c4d86f779..6c8b59224 100644 --- a/api/http/handler/hostmanagement/openamt/amtactivation.go +++ b/api/http/handler/hostmanagement/openamt/amtactivation.go @@ -3,12 +3,12 @@ package openamt import ( "errors" "fmt" + "github.com/portainer/portainer/api/database" "net/http" httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" - portainer "github.com/portainer/portainer/api" bolterrors "github.com/portainer/portainer/api/dataservices/errors" "github.com/portainer/portainer/api/internal/endpointutils" ) @@ -32,7 +32,7 @@ func (handler *Handler) openAMTActivate(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if err == bolterrors.ErrObjectNotFound { return &httperror.HandlerError{StatusCode: http.StatusNotFound, Message: "Unable to find an endpoint with the specified identifier inside the database", Err: err} } else if err != nil { diff --git a/api/http/handler/hostmanagement/openamt/amtdevices.go b/api/http/handler/hostmanagement/openamt/amtdevices.go index 5459e2ac1..28cbcf834 100644 --- a/api/http/handler/hostmanagement/openamt/amtdevices.go +++ b/api/http/handler/hostmanagement/openamt/amtdevices.go @@ -2,6 +2,7 @@ package openamt import ( "errors" + "github.com/portainer/portainer/api/database" "net/http" httperror "github.com/portainer/libhttp/error" @@ -31,7 +32,7 @@ func (handler *Handler) openAMTDevices(w http.ResponseWriter, r *http.Request) * return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if err == bolterrors.ErrObjectNotFound { return &httperror.HandlerError{StatusCode: http.StatusNotFound, Message: "Unable to find an endpoint with the specified identifier inside the database", Err: err} } else if err != nil { diff --git a/api/http/handler/hostmanagement/openamt/amtrpc.go b/api/http/handler/hostmanagement/openamt/amtrpc.go index 8f9595f5f..ffe063147 100644 --- a/api/http/handler/hostmanagement/openamt/amtrpc.go +++ b/api/http/handler/hostmanagement/openamt/amtrpc.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/portainer/portainer/api/database" "github.com/portainer/portainer/api/hostmanagement/openamt" "io/ioutil" "log" @@ -25,14 +26,14 @@ import ( ) type HostInfo struct { - EndpointID portainer.EndpointID `json:"EndpointID"` - RawOutput string `json:"RawOutput"` - AMT string `json:"AMT"` - UUID string `json:"UUID"` - DNSSuffix string `json:"DNS Suffix"` - BuildNumber string `json:"Build Number"` - ControlMode string `json:"Control Mode"` - ControlModeRaw int `json:"Control Mode (Raw)"` + EndpointID database.EndpointID `json:"EndpointID"` + RawOutput string `json:"RawOutput"` + AMT string `json:"AMT"` + UUID string `json:"UUID"` + DNSSuffix string `json:"DNS Suffix"` + BuildNumber string `json:"Build Number"` + ControlMode string `json:"Control Mode"` + ControlModeRaw int `json:"Control Mode (Raw)"` } const ( @@ -62,7 +63,7 @@ func (handler *Handler) openAMTHostInfo(w http.ResponseWriter, r *http.Request) logrus.WithField("endpointID", endpointID).Info("OpenAMTHostInfo") - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if err == bolterrors.ErrObjectNotFound { return &httperror.HandlerError{StatusCode: http.StatusNotFound, Message: "Unable to find an endpoint with the specified identifier inside the database", Err: err} } else if err != nil { diff --git a/api/http/handler/kubernetes/kubernetes_config.go b/api/http/handler/kubernetes/kubernetes_config.go index e9211cc63..cd2f4f669 100644 --- a/api/http/handler/kubernetes/kubernetes_config.go +++ b/api/http/handler/kubernetes/kubernetes_config.go @@ -3,6 +3,7 @@ package kubernetes import ( "errors" "fmt" + "github.com/portainer/portainer/api/database" "net/http" "strings" @@ -61,10 +62,10 @@ func (handler *Handler) getKubernetesConfig(w http.ResponseWriter, r *http.Reque } func (handler *Handler) filterUserKubeEndpoints(r *http.Request) ([]portainer.Endpoint, *httperror.HandlerError) { - var endpointIDs []portainer.EndpointID + var endpointIDs []database.EndpointID _ = request.RetrieveJSONQueryParameter(r, "ids", &endpointIDs, true) - var excludeEndpointIDs []portainer.EndpointID + var excludeEndpointIDs []database.EndpointID _ = request.RetrieveJSONQueryParameter(r, "excludeIds", &excludeEndpointIDs, true) if len(endpointIDs) > 0 && len(excludeEndpointIDs) > 0 { diff --git a/api/http/handler/kubernetes/kubernetes_nodes_limits.go b/api/http/handler/kubernetes/kubernetes_nodes_limits.go index 796c642b5..7b55b4dc7 100644 --- a/api/http/handler/kubernetes/kubernetes_nodes_limits.go +++ b/api/http/handler/kubernetes/kubernetes_nodes_limits.go @@ -1,12 +1,12 @@ package kubernetes import ( + "github.com/portainer/portainer/api/database" "net/http" httperror "github.com/portainer/libhttp/error" "github.com/portainer/libhttp/request" "github.com/portainer/libhttp/response" - portainer "github.com/portainer/portainer/api" ) // @id getKubernetesNodesLimits @@ -32,7 +32,7 @@ func (handler *Handler) getKubernetesNodesLimits(w http.ResponseWriter, r *http. return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err} } - endpoint, err := handler.dataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.dataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.dataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/stacks/handler.go b/api/http/handler/stacks/handler.go index d0043d49b..10d720fb6 100644 --- a/api/http/handler/stacks/handler.go +++ b/api/http/handler/stacks/handler.go @@ -3,6 +3,7 @@ package stacks import ( "context" "fmt" + "github.com/portainer/portainer/api/database" "net/http" "strings" "sync" @@ -89,7 +90,7 @@ func NewHandler(bouncer *security.RequestBouncer) *Handler { return h } -func (handler *Handler) userCanAccessStack(securityContext *security.RestrictedRequestContext, endpointID portainer.EndpointID, resourceControl *portainer.ResourceControl) (bool, error) { +func (handler *Handler) userCanAccessStack(securityContext *security.RestrictedRequestContext, endpointID database.EndpointID, resourceControl *portainer.ResourceControl) (bool, error) { user, err := handler.DataStore.User().User(securityContext.UserID) if err != nil { return false, err @@ -118,13 +119,13 @@ func (handler *Handler) userIsAdmin(userID portainer.UserID) (bool, error) { return isAdmin, nil } -func (handler *Handler) userIsAdminOrEndpointAdmin(user *portainer.User, endpointID portainer.EndpointID) (bool, error) { +func (handler *Handler) userIsAdminOrEndpointAdmin(user *portainer.User, endpointID database.EndpointID) (bool, error) { isAdmin := user.Role == portainer.AdministratorRole return isAdmin, nil } -func (handler *Handler) userCanCreateStack(securityContext *security.RestrictedRequestContext, endpointID portainer.EndpointID) (bool, error) { +func (handler *Handler) userCanCreateStack(securityContext *security.RestrictedRequestContext, endpointID database.EndpointID) (bool, error) { user, err := handler.DataStore.User().User(securityContext.UserID) if err != nil { return false, err diff --git a/api/http/handler/stacks/stack_associate.go b/api/http/handler/stacks/stack_associate.go index 954984e2b..378799949 100644 --- a/api/http/handler/stacks/stack_associate.go +++ b/api/http/handler/stacks/stack_associate.go @@ -2,6 +2,7 @@ package stacks import ( "fmt" + "github.com/portainer/portainer/api/database" "net/http" "time" @@ -82,7 +83,7 @@ func (handler *Handler) stackAssociate(w http.ResponseWriter, r *http.Request) * } } - stack.EndpointID = portainer.EndpointID(endpointID) + stack.EndpointID = database.EndpointID(endpointID) stack.SwarmID = swarmId if orphanedRunning { diff --git a/api/http/handler/stacks/stack_create.go b/api/http/handler/stacks/stack_create.go index 2fb77acff..10c718280 100644 --- a/api/http/handler/stacks/stack_create.go +++ b/api/http/handler/stacks/stack_create.go @@ -1,6 +1,7 @@ package stacks import ( + "github.com/portainer/portainer/api/database" "log" "net/http" @@ -69,7 +70,7 @@ func (handler *Handler) stackCreate(w http.ResponseWriter, r *http.Request) *htt return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: endpointId", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { @@ -82,7 +83,7 @@ func (handler *Handler) stackCreate(w http.ResponseWriter, r *http.Request) *htt return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve user info from request context", err} } - canCreate, err := handler.userCanCreateStack(securityContext, portainer.EndpointID(endpointID)) + canCreate, err := handler.userCanCreateStack(securityContext, database.EndpointID(endpointID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to verify user authorizations to validate stack creation", err} diff --git a/api/http/handler/stacks/stack_delete.go b/api/http/handler/stacks/stack_delete.go index b98fa4cab..f1ddb3bb9 100644 --- a/api/http/handler/stacks/stack_delete.go +++ b/api/http/handler/stacks/stack_delete.go @@ -3,6 +3,7 @@ package stacks import ( "context" "fmt" + "github.com/portainer/portainer/api/database" "io/ioutil" "net/http" "os" @@ -68,13 +69,13 @@ func (handler *Handler) stackDelete(w http.ResponseWriter, r *http.Request) *htt return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid query parameter: endpointId", Err: err} } - isOrphaned := portainer.EndpointID(endpointID) != stack.EndpointID + isOrphaned := database.EndpointID(endpointID) != stack.EndpointID if isOrphaned && !securityContext.IsAdmin { return &httperror.HandlerError{StatusCode: http.StatusForbidden, Message: "Permission denied to remove orphaned stack", Err: errors.New("Permission denied to remove orphaned stack")} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{StatusCode: http.StatusNotFound, Message: "Unable to find the endpoint associated to the stack inside the database", Err: err} } else if err != nil { @@ -151,7 +152,7 @@ func (handler *Handler) deleteExternalStack(r *http.Request, w http.ResponseWrit return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "A stack with this name exists inside the database. Cannot use external delete method", Err: errors.New("A tag already exists with this name")} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{StatusCode: http.StatusNotFound, Message: "Unable to find the endpoint associated to the stack inside the database", Err: err} } else if err != nil { diff --git a/api/http/handler/stacks/stack_list.go b/api/http/handler/stacks/stack_list.go index 9b03776f9..c83b408a2 100644 --- a/api/http/handler/stacks/stack_list.go +++ b/api/http/handler/stacks/stack_list.go @@ -1,6 +1,7 @@ package stacks import ( + "github.com/portainer/portainer/api/database" "net/http" httperrors "github.com/portainer/portainer/api/http/errors" @@ -106,7 +107,7 @@ func filterStacks(stacks []portainer.Stack, filters *stackListOperationFilters, continue } - if stack.Type == portainer.DockerComposeStack && stack.EndpointID == portainer.EndpointID(filters.EndpointID) { + if stack.Type == portainer.DockerComposeStack && stack.EndpointID == database.EndpointID(filters.EndpointID) { filteredStacks = append(filteredStacks, stack) } if stack.Type == portainer.DockerSwarmStack && stack.SwarmID == filters.SwarmID { diff --git a/api/http/handler/stacks/stack_migrate.go b/api/http/handler/stacks/stack_migrate.go index 702cda341..4724b0313 100644 --- a/api/http/handler/stacks/stack_migrate.go +++ b/api/http/handler/stacks/stack_migrate.go @@ -3,6 +3,7 @@ package stacks import ( "errors" "fmt" + "github.com/portainer/portainer/api/database" "net/http" httperror "github.com/portainer/libhttp/error" @@ -108,17 +109,17 @@ func (handler *Handler) stackMigrate(w http.ResponseWriter, r *http.Request) *ht return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid query parameter: endpointId", Err: err} } if endpointID != int(stack.EndpointID) { - stack.EndpointID = portainer.EndpointID(endpointID) + stack.EndpointID = database.EndpointID(endpointID) } - targetEndpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(payload.EndpointID)) + targetEndpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(payload.EndpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{StatusCode: http.StatusNotFound, Message: "Unable to find an endpoint with the specified identifier inside the database", Err: err} } else if err != nil { return &httperror.HandlerError{StatusCode: http.StatusInternalServerError, Message: "Unable to find an endpoint with the specified identifier inside the database", Err: err} } - stack.EndpointID = portainer.EndpointID(payload.EndpointID) + stack.EndpointID = database.EndpointID(payload.EndpointID) if payload.SwarmID != "" { stack.SwarmID = payload.SwarmID } diff --git a/api/http/handler/stacks/stack_update.go b/api/http/handler/stacks/stack_update.go index ac60dbf57..7a4091f1d 100644 --- a/api/http/handler/stacks/stack_update.go +++ b/api/http/handler/stacks/stack_update.go @@ -1,6 +1,7 @@ package stacks import ( + "github.com/portainer/portainer/api/database" "net/http" "strconv" "time" @@ -86,7 +87,7 @@ func (handler *Handler) stackUpdate(w http.ResponseWriter, r *http.Request) *htt return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid query parameter: endpointId", Err: err} } if endpointID != int(stack.EndpointID) { - stack.EndpointID = portainer.EndpointID(endpointID) + stack.EndpointID = database.EndpointID(endpointID) } endpoint, err := handler.DataStore.Endpoint().Endpoint(stack.EndpointID) diff --git a/api/http/handler/stacks/stack_update_git.go b/api/http/handler/stacks/stack_update_git.go index 42e829cdd..5431815f6 100644 --- a/api/http/handler/stacks/stack_update_git.go +++ b/api/http/handler/stacks/stack_update_git.go @@ -1,6 +1,7 @@ package stacks import ( + "github.com/portainer/portainer/api/database" "net/http" "time" @@ -84,7 +85,7 @@ func (handler *Handler) stackUpdateGit(w http.ResponseWriter, r *http.Request) * return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid query parameter: endpointId", Err: err} } if endpointID != int(stack.EndpointID) { - stack.EndpointID = portainer.EndpointID(endpointID) + stack.EndpointID = database.EndpointID(endpointID) } endpoint, err := handler.DataStore.Endpoint().Endpoint(stack.EndpointID) diff --git a/api/http/handler/stacks/stack_update_git_redeploy.go b/api/http/handler/stacks/stack_update_git_redeploy.go index 984855a1d..52f32afce 100644 --- a/api/http/handler/stacks/stack_update_git_redeploy.go +++ b/api/http/handler/stacks/stack_update_git_redeploy.go @@ -2,6 +2,7 @@ package stacks import ( "fmt" + "github.com/portainer/portainer/api/database" "log" "net/http" "time" @@ -77,7 +78,7 @@ func (handler *Handler) stackGitRedeploy(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{StatusCode: http.StatusBadRequest, Message: "Invalid query parameter: endpointId", Err: err} } if endpointID != int(stack.EndpointID) { - stack.EndpointID = portainer.EndpointID(endpointID) + stack.EndpointID = database.EndpointID(endpointID) } endpoint, err := handler.DataStore.Endpoint().Endpoint(stack.EndpointID) diff --git a/api/http/handler/tags/tag_create.go b/api/http/handler/tags/tag_create.go index 12e0950ad..d4f83e17e 100644 --- a/api/http/handler/tags/tag_create.go +++ b/api/http/handler/tags/tag_create.go @@ -2,6 +2,7 @@ package tags import ( "errors" + "github.com/portainer/portainer/api/database" "net/http" "github.com/asaskevich/govalidator" @@ -58,7 +59,7 @@ func (handler *Handler) tagCreate(w http.ResponseWriter, r *http.Request) *httpe tag := &portainer.Tag{ Name: payload.Name, EndpointGroups: map[portainer.EndpointGroupID]bool{}, - Endpoints: map[portainer.EndpointID]bool{}, + Endpoints: map[database.EndpointID]bool{}, } err = handler.DataStore.Tag().Create(tag) diff --git a/api/http/handler/webhooks/webhook_create.go b/api/http/handler/webhooks/webhook_create.go index b0d7b00dc..3572c1c85 100644 --- a/api/http/handler/webhooks/webhook_create.go +++ b/api/http/handler/webhooks/webhook_create.go @@ -2,6 +2,7 @@ package webhooks import ( "errors" + "github.com/portainer/portainer/api/database" "github.com/portainer/portainer/api/http/security" "github.com/portainer/portainer/api/internal/registryutils/access" "net/http" @@ -62,7 +63,7 @@ func (handler *Handler) webhookCreate(w http.ResponseWriter, r *http.Request) *h return &httperror.HandlerError{http.StatusConflict, "A webhook for this resource already exists", errors.New("A webhook for this resource already exists")} } - endpointID := portainer.EndpointID(payload.EndpointID) + endpointID := database.EndpointID(payload.EndpointID) securityContext, err := security.RetrieveRestrictedRequestContext(r) if err != nil { diff --git a/api/http/handler/webhooks/webhook_execute.go b/api/http/handler/webhooks/webhook_execute.go index 9b0d581d7..c578e9af2 100644 --- a/api/http/handler/webhooks/webhook_execute.go +++ b/api/http/handler/webhooks/webhook_execute.go @@ -3,6 +3,7 @@ package webhooks import ( "context" "errors" + "github.com/portainer/portainer/api/database" "github.com/portainer/portainer/api/internal/registryutils" "io" "net/http" @@ -45,7 +46,7 @@ func (handler *Handler) webhookExecute(w http.ResponseWriter, r *http.Request) * registryID := webhook.RegistryID webhookType := webhook.WebhookType - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/webhooks/webhook_list.go b/api/http/handler/webhooks/webhook_list.go index 46ef0a18d..5580a2291 100644 --- a/api/http/handler/webhooks/webhook_list.go +++ b/api/http/handler/webhooks/webhook_list.go @@ -1,6 +1,7 @@ package webhooks import ( + "github.com/portainer/portainer/api/database" "github.com/portainer/portainer/api/http/security" "net/http" @@ -58,7 +59,7 @@ func filterWebhooks(webhooks []portainer.Webhook, filters *webhookListOperationF filteredWebhooks := make([]portainer.Webhook, 0, len(webhooks)) for _, webhook := range webhooks { - if webhook.EndpointID == portainer.EndpointID(filters.EndpointID) && webhook.ResourceID == string(filters.ResourceID) { + if webhook.EndpointID == database.EndpointID(filters.EndpointID) && webhook.ResourceID == string(filters.ResourceID) { filteredWebhooks = append(filteredWebhooks, webhook) } } diff --git a/api/http/handler/websocket/attach.go b/api/http/handler/websocket/attach.go index f7a95e9ef..878b079cc 100644 --- a/api/http/handler/websocket/attach.go +++ b/api/http/handler/websocket/attach.go @@ -1,6 +1,7 @@ package websocket import ( + "github.com/portainer/portainer/api/database" "net" "net/http" "net/http/httputil" @@ -46,7 +47,7 @@ func (handler *Handler) websocketAttach(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: endpointId", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find the environment associated to the stack inside the database", err} } else if err != nil { diff --git a/api/http/handler/websocket/exec.go b/api/http/handler/websocket/exec.go index b089d92ed..4f7f45224 100644 --- a/api/http/handler/websocket/exec.go +++ b/api/http/handler/websocket/exec.go @@ -3,6 +3,7 @@ package websocket import ( "bytes" "encoding/json" + "github.com/portainer/portainer/api/database" "net" "net/http" "net/http/httputil" @@ -52,7 +53,7 @@ func (handler *Handler) websocketExec(w http.ResponseWriter, r *http.Request) *h return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: endpointId", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find the environment associated to the stack inside the database", err} } else if err != nil { diff --git a/api/http/handler/websocket/pod.go b/api/http/handler/websocket/pod.go index 07ff1a8f2..55b278f47 100644 --- a/api/http/handler/websocket/pod.go +++ b/api/http/handler/websocket/pod.go @@ -2,6 +2,7 @@ package websocket import ( "fmt" + "github.com/portainer/portainer/api/database" "io" "log" "net/http" @@ -62,7 +63,7 @@ func (handler *Handler) websocketPodExec(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: command", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find the environment associated to the stack inside the database", err} } else if err != nil { diff --git a/api/http/handler/websocket/shell_pod.go b/api/http/handler/websocket/shell_pod.go index eb6d1a08d..a0a1008d8 100644 --- a/api/http/handler/websocket/shell_pod.go +++ b/api/http/handler/websocket/shell_pod.go @@ -1,6 +1,7 @@ package websocket import ( + "github.com/portainer/portainer/api/database" "net/http" httperror "github.com/portainer/libhttp/error" @@ -30,7 +31,7 @@ func (handler *Handler) websocketShellPodExec(w http.ResponseWriter, r *http.Req return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: endpointId", err} } - endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(database.EndpointID(endpointID)) if handler.DataStore.IsErrObjectNotFound(err) { return &httperror.HandlerError{http.StatusNotFound, "Unable to find the environment associated to the stack inside the database", err} } else if err != nil { diff --git a/api/http/middlewares/endpoint.go b/api/http/middlewares/endpoint.go index 2a4593fa2..0bc5baba8 100644 --- a/api/http/middlewares/endpoint.go +++ b/api/http/middlewares/endpoint.go @@ -3,6 +3,7 @@ package middlewares import ( "context" "errors" + "github.com/portainer/portainer/api/database" "net/http" "github.com/gorilla/mux" @@ -30,7 +31,7 @@ func WithEndpoint(endpointService dataservices.EndpointService, endpointIDParam return } - endpoint, err := endpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := endpointService.Endpoint(database.EndpointID(endpointID)) if err != nil { statusCode := http.StatusInternalServerError diff --git a/api/http/proxy/factory/agent/transport.go b/api/http/proxy/factory/agent/transport.go index 4250f861d..961800f26 100644 --- a/api/http/proxy/factory/agent/transport.go +++ b/api/http/proxy/factory/agent/transport.go @@ -1,6 +1,7 @@ package agent import ( + "github.com/portainer/portainer/api/database" "net/http" portainer "github.com/portainer/portainer/api" @@ -11,7 +12,7 @@ type ( Transport struct { httpTransport *http.Transport signatureService portainer.DigitalSignatureService - endpointIdentifier portainer.EndpointID + endpointIdentifier database.EndpointID } ) diff --git a/api/http/proxy/factory/docker/access_control.go b/api/http/proxy/factory/docker/access_control.go index 1d097ecc1..c7ac52cff 100644 --- a/api/http/proxy/factory/docker/access_control.go +++ b/api/http/proxy/factory/docker/access_control.go @@ -1,6 +1,7 @@ package docker import ( + "github.com/portainer/portainer/api/database" "log" "net/http" "strings" @@ -311,7 +312,7 @@ func (transport *Transport) findResourceControl(resourceIdentifier string, resou return nil, nil } -func getStackResourceIDFromLabels(resourceLabelsObject map[string]string, endpointID portainer.EndpointID) string { +func getStackResourceIDFromLabels(resourceLabelsObject map[string]string, endpointID database.EndpointID) string { if resourceLabelsObject[resourceLabelForDockerSwarmStackName] != "" { stackName := resourceLabelsObject[resourceLabelForDockerSwarmStackName] return stackutils.ResourceControlID(endpointID, stackName) diff --git a/api/http/proxy/factory/docker/configs.go b/api/http/proxy/factory/docker/configs.go index 4820b74c6..424f0580f 100644 --- a/api/http/proxy/factory/docker/configs.go +++ b/api/http/proxy/factory/docker/configs.go @@ -2,6 +2,7 @@ package docker import ( "context" + "github.com/portainer/portainer/api/database" "net/http" "github.com/docker/docker/client" @@ -15,7 +16,7 @@ const ( configObjectIdentifier = "ID" ) -func getInheritedResourceControlFromConfigLabels(dockerClient *client.Client, endpointID portainer.EndpointID, configID string, resourceControls []portainer.ResourceControl) (*portainer.ResourceControl, error) { +func getInheritedResourceControlFromConfigLabels(dockerClient *client.Client, endpointID database.EndpointID, configID string, resourceControls []portainer.ResourceControl) (*portainer.ResourceControl, error) { config, _, err := dockerClient.ConfigInspectWithRaw(context.Background(), configID) if err != nil { return nil, err diff --git a/api/http/proxy/factory/docker/containers.go b/api/http/proxy/factory/docker/containers.go index 953b591e9..f720c9f56 100644 --- a/api/http/proxy/factory/docker/containers.go +++ b/api/http/proxy/factory/docker/containers.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "errors" + "github.com/portainer/portainer/api/database" "io/ioutil" "net/http" @@ -19,7 +20,7 @@ const ( containerObjectIdentifier = "Id" ) -func getInheritedResourceControlFromContainerLabels(dockerClient *client.Client, endpointID portainer.EndpointID, containerID string, resourceControls []portainer.ResourceControl) (*portainer.ResourceControl, error) { +func getInheritedResourceControlFromContainerLabels(dockerClient *client.Client, endpointID database.EndpointID, containerID string, resourceControls []portainer.ResourceControl) (*portainer.ResourceControl, error) { container, err := dockerClient.ContainerInspect(context.Background(), containerID) if err != nil { return nil, err diff --git a/api/http/proxy/factory/docker/networks.go b/api/http/proxy/factory/docker/networks.go index 05df57589..6ac23f14e 100644 --- a/api/http/proxy/factory/docker/networks.go +++ b/api/http/proxy/factory/docker/networks.go @@ -2,6 +2,7 @@ package docker import ( "context" + "github.com/portainer/portainer/api/database" "net/http" portainer "github.com/portainer/portainer/api" @@ -19,7 +20,7 @@ const ( networkObjectName = "Name" ) -func getInheritedResourceControlFromNetworkLabels(dockerClient *client.Client, endpointID portainer.EndpointID, networkID string, resourceControls []portainer.ResourceControl) (*portainer.ResourceControl, error) { +func getInheritedResourceControlFromNetworkLabels(dockerClient *client.Client, endpointID database.EndpointID, networkID string, resourceControls []portainer.ResourceControl) (*portainer.ResourceControl, error) { network, err := dockerClient.NetworkInspect(context.Background(), networkID, types.NetworkInspectOptions{}) if err != nil { return nil, err diff --git a/api/http/proxy/factory/docker/registry.go b/api/http/proxy/factory/docker/registry.go index 7992634f7..39e65ed2e 100644 --- a/api/http/proxy/factory/docker/registry.go +++ b/api/http/proxy/factory/docker/registry.go @@ -2,6 +2,7 @@ package docker import ( portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/http/security" "github.com/portainer/portainer/api/internal/registryutils" @@ -11,7 +12,7 @@ type ( registryAccessContext struct { isAdmin bool user *portainer.User - endpointID portainer.EndpointID + endpointID database.EndpointID teamMemberships []portainer.TeamMembership registries []portainer.Registry } diff --git a/api/http/proxy/factory/docker/secrets.go b/api/http/proxy/factory/docker/secrets.go index 6f7c203f8..ab4308781 100644 --- a/api/http/proxy/factory/docker/secrets.go +++ b/api/http/proxy/factory/docker/secrets.go @@ -2,6 +2,7 @@ package docker import ( "context" + "github.com/portainer/portainer/api/database" "net/http" "github.com/docker/docker/client" @@ -15,7 +16,7 @@ const ( secretObjectIdentifier = "ID" ) -func getInheritedResourceControlFromSecretLabels(dockerClient *client.Client, endpointID portainer.EndpointID, secretID string, resourceControls []portainer.ResourceControl) (*portainer.ResourceControl, error) { +func getInheritedResourceControlFromSecretLabels(dockerClient *client.Client, endpointID database.EndpointID, secretID string, resourceControls []portainer.ResourceControl) (*portainer.ResourceControl, error) { secret, _, err := dockerClient.SecretInspectWithRaw(context.Background(), secretID) if err != nil { return nil, err diff --git a/api/http/proxy/factory/docker/services.go b/api/http/proxy/factory/docker/services.go index 205c48c60..b470bac59 100644 --- a/api/http/proxy/factory/docker/services.go +++ b/api/http/proxy/factory/docker/services.go @@ -5,6 +5,7 @@ import ( "context" "encoding/json" "errors" + "github.com/portainer/portainer/api/database" "io/ioutil" "net/http" @@ -20,7 +21,7 @@ const ( serviceObjectIdentifier = "ID" ) -func getInheritedResourceControlFromServiceLabels(dockerClient *client.Client, endpointID portainer.EndpointID, serviceID string, resourceControls []portainer.ResourceControl) (*portainer.ResourceControl, error) { +func getInheritedResourceControlFromServiceLabels(dockerClient *client.Client, endpointID database.EndpointID, serviceID string, resourceControls []portainer.ResourceControl) (*portainer.ResourceControl, error) { service, _, err := dockerClient.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{}) if err != nil { return nil, err diff --git a/api/http/proxy/factory/docker/transport.go b/api/http/proxy/factory/docker/transport.go index d705d01ff..cbc0de1fb 100644 --- a/api/http/proxy/factory/docker/transport.go +++ b/api/http/proxy/factory/docker/transport.go @@ -6,6 +6,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/portainer/portainer/api/database" "io/ioutil" "log" "net/http" @@ -728,7 +729,7 @@ func (transport *Transport) isAdminOrEndpointAdmin(request *http.Request) (bool, } func (transport *Transport) fetchEndpointSecuritySettings() (*portainer.EndpointSecuritySettings, error) { - endpoint, err := transport.dataStore.Endpoint().Endpoint(portainer.EndpointID(transport.endpoint.ID)) + endpoint, err := transport.dataStore.Endpoint().Endpoint(database.EndpointID(transport.endpoint.ID)) if err != nil { return nil, err } diff --git a/api/http/proxy/factory/docker/volumes.go b/api/http/proxy/factory/docker/volumes.go index c4b44c985..19ad6a2b0 100644 --- a/api/http/proxy/factory/docker/volumes.go +++ b/api/http/proxy/factory/docker/volumes.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "github.com/portainer/portainer/api/database" "net/http" "path" @@ -20,7 +21,7 @@ const ( volumeObjectIdentifier = "ResourceID" ) -func getInheritedResourceControlFromVolumeLabels(dockerClient *client.Client, endpointID portainer.EndpointID, volumeID string, resourceControls []portainer.ResourceControl) (*portainer.ResourceControl, error) { +func getInheritedResourceControlFromVolumeLabels(dockerClient *client.Client, endpointID database.EndpointID, volumeID string, resourceControls []portainer.ResourceControl) (*portainer.ResourceControl, error) { volume, err := dockerClient.VolumeInspect(context.Background(), volumeID) if err != nil { return nil, err diff --git a/api/http/proxy/factory/kubernetes/token.go b/api/http/proxy/factory/kubernetes/token.go index 997898ea1..c587a29bd 100644 --- a/api/http/proxy/factory/kubernetes/token.go +++ b/api/http/proxy/factory/kubernetes/token.go @@ -1,6 +1,7 @@ package kubernetes import ( + "github.com/portainer/portainer/api/database" "io/ioutil" portainer "github.com/portainer/portainer/api" @@ -43,7 +44,7 @@ func (manager *tokenManager) GetAdminServiceAccountToken() string { return manager.adminToken } -func (manager *tokenManager) GetUserServiceAccountToken(userID int, endpointID portainer.EndpointID) (string, error) { +func (manager *tokenManager) GetUserServiceAccountToken(userID int, endpointID database.EndpointID) (string, error) { manager.tokenCache.mutex.Lock() defer manager.tokenCache.mutex.Unlock() diff --git a/api/http/proxy/manager.go b/api/http/proxy/manager.go index 953493be9..c35edc9b7 100644 --- a/api/http/proxy/manager.go +++ b/api/http/proxy/manager.go @@ -2,6 +2,7 @@ package proxy import ( "fmt" + "github.com/portainer/portainer/api/database" "net/http" "github.com/portainer/portainer/api/dataservices" @@ -64,7 +65,7 @@ func (manager *Manager) GetEndpointProxy(endpoint *portainer.Endpoint) http.Hand // DeleteEndpointProxy deletes the proxy associated to a key // and cleans the k8s environment(endpoint) client cache. DeleteEndpointProxy // is currently only called for edge connection clean up. -func (manager *Manager) DeleteEndpointProxy(endpointID portainer.EndpointID) { +func (manager *Manager) DeleteEndpointProxy(endpointID database.EndpointID) { manager.endpointProxies.Remove(fmt.Sprint(endpointID)) manager.k8sClientFactory.RemoveKubeClient(endpointID) } diff --git a/api/http/security/authorization.go b/api/http/security/authorization.go index 5be89aad3..2b74d1aeb 100644 --- a/api/http/security/authorization.go +++ b/api/http/security/authorization.go @@ -1,6 +1,7 @@ package security import ( + "github.com/portainer/portainer/api/database" "net/http" portainer "github.com/portainer/portainer/api" @@ -124,7 +125,7 @@ func authorizedEndpointGroupAccess(endpointGroup *portainer.EndpointGroup, userI // AuthorizedRegistryAccess ensure that the user can access the specified registry. // It will check if the user is part of the authorized users or part of a team that is // listed in the authorized teams for a specified environment(endpoint), -func AuthorizedRegistryAccess(registry *portainer.Registry, user *portainer.User, teamMemberships []portainer.TeamMembership, endpointID portainer.EndpointID) bool { +func AuthorizedRegistryAccess(registry *portainer.Registry, user *portainer.User, teamMemberships []portainer.TeamMembership, endpointID database.EndpointID) bool { if user.Role == portainer.AdministratorRole { return true } diff --git a/api/http/security/filter.go b/api/http/security/filter.go index a2593de80..31745143a 100644 --- a/api/http/security/filter.go +++ b/api/http/security/filter.go @@ -2,6 +2,7 @@ package security import ( portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" ) // FilterUserTeams filters teams based on user role. @@ -64,7 +65,7 @@ func FilterUsers(users []portainer.User, context *RestrictedRequestContext) []po // FilterRegistries filters registries based on user role and team memberships. // Non administrator users only have access to authorized registries. -func FilterRegistries(registries []portainer.Registry, user *portainer.User, teamMemberships []portainer.TeamMembership, endpointID portainer.EndpointID) []portainer.Registry { +func FilterRegistries(registries []portainer.Registry, user *portainer.User, teamMemberships []portainer.TeamMembership, endpointID database.EndpointID) []portainer.Registry { if user.Role == portainer.AdministratorRole { return registries } diff --git a/api/internal/authorization/authorizations.go b/api/internal/authorization/authorizations.go index 816cf7912..2fe074a63 100644 --- a/api/internal/authorization/authorizations.go +++ b/api/internal/authorization/authorizations.go @@ -2,6 +2,7 @@ package authorization import ( portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" "github.com/portainer/portainer/api/dataservices" "github.com/portainer/portainer/api/kubernetes/cli" ) @@ -601,7 +602,7 @@ func getAuthorizationsFromRoles(roleIdentifiers []portainer.RoleID, roles []port return authorizations } -func (service *Service) UserIsAdminOrAuthorized(userID portainer.UserID, endpointID portainer.EndpointID, authorizations []portainer.Authorization) (bool, error) { +func (service *Service) UserIsAdminOrAuthorized(userID portainer.UserID, endpointID database.EndpointID, authorizations []portainer.Authorization) (bool, error) { user, err := service.dataStore.User().User(userID) if err != nil { return false, err diff --git a/api/internal/edge/edgegroup.go b/api/internal/edge/edgegroup.go index 745f480ce..15bedb791 100644 --- a/api/internal/edge/edgegroup.go +++ b/api/internal/edge/edgegroup.go @@ -2,16 +2,17 @@ package edge import ( "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" "github.com/portainer/portainer/api/internal/tag" ) // EdgeGroupRelatedEndpoints returns a list of environments(endpoints) related to this Edge group -func EdgeGroupRelatedEndpoints(edgeGroup *portainer.EdgeGroup, endpoints []portainer.Endpoint, endpointGroups []portainer.EndpointGroup) []portainer.EndpointID { +func EdgeGroupRelatedEndpoints(edgeGroup *portainer.EdgeGroup, endpoints []portainer.Endpoint, endpointGroups []portainer.EndpointGroup) []database.EndpointID { if !edgeGroup.Dynamic { return edgeGroup.Endpoints } - endpointIDs := []portainer.EndpointID{} + endpointIDs := []database.EndpointID{} for _, endpoint := range endpoints { if endpoint.Type != portainer.EdgeAgentOnDockerEnvironment && endpoint.Type != portainer.EdgeAgentOnKubernetesEnvironment { continue diff --git a/api/internal/edge/edgestack.go b/api/internal/edge/edgestack.go index 10633598a..16e321c6a 100644 --- a/api/internal/edge/edgestack.go +++ b/api/internal/edge/edgestack.go @@ -3,11 +3,12 @@ package edge import ( "errors" "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" ) // EdgeStackRelatedEndpoints returns a list of environments(endpoints) related to this Edge stack -func EdgeStackRelatedEndpoints(edgeGroupIDs []portainer.EdgeGroupID, endpoints []portainer.Endpoint, endpointGroups []portainer.EndpointGroup, edgeGroups []portainer.EdgeGroup) ([]portainer.EndpointID, error) { - edgeStackEndpoints := []portainer.EndpointID{} +func EdgeStackRelatedEndpoints(edgeGroupIDs []portainer.EdgeGroupID, endpoints []portainer.Endpoint, endpointGroups []portainer.EndpointGroup, edgeGroups []portainer.EdgeGroup) ([]database.EndpointID, error) { + edgeStackEndpoints := []database.EndpointID{} for _, edgeGroupID := range edgeGroupIDs { var edgeGroup *portainer.EdgeGroup diff --git a/api/internal/endpointutils/endpoint_test.go b/api/internal/endpointutils/endpoint_test.go index 208d79dfd..f1ea305d7 100644 --- a/api/internal/endpointutils/endpoint_test.go +++ b/api/internal/endpointutils/endpoint_test.go @@ -1,6 +1,7 @@ package endpointutils import ( + "github.com/portainer/portainer/api/database" "testing" portainer "github.com/portainer/portainer/api" @@ -67,33 +68,33 @@ func Test_FilterByExcludeIDs(t *testing.T) { tests := []struct { name string inputArray []portainer.Endpoint - inputExcludeIDs []portainer.EndpointID + inputExcludeIDs []database.EndpointID asserts func(*testing.T, []portainer.Endpoint) }{ { name: "filter endpoints", inputArray: []portainer.Endpoint{ - {ID: portainer.EndpointID(1)}, - {ID: portainer.EndpointID(2)}, - {ID: portainer.EndpointID(3)}, - {ID: portainer.EndpointID(4)}, + {ID: database.EndpointID(1)}, + {ID: database.EndpointID(2)}, + {ID: database.EndpointID(3)}, + {ID: database.EndpointID(4)}, }, - inputExcludeIDs: []portainer.EndpointID{ - portainer.EndpointID(2), - portainer.EndpointID(3), + inputExcludeIDs: []database.EndpointID{ + database.EndpointID(2), + database.EndpointID(3), }, asserts: func(t *testing.T, output []portainer.Endpoint) { - assert.Contains(t, output, portainer.Endpoint{ID: portainer.EndpointID(1)}) - assert.NotContains(t, output, portainer.Endpoint{ID: portainer.EndpointID(2)}) - assert.NotContains(t, output, portainer.Endpoint{ID: portainer.EndpointID(3)}) - assert.Contains(t, output, portainer.Endpoint{ID: portainer.EndpointID(4)}) + assert.Contains(t, output, portainer.Endpoint{ID: database.EndpointID(1)}) + assert.NotContains(t, output, portainer.Endpoint{ID: database.EndpointID(2)}) + assert.NotContains(t, output, portainer.Endpoint{ID: database.EndpointID(3)}) + assert.Contains(t, output, portainer.Endpoint{ID: database.EndpointID(4)}) }, }, { name: "empty input", inputArray: []portainer.Endpoint{}, - inputExcludeIDs: []portainer.EndpointID{ - portainer.EndpointID(2), + inputExcludeIDs: []database.EndpointID{ + database.EndpointID(2), }, asserts: func(t *testing.T, output []portainer.Endpoint) { assert.Equal(t, 0, len(output)) @@ -102,10 +103,10 @@ func Test_FilterByExcludeIDs(t *testing.T) { { name: "no filter", inputArray: []portainer.Endpoint{ - {ID: portainer.EndpointID(1)}, - {ID: portainer.EndpointID(2)}, + {ID: database.EndpointID(1)}, + {ID: database.EndpointID(2)}, }, - inputExcludeIDs: []portainer.EndpointID{}, + inputExcludeIDs: []database.EndpointID{}, asserts: func(t *testing.T, output []portainer.Endpoint) { assert.Equal(t, 2, len(output)) }, diff --git a/api/internal/endpointutils/endpointutils.go b/api/internal/endpointutils/endpointutils.go index 128e61a86..f28fed3a9 100644 --- a/api/internal/endpointutils/endpointutils.go +++ b/api/internal/endpointutils/endpointutils.go @@ -1,6 +1,7 @@ package endpointutils import ( + "github.com/portainer/portainer/api/database" "strings" portainer "github.com/portainer/portainer/api" @@ -39,14 +40,14 @@ func IsAgentEndpoint(endpoint *portainer.Endpoint) bool { } // FilterByExcludeIDs receives an environment(endpoint) array and returns a filtered array using an excludeIds param -func FilterByExcludeIDs(endpoints []portainer.Endpoint, excludeIds []portainer.EndpointID) []portainer.Endpoint { +func FilterByExcludeIDs(endpoints []portainer.Endpoint, excludeIds []database.EndpointID) []portainer.Endpoint { if len(excludeIds) == 0 { return endpoints } filteredEndpoints := make([]portainer.Endpoint, 0) - idsSet := make(map[portainer.EndpointID]bool) + idsSet := make(map[database.EndpointID]bool) for _, id := range excludeIds { idsSet[id] = true } diff --git a/api/internal/registryutils/access/access.go b/api/internal/registryutils/access/access.go index 53f0d6a5e..7659a0e70 100644 --- a/api/internal/registryutils/access/access.go +++ b/api/internal/registryutils/access/access.go @@ -2,6 +2,7 @@ package access import ( "fmt" + "github.com/portainer/portainer/api/database" portainer "github.com/portainer/portainer/api" "github.com/portainer/portainer/api/dataservices" @@ -11,7 +12,7 @@ import ( func hasPermission( dataStore dataservices.DataStore, userID portainer.UserID, - endpointID portainer.EndpointID, + endpointID database.EndpointID, registry *portainer.Registry, ) (hasPermission bool, err error) { user, err := dataStore.User().User(userID) @@ -37,7 +38,7 @@ func hasPermission( func GetAccessibleRegistry( dataStore dataservices.DataStore, userID portainer.UserID, - endpointID portainer.EndpointID, + endpointID database.EndpointID, registryID portainer.RegistryID, ) (registry *portainer.Registry, err error) { diff --git a/api/internal/registryutils/ecr_kube_secret.go b/api/internal/registryutils/ecr_kube_secret.go index 040fb4a40..4e6f818ba 100644 --- a/api/internal/registryutils/ecr_kube_secret.go +++ b/api/internal/registryutils/ecr_kube_secret.go @@ -2,10 +2,11 @@ package registryutils import ( portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" "github.com/portainer/portainer/api/dataservices" ) -func isRegistryAssignedToNamespace(registry portainer.Registry, endpointID portainer.EndpointID, namespace string) (in bool) { +func isRegistryAssignedToNamespace(registry portainer.Registry, endpointID database.EndpointID, namespace string) (in bool) { for _, ns := range registry.RegistryAccesses[endpointID].Namespaces { if ns == namespace { return true diff --git a/api/internal/stackutils/stackutils.go b/api/internal/stackutils/stackutils.go index 0d8b92e6b..86352a252 100644 --- a/api/internal/stackutils/stackutils.go +++ b/api/internal/stackutils/stackutils.go @@ -2,6 +2,7 @@ package stackutils import ( "fmt" + "github.com/portainer/portainer/api/database" "io/ioutil" "github.com/pkg/errors" @@ -11,7 +12,7 @@ import ( ) // ResourceControlID returns the stack resource control id -func ResourceControlID(endpointID portainer.EndpointID, name string) string { +func ResourceControlID(endpointID database.EndpointID, name string) string { return fmt.Sprintf("%d_%s", endpointID, name) } diff --git a/api/internal/testhelpers/datastore.go b/api/internal/testhelpers/datastore.go index ff9f9e68a..403150698 100644 --- a/api/internal/testhelpers/datastore.go +++ b/api/internal/testhelpers/datastore.go @@ -1,6 +1,8 @@ package testhelpers import ( + "github.com/portainer/portainer/api/database" + "github.com/portainer/portainer/api/dataservices/edgejob" "io" portainer "github.com/portainer/portainer/api" @@ -145,23 +147,23 @@ func WithUsers(us []portainer.User) datastoreOption { } type stubEdgeJobService struct { - jobs []portainer.EdgeJob + jobs []edgejob.EdgeJob } -func (s *stubEdgeJobService) BucketName() string { return "edgejob" } -func (s *stubEdgeJobService) EdgeJobs() ([]portainer.EdgeJob, error) { return s.jobs, nil } -func (s *stubEdgeJobService) EdgeJob(ID portainer.EdgeJobID) (*portainer.EdgeJob, error) { +func (s *stubEdgeJobService) BucketName() string { return "edgejob" } +func (s *stubEdgeJobService) EdgeJobs() ([]edgejob.EdgeJob, error) { return s.jobs, nil } +func (s *stubEdgeJobService) EdgeJob(ID edgejob.EdgeJobID) (*edgejob.EdgeJob, error) { return nil, nil } -func (s *stubEdgeJobService) Create(edgeJob *portainer.EdgeJob) error { return nil } -func (s *stubEdgeJobService) UpdateEdgeJob(ID portainer.EdgeJobID, edgeJob *portainer.EdgeJob) error { +func (s *stubEdgeJobService) Create(edgeJob *edgejob.EdgeJob) error { return nil } +func (s *stubEdgeJobService) UpdateEdgeJob(ID edgejob.EdgeJobID, edgeJob *edgejob.EdgeJob) error { return nil } -func (s *stubEdgeJobService) DeleteEdgeJob(ID portainer.EdgeJobID) error { return nil } -func (s *stubEdgeJobService) GetNextIdentifier() int { return 0 } +func (s *stubEdgeJobService) DeleteEdgeJob(ID edgejob.EdgeJobID) error { return nil } +func (s *stubEdgeJobService) GetNextIdentifier() int { return 0 } // WithEdgeJobs option will instruct testDatastore to return provided jobs -func WithEdgeJobs(js []portainer.EdgeJob) datastoreOption { +func WithEdgeJobs(js []edgejob.EdgeJob) datastoreOption { return func(d *testDatastore) { d.edgeJob = &stubEdgeJobService{jobs: js} } @@ -175,7 +177,7 @@ func (s *stubEndpointRelationService) BucketName() string { return "endpoint_rel func (s *stubEndpointRelationService) EndpointRelations() ([]portainer.EndpointRelation, error) { return s.relations, nil } -func (s *stubEndpointRelationService) EndpointRelation(ID portainer.EndpointID) (*portainer.EndpointRelation, error) { +func (s *stubEndpointRelationService) EndpointRelation(ID database.EndpointID) (*portainer.EndpointRelation, error) { for _, relation := range s.relations { if relation.EndpointID == ID { return &relation, nil @@ -187,7 +189,7 @@ func (s *stubEndpointRelationService) EndpointRelation(ID portainer.EndpointID) func (s *stubEndpointRelationService) Create(EndpointRelation *portainer.EndpointRelation) error { return nil } -func (s *stubEndpointRelationService) UpdateEndpointRelation(ID portainer.EndpointID, relation *portainer.EndpointRelation) error { +func (s *stubEndpointRelationService) UpdateEndpointRelation(ID database.EndpointID, relation *portainer.EndpointRelation) error { for i, r := range s.relations { if r.EndpointID == ID { s.relations[i] = *relation @@ -196,7 +198,7 @@ func (s *stubEndpointRelationService) UpdateEndpointRelation(ID portainer.Endpoi return nil } -func (s *stubEndpointRelationService) DeleteEndpointRelation(ID portainer.EndpointID) error { +func (s *stubEndpointRelationService) DeleteEndpointRelation(ID database.EndpointID) error { return nil } func (s *stubEndpointRelationService) GetNextIdentifier() int { return 0 } @@ -213,7 +215,7 @@ type stubEndpointService struct { } func (s *stubEndpointService) BucketName() string { return "endpoint" } -func (s *stubEndpointService) Endpoint(ID portainer.EndpointID) (*portainer.Endpoint, error) { +func (s *stubEndpointService) Endpoint(ID database.EndpointID) (*portainer.Endpoint, error) { for _, endpoint := range s.endpoints { if endpoint.ID == ID { return &endpoint, nil @@ -233,7 +235,7 @@ func (s *stubEndpointService) Create(endpoint *portainer.Endpoint) error { return nil } -func (s *stubEndpointService) UpdateEndpoint(ID portainer.EndpointID, endpoint *portainer.Endpoint) error { +func (s *stubEndpointService) UpdateEndpoint(ID database.EndpointID, endpoint *portainer.Endpoint) error { for i, e := range s.endpoints { if e.ID == ID { s.endpoints[i] = *endpoint @@ -243,7 +245,7 @@ func (s *stubEndpointService) UpdateEndpoint(ID portainer.EndpointID, endpoint * return nil } -func (s *stubEndpointService) DeleteEndpoint(ID portainer.EndpointID) error { +func (s *stubEndpointService) DeleteEndpoint(ID database.EndpointID) error { endpoints := []portainer.Endpoint{} for _, endpoint := range s.endpoints { diff --git a/api/internal/testhelpers/reverse_tunnel_service.go b/api/internal/testhelpers/reverse_tunnel_service.go index 0dbc19d19..d708ebda4 100644 --- a/api/internal/testhelpers/reverse_tunnel_service.go +++ b/api/internal/testhelpers/reverse_tunnel_service.go @@ -1,6 +1,10 @@ package testhelpers -import portainer "github.com/portainer/portainer/api" +import ( + portainer "github.com/portainer/portainer/api" + "github.com/portainer/portainer/api/database" + "github.com/portainer/portainer/api/dataservices/edgejob" +) type ReverseTunnelService struct{} @@ -10,14 +14,14 @@ func (r ReverseTunnelService) StartTunnelServer(addr, port string, snapshotServi func (r ReverseTunnelService) GenerateEdgeKey(url, host string, endpointIdentifier int) string { return "nil" } -func (r ReverseTunnelService) SetTunnelStatusToActive(endpointID portainer.EndpointID) {} -func (r ReverseTunnelService) SetTunnelStatusToRequired(endpointID portainer.EndpointID) error { +func (r ReverseTunnelService) SetTunnelStatusToActive(endpointID database.EndpointID) {} +func (r ReverseTunnelService) SetTunnelStatusToRequired(endpointID database.EndpointID) error { return nil } -func (r ReverseTunnelService) SetTunnelStatusToIdle(endpointID portainer.EndpointID) {} -func (r ReverseTunnelService) GetTunnelDetails(endpointID portainer.EndpointID) *portainer.TunnelDetails { +func (r ReverseTunnelService) SetTunnelStatusToIdle(endpointID database.EndpointID) {} +func (r ReverseTunnelService) GetTunnelDetails(endpointID database.EndpointID) *portainer.TunnelDetails { return nil } -func (r ReverseTunnelService) AddEdgeJob(endpointID portainer.EndpointID, edgeJob *portainer.EdgeJob) { +func (r ReverseTunnelService) AddEdgeJob(endpointID database.EndpointID, edgeJob *edgejob.EdgeJob) { } -func (r ReverseTunnelService) RemoveEdgeJob(edgeJobID portainer.EdgeJobID) {} +func (r ReverseTunnelService) RemoveEdgeJob(edgeJobID edgejob.EdgeJobID) {} diff --git a/api/kubernetes/cli/client.go b/api/kubernetes/cli/client.go index bc3e43c46..122d10282 100644 --- a/api/kubernetes/cli/client.go +++ b/api/kubernetes/cli/client.go @@ -3,6 +3,7 @@ package cli import ( "fmt" cmap "github.com/orcaman/concurrent-map" + "github.com/portainer/portainer/api/database" "net/http" "strconv" "sync" @@ -50,7 +51,7 @@ func (factory *ClientFactory) GetInstanceID() (instanceID string) { } // Remove the cached kube client so a new one can be created -func (factory *ClientFactory) RemoveKubeClient(endpointID portainer.EndpointID) { +func (factory *ClientFactory) RemoveKubeClient(endpointID database.EndpointID) { factory.endpointClients.Remove(strconv.Itoa(int(endpointID))) } diff --git a/api/kubernetes/kubeconfig_service.go b/api/kubernetes/kubeconfig_service.go index 84ac6834a..3e608b278 100644 --- a/api/kubernetes/kubeconfig_service.go +++ b/api/kubernetes/kubeconfig_service.go @@ -5,17 +5,17 @@ import ( "encoding/base64" "encoding/pem" "fmt" + "github.com/portainer/portainer/api/database" "io/ioutil" "log" "github.com/pkg/errors" - portainer "github.com/portainer/portainer/api" ) // KubeConfigService represents a service that is responsible for handling kubeconfig operations type KubeConfigService interface { IsSecure() bool - GetKubeConfigInternal(endpointId portainer.EndpointID, authToken string) kubernetesClusterAccess + GetKubeConfigInternal(endpointId database.EndpointID, authToken string) kubernetesClusterAccess } // KubernetesClusterAccess represents core details which can be used to generate KubeConfig file/data @@ -93,7 +93,7 @@ func (kccas *kubeConfigCAService) IsSecure() bool { // The struct can be used to: // - generate a kubeconfig file // - pass down params to binaries -func (kccas *kubeConfigCAService) GetKubeConfigInternal(endpointId portainer.EndpointID, authToken string) kubernetesClusterAccess { +func (kccas *kubeConfigCAService) GetKubeConfigInternal(endpointId database.EndpointID, authToken string) kubernetesClusterAccess { clusterServerUrl := fmt.Sprintf("https://localhost%s/api/endpoints/%s/kubernetes", kccas.httpsBindAddr, fmt.Sprint(endpointId)) return kubernetesClusterAccess{ ClusterServerURL: clusterServerUrl, diff --git a/api/portainer.go b/api/portainer.go index 74d1e0c22..b926c6477 100644 --- a/api/portainer.go +++ b/api/portainer.go @@ -2,6 +2,8 @@ package portainer import ( "context" + "github.com/portainer/portainer/api/database" + "github.com/portainer/portainer/api/dataservices/edgejob" "io" "time" @@ -191,64 +193,40 @@ type ( // EdgeGroup represents an Edge group EdgeGroup struct { // EdgeGroup Identifier - ID EdgeGroupID `json:"Id" example:"1"` - Name string `json:"Name"` - Dynamic bool `json:"Dynamic"` - TagIDs []TagID `json:"TagIds"` - Endpoints []EndpointID `json:"Endpoints"` - PartialMatch bool `json:"PartialMatch"` + ID EdgeGroupID `json:"Id" example:"1"` + Name string `json:"Name"` + Dynamic bool `json:"Dynamic"` + TagIDs []TagID `json:"TagIds"` + Endpoints []database.EndpointID `json:"Endpoints"` + PartialMatch bool `json:"PartialMatch"` } + // EdgeGroupID represents an Edge group identifier // EdgeGroupID represents an Edge group identifier EdgeGroupID int - // EdgeJob represents a job that can run on Edge environments(endpoints). - EdgeJob struct { - // EdgeJob Identifier - ID EdgeJobID `json:"Id" example:"1"` - Created int64 `json:"Created"` - CronExpression string `json:"CronExpression"` - Endpoints map[EndpointID]EdgeJobEndpointMeta `json:"Endpoints"` - Name string `json:"Name"` - ScriptPath string `json:"ScriptPath"` - Recurring bool `json:"Recurring"` - Version int `json:"Version"` - } - - // EdgeJobEndpointMeta represents a meta data object for an Edge job and Environment(Endpoint) relation - EdgeJobEndpointMeta struct { - LogsStatus EdgeJobLogsStatus - CollectLogs bool - } - - // EdgeJobID represents an Edge job identifier - EdgeJobID int - - // EdgeJobLogsStatus represent status of logs collection job - EdgeJobLogsStatus int - // EdgeSchedule represents a scheduled job that can run on Edge environments(endpoints). // Deprecated in favor of EdgeJob EdgeSchedule struct { // EdgeSchedule Identifier - ID ScheduleID `json:"Id" example:"1"` - CronExpression string `json:"CronExpression"` - Script string `json:"Script"` - Version int `json:"Version"` - Endpoints []EndpointID `json:"Endpoints"` + ID ScheduleID `json:"Id" example:"1"` + CronExpression string `json:"CronExpression"` + Script string `json:"Script"` + Version int `json:"Version"` + Endpoints []database.EndpointID `json:"Endpoints"` } //EdgeStack represents an edge stack EdgeStack struct { // EdgeStack Identifier - ID EdgeStackID `json:"Id" example:"1"` - Name string `json:"Name"` - Status map[EndpointID]EdgeStackStatus `json:"Status"` - CreationDate int64 `json:"CreationDate"` - EdgeGroups []EdgeGroupID `json:"EdgeGroups"` - ProjectPath string `json:"ProjectPath"` - EntryPoint string `json:"EntryPoint"` - Version int `json:"Version"` + ID EdgeStackID `json:"Id" example:"1"` + Name string `json:"Name"` + Status map[database.EndpointID]EdgeStackStatus `json:"Status"` + CreationDate int64 `json:"CreationDate"` + EdgeGroups []EdgeGroupID `json:"EdgeGroups"` + ProjectPath string `json:"ProjectPath"` + EntryPoint string `json:"EntryPoint"` + Version int `json:"Version"` ManifestPath string DeploymentType EdgeStackDeploymentType @@ -265,7 +243,7 @@ type ( EdgeStackStatus struct { Type EdgeStackStatusType `json:"Type"` Error string `json:"Error"` - EndpointID EndpointID `json:"EndpointID"` + EndpointID database.EndpointID `json:"EndpointID"` } //EdgeStackStatusType represents an edge stack status type @@ -275,7 +253,7 @@ type ( // to connect to it Endpoint struct { // Environment(Endpoint) Identifier - ID EndpointID `json:"Id" example:"1"` + ID database.EndpointID `json:"Id" example:"1"` // Environment(Endpoint) name Name string `json:"Name" example:"my-environment"` // Environment(Endpoint) environment(endpoint) type. 1 for a Docker environment(endpoint), 2 for an agent on Docker environment(endpoint) or 3 for an Azure environment(endpoint). @@ -335,7 +313,7 @@ type ( } // EndpointAuthorizations represents the authorizations associated to a set of environments(endpoints) - EndpointAuthorizations map[EndpointID]Authorizations + EndpointAuthorizations map[database.EndpointID]Authorizations // EndpointGroup represents a group of environments(endpoints) EndpointGroup struct { @@ -364,9 +342,6 @@ type ( // EndpointGroupID represents an environment(endpoint) group identifier EndpointGroupID int - // EndpointID represents an environment(endpoint) identifier - EndpointID int - // EndpointStatus represents the status of an environment(endpoint) EndpointStatus int @@ -401,7 +376,7 @@ type ( // EndpointRelation represents a environment(endpoint) relation object EndpointRelation struct { - EndpointID EndpointID + EndpointID database.EndpointID EdgeStacks map[EdgeStackID]bool } @@ -643,7 +618,7 @@ type ( AccessTokenExpiry int64 `json:"AccessTokenExpiry,omitempty"` } - RegistryAccesses map[EndpointID]RegistryAccessPolicies + RegistryAccesses map[database.EndpointID]RegistryAccessPolicies RegistryAccessPolicies struct { UserAccessPolicies UserAccessPolicies `json:"UserAccessPolicies"` @@ -758,7 +733,7 @@ type ( // ScriptExecutionJob represents a scheduled job that can execute a script via a privileged container ScriptExecutionJob struct { - Endpoints []EndpointID + Endpoints []database.EndpointID Image string ScriptPath string RetryCount int @@ -839,7 +814,7 @@ type ( // Stack type. 1 for a Swarm stack, 2 for a Compose stack Type StackType `json:"Type" example:"2"` // Environment(Endpoint) identifier. Reference the environment(endpoint) that will be used for deployment - EndpointID EndpointID `json:"EndpointId" example:"1"` + EndpointID database.EndpointID `json:"EndpointId" example:"1"` // Cluster identifier of the Swarm cluster where the stack is deployed SwarmID string `json:"SwarmId" example:"jpofkc0i9uo9wtx1zesuk649w"` // Path to the Stack file @@ -908,7 +883,7 @@ type ( // Tag name Name string `json:"Name" example:"org/acme"` // A set of environment(endpoint) ids that have this tag - Endpoints map[EndpointID]bool `json:"Endpoints"` + Endpoints map[database.EndpointID]bool `json:"Endpoints"` // A set of environment(endpoint) group ids that have this tag EndpointGroups map[EndpointGroupID]bool `json:"EndpointGroups"` } @@ -1097,7 +1072,7 @@ type ( Status string LastActivity time.Time Port int - Jobs []EdgeJob + Jobs []edgejob.EdgeJob Credentials string } @@ -1143,12 +1118,12 @@ type ( // Webhook represents a url webhook that can be used to update a service Webhook struct { // Webhook Identifier - ID WebhookID `json:"Id" example:"1"` - Token string `json:"Token"` - ResourceID string `json:"ResourceId"` - EndpointID EndpointID `json:"EndpointId"` - RegistryID RegistryID `json:"RegistryId"` - WebhookType WebhookType `json:"Type"` + ID WebhookID `json:"Id" example:"1"` + Token string `json:"Token"` + ResourceID string `json:"ResourceId"` + EndpointID database.EndpointID `json:"EndpointId"` + RegistryID RegistryID `json:"RegistryId"` + WebhookType WebhookType `json:"Type"` } // WebhookID represents a webhook identifier. @@ -1290,14 +1265,14 @@ type ( StartTunnelServer(addr, port string, snapshotService SnapshotService) error StopTunnelServer() error GenerateEdgeKey(url, host string, endpointIdentifier int) string - SetTunnelStatusToActive(endpointID EndpointID) - SetTunnelStatusToRequired(endpointID EndpointID) error - SetTunnelStatusToIdle(endpointID EndpointID) - KeepTunnelAlive(endpointID EndpointID, ctx context.Context, maxKeepAlive time.Duration) - GetTunnelDetails(endpointID EndpointID) *TunnelDetails + SetTunnelStatusToActive(endpointID database.EndpointID) + SetTunnelStatusToRequired(endpointID database.EndpointID) error + SetTunnelStatusToIdle(endpointID database.EndpointID) + KeepTunnelAlive(endpointID database.EndpointID, ctx context.Context, maxKeepAlive time.Duration) + GetTunnelDetails(endpointID database.EndpointID) *TunnelDetails GetActiveTunnel(endpoint *Endpoint) (*TunnelDetails, error) - AddEdgeJob(endpointID EndpointID, edgeJob *EdgeJob) - RemoveEdgeJob(edgeJobID EdgeJobID) + AddEdgeJob(endpointID database.EndpointID, edgeJob *edgejob.EdgeJob) + RemoveEdgeJob(edgeJobID edgejob.EdgeJobID) } // Server defines the interface to serve the API @@ -1393,7 +1368,7 @@ const ( ) const ( - _ EdgeJobLogsStatus = iota + _ edgejob.EdgeJobLogsStatus = iota // EdgeJobLogsStatusIdle represents an idle log collection job EdgeJobLogsStatusIdle // EdgeJobLogsStatusPending represents a pending log collection job diff --git a/api/stacks/deploy.go b/api/stacks/deploy.go index 3e7180687..8e8371279 100644 --- a/api/stacks/deploy.go +++ b/api/stacks/deploy.go @@ -2,6 +2,7 @@ package stacks import ( "fmt" + "github.com/portainer/portainer/api/database" "strings" "time" @@ -117,7 +118,7 @@ func RedeployWhenChanged(stackID portainer.StackID, deployer StackDeployer, data return nil } -func getUserRegistries(datastore dataservices.DataStore, user *portainer.User, endpointID portainer.EndpointID) ([]portainer.Registry, error) { +func getUserRegistries(datastore dataservices.DataStore, user *portainer.User, endpointID database.EndpointID) ([]portainer.Registry, error) { registries, err := datastore.Registry().Registries() if err != nil { return nil, errors.WithMessage(err, "unable to retrieve registries from the database") diff --git a/api/stacks/deploy_test.go b/api/stacks/deploy_test.go index 3620e53c4..9cba28993 100644 --- a/api/stacks/deploy_test.go +++ b/api/stacks/deploy_test.go @@ -2,6 +2,7 @@ package stacks import ( "errors" + "github.com/portainer/portainer/api/database" "io/ioutil" "strings" "testing" @@ -191,7 +192,7 @@ func Test_getUserRegistries(t *testing.T) { ID: 1, Name: "registryReachableByUser", RegistryAccesses: portainer.RegistryAccesses{ - portainer.EndpointID(endpointID): { + database.EndpointID(endpointID): { UserAccessPolicies: map[portainer.UserID]portainer.AccessPolicy{ user.ID: {RoleID: portainer.RoleID(portainer.StandardUserRole)}, }, @@ -205,7 +206,7 @@ func Test_getUserRegistries(t *testing.T) { ID: 2, Name: "registryReachableByTeam", RegistryAccesses: portainer.RegistryAccesses{ - portainer.EndpointID(endpointID): { + database.EndpointID(endpointID): { TeamAccessPolicies: map[portainer.TeamID]portainer.AccessPolicy{ team.ID: {RoleID: portainer.RoleID(portainer.StandardUserRole)}, }, @@ -219,7 +220,7 @@ func Test_getUserRegistries(t *testing.T) { ID: 3, Name: "registryRestricted", RegistryAccesses: portainer.RegistryAccesses{ - portainer.EndpointID(endpointID): { + database.EndpointID(endpointID): { UserAccessPolicies: map[portainer.UserID]portainer.AccessPolicy{ user.ID + 100: {RoleID: portainer.RoleID(portainer.StandardUserRole)}, }, @@ -230,13 +231,13 @@ func Test_getUserRegistries(t *testing.T) { assert.NoError(t, err, "couldn't create a registry") t.Run("admin should has access to all registries", func(t *testing.T) { - registries, err := getUserRegistries(store, admin, portainer.EndpointID(endpointID)) + registries, err := getUserRegistries(store, admin, database.EndpointID(endpointID)) assert.NoError(t, err) assert.ElementsMatch(t, []portainer.Registry{registryReachableByUser, registryReachableByTeam, registryRestricted}, registries) }) t.Run("regular user has access to registries allowed to him and/or his team", func(t *testing.T) { - registries, err := getUserRegistries(store, user, portainer.EndpointID(endpointID)) + registries, err := getUserRegistries(store, user, database.EndpointID(endpointID)) assert.NoError(t, err) assert.ElementsMatch(t, []portainer.Registry{registryReachableByUser, registryReachableByTeam}, registries) })