mirror of
https://github.com/gravitational/teleport.git
synced 2026-11-03 16:04:29 +08:00
Attempts to reduce some of the boilerplate required to marshal a resource by moving common resource idenfitier handling to a utility method. This currently only works for resources that are proto in nature and not plain structs(access lists, secreports, discovery config, etc.).
96 lines
3.0 KiB
Go
96 lines
3.0 KiB
Go
/*
|
|
* Teleport
|
|
* Copyright (C) 2023 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 services
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gravitational/trace"
|
|
|
|
"github.com/gravitational/teleport/api/types"
|
|
"github.com/gravitational/teleport/lib/utils"
|
|
)
|
|
|
|
// 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)
|
|
|
|
// DeleteDatabaseService removes the specified DatabaseService resource.
|
|
DeleteDatabaseService(ctx context.Context, name string) error
|
|
|
|
// DeleteAllDatabaseServices removes all DatabaseService resources.
|
|
DeleteAllDatabaseServices(context.Context) error
|
|
}
|
|
|
|
// MarshalDatabaseService marshals the DatabaseService resource to JSON.
|
|
func MarshalDatabaseService(databaseService types.DatabaseService, opts ...MarshalOption) ([]byte, error) {
|
|
cfg, err := CollectOptions(opts)
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
|
|
switch databaseService := databaseService.(type) {
|
|
case *types.DatabaseServiceV1:
|
|
if err := databaseService.CheckAndSetDefaults(); err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
|
|
return utils.FastMarshal(maybeResetProtoResourceID(cfg.PreserveResourceID, databaseService))
|
|
default:
|
|
return nil, trace.BadParameter("unrecognized DatabaseService version %T", databaseService)
|
|
}
|
|
}
|
|
|
|
// UnmarshalDatabaseService unmarshals the DatabaseService resource from JSON.
|
|
func UnmarshalDatabaseService(data []byte, opts ...MarshalOption) (types.DatabaseService, error) {
|
|
if len(data) == 0 {
|
|
return nil, trace.BadParameter("missing DatabaseService data")
|
|
}
|
|
cfg, err := CollectOptions(opts)
|
|
if err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
var h types.ResourceHeader
|
|
if err := utils.FastUnmarshal(data, &h); err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
switch h.Version {
|
|
case types.V1:
|
|
var s types.DatabaseServiceV1
|
|
if err := utils.FastUnmarshal(data, &s); err != nil {
|
|
return nil, trace.BadParameter(err.Error())
|
|
}
|
|
if err := s.CheckAndSetDefaults(); err != nil {
|
|
return nil, trace.Wrap(err)
|
|
}
|
|
if cfg.ID != 0 {
|
|
s.SetResourceID(cfg.ID)
|
|
}
|
|
if cfg.Revision != "" {
|
|
s.SetRevision(cfg.Revision)
|
|
}
|
|
if !cfg.Expires.IsZero() {
|
|
s.SetExpiry(cfg.Expires)
|
|
}
|
|
return &s, nil
|
|
}
|
|
return nil, trace.BadParameter("database service resource version %q is not supported", h.Version)
|
|
}
|