mirror of
https://github.com/gravitational/teleport.git
synced 2026-10-31 03:27:49 +08:00
* GitHub proxy part 2.5: git_server cache * revert event * fix getAll * review comments
96 lines
2.9 KiB
Go
96 lines
2.9 KiB
Go
/*
|
|
* Teleport
|
|
* Copyright (C) 2024 Gravitational, Inc.
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package cache
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gravitational/trace"
|
|
|
|
apidefaults "github.com/gravitational/teleport/api/defaults"
|
|
"github.com/gravitational/teleport/api/types"
|
|
"github.com/gravitational/teleport/lib/services"
|
|
)
|
|
|
|
func (c *Cache) GetGitServer(ctx context.Context, name string) (types.Server, error) {
|
|
ctx, span := c.Tracer.Start(ctx, "cache/GetGitServer")
|
|
defer span.End()
|
|
|
|
rg, err := readCollectionCache(c, c.collections.gitServers)
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
defer rg.Release()
|
|
return rg.reader.GetGitServer(ctx, name)
|
|
}
|
|
|
|
func (c *Cache) ListGitServers(ctx context.Context, pageSize int, pageToken string) ([]types.Server, string, error) {
|
|
ctx, span := c.Tracer.Start(ctx, "cache/ListGitServers")
|
|
defer span.End()
|
|
|
|
rg, err := readCollectionCache(c, c.collections.gitServers)
|
|
if err != nil {
|
|
return nil, "", trace.Wrap(err)
|
|
}
|
|
defer rg.Release()
|
|
return rg.reader.ListGitServers(ctx, pageSize, pageToken)
|
|
}
|
|
|
|
type gitServerExecutor struct{}
|
|
|
|
func (gitServerExecutor) getAll(ctx context.Context, cache *Cache, loadSecrets bool) (all []types.Server, err error) {
|
|
var page []types.Server
|
|
var nextToken string
|
|
for {
|
|
page, nextToken, err = cache.GitServers.ListGitServers(ctx, apidefaults.DefaultChunkSize, nextToken)
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
all = append(all, page...)
|
|
if nextToken == "" {
|
|
break
|
|
}
|
|
}
|
|
return all, nil
|
|
}
|
|
|
|
func (gitServerExecutor) upsert(ctx context.Context, cache *Cache, resource types.Server) error {
|
|
_, err := cache.gitServersCache.UpsertGitServer(ctx, resource)
|
|
return trace.Wrap(err)
|
|
}
|
|
|
|
func (gitServerExecutor) deleteAll(ctx context.Context, cache *Cache) error {
|
|
return cache.gitServersCache.DeleteAllGitServers(ctx)
|
|
}
|
|
|
|
func (gitServerExecutor) delete(ctx context.Context, cache *Cache, resource types.Resource) error {
|
|
return cache.gitServersCache.DeleteGitServer(ctx, resource.GetName())
|
|
}
|
|
|
|
func (gitServerExecutor) isSingleton() bool { return false }
|
|
|
|
func (gitServerExecutor) getReader(cache *Cache, cacheOK bool) services.GitServerGetter {
|
|
if cacheOK {
|
|
return cache.gitServersCache
|
|
}
|
|
return cache.Config.GitServers
|
|
}
|
|
|
|
var _ executor[types.Server, services.GitServerGetter] = gitServerExecutor{}
|