diff --git a/api/authorizations.go b/api/authorizations.go index 2309aec23..78b84b6bf 100644 --- a/api/authorizations.go +++ b/api/authorizations.go @@ -3,34 +3,13 @@ package portainer // AuthorizationService represents a service used to // update authorizations associated to a user or team. type AuthorizationService struct { - endpointService EndpointService - endpointGroupService EndpointGroupService - registryService RegistryService - roleService RoleService - teamMembershipService TeamMembershipService - userService UserService -} - -// AuthorizationServiceParameters are the required parameters -// used to create a new AuthorizationService. -type AuthorizationServiceParameters struct { - EndpointService EndpointService - EndpointGroupService EndpointGroupService - RegistryService RegistryService - RoleService RoleService - TeamMembershipService TeamMembershipService - UserService UserService + dataStore DataStore } // NewAuthorizationService returns a point to a new AuthorizationService instance. -func NewAuthorizationService(parameters *AuthorizationServiceParameters) *AuthorizationService { +func NewAuthorizationService(dataStore DataStore) *AuthorizationService { return &AuthorizationService{ - endpointService: parameters.EndpointService, - endpointGroupService: parameters.EndpointGroupService, - registryService: parameters.RegistryService, - roleService: parameters.RoleService, - teamMembershipService: parameters.TeamMembershipService, - userService: parameters.UserService, + dataStore: dataStore, } } @@ -449,7 +428,7 @@ func DefaultPortainerAuthorizations() Authorizations { // the authorizations will be dropped for the each role. If removeAuthorizations is set to false, the authorizations // will be reset based for each role. func (service AuthorizationService) UpdateVolumeBrowsingAuthorizations(remove bool) error { - roles, err := service.roleService.Roles() + roles, err := service.dataStore.Role().Roles() if err != nil { return err } @@ -459,7 +438,7 @@ func (service AuthorizationService) UpdateVolumeBrowsingAuthorizations(remove bo if role.ID != RoleID(1) { updateRoleVolumeBrowsingAuthorizations(&role, remove) - err := service.roleService.UpdateRole(role.ID, &role) + err := service.dataStore.Role().UpdateRole(role.ID, &role) if err != nil { return err } @@ -492,7 +471,7 @@ func updateRoleVolumeBrowsingAuthorizations(role *Role, removeAuthorizations boo // RemoveTeamAccessPolicies will remove all existing access policies associated to the specified team func (service *AuthorizationService) RemoveTeamAccessPolicies(teamID TeamID) error { - endpoints, err := service.endpointService.Endpoints() + endpoints, err := service.dataStore.Endpoint().Endpoints() if err != nil { return err } @@ -502,7 +481,7 @@ func (service *AuthorizationService) RemoveTeamAccessPolicies(teamID TeamID) err if policyTeamID == teamID { delete(endpoint.TeamAccessPolicies, policyTeamID) - err := service.endpointService.UpdateEndpoint(endpoint.ID, &endpoint) + err := service.dataStore.Endpoint().UpdateEndpoint(endpoint.ID, &endpoint) if err != nil { return err } @@ -512,7 +491,7 @@ func (service *AuthorizationService) RemoveTeamAccessPolicies(teamID TeamID) err } } - endpointGroups, err := service.endpointGroupService.EndpointGroups() + endpointGroups, err := service.dataStore.EndpointGroup().EndpointGroups() if err != nil { return err } @@ -522,7 +501,7 @@ func (service *AuthorizationService) RemoveTeamAccessPolicies(teamID TeamID) err if policyTeamID == teamID { delete(endpointGroup.TeamAccessPolicies, policyTeamID) - err := service.endpointGroupService.UpdateEndpointGroup(endpointGroup.ID, &endpointGroup) + err := service.dataStore.EndpointGroup().UpdateEndpointGroup(endpointGroup.ID, &endpointGroup) if err != nil { return err } @@ -532,7 +511,7 @@ func (service *AuthorizationService) RemoveTeamAccessPolicies(teamID TeamID) err } } - registries, err := service.registryService.Registries() + registries, err := service.dataStore.Registry().Registries() if err != nil { return err } @@ -542,7 +521,7 @@ func (service *AuthorizationService) RemoveTeamAccessPolicies(teamID TeamID) err if policyTeamID == teamID { delete(registry.TeamAccessPolicies, policyTeamID) - err := service.registryService.UpdateRegistry(registry.ID, ®istry) + err := service.dataStore.Registry().UpdateRegistry(registry.ID, ®istry) if err != nil { return err } @@ -557,7 +536,7 @@ func (service *AuthorizationService) RemoveTeamAccessPolicies(teamID TeamID) err // RemoveUserAccessPolicies will remove all existing access policies associated to the specified user func (service *AuthorizationService) RemoveUserAccessPolicies(userID UserID) error { - endpoints, err := service.endpointService.Endpoints() + endpoints, err := service.dataStore.Endpoint().Endpoints() if err != nil { return err } @@ -567,7 +546,7 @@ func (service *AuthorizationService) RemoveUserAccessPolicies(userID UserID) err if policyUserID == userID { delete(endpoint.UserAccessPolicies, policyUserID) - err := service.endpointService.UpdateEndpoint(endpoint.ID, &endpoint) + err := service.dataStore.Endpoint().UpdateEndpoint(endpoint.ID, &endpoint) if err != nil { return err } @@ -577,7 +556,7 @@ func (service *AuthorizationService) RemoveUserAccessPolicies(userID UserID) err } } - endpointGroups, err := service.endpointGroupService.EndpointGroups() + endpointGroups, err := service.dataStore.EndpointGroup().EndpointGroups() if err != nil { return err } @@ -587,7 +566,7 @@ func (service *AuthorizationService) RemoveUserAccessPolicies(userID UserID) err if policyUserID == userID { delete(endpointGroup.UserAccessPolicies, policyUserID) - err := service.endpointGroupService.UpdateEndpointGroup(endpointGroup.ID, &endpointGroup) + err := service.dataStore.EndpointGroup().UpdateEndpointGroup(endpointGroup.ID, &endpointGroup) if err != nil { return err } @@ -597,7 +576,7 @@ func (service *AuthorizationService) RemoveUserAccessPolicies(userID UserID) err } } - registries, err := service.registryService.Registries() + registries, err := service.dataStore.Registry().Registries() if err != nil { return err } @@ -607,7 +586,7 @@ func (service *AuthorizationService) RemoveUserAccessPolicies(userID UserID) err if policyUserID == userID { delete(registry.UserAccessPolicies, policyUserID) - err := service.registryService.UpdateRegistry(registry.ID, ®istry) + err := service.dataStore.Registry().UpdateRegistry(registry.ID, ®istry) if err != nil { return err } @@ -622,7 +601,7 @@ func (service *AuthorizationService) RemoveUserAccessPolicies(userID UserID) err // UpdateUsersAuthorizations will trigger an update of the authorizations for all the users. func (service *AuthorizationService) UpdateUsersAuthorizations() error { - users, err := service.userService.Users() + users, err := service.dataStore.User().Users() if err != nil { return err } @@ -638,7 +617,7 @@ func (service *AuthorizationService) UpdateUsersAuthorizations() error { } func (service *AuthorizationService) updateUserAuthorizations(userID UserID) error { - user, err := service.userService.User(userID) + user, err := service.dataStore.User().User(userID) if err != nil { return err } @@ -650,7 +629,7 @@ func (service *AuthorizationService) updateUserAuthorizations(userID UserID) err user.EndpointAuthorizations = endpointAuthorizations - return service.userService.UpdateUser(userID, user) + return service.dataStore.User().UpdateUser(userID, user) } func (service *AuthorizationService) getAuthorizations(user *User) (EndpointAuthorizations, error) { @@ -659,22 +638,22 @@ func (service *AuthorizationService) getAuthorizations(user *User) (EndpointAuth return endpointAuthorizations, nil } - userMemberships, err := service.teamMembershipService.TeamMembershipsByUserID(user.ID) + userMemberships, err := service.dataStore.TeamMembership().TeamMembershipsByUserID(user.ID) if err != nil { return endpointAuthorizations, err } - endpoints, err := service.endpointService.Endpoints() + endpoints, err := service.dataStore.Endpoint().Endpoints() if err != nil { return endpointAuthorizations, err } - endpointGroups, err := service.endpointGroupService.EndpointGroups() + endpointGroups, err := service.dataStore.EndpointGroup().EndpointGroups() if err != nil { return endpointAuthorizations, err } - roles, err := service.roleService.Roles() + roles, err := service.dataStore.Role().Roles() if err != nil { return endpointAuthorizations, err } diff --git a/api/bolt/datastore.go b/api/bolt/datastore.go index c4b51ba31..476082604 100644 --- a/api/bolt/datastore.go +++ b/api/bolt/datastore.go @@ -41,7 +41,6 @@ type Store struct { db *bolt.DB isNew bool fileService portainer.FileService - RoleService *role.Service DockerHubService *dockerhub.Service EdgeGroupService *edgegroup.Service EdgeStackService *edgestack.Service @@ -51,6 +50,8 @@ type Store struct { ExtensionService *extension.Service RegistryService *registry.Service ResourceControlService *resourcecontrol.Service + RoleService *role.Service + ScheduleService *schedule.Service SettingsService *settings.Service StackService *stack.Service TagService *tag.Service @@ -60,7 +61,6 @@ type Store struct { UserService *user.Service VersionService *version.Service WebhookService *webhook.Service - ScheduleService *schedule.Service } // NewStore initializes a new Store and the associated services @@ -143,6 +143,7 @@ func (store *Store) MigrateData() error { UserService: store.UserService, VersionService: store.VersionService, FileService: store.fileService, + AuthorizationService: portainer.NewAuthorizationService(store), } migrator := migrator.NewMigrator(migratorParams) @@ -280,3 +281,103 @@ func (store *Store) initServices() error { return nil } + +// DockerHub gives access to the DockerHub data management layer +func (store *Store) DockerHub() portainer.DockerHubService { + return store.DockerHubService +} + +// EdgeGroup gives access to the EdgeGroup data management layer +func (store *Store) EdgeGroup() portainer.EdgeGroupService { + return store.EdgeGroupService +} + +// EdgeStack gives access to the EdgeStack data management layer +func (store *Store) EdgeStack() portainer.EdgeStackService { + return store.EdgeStackService +} + +// Endpoint gives access to the Endpoint data management layer +func (store *Store) Endpoint() portainer.EndpointService { + return store.EndpointService +} + +// EndpointGroup gives access to the EndpointGroup data management layer +func (store *Store) EndpointGroup() portainer.EndpointGroupService { + return store.EndpointGroupService +} + +// EndpointRelation gives access to the EndpointRelation data management layer +func (store *Store) EndpointRelation() portainer.EndpointRelationService { + return store.EndpointRelationService +} + +// Extension gives access to the Extension data management layer +func (store *Store) Extension() portainer.ExtensionService { + return store.ExtensionService +} + +// Registry gives access to the Registry data management layer +func (store *Store) Registry() portainer.RegistryService { + return store.RegistryService +} + +// ResourceControl gives access to the ResourceControl data management layer +func (store *Store) ResourceControl() portainer.ResourceControlService { + return store.ResourceControlService +} + +// Role gives access to the Role data management layer +func (store *Store) Role() portainer.RoleService { + return store.RoleService +} + +// Schedule gives access to the Schedule data management layer +func (store *Store) Schedule() portainer.ScheduleService { + return store.ScheduleService +} + +// Settings gives access to the Settings data management layer +func (store *Store) Settings() portainer.SettingsService { + return store.SettingsService +} + +// Stack gives access to the Stack data management layer +func (store *Store) Stack() portainer.StackService { + return store.StackService +} + +// Tag gives access to the Tag data management layer +func (store *Store) Tag() portainer.TagService { + return store.TagService +} + +// TeamMembership gives access to the TeamMembership data management layer +func (store *Store) TeamMembership() portainer.TeamMembershipService { + return store.TeamMembershipService +} + +// Team gives access to the Team data management layer +func (store *Store) Team() portainer.TeamService { + return store.TeamService +} + +// TunnelServer gives access to the TunnelServer data management layer +func (store *Store) TunnelServer() portainer.TunnelServerService { + return store.TunnelServerService +} + +// User gives access to the User data management layer +func (store *Store) User() portainer.UserService { + return store.UserService +} + +// Version gives access to the Version data management layer +func (store *Store) Version() portainer.VersionService { + return store.VersionService +} + +// Webhook gives access to the Webhook data management layer +func (store *Store) Webhook() portainer.WebhookService { + return store.WebhookService +} diff --git a/api/bolt/migrator/migrate_dbversion19.go b/api/bolt/migrator/migrate_dbversion19.go index 0692db5af..00a41a4e4 100644 --- a/api/bolt/migrator/migrate_dbversion19.go +++ b/api/bolt/migrator/migrate_dbversion19.go @@ -7,17 +7,7 @@ import ( ) func (m *Migrator) updateUsersToDBVersion20() error { - authorizationServiceParameters := &portainer.AuthorizationServiceParameters{ - EndpointService: m.endpointService, - EndpointGroupService: m.endpointGroupService, - RegistryService: m.registryService, - RoleService: m.roleService, - TeamMembershipService: m.teamMembershipService, - UserService: m.userService, - } - - authorizationService := portainer.NewAuthorizationService(authorizationServiceParameters) - return authorizationService.UpdateUsersAuthorizations() + return m.authorizationService.UpdateUsersAuthorizations() } func (m *Migrator) updateSettingsToDBVersion20() error { diff --git a/api/bolt/migrator/migrate_dbversion20.go b/api/bolt/migrator/migrate_dbversion20.go index 1698f10c3..df272905a 100644 --- a/api/bolt/migrator/migrate_dbversion20.go +++ b/api/bolt/migrator/migrate_dbversion20.go @@ -74,16 +74,9 @@ func (m *Migrator) updateUsersAndRolesToDBVersion22() error { readOnlyUserRole.Authorizations = portainer.DefaultEndpointAuthorizationsForReadOnlyUserRole(settings.AllowVolumeBrowserForRegularUsers) err = m.roleService.UpdateRole(readOnlyUserRole.ID, readOnlyUserRole) - - authorizationServiceParameters := &portainer.AuthorizationServiceParameters{ - EndpointService: m.endpointService, - EndpointGroupService: m.endpointGroupService, - RegistryService: m.registryService, - RoleService: m.roleService, - TeamMembershipService: m.teamMembershipService, - UserService: m.userService, + if err != nil { + return err } - authorizationService := portainer.NewAuthorizationService(authorizationServiceParameters) - return authorizationService.UpdateUsersAuthorizations() + return m.authorizationService.UpdateUsersAuthorizations() } diff --git a/api/bolt/migrator/migrator.go b/api/bolt/migrator/migrator.go index 1f4fa7bb0..0f72997fd 100644 --- a/api/bolt/migrator/migrator.go +++ b/api/bolt/migrator/migrator.go @@ -39,6 +39,7 @@ type ( userService *user.Service versionService *version.Service fileService portainer.FileService + authorizationService *portainer.AuthorizationService } // Parameters represents the required parameters to create a new Migrator instance. @@ -60,6 +61,7 @@ type ( UserService *user.Service VersionService *version.Service FileService portainer.FileService + AuthorizationService *portainer.AuthorizationService } ) @@ -83,12 +85,12 @@ func NewMigrator(parameters *Parameters) *Migrator { userService: parameters.UserService, versionService: parameters.VersionService, fileService: parameters.FileService, + authorizationService: parameters.AuthorizationService, } } // Migrate checks the database version and migrate the existing data to the most recent data model. func (m *Migrator) Migrate() error { - // Portainer < 1.12 if m.currentDBVersion < 1 { err := m.updateAdminUserToDBVersion1() diff --git a/api/chisel/service.go b/api/chisel/service.go index efb08db71..e007a416a 100644 --- a/api/chisel/service.go +++ b/api/chisel/service.go @@ -24,21 +24,19 @@ const ( // It is used to start a reverse tunnel server and to manage the connection status of each tunnel // connected to the tunnel server. type Service struct { - serverFingerprint string - serverPort string - tunnelDetailsMap cmap.ConcurrentMap - endpointService portainer.EndpointService - tunnelServerService portainer.TunnelServerService - snapshotter portainer.Snapshotter - chiselServer *chserver.Server + serverFingerprint string + serverPort string + tunnelDetailsMap cmap.ConcurrentMap + dataStore portainer.DataStore + snapshotter portainer.Snapshotter + chiselServer *chserver.Server } // NewService returns a pointer to a new instance of Service -func NewService(endpointService portainer.EndpointService, tunnelServerService portainer.TunnelServerService) *Service { +func NewService(dataStore portainer.DataStore) *Service { return &Service{ - tunnelDetailsMap: cmap.New(), - endpointService: endpointService, - tunnelServerService: tunnelServerService, + tunnelDetailsMap: cmap.New(), + dataStore: dataStore, } } @@ -89,7 +87,7 @@ func (service *Service) StartTunnelServer(addr, port string, snapshotter portain func (service *Service) retrievePrivateKeySeed() (string, error) { var serverInfo *portainer.TunnelServerInfo - serverInfo, err := service.tunnelServerService.Info() + serverInfo, err := service.dataStore.TunnelServer().Info() if err == portainer.ErrObjectNotFound { keySeed := uniuri.NewLen(16) @@ -97,7 +95,7 @@ func (service *Service) retrievePrivateKeySeed() (string, error) { PrivateKeySeed: keySeed, } - err := service.tunnelServerService.UpdateInfo(serverInfo) + err := service.dataStore.TunnelServer().UpdateInfo(serverInfo) if err != nil { return "", err } @@ -173,7 +171,7 @@ func (service *Service) checkTunnels() { } func (service *Service) snapshotEnvironment(endpointID portainer.EndpointID, tunnelPort int) error { - endpoint, err := service.endpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := service.dataStore.Endpoint().Endpoint(endpointID) if err != nil { return err } @@ -187,5 +185,5 @@ func (service *Service) snapshotEnvironment(endpointID portainer.EndpointID, tun endpoint.Snapshots = []portainer.Snapshot{*snapshot} endpoint.URL = endpointURL - return service.endpointService.UpdateEndpoint(endpoint.ID, endpoint) + return service.dataStore.Endpoint().UpdateEndpoint(endpoint.ID, endpoint) } diff --git a/api/chisel/tunnel.go b/api/chisel/tunnel.go index e0cba1caf..ba9495419 100644 --- a/api/chisel/tunnel.go +++ b/api/chisel/tunnel.go @@ -97,7 +97,7 @@ func (service *Service) SetTunnelStatusToRequired(endpointID portainer.EndpointI tunnel := service.GetTunnelDetails(endpointID) if tunnel.Port == 0 { - endpoint, err := service.endpointService.Endpoint(endpointID) + endpoint, err := service.dataStore.Endpoint().Endpoint(endpointID) if err != nil { return err } diff --git a/api/cmd/portainer/main.go b/api/cmd/portainer/main.go index 829d6b0b7..34eed9cef 100644 --- a/api/cmd/portainer/main.go +++ b/api/cmd/portainer/main.go @@ -25,13 +25,13 @@ import ( ) func initCLI() *portainer.CLIFlags { - var cli portainer.CLIService = &cli.Service{} - flags, err := cli.ParseFlags(portainer.APIVersion) + var cliService portainer.CLIService = &cli.Service{} + flags, err := cliService.ParseFlags(portainer.APIVersion) if err != nil { log.Fatal(err) } - err = cli.ValidateFlags(flags) + err = cliService.ValidateFlags(flags) if err != nil { log.Fatal(err) } @@ -46,7 +46,7 @@ func initFileService(dataStorePath string) portainer.FileService { return fileService } -func initStore(dataStorePath string, fileService portainer.FileService) *bolt.Store { +func initDataStore(dataStorePath string, fileService portainer.FileService) portainer.DataStore { store, err := bolt.NewStore(dataStorePath, fileService) if err != nil { log.Fatal(err) @@ -116,13 +116,13 @@ func initJobScheduler() portainer.JobScheduler { return cron.NewJobScheduler() } -func loadSnapshotSystemSchedule(jobScheduler portainer.JobScheduler, snapshotter portainer.Snapshotter, scheduleService portainer.ScheduleService, endpointService portainer.EndpointService, settingsService portainer.SettingsService) error { - settings, err := settingsService.Settings() +func loadSnapshotSystemSchedule(jobScheduler portainer.JobScheduler, snapshotter portainer.Snapshotter, dataStore portainer.DataStore) error { + settings, err := dataStore.Settings().Settings() if err != nil { return err } - schedules, err := scheduleService.SchedulesByJobType(portainer.SnapshotJobType) + schedules, err := dataStore.Schedule().SchedulesByJobType(portainer.SnapshotJobType) if err != nil { return err } @@ -131,7 +131,7 @@ func loadSnapshotSystemSchedule(jobScheduler portainer.JobScheduler, snapshotter if len(schedules) == 0 { snapshotJob := &portainer.SnapshotJob{} snapshotSchedule = &portainer.Schedule{ - ID: portainer.ScheduleID(scheduleService.GetNextIdentifier()), + ID: portainer.ScheduleID(dataStore.Schedule().GetNextIdentifier()), Name: "system_snapshot", CronExpression: "@every " + settings.SnapshotInterval, Recurring: true, @@ -143,7 +143,7 @@ func loadSnapshotSystemSchedule(jobScheduler portainer.JobScheduler, snapshotter snapshotSchedule = &schedules[0] } - snapshotJobContext := cron.NewSnapshotJobContext(endpointService, snapshotter) + snapshotJobContext := cron.NewSnapshotJobContext(dataStore, snapshotter) snapshotJobRunner := cron.NewSnapshotJobRunner(snapshotSchedule, snapshotJobContext) err = jobScheduler.ScheduleJob(snapshotJobRunner) @@ -152,13 +152,13 @@ func loadSnapshotSystemSchedule(jobScheduler portainer.JobScheduler, snapshotter } if len(schedules) == 0 { - return scheduleService.CreateSchedule(snapshotSchedule) + return dataStore.Schedule().CreateSchedule(snapshotSchedule) } return nil } -func loadSchedulesFromDatabase(jobScheduler portainer.JobScheduler, jobService portainer.JobService, scheduleService portainer.ScheduleService, endpointService portainer.EndpointService, fileService portainer.FileService, reverseTunnelService portainer.ReverseTunnelService) error { - schedules, err := scheduleService.Schedules() +func loadSchedulesFromDatabase(jobScheduler portainer.JobScheduler, jobService portainer.JobService, dataStore portainer.DataStore, fileService portainer.FileService, reverseTunnelService portainer.ReverseTunnelService) error { + schedules, err := dataStore.Schedule().Schedules() if err != nil { return err } @@ -166,7 +166,7 @@ func loadSchedulesFromDatabase(jobScheduler portainer.JobScheduler, jobService p for _, schedule := range schedules { if schedule.JobType == portainer.ScriptExecutionJobType { - jobContext := cron.NewScriptExecutionJobContext(jobService, endpointService, fileService) + jobContext := cron.NewScriptExecutionJobContext(jobService, dataStore, fileService) jobRunner := cron.NewScriptExecutionJobRunner(&schedule, jobContext) err = jobScheduler.ScheduleJob(jobRunner) @@ -194,8 +194,8 @@ func initStatus(flags *portainer.CLIFlags) *portainer.Status { } } -func updateSettingsFromFlags(settingsService portainer.SettingsService, flags *portainer.CLIFlags) error { - settings, err := settingsService.Settings() +func updateSettingsFromFlags(dataStore portainer.DataStore, flags *portainer.CLIFlags) error { + settings, err := dataStore.Settings().Settings() if err != nil { return err } @@ -211,7 +211,7 @@ func updateSettingsFromFlags(settingsService portainer.SettingsService, flags *p settings.BlackListedLabels = *flags.Labels } - return settingsService.UpdateSettings(settings) + return dataStore.Settings().UpdateSettings(settings) } func loadAndParseKeyPair(fileService portainer.FileService, signatureService portainer.DigitalSignatureService) error { @@ -243,7 +243,7 @@ func initKeyPair(fileService portainer.FileService, signatureService portainer.D return generateAndStoreKeyPair(fileService, signatureService) } -func createTLSSecuredEndpoint(flags *portainer.CLIFlags, endpointService portainer.EndpointService, snapshotter portainer.Snapshotter) error { +func createTLSSecuredEndpoint(flags *portainer.CLIFlags, dataStore portainer.DataStore, snapshotter portainer.Snapshotter) error { tlsConfiguration := portainer.TLSConfiguration{ TLS: *flags.TLS, TLSSkipVerify: *flags.TLSSkipVerify, @@ -257,7 +257,7 @@ func createTLSSecuredEndpoint(flags *portainer.CLIFlags, endpointService portain tlsConfiguration.TLS = true } - endpointID := endpointService.GetNextIdentifier() + endpointID := dataStore.Endpoint().GetNextIdentifier() endpoint := &portainer.Endpoint{ ID: portainer.EndpointID(endpointID), Name: "primary", @@ -289,10 +289,10 @@ func createTLSSecuredEndpoint(flags *portainer.CLIFlags, endpointService portain } } - return snapshotAndPersistEndpoint(endpoint, endpointService, snapshotter) + return snapshotAndPersistEndpoint(endpoint, dataStore, snapshotter) } -func createUnsecuredEndpoint(endpointURL string, endpointService portainer.EndpointService, snapshotter portainer.Snapshotter) error { +func createUnsecuredEndpoint(endpointURL string, dataStore portainer.DataStore, snapshotter portainer.Snapshotter) error { if strings.HasPrefix(endpointURL, "tcp://") { _, err := client.ExecutePingOperation(endpointURL, nil) if err != nil { @@ -300,7 +300,7 @@ func createUnsecuredEndpoint(endpointURL string, endpointService portainer.Endpo } } - endpointID := endpointService.GetNextIdentifier() + endpointID := dataStore.Endpoint().GetNextIdentifier() endpoint := &portainer.Endpoint{ ID: portainer.EndpointID(endpointID), Name: "primary", @@ -316,10 +316,10 @@ func createUnsecuredEndpoint(endpointURL string, endpointService portainer.Endpo Snapshots: []portainer.Snapshot{}, } - return snapshotAndPersistEndpoint(endpoint, endpointService, snapshotter) + return snapshotAndPersistEndpoint(endpoint, dataStore, snapshotter) } -func snapshotAndPersistEndpoint(endpoint *portainer.Endpoint, endpointService portainer.EndpointService, snapshotter portainer.Snapshotter) error { +func snapshotAndPersistEndpoint(endpoint *portainer.Endpoint, dataStore portainer.DataStore, snapshotter portainer.Snapshotter) error { snapshot, err := snapshotter.CreateSnapshot(endpoint) endpoint.Status = portainer.EndpointStatusUp if err != nil { @@ -330,15 +330,15 @@ func snapshotAndPersistEndpoint(endpoint *portainer.Endpoint, endpointService po endpoint.Snapshots = []portainer.Snapshot{*snapshot} } - return endpointService.CreateEndpoint(endpoint) + return dataStore.Endpoint().CreateEndpoint(endpoint) } -func initEndpoint(flags *portainer.CLIFlags, endpointService portainer.EndpointService, snapshotter portainer.Snapshotter) error { +func initEndpoint(flags *portainer.CLIFlags, dataStore portainer.DataStore, snapshotter portainer.Snapshotter) error { if *flags.EndpointURL == "" { return nil } - endpoints, err := endpointService.Endpoints() + endpoints, err := dataStore.Endpoint().Endpoints() if err != nil { return err } @@ -349,17 +349,17 @@ func initEndpoint(flags *portainer.CLIFlags, endpointService portainer.EndpointS } if *flags.TLS || *flags.TLSSkipVerify { - return createTLSSecuredEndpoint(flags, endpointService, snapshotter) + return createTLSSecuredEndpoint(flags, dataStore, snapshotter) } - return createUnsecuredEndpoint(*flags.EndpointURL, endpointService, snapshotter) + return createUnsecuredEndpoint(*flags.EndpointURL, dataStore, snapshotter) } func initJobService(dockerClientFactory *docker.ClientFactory) portainer.JobService { return docker.NewJobService(dockerClientFactory) } -func initExtensionManager(fileService portainer.FileService, extensionService portainer.ExtensionService) (portainer.ExtensionManager, error) { - extensionManager := exec.NewExtensionManager(fileService, extensionService) +func initExtensionManager(fileService portainer.FileService, dataStore portainer.DataStore) (portainer.ExtensionManager, error) { + extensionManager := exec.NewExtensionManager(fileService, dataStore) err := extensionManager.StartExtensions() if err != nil { @@ -369,11 +369,11 @@ func initExtensionManager(fileService portainer.FileService, extensionService po return extensionManager, nil } -func terminateIfNoAdminCreated(userService portainer.UserService) { +func terminateIfNoAdminCreated(dataStore portainer.DataStore) { timer1 := time.NewTimer(5 * time.Minute) <-timer1.C - users, err := userService.UsersByRole(portainer.AdministratorRole) + users, err := dataStore.User().UsersByRole(portainer.AdministratorRole) if err != nil { log.Fatal(err) } @@ -389,8 +389,8 @@ func main() { fileService := initFileService(*flags.Data) - store := initStore(*flags.Data, fileService) - defer store.Close() + dataStore := initDataStore(*flags.Data, fileService) + defer dataStore.Close() jwtService := initJWTService(!*flags.NoAuth) @@ -407,12 +407,12 @@ func main() { log.Fatal(err) } - extensionManager, err := initExtensionManager(fileService, store.ExtensionService) + extensionManager, err := initExtensionManager(fileService, dataStore) if err != nil { log.Fatal(err) } - reverseTunnelService := chisel.NewService(store.EndpointService, store.TunnelServerService) + reverseTunnelService := chisel.NewService(dataStore) clientFactory := initClientFactory(digitalSignatureService, reverseTunnelService) @@ -427,8 +427,8 @@ func main() { composeStackManager := initComposeStackManager(*flags.Data, reverseTunnelService) - if store.IsNew() { - err = updateSettingsFromFlags(store.SettingsService, flags) + if dataStore.IsNew() { + err = updateSettingsFromFlags(dataStore, flags) if err != nil { log.Fatal(err) } @@ -436,12 +436,12 @@ func main() { jobScheduler := initJobScheduler() - err = loadSchedulesFromDatabase(jobScheduler, jobService, store.ScheduleService, store.EndpointService, fileService, reverseTunnelService) + err = loadSchedulesFromDatabase(jobScheduler, jobService, dataStore, fileService, reverseTunnelService) if err != nil { log.Fatal(err) } - err = loadSnapshotSystemSchedule(jobScheduler, snapshotter, store.ScheduleService, store.EndpointService, store.SettingsService) + err = loadSnapshotSystemSchedule(jobScheduler, snapshotter, dataStore) if err != nil { log.Fatal(err) } @@ -450,7 +450,7 @@ func main() { applicationStatus := initStatus(flags) - err = initEndpoint(flags, store.EndpointService, snapshotter) + err = initEndpoint(flags, dataStore, snapshotter) if err != nil { log.Fatal(err) } @@ -470,7 +470,7 @@ func main() { } if adminPasswordHash != "" { - users, err := store.UserService.UsersByRole(portainer.AdministratorRole) + users, err := dataStore.User().UsersByRole(portainer.AdministratorRole) if err != nil { log.Fatal(err) } @@ -483,7 +483,7 @@ func main() { Password: adminPasswordHash, PortainerAuthorizations: portainer.DefaultPortainerAuthorizations(), } - err := store.UserService.CreateUser(user) + err := dataStore.User().CreateUser(user) if err != nil { log.Fatal(err) } @@ -493,7 +493,7 @@ func main() { } if !*flags.NoAuth { - go terminateIfNoAdminCreated(store.UserService) + go terminateIfNoAdminCreated(dataStore) } err = reverseTunnelService.StartTunnelServer(*flags.TunnelAddr, *flags.TunnelPort, snapshotter) @@ -502,45 +502,28 @@ func main() { } var server portainer.Server = &http.Server{ - ReverseTunnelService: reverseTunnelService, - Status: applicationStatus, - BindAddress: *flags.Addr, - AssetsPath: *flags.Assets, - AuthDisabled: *flags.NoAuth, - RoleService: store.RoleService, - UserService: store.UserService, - TeamService: store.TeamService, - TeamMembershipService: store.TeamMembershipService, - EdgeGroupService: store.EdgeGroupService, - EdgeStackService: store.EdgeStackService, - EndpointService: store.EndpointService, - EndpointGroupService: store.EndpointGroupService, - EndpointRelationService: store.EndpointRelationService, - ExtensionService: store.ExtensionService, - ResourceControlService: store.ResourceControlService, - SettingsService: store.SettingsService, - RegistryService: store.RegistryService, - DockerHubService: store.DockerHubService, - StackService: store.StackService, - ScheduleService: store.ScheduleService, - TagService: store.TagService, - WebhookService: store.WebhookService, - SwarmStackManager: swarmStackManager, - ComposeStackManager: composeStackManager, - ExtensionManager: extensionManager, - CryptoService: cryptoService, - JWTService: jwtService, - FileService: fileService, - LDAPService: ldapService, - GitService: gitService, - SignatureService: digitalSignatureService, - JobScheduler: jobScheduler, - Snapshotter: snapshotter, - SSL: *flags.SSL, - SSLCert: *flags.SSLCert, - SSLKey: *flags.SSLKey, - DockerClientFactory: clientFactory, - JobService: jobService, + ReverseTunnelService: reverseTunnelService, + Status: applicationStatus, + BindAddress: *flags.Addr, + AssetsPath: *flags.Assets, + AuthDisabled: *flags.NoAuth, + DataStore: dataStore, + SwarmStackManager: swarmStackManager, + ComposeStackManager: composeStackManager, + ExtensionManager: extensionManager, + CryptoService: cryptoService, + JWTService: jwtService, + FileService: fileService, + LDAPService: ldapService, + GitService: gitService, + SignatureService: digitalSignatureService, + JobScheduler: jobScheduler, + Snapshotter: snapshotter, + SSL: *flags.SSL, + SSLCert: *flags.SSLCert, + SSLKey: *flags.SSLKey, + DockerClientFactory: clientFactory, + JobService: jobService, } log.Printf("Starting Portainer %s on %s", portainer.APIVersion, *flags.Addr) diff --git a/api/cron/job_script_execution.go b/api/cron/job_script_execution.go index f9634813f..b554e6047 100644 --- a/api/cron/job_script_execution.go +++ b/api/cron/job_script_execution.go @@ -16,17 +16,17 @@ type ScriptExecutionJobRunner struct { // ScriptExecutionJobContext represents the context of execution of a ScriptExecutionJob type ScriptExecutionJobContext struct { - jobService portainer.JobService - endpointService portainer.EndpointService - fileService portainer.FileService + dataStore portainer.DataStore + jobService portainer.JobService + fileService portainer.FileService } // NewScriptExecutionJobContext returns a new context that can be used to execute a ScriptExecutionJob -func NewScriptExecutionJobContext(jobService portainer.JobService, endpointService portainer.EndpointService, fileService portainer.FileService) *ScriptExecutionJobContext { +func NewScriptExecutionJobContext(jobService portainer.JobService, dataStore portainer.DataStore, fileService portainer.FileService) *ScriptExecutionJobContext { return &ScriptExecutionJobContext{ - jobService: jobService, - endpointService: endpointService, - fileService: fileService, + jobService: jobService, + dataStore: dataStore, + fileService: fileService, } } @@ -56,7 +56,7 @@ func (runner *ScriptExecutionJobRunner) Run() { targets := make([]*portainer.Endpoint, 0) for _, endpointID := range runner.schedule.ScriptExecutionJob.Endpoints { - endpoint, err := runner.context.endpointService.Endpoint(endpointID) + endpoint, err := runner.context.dataStore.Endpoint().Endpoint(endpointID) if err != nil { log.Printf("scheduled job error (script execution). Unable to retrieve information about endpoint (id=%d) (err=%s)\n", endpointID, err) return diff --git a/api/cron/job_snapshot.go b/api/cron/job_snapshot.go index b3dcc9b74..e9c6e602b 100644 --- a/api/cron/job_snapshot.go +++ b/api/cron/job_snapshot.go @@ -14,15 +14,15 @@ type SnapshotJobRunner struct { // SnapshotJobContext represents the context of execution of a SnapshotJob type SnapshotJobContext struct { - endpointService portainer.EndpointService - snapshotter portainer.Snapshotter + dataStore portainer.DataStore + snapshotter portainer.Snapshotter } // NewSnapshotJobContext returns a new context that can be used to execute a SnapshotJob -func NewSnapshotJobContext(endpointService portainer.EndpointService, snapshotter portainer.Snapshotter) *SnapshotJobContext { +func NewSnapshotJobContext(dataStore portainer.DataStore, snapshotter portainer.Snapshotter) *SnapshotJobContext { return &SnapshotJobContext{ - endpointService: endpointService, - snapshotter: snapshotter, + dataStore: dataStore, + snapshotter: snapshotter, } } @@ -46,7 +46,7 @@ func (runner *SnapshotJobRunner) GetSchedule() *portainer.Schedule { // retrieve the latest version of the endpoint right after a snapshot. func (runner *SnapshotJobRunner) Run() { go func() { - endpoints, err := runner.context.endpointService.Endpoints() + endpoints, err := runner.context.dataStore.Endpoint().Endpoints() if err != nil { log.Printf("background schedule error (endpoint snapshot). Unable to retrieve endpoint list (err=%s)\n", err) return @@ -59,7 +59,7 @@ func (runner *SnapshotJobRunner) Run() { snapshot, snapshotError := runner.context.snapshotter.CreateSnapshot(&endpoint) - latestEndpointReference, err := runner.context.endpointService.Endpoint(endpoint.ID) + latestEndpointReference, err := runner.context.dataStore.Endpoint().Endpoint(endpoint.ID) if latestEndpointReference == nil { log.Printf("background schedule error (endpoint snapshot). Endpoint not found inside the database anymore (endpoint=%s, URL=%s) (err=%s)\n", endpoint.Name, endpoint.URL, err) continue @@ -75,7 +75,7 @@ func (runner *SnapshotJobRunner) Run() { latestEndpointReference.Snapshots = []portainer.Snapshot{*snapshot} } - err = runner.context.endpointService.UpdateEndpoint(latestEndpointReference.ID, latestEndpointReference) + err = runner.context.dataStore.Endpoint().UpdateEndpoint(latestEndpointReference.ID, latestEndpointReference) if err != nil { log.Printf("background schedule error (endpoint snapshot). Unable to update endpoint (endpoint=%s, URL=%s) (err=%s)\n", endpoint.Name, endpoint.URL, err) return diff --git a/api/exec/extension.go b/api/exec/extension.go index e41cf1f49..d70cd98bb 100644 --- a/api/exec/extension.go +++ b/api/exec/extension.go @@ -34,17 +34,17 @@ var extensionBinaryMap = map[portainer.ExtensionID]string{ // ExtensionManager represents a service used to // manage extension processes. type ExtensionManager struct { - processes cmap.ConcurrentMap - fileService portainer.FileService - extensionService portainer.ExtensionService + processes cmap.ConcurrentMap + fileService portainer.FileService + dataStore portainer.DataStore } // NewExtensionManager returns a pointer to an ExtensionManager -func NewExtensionManager(fileService portainer.FileService, extensionService portainer.ExtensionService) *ExtensionManager { +func NewExtensionManager(fileService portainer.FileService, dataStore portainer.DataStore) *ExtensionManager { return &ExtensionManager{ - processes: cmap.New(), - fileService: fileService, - extensionService: extensionService, + processes: cmap.New(), + fileService: fileService, + dataStore: dataStore, } } @@ -188,7 +188,7 @@ func (manager *ExtensionManager) DisableExtension(extension *portainer.Extension // The purpose of this function is to be ran at startup, as such most of the error handling won't block the program execution // and will log warning messages instead. func (manager *ExtensionManager) StartExtensions() error { - extensions, err := manager.extensionService.Extensions() + extensions, err := manager.dataStore.Extension().Extensions() if err != nil { return err } @@ -224,7 +224,7 @@ func (manager *ExtensionManager) updateAndStartExtensions(extensions []portainer } } - err := manager.extensionService.Persist(&extension) + err := manager.dataStore.Extension().Persist(&extension) if err != nil { return err } diff --git a/api/http/handler/auth/authenticate.go b/api/http/handler/auth/authenticate.go index 8e51f65db..1db970bb3 100644 --- a/api/http/handler/auth/authenticate.go +++ b/api/http/handler/auth/authenticate.go @@ -42,12 +42,12 @@ func (handler *Handler) authenticate(w http.ResponseWriter, r *http.Request) *ht return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err} } - u, err := handler.UserService.UserByUsername(payload.Username) + u, err := handler.DataStore.User().UserByUsername(payload.Username) if err != nil && err != portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve a user with the specified username from the database", err} } @@ -108,7 +108,7 @@ func (handler *Handler) authenticateLDAPAndCreateUser(w http.ResponseWriter, use PortainerAuthorizations: portainer.DefaultPortainerAuthorizations(), } - err = handler.UserService.CreateUser(user) + err = handler.DataStore.User().CreateUser(user) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist user inside the database", err} } @@ -146,7 +146,7 @@ func (handler *Handler) persistAndWriteToken(w http.ResponseWriter, tokenData *p } func (handler *Handler) addUserIntoTeams(user *portainer.User, settings *portainer.LDAPSettings) error { - teams, err := handler.TeamService.Teams() + teams, err := handler.DataStore.Team().Teams() if err != nil { return err } @@ -156,7 +156,7 @@ func (handler *Handler) addUserIntoTeams(user *portainer.User, settings *portain return err } - userMemberships, err := handler.TeamMembershipService.TeamMembershipsByUserID(user.ID) + userMemberships, err := handler.DataStore.TeamMembership().TeamMembershipsByUserID(user.ID) if err != nil { return err } @@ -174,7 +174,7 @@ func (handler *Handler) addUserIntoTeams(user *portainer.User, settings *portain Role: portainer.TeamMember, } - err := handler.TeamMembershipService.CreateTeamMembership(membership) + err := handler.DataStore.TeamMembership().CreateTeamMembership(membership) if err != nil { return err } diff --git a/api/http/handler/auth/authenticate_oauth.go b/api/http/handler/auth/authenticate_oauth.go index de0cbd8dc..757f3fd76 100644 --- a/api/http/handler/auth/authenticate_oauth.go +++ b/api/http/handler/auth/authenticate_oauth.go @@ -78,7 +78,7 @@ func (handler *Handler) validateOAuth(w http.ResponseWriter, r *http.Request) *h return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err} } @@ -87,7 +87,7 @@ func (handler *Handler) validateOAuth(w http.ResponseWriter, r *http.Request) *h return &httperror.HandlerError{http.StatusForbidden, "OAuth authentication is not enabled", portainer.Error("OAuth authentication is not enabled")} } - extension, err := handler.ExtensionService.Extension(portainer.OAuthAuthenticationExtension) + extension, err := handler.DataStore.Extension().Extension(portainer.OAuthAuthenticationExtension) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Oauth authentication extension is not enabled", err} } else if err != nil { @@ -100,7 +100,7 @@ func (handler *Handler) validateOAuth(w http.ResponseWriter, r *http.Request) *h return &httperror.HandlerError{http.StatusInternalServerError, "Unable to authenticate through OAuth", portainer.ErrUnauthorized} } - user, err := handler.UserService.UserByUsername(username) + user, err := handler.DataStore.User().UserByUsername(username) if err != nil && err != portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve a user with the specified username from the database", err} } @@ -116,7 +116,7 @@ func (handler *Handler) validateOAuth(w http.ResponseWriter, r *http.Request) *h PortainerAuthorizations: portainer.DefaultPortainerAuthorizations(), } - err = handler.UserService.CreateUser(user) + err = handler.DataStore.User().CreateUser(user) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist user inside the database", err} } @@ -128,7 +128,7 @@ func (handler *Handler) validateOAuth(w http.ResponseWriter, r *http.Request) *h Role: portainer.TeamMember, } - err = handler.TeamMembershipService.CreateTeamMembership(membership) + err = handler.DataStore.TeamMembership().CreateTeamMembership(membership) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist team membership inside the database", err} } diff --git a/api/http/handler/auth/handler.go b/api/http/handler/auth/handler.go index 24a211f94..345884980 100644 --- a/api/http/handler/auth/handler.go +++ b/api/http/handler/auth/handler.go @@ -11,8 +11,6 @@ import ( ) const ( - // ErrInvalidCredentials is an error raised when credentials for a user are invalid - ErrInvalidCredentials = portainer.Error("Invalid credentials") // ErrAuthDisabled is an error raised when trying to access the authentication endpoints // when the server has been started with the --no-auth flag ErrAuthDisabled = portainer.Error("Authentication is disabled") @@ -21,20 +19,13 @@ const ( // Handler is the HTTP handler used to handle authentication operations. type Handler struct { *mux.Router - authDisabled bool - UserService portainer.UserService - CryptoService portainer.CryptoService - JWTService portainer.JWTService - LDAPService portainer.LDAPService - SettingsService portainer.SettingsService - TeamService portainer.TeamService - TeamMembershipService portainer.TeamMembershipService - ExtensionService portainer.ExtensionService - EndpointService portainer.EndpointService - EndpointGroupService portainer.EndpointGroupService - RoleService portainer.RoleService - ProxyManager *proxy.Manager - AuthorizationService *portainer.AuthorizationService + authDisabled bool + DataStore portainer.DataStore + CryptoService portainer.CryptoService + JWTService portainer.JWTService + LDAPService portainer.LDAPService + ProxyManager *proxy.Manager + AuthorizationService *portainer.AuthorizationService } // NewHandler creates a handler to manage authentication operations. diff --git a/api/http/handler/dockerhub/dockerhub_inspect.go b/api/http/handler/dockerhub/dockerhub_inspect.go index b149a2a35..fd4713849 100644 --- a/api/http/handler/dockerhub/dockerhub_inspect.go +++ b/api/http/handler/dockerhub/dockerhub_inspect.go @@ -9,7 +9,7 @@ import ( // GET request on /api/dockerhub func (handler *Handler) dockerhubInspect(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - dockerhub, err := handler.DockerHubService.DockerHub() + dockerhub, err := handler.DataStore.DockerHub().DockerHub() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve DockerHub details from the database", err} } diff --git a/api/http/handler/dockerhub/dockerhub_update.go b/api/http/handler/dockerhub/dockerhub_update.go index 5606677fb..78787566e 100644 --- a/api/http/handler/dockerhub/dockerhub_update.go +++ b/api/http/handler/dockerhub/dockerhub_update.go @@ -43,7 +43,7 @@ func (handler *Handler) dockerhubUpdate(w http.ResponseWriter, r *http.Request) dockerhub.Password = payload.Password } - err = handler.DockerHubService.UpdateDockerHub(dockerhub) + err = handler.DataStore.DockerHub().UpdateDockerHub(dockerhub) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the Dockerhub changes inside the database", err} } diff --git a/api/http/handler/dockerhub/handler.go b/api/http/handler/dockerhub/handler.go index ba4ed2c34..f1328acb8 100644 --- a/api/http/handler/dockerhub/handler.go +++ b/api/http/handler/dockerhub/handler.go @@ -16,7 +16,7 @@ func hideFields(dockerHub *portainer.DockerHub) { // Handler is the HTTP handler used to handle DockerHub operations. type Handler struct { *mux.Router - DockerHubService portainer.DockerHubService + DataStore portainer.DataStore } // NewHandler creates a handler to manage Dockerhub operations. diff --git a/api/http/handler/edgegroups/associated_endpoints.go b/api/http/handler/edgegroups/associated_endpoints.go index 8ff2f7693..8eeed5620 100644 --- a/api/http/handler/edgegroups/associated_endpoints.go +++ b/api/http/handler/edgegroups/associated_endpoints.go @@ -11,7 +11,7 @@ func (handler *Handler) getEndpointsByTags(tagIDs []portainer.TagID, partialMatc return []portainer.EndpointID{}, nil } - endpoints, err := handler.EndpointService.Endpoints() + endpoints, err := handler.DataStore.Endpoint().Endpoints() if err != nil { return nil, err } @@ -20,7 +20,7 @@ func (handler *Handler) getEndpointsByTags(tagIDs []portainer.TagID, partialMatc tags := []portainer.Tag{} for _, tagID := range tagIDs { - tag, err := handler.TagService.Tag(tagID) + tag, err := handler.DataStore.Tag().Tag(tagID) if err != nil { return nil, err } diff --git a/api/http/handler/edgegroups/edgegroup_create.go b/api/http/handler/edgegroups/edgegroup_create.go index 26bbd0d90..fc0d09d37 100644 --- a/api/http/handler/edgegroups/edgegroup_create.go +++ b/api/http/handler/edgegroups/edgegroup_create.go @@ -38,7 +38,7 @@ func (handler *Handler) edgeGroupCreate(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - edgeGroups, err := handler.EdgeGroupService.EdgeGroups() + edgeGroups, err := handler.DataStore.EdgeGroup().EdgeGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve Edge groups from the database", err} } @@ -62,7 +62,7 @@ func (handler *Handler) edgeGroupCreate(w http.ResponseWriter, r *http.Request) } else { endpointIDs := []portainer.EndpointID{} for _, endpointID := range payload.Endpoints { - endpoint, err := handler.EndpointService.Endpoint(endpointID) + endpoint, err := handler.DataStore.Endpoint().Endpoint(endpointID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoint from the database", err} } @@ -74,7 +74,7 @@ func (handler *Handler) edgeGroupCreate(w http.ResponseWriter, r *http.Request) edgeGroup.Endpoints = endpointIDs } - err = handler.EdgeGroupService.CreateEdgeGroup(edgeGroup) + err = handler.DataStore.EdgeGroup().CreateEdgeGroup(edgeGroup) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the Edge group inside the database", err} } diff --git a/api/http/handler/edgegroups/edgegroup_delete.go b/api/http/handler/edgegroups/edgegroup_delete.go index 8ad9e949f..806888617 100644 --- a/api/http/handler/edgegroups/edgegroup_delete.go +++ b/api/http/handler/edgegroups/edgegroup_delete.go @@ -15,14 +15,14 @@ func (handler *Handler) edgeGroupDelete(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid Edge group identifier route variable", err} } - _, err = handler.EdgeGroupService.EdgeGroup(portainer.EdgeGroupID(edgeGroupID)) + _, err = handler.DataStore.EdgeGroup().EdgeGroup(portainer.EdgeGroupID(edgeGroupID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an Edge group with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an Edge group with the specified identifier inside the database", err} } - edgeStacks, err := handler.EdgeStackService.EdgeStacks() + edgeStacks, err := handler.DataStore.EdgeStack().EdgeStacks() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve Edge stacks from the database", err} } @@ -35,7 +35,7 @@ func (handler *Handler) edgeGroupDelete(w http.ResponseWriter, r *http.Request) } } - err = handler.EdgeGroupService.DeleteEdgeGroup(portainer.EdgeGroupID(edgeGroupID)) + err = handler.DataStore.EdgeGroup().DeleteEdgeGroup(portainer.EdgeGroupID(edgeGroupID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove the Edge group from the database", err} } diff --git a/api/http/handler/edgegroups/edgegroup_inspect.go b/api/http/handler/edgegroups/edgegroup_inspect.go index 5fdadf2ec..db88789c1 100644 --- a/api/http/handler/edgegroups/edgegroup_inspect.go +++ b/api/http/handler/edgegroups/edgegroup_inspect.go @@ -15,7 +15,7 @@ func (handler *Handler) edgeGroupInspect(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid Edge group identifier route variable", err} } - edgeGroup, err := handler.EdgeGroupService.EdgeGroup(portainer.EdgeGroupID(edgeGroupID)) + edgeGroup, err := handler.DataStore.EdgeGroup().EdgeGroup(portainer.EdgeGroupID(edgeGroupID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an Edge group with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/edgegroups/edgegroup_list.go b/api/http/handler/edgegroups/edgegroup_list.go index f859300aa..7ba9fbdcf 100644 --- a/api/http/handler/edgegroups/edgegroup_list.go +++ b/api/http/handler/edgegroups/edgegroup_list.go @@ -14,12 +14,12 @@ type decoratedEdgeGroup struct { } func (handler *Handler) edgeGroupList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - edgeGroups, err := handler.EdgeGroupService.EdgeGroups() + edgeGroups, err := handler.DataStore.EdgeGroup().EdgeGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve Edge groups from the database", err} } - edgeStacks, err := handler.EdgeStackService.EdgeStacks() + edgeStacks, err := handler.DataStore.EdgeStack().EdgeStacks() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve Edge stacks from the database", err} } diff --git a/api/http/handler/edgegroups/edgegroup_update.go b/api/http/handler/edgegroups/edgegroup_update.go index d6a72ea6d..6c227b11b 100644 --- a/api/http/handler/edgegroups/edgegroup_update.go +++ b/api/http/handler/edgegroups/edgegroup_update.go @@ -43,7 +43,7 @@ func (handler *Handler) edgeGroupUpdate(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - edgeGroup, err := handler.EdgeGroupService.EdgeGroup(portainer.EdgeGroupID(edgeGroupID)) + edgeGroup, err := handler.DataStore.EdgeGroup().EdgeGroup(portainer.EdgeGroupID(edgeGroupID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an Edge group with the specified identifier inside the database", err} } else if err != nil { @@ -51,7 +51,7 @@ func (handler *Handler) edgeGroupUpdate(w http.ResponseWriter, r *http.Request) } if payload.Name != "" { - edgeGroups, err := handler.EdgeGroupService.EdgeGroups() + edgeGroups, err := handler.DataStore.EdgeGroup().EdgeGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve Edge groups from the database", err} } @@ -63,12 +63,12 @@ func (handler *Handler) edgeGroupUpdate(w http.ResponseWriter, r *http.Request) edgeGroup.Name = payload.Name } - endpoints, err := handler.EndpointService.Endpoints() + endpoints, err := handler.DataStore.Endpoint().Endpoints() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoints from database", err} } - endpointGroups, err := handler.EndpointGroupService.EndpointGroups() + endpointGroups, err := handler.DataStore.EndpointGroup().EndpointGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoint groups from database", err} } @@ -81,7 +81,7 @@ func (handler *Handler) edgeGroupUpdate(w http.ResponseWriter, r *http.Request) } else { endpointIDs := []portainer.EndpointID{} for _, endpointID := range payload.Endpoints { - endpoint, err := handler.EndpointService.Endpoint(endpointID) + endpoint, err := handler.DataStore.Endpoint().Endpoint(endpointID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoint from the database", err} } @@ -97,7 +97,7 @@ func (handler *Handler) edgeGroupUpdate(w http.ResponseWriter, r *http.Request) edgeGroup.PartialMatch = *payload.PartialMatch } - err = handler.EdgeGroupService.UpdateEdgeGroup(edgeGroup.ID, edgeGroup) + err = handler.DataStore.EdgeGroup().UpdateEdgeGroup(edgeGroup.ID, edgeGroup) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist Edge group changes inside the database", err} } @@ -116,27 +116,27 @@ func (handler *Handler) edgeGroupUpdate(w http.ResponseWriter, r *http.Request) } func (handler *Handler) updateEndpoint(endpointID portainer.EndpointID) error { - relation, err := handler.EndpointRelationService.EndpointRelation(endpointID) + relation, err := handler.DataStore.EndpointRelation().EndpointRelation(endpointID) if err != nil { return err } - endpoint, err := handler.EndpointService.Endpoint(endpointID) + endpoint, err := handler.DataStore.Endpoint().Endpoint(endpointID) if err != nil { return err } - endpointGroup, err := handler.EndpointGroupService.EndpointGroup(endpoint.GroupID) + endpointGroup, err := handler.DataStore.EndpointGroup().EndpointGroup(endpoint.GroupID) if err != nil { return err } - edgeGroups, err := handler.EdgeGroupService.EdgeGroups() + edgeGroups, err := handler.DataStore.EdgeGroup().EdgeGroups() if err != nil { return err } - edgeStacks, err := handler.EdgeStackService.EdgeStacks() + edgeStacks, err := handler.DataStore.EdgeStack().EdgeStacks() if err != nil { return err } @@ -150,5 +150,5 @@ func (handler *Handler) updateEndpoint(endpointID portainer.EndpointID) error { relation.EdgeStacks = edgeStackSet - return handler.EndpointRelationService.UpdateEndpointRelation(endpoint.ID, relation) + return handler.DataStore.EndpointRelation().UpdateEndpointRelation(endpoint.ID, relation) } diff --git a/api/http/handler/edgegroups/handler.go b/api/http/handler/edgegroups/handler.go index 874b13477..5f8446f8a 100644 --- a/api/http/handler/edgegroups/handler.go +++ b/api/http/handler/edgegroups/handler.go @@ -12,12 +12,7 @@ import ( // Handler is the HTTP handler used to handle endpoint group operations. type Handler struct { *mux.Router - EdgeGroupService portainer.EdgeGroupService - EdgeStackService portainer.EdgeStackService - EndpointService portainer.EndpointService - EndpointGroupService portainer.EndpointGroupService - EndpointRelationService portainer.EndpointRelationService - TagService portainer.TagService + DataStore portainer.DataStore } // NewHandler creates a handler to manage endpoint group operations. diff --git a/api/http/handler/edgestacks/edgestack_create.go b/api/http/handler/edgestacks/edgestack_create.go index e3a315cb9..27f7ce87b 100644 --- a/api/http/handler/edgestacks/edgestack_create.go +++ b/api/http/handler/edgestacks/edgestack_create.go @@ -27,17 +27,17 @@ func (handler *Handler) edgeStackCreate(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusInternalServerError, "Unable to create Edge stack", err} } - endpoints, err := handler.EndpointService.Endpoints() + endpoints, err := handler.DataStore.Endpoint().Endpoints() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoints from database", err} } - endpointGroups, err := handler.EndpointGroupService.EndpointGroups() + endpointGroups, err := handler.DataStore.EndpointGroup().EndpointGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoint groups from database", err} } - edgeGroups, err := handler.EdgeGroupService.EdgeGroups() + edgeGroups, err := handler.DataStore.EdgeGroup().EdgeGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge groups from database", err} } @@ -45,14 +45,14 @@ func (handler *Handler) edgeStackCreate(w http.ResponseWriter, r *http.Request) relatedEndpoints, err := portainer.EdgeStackRelatedEndpoints(edgeStack.EdgeGroups, endpoints, endpointGroups, edgeGroups) for _, endpointID := range relatedEndpoints { - relation, err := handler.EndpointRelationService.EndpointRelation(endpointID) + relation, err := handler.DataStore.EndpointRelation().EndpointRelation(endpointID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find endpoint relation in database", err} } relation.EdgeStacks[edgeStack.ID] = true - err = handler.EndpointRelationService.UpdateEndpointRelation(endpointID, relation) + err = handler.DataStore.EndpointRelation().UpdateEndpointRelation(endpointID, relation) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint relation in database", err} } @@ -104,7 +104,7 @@ func (handler *Handler) createSwarmStackFromFileContent(r *http.Request) (*porta return nil, err } - stackID := handler.EdgeStackService.GetNextIdentifier() + stackID := handler.DataStore.EdgeStack().GetNextIdentifier() stack := &portainer.EdgeStack{ ID: portainer.EdgeStackID(stackID), Name: payload.Name, @@ -122,7 +122,7 @@ func (handler *Handler) createSwarmStackFromFileContent(r *http.Request) (*porta } stack.ProjectPath = projectPath - err = handler.EdgeStackService.CreateEdgeStack(stack) + err = handler.DataStore.EdgeStack().CreateEdgeStack(stack) if err != nil { return nil, err } @@ -172,7 +172,7 @@ func (handler *Handler) createSwarmStackFromGitRepository(r *http.Request) (*por return nil, err } - stackID := handler.EdgeStackService.GetNextIdentifier() + stackID := handler.DataStore.EdgeStack().GetNextIdentifier() stack := &portainer.EdgeStack{ ID: portainer.EdgeStackID(stackID), Name: payload.Name, @@ -200,7 +200,7 @@ func (handler *Handler) createSwarmStackFromGitRepository(r *http.Request) (*por return nil, err } - err = handler.EdgeStackService.CreateEdgeStack(stack) + err = handler.DataStore.EdgeStack().CreateEdgeStack(stack) if err != nil { return nil, err } @@ -248,7 +248,7 @@ func (handler *Handler) createSwarmStackFromFileUpload(r *http.Request) (*portai return nil, err } - stackID := handler.EdgeStackService.GetNextIdentifier() + stackID := handler.DataStore.EdgeStack().GetNextIdentifier() stack := &portainer.EdgeStack{ ID: portainer.EdgeStackID(stackID), Name: payload.Name, @@ -266,7 +266,7 @@ func (handler *Handler) createSwarmStackFromFileUpload(r *http.Request) (*portai } stack.ProjectPath = projectPath - err = handler.EdgeStackService.CreateEdgeStack(stack) + err = handler.DataStore.EdgeStack().CreateEdgeStack(stack) if err != nil { return nil, err } @@ -275,7 +275,7 @@ func (handler *Handler) createSwarmStackFromFileUpload(r *http.Request) (*portai } func (handler *Handler) validateUniqueName(name string) error { - edgeStacks, err := handler.EdgeStackService.EdgeStacks() + edgeStacks, err := handler.DataStore.EdgeStack().EdgeStacks() if err != nil { return err } diff --git a/api/http/handler/edgestacks/edgestack_delete.go b/api/http/handler/edgestacks/edgestack_delete.go index ae5d1b476..cec2a3c82 100644 --- a/api/http/handler/edgestacks/edgestack_delete.go +++ b/api/http/handler/edgestacks/edgestack_delete.go @@ -15,29 +15,29 @@ func (handler *Handler) edgeStackDelete(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid edge stack identifier route variable", err} } - edgeStack, err := handler.EdgeStackService.EdgeStack(portainer.EdgeStackID(edgeStackID)) + edgeStack, err := handler.DataStore.EdgeStack().EdgeStack(portainer.EdgeStackID(edgeStackID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an edge stack with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an edge stack with the specified identifier inside the database", err} } - err = handler.EdgeStackService.DeleteEdgeStack(portainer.EdgeStackID(edgeStackID)) + err = handler.DataStore.EdgeStack().DeleteEdgeStack(portainer.EdgeStackID(edgeStackID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove the edge stack from the database", err} } - endpoints, err := handler.EndpointService.Endpoints() + endpoints, err := handler.DataStore.Endpoint().Endpoints() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoints from database", err} } - endpointGroups, err := handler.EndpointGroupService.EndpointGroups() + endpointGroups, err := handler.DataStore.EndpointGroup().EndpointGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoint groups from database", err} } - edgeGroups, err := handler.EdgeGroupService.EdgeGroups() + edgeGroups, err := handler.DataStore.EdgeGroup().EdgeGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge groups from database", err} } @@ -45,14 +45,14 @@ func (handler *Handler) edgeStackDelete(w http.ResponseWriter, r *http.Request) relatedEndpoints, err := portainer.EdgeStackRelatedEndpoints(edgeStack.EdgeGroups, endpoints, endpointGroups, edgeGroups) for _, endpointID := range relatedEndpoints { - relation, err := handler.EndpointRelationService.EndpointRelation(endpointID) + relation, err := handler.DataStore.EndpointRelation().EndpointRelation(endpointID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find endpoint relation in database", err} } delete(relation.EdgeStacks, edgeStack.ID) - err = handler.EndpointRelationService.UpdateEndpointRelation(endpointID, relation) + err = handler.DataStore.EndpointRelation().UpdateEndpointRelation(endpointID, relation) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint relation in database", err} } diff --git a/api/http/handler/edgestacks/edgestack_file.go b/api/http/handler/edgestacks/edgestack_file.go index c82348b8d..bcb20f626 100644 --- a/api/http/handler/edgestacks/edgestack_file.go +++ b/api/http/handler/edgestacks/edgestack_file.go @@ -21,7 +21,7 @@ func (handler *Handler) edgeStackFile(w http.ResponseWriter, r *http.Request) *h return &httperror.HandlerError{http.StatusBadRequest, "Invalid edge stack identifier route variable", err} } - stack, err := handler.EdgeStackService.EdgeStack(portainer.EdgeStackID(stackID)) + stack, err := handler.DataStore.EdgeStack().EdgeStack(portainer.EdgeStackID(stackID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an edge stack with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/edgestacks/edgestack_inspect.go b/api/http/handler/edgestacks/edgestack_inspect.go index 66a591633..ae417e603 100644 --- a/api/http/handler/edgestacks/edgestack_inspect.go +++ b/api/http/handler/edgestacks/edgestack_inspect.go @@ -15,7 +15,7 @@ func (handler *Handler) edgeStackInspect(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid edge stack identifier route variable", err} } - edgeStack, err := handler.EdgeStackService.EdgeStack(portainer.EdgeStackID(edgeStackID)) + edgeStack, err := handler.DataStore.EdgeStack().EdgeStack(portainer.EdgeStackID(edgeStackID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an edge stack with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/edgestacks/edgestack_list.go b/api/http/handler/edgestacks/edgestack_list.go index dd15e58c1..1db0159c6 100644 --- a/api/http/handler/edgestacks/edgestack_list.go +++ b/api/http/handler/edgestacks/edgestack_list.go @@ -8,7 +8,7 @@ import ( ) func (handler *Handler) edgeStackList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - edgeStacks, err := handler.EdgeStackService.EdgeStacks() + edgeStacks, err := handler.DataStore.EdgeStack().EdgeStacks() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge stacks from the database", err} } diff --git a/api/http/handler/edgestacks/edgestack_status_update.go b/api/http/handler/edgestacks/edgestack_status_update.go index bcf0a639b..5d5c3ebda 100644 --- a/api/http/handler/edgestacks/edgestack_status_update.go +++ b/api/http/handler/edgestacks/edgestack_status_update.go @@ -35,7 +35,7 @@ func (handler *Handler) edgeStackStatusUpdate(w http.ResponseWriter, r *http.Req return &httperror.HandlerError{http.StatusBadRequest, "Invalid stack identifier route variable", err} } - stack, err := handler.EdgeStackService.EdgeStack(portainer.EdgeStackID(stackID)) + stack, err := handler.DataStore.EdgeStack().EdgeStack(portainer.EdgeStackID(stackID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a stack with the specified identifier inside the database", err} } else if err != nil { @@ -48,7 +48,7 @@ func (handler *Handler) edgeStackStatusUpdate(w http.ResponseWriter, r *http.Req return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(*payload.EndpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(*payload.EndpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -66,7 +66,7 @@ func (handler *Handler) edgeStackStatusUpdate(w http.ResponseWriter, r *http.Req EndpointID: *payload.EndpointID, } - err = handler.EdgeStackService.UpdateEdgeStack(stack.ID, stack) + err = handler.DataStore.EdgeStack().UpdateEdgeStack(stack.ID, stack) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the stack changes inside the database", err} } diff --git a/api/http/handler/edgestacks/edgestack_update.go b/api/http/handler/edgestacks/edgestack_update.go index 404ff1d92..3d7ac1ddc 100644 --- a/api/http/handler/edgestacks/edgestack_update.go +++ b/api/http/handler/edgestacks/edgestack_update.go @@ -34,7 +34,7 @@ func (handler *Handler) edgeStackUpdate(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid stack identifier route variable", err} } - stack, err := handler.EdgeStackService.EdgeStack(portainer.EdgeStackID(stackID)) + stack, err := handler.DataStore.EdgeStack().EdgeStack(portainer.EdgeStackID(stackID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a stack with the specified identifier inside the database", err} } else if err != nil { @@ -48,17 +48,17 @@ func (handler *Handler) edgeStackUpdate(w http.ResponseWriter, r *http.Request) } if payload.EdgeGroups != nil { - endpoints, err := handler.EndpointService.Endpoints() + endpoints, err := handler.DataStore.Endpoint().Endpoints() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoints from database", err} } - endpointGroups, err := handler.EndpointGroupService.EndpointGroups() + endpointGroups, err := handler.DataStore.EndpointGroup().EndpointGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoint groups from database", err} } - edgeGroups, err := handler.EdgeGroupService.EdgeGroups() + edgeGroups, err := handler.DataStore.EdgeGroup().EdgeGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge groups from database", err} } @@ -84,14 +84,14 @@ func (handler *Handler) edgeStackUpdate(w http.ResponseWriter, r *http.Request) } for endpointID := range endpointsToRemove { - relation, err := handler.EndpointRelationService.EndpointRelation(endpointID) + relation, err := handler.DataStore.EndpointRelation().EndpointRelation(endpointID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find endpoint relation in database", err} } delete(relation.EdgeStacks, stack.ID) - err = handler.EndpointRelationService.UpdateEndpointRelation(endpointID, relation) + err = handler.DataStore.EndpointRelation().UpdateEndpointRelation(endpointID, relation) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint relation in database", err} } @@ -105,14 +105,14 @@ func (handler *Handler) edgeStackUpdate(w http.ResponseWriter, r *http.Request) } for endpointID := range endpointsToAdd { - relation, err := handler.EndpointRelationService.EndpointRelation(endpointID) + relation, err := handler.DataStore.EndpointRelation().EndpointRelation(endpointID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find endpoint relation in database", err} } relation.EdgeStacks[stack.ID] = true - err = handler.EndpointRelationService.UpdateEndpointRelation(endpointID, relation) + err = handler.DataStore.EndpointRelation().UpdateEndpointRelation(endpointID, relation) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint relation in database", err} } @@ -137,7 +137,7 @@ func (handler *Handler) edgeStackUpdate(w http.ResponseWriter, r *http.Request) stack.Status = map[portainer.EndpointID]portainer.EdgeStackStatus{} } - err = handler.EdgeStackService.UpdateEdgeStack(stack.ID, stack) + err = handler.DataStore.EdgeStack().UpdateEdgeStack(stack.ID, stack) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the stack changes inside the database", err} } diff --git a/api/http/handler/edgestacks/handler.go b/api/http/handler/edgestacks/handler.go index 45c823e5c..3c75f837e 100644 --- a/api/http/handler/edgestacks/handler.go +++ b/api/http/handler/edgestacks/handler.go @@ -12,14 +12,10 @@ import ( // Handler is the HTTP handler used to handle endpoint group operations. type Handler struct { *mux.Router - requestBouncer *security.RequestBouncer - EdgeGroupService portainer.EdgeGroupService - EdgeStackService portainer.EdgeStackService - EndpointService portainer.EndpointService - EndpointGroupService portainer.EndpointGroupService - EndpointRelationService portainer.EndpointRelationService - FileService portainer.FileService - GitService portainer.GitService + requestBouncer *security.RequestBouncer + DataStore portainer.DataStore + FileService portainer.FileService + GitService portainer.GitService } // NewHandler creates a handler to manage endpoint group operations. diff --git a/api/http/handler/edgetemplates/edgetemplate_list.go b/api/http/handler/edgetemplates/edgetemplate_list.go index 2afb90f6d..00f271dbb 100644 --- a/api/http/handler/edgetemplates/edgetemplate_list.go +++ b/api/http/handler/edgetemplates/edgetemplate_list.go @@ -17,7 +17,7 @@ type templateFileFormat struct { // GET request on /api/edgetemplates func (handler *Handler) edgeTemplateList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err} } diff --git a/api/http/handler/edgetemplates/handler.go b/api/http/handler/edgetemplates/handler.go index 75473c49a..963ddb931 100644 --- a/api/http/handler/edgetemplates/handler.go +++ b/api/http/handler/edgetemplates/handler.go @@ -13,8 +13,8 @@ import ( // Handler is the HTTP handler used to handle edge endpoint operations. type Handler struct { *mux.Router - requestBouncer *security.RequestBouncer - SettingsService portainer.SettingsService + requestBouncer *security.RequestBouncer + DataStore portainer.DataStore } // NewHandler creates a handler to manage endpoint operations. diff --git a/api/http/handler/endpointedge/endpoint_edgestack_inspect.go b/api/http/handler/endpointedge/endpoint_edgestack_inspect.go index 3853e3bba..54af3f3ef 100644 --- a/api/http/handler/endpointedge/endpoint_edgestack_inspect.go +++ b/api/http/handler/endpointedge/endpoint_edgestack_inspect.go @@ -23,7 +23,7 @@ func (handler *Handler) endpointEdgeStackInspect(w http.ResponseWriter, r *http. return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint identifier route variable", err} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -40,7 +40,7 @@ func (handler *Handler) endpointEdgeStackInspect(w http.ResponseWriter, r *http. return &httperror.HandlerError{http.StatusBadRequest, "Invalid edge stack identifier route variable", err} } - edgeStack, err := handler.EdgeStackService.EdgeStack(portainer.EdgeStackID(edgeStackID)) + edgeStack, err := handler.DataStore.EdgeStack().EdgeStack(portainer.EdgeStackID(edgeStackID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an edge stack with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpointedge/handler.go b/api/http/handler/endpointedge/handler.go index e8dfc2995..14aff281e 100644 --- a/api/http/handler/endpointedge/handler.go +++ b/api/http/handler/endpointedge/handler.go @@ -13,10 +13,9 @@ import ( // Handler is the HTTP handler used to handle edge endpoint operations. type Handler struct { *mux.Router - requestBouncer *security.RequestBouncer - EndpointService portainer.EndpointService - EdgeStackService portainer.EdgeStackService - FileService portainer.FileService + requestBouncer *security.RequestBouncer + DataStore portainer.DataStore + FileService portainer.FileService } // NewHandler creates a handler to manage endpoint operations. diff --git a/api/http/handler/endpointgroups/endpointgroup_create.go b/api/http/handler/endpointgroups/endpointgroup_create.go index f296fee64..d77464744 100644 --- a/api/http/handler/endpointgroups/endpointgroup_create.go +++ b/api/http/handler/endpointgroups/endpointgroup_create.go @@ -43,12 +43,12 @@ func (handler *Handler) endpointGroupCreate(w http.ResponseWriter, r *http.Reque TagIDs: payload.TagIDs, } - err = handler.EndpointGroupService.CreateEndpointGroup(endpointGroup) + err = handler.DataStore.EndpointGroup().CreateEndpointGroup(endpointGroup) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the endpoint group inside the database", err} } - endpoints, err := handler.EndpointService.Endpoints() + endpoints, err := handler.DataStore.Endpoint().Endpoints() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoints from the database", err} } @@ -58,7 +58,7 @@ func (handler *Handler) endpointGroupCreate(w http.ResponseWriter, r *http.Reque if endpoint.ID == id { endpoint.GroupID = endpointGroup.ID - err := handler.EndpointService.UpdateEndpoint(endpoint.ID, &endpoint) + err := handler.DataStore.Endpoint().UpdateEndpoint(endpoint.ID, &endpoint) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to update endpoint", err} } @@ -74,14 +74,14 @@ func (handler *Handler) endpointGroupCreate(w http.ResponseWriter, r *http.Reque } for _, tagID := range endpointGroup.TagIDs { - tag, err := handler.TagService.Tag(tagID) + tag, err := handler.DataStore.Tag().Tag(tagID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve tag from the database", err} } tag.EndpointGroups[endpointGroup.ID] = true - err = handler.TagService.UpdateTag(tagID, tag) + err = handler.DataStore.Tag().UpdateTag(tagID, tag) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist tag changes inside the database", err} } diff --git a/api/http/handler/endpointgroups/endpointgroup_delete.go b/api/http/handler/endpointgroups/endpointgroup_delete.go index 76cd4ce86..0dba168eb 100644 --- a/api/http/handler/endpointgroups/endpointgroup_delete.go +++ b/api/http/handler/endpointgroups/endpointgroup_delete.go @@ -20,19 +20,19 @@ func (handler *Handler) endpointGroupDelete(w http.ResponseWriter, r *http.Reque return &httperror.HandlerError{http.StatusForbidden, "Unable to remove the default 'Unassigned' group", portainer.ErrCannotRemoveDefaultGroup} } - endpointGroup, err := handler.EndpointGroupService.EndpointGroup(portainer.EndpointGroupID(endpointGroupID)) + endpointGroup, err := handler.DataStore.EndpointGroup().EndpointGroup(portainer.EndpointGroupID(endpointGroupID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint group with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an endpoint group with the specified identifier inside the database", err} } - err = handler.EndpointGroupService.DeleteEndpointGroup(portainer.EndpointGroupID(endpointGroupID)) + err = handler.DataStore.EndpointGroup().DeleteEndpointGroup(portainer.EndpointGroupID(endpointGroupID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove the endpoint group from the database", err} } - endpoints, err := handler.EndpointService.Endpoints() + endpoints, err := handler.DataStore.Endpoint().Endpoints() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoints from the database", err} } @@ -42,7 +42,7 @@ func (handler *Handler) endpointGroupDelete(w http.ResponseWriter, r *http.Reque if endpoint.GroupID == portainer.EndpointGroupID(endpointGroupID) { updateAuthorizations = true endpoint.GroupID = portainer.EndpointGroupID(1) - err = handler.EndpointService.UpdateEndpoint(endpoint.ID, &endpoint) + err = handler.DataStore.Endpoint().UpdateEndpoint(endpoint.ID, &endpoint) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to update endpoint", err} } @@ -62,14 +62,14 @@ func (handler *Handler) endpointGroupDelete(w http.ResponseWriter, r *http.Reque } for _, tagID := range endpointGroup.TagIDs { - tag, err := handler.TagService.Tag(tagID) + tag, err := handler.DataStore.Tag().Tag(tagID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve tag from the database", err} } delete(tag.EndpointGroups, endpointGroup.ID) - err = handler.TagService.UpdateTag(tagID, tag) + err = handler.DataStore.Tag().UpdateTag(tagID, tag) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist tag changes inside the database", err} } diff --git a/api/http/handler/endpointgroups/endpointgroup_endpoint_add.go b/api/http/handler/endpointgroups/endpointgroup_endpoint_add.go index f2435ab33..710aa7df5 100644 --- a/api/http/handler/endpointgroups/endpointgroup_endpoint_add.go +++ b/api/http/handler/endpointgroups/endpointgroup_endpoint_add.go @@ -21,14 +21,14 @@ func (handler *Handler) endpointGroupAddEndpoint(w http.ResponseWriter, r *http. return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint identifier route variable", err} } - endpointGroup, err := handler.EndpointGroupService.EndpointGroup(portainer.EndpointGroupID(endpointGroupID)) + endpointGroup, err := handler.DataStore.EndpointGroup().EndpointGroup(portainer.EndpointGroupID(endpointGroupID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint group with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an endpoint group with the specified identifier inside the database", err} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -37,7 +37,7 @@ func (handler *Handler) endpointGroupAddEndpoint(w http.ResponseWriter, r *http. endpoint.GroupID = endpointGroup.ID - err = handler.EndpointService.UpdateEndpoint(endpoint.ID, endpoint) + err = handler.DataStore.Endpoint().UpdateEndpoint(endpoint.ID, endpoint) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint changes inside the database", err} } diff --git a/api/http/handler/endpointgroups/endpointgroup_endpoint_delete.go b/api/http/handler/endpointgroups/endpointgroup_endpoint_delete.go index 0e4a21611..f235b3ab9 100644 --- a/api/http/handler/endpointgroups/endpointgroup_endpoint_delete.go +++ b/api/http/handler/endpointgroups/endpointgroup_endpoint_delete.go @@ -21,14 +21,14 @@ func (handler *Handler) endpointGroupDeleteEndpoint(w http.ResponseWriter, r *ht return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint identifier route variable", err} } - _, err = handler.EndpointGroupService.EndpointGroup(portainer.EndpointGroupID(endpointGroupID)) + _, err = handler.DataStore.EndpointGroup().EndpointGroup(portainer.EndpointGroupID(endpointGroupID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint group with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an endpoint group with the specified identifier inside the database", err} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -37,7 +37,7 @@ func (handler *Handler) endpointGroupDeleteEndpoint(w http.ResponseWriter, r *ht endpoint.GroupID = portainer.EndpointGroupID(1) - err = handler.EndpointService.UpdateEndpoint(endpoint.ID, endpoint) + err = handler.DataStore.Endpoint().UpdateEndpoint(endpoint.ID, endpoint) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint changes inside the database", err} } diff --git a/api/http/handler/endpointgroups/endpointgroup_inspect.go b/api/http/handler/endpointgroups/endpointgroup_inspect.go index 3ddc464e7..b931ba82b 100644 --- a/api/http/handler/endpointgroups/endpointgroup_inspect.go +++ b/api/http/handler/endpointgroups/endpointgroup_inspect.go @@ -16,7 +16,7 @@ func (handler *Handler) endpointGroupInspect(w http.ResponseWriter, r *http.Requ return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint group identifier route variable", err} } - endpointGroup, err := handler.EndpointGroupService.EndpointGroup(portainer.EndpointGroupID(endpointGroupID)) + endpointGroup, err := handler.DataStore.EndpointGroup().EndpointGroup(portainer.EndpointGroupID(endpointGroupID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint group with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpointgroups/endpointgroup_list.go b/api/http/handler/endpointgroups/endpointgroup_list.go index 7ea73d2d5..e4e1bb2da 100644 --- a/api/http/handler/endpointgroups/endpointgroup_list.go +++ b/api/http/handler/endpointgroups/endpointgroup_list.go @@ -10,7 +10,7 @@ import ( // GET request on /api/endpoint_groups func (handler *Handler) endpointGroupList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - endpointGroups, err := handler.EndpointGroupService.EndpointGroups() + endpointGroups, err := handler.DataStore.EndpointGroup().EndpointGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoint groups from the database", err} } diff --git a/api/http/handler/endpointgroups/endpointgroup_update.go b/api/http/handler/endpointgroups/endpointgroup_update.go index 362b8b697..e7d00d41c 100644 --- a/api/http/handler/endpointgroups/endpointgroup_update.go +++ b/api/http/handler/endpointgroups/endpointgroup_update.go @@ -35,7 +35,7 @@ func (handler *Handler) endpointGroupUpdate(w http.ResponseWriter, r *http.Reque return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - endpointGroup, err := handler.EndpointGroupService.EndpointGroup(portainer.EndpointGroupID(endpointGroupID)) + endpointGroup, err := handler.DataStore.EndpointGroup().EndpointGroup(portainer.EndpointGroupID(endpointGroupID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint group with the specified identifier inside the database", err} } else if err != nil { @@ -62,12 +62,12 @@ func (handler *Handler) endpointGroupUpdate(w http.ResponseWriter, r *http.Reque removeTags := portainer.TagDifference(endpointGroupTagSet, payloadTagSet) for tagID := range removeTags { - tag, err := handler.TagService.Tag(tagID) + tag, err := handler.DataStore.Tag().Tag(tagID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a tag inside the database", err} } delete(tag.EndpointGroups, endpointGroup.ID) - err = handler.TagService.UpdateTag(tag.ID, tag) + err = handler.DataStore.Tag().UpdateTag(tag.ID, tag) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist tag changes inside the database", err} } @@ -75,14 +75,14 @@ func (handler *Handler) endpointGroupUpdate(w http.ResponseWriter, r *http.Reque endpointGroup.TagIDs = payload.TagIDs for _, tagID := range payload.TagIDs { - tag, err := handler.TagService.Tag(tagID) + tag, err := handler.DataStore.Tag().Tag(tagID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a tag inside the database", err} } tag.EndpointGroups[endpointGroup.ID] = true - err = handler.TagService.UpdateTag(tag.ID, tag) + err = handler.DataStore.Tag().UpdateTag(tag.ID, tag) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist tag changes inside the database", err} } @@ -101,7 +101,7 @@ func (handler *Handler) endpointGroupUpdate(w http.ResponseWriter, r *http.Reque updateAuthorizations = true } - err = handler.EndpointGroupService.UpdateEndpointGroup(endpointGroup.ID, endpointGroup) + err = handler.DataStore.EndpointGroup().UpdateEndpointGroup(endpointGroup.ID, endpointGroup) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint group changes inside the database", err} } @@ -114,7 +114,7 @@ func (handler *Handler) endpointGroupUpdate(w http.ResponseWriter, r *http.Reque } if tagsChanged { - endpoints, err := handler.EndpointService.Endpoints() + endpoints, err := handler.DataStore.Endpoint().Endpoints() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoints from the database", err} diff --git a/api/http/handler/endpointgroups/endpoints.go b/api/http/handler/endpointgroups/endpoints.go index 11e760e15..3ff3096cb 100644 --- a/api/http/handler/endpointgroups/endpoints.go +++ b/api/http/handler/endpointgroups/endpoints.go @@ -8,7 +8,7 @@ func (handler *Handler) updateEndpointRelations(endpoint *portainer.Endpoint, en } if endpointGroup == nil { - unassignedGroup, err := handler.EndpointGroupService.EndpointGroup(portainer.EndpointGroupID(1)) + unassignedGroup, err := handler.DataStore.EndpointGroup().EndpointGroup(portainer.EndpointGroupID(1)) if err != nil { return err } @@ -16,17 +16,17 @@ func (handler *Handler) updateEndpointRelations(endpoint *portainer.Endpoint, en endpointGroup = unassignedGroup } - endpointRelation, err := handler.EndpointRelationService.EndpointRelation(endpoint.ID) + endpointRelation, err := handler.DataStore.EndpointRelation().EndpointRelation(endpoint.ID) if err != nil { return err } - edgeGroups, err := handler.EdgeGroupService.EdgeGroups() + edgeGroups, err := handler.DataStore.EdgeGroup().EdgeGroups() if err != nil { return err } - edgeStacks, err := handler.EdgeStackService.EdgeStacks() + edgeStacks, err := handler.DataStore.EdgeStack().EdgeStacks() if err != nil { return err } @@ -38,5 +38,5 @@ func (handler *Handler) updateEndpointRelations(endpoint *portainer.Endpoint, en } endpointRelation.EdgeStacks = stacksSet - return handler.EndpointRelationService.UpdateEndpointRelation(endpoint.ID, endpointRelation) + return handler.DataStore.EndpointRelation().UpdateEndpointRelation(endpoint.ID, endpointRelation) } diff --git a/api/http/handler/endpointgroups/handler.go b/api/http/handler/endpointgroups/handler.go index a738a2dc1..9730e875d 100644 --- a/api/http/handler/endpointgroups/handler.go +++ b/api/http/handler/endpointgroups/handler.go @@ -12,13 +12,8 @@ import ( // Handler is the HTTP handler used to handle endpoint group operations. type Handler struct { *mux.Router - AuthorizationService *portainer.AuthorizationService - EdgeGroupService portainer.EdgeGroupService - EdgeStackService portainer.EdgeStackService - EndpointService portainer.EndpointService - EndpointGroupService portainer.EndpointGroupService - EndpointRelationService portainer.EndpointRelationService - TagService portainer.TagService + DataStore portainer.DataStore + AuthorizationService *portainer.AuthorizationService } // NewHandler creates a handler to manage endpoint group operations. diff --git a/api/http/handler/endpointproxy/handler.go b/api/http/handler/endpointproxy/handler.go index ed81a6527..394fa0b54 100644 --- a/api/http/handler/endpointproxy/handler.go +++ b/api/http/handler/endpointproxy/handler.go @@ -11,9 +11,8 @@ import ( // Handler is the HTTP handler used to proxy requests to external APIs. type Handler struct { *mux.Router + DataStore portainer.DataStore requestBouncer *security.RequestBouncer - EndpointService portainer.EndpointService - SettingsService portainer.SettingsService ProxyManager *proxy.Manager ReverseTunnelService portainer.ReverseTunnelService } diff --git a/api/http/handler/endpointproxy/proxy_docker.go b/api/http/handler/endpointproxy/proxy_docker.go index d84aacc62..8a228fcf4 100644 --- a/api/http/handler/endpointproxy/proxy_docker.go +++ b/api/http/handler/endpointproxy/proxy_docker.go @@ -18,7 +18,7 @@ func (handler *Handler) proxyRequestsToDockerAPI(w http.ResponseWriter, r *http. return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint identifier route variable", err} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -44,7 +44,7 @@ func (handler *Handler) proxyRequestsToDockerAPI(w http.ResponseWriter, r *http. return &httperror.HandlerError{http.StatusInternalServerError, "Unable to update tunnel status", err} } - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err} } diff --git a/api/http/handler/endpointproxy/proxy_storidge.go b/api/http/handler/endpointproxy/proxy_storidge.go index f2d2aacb1..70a019a83 100644 --- a/api/http/handler/endpointproxy/proxy_storidge.go +++ b/api/http/handler/endpointproxy/proxy_storidge.go @@ -18,7 +18,7 @@ func (handler *Handler) proxyRequestsToStoridgeAPI(w http.ResponseWriter, r *htt return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint identifier route variable", err} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpoints/endpoint_create.go b/api/http/handler/endpoints/endpoint_create.go index cd1b84cb5..e53d7f96d 100644 --- a/api/http/handler/endpoints/endpoint_create.go +++ b/api/http/handler/endpoints/endpoint_create.go @@ -118,17 +118,17 @@ func (handler *Handler) endpointCreate(w http.ResponseWriter, r *http.Request) * return endpointCreationError } - endpointGroup, err := handler.EndpointGroupService.EndpointGroup(endpoint.GroupID) + endpointGroup, err := handler.DataStore.EndpointGroup().EndpointGroup(endpoint.GroupID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an endpoint group inside the database", err} } - edgeGroups, err := handler.EdgeGroupService.EdgeGroups() + edgeGroups, err := handler.DataStore.EdgeGroup().EdgeGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge groups from the database", err} } - edgeStacks, err := handler.EdgeStackService.EdgeStacks() + edgeStacks, err := handler.DataStore.EdgeStack().EdgeStacks() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge stacks from the database", err} } @@ -145,7 +145,7 @@ func (handler *Handler) endpointCreate(w http.ResponseWriter, r *http.Request) * } } - err = handler.EndpointRelationService.CreateEndpointRelation(relationObject) + err = handler.DataStore.EndpointRelation().CreateEndpointRelation(relationObject) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the relation object inside the database", err} } @@ -166,7 +166,7 @@ func (handler *Handler) createEndpoint(payload *endpointCreatePayload) (*portain func (handler *Handler) createEdgeAgentEndpoint(payload *endpointCreatePayload) (*portainer.Endpoint, *httperror.HandlerError) { endpointType := portainer.EdgeAgentEnvironment - endpointID := handler.EndpointService.GetNextIdentifier() + endpointID := handler.DataStore.Endpoint().GetNextIdentifier() portainerURL, err := url.Parse(payload.URL) if err != nil { @@ -228,7 +228,7 @@ func (handler *Handler) createUnsecuredEndpoint(payload *endpointCreatePayload) } } - endpointID := handler.EndpointService.GetNextIdentifier() + endpointID := handler.DataStore.Endpoint().GetNextIdentifier() endpoint := &portainer.Endpoint{ ID: portainer.EndpointID(endpointID), Name: payload.Name, @@ -271,7 +271,7 @@ func (handler *Handler) createTLSSecuredEndpoint(payload *endpointCreatePayload) endpointType = portainer.AgentOnDockerEnvironment } - endpointID := handler.EndpointService.GetNextIdentifier() + endpointID := handler.DataStore.Endpoint().GetNextIdentifier() endpoint := &portainer.Endpoint{ ID: portainer.EndpointID(endpointID), Name: payload.Name, @@ -327,12 +327,12 @@ func (handler *Handler) snapshotAndPersistEndpoint(endpoint *portainer.Endpoint) } func (handler *Handler) saveEndpointAndUpdateAuthorizations(endpoint *portainer.Endpoint) error { - err := handler.EndpointService.CreateEndpoint(endpoint) + err := handler.DataStore.Endpoint().CreateEndpoint(endpoint) if err != nil { return err } - group, err := handler.EndpointGroupService.EndpointGroup(endpoint.GroupID) + group, err := handler.DataStore.EndpointGroup().EndpointGroup(endpoint.GroupID) if err != nil { return err } @@ -342,14 +342,14 @@ func (handler *Handler) saveEndpointAndUpdateAuthorizations(endpoint *portainer. } for _, tagID := range endpoint.TagIDs { - tag, err := handler.TagService.Tag(tagID) + tag, err := handler.DataStore.Tag().Tag(tagID) if err != nil { return err } tag.Endpoints[endpoint.ID] = true - err = handler.TagService.UpdateTag(tagID, tag) + err = handler.DataStore.Tag().UpdateTag(tagID, tag) if err != nil { return err } diff --git a/api/http/handler/endpoints/endpoint_delete.go b/api/http/handler/endpoints/endpoint_delete.go index 7ef6c0a0d..7a280d703 100644 --- a/api/http/handler/endpoints/endpoint_delete.go +++ b/api/http/handler/endpoints/endpoint_delete.go @@ -17,7 +17,7 @@ func (handler *Handler) endpointDelete(w http.ResponseWriter, r *http.Request) * return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint identifier route variable", err} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -32,7 +32,7 @@ func (handler *Handler) endpointDelete(w http.ResponseWriter, r *http.Request) * } } - err = handler.EndpointService.DeleteEndpoint(portainer.EndpointID(endpointID)) + err = handler.DataStore.Endpoint().DeleteEndpoint(portainer.EndpointID(endpointID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove endpoint from the database", err} } @@ -46,26 +46,26 @@ func (handler *Handler) endpointDelete(w http.ResponseWriter, r *http.Request) * } } - err = handler.EndpointRelationService.DeleteEndpointRelation(endpoint.ID) + err = handler.DataStore.EndpointRelation().DeleteEndpointRelation(endpoint.ID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove endpoint relation from the database", err} } for _, tagID := range endpoint.TagIDs { - tag, err := handler.TagService.Tag(tagID) + tag, err := handler.DataStore.Tag().Tag(tagID) if err != nil { return &httperror.HandlerError{http.StatusNotFound, "Unable to find tag inside the database", err} } delete(tag.Endpoints, endpoint.ID) - err = handler.TagService.UpdateTag(tagID, tag) + err = handler.DataStore.Tag().UpdateTag(tagID, tag) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist tag relation inside the database", err} } } - edgeGroups, err := handler.EdgeGroupService.EdgeGroups() + edgeGroups, err := handler.DataStore.EdgeGroup().EdgeGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge groups from the database", err} } @@ -75,14 +75,14 @@ func (handler *Handler) endpointDelete(w http.ResponseWriter, r *http.Request) * endpointIdx := findEndpointIndex(edgeGroup.Endpoints, endpoint.ID) if endpointIdx != -1 { edgeGroup.Endpoints = removeElement(edgeGroup.Endpoints, endpointIdx) - err = handler.EdgeGroupService.UpdateEdgeGroup(edgeGroup.ID, edgeGroup) + err = handler.DataStore.EdgeGroup().UpdateEdgeGroup(edgeGroup.ID, edgeGroup) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to update edge group", err} } } } - edgeStacks, err := handler.EdgeStackService.EdgeStacks() + edgeStacks, err := handler.DataStore.EdgeStack().EdgeStacks() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge stacks from the database", err} } @@ -91,7 +91,7 @@ func (handler *Handler) endpointDelete(w http.ResponseWriter, r *http.Request) * edgeStack := &edgeStacks[idx] if _, ok := edgeStack.Status[endpoint.ID]; ok { delete(edgeStack.Status, endpoint.ID) - err = handler.EdgeStackService.UpdateEdgeStack(edgeStack.ID, edgeStack) + err = handler.DataStore.EdgeStack().UpdateEdgeStack(edgeStack.ID, edgeStack) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to update edge stack", err} } diff --git a/api/http/handler/endpoints/endpoint_extension_add.go b/api/http/handler/endpoints/endpoint_extension_add.go index f91a714c3..4472913dc 100644 --- a/api/http/handler/endpoints/endpoint_extension_add.go +++ b/api/http/handler/endpoints/endpoint_extension_add.go @@ -34,7 +34,7 @@ func (handler *Handler) endpointExtensionAdd(w http.ResponseWriter, r *http.Requ return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint identifier route variable", err} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -66,7 +66,7 @@ func (handler *Handler) endpointExtensionAdd(w http.ResponseWriter, r *http.Requ endpoint.Extensions = append(endpoint.Extensions, *extension) } - err = handler.EndpointService.UpdateEndpoint(endpoint.ID, endpoint) + err = handler.DataStore.Endpoint().UpdateEndpoint(endpoint.ID, endpoint) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint changes inside the database", err} } diff --git a/api/http/handler/endpoints/endpoint_extension_remove.go b/api/http/handler/endpoints/endpoint_extension_remove.go index b426071e0..6d81da363 100644 --- a/api/http/handler/endpoints/endpoint_extension_remove.go +++ b/api/http/handler/endpoints/endpoint_extension_remove.go @@ -18,7 +18,7 @@ func (handler *Handler) endpointExtensionRemove(w http.ResponseWriter, r *http.R return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint identifier route variable", err} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -36,7 +36,7 @@ func (handler *Handler) endpointExtensionRemove(w http.ResponseWriter, r *http.R } } - err = handler.EndpointService.UpdateEndpoint(endpoint.ID, endpoint) + err = handler.DataStore.Endpoint().UpdateEndpoint(endpoint.ID, endpoint) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint changes inside the database", err} } diff --git a/api/http/handler/endpoints/endpoint_inspect.go b/api/http/handler/endpoints/endpoint_inspect.go index 10cf34ed9..01abe2c3d 100644 --- a/api/http/handler/endpoints/endpoint_inspect.go +++ b/api/http/handler/endpoints/endpoint_inspect.go @@ -16,7 +16,7 @@ func (handler *Handler) endpointInspect(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint identifier route variable", err} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/endpoints/endpoint_job.go b/api/http/handler/endpoints/endpoint_job.go index 78d00bc9c..77f727ea3 100644 --- a/api/http/handler/endpoints/endpoint_job.go +++ b/api/http/handler/endpoints/endpoint_job.go @@ -63,7 +63,7 @@ func (handler *Handler) endpointJob(w http.ResponseWriter, r *http.Request) *htt nodeName, _ := request.RetrieveQueryParameter(r, "nodeName", true) - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint 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 57ffacbce..fe7c489ae 100644 --- a/api/http/handler/endpoints/endpoint_list.go +++ b/api/http/handler/endpoints/endpoint_list.go @@ -38,12 +38,12 @@ func (handler *Handler) endpointList(w http.ResponseWriter, r *http.Request) *ht var endpointIDs []portainer.EndpointID request.RetrieveJSONQueryParameter(r, "endpointIds", &endpointIDs, true) - endpointGroups, err := handler.EndpointGroupService.EndpointGroups() + endpointGroups, err := handler.DataStore.EndpointGroup().EndpointGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoint groups from the database", err} } - endpoints, err := handler.EndpointService.Endpoints() + endpoints, err := handler.DataStore.Endpoint().Endpoints() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoints from the database", err} } @@ -64,7 +64,7 @@ func (handler *Handler) endpointList(w http.ResponseWriter, r *http.Request) *ht } if search != "" { - tags, err := handler.TagService.Tags() + tags, err := handler.DataStore.Tag().Tags() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve tags from the database", err} } diff --git a/api/http/handler/endpoints/endpoint_snapshot.go b/api/http/handler/endpoints/endpoint_snapshot.go index ba73674a9..21d6b8b0a 100644 --- a/api/http/handler/endpoints/endpoint_snapshot.go +++ b/api/http/handler/endpoints/endpoint_snapshot.go @@ -16,7 +16,7 @@ func (handler *Handler) endpointSnapshot(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint identifier route variable", err} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -25,7 +25,7 @@ func (handler *Handler) endpointSnapshot(w http.ResponseWriter, r *http.Request) snapshot, snapshotError := handler.Snapshotter.CreateSnapshot(endpoint) - latestEndpointReference, err := handler.EndpointService.Endpoint(endpoint.ID) + latestEndpointReference, err := handler.DataStore.Endpoint().Endpoint(endpoint.ID) if latestEndpointReference == nil { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } @@ -39,7 +39,7 @@ func (handler *Handler) endpointSnapshot(w http.ResponseWriter, r *http.Request) latestEndpointReference.Snapshots = []portainer.Snapshot{*snapshot} } - err = handler.EndpointService.UpdateEndpoint(latestEndpointReference.ID, latestEndpointReference) + err = handler.DataStore.Endpoint().UpdateEndpoint(latestEndpointReference.ID, latestEndpointReference) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint changes inside the database", err} } diff --git a/api/http/handler/endpoints/endpoint_snapshots.go b/api/http/handler/endpoints/endpoint_snapshots.go index 11a5b07ab..092dc5df1 100644 --- a/api/http/handler/endpoints/endpoint_snapshots.go +++ b/api/http/handler/endpoints/endpoint_snapshots.go @@ -11,7 +11,7 @@ import ( // POST request on /api/endpoints/snapshot func (handler *Handler) endpointSnapshots(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - endpoints, err := handler.EndpointService.Endpoints() + endpoints, err := handler.DataStore.Endpoint().Endpoints() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoints from the database", err} } @@ -19,7 +19,7 @@ func (handler *Handler) endpointSnapshots(w http.ResponseWriter, r *http.Request for _, endpoint := range endpoints { snapshot, snapshotError := handler.Snapshotter.CreateSnapshot(&endpoint) - latestEndpointReference, err := handler.EndpointService.Endpoint(endpoint.ID) + latestEndpointReference, err := handler.DataStore.Endpoint().Endpoint(endpoint.ID) if latestEndpointReference == nil { log.Printf("background schedule error (endpoint snapshot). Endpoint not found inside the database anymore (endpoint=%s, URL=%s) (err=%s)\n", endpoint.Name, endpoint.URL, err) continue @@ -35,7 +35,7 @@ func (handler *Handler) endpointSnapshots(w http.ResponseWriter, r *http.Request latestEndpointReference.Snapshots = []portainer.Snapshot{*snapshot} } - err = handler.EndpointService.UpdateEndpoint(latestEndpointReference.ID, latestEndpointReference) + err = handler.DataStore.Endpoint().UpdateEndpoint(latestEndpointReference.ID, latestEndpointReference) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint changes inside the database", err} } diff --git a/api/http/handler/endpoints/endpoint_status_inspect.go b/api/http/handler/endpoints/endpoint_status_inspect.go index 13ae819a0..b8b1c4d63 100644 --- a/api/http/handler/endpoints/endpoint_status_inspect.go +++ b/api/http/handler/endpoints/endpoint_status_inspect.go @@ -30,7 +30,7 @@ func (handler *Handler) endpointStatusInspect(w http.ResponseWriter, r *http.Req return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint identifier route variable", err} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -47,13 +47,13 @@ func (handler *Handler) endpointStatusInspect(w http.ResponseWriter, r *http.Req endpoint.EdgeID = edgeIdentifier - err := handler.EndpointService.UpdateEndpoint(endpoint.ID, endpoint) + err := handler.DataStore.Endpoint().UpdateEndpoint(endpoint.ID, endpoint) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to Unable to persist endpoint changes inside the database", err} } } - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err} } @@ -72,14 +72,14 @@ func (handler *Handler) endpointStatusInspect(w http.ResponseWriter, r *http.Req handler.ReverseTunnelService.SetTunnelStatusToActive(endpoint.ID) } - relation, err := handler.EndpointRelationService.EndpointRelation(endpoint.ID) + relation, err := handler.DataStore.EndpointRelation().EndpointRelation(endpoint.ID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve relation object from the database", err} } edgeStacksStatus := []stackStatusResponse{} for stackID := range relation.EdgeStacks { - stack, err := handler.EdgeStackService.EdgeStack(stackID) + stack, err := handler.DataStore.EdgeStack().EdgeStack(stackID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge stack from the database", err} } diff --git a/api/http/handler/endpoints/endpoint_update.go b/api/http/handler/endpoints/endpoint_update.go index a1c63d53c..6ba76c992 100644 --- a/api/http/handler/endpoints/endpoint_update.go +++ b/api/http/handler/endpoints/endpoint_update.go @@ -42,7 +42,7 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) * return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -80,13 +80,13 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) * removeTags := portainer.TagDifference(endpointTagSet, payloadTagSet) for tagID := range removeTags { - tag, err := handler.TagService.Tag(tagID) + tag, err := handler.DataStore.Tag().Tag(tagID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a tag inside the database", err} } delete(tag.Endpoints, endpoint.ID) - err = handler.TagService.UpdateTag(tag.ID, tag) + err = handler.DataStore.Tag().UpdateTag(tag.ID, tag) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist tag changes inside the database", err} } @@ -94,14 +94,14 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) * endpoint.TagIDs = payload.TagIDs for _, tagID := range payload.TagIDs { - tag, err := handler.TagService.Tag(tagID) + tag, err := handler.DataStore.Tag().Tag(tagID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a tag inside the database", err} } tag.Endpoints[endpoint.ID] = true - err = handler.TagService.UpdateTag(tag.ID, tag) + err = handler.DataStore.Tag().UpdateTag(tag.ID, tag) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist tag changes inside the database", err} } @@ -184,7 +184,7 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) * } } - err = handler.EndpointService.UpdateEndpoint(endpoint.ID, endpoint) + err = handler.DataStore.Endpoint().UpdateEndpoint(endpoint.ID, endpoint) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint changes inside the database", err} } @@ -197,22 +197,22 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) * } if endpoint.Type == portainer.EdgeAgentEnvironment && (groupIDChanged || tagsChanged) { - relation, err := handler.EndpointRelationService.EndpointRelation(endpoint.ID) + relation, err := handler.DataStore.EndpointRelation().EndpointRelation(endpoint.ID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find endpoint relation inside the database", err} } - endpointGroup, err := handler.EndpointGroupService.EndpointGroup(endpoint.GroupID) + endpointGroup, err := handler.DataStore.EndpointGroup().EndpointGroup(endpoint.GroupID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find endpoint group inside the database", err} } - edgeGroups, err := handler.EdgeGroupService.EdgeGroups() + edgeGroups, err := handler.DataStore.EdgeGroup().EdgeGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge groups from the database", err} } - edgeStacks, err := handler.EdgeStackService.EdgeStacks() + edgeStacks, err := handler.DataStore.EdgeStack().EdgeStacks() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge stacks from the database", err} } @@ -226,7 +226,7 @@ func (handler *Handler) endpointUpdate(w http.ResponseWriter, r *http.Request) * relation.EdgeStacks = edgeStackSet - err = handler.EndpointRelationService.UpdateEndpointRelation(endpoint.ID, relation) + err = handler.DataStore.EndpointRelation().UpdateEndpointRelation(endpoint.ID, relation) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint relation changes inside the database", err} } diff --git a/api/http/handler/endpoints/handler.go b/api/http/handler/endpoints/handler.go index 605c81322..15e8a55c0 100644 --- a/api/http/handler/endpoints/handler.go +++ b/api/http/handler/endpoints/handler.go @@ -20,27 +20,21 @@ func hideFields(endpoint *portainer.Endpoint) { // Handler is the HTTP handler used to handle endpoint operations. type Handler struct { *mux.Router - requestBouncer *security.RequestBouncer - AuthorizationService *portainer.AuthorizationService - EdgeGroupService portainer.EdgeGroupService - EdgeStackService portainer.EdgeStackService - EndpointService portainer.EndpointService - EndpointGroupService portainer.EndpointGroupService - EndpointRelationService portainer.EndpointRelationService - FileService portainer.FileService - JobService portainer.JobService - ProxyManager *proxy.Manager - ReverseTunnelService portainer.ReverseTunnelService - SettingsService portainer.SettingsService - Snapshotter portainer.Snapshotter - TagService portainer.TagService + requestBouncer *security.RequestBouncer + DataStore portainer.DataStore + AuthorizationService *portainer.AuthorizationService + FileService portainer.FileService + JobService portainer.JobService + ProxyManager *proxy.Manager + ReverseTunnelService portainer.ReverseTunnelService + Snapshotter portainer.Snapshotter } // NewHandler creates a handler to manage endpoint operations. func NewHandler(bouncer *security.RequestBouncer) *Handler { h := &Handler{ - Router: mux.NewRouter(), - requestBouncer: bouncer, + Router: mux.NewRouter(), + requestBouncer: bouncer, } h.Handle("/endpoints", diff --git a/api/http/handler/extensions/data.go b/api/http/handler/extensions/data.go index 8c950e608..37dcd62fc 100644 --- a/api/http/handler/extensions/data.go +++ b/api/http/handler/extensions/data.go @@ -17,7 +17,7 @@ func updateTeamAccessPolicyToReadOnlyRole(policies portainer.TeamAccessPolicies, } func (handler *Handler) upgradeRBACData() error { - endpointGroups, err := handler.EndpointGroupService.EndpointGroups() + endpointGroups, err := handler.DataStore.EndpointGroup().EndpointGroups() if err != nil { return err } @@ -31,13 +31,13 @@ func (handler *Handler) upgradeRBACData() error { updateTeamAccessPolicyToReadOnlyRole(endpointGroup.TeamAccessPolicies, key) } - err := handler.EndpointGroupService.UpdateEndpointGroup(endpointGroup.ID, &endpointGroup) + err := handler.DataStore.EndpointGroup().UpdateEndpointGroup(endpointGroup.ID, &endpointGroup) if err != nil { return err } } - endpoints, err := handler.EndpointService.Endpoints() + endpoints, err := handler.DataStore.Endpoint().Endpoints() if err != nil { return err } @@ -51,7 +51,7 @@ func (handler *Handler) upgradeRBACData() error { updateTeamAccessPolicyToReadOnlyRole(endpoint.TeamAccessPolicies, key) } - err := handler.EndpointService.UpdateEndpoint(endpoint.ID, &endpoint) + err := handler.DataStore.Endpoint().UpdateEndpoint(endpoint.ID, &endpoint) if err != nil { return err } @@ -73,7 +73,7 @@ func updateTeamAccessPolicyToNoRole(policies portainer.TeamAccessPolicies, key p } func (handler *Handler) downgradeRBACData() error { - endpointGroups, err := handler.EndpointGroupService.EndpointGroups() + endpointGroups, err := handler.DataStore.EndpointGroup().EndpointGroups() if err != nil { return err } @@ -87,13 +87,13 @@ func (handler *Handler) downgradeRBACData() error { updateTeamAccessPolicyToNoRole(endpointGroup.TeamAccessPolicies, key) } - err := handler.EndpointGroupService.UpdateEndpointGroup(endpointGroup.ID, &endpointGroup) + err := handler.DataStore.EndpointGroup().UpdateEndpointGroup(endpointGroup.ID, &endpointGroup) if err != nil { return err } } - endpoints, err := handler.EndpointService.Endpoints() + endpoints, err := handler.DataStore.Endpoint().Endpoints() if err != nil { return err } @@ -107,7 +107,7 @@ func (handler *Handler) downgradeRBACData() error { updateTeamAccessPolicyToNoRole(endpoint.TeamAccessPolicies, key) } - err := handler.EndpointService.UpdateEndpoint(endpoint.ID, &endpoint) + err := handler.DataStore.Endpoint().UpdateEndpoint(endpoint.ID, &endpoint) if err != nil { return err } diff --git a/api/http/handler/extensions/extension_create.go b/api/http/handler/extensions/extension_create.go index 7e41c9595..dd602f3b0 100644 --- a/api/http/handler/extensions/extension_create.go +++ b/api/http/handler/extensions/extension_create.go @@ -36,7 +36,7 @@ func (handler *Handler) extensionCreate(w http.ResponseWriter, r *http.Request) } extensionID := portainer.ExtensionID(extensionIdentifier) - extensions, err := handler.ExtensionService.Extensions() + extensions, err := handler.DataStore.Extension().Extensions() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve extensions status from the database", err} } @@ -77,7 +77,7 @@ func (handler *Handler) extensionCreate(w http.ResponseWriter, r *http.Request) } } - err = handler.ExtensionService.Persist(extension) + err = handler.DataStore.Extension().Persist(extension) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist extension status inside the database", err} } diff --git a/api/http/handler/extensions/extension_delete.go b/api/http/handler/extensions/extension_delete.go index 3f9853016..789fa84fd 100644 --- a/api/http/handler/extensions/extension_delete.go +++ b/api/http/handler/extensions/extension_delete.go @@ -17,7 +17,7 @@ func (handler *Handler) extensionDelete(w http.ResponseWriter, r *http.Request) } extensionID := portainer.ExtensionID(extensionIdentifier) - extension, err := handler.ExtensionService.Extension(extensionID) + extension, err := handler.DataStore.Extension().Extension(extensionID) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a extension with the specified identifier inside the database", err} } else if err != nil { @@ -36,7 +36,7 @@ func (handler *Handler) extensionDelete(w http.ResponseWriter, r *http.Request) } } - err = handler.ExtensionService.DeleteExtension(extensionID) + err = handler.DataStore.Extension().DeleteExtension(extensionID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to delete the extension from the database", err} } diff --git a/api/http/handler/extensions/extension_inspect.go b/api/http/handler/extensions/extension_inspect.go index 6af8b1940..94c8292fe 100644 --- a/api/http/handler/extensions/extension_inspect.go +++ b/api/http/handler/extensions/extension_inspect.go @@ -25,7 +25,7 @@ func (handler *Handler) extensionInspect(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve extensions informations", err} } - localExtension, err := handler.ExtensionService.Extension(extensionID) + localExtension, err := handler.DataStore.Extension().Extension(extensionID) if err != nil && err != portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve extension information from the database", err} } diff --git a/api/http/handler/extensions/extension_list.go b/api/http/handler/extensions/extension_list.go index e96e103c8..661f7b7df 100644 --- a/api/http/handler/extensions/extension_list.go +++ b/api/http/handler/extensions/extension_list.go @@ -12,7 +12,7 @@ import ( func (handler *Handler) extensionList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { fetchManifestInformation, _ := request.RetrieveBooleanQueryParameter(r, "store", true) - extensions, err := handler.ExtensionService.Extensions() + extensions, err := handler.DataStore.Extension().Extensions() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve extensions from the database", err} } diff --git a/api/http/handler/extensions/extension_update.go b/api/http/handler/extensions/extension_update.go index b51bf93ba..38487a2f7 100644 --- a/api/http/handler/extensions/extension_update.go +++ b/api/http/handler/extensions/extension_update.go @@ -35,7 +35,7 @@ func (handler *Handler) extensionUpdate(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - extension, err := handler.ExtensionService.Extension(extensionID) + extension, err := handler.DataStore.Extension().Extension(extensionID) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a extension with the specified identifier inside the database", err} } else if err != nil { @@ -47,7 +47,7 @@ func (handler *Handler) extensionUpdate(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusInternalServerError, "Unable to update extension", err} } - err = handler.ExtensionService.Persist(extension) + err = handler.DataStore.Extension().Persist(extension) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist extension status inside the database", err} } diff --git a/api/http/handler/extensions/extension_upload.go b/api/http/handler/extensions/extension_upload.go index 46d403fc6..9469726be 100644 --- a/api/http/handler/extensions/extension_upload.go +++ b/api/http/handler/extensions/extension_upload.go @@ -66,7 +66,7 @@ func (handler *Handler) extensionUpload(w http.ResponseWriter, r *http.Request) } } - err = handler.ExtensionService.Persist(extension) + err = handler.DataStore.Extension().Persist(extension) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist extension status inside the database", err} } diff --git a/api/http/handler/extensions/handler.go b/api/http/handler/extensions/handler.go index 15df2ce17..cba6b354c 100644 --- a/api/http/handler/extensions/handler.go +++ b/api/http/handler/extensions/handler.go @@ -14,11 +14,8 @@ import ( // Handler is the HTTP handler used to handle extension operations. type Handler struct { *mux.Router - ExtensionService portainer.ExtensionService + DataStore portainer.DataStore ExtensionManager portainer.ExtensionManager - EndpointGroupService portainer.EndpointGroupService - EndpointService portainer.EndpointService - RegistryService portainer.RegistryService AuthorizationService *portainer.AuthorizationService } diff --git a/api/http/handler/registries/handler.go b/api/http/handler/registries/handler.go index 3b2646dcb..74a933586 100644 --- a/api/http/handler/registries/handler.go +++ b/api/http/handler/registries/handler.go @@ -18,11 +18,10 @@ func hideFields(registry *portainer.Registry) { // Handler is the HTTP handler used to handle registry operations. type Handler struct { *mux.Router - requestBouncer *security.RequestBouncer - RegistryService portainer.RegistryService - ExtensionService portainer.ExtensionService - FileService portainer.FileService - ProxyManager *proxy.Manager + requestBouncer *security.RequestBouncer + DataStore portainer.DataStore + FileService portainer.FileService + ProxyManager *proxy.Manager } // NewHandler creates a handler to manage registry operations. diff --git a/api/http/handler/registries/proxy.go b/api/http/handler/registries/proxy.go index 3f94bed4a..0bc996d95 100644 --- a/api/http/handler/registries/proxy.go +++ b/api/http/handler/registries/proxy.go @@ -17,7 +17,7 @@ func (handler *Handler) proxyRequestsToRegistryAPI(w http.ResponseWriter, r *htt return &httperror.HandlerError{http.StatusBadRequest, "Invalid registry identifier route variable", err} } - registry, err := handler.RegistryService.Registry(portainer.RegistryID(registryID)) + registry, err := handler.DataStore.Registry().Registry(portainer.RegistryID(registryID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a registry with the specified identifier inside the database", err} } else if err != nil { @@ -29,7 +29,7 @@ func (handler *Handler) proxyRequestsToRegistryAPI(w http.ResponseWriter, r *htt return &httperror.HandlerError{http.StatusForbidden, "Permission denied to access registry", portainer.ErrEndpointAccessDenied} } - extension, err := handler.ExtensionService.Extension(portainer.RegistryManagementExtension) + extension, err := handler.DataStore.Extension().Extension(portainer.RegistryManagementExtension) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Registry management extension is not enabled", err} } else if err != nil { diff --git a/api/http/handler/registries/proxy_management_gitlab.go b/api/http/handler/registries/proxy_management_gitlab.go index 28f1ead12..c9dcbfac0 100644 --- a/api/http/handler/registries/proxy_management_gitlab.go +++ b/api/http/handler/registries/proxy_management_gitlab.go @@ -17,7 +17,7 @@ func (handler *Handler) proxyRequestsToGitlabAPIWithRegistry(w http.ResponseWrit return &httperror.HandlerError{http.StatusBadRequest, "Invalid registry identifier route variable", err} } - registry, err := handler.RegistryService.Registry(portainer.RegistryID(registryID)) + registry, err := handler.DataStore.Registry().Registry(portainer.RegistryID(registryID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a registry with the specified identifier inside the database", err} } else if err != nil { @@ -29,7 +29,7 @@ func (handler *Handler) proxyRequestsToGitlabAPIWithRegistry(w http.ResponseWrit return &httperror.HandlerError{http.StatusForbidden, "Permission denied to access registry", portainer.ErrEndpointAccessDenied} } - extension, err := handler.ExtensionService.Extension(portainer.RegistryManagementExtension) + extension, err := handler.DataStore.Extension().Extension(portainer.RegistryManagementExtension) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Registry management extension is not enabled", err} } else if err != nil { diff --git a/api/http/handler/registries/registry_configure.go b/api/http/handler/registries/registry_configure.go index c967b6996..21d2f92f8 100644 --- a/api/http/handler/registries/registry_configure.go +++ b/api/http/handler/registries/registry_configure.go @@ -78,7 +78,7 @@ func (handler *Handler) registryConfigure(w http.ResponseWriter, r *http.Request return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - registry, err := handler.RegistryService.Registry(portainer.RegistryID(registryID)) + registry, err := handler.DataStore.Registry().Registry(portainer.RegistryID(registryID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a registry with the specified identifier inside the database", err} } else if err != nil { @@ -128,7 +128,7 @@ func (handler *Handler) registryConfigure(w http.ResponseWriter, r *http.Request } } - err = handler.RegistryService.UpdateRegistry(registry.ID, registry) + err = handler.DataStore.Registry().UpdateRegistry(registry.ID, registry) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist registry changes inside the database", err} } diff --git a/api/http/handler/registries/registry_create.go b/api/http/handler/registries/registry_create.go index 09f6d0a2e..aac899138 100644 --- a/api/http/handler/registries/registry_create.go +++ b/api/http/handler/registries/registry_create.go @@ -55,7 +55,7 @@ func (handler *Handler) registryCreate(w http.ResponseWriter, r *http.Request) * Gitlab: payload.Gitlab, } - err = handler.RegistryService.CreateRegistry(registry) + err = handler.DataStore.Registry().CreateRegistry(registry) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the registry inside the database", err} } diff --git a/api/http/handler/registries/registry_delete.go b/api/http/handler/registries/registry_delete.go index ebb833ab4..62e0c663f 100644 --- a/api/http/handler/registries/registry_delete.go +++ b/api/http/handler/registries/registry_delete.go @@ -16,14 +16,14 @@ func (handler *Handler) registryDelete(w http.ResponseWriter, r *http.Request) * return &httperror.HandlerError{http.StatusBadRequest, "Invalid registry identifier route variable", err} } - _, err = handler.RegistryService.Registry(portainer.RegistryID(registryID)) + _, err = handler.DataStore.Registry().Registry(portainer.RegistryID(registryID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a registry with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a registry with the specified identifier inside the database", err} } - err = handler.RegistryService.DeleteRegistry(portainer.RegistryID(registryID)) + err = handler.DataStore.Registry().DeleteRegistry(portainer.RegistryID(registryID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove the registry from the database", err} } diff --git a/api/http/handler/registries/registry_inspect.go b/api/http/handler/registries/registry_inspect.go index d40ef693a..586f198db 100644 --- a/api/http/handler/registries/registry_inspect.go +++ b/api/http/handler/registries/registry_inspect.go @@ -16,7 +16,7 @@ func (handler *Handler) registryInspect(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid registry identifier route variable", err} } - registry, err := handler.RegistryService.Registry(portainer.RegistryID(registryID)) + registry, err := handler.DataStore.Registry().Registry(portainer.RegistryID(registryID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a registry with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/registries/registry_list.go b/api/http/handler/registries/registry_list.go index b78763375..1acd380f4 100644 --- a/api/http/handler/registries/registry_list.go +++ b/api/http/handler/registries/registry_list.go @@ -10,7 +10,7 @@ import ( // GET request on /api/registries func (handler *Handler) registryList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - registries, err := handler.RegistryService.Registries() + registries, err := handler.DataStore.Registry().Registries() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve registries from the database", err} } diff --git a/api/http/handler/registries/registry_update.go b/api/http/handler/registries/registry_update.go index 15dd2d9dc..cd4255140 100644 --- a/api/http/handler/registries/registry_update.go +++ b/api/http/handler/registries/registry_update.go @@ -36,7 +36,7 @@ func (handler *Handler) registryUpdate(w http.ResponseWriter, r *http.Request) * return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - registry, err := handler.RegistryService.Registry(portainer.RegistryID(registryID)) + registry, err := handler.DataStore.Registry().Registry(portainer.RegistryID(registryID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a registry with the specified identifier inside the database", err} } else if err != nil { @@ -48,7 +48,7 @@ func (handler *Handler) registryUpdate(w http.ResponseWriter, r *http.Request) * } if payload.URL != nil { - registries, err := handler.RegistryService.Registries() + registries, err := handler.DataStore.Registry().Registries() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve registries from the database", err} } @@ -88,7 +88,7 @@ func (handler *Handler) registryUpdate(w http.ResponseWriter, r *http.Request) * registry.TeamAccessPolicies = payload.TeamAccessPolicies } - err = handler.RegistryService.UpdateRegistry(registry.ID, registry) + err = handler.DataStore.Registry().UpdateRegistry(registry.ID, registry) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist registry changes inside the database", err} } diff --git a/api/http/handler/resourcecontrols/handler.go b/api/http/handler/resourcecontrols/handler.go index e6851c649..d0dc65b19 100644 --- a/api/http/handler/resourcecontrols/handler.go +++ b/api/http/handler/resourcecontrols/handler.go @@ -12,7 +12,7 @@ import ( // Handler is the HTTP handler used to handle resource control operations. type Handler struct { *mux.Router - ResourceControlService portainer.ResourceControlService + DataStore portainer.DataStore } // NewHandler creates a handler to manage resource control operations. diff --git a/api/http/handler/resourcecontrols/resourcecontrol_create.go b/api/http/handler/resourcecontrols/resourcecontrol_create.go index 0a6696bd9..5f56daa19 100644 --- a/api/http/handler/resourcecontrols/resourcecontrol_create.go +++ b/api/http/handler/resourcecontrols/resourcecontrol_create.go @@ -68,7 +68,7 @@ func (handler *Handler) resourceControlCreate(w http.ResponseWriter, r *http.Req return &httperror.HandlerError{http.StatusBadRequest, "Invalid type value. Value must be one of: container, service, volume, network, secret, stack or config", portainer.ErrInvalidResourceControlType} } - rc, err := handler.ResourceControlService.ResourceControlByResourceIDAndType(payload.ResourceID, resourceControlType) + rc, err := handler.DataStore.ResourceControl().ResourceControlByResourceIDAndType(payload.ResourceID, resourceControlType) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve resource controls from the database", err} } @@ -104,7 +104,7 @@ func (handler *Handler) resourceControlCreate(w http.ResponseWriter, r *http.Req TeamAccesses: teamAccesses, } - err = handler.ResourceControlService.CreateResourceControl(&resourceControl) + err = handler.DataStore.ResourceControl().CreateResourceControl(&resourceControl) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the resource control inside the database", err} } diff --git a/api/http/handler/resourcecontrols/resourcecontrol_delete.go b/api/http/handler/resourcecontrols/resourcecontrol_delete.go index 76794e423..076e86519 100644 --- a/api/http/handler/resourcecontrols/resourcecontrol_delete.go +++ b/api/http/handler/resourcecontrols/resourcecontrol_delete.go @@ -16,14 +16,14 @@ func (handler *Handler) resourceControlDelete(w http.ResponseWriter, r *http.Req return &httperror.HandlerError{http.StatusBadRequest, "Invalid resource control identifier route variable", err} } - _, err = handler.ResourceControlService.ResourceControl(portainer.ResourceControlID(resourceControlID)) + _, err = handler.DataStore.ResourceControl().ResourceControl(portainer.ResourceControlID(resourceControlID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a resource control with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a resource control with with the specified identifier inside the database", err} } - err = handler.ResourceControlService.DeleteResourceControl(portainer.ResourceControlID(resourceControlID)) + err = handler.DataStore.ResourceControl().DeleteResourceControl(portainer.ResourceControlID(resourceControlID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove the resource control from the database", err} } diff --git a/api/http/handler/resourcecontrols/resourcecontrol_update.go b/api/http/handler/resourcecontrols/resourcecontrol_update.go index fc170f1bd..9e5f52752 100644 --- a/api/http/handler/resourcecontrols/resourcecontrol_update.go +++ b/api/http/handler/resourcecontrols/resourcecontrol_update.go @@ -42,7 +42,7 @@ func (handler *Handler) resourceControlUpdate(w http.ResponseWriter, r *http.Req return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - resourceControl, err := handler.ResourceControlService.ResourceControl(portainer.ResourceControlID(resourceControlID)) + resourceControl, err := handler.DataStore.ResourceControl().ResourceControl(portainer.ResourceControlID(resourceControlID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a resource control with the specified identifier inside the database", err} } else if err != nil { @@ -85,7 +85,7 @@ func (handler *Handler) resourceControlUpdate(w http.ResponseWriter, r *http.Req return &httperror.HandlerError{http.StatusForbidden, "Permission denied to update the resource control", portainer.ErrResourceAccessDenied} } - err = handler.ResourceControlService.UpdateResourceControl(resourceControl.ID, resourceControl) + err = handler.DataStore.ResourceControl().UpdateResourceControl(resourceControl.ID, resourceControl) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist resource control changes inside the database", err} } diff --git a/api/http/handler/roles/handler.go b/api/http/handler/roles/handler.go index 89ec52452..a4a709562 100644 --- a/api/http/handler/roles/handler.go +++ b/api/http/handler/roles/handler.go @@ -12,7 +12,7 @@ import ( // Handler is the HTTP handler used to handle role operations. type Handler struct { *mux.Router - RoleService portainer.RoleService + DataStore portainer.DataStore } // NewHandler creates a handler to manage role operations. diff --git a/api/http/handler/roles/role_list.go b/api/http/handler/roles/role_list.go index e39e38595..11817c2f3 100644 --- a/api/http/handler/roles/role_list.go +++ b/api/http/handler/roles/role_list.go @@ -9,7 +9,7 @@ import ( // GET request on /api/Role func (handler *Handler) roleList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - roles, err := handler.RoleService.Roles() + roles, err := handler.DataStore.Role().Roles() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve authorization sets from the database", err} } diff --git a/api/http/handler/schedules/handler.go b/api/http/handler/schedules/handler.go index cc7d3dbf2..2d4382bf3 100644 --- a/api/http/handler/schedules/handler.go +++ b/api/http/handler/schedules/handler.go @@ -12,9 +12,7 @@ import ( // Handler is the HTTP handler used to handle schedule operations. type Handler struct { *mux.Router - ScheduleService portainer.ScheduleService - EndpointService portainer.EndpointService - SettingsService portainer.SettingsService + DataStore portainer.DataStore FileService portainer.FileService JobService portainer.JobService JobScheduler portainer.JobScheduler diff --git a/api/http/handler/schedules/schedule_create.go b/api/http/handler/schedules/schedule_create.go index 196913a33..9e54bbcab 100644 --- a/api/http/handler/schedules/schedule_create.go +++ b/api/http/handler/schedules/schedule_create.go @@ -117,7 +117,7 @@ func (payload *scheduleCreateFromFileContentPayload) Validate(r *http.Request) e // POST /api/schedules?method=file|string func (handler *Handler) scheduleCreate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusServiceUnavailable, "Unable to retrieve settings", err} } @@ -175,7 +175,7 @@ func (handler *Handler) createScheduleFromFile(w http.ResponseWriter, r *http.Re } func (handler *Handler) createScheduleObjectFromFilePayload(payload *scheduleCreateFromFilePayload) *portainer.Schedule { - scheduleIdentifier := portainer.ScheduleID(handler.ScheduleService.GetNextIdentifier()) + scheduleIdentifier := portainer.ScheduleID(handler.DataStore.Schedule().GetNextIdentifier()) job := &portainer.ScriptExecutionJob{ Endpoints: payload.Endpoints, @@ -198,7 +198,7 @@ func (handler *Handler) createScheduleObjectFromFilePayload(payload *scheduleCre } func (handler *Handler) createScheduleObjectFromFileContentPayload(payload *scheduleCreateFromFileContentPayload) *portainer.Schedule { - scheduleIdentifier := portainer.ScheduleID(handler.ScheduleService.GetNextIdentifier()) + scheduleIdentifier := portainer.ScheduleID(handler.DataStore.Schedule().GetNextIdentifier()) job := &portainer.ScriptExecutionJob{ Endpoints: payload.Endpoints, @@ -231,7 +231,7 @@ func (handler *Handler) addAndPersistSchedule(schedule *portainer.Schedule, file for _, ID := range schedule.ScriptExecutionJob.Endpoints { - endpoint, err := handler.EndpointService.Endpoint(ID) + endpoint, err := handler.DataStore.Endpoint().Endpoint(ID) if err != nil { return err } @@ -268,7 +268,7 @@ func (handler *Handler) addAndPersistSchedule(schedule *portainer.Schedule, file schedule.ScriptExecutionJob.ScriptPath = scriptPath - jobContext := cron.NewScriptExecutionJobContext(handler.JobService, handler.EndpointService, handler.FileService) + jobContext := cron.NewScriptExecutionJobContext(handler.JobService, handler.DataStore, handler.FileService) jobRunner := cron.NewScriptExecutionJobRunner(schedule, jobContext) err = handler.JobScheduler.ScheduleJob(jobRunner) @@ -276,5 +276,5 @@ func (handler *Handler) addAndPersistSchedule(schedule *portainer.Schedule, file return err } - return handler.ScheduleService.CreateSchedule(schedule) + return handler.DataStore.Schedule().CreateSchedule(schedule) } diff --git a/api/http/handler/schedules/schedule_delete.go b/api/http/handler/schedules/schedule_delete.go index c30b01696..8970e35bf 100644 --- a/api/http/handler/schedules/schedule_delete.go +++ b/api/http/handler/schedules/schedule_delete.go @@ -12,7 +12,7 @@ import ( ) func (handler *Handler) scheduleDelete(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusServiceUnavailable, "Unable to retrieve settings", err} } @@ -25,7 +25,7 @@ func (handler *Handler) scheduleDelete(w http.ResponseWriter, r *http.Request) * return &httperror.HandlerError{http.StatusBadRequest, "Invalid schedule identifier route variable", err} } - schedule, err := handler.ScheduleService.Schedule(portainer.ScheduleID(scheduleID)) + schedule, err := handler.DataStore.Schedule().Schedule(portainer.ScheduleID(scheduleID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a schedule with the specified identifier inside the database", err} } else if err != nil { @@ -46,7 +46,7 @@ func (handler *Handler) scheduleDelete(w http.ResponseWriter, r *http.Request) * handler.JobScheduler.UnscheduleJob(schedule.ID) - err = handler.ScheduleService.DeleteSchedule(portainer.ScheduleID(scheduleID)) + err = handler.DataStore.Schedule().DeleteSchedule(portainer.ScheduleID(scheduleID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove the schedule from the database", err} } diff --git a/api/http/handler/schedules/schedule_file.go b/api/http/handler/schedules/schedule_file.go index c10b698dd..263ff6eb2 100644 --- a/api/http/handler/schedules/schedule_file.go +++ b/api/http/handler/schedules/schedule_file.go @@ -16,7 +16,7 @@ type scheduleFileResponse struct { // GET request on /api/schedules/:id/file func (handler *Handler) scheduleFile(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusServiceUnavailable, "Unable to retrieve settings", err} } @@ -29,7 +29,7 @@ func (handler *Handler) scheduleFile(w http.ResponseWriter, r *http.Request) *ht return &httperror.HandlerError{http.StatusBadRequest, "Invalid schedule identifier route variable", err} } - schedule, err := handler.ScheduleService.Schedule(portainer.ScheduleID(scheduleID)) + schedule, err := handler.DataStore.Schedule().Schedule(portainer.ScheduleID(scheduleID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a schedule with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/schedules/schedule_inspect.go b/api/http/handler/schedules/schedule_inspect.go index 594c29b1a..9067c81f7 100644 --- a/api/http/handler/schedules/schedule_inspect.go +++ b/api/http/handler/schedules/schedule_inspect.go @@ -11,7 +11,7 @@ import ( ) func (handler *Handler) scheduleInspect(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusServiceUnavailable, "Unable to retrieve settings", err} } @@ -24,7 +24,7 @@ func (handler *Handler) scheduleInspect(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid schedule identifier route variable", err} } - schedule, err := handler.ScheduleService.Schedule(portainer.ScheduleID(scheduleID)) + schedule, err := handler.DataStore.Schedule().Schedule(portainer.ScheduleID(scheduleID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a schedule with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/schedules/schedule_list.go b/api/http/handler/schedules/schedule_list.go index 55662dc0e..399e7e1bb 100644 --- a/api/http/handler/schedules/schedule_list.go +++ b/api/http/handler/schedules/schedule_list.go @@ -10,7 +10,7 @@ import ( // GET request on /api/schedules func (handler *Handler) scheduleList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusServiceUnavailable, "Unable to retrieve settings", err} } @@ -18,7 +18,7 @@ func (handler *Handler) scheduleList(w http.ResponseWriter, r *http.Request) *ht return &httperror.HandlerError{http.StatusServiceUnavailable, "Host management features are disabled", portainer.ErrHostManagementFeaturesDisabled} } - schedules, err := handler.ScheduleService.Schedules() + schedules, err := handler.DataStore.Schedule().Schedules() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve schedules from the database", err} } diff --git a/api/http/handler/schedules/schedule_tasks.go b/api/http/handler/schedules/schedule_tasks.go index a4993e6cd..48dfc35aa 100644 --- a/api/http/handler/schedules/schedule_tasks.go +++ b/api/http/handler/schedules/schedule_tasks.go @@ -24,7 +24,7 @@ type taskContainer struct { // GET request on /api/schedules/:id/tasks func (handler *Handler) scheduleTasks(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusServiceUnavailable, "Unable to retrieve settings", err} } @@ -37,7 +37,7 @@ func (handler *Handler) scheduleTasks(w http.ResponseWriter, r *http.Request) *h return &httperror.HandlerError{http.StatusBadRequest, "Invalid schedule identifier route variable", err} } - schedule, err := handler.ScheduleService.Schedule(portainer.ScheduleID(scheduleID)) + schedule, err := handler.DataStore.Schedule().Schedule(portainer.ScheduleID(scheduleID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a schedule with the specified identifier inside the database", err} } else if err != nil { @@ -51,7 +51,7 @@ func (handler *Handler) scheduleTasks(w http.ResponseWriter, r *http.Request) *h tasks := make([]taskContainer, 0) for _, endpointID := range schedule.ScriptExecutionJob.Endpoints { - endpoint, err := handler.EndpointService.Endpoint(endpointID) + endpoint, err := handler.DataStore.Endpoint().Endpoint(endpointID) if err == portainer.ErrObjectNotFound { continue } else if err != nil { diff --git a/api/http/handler/schedules/schedule_update.go b/api/http/handler/schedules/schedule_update.go index f68e77126..2bf2faf39 100644 --- a/api/http/handler/schedules/schedule_update.go +++ b/api/http/handler/schedules/schedule_update.go @@ -33,7 +33,7 @@ func (payload *scheduleUpdatePayload) Validate(r *http.Request) error { } func (handler *Handler) scheduleUpdate(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusServiceUnavailable, "Unable to retrieve settings", err} } @@ -52,7 +52,7 @@ func (handler *Handler) scheduleUpdate(w http.ResponseWriter, r *http.Request) * return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - schedule, err := handler.ScheduleService.Schedule(portainer.ScheduleID(scheduleID)) + schedule, err := handler.DataStore.Schedule().Schedule(portainer.ScheduleID(scheduleID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a schedule with the specified identifier inside the database", err} } else if err != nil { @@ -78,7 +78,7 @@ func (handler *Handler) scheduleUpdate(w http.ResponseWriter, r *http.Request) * } if updateJobSchedule { - jobContext := cron.NewScriptExecutionJobContext(handler.JobService, handler.EndpointService, handler.FileService) + jobContext := cron.NewScriptExecutionJobContext(handler.JobService, handler.DataStore, handler.FileService) jobRunner := cron.NewScriptExecutionJobRunner(schedule, jobContext) err := handler.JobScheduler.UpdateJobSchedule(jobRunner) if err != nil { @@ -86,7 +86,7 @@ func (handler *Handler) scheduleUpdate(w http.ResponseWriter, r *http.Request) * } } - err = handler.ScheduleService.UpdateSchedule(portainer.ScheduleID(scheduleID), schedule) + err = handler.DataStore.Schedule().UpdateSchedule(portainer.ScheduleID(scheduleID), schedule) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist schedule changes inside the database", err} } @@ -104,7 +104,7 @@ func (handler *Handler) updateEdgeSchedule(schedule *portainer.Schedule, payload edgeEndpointIDs := make([]portainer.EndpointID, 0) for _, ID := range payload.Endpoints { - endpoint, err := handler.EndpointService.Endpoint(ID) + endpoint, err := handler.DataStore.Endpoint().Endpoint(ID) if err != nil { return err } diff --git a/api/http/handler/settings/handler.go b/api/http/handler/settings/handler.go index 1f688f343..349d05228 100644 --- a/api/http/handler/settings/handler.go +++ b/api/http/handler/settings/handler.go @@ -17,13 +17,10 @@ func hideFields(settings *portainer.Settings) { // Handler is the HTTP handler used to handle settings operations. type Handler struct { *mux.Router - SettingsService portainer.SettingsService + DataStore portainer.DataStore LDAPService portainer.LDAPService FileService portainer.FileService JobScheduler portainer.JobScheduler - ScheduleService portainer.ScheduleService - RoleService portainer.RoleService - ExtensionService portainer.ExtensionService AuthorizationService *portainer.AuthorizationService } diff --git a/api/http/handler/settings/settings_inspect.go b/api/http/handler/settings/settings_inspect.go index a28b1ef09..0e732ee43 100644 --- a/api/http/handler/settings/settings_inspect.go +++ b/api/http/handler/settings/settings_inspect.go @@ -9,7 +9,7 @@ import ( // GET request on /api/settings func (handler *Handler) settingsInspect(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve the settings from the database", err} } diff --git a/api/http/handler/settings/settings_public.go b/api/http/handler/settings/settings_public.go index a5cd0ac57..5af534d45 100644 --- a/api/http/handler/settings/settings_public.go +++ b/api/http/handler/settings/settings_public.go @@ -22,7 +22,7 @@ type publicSettingsResponse struct { // GET request on /api/settings/public func (handler *Handler) settingsPublic(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve the settings from the database", err} } diff --git a/api/http/handler/settings/settings_update.go b/api/http/handler/settings/settings_update.go index cef3bdf0f..86d26bdaa 100644 --- a/api/http/handler/settings/settings_update.go +++ b/api/http/handler/settings/settings_update.go @@ -48,7 +48,7 @@ func (handler *Handler) settingsUpdate(w http.ResponseWriter, r *http.Request) * return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve the settings from the database", err} } @@ -130,7 +130,7 @@ func (handler *Handler) settingsUpdate(w http.ResponseWriter, r *http.Request) * return tlsError } - err = handler.SettingsService.UpdateSettings(settings) + err = handler.DataStore.Settings().UpdateSettings(settings) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist settings changes inside the database", err} } @@ -151,7 +151,7 @@ func (handler *Handler) updateVolumeBrowserSetting(settings *portainer.Settings) return err } - extension, err := handler.ExtensionService.Extension(portainer.RBACExtension) + extension, err := handler.DataStore.Extension().Extension(portainer.RBACExtension) if err != nil && err != portainer.ErrObjectNotFound { return err } @@ -169,7 +169,7 @@ func (handler *Handler) updateVolumeBrowserSetting(settings *portainer.Settings) func (handler *Handler) updateSnapshotInterval(settings *portainer.Settings, snapshotInterval string) error { settings.SnapshotInterval = snapshotInterval - schedules, err := handler.ScheduleService.SchedulesByJobType(portainer.SnapshotJobType) + schedules, err := handler.DataStore.Schedule().SchedulesByJobType(portainer.SnapshotJobType) if err != nil { return err } @@ -183,7 +183,7 @@ func (handler *Handler) updateSnapshotInterval(settings *portainer.Settings, sna return err } - err = handler.ScheduleService.UpdateSchedule(snapshotSchedule.ID, &snapshotSchedule) + err = handler.DataStore.Schedule().UpdateSchedule(snapshotSchedule.ID, &snapshotSchedule) if err != nil { return err } diff --git a/api/http/handler/stacks/create_compose_stack.go b/api/http/handler/stacks/create_compose_stack.go index ebdcea252..051999980 100644 --- a/api/http/handler/stacks/create_compose_stack.go +++ b/api/http/handler/stacks/create_compose_stack.go @@ -47,7 +47,7 @@ func (handler *Handler) createComposeStackFromFileContent(w http.ResponseWriter, return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - stacks, err := handler.StackService.Stacks() + stacks, err := handler.DataStore.Stack().Stacks() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve stacks from the database", err} } @@ -58,7 +58,7 @@ func (handler *Handler) createComposeStackFromFileContent(w http.ResponseWriter, } } - stackID := handler.StackService.GetNextIdentifier() + stackID := handler.DataStore.Stack().GetNextIdentifier() stack := &portainer.Stack{ ID: portainer.StackID(stackID), Name: payload.Name, @@ -88,7 +88,7 @@ func (handler *Handler) createComposeStackFromFileContent(w http.ResponseWriter, return &httperror.HandlerError{http.StatusInternalServerError, err.Error(), err} } - err = handler.StackService.CreateStack(stack) + err = handler.DataStore.Stack().CreateStack(stack) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the stack inside the database", err} } @@ -132,7 +132,7 @@ func (handler *Handler) createComposeStackFromGitRepository(w http.ResponseWrite return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - stacks, err := handler.StackService.Stacks() + stacks, err := handler.DataStore.Stack().Stacks() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve stacks from the database", err} } @@ -143,7 +143,7 @@ func (handler *Handler) createComposeStackFromGitRepository(w http.ResponseWrite } } - stackID := handler.StackService.GetNextIdentifier() + stackID := handler.DataStore.Stack().GetNextIdentifier() stack := &portainer.Stack{ ID: portainer.StackID(stackID), Name: payload.Name, @@ -183,7 +183,7 @@ func (handler *Handler) createComposeStackFromGitRepository(w http.ResponseWrite return &httperror.HandlerError{http.StatusInternalServerError, err.Error(), err} } - err = handler.StackService.CreateStack(stack) + err = handler.DataStore.Stack().CreateStack(stack) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the stack inside the database", err} } @@ -227,7 +227,7 @@ func (handler *Handler) createComposeStackFromFileUpload(w http.ResponseWriter, return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - stacks, err := handler.StackService.Stacks() + stacks, err := handler.DataStore.Stack().Stacks() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve stacks from the database", err} } @@ -238,7 +238,7 @@ func (handler *Handler) createComposeStackFromFileUpload(w http.ResponseWriter, } } - stackID := handler.StackService.GetNextIdentifier() + stackID := handler.DataStore.Stack().GetNextIdentifier() stack := &portainer.Stack{ ID: portainer.StackID(stackID), Name: payload.Name, @@ -268,7 +268,7 @@ func (handler *Handler) createComposeStackFromFileUpload(w http.ResponseWriter, return &httperror.HandlerError{http.StatusInternalServerError, err.Error(), err} } - err = handler.StackService.CreateStack(stack) + err = handler.DataStore.Stack().CreateStack(stack) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the stack inside the database", err} } @@ -291,12 +291,12 @@ func (handler *Handler) createComposeDeployConfig(r *http.Request, stack *portai return nil, &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve info from request context", err} } - dockerhub, err := handler.DockerHubService.DockerHub() + dockerhub, err := handler.DataStore.DockerHub().DockerHub() if err != nil { return nil, &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve DockerHub details from the database", err} } - registries, err := handler.RegistryService.Registries() + registries, err := handler.DataStore.Registry().Registries() if err != nil { return nil, &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve registries from the database", err} } @@ -319,7 +319,7 @@ func (handler *Handler) createComposeDeployConfig(r *http.Request, stack *portai // clean it. Hence the use of the mutex. // We should contribute to libcompose to support authentication without using the config.json file. func (handler *Handler) deployComposeStack(config *composeStackDeploymentConfig) error { - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return err } diff --git a/api/http/handler/stacks/create_swarm_stack.go b/api/http/handler/stacks/create_swarm_stack.go index 143292ea9..09e743e44 100644 --- a/api/http/handler/stacks/create_swarm_stack.go +++ b/api/http/handler/stacks/create_swarm_stack.go @@ -42,7 +42,7 @@ func (handler *Handler) createSwarmStackFromFileContent(w http.ResponseWriter, r return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - stacks, err := handler.StackService.Stacks() + stacks, err := handler.DataStore.Stack().Stacks() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve stacks from the database", err} } @@ -53,7 +53,7 @@ func (handler *Handler) createSwarmStackFromFileContent(w http.ResponseWriter, r } } - stackID := handler.StackService.GetNextIdentifier() + stackID := handler.DataStore.Stack().GetNextIdentifier() stack := &portainer.Stack{ ID: portainer.StackID(stackID), Name: payload.Name, @@ -84,7 +84,7 @@ func (handler *Handler) createSwarmStackFromFileContent(w http.ResponseWriter, r return &httperror.HandlerError{http.StatusInternalServerError, err.Error(), err} } - err = handler.StackService.CreateStack(stack) + err = handler.DataStore.Stack().CreateStack(stack) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the stack inside the database", err} } @@ -131,7 +131,7 @@ func (handler *Handler) createSwarmStackFromGitRepository(w http.ResponseWriter, return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - stacks, err := handler.StackService.Stacks() + stacks, err := handler.DataStore.Stack().Stacks() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve stacks from the database", err} } @@ -142,7 +142,7 @@ func (handler *Handler) createSwarmStackFromGitRepository(w http.ResponseWriter, } } - stackID := handler.StackService.GetNextIdentifier() + stackID := handler.DataStore.Stack().GetNextIdentifier() stack := &portainer.Stack{ ID: portainer.StackID(stackID), Name: payload.Name, @@ -183,7 +183,7 @@ func (handler *Handler) createSwarmStackFromGitRepository(w http.ResponseWriter, return &httperror.HandlerError{http.StatusInternalServerError, err.Error(), err} } - err = handler.StackService.CreateStack(stack) + err = handler.DataStore.Stack().CreateStack(stack) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the stack inside the database", err} } @@ -234,7 +234,7 @@ func (handler *Handler) createSwarmStackFromFileUpload(w http.ResponseWriter, r return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - stacks, err := handler.StackService.Stacks() + stacks, err := handler.DataStore.Stack().Stacks() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve stacks from the database", err} } @@ -245,7 +245,7 @@ func (handler *Handler) createSwarmStackFromFileUpload(w http.ResponseWriter, r } } - stackID := handler.StackService.GetNextIdentifier() + stackID := handler.DataStore.Stack().GetNextIdentifier() stack := &portainer.Stack{ ID: portainer.StackID(stackID), Name: payload.Name, @@ -276,7 +276,7 @@ func (handler *Handler) createSwarmStackFromFileUpload(w http.ResponseWriter, r return &httperror.HandlerError{http.StatusInternalServerError, err.Error(), err} } - err = handler.StackService.CreateStack(stack) + err = handler.DataStore.Stack().CreateStack(stack) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the stack inside the database", err} } @@ -300,12 +300,12 @@ func (handler *Handler) createSwarmDeployConfig(r *http.Request, stack *portaine return nil, &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve info from request context", err} } - dockerhub, err := handler.DockerHubService.DockerHub() + dockerhub, err := handler.DataStore.DockerHub().DockerHub() if err != nil { return nil, &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve DockerHub details from the database", err} } - registries, err := handler.RegistryService.Registries() + registries, err := handler.DataStore.Registry().Registries() if err != nil { return nil, &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve registries from the database", err} } @@ -324,7 +324,7 @@ func (handler *Handler) createSwarmDeployConfig(r *http.Request, stack *portaine } func (handler *Handler) deploySwarmStack(config *swarmStackDeploymentConfig) error { - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return err } diff --git a/api/http/handler/stacks/handler.go b/api/http/handler/stacks/handler.go index d0a6b4ea5..6722feaa8 100644 --- a/api/http/handler/stacks/handler.go +++ b/api/http/handler/stacks/handler.go @@ -16,18 +16,11 @@ type Handler struct { stackDeletionMutex *sync.Mutex requestBouncer *security.RequestBouncer *mux.Router - FileService portainer.FileService - GitService portainer.GitService - StackService portainer.StackService - EndpointService portainer.EndpointService - ResourceControlService portainer.ResourceControlService - RegistryService portainer.RegistryService - DockerHubService portainer.DockerHubService - SwarmStackManager portainer.SwarmStackManager - ComposeStackManager portainer.ComposeStackManager - SettingsService portainer.SettingsService - UserService portainer.UserService - ExtensionService portainer.ExtensionService + DataStore portainer.DataStore + FileService portainer.FileService + GitService portainer.GitService + SwarmStackManager portainer.SwarmStackManager + ComposeStackManager portainer.ComposeStackManager } // NewHandler creates a handler to manage stack operations. @@ -69,14 +62,14 @@ func (handler *Handler) userCanAccessStack(securityContext *security.RestrictedR return true, nil } - _, err := handler.ExtensionService.Extension(portainer.RBACExtension) + _, err := handler.DataStore.Extension().Extension(portainer.RBACExtension) if err == portainer.ErrObjectNotFound { return false, nil } else if err != nil && err != portainer.ErrObjectNotFound { return false, err } - user, err := handler.UserService.User(securityContext.UserID) + user, err := handler.DataStore.User().User(securityContext.UserID) if err != nil { return false, err } diff --git a/api/http/handler/stacks/stack_create.go b/api/http/handler/stacks/stack_create.go index dc374c4b1..7f3244520 100644 --- a/api/http/handler/stacks/stack_create.go +++ b/api/http/handler/stacks/stack_create.go @@ -43,7 +43,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.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -135,7 +135,7 @@ func (handler *Handler) isValidStackFile(stackFileContent []byte) (bool, error) func (handler *Handler) decorateStackResponse(w http.ResponseWriter, stack *portainer.Stack, userID portainer.UserID) *httperror.HandlerError { resourceControl := portainer.NewPrivateResourceControl(stack.Name, portainer.StackResourceControl, userID) - err := handler.ResourceControlService.CreateResourceControl(resourceControl) + err := handler.DataStore.ResourceControl().CreateResourceControl(resourceControl) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist resource control inside the database", err} } diff --git a/api/http/handler/stacks/stack_delete.go b/api/http/handler/stacks/stack_delete.go index da7e82751..cac9b21fe 100644 --- a/api/http/handler/stacks/stack_delete.go +++ b/api/http/handler/stacks/stack_delete.go @@ -36,7 +36,7 @@ func (handler *Handler) stackDelete(w http.ResponseWriter, r *http.Request) *htt return &httperror.HandlerError{http.StatusBadRequest, "Invalid stack identifier route variable", err} } - stack, err := handler.StackService.Stack(portainer.StackID(id)) + stack, err := handler.DataStore.Stack().Stack(portainer.StackID(id)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a stack with the specified identifier inside the database", err} } else if err != nil { @@ -56,7 +56,7 @@ func (handler *Handler) stackDelete(w http.ResponseWriter, r *http.Request) *htt endpointIdentifier = portainer.EndpointID(endpointID) } - endpoint, err := handler.EndpointService.Endpoint(endpointIdentifier) + endpoint, err := handler.DataStore.Endpoint().Endpoint(endpointIdentifier) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find the endpoint associated to the stack inside the database", err} } else if err != nil { @@ -68,7 +68,7 @@ func (handler *Handler) stackDelete(w http.ResponseWriter, r *http.Request) *htt return &httperror.HandlerError{http.StatusForbidden, "Permission denied to access endpoint", err} } - resourceControl, err := handler.ResourceControlService.ResourceControlByResourceIDAndType(stack.Name, portainer.StackResourceControl) + resourceControl, err := handler.DataStore.ResourceControl().ResourceControlByResourceIDAndType(stack.Name, portainer.StackResourceControl) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve a resource control associated to the stack", err} } @@ -86,13 +86,13 @@ func (handler *Handler) stackDelete(w http.ResponseWriter, r *http.Request) *htt return &httperror.HandlerError{http.StatusInternalServerError, err.Error(), err} } - err = handler.StackService.DeleteStack(portainer.StackID(id)) + err = handler.DataStore.Stack().DeleteStack(portainer.StackID(id)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove the stack from the database", err} } if resourceControl != nil { - err = handler.ResourceControlService.DeleteResourceControl(resourceControl.ID) + err = handler.DataStore.ResourceControl().DeleteResourceControl(resourceControl.ID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove the associated resource control from the database", err} } @@ -112,12 +112,12 @@ func (handler *Handler) deleteExternalStack(r *http.Request, w http.ResponseWrit return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: endpointId", err} } - user, err := handler.UserService.User(securityContext.UserID) + user, err := handler.DataStore.User().User(securityContext.UserID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to load user information from the database", err} } - rbacExtension, err := handler.ExtensionService.Extension(portainer.RBACExtension) + rbacExtension, err := handler.DataStore.Extension().Extension(portainer.RBACExtension) if err != nil && err != portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to verify if RBAC extension is loaded", err} } @@ -138,7 +138,7 @@ func (handler *Handler) deleteExternalStack(r *http.Request, w http.ResponseWrit } } - stack, err := handler.StackService.StackByName(stackName) + stack, err := handler.DataStore.Stack().StackByName(stackName) if err != nil && err != portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to check for stack existence inside the database", err} } @@ -146,7 +146,7 @@ func (handler *Handler) deleteExternalStack(r *http.Request, w http.ResponseWrit return &httperror.HandlerError{http.StatusBadRequest, "A stack with this name exists inside the database. Cannot use external delete method", portainer.ErrStackNotExternal} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find the endpoint associated to the stack inside the database", err} } else if err != nil { diff --git a/api/http/handler/stacks/stack_file.go b/api/http/handler/stacks/stack_file.go index 7c966a08e..7c601a263 100644 --- a/api/http/handler/stacks/stack_file.go +++ b/api/http/handler/stacks/stack_file.go @@ -22,14 +22,14 @@ func (handler *Handler) stackFile(w http.ResponseWriter, r *http.Request) *httpe return &httperror.HandlerError{http.StatusBadRequest, "Invalid stack identifier route variable", err} } - stack, err := handler.StackService.Stack(portainer.StackID(stackID)) + stack, err := handler.DataStore.Stack().Stack(portainer.StackID(stackID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a stack with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a stack with the specified identifier inside the database", err} } - endpoint, err := handler.EndpointService.Endpoint(stack.EndpointID) + endpoint, err := handler.DataStore.Endpoint().Endpoint(stack.EndpointID) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -41,7 +41,7 @@ func (handler *Handler) stackFile(w http.ResponseWriter, r *http.Request) *httpe return &httperror.HandlerError{http.StatusForbidden, "Permission denied to access endpoint", err} } - resourceControl, err := handler.ResourceControlService.ResourceControlByResourceIDAndType(stack.Name, portainer.StackResourceControl) + resourceControl, err := handler.DataStore.ResourceControl().ResourceControlByResourceIDAndType(stack.Name, portainer.StackResourceControl) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve a resource control associated to the stack", err} } diff --git a/api/http/handler/stacks/stack_inspect.go b/api/http/handler/stacks/stack_inspect.go index 42d0ad9c5..eb41dc793 100644 --- a/api/http/handler/stacks/stack_inspect.go +++ b/api/http/handler/stacks/stack_inspect.go @@ -17,14 +17,14 @@ func (handler *Handler) stackInspect(w http.ResponseWriter, r *http.Request) *ht return &httperror.HandlerError{http.StatusBadRequest, "Invalid stack identifier route variable", err} } - stack, err := handler.StackService.Stack(portainer.StackID(stackID)) + stack, err := handler.DataStore.Stack().Stack(portainer.StackID(stackID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a stack with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a stack with the specified identifier inside the database", err} } - endpoint, err := handler.EndpointService.Endpoint(stack.EndpointID) + endpoint, err := handler.DataStore.Endpoint().Endpoint(stack.EndpointID) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -41,7 +41,7 @@ func (handler *Handler) stackInspect(w http.ResponseWriter, r *http.Request) *ht return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve user info from request context", err} } - resourceControl, err := handler.ResourceControlService.ResourceControlByResourceIDAndType(stack.Name, portainer.StackResourceControl) + resourceControl, err := handler.DataStore.ResourceControl().ResourceControlByResourceIDAndType(stack.Name, portainer.StackResourceControl) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve a resource control associated to the stack", err} } diff --git a/api/http/handler/stacks/stack_list.go b/api/http/handler/stacks/stack_list.go index 2c87cb40b..eab7d7ca1 100644 --- a/api/http/handler/stacks/stack_list.go +++ b/api/http/handler/stacks/stack_list.go @@ -23,13 +23,13 @@ func (handler *Handler) stackList(w http.ResponseWriter, r *http.Request) *httpe return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: filters", err} } - stacks, err := handler.StackService.Stacks() + stacks, err := handler.DataStore.Stack().Stacks() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve stacks from the database", err} } stacks = filterStacks(stacks, &filters) - resourceControls, err := handler.ResourceControlService.ResourceControls() + resourceControls, err := handler.DataStore.ResourceControl().ResourceControls() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve resource controls from the database", err} } @@ -43,14 +43,14 @@ func (handler *Handler) stackList(w http.ResponseWriter, r *http.Request) *httpe if !securityContext.IsAdmin { rbacExtensionEnabled := true - _, err := handler.ExtensionService.Extension(portainer.RBACExtension) + _, err := handler.DataStore.Extension().Extension(portainer.RBACExtension) if err == portainer.ErrObjectNotFound { rbacExtensionEnabled = false } else if err != nil && err != portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to check if RBAC extension is enabled", err} } - user, err := handler.UserService.User(securityContext.UserID) + user, err := handler.DataStore.User().User(securityContext.UserID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve user information from the database", err} } diff --git a/api/http/handler/stacks/stack_migrate.go b/api/http/handler/stacks/stack_migrate.go index 690622a7a..1c0f98497 100644 --- a/api/http/handler/stacks/stack_migrate.go +++ b/api/http/handler/stacks/stack_migrate.go @@ -36,14 +36,14 @@ func (handler *Handler) stackMigrate(w http.ResponseWriter, r *http.Request) *ht return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - stack, err := handler.StackService.Stack(portainer.StackID(stackID)) + stack, err := handler.DataStore.Stack().Stack(portainer.StackID(stackID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a stack with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a stack with the specified identifier inside the database", err} } - endpoint, err := handler.EndpointService.Endpoint(stack.EndpointID) + endpoint, err := handler.DataStore.Endpoint().Endpoint(stack.EndpointID) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -55,7 +55,7 @@ func (handler *Handler) stackMigrate(w http.ResponseWriter, r *http.Request) *ht return &httperror.HandlerError{http.StatusForbidden, "Permission denied to access endpoint", err} } - resourceControl, err := handler.ResourceControlService.ResourceControlByResourceIDAndType(stack.Name, portainer.StackResourceControl) + resourceControl, err := handler.DataStore.ResourceControl().ResourceControlByResourceIDAndType(stack.Name, portainer.StackResourceControl) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve a resource control associated to the stack", err} } @@ -84,7 +84,7 @@ func (handler *Handler) stackMigrate(w http.ResponseWriter, r *http.Request) *ht stack.EndpointID = portainer.EndpointID(endpointID) } - targetEndpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(payload.EndpointID)) + targetEndpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(payload.EndpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err} } else if err != nil { @@ -112,7 +112,7 @@ func (handler *Handler) stackMigrate(w http.ResponseWriter, r *http.Request) *ht return &httperror.HandlerError{http.StatusInternalServerError, err.Error(), err} } - err = handler.StackService.UpdateStack(stack.ID, stack) + err = handler.DataStore.Stack().UpdateStack(stack.ID, stack) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the stack changes inside the database", err} } diff --git a/api/http/handler/stacks/stack_update.go b/api/http/handler/stacks/stack_update.go index 1e4e08fdf..3781f9a78 100644 --- a/api/http/handler/stacks/stack_update.go +++ b/api/http/handler/stacks/stack_update.go @@ -45,7 +45,7 @@ func (handler *Handler) stackUpdate(w http.ResponseWriter, r *http.Request) *htt return &httperror.HandlerError{http.StatusBadRequest, "Invalid stack identifier route variable", err} } - stack, err := handler.StackService.Stack(portainer.StackID(stackID)) + stack, err := handler.DataStore.Stack().Stack(portainer.StackID(stackID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a stack with the specified identifier inside the database", err} } else if err != nil { @@ -63,7 +63,7 @@ func (handler *Handler) stackUpdate(w http.ResponseWriter, r *http.Request) *htt stack.EndpointID = portainer.EndpointID(endpointID) } - endpoint, err := handler.EndpointService.Endpoint(stack.EndpointID) + endpoint, err := handler.DataStore.Endpoint().Endpoint(stack.EndpointID) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find the endpoint associated to the stack inside the database", err} } else if err != nil { @@ -75,7 +75,7 @@ func (handler *Handler) stackUpdate(w http.ResponseWriter, r *http.Request) *htt return &httperror.HandlerError{http.StatusForbidden, "Permission denied to access endpoint", err} } - resourceControl, err := handler.ResourceControlService.ResourceControlByResourceIDAndType(stack.Name, portainer.StackResourceControl) + resourceControl, err := handler.DataStore.ResourceControl().ResourceControlByResourceIDAndType(stack.Name, portainer.StackResourceControl) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve a resource control associated to the stack", err} } @@ -98,7 +98,7 @@ func (handler *Handler) stackUpdate(w http.ResponseWriter, r *http.Request) *htt return updateError } - err = handler.StackService.UpdateStack(stack.ID, stack) + err = handler.DataStore.Stack().UpdateStack(stack.ID, stack) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the stack changes inside the database", err} } diff --git a/api/http/handler/tags/handler.go b/api/http/handler/tags/handler.go index 21ca61acb..f7b6fd75d 100644 --- a/api/http/handler/tags/handler.go +++ b/api/http/handler/tags/handler.go @@ -12,12 +12,7 @@ import ( // Handler is the HTTP handler used to handle tag operations. type Handler struct { *mux.Router - TagService portainer.TagService - EdgeGroupService portainer.EdgeGroupService - EdgeStackService portainer.EdgeStackService - EndpointService portainer.EndpointService - EndpointGroupService portainer.EndpointGroupService - EndpointRelationService portainer.EndpointRelationService + DataStore portainer.DataStore } // NewHandler creates a handler to manage tag operations. diff --git a/api/http/handler/tags/tag_create.go b/api/http/handler/tags/tag_create.go index 846d256ee..d1857bcf5 100644 --- a/api/http/handler/tags/tag_create.go +++ b/api/http/handler/tags/tag_create.go @@ -29,7 +29,7 @@ func (handler *Handler) tagCreate(w http.ResponseWriter, r *http.Request) *httpe return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - tags, err := handler.TagService.Tags() + tags, err := handler.DataStore.Tag().Tags() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve tags from the database", err} } @@ -46,7 +46,7 @@ func (handler *Handler) tagCreate(w http.ResponseWriter, r *http.Request) *httpe Endpoints: map[portainer.EndpointID]bool{}, } - err = handler.TagService.CreateTag(tag) + err = handler.DataStore.Tag().CreateTag(tag) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the tag inside the database", err} } diff --git a/api/http/handler/tags/tag_delete.go b/api/http/handler/tags/tag_delete.go index c2cafe43e..52dac6a9c 100644 --- a/api/http/handler/tags/tag_delete.go +++ b/api/http/handler/tags/tag_delete.go @@ -17,7 +17,7 @@ func (handler *Handler) tagDelete(w http.ResponseWriter, r *http.Request) *httpe } tagID := portainer.TagID(id) - tag, err := handler.TagService.Tag(tagID) + tag, err := handler.DataStore.Tag().Tag(tagID) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a tag with the specified identifier inside the database", err} } else if err != nil { @@ -25,7 +25,7 @@ func (handler *Handler) tagDelete(w http.ResponseWriter, r *http.Request) *httpe } for endpointID := range tag.Endpoints { - endpoint, err := handler.EndpointService.Endpoint(endpointID) + endpoint, err := handler.DataStore.Endpoint().Endpoint(endpointID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoint from the database", err} } @@ -33,7 +33,7 @@ func (handler *Handler) tagDelete(w http.ResponseWriter, r *http.Request) *httpe tagIdx := findTagIndex(endpoint.TagIDs, tagID) if tagIdx != -1 { endpoint.TagIDs = removeElement(endpoint.TagIDs, tagIdx) - err = handler.EndpointService.UpdateEndpoint(endpoint.ID, endpoint) + err = handler.DataStore.Endpoint().UpdateEndpoint(endpoint.ID, endpoint) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to update endpoint", err} } @@ -41,7 +41,7 @@ func (handler *Handler) tagDelete(w http.ResponseWriter, r *http.Request) *httpe } for endpointGroupID := range tag.EndpointGroups { - endpointGroup, err := handler.EndpointGroupService.EndpointGroup(endpointGroupID) + endpointGroup, err := handler.DataStore.EndpointGroup().EndpointGroup(endpointGroupID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoint group from the database", err} } @@ -49,24 +49,24 @@ func (handler *Handler) tagDelete(w http.ResponseWriter, r *http.Request) *httpe tagIdx := findTagIndex(endpointGroup.TagIDs, tagID) if tagIdx != -1 { endpointGroup.TagIDs = removeElement(endpointGroup.TagIDs, tagIdx) - err = handler.EndpointGroupService.UpdateEndpointGroup(endpointGroup.ID, endpointGroup) + err = handler.DataStore.EndpointGroup().UpdateEndpointGroup(endpointGroup.ID, endpointGroup) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to update endpoint group", err} } } } - endpoints, err := handler.EndpointService.Endpoints() + endpoints, err := handler.DataStore.Endpoint().Endpoints() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve endpoints from the database", err} } - edgeGroups, err := handler.EdgeGroupService.EdgeGroups() + edgeGroups, err := handler.DataStore.EdgeGroup().EdgeGroups() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge groups from the database", err} } - edgeStacks, err := handler.EdgeStackService.EdgeStacks() + edgeStacks, err := handler.DataStore.EdgeStack().EdgeStacks() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve edge stacks from the database", err} } @@ -85,14 +85,14 @@ func (handler *Handler) tagDelete(w http.ResponseWriter, r *http.Request) *httpe tagIdx := findTagIndex(edgeGroup.TagIDs, tagID) if tagIdx != -1 { edgeGroup.TagIDs = removeElement(edgeGroup.TagIDs, tagIdx) - err = handler.EdgeGroupService.UpdateEdgeGroup(edgeGroup.ID, edgeGroup) + err = handler.DataStore.EdgeGroup().UpdateEdgeGroup(edgeGroup.ID, edgeGroup) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to update endpoint group", err} } } } - err = handler.TagService.DeleteTag(tagID) + err = handler.DataStore.Tag().DeleteTag(tagID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove the tag from the database", err} } @@ -101,12 +101,12 @@ func (handler *Handler) tagDelete(w http.ResponseWriter, r *http.Request) *httpe } func (handler *Handler) updateEndpointRelations(endpoint portainer.Endpoint, edgeGroups []portainer.EdgeGroup, edgeStacks []portainer.EdgeStack) error { - endpointRelation, err := handler.EndpointRelationService.EndpointRelation(endpoint.ID) + endpointRelation, err := handler.DataStore.EndpointRelation().EndpointRelation(endpoint.ID) if err != nil { return err } - endpointGroup, err := handler.EndpointGroupService.EndpointGroup(endpoint.GroupID) + endpointGroup, err := handler.DataStore.EndpointGroup().EndpointGroup(endpoint.GroupID) if err != nil { return err } @@ -118,7 +118,7 @@ func (handler *Handler) updateEndpointRelations(endpoint portainer.Endpoint, edg } endpointRelation.EdgeStacks = stacksSet - return handler.EndpointRelationService.UpdateEndpointRelation(endpoint.ID, endpointRelation) + return handler.DataStore.EndpointRelation().UpdateEndpointRelation(endpoint.ID, endpointRelation) } func findTagIndex(tags []portainer.TagID, searchTagID portainer.TagID) int { diff --git a/api/http/handler/tags/tag_list.go b/api/http/handler/tags/tag_list.go index a19aa48e7..e4d7f2afa 100644 --- a/api/http/handler/tags/tag_list.go +++ b/api/http/handler/tags/tag_list.go @@ -9,7 +9,7 @@ import ( // GET request on /api/tags func (handler *Handler) tagList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - tags, err := handler.TagService.Tags() + tags, err := handler.DataStore.Tag().Tags() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve tags from the database", err} } diff --git a/api/http/handler/teammemberships/handler.go b/api/http/handler/teammemberships/handler.go index 0428241ec..e9faca62d 100644 --- a/api/http/handler/teammemberships/handler.go +++ b/api/http/handler/teammemberships/handler.go @@ -13,8 +13,8 @@ import ( // Handler is the HTTP handler used to handle team membership operations. type Handler struct { *mux.Router - TeamMembershipService portainer.TeamMembershipService - AuthorizationService *portainer.AuthorizationService + DataStore portainer.DataStore + AuthorizationService *portainer.AuthorizationService } // NewHandler creates a handler to manage team membership operations. diff --git a/api/http/handler/teammemberships/teammembership_create.go b/api/http/handler/teammemberships/teammembership_create.go index 245b1fe67..21f5cd74d 100644 --- a/api/http/handler/teammemberships/teammembership_create.go +++ b/api/http/handler/teammemberships/teammembership_create.go @@ -46,7 +46,7 @@ func (handler *Handler) teamMembershipCreate(w http.ResponseWriter, r *http.Requ return &httperror.HandlerError{http.StatusForbidden, "Permission denied to manage team memberships", portainer.ErrResourceAccessDenied} } - memberships, err := handler.TeamMembershipService.TeamMembershipsByUserID(portainer.UserID(payload.UserID)) + memberships, err := handler.DataStore.TeamMembership().TeamMembershipsByUserID(portainer.UserID(payload.UserID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve team memberships from the database", err} } @@ -65,7 +65,7 @@ func (handler *Handler) teamMembershipCreate(w http.ResponseWriter, r *http.Requ Role: portainer.MembershipRole(payload.Role), } - err = handler.TeamMembershipService.CreateTeamMembership(membership) + err = handler.DataStore.TeamMembership().CreateTeamMembership(membership) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist team memberships inside the database", err} } diff --git a/api/http/handler/teammemberships/teammembership_delete.go b/api/http/handler/teammemberships/teammembership_delete.go index 179bbe800..775ba4d1a 100644 --- a/api/http/handler/teammemberships/teammembership_delete.go +++ b/api/http/handler/teammemberships/teammembership_delete.go @@ -17,7 +17,7 @@ func (handler *Handler) teamMembershipDelete(w http.ResponseWriter, r *http.Requ return &httperror.HandlerError{http.StatusBadRequest, "Invalid membership identifier route variable", err} } - membership, err := handler.TeamMembershipService.TeamMembership(portainer.TeamMembershipID(membershipID)) + membership, err := handler.DataStore.TeamMembership().TeamMembership(portainer.TeamMembershipID(membershipID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a team membership with the specified identifier inside the database", err} } else if err != nil { @@ -33,7 +33,7 @@ func (handler *Handler) teamMembershipDelete(w http.ResponseWriter, r *http.Requ return &httperror.HandlerError{http.StatusForbidden, "Permission denied to delete the membership", portainer.ErrResourceAccessDenied} } - err = handler.TeamMembershipService.DeleteTeamMembership(portainer.TeamMembershipID(membershipID)) + err = handler.DataStore.TeamMembership().DeleteTeamMembership(portainer.TeamMembershipID(membershipID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove the team membership from the database", err} } diff --git a/api/http/handler/teammemberships/teammembership_list.go b/api/http/handler/teammemberships/teammembership_list.go index bb0196458..20e42b599 100644 --- a/api/http/handler/teammemberships/teammembership_list.go +++ b/api/http/handler/teammemberships/teammembership_list.go @@ -20,7 +20,7 @@ func (handler *Handler) teamMembershipList(w http.ResponseWriter, r *http.Reques return &httperror.HandlerError{http.StatusForbidden, "Permission denied to list team memberships", portainer.ErrResourceAccessDenied} } - memberships, err := handler.TeamMembershipService.TeamMemberships() + memberships, err := handler.DataStore.TeamMembership().TeamMemberships() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve team memberships from the database", err} } diff --git a/api/http/handler/teammemberships/teammembership_update.go b/api/http/handler/teammemberships/teammembership_update.go index 84ab44323..d98929a75 100644 --- a/api/http/handler/teammemberships/teammembership_update.go +++ b/api/http/handler/teammemberships/teammembership_update.go @@ -51,7 +51,7 @@ func (handler *Handler) teamMembershipUpdate(w http.ResponseWriter, r *http.Requ return &httperror.HandlerError{http.StatusForbidden, "Permission denied to update the membership", portainer.ErrResourceAccessDenied} } - membership, err := handler.TeamMembershipService.TeamMembership(portainer.TeamMembershipID(membershipID)) + membership, err := handler.DataStore.TeamMembership().TeamMembership(portainer.TeamMembershipID(membershipID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a team membership with the specified identifier inside the database", err} } else if err != nil { @@ -66,7 +66,7 @@ func (handler *Handler) teamMembershipUpdate(w http.ResponseWriter, r *http.Requ membership.TeamID = portainer.TeamID(payload.TeamID) membership.Role = portainer.MembershipRole(payload.Role) - err = handler.TeamMembershipService.UpdateTeamMembership(membership.ID, membership) + err = handler.DataStore.TeamMembership().UpdateTeamMembership(membership.ID, membership) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist membership changes inside the database", err} } diff --git a/api/http/handler/teams/handler.go b/api/http/handler/teams/handler.go index e5eea77fc..ce87e0513 100644 --- a/api/http/handler/teams/handler.go +++ b/api/http/handler/teams/handler.go @@ -12,9 +12,8 @@ import ( // Handler is the HTTP handler used to handle team operations. type Handler struct { *mux.Router - TeamService portainer.TeamService - TeamMembershipService portainer.TeamMembershipService - AuthorizationService *portainer.AuthorizationService + DataStore portainer.DataStore + AuthorizationService *portainer.AuthorizationService } // NewHandler creates a handler to manage team operations. diff --git a/api/http/handler/teams/team_create.go b/api/http/handler/teams/team_create.go index 3cfad7acb..9012b0b88 100644 --- a/api/http/handler/teams/team_create.go +++ b/api/http/handler/teams/team_create.go @@ -28,7 +28,7 @@ func (handler *Handler) teamCreate(w http.ResponseWriter, r *http.Request) *http return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - team, err := handler.TeamService.TeamByName(payload.Name) + team, err := handler.DataStore.Team().TeamByName(payload.Name) if err != nil && err != portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve teams from the database", err} } @@ -40,7 +40,7 @@ func (handler *Handler) teamCreate(w http.ResponseWriter, r *http.Request) *http Name: payload.Name, } - err = handler.TeamService.CreateTeam(team) + err = handler.DataStore.Team().CreateTeam(team) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the team inside the database", err} } diff --git a/api/http/handler/teams/team_delete.go b/api/http/handler/teams/team_delete.go index 2b96e9351..d1629347e 100644 --- a/api/http/handler/teams/team_delete.go +++ b/api/http/handler/teams/team_delete.go @@ -16,19 +16,19 @@ func (handler *Handler) teamDelete(w http.ResponseWriter, r *http.Request) *http return &httperror.HandlerError{http.StatusBadRequest, "Invalid team identifier route variable", err} } - _, err = handler.TeamService.Team(portainer.TeamID(teamID)) + _, err = handler.DataStore.Team().Team(portainer.TeamID(teamID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a team with the specified identifier inside the database", err} } else if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a team with the specified identifier inside the database", err} } - err = handler.TeamService.DeleteTeam(portainer.TeamID(teamID)) + err = handler.DataStore.Team().DeleteTeam(portainer.TeamID(teamID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to delete the team from the database", err} } - err = handler.TeamMembershipService.DeleteTeamMembershipByTeamID(portainer.TeamID(teamID)) + err = handler.DataStore.TeamMembership().DeleteTeamMembershipByTeamID(portainer.TeamID(teamID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to delete associated team memberships from the database", err} } diff --git a/api/http/handler/teams/team_inspect.go b/api/http/handler/teams/team_inspect.go index 0eeb89e8f..f543a8f37 100644 --- a/api/http/handler/teams/team_inspect.go +++ b/api/http/handler/teams/team_inspect.go @@ -26,7 +26,7 @@ func (handler *Handler) teamInspect(w http.ResponseWriter, r *http.Request) *htt return &httperror.HandlerError{http.StatusForbidden, "Access denied to team", portainer.ErrResourceAccessDenied} } - team, err := handler.TeamService.Team(portainer.TeamID(teamID)) + team, err := handler.DataStore.Team().Team(portainer.TeamID(teamID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a team with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/teams/team_list.go b/api/http/handler/teams/team_list.go index da67b696b..5056a69b6 100644 --- a/api/http/handler/teams/team_list.go +++ b/api/http/handler/teams/team_list.go @@ -10,7 +10,7 @@ import ( // GET request on /api/teams func (handler *Handler) teamList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - teams, err := handler.TeamService.Teams() + teams, err := handler.DataStore.Team().Teams() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve teams from the database", err} } diff --git a/api/http/handler/teams/team_memberships.go b/api/http/handler/teams/team_memberships.go index e7c26fff6..1d61e1a01 100644 --- a/api/http/handler/teams/team_memberships.go +++ b/api/http/handler/teams/team_memberships.go @@ -26,7 +26,7 @@ func (handler *Handler) teamMemberships(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusForbidden, "Access denied to team", portainer.ErrResourceAccessDenied} } - memberships, err := handler.TeamMembershipService.TeamMembershipsByTeamID(portainer.TeamID(teamID)) + memberships, err := handler.DataStore.TeamMembership().TeamMembershipsByTeamID(portainer.TeamID(teamID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve associated team memberships from the database", err} } diff --git a/api/http/handler/teams/team_update.go b/api/http/handler/teams/team_update.go index 740e4fc61..06e1ef9d0 100644 --- a/api/http/handler/teams/team_update.go +++ b/api/http/handler/teams/team_update.go @@ -30,7 +30,7 @@ func (handler *Handler) teamUpdate(w http.ResponseWriter, r *http.Request) *http return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - team, err := handler.TeamService.Team(portainer.TeamID(teamID)) + team, err := handler.DataStore.Team().Team(portainer.TeamID(teamID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a team with the specified identifier inside the database", err} } else if err != nil { @@ -41,7 +41,7 @@ func (handler *Handler) teamUpdate(w http.ResponseWriter, r *http.Request) *http team.Name = payload.Name } - err = handler.TeamService.UpdateTeam(team.ID, team) + err = handler.DataStore.Team().UpdateTeam(team.ID, team) if err != nil { return &httperror.HandlerError{http.StatusNotFound, "Unable to persist team changes inside the database", err} } diff --git a/api/http/handler/templates/handler.go b/api/http/handler/templates/handler.go index 994d27306..7360d5252 100644 --- a/api/http/handler/templates/handler.go +++ b/api/http/handler/templates/handler.go @@ -12,7 +12,7 @@ import ( // Handler represents an HTTP API handler for managing templates. type Handler struct { *mux.Router - SettingsService portainer.SettingsService + DataStore portainer.DataStore } // NewHandler returns a new instance of Handler. diff --git a/api/http/handler/templates/template_list.go b/api/http/handler/templates/template_list.go index 5d86941dd..236b5cfdd 100644 --- a/api/http/handler/templates/template_list.go +++ b/api/http/handler/templates/template_list.go @@ -9,7 +9,7 @@ import ( // GET request on /api/templates func (handler *Handler) templateList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err} } diff --git a/api/http/handler/users/admin_check.go b/api/http/handler/users/admin_check.go index 0f53e6693..7c1a54f33 100644 --- a/api/http/handler/users/admin_check.go +++ b/api/http/handler/users/admin_check.go @@ -10,7 +10,7 @@ import ( // GET request on /api/users/admin/check func (handler *Handler) adminCheck(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - users, err := handler.UserService.UsersByRole(portainer.AdministratorRole) + users, err := handler.DataStore.User().UsersByRole(portainer.AdministratorRole) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve users from the database", err} } diff --git a/api/http/handler/users/admin_init.go b/api/http/handler/users/admin_init.go index a57e95f62..67010b660 100644 --- a/api/http/handler/users/admin_init.go +++ b/api/http/handler/users/admin_init.go @@ -33,7 +33,7 @@ func (handler *Handler) adminInit(w http.ResponseWriter, r *http.Request) *httpe return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - users, err := handler.UserService.UsersByRole(portainer.AdministratorRole) + users, err := handler.DataStore.User().UsersByRole(portainer.AdministratorRole) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve users from the database", err} } @@ -53,7 +53,7 @@ func (handler *Handler) adminInit(w http.ResponseWriter, r *http.Request) *httpe return &httperror.HandlerError{http.StatusInternalServerError, "Unable to hash user password", portainer.ErrCryptoHashFailure} } - err = handler.UserService.CreateUser(user) + err = handler.DataStore.User().CreateUser(user) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist user inside the database", err} } diff --git a/api/http/handler/users/handler.go b/api/http/handler/users/handler.go index 646bf8ae5..cbdc9eaf7 100644 --- a/api/http/handler/users/handler.go +++ b/api/http/handler/users/handler.go @@ -17,13 +17,9 @@ func hideFields(user *portainer.User) { // Handler is the HTTP handler used to handle user operations. type Handler struct { *mux.Router - UserService portainer.UserService - TeamService portainer.TeamService - TeamMembershipService portainer.TeamMembershipService - ResourceControlService portainer.ResourceControlService - CryptoService portainer.CryptoService - SettingsService portainer.SettingsService - AuthorizationService *portainer.AuthorizationService + DataStore portainer.DataStore + CryptoService portainer.CryptoService + AuthorizationService *portainer.AuthorizationService } // NewHandler creates a handler to manage user operations. diff --git a/api/http/handler/users/user_create.go b/api/http/handler/users/user_create.go index 582ca084d..43bcab0e7 100644 --- a/api/http/handler/users/user_create.go +++ b/api/http/handler/users/user_create.go @@ -49,7 +49,7 @@ func (handler *Handler) userCreate(w http.ResponseWriter, r *http.Request) *http return &httperror.HandlerError{http.StatusForbidden, "Permission denied to create administrator user", portainer.ErrResourceAccessDenied} } - user, err := handler.UserService.UserByUsername(payload.Username) + user, err := handler.DataStore.User().UserByUsername(payload.Username) if err != nil && err != portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve users from the database", err} } @@ -63,7 +63,7 @@ func (handler *Handler) userCreate(w http.ResponseWriter, r *http.Request) *http PortainerAuthorizations: portainer.DefaultPortainerAuthorizations(), } - settings, err := handler.SettingsService.Settings() + settings, err := handler.DataStore.Settings().Settings() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err} } @@ -75,7 +75,7 @@ func (handler *Handler) userCreate(w http.ResponseWriter, r *http.Request) *http } } - err = handler.UserService.CreateUser(user) + err = handler.DataStore.User().CreateUser(user) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist user inside the database", err} } diff --git a/api/http/handler/users/user_delete.go b/api/http/handler/users/user_delete.go index 203a3be47..df72fe673 100644 --- a/api/http/handler/users/user_delete.go +++ b/api/http/handler/users/user_delete.go @@ -26,7 +26,7 @@ func (handler *Handler) userDelete(w http.ResponseWriter, r *http.Request) *http return &httperror.HandlerError{http.StatusForbidden, "Cannot remove your own user account. Contact another administrator", portainer.ErrAdminCannotRemoveSelf} } - user, err := handler.UserService.User(portainer.UserID(userID)) + user, err := handler.DataStore.User().User(portainer.UserID(userID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a user with the specified identifier inside the database", err} } else if err != nil { @@ -45,7 +45,7 @@ func (handler *Handler) deleteAdminUser(w http.ResponseWriter, user *portainer.U return handler.deleteUser(w, user) } - users, err := handler.UserService.Users() + users, err := handler.DataStore.User().Users() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve users from the database", err} } @@ -65,12 +65,12 @@ func (handler *Handler) deleteAdminUser(w http.ResponseWriter, user *portainer.U } func (handler *Handler) deleteUser(w http.ResponseWriter, user *portainer.User) *httperror.HandlerError { - err := handler.UserService.DeleteUser(user.ID) + err := handler.DataStore.User().DeleteUser(user.ID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove user from the database", err} } - err = handler.TeamMembershipService.DeleteTeamMembershipByUserID(user.ID) + err = handler.DataStore.TeamMembership().DeleteTeamMembershipByUserID(user.ID) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove user memberships from the database", err} } diff --git a/api/http/handler/users/user_inspect.go b/api/http/handler/users/user_inspect.go index 4c5954283..b35d67d3a 100644 --- a/api/http/handler/users/user_inspect.go +++ b/api/http/handler/users/user_inspect.go @@ -27,7 +27,7 @@ func (handler *Handler) userInspect(w http.ResponseWriter, r *http.Request) *htt return &httperror.HandlerError{http.StatusForbidden, "Permission denied inspect user", portainer.ErrResourceAccessDenied} } - user, err := handler.UserService.User(portainer.UserID(userID)) + user, err := handler.DataStore.User().User(portainer.UserID(userID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a user with the specified identifier inside the database", err} } else if err != nil { diff --git a/api/http/handler/users/user_list.go b/api/http/handler/users/user_list.go index 5792689a0..f79b65bb3 100644 --- a/api/http/handler/users/user_list.go +++ b/api/http/handler/users/user_list.go @@ -10,7 +10,7 @@ import ( // GET request on /api/users func (handler *Handler) userList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { - users, err := handler.UserService.Users() + users, err := handler.DataStore.User().Users() if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve users from the database", err} } diff --git a/api/http/handler/users/user_memberships.go b/api/http/handler/users/user_memberships.go index 090880e6b..3d4655bbb 100644 --- a/api/http/handler/users/user_memberships.go +++ b/api/http/handler/users/user_memberships.go @@ -26,7 +26,7 @@ func (handler *Handler) userMemberships(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusForbidden, "Permission denied to update user memberships", portainer.ErrUnauthorized} } - memberships, err := handler.TeamMembershipService.TeamMembershipsByUserID(portainer.UserID(userID)) + memberships, err := handler.DataStore.TeamMembership().TeamMembershipsByUserID(portainer.UserID(userID)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist membership changes inside the database", err} } diff --git a/api/http/handler/users/user_update.go b/api/http/handler/users/user_update.go index 62ba24f46..e61a62085 100644 --- a/api/http/handler/users/user_update.go +++ b/api/http/handler/users/user_update.go @@ -48,7 +48,7 @@ func (handler *Handler) userUpdate(w http.ResponseWriter, r *http.Request) *http return &httperror.HandlerError{http.StatusForbidden, "Permission denied to update user to administrator role", portainer.ErrResourceAccessDenied} } - user, err := handler.UserService.User(portainer.UserID(userID)) + user, err := handler.DataStore.User().User(portainer.UserID(userID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a user with the specified identifier inside the database", err} } else if err != nil { @@ -66,7 +66,7 @@ func (handler *Handler) userUpdate(w http.ResponseWriter, r *http.Request) *http user.Role = portainer.UserRole(payload.Role) } - err = handler.UserService.UpdateUser(user.ID, user) + err = handler.DataStore.User().UpdateUser(user.ID, user) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist user changes inside the database", err} } diff --git a/api/http/handler/users/user_update_password.go b/api/http/handler/users/user_update_password.go index 77ce99b49..8905a0462 100644 --- a/api/http/handler/users/user_update_password.go +++ b/api/http/handler/users/user_update_password.go @@ -48,7 +48,7 @@ func (handler *Handler) userUpdatePassword(w http.ResponseWriter, r *http.Reques return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - user, err := handler.UserService.User(portainer.UserID(userID)) + user, err := handler.DataStore.User().User(portainer.UserID(userID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a user with the specified identifier inside the database", err} } else if err != nil { @@ -65,7 +65,7 @@ func (handler *Handler) userUpdatePassword(w http.ResponseWriter, r *http.Reques return &httperror.HandlerError{http.StatusInternalServerError, "Unable to hash user password", portainer.ErrCryptoHashFailure} } - err = handler.UserService.UpdateUser(user.ID, user) + err = handler.DataStore.User().UpdateUser(user.ID, user) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist user changes inside the database", err} } diff --git a/api/http/handler/webhooks/handler.go b/api/http/handler/webhooks/handler.go index 2e342114e..aa5046cfe 100644 --- a/api/http/handler/webhooks/handler.go +++ b/api/http/handler/webhooks/handler.go @@ -13,8 +13,7 @@ import ( // Handler is the HTTP handler used to handle webhook operations. type Handler struct { *mux.Router - WebhookService portainer.WebhookService - EndpointService portainer.EndpointService + DataStore portainer.DataStore DockerClientFactory *docker.ClientFactory } diff --git a/api/http/handler/webhooks/webhook_create.go b/api/http/handler/webhooks/webhook_create.go index 883521608..5b960ec2d 100644 --- a/api/http/handler/webhooks/webhook_create.go +++ b/api/http/handler/webhooks/webhook_create.go @@ -37,7 +37,7 @@ func (handler *Handler) webhookCreate(w http.ResponseWriter, r *http.Request) *h return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err} } - webhook, err := handler.WebhookService.WebhookByResourceID(payload.ResourceID) + webhook, err := handler.DataStore.Webhook().WebhookByResourceID(payload.ResourceID) if err != nil && err != portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusInternalServerError, "An error occurred retrieving webhooks from the database", err} } @@ -57,7 +57,7 @@ func (handler *Handler) webhookCreate(w http.ResponseWriter, r *http.Request) *h WebhookType: portainer.WebhookType(payload.WebhookType), } - err = handler.WebhookService.CreateWebhook(webhook) + err = handler.DataStore.Webhook().CreateWebhook(webhook) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist the webhook inside the database", err} } diff --git a/api/http/handler/webhooks/webhook_delete.go b/api/http/handler/webhooks/webhook_delete.go index ee36f822c..305dbca6b 100644 --- a/api/http/handler/webhooks/webhook_delete.go +++ b/api/http/handler/webhooks/webhook_delete.go @@ -16,7 +16,7 @@ func (handler *Handler) webhookDelete(w http.ResponseWriter, r *http.Request) *h return &httperror.HandlerError{http.StatusBadRequest, "Invalid webhook id", err} } - err = handler.WebhookService.DeleteWebhook(portainer.WebhookID(id)) + err = handler.DataStore.Webhook().DeleteWebhook(portainer.WebhookID(id)) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to remove the webhook from the database", err} } diff --git a/api/http/handler/webhooks/webhook_execute.go b/api/http/handler/webhooks/webhook_execute.go index 2e82f0e1a..11e62fd99 100644 --- a/api/http/handler/webhooks/webhook_execute.go +++ b/api/http/handler/webhooks/webhook_execute.go @@ -21,7 +21,7 @@ func (handler *Handler) webhookExecute(w http.ResponseWriter, r *http.Request) * return &httperror.HandlerError{http.StatusInternalServerError, "Invalid service id parameter", err} } - webhook, err := handler.WebhookService.WebhookByToken(webhookToken) + webhook, err := handler.DataStore.Webhook().WebhookByToken(webhookToken) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find a webhook with this token", err} @@ -33,7 +33,7 @@ func (handler *Handler) webhookExecute(w http.ResponseWriter, r *http.Request) * endpointID := webhook.EndpointID webhookType := webhook.WebhookType - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint 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 a45051884..df9f5550c 100644 --- a/api/http/handler/webhooks/webhook_list.go +++ b/api/http/handler/webhooks/webhook_list.go @@ -22,7 +22,7 @@ func (handler *Handler) webhookList(w http.ResponseWriter, r *http.Request) *htt return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: filters", err} } - webhooks, err := handler.WebhookService.Webhooks() + webhooks, err := handler.DataStore.Webhook().Webhooks() webhooks = filterWebhooks(webhooks, &filters) if err != nil { return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve webhooks from the database", err} diff --git a/api/http/handler/websocket/attach.go b/api/http/handler/websocket/attach.go index 5de214eca..2a498f82e 100644 --- a/api/http/handler/websocket/attach.go +++ b/api/http/handler/websocket/attach.go @@ -32,7 +32,7 @@ func (handler *Handler) websocketAttach(w http.ResponseWriter, r *http.Request) return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: endpointId", err} } - endpoint, err := handler.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find the endpoint 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 afe670a56..8c3318a83 100644 --- a/api/http/handler/websocket/exec.go +++ b/api/http/handler/websocket/exec.go @@ -39,7 +39,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.EndpointService.Endpoint(portainer.EndpointID(endpointID)) + endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID)) if err == portainer.ErrObjectNotFound { return &httperror.HandlerError{http.StatusNotFound, "Unable to find the endpoint associated to the stack inside the database", err} } else if err != nil { diff --git a/api/http/handler/websocket/handler.go b/api/http/handler/websocket/handler.go index cc0165eb0..eb86de0a9 100644 --- a/api/http/handler/websocket/handler.go +++ b/api/http/handler/websocket/handler.go @@ -11,7 +11,7 @@ import ( // Handler is the HTTP handler used to handle websocket operations. type Handler struct { *mux.Router - EndpointService portainer.EndpointService + DataStore portainer.DataStore SignatureService portainer.DigitalSignatureService ReverseTunnelService portainer.ReverseTunnelService requestBouncer *security.RequestBouncer diff --git a/api/http/proxy/factory/docker.go b/api/http/proxy/factory/docker.go index acf0731bd..149c488d9 100644 --- a/api/http/proxy/factory/docker.go +++ b/api/http/proxy/factory/docker.go @@ -56,18 +56,11 @@ func (factory *ProxyFactory) newDockerHTTPProxy(endpoint *portainer.Endpoint) (h } transportParameters := &docker.TransportParameters{ - Endpoint: endpoint, - ResourceControlService: factory.resourceControlService, - UserService: factory.userService, - TeamService: factory.teamService, - TeamMembershipService: factory.teamMembershipService, - RegistryService: factory.registryService, - DockerHubService: factory.dockerHubService, - SettingsService: factory.settingsService, - ReverseTunnelService: factory.reverseTunnelService, - ExtensionService: factory.extensionService, - SignatureService: factory.signatureService, - DockerClientFactory: factory.dockerClientFactory, + Endpoint: endpoint, + DataStore: factory.dataStore, + ReverseTunnelService: factory.reverseTunnelService, + SignatureService: factory.signatureService, + DockerClientFactory: factory.dockerClientFactory, } dockerTransport, err := docker.NewTransport(transportParameters, httpTransport) diff --git a/api/http/proxy/factory/docker/access_control.go b/api/http/proxy/factory/docker/access_control.go index 2b8e183f9..d3429f3b6 100644 --- a/api/http/proxy/factory/docker/access_control.go +++ b/api/http/proxy/factory/docker/access_control.go @@ -32,7 +32,7 @@ func (transport *Transport) newResourceControlFromPortainerLabels(labelsObject m if labelsObject[resourceLabelForPortainerPublicResourceControl] != nil { resourceControl := portainer.NewPublicResourceControl(resourceID, resourceType) - err := transport.resourceControlService.CreateResourceControl(resourceControl) + err := transport.dataStore.ResourceControl().CreateResourceControl(resourceControl) if err != nil { return nil, err } @@ -57,7 +57,7 @@ func (transport *Transport) newResourceControlFromPortainerLabels(labelsObject m userIDs := make([]portainer.UserID, 0) for _, name := range teamNames { - team, err := transport.teamService.TeamByName(name) + team, err := transport.dataStore.Team().TeamByName(name) if err != nil { log.Printf("[WARN] [http,proxy,docker] [message: unknown team name in access control label, ignoring access control rule for this team] [name: %s] [resource_id: %s]", name, resourceID) continue @@ -67,7 +67,7 @@ func (transport *Transport) newResourceControlFromPortainerLabels(labelsObject m } for _, name := range userNames { - user, err := transport.userService.UserByUsername(name) + user, err := transport.dataStore.User().UserByUsername(name) if err != nil { log.Printf("[WARN] [http,proxy,docker] [message: unknown user name in access control label, ignoring access control rule for this user] [name: %s] [resource_id: %s]", name, resourceID) continue @@ -78,7 +78,7 @@ func (transport *Transport) newResourceControlFromPortainerLabels(labelsObject m resourceControl := portainer.NewRestrictedResourceControl(resourceID, resourceType, userIDs, teamIDs) - err := transport.resourceControlService.CreateResourceControl(resourceControl) + err := transport.dataStore.ResourceControl().CreateResourceControl(resourceControl) if err != nil { return nil, err } @@ -92,7 +92,7 @@ func (transport *Transport) newResourceControlFromPortainerLabels(labelsObject m func (transport *Transport) createPrivateResourceControl(resourceIdentifier string, resourceType portainer.ResourceControlType, userID portainer.UserID) (*portainer.ResourceControl, error) { resourceControl := portainer.NewPrivateResourceControl(resourceIdentifier, resourceType, userID) - err := transport.resourceControlService.CreateResourceControl(resourceControl) + err := transport.dataStore.ResourceControl().CreateResourceControl(resourceControl) if err != nil { log.Printf("[ERROR] [http,proxy,docker,transport] [message: unable to persist resource control] [resource: %s] [err: %s]", resourceIdentifier, err) return nil, err diff --git a/api/http/proxy/factory/docker/transport.go b/api/http/proxy/factory/docker/transport.go index c511948fa..2456cc860 100644 --- a/api/http/proxy/factory/docker/transport.go +++ b/api/http/proxy/factory/docker/transport.go @@ -24,36 +24,22 @@ type ( // Transport is a custom transport for Docker API reverse proxy. It allows // interception of requests and rewriting of responses. Transport struct { - HTTPTransport *http.Transport - endpoint *portainer.Endpoint - resourceControlService portainer.ResourceControlService - userService portainer.UserService - teamService portainer.TeamService - teamMembershipService portainer.TeamMembershipService - registryService portainer.RegistryService - dockerHubService portainer.DockerHubService - settingsService portainer.SettingsService - signatureService portainer.DigitalSignatureService - reverseTunnelService portainer.ReverseTunnelService - extensionService portainer.ExtensionService - dockerClient *client.Client - dockerClientFactory *docker.ClientFactory + HTTPTransport *http.Transport + endpoint *portainer.Endpoint + dataStore portainer.DataStore + signatureService portainer.DigitalSignatureService + reverseTunnelService portainer.ReverseTunnelService + dockerClient *client.Client + dockerClientFactory *docker.ClientFactory } // TransportParameters is used to create a new Transport TransportParameters struct { - Endpoint *portainer.Endpoint - ResourceControlService portainer.ResourceControlService - UserService portainer.UserService - TeamService portainer.TeamService - TeamMembershipService portainer.TeamMembershipService - RegistryService portainer.RegistryService - DockerHubService portainer.DockerHubService - SettingsService portainer.SettingsService - SignatureService portainer.DigitalSignatureService - ReverseTunnelService portainer.ReverseTunnelService - ExtensionService portainer.ExtensionService - DockerClientFactory *docker.ClientFactory + Endpoint *portainer.Endpoint + DataStore portainer.DataStore + SignatureService portainer.DigitalSignatureService + ReverseTunnelService portainer.ReverseTunnelService + DockerClientFactory *docker.ClientFactory } restrictedDockerOperationContext struct { @@ -80,20 +66,13 @@ func NewTransport(parameters *TransportParameters, httpTransport *http.Transport } transport := &Transport{ - endpoint: parameters.Endpoint, - resourceControlService: parameters.ResourceControlService, - userService: parameters.UserService, - teamService: parameters.TeamService, - teamMembershipService: parameters.TeamMembershipService, - registryService: parameters.RegistryService, - dockerHubService: parameters.DockerHubService, - settingsService: parameters.SettingsService, - signatureService: parameters.SignatureService, - reverseTunnelService: parameters.ReverseTunnelService, - extensionService: parameters.ExtensionService, - dockerClientFactory: parameters.DockerClientFactory, - HTTPTransport: httpTransport, - dockerClient: dockerClient, + endpoint: parameters.Endpoint, + dataStore: parameters.DataStore, + signatureService: parameters.SignatureService, + reverseTunnelService: parameters.ReverseTunnelService, + dockerClientFactory: parameters.DockerClientFactory, + HTTPTransport: httpTransport, + dockerClient: dockerClient, } return transport, nil @@ -429,18 +408,18 @@ func (transport *Transport) restrictedResourceOperation(request *http.Request, r } if tokenData.Role != portainer.AdministratorRole { - rbacExtension, err := transport.extensionService.Extension(portainer.RBACExtension) + rbacExtension, err := transport.dataStore.Extension().Extension(portainer.RBACExtension) if err != nil && err != portainer.ErrObjectNotFound { return nil, err } - user, err := transport.userService.User(tokenData.ID) + user, err := transport.dataStore.User().User(tokenData.ID) if err != nil { return nil, err } if volumeBrowseRestrictionCheck { - settings, err := transport.settingsService.Settings() + settings, err := transport.dataStore.Settings().Settings() if err != nil { return nil, err } @@ -468,7 +447,7 @@ func (transport *Transport) restrictedResourceOperation(request *http.Request, r return transport.executeDockerRequest(request) } - teamMemberships, err := transport.teamMembershipService.TeamMembershipsByUserID(tokenData.ID) + teamMemberships, err := transport.dataStore.TeamMembership().TeamMembershipsByUserID(tokenData.ID) if err != nil { return nil, err } @@ -478,7 +457,7 @@ func (transport *Transport) restrictedResourceOperation(request *http.Request, r userTeamIDs = append(userTeamIDs, membership.TeamID) } - resourceControls, err := transport.resourceControlService.ResourceControls() + resourceControls, err := transport.dataStore.ResourceControl().ResourceControls() if err != nil { return nil, err } @@ -516,7 +495,7 @@ func (transport *Transport) rewriteOperationWithLabelFiltering(request *http.Req return nil, err } - settings, err := transport.settingsService.Settings() + settings, err := transport.dataStore.Settings().Settings() if err != nil { return nil, err } @@ -610,13 +589,13 @@ func (transport *Transport) executeGenericResourceDeletionOperation(request *htt return response, err } - resourceControl, err := transport.resourceControlService.ResourceControlByResourceIDAndType(resourceIdentifierAttribute, resourceType) + resourceControl, err := transport.dataStore.ResourceControl().ResourceControlByResourceIDAndType(resourceIdentifierAttribute, resourceType) if err != nil { return response, err } if resourceControl != nil { - err = transport.resourceControlService.DeleteResourceControl(resourceControl.ID) + err = transport.dataStore.ResourceControl().DeleteResourceControl(resourceControl.ID) if err != nil { return response, err } @@ -661,13 +640,13 @@ func (transport *Transport) createRegistryAccessContext(request *http.Request) ( userID: tokenData.ID, } - hub, err := transport.dockerHubService.DockerHub() + hub, err := transport.dataStore.DockerHub().DockerHub() if err != nil { return nil, err } accessContext.dockerHub = hub - registries, err := transport.registryService.Registries() + registries, err := transport.dataStore.Registry().Registries() if err != nil { return nil, err } @@ -676,7 +655,7 @@ func (transport *Transport) createRegistryAccessContext(request *http.Request) ( if tokenData.Role != portainer.AdministratorRole { accessContext.isAdmin = false - teamMemberships, err := transport.teamMembershipService.TeamMembershipsByUserID(tokenData.ID) + teamMemberships, err := transport.dataStore.TeamMembership().TeamMembershipsByUserID(tokenData.ID) if err != nil { return nil, err } @@ -694,7 +673,7 @@ func (transport *Transport) createOperationContext(request *http.Request) (*rest return nil, err } - resourceControls, err := transport.resourceControlService.ResourceControls() + resourceControls, err := transport.dataStore.ResourceControl().ResourceControls() if err != nil { return nil, err } @@ -709,7 +688,7 @@ func (transport *Transport) createOperationContext(request *http.Request) (*rest if tokenData.Role != portainer.AdministratorRole { operationContext.isAdmin = false - user, err := transport.userService.User(operationContext.userID) + user, err := transport.dataStore.User().User(operationContext.userID) if err != nil { return nil, err } @@ -719,7 +698,7 @@ func (transport *Transport) createOperationContext(request *http.Request) (*rest operationContext.endpointResourceAccess = true } - teamMemberships, err := transport.teamMembershipService.TeamMembershipsByUserID(tokenData.ID) + teamMemberships, err := transport.dataStore.TeamMembership().TeamMembershipsByUserID(tokenData.ID) if err != nil { return nil, err } diff --git a/api/http/proxy/factory/docker_unix.go b/api/http/proxy/factory/docker_unix.go index 214b50747..32a572d30 100644 --- a/api/http/proxy/factory/docker_unix.go +++ b/api/http/proxy/factory/docker_unix.go @@ -12,18 +12,11 @@ import ( func (factory ProxyFactory) newOSBasedLocalProxy(path string, endpoint *portainer.Endpoint) (http.Handler, error) { transportParameters := &docker.TransportParameters{ - Endpoint: endpoint, - ResourceControlService: factory.resourceControlService, - UserService: factory.userService, - TeamService: factory.teamService, - TeamMembershipService: factory.teamMembershipService, - RegistryService: factory.registryService, - DockerHubService: factory.dockerHubService, - SettingsService: factory.settingsService, - ReverseTunnelService: factory.reverseTunnelService, - ExtensionService: factory.extensionService, - SignatureService: factory.signatureService, - DockerClientFactory: factory.dockerClientFactory, + Endpoint: endpoint, + DataStore: factory.dataStore, + ReverseTunnelService: factory.reverseTunnelService, + SignatureService: factory.signatureService, + DockerClientFactory: factory.dockerClientFactory, } proxy := &dockerLocalProxy{} diff --git a/api/http/proxy/factory/docker_windows.go b/api/http/proxy/factory/docker_windows.go index 50ba768f7..fb71b91d1 100644 --- a/api/http/proxy/factory/docker_windows.go +++ b/api/http/proxy/factory/docker_windows.go @@ -13,18 +13,11 @@ import ( func (factory ProxyFactory) newOSBasedLocalProxy(path string, endpoint *portainer.Endpoint) (http.Handler, error) { transportParameters := &docker.TransportParameters{ - Endpoint: endpoint, - ResourceControlService: factory.resourceControlService, - UserService: factory.userService, - TeamService: factory.teamService, - TeamMembershipService: factory.teamMembershipService, - RegistryService: factory.registryService, - DockerHubService: factory.dockerHubService, - SettingsService: factory.settingsService, - ReverseTunnelService: factory.reverseTunnelService, - ExtensionService: factory.extensionService, - SignatureService: factory.signatureService, - DockerClientFactory: factory.dockerClientFactory, + Endpoint: endpoint, + DataStore: factory.dataStore, + ReverseTunnelService: factory.reverseTunnelService, + SignatureService: factory.signatureService, + DockerClientFactory: factory.dockerClientFactory, } proxy := &dockerLocalProxy{} diff --git a/api/http/proxy/factory/factory.go b/api/http/proxy/factory/factory.go index 3f44b1211..b81291ab9 100644 --- a/api/http/proxy/factory/factory.go +++ b/api/http/proxy/factory/factory.go @@ -19,49 +19,20 @@ var extensionPorts = map[portainer.ExtensionID]string{ type ( // ProxyFactory is a factory to create reverse proxies to Docker endpoints and extensions ProxyFactory struct { - resourceControlService portainer.ResourceControlService - userService portainer.UserService - teamService portainer.TeamService - teamMembershipService portainer.TeamMembershipService - settingsService portainer.SettingsService - registryService portainer.RegistryService - dockerHubService portainer.DockerHubService - signatureService portainer.DigitalSignatureService - reverseTunnelService portainer.ReverseTunnelService - extensionService portainer.ExtensionService - dockerClientFactory *docker.ClientFactory - } - - // ProxyFactoryParameters is used to create a new ProxyFactory - ProxyFactoryParameters struct { - ResourceControlService portainer.ResourceControlService - UserService portainer.UserService - TeamService portainer.TeamService - TeamMembershipService portainer.TeamMembershipService - SettingsService portainer.SettingsService - RegistryService portainer.RegistryService - DockerHubService portainer.DockerHubService - SignatureService portainer.DigitalSignatureService - ReverseTunnelService portainer.ReverseTunnelService - ExtensionService portainer.ExtensionService - DockerClientFactory *docker.ClientFactory + dataStore portainer.DataStore + signatureService portainer.DigitalSignatureService + reverseTunnelService portainer.ReverseTunnelService + dockerClientFactory *docker.ClientFactory } ) // NewProxyFactory returns a pointer to a new instance of a ProxyFactory -func NewProxyFactory(parameters *ProxyFactoryParameters) *ProxyFactory { +func NewProxyFactory(dataStore portainer.DataStore, signatureService portainer.DigitalSignatureService, tunnelService portainer.ReverseTunnelService, clientFactory *docker.ClientFactory) *ProxyFactory { return &ProxyFactory{ - resourceControlService: parameters.ResourceControlService, - userService: parameters.UserService, - teamService: parameters.TeamService, - teamMembershipService: parameters.TeamMembershipService, - settingsService: parameters.SettingsService, - registryService: parameters.RegistryService, - dockerHubService: parameters.DockerHubService, - signatureService: parameters.SignatureService, - reverseTunnelService: parameters.ReverseTunnelService, - extensionService: parameters.ExtensionService, - dockerClientFactory: parameters.DockerClientFactory, + dataStore: dataStore, + signatureService: signatureService, + reverseTunnelService: tunnelService, + dockerClientFactory: clientFactory, } } diff --git a/api/http/proxy/manager.go b/api/http/proxy/manager.go index fa27f6399..336481f9c 100644 --- a/api/http/proxy/manager.go +++ b/api/http/proxy/manager.go @@ -20,44 +20,15 @@ type ( extensionProxies cmap.ConcurrentMap legacyExtensionProxies cmap.ConcurrentMap } - - // ManagerParams represents the required parameters to create a new Manager instance. - ManagerParams struct { - ResourceControlService portainer.ResourceControlService - UserService portainer.UserService - TeamService portainer.TeamService - TeamMembershipService portainer.TeamMembershipService - SettingsService portainer.SettingsService - RegistryService portainer.RegistryService - DockerHubService portainer.DockerHubService - SignatureService portainer.DigitalSignatureService - ReverseTunnelService portainer.ReverseTunnelService - ExtensionService portainer.ExtensionService - DockerClientFactory *docker.ClientFactory - } ) // NewManager initializes a new proxy Service -func NewManager(parameters *ManagerParams) *Manager { - proxyFactoryParameters := &factory.ProxyFactoryParameters{ - ResourceControlService: parameters.ResourceControlService, - UserService: parameters.UserService, - TeamService: parameters.TeamService, - TeamMembershipService: parameters.TeamMembershipService, - SettingsService: parameters.SettingsService, - RegistryService: parameters.RegistryService, - DockerHubService: parameters.DockerHubService, - SignatureService: parameters.SignatureService, - ReverseTunnelService: parameters.ReverseTunnelService, - ExtensionService: parameters.ExtensionService, - DockerClientFactory: parameters.DockerClientFactory, - } - +func NewManager(dataStore portainer.DataStore, signatureService portainer.DigitalSignatureService, tunnelService portainer.ReverseTunnelService, clientFactory *docker.ClientFactory) *Manager { return &Manager{ endpointProxies: cmap.New(), extensionProxies: cmap.New(), legacyExtensionProxies: cmap.New(), - proxyFactory: factory.NewProxyFactory(proxyFactoryParameters), + proxyFactory: factory.NewProxyFactory(dataStore, signatureService, tunnelService, clientFactory), } } diff --git a/api/http/security/bouncer.go b/api/http/security/bouncer.go index 5bcfe0c72..d6ded7f62 100644 --- a/api/http/security/bouncer.go +++ b/api/http/security/bouncer.go @@ -13,26 +13,10 @@ import ( type ( // RequestBouncer represents an entity that manages API request accesses RequestBouncer struct { - jwtService portainer.JWTService - userService portainer.UserService - teamMembershipService portainer.TeamMembershipService - endpointService portainer.EndpointService - endpointGroupService portainer.EndpointGroupService - extensionService portainer.ExtensionService - rbacExtensionClient *rbacExtensionClient - authDisabled bool - } - - // RequestBouncerParams represents the required parameters to create a new RequestBouncer instance. - RequestBouncerParams struct { - JWTService portainer.JWTService - UserService portainer.UserService - TeamMembershipService portainer.TeamMembershipService - EndpointService portainer.EndpointService - EndpointGroupService portainer.EndpointGroupService - ExtensionService portainer.ExtensionService - RBACExtensionURL string - AuthDisabled bool + dataStore portainer.DataStore + jwtService portainer.JWTService + rbacExtensionClient *rbacExtensionClient + authDisabled bool } // RestrictedRequestContext is a data structure containing information @@ -46,16 +30,12 @@ type ( ) // NewRequestBouncer initializes a new RequestBouncer -func NewRequestBouncer(parameters *RequestBouncerParams) *RequestBouncer { +func NewRequestBouncer(dataStore portainer.DataStore, jwtService portainer.JWTService, authenticationDisabled bool, rbacExtensionURL string) *RequestBouncer { return &RequestBouncer{ - jwtService: parameters.JWTService, - userService: parameters.UserService, - teamMembershipService: parameters.TeamMembershipService, - endpointService: parameters.EndpointService, - endpointGroupService: parameters.EndpointGroupService, - extensionService: parameters.ExtensionService, - rbacExtensionClient: newRBACExtensionClient(parameters.RBACExtensionURL), - authDisabled: parameters.AuthDisabled, + dataStore: dataStore, + jwtService: jwtService, + rbacExtensionClient: newRBACExtensionClient(rbacExtensionURL), + authDisabled: authenticationDisabled, } } @@ -121,12 +101,12 @@ func (bouncer *RequestBouncer) AuthorizedEndpointOperation(r *http.Request, endp return nil } - memberships, err := bouncer.teamMembershipService.TeamMembershipsByUserID(tokenData.ID) + memberships, err := bouncer.dataStore.TeamMembership().TeamMembershipsByUserID(tokenData.ID) if err != nil { return err } - group, err := bouncer.endpointGroupService.EndpointGroup(endpoint.GroupID) + group, err := bouncer.dataStore.EndpointGroup().EndpointGroup(endpoint.GroupID) if err != nil { return err } @@ -173,14 +153,14 @@ func (bouncer *RequestBouncer) checkEndpointOperationAuthorization(r *http.Reque return nil } - extension, err := bouncer.extensionService.Extension(portainer.RBACExtension) + extension, err := bouncer.dataStore.Extension().Extension(portainer.RBACExtension) if err == portainer.ErrObjectNotFound { return nil } else if err != nil { return err } - user, err := bouncer.userService.User(tokenData.ID) + user, err := bouncer.dataStore.User().User(tokenData.ID) if err != nil { return err } @@ -208,7 +188,7 @@ func (bouncer *RequestBouncer) RegistryAccess(r *http.Request, registry *portain return nil } - memberships, err := bouncer.teamMembershipService.TeamMembershipsByUserID(tokenData.ID) + memberships, err := bouncer.dataStore.TeamMembership().TeamMembershipsByUserID(tokenData.ID) if err != nil { return err } @@ -244,7 +224,7 @@ func (bouncer *RequestBouncer) mwCheckPortainerAuthorizations(next http.Handler, return } - extension, err := bouncer.extensionService.Extension(portainer.RBACExtension) + extension, err := bouncer.dataStore.Extension().Extension(portainer.RBACExtension) if err == portainer.ErrObjectNotFound { if administratorOnly { httperror.WriteError(w, http.StatusForbidden, "Access denied", portainer.ErrUnauthorized) @@ -258,7 +238,7 @@ func (bouncer *RequestBouncer) mwCheckPortainerAuthorizations(next http.Handler, return } - user, err := bouncer.userService.User(tokenData.ID) + user, err := bouncer.dataStore.User().User(tokenData.ID) if err != nil && err == portainer.ErrObjectNotFound { httperror.WriteError(w, http.StatusUnauthorized, "Unauthorized", portainer.ErrUnauthorized) return @@ -335,7 +315,7 @@ func (bouncer *RequestBouncer) mwCheckAuthentication(next http.Handler) http.Han return } - _, err = bouncer.userService.User(tokenData.ID) + _, err = bouncer.dataStore.User().User(tokenData.ID) if err != nil && err == portainer.ErrObjectNotFound { httperror.WriteError(w, http.StatusUnauthorized, "Unauthorized", portainer.ErrUnauthorized) return @@ -372,7 +352,7 @@ func (bouncer *RequestBouncer) newRestrictedContextRequest(userID portainer.User if userRole != portainer.AdministratorRole { requestContext.IsAdmin = false - memberships, err := bouncer.teamMembershipService.TeamMembershipsByUserID(userID) + memberships, err := bouncer.dataStore.TeamMembership().TeamMembershipsByUserID(userID) if err != nil { return nil, err } diff --git a/api/http/server.go b/api/http/server.go index fc9d742e6..40e2a880f 100644 --- a/api/http/server.go +++ b/api/http/server.go @@ -45,163 +45,87 @@ import ( // Server implements the portainer.Server interface type Server struct { - BindAddress string - AssetsPath string - AuthDisabled bool - Status *portainer.Status - ReverseTunnelService portainer.ReverseTunnelService - ExtensionManager portainer.ExtensionManager - ComposeStackManager portainer.ComposeStackManager - CryptoService portainer.CryptoService - SignatureService portainer.DigitalSignatureService - JobScheduler portainer.JobScheduler - Snapshotter portainer.Snapshotter - RoleService portainer.RoleService - DockerHubService portainer.DockerHubService - EdgeGroupService portainer.EdgeGroupService - EdgeStackService portainer.EdgeStackService - EndpointService portainer.EndpointService - EndpointGroupService portainer.EndpointGroupService - EndpointRelationService portainer.EndpointRelationService - FileService portainer.FileService - GitService portainer.GitService - JWTService portainer.JWTService - LDAPService portainer.LDAPService - ExtensionService portainer.ExtensionService - RegistryService portainer.RegistryService - ResourceControlService portainer.ResourceControlService - ScheduleService portainer.ScheduleService - SettingsService portainer.SettingsService - StackService portainer.StackService - SwarmStackManager portainer.SwarmStackManager - TagService portainer.TagService - TeamService portainer.TeamService - TeamMembershipService portainer.TeamMembershipService - UserService portainer.UserService - WebhookService portainer.WebhookService - Handler *handler.Handler - SSL bool - SSLCert string - SSLKey string - DockerClientFactory *docker.ClientFactory - JobService portainer.JobService + BindAddress string + AssetsPath string + AuthDisabled bool + Status *portainer.Status + ReverseTunnelService portainer.ReverseTunnelService + ExtensionManager portainer.ExtensionManager + ComposeStackManager portainer.ComposeStackManager + CryptoService portainer.CryptoService + SignatureService portainer.DigitalSignatureService + JobScheduler portainer.JobScheduler + Snapshotter portainer.Snapshotter + FileService portainer.FileService + DataStore portainer.DataStore + GitService portainer.GitService + JWTService portainer.JWTService + LDAPService portainer.LDAPService + SwarmStackManager portainer.SwarmStackManager + Handler *handler.Handler + SSL bool + SSLCert string + SSLKey string + DockerClientFactory *docker.ClientFactory + JobService portainer.JobService } // Start starts the HTTP server func (server *Server) Start() error { - proxyManagerParameters := &proxy.ManagerParams{ - ResourceControlService: server.ResourceControlService, - UserService: server.UserService, - TeamService: server.TeamService, - TeamMembershipService: server.TeamMembershipService, - SettingsService: server.SettingsService, - RegistryService: server.RegistryService, - DockerHubService: server.DockerHubService, - SignatureService: server.SignatureService, - ReverseTunnelService: server.ReverseTunnelService, - ExtensionService: server.ExtensionService, - DockerClientFactory: server.DockerClientFactory, - } - proxyManager := proxy.NewManager(proxyManagerParameters) + proxyManager := proxy.NewManager(server.DataStore, server.SignatureService, server.ReverseTunnelService, server.DockerClientFactory) - authorizationServiceParameters := &portainer.AuthorizationServiceParameters{ - EndpointService: server.EndpointService, - EndpointGroupService: server.EndpointGroupService, - RegistryService: server.RegistryService, - RoleService: server.RoleService, - TeamMembershipService: server.TeamMembershipService, - UserService: server.UserService, - } - authorizationService := portainer.NewAuthorizationService(authorizationServiceParameters) + authorizationService := portainer.NewAuthorizationService(server.DataStore) - requestBouncerParameters := &security.RequestBouncerParams{ - JWTService: server.JWTService, - UserService: server.UserService, - TeamMembershipService: server.TeamMembershipService, - EndpointService: server.EndpointService, - EndpointGroupService: server.EndpointGroupService, - ExtensionService: server.ExtensionService, - RBACExtensionURL: proxyManager.GetExtensionURL(portainer.RBACExtension), - AuthDisabled: server.AuthDisabled, - } - requestBouncer := security.NewRequestBouncer(requestBouncerParameters) + rbacExtensionURL := proxyManager.GetExtensionURL(portainer.RBACExtension) + requestBouncer := security.NewRequestBouncer(server.DataStore, server.JWTService, server.AuthDisabled, rbacExtensionURL) rateLimiter := security.NewRateLimiter(10, 1*time.Second, 1*time.Hour) var authHandler = auth.NewHandler(requestBouncer, rateLimiter, server.AuthDisabled) - authHandler.UserService = server.UserService + authHandler.DataStore = server.DataStore authHandler.CryptoService = server.CryptoService authHandler.JWTService = server.JWTService authHandler.LDAPService = server.LDAPService - authHandler.SettingsService = server.SettingsService - authHandler.TeamService = server.TeamService - authHandler.TeamMembershipService = server.TeamMembershipService - authHandler.ExtensionService = server.ExtensionService - authHandler.EndpointService = server.EndpointService - authHandler.EndpointGroupService = server.EndpointGroupService - authHandler.RoleService = server.RoleService authHandler.ProxyManager = proxyManager authHandler.AuthorizationService = authorizationService var roleHandler = roles.NewHandler(requestBouncer) - roleHandler.RoleService = server.RoleService + roleHandler.DataStore = server.DataStore var dockerHubHandler = dockerhub.NewHandler(requestBouncer) - dockerHubHandler.DockerHubService = server.DockerHubService + dockerHubHandler.DataStore = server.DataStore var edgeGroupsHandler = edgegroups.NewHandler(requestBouncer) - edgeGroupsHandler.EdgeGroupService = server.EdgeGroupService - edgeGroupsHandler.EdgeStackService = server.EdgeStackService - edgeGroupsHandler.EndpointService = server.EndpointService - edgeGroupsHandler.EndpointGroupService = server.EndpointGroupService - edgeGroupsHandler.EndpointRelationService = server.EndpointRelationService - edgeGroupsHandler.TagService = server.TagService + edgeGroupsHandler.DataStore = server.DataStore var edgeStacksHandler = edgestacks.NewHandler(requestBouncer) - edgeStacksHandler.EdgeGroupService = server.EdgeGroupService - edgeStacksHandler.EdgeStackService = server.EdgeStackService - edgeStacksHandler.EndpointService = server.EndpointService - edgeStacksHandler.EndpointGroupService = server.EndpointGroupService - edgeStacksHandler.EndpointRelationService = server.EndpointRelationService + edgeStacksHandler.DataStore = server.DataStore edgeStacksHandler.FileService = server.FileService edgeStacksHandler.GitService = server.GitService var edgeTemplatesHandler = edgetemplates.NewHandler(requestBouncer) - edgeTemplatesHandler.SettingsService = server.SettingsService + edgeTemplatesHandler.DataStore = server.DataStore var endpointHandler = endpoints.NewHandler(requestBouncer) + endpointHandler.DataStore = server.DataStore endpointHandler.AuthorizationService = authorizationService - endpointHandler.EdgeGroupService = server.EdgeGroupService - endpointHandler.EdgeStackService = server.EdgeStackService - endpointHandler.EndpointService = server.EndpointService - endpointHandler.EndpointGroupService = server.EndpointGroupService - endpointHandler.EndpointRelationService = server.EndpointRelationService endpointHandler.FileService = server.FileService endpointHandler.JobService = server.JobService endpointHandler.ProxyManager = proxyManager endpointHandler.ReverseTunnelService = server.ReverseTunnelService - endpointHandler.SettingsService = server.SettingsService endpointHandler.Snapshotter = server.Snapshotter - endpointHandler.TagService = server.TagService var endpointEdgeHandler = endpointedge.NewHandler(requestBouncer) - endpointEdgeHandler.EdgeStackService = server.EdgeStackService - endpointEdgeHandler.EndpointService = server.EndpointService + endpointEdgeHandler.DataStore = server.DataStore endpointEdgeHandler.FileService = server.FileService var endpointGroupHandler = endpointgroups.NewHandler(requestBouncer) + endpointGroupHandler.DataStore = server.DataStore endpointGroupHandler.AuthorizationService = authorizationService - endpointGroupHandler.EdgeGroupService = server.EdgeGroupService - endpointGroupHandler.EdgeStackService = server.EdgeStackService - endpointGroupHandler.EndpointService = server.EndpointService - endpointGroupHandler.EndpointGroupService = server.EndpointGroupService - endpointGroupHandler.EndpointRelationService = server.EndpointRelationService - endpointGroupHandler.TagService = server.TagService var endpointProxyHandler = endpointproxy.NewHandler(requestBouncer) - endpointProxyHandler.EndpointService = server.EndpointService + endpointProxyHandler.DataStore = server.DataStore endpointProxyHandler.ProxyManager = proxyManager - endpointProxyHandler.SettingsService = server.SettingsService endpointProxyHandler.ReverseTunnelService = server.ReverseTunnelService var fileHandler = file.NewHandler(filepath.Join(server.AssetsPath, "public")) @@ -209,70 +133,48 @@ func (server *Server) Start() error { var motdHandler = motd.NewHandler(requestBouncer) var extensionHandler = extensions.NewHandler(requestBouncer) - extensionHandler.ExtensionService = server.ExtensionService + extensionHandler.DataStore = server.DataStore extensionHandler.ExtensionManager = server.ExtensionManager - extensionHandler.EndpointGroupService = server.EndpointGroupService - extensionHandler.EndpointService = server.EndpointService - extensionHandler.RegistryService = server.RegistryService extensionHandler.AuthorizationService = authorizationService var registryHandler = registries.NewHandler(requestBouncer) - registryHandler.RegistryService = server.RegistryService - registryHandler.ExtensionService = server.ExtensionService + registryHandler.DataStore = server.DataStore registryHandler.FileService = server.FileService registryHandler.ProxyManager = proxyManager var resourceControlHandler = resourcecontrols.NewHandler(requestBouncer) - resourceControlHandler.ResourceControlService = server.ResourceControlService + resourceControlHandler.DataStore = server.DataStore var schedulesHandler = schedules.NewHandler(requestBouncer) - schedulesHandler.ScheduleService = server.ScheduleService - schedulesHandler.EndpointService = server.EndpointService + schedulesHandler.DataStore = server.DataStore schedulesHandler.FileService = server.FileService schedulesHandler.JobService = server.JobService schedulesHandler.JobScheduler = server.JobScheduler - schedulesHandler.SettingsService = server.SettingsService schedulesHandler.ReverseTunnelService = server.ReverseTunnelService var settingsHandler = settings.NewHandler(requestBouncer) - settingsHandler.SettingsService = server.SettingsService + settingsHandler.DataStore = server.DataStore settingsHandler.LDAPService = server.LDAPService settingsHandler.FileService = server.FileService settingsHandler.JobScheduler = server.JobScheduler - settingsHandler.ScheduleService = server.ScheduleService - settingsHandler.RoleService = server.RoleService - settingsHandler.ExtensionService = server.ExtensionService settingsHandler.AuthorizationService = authorizationService var stackHandler = stacks.NewHandler(requestBouncer) + stackHandler.DataStore = server.DataStore stackHandler.FileService = server.FileService - stackHandler.StackService = server.StackService - stackHandler.EndpointService = server.EndpointService - stackHandler.ResourceControlService = server.ResourceControlService stackHandler.SwarmStackManager = server.SwarmStackManager stackHandler.ComposeStackManager = server.ComposeStackManager stackHandler.GitService = server.GitService - stackHandler.RegistryService = server.RegistryService - stackHandler.DockerHubService = server.DockerHubService - stackHandler.SettingsService = server.SettingsService - stackHandler.UserService = server.UserService - stackHandler.ExtensionService = server.ExtensionService var tagHandler = tags.NewHandler(requestBouncer) - tagHandler.EdgeGroupService = server.EdgeGroupService - tagHandler.EdgeStackService = server.EdgeStackService - tagHandler.EndpointService = server.EndpointService - tagHandler.EndpointGroupService = server.EndpointGroupService - tagHandler.EndpointRelationService = server.EndpointRelationService - tagHandler.TagService = server.TagService + tagHandler.DataStore = server.DataStore var teamHandler = teams.NewHandler(requestBouncer) - teamHandler.TeamService = server.TeamService - teamHandler.TeamMembershipService = server.TeamMembershipService + teamHandler.DataStore = server.DataStore teamHandler.AuthorizationService = authorizationService var teamMembershipHandler = teammemberships.NewHandler(requestBouncer) - teamMembershipHandler.TeamMembershipService = server.TeamMembershipService + teamMembershipHandler.DataStore = server.DataStore teamMembershipHandler.AuthorizationService = authorizationService var statusHandler = status.NewHandler(requestBouncer, server.Status) @@ -280,28 +182,23 @@ func (server *Server) Start() error { var supportHandler = support.NewHandler(requestBouncer) var templatesHandler = templates.NewHandler(requestBouncer) - templatesHandler.SettingsService = server.SettingsService + templatesHandler.DataStore = server.DataStore var uploadHandler = upload.NewHandler(requestBouncer) uploadHandler.FileService = server.FileService var userHandler = users.NewHandler(requestBouncer, rateLimiter) - userHandler.UserService = server.UserService - userHandler.TeamService = server.TeamService - userHandler.TeamMembershipService = server.TeamMembershipService + userHandler.DataStore = server.DataStore userHandler.CryptoService = server.CryptoService - userHandler.ResourceControlService = server.ResourceControlService - userHandler.SettingsService = server.SettingsService userHandler.AuthorizationService = authorizationService var websocketHandler = websocket.NewHandler(requestBouncer) - websocketHandler.EndpointService = server.EndpointService + websocketHandler.DataStore = server.DataStore websocketHandler.SignatureService = server.SignatureService websocketHandler.ReverseTunnelService = server.ReverseTunnelService var webhookHandler = webhooks.NewHandler(requestBouncer) - webhookHandler.WebhookService = server.WebhookService - webhookHandler.EndpointService = server.EndpointService + webhookHandler.DataStore = server.DataStore webhookHandler.DockerClientFactory = server.DockerClientFactory server.Handler = &handler.Handler{ diff --git a/api/portainer.go b/api/portainer.go index a53c5355c..ae675174a 100644 --- a/api/portainer.go +++ b/api/portainer.go @@ -1,6 +1,8 @@ package portainer -import "time" +import ( + "time" +) type ( // AccessPolicy represent a policy that can be associated to a user or team @@ -71,6 +73,27 @@ type ( Close() error IsNew() bool MigrateData() error + + DockerHub() DockerHubService + EdgeGroup() EdgeGroupService + EdgeStack() EdgeStackService + Endpoint() EndpointService + EndpointGroup() EndpointGroupService + EndpointRelation() EndpointRelationService + Extension() ExtensionService + Registry() RegistryService + ResourceControl() ResourceControlService + Role() RoleService + Schedule() ScheduleService + Settings() SettingsService + Stack() StackService + Tag() TagService + TeamMembership() TeamMembershipService + Team() TeamService + TunnelServer() TunnelServerService + User() UserService + Version() VersionService + Webhook() WebhookService } // DockerHub represents all the required information to connect and use the