mirror of
https://github.com/gravitational/teleport.git
synced 2026-10-29 17:58:15 +08:00
DatabaseService: move Upsert op to DBServicesService (#20930)
This commit is contained in:
Vendored
+2
-2
@@ -2248,7 +2248,7 @@ func TestDatabaseServices(t *testing.T) {
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
_, err = p.presenceS.UpsertDatabaseService(ctx, service)
|
||||
_, err = p.databaseServices.UpsertDatabaseService(ctx, service)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Check that the DatabaseService is now in the backend.
|
||||
@@ -2284,7 +2284,7 @@ func TestDatabaseServices(t *testing.T) {
|
||||
|
||||
// Update the DatabaseService and upsert it into the backend again.
|
||||
service.SetExpiry(time.Now().Add(30 * time.Minute).UTC())
|
||||
_, err = p.presenceS.UpsertDatabaseService(context.Background(), service)
|
||||
_, err = p.databaseServices.UpsertDatabaseService(context.Background(), service)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Check that the DatabaseService is in the backend and only one exists (so an
|
||||
|
||||
Vendored
+2
-2
@@ -1404,7 +1404,7 @@ func (s *databaseService) fetch(ctx context.Context) (apply func(ctx context.Con
|
||||
}
|
||||
|
||||
for _, resource := range databaseServices {
|
||||
if _, err := s.presenceCache.UpsertDatabaseService(ctx, resource); err != nil {
|
||||
if _, err := s.databaseServicesCache.UpsertDatabaseService(ctx, resource); err != nil {
|
||||
return trace.Wrap(err)
|
||||
}
|
||||
}
|
||||
@@ -1435,7 +1435,7 @@ func (s *databaseService) processEvent(ctx context.Context, event types.Event) e
|
||||
if !ok {
|
||||
return trace.BadParameter("unexpected type %T", event.Resource)
|
||||
}
|
||||
if _, err := s.presenceCache.UpsertDatabaseService(ctx, resource); err != nil {
|
||||
if _, err := s.databaseServicesCache.UpsertDatabaseService(ctx, resource); err != nil {
|
||||
return trace.Wrap(err)
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -27,8 +27,12 @@ import (
|
||||
|
||||
// DatabaseServices defines an interface for managing DatabaseService resources.
|
||||
type DatabaseServices interface {
|
||||
// UpsertDatabaseService updates an existing DatabaseService resource.
|
||||
UpsertDatabaseService(context.Context, types.DatabaseService) (*types.KeepAlive, error)
|
||||
|
||||
// DeleteDatabasService removes the specified DatabaseService resource.
|
||||
DeleteDatabaseService(ctx context.Context, name string) error
|
||||
|
||||
// DeleteAllDatabaseServices removes all DatabaseService resources.
|
||||
DeleteAllDatabaseServices(context.Context) error
|
||||
}
|
||||
|
||||
@@ -21,7 +21,10 @@ import (
|
||||
|
||||
"github.com/gravitational/trace"
|
||||
|
||||
apidefaults "github.com/gravitational/teleport/api/defaults"
|
||||
"github.com/gravitational/teleport/api/types"
|
||||
"github.com/gravitational/teleport/lib/backend"
|
||||
"github.com/gravitational/teleport/lib/services"
|
||||
)
|
||||
|
||||
// DatabaseServicesService manages DatabaseService resources in the backend.
|
||||
@@ -34,6 +37,38 @@ func NewDatabaseServicesService(backend backend.Backend) *DatabaseServicesServic
|
||||
return &DatabaseServicesService{Backend: backend}
|
||||
}
|
||||
|
||||
// UpsertDatabaseService creates or updates (by name) a DatabaseService resource.
|
||||
func (s *DatabaseServicesService) UpsertDatabaseService(ctx context.Context, service types.DatabaseService) (*types.KeepAlive, error) {
|
||||
if err := service.CheckAndSetDefaults(); err != nil {
|
||||
return nil, trace.Wrap(err)
|
||||
}
|
||||
value, err := services.MarshalDatabaseService(service)
|
||||
if err != nil {
|
||||
return nil, trace.Wrap(err)
|
||||
}
|
||||
item := backend.Item{
|
||||
Key: backend.Key(databaseServicePrefix, service.GetName()),
|
||||
Value: value,
|
||||
Expires: service.Expiry(),
|
||||
ID: service.GetResourceID(),
|
||||
}
|
||||
lease, err := s.Put(ctx, item)
|
||||
if err != nil {
|
||||
return nil, trace.Wrap(err)
|
||||
}
|
||||
if service.Expiry().IsZero() {
|
||||
return &types.KeepAlive{}, nil
|
||||
}
|
||||
return &types.KeepAlive{
|
||||
Type: types.KeepAlive_DATABASE,
|
||||
LeaseID: lease.ID,
|
||||
Namespace: apidefaults.Namespace,
|
||||
Name: service.GetName(),
|
||||
HostID: service.GetName(),
|
||||
Expires: service.Expiry(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// DeleteDatabaseService removes the specified DatabaseService resource.
|
||||
func (s *DatabaseServicesService) DeleteDatabaseService(ctx context.Context, name string) error {
|
||||
err := s.Delete(ctx, backend.Key(databaseServicePrefix, name))
|
||||
|
||||
@@ -40,7 +40,6 @@ func TestDatabaseServices(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
service := NewDatabaseServicesService(bk)
|
||||
presence := NewPresenceService(bk)
|
||||
|
||||
// Upsert some DatabaseServices.
|
||||
ds1, err := types.NewDatabaseServiceV1(
|
||||
@@ -56,7 +55,7 @@ func TestDatabaseServices(t *testing.T) {
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
_, err = presence.UpsertDatabaseService(ctx, ds1)
|
||||
_, err = service.UpsertDatabaseService(ctx, ds1)
|
||||
require.NoError(t, err)
|
||||
|
||||
ds2, err := types.NewDatabaseServiceV1(
|
||||
@@ -72,7 +71,7 @@ func TestDatabaseServices(t *testing.T) {
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
_, err = presence.UpsertDatabaseService(ctx, ds2)
|
||||
_, err = service.UpsertDatabaseService(ctx, ds2)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Replace the DS1
|
||||
@@ -81,7 +80,7 @@ func TestDatabaseServices(t *testing.T) {
|
||||
Labels: &types.Labels{"env": []string{"ds1", "ds2"}},
|
||||
}
|
||||
ds1.Spec.ResourceMatchers = initialResourceMatchers
|
||||
_, err = presence.UpsertDatabaseService(ctx, ds1)
|
||||
_, err = service.UpsertDatabaseService(ctx, ds1)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Remove one of the DatabaseServices
|
||||
|
||||
@@ -1580,38 +1580,6 @@ func (s *PresenceService) listResources(ctx context.Context, req proto.ListResou
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UpsertDatabaseService creates or updates (by name) a DatabaseService resource.
|
||||
func (s *PresenceService) UpsertDatabaseService(ctx context.Context, service types.DatabaseService) (*types.KeepAlive, error) {
|
||||
if err := service.CheckAndSetDefaults(); err != nil {
|
||||
return nil, trace.Wrap(err)
|
||||
}
|
||||
value, err := services.MarshalDatabaseService(service)
|
||||
if err != nil {
|
||||
return nil, trace.Wrap(err)
|
||||
}
|
||||
item := backend.Item{
|
||||
Key: backend.Key(databaseServicePrefix, service.GetName()),
|
||||
Value: value,
|
||||
Expires: service.Expiry(),
|
||||
ID: service.GetResourceID(),
|
||||
}
|
||||
lease, err := s.Put(ctx, item)
|
||||
if err != nil {
|
||||
return nil, trace.Wrap(err)
|
||||
}
|
||||
if service.Expiry().IsZero() {
|
||||
return &types.KeepAlive{}, nil
|
||||
}
|
||||
return &types.KeepAlive{
|
||||
Type: types.KeepAlive_DATABASE,
|
||||
LeaseID: lease.ID,
|
||||
Namespace: apidefaults.Namespace,
|
||||
Name: service.GetName(),
|
||||
HostID: service.GetName(),
|
||||
Expires: service.Expiry(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// listResourcesWithSort supports sorting by falling back to retrieving all resources
|
||||
// with GetXXXs, filter, and then fake pagination.
|
||||
func (s *PresenceService) listResourcesWithSort(ctx context.Context, req proto.ListResourcesRequest) (*types.ListResourcesResponse, error) {
|
||||
|
||||
@@ -237,9 +237,6 @@ type Presence interface {
|
||||
|
||||
// ListResoures returns a paginated list of resources.
|
||||
ListResources(ctx context.Context, req proto.ListResourcesRequest) (*types.ListResourcesResponse, error)
|
||||
|
||||
// UpsertDatabaseService updates an existing DatabaseService resource.
|
||||
UpsertDatabaseService(context.Context, types.DatabaseService) (*types.KeepAlive, error)
|
||||
}
|
||||
|
||||
// PresenceInternal extends the Presence interface with auth-specific internal methods.
|
||||
|
||||
Reference in New Issue
Block a user