Files
teleport/lib/services/networking.go
T
rosstimothy dad8c4475c Add helper method to preserve resource identifiers (#35573)
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.).
2023-12-14 19:17:53 +00:00

80 lines
2.3 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 (
"github.com/gravitational/trace"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/utils"
)
// UnmarshalClusterNetworkingConfig unmarshals the ClusterNetworkingConfig resource from JSON.
func UnmarshalClusterNetworkingConfig(bytes []byte, opts ...MarshalOption) (types.ClusterNetworkingConfig, error) {
var netConfig types.ClusterNetworkingConfigV2
if len(bytes) == 0 {
return nil, trace.BadParameter("missing resource data")
}
cfg, err := CollectOptions(opts)
if err != nil {
return nil, trace.Wrap(err)
}
if err := utils.FastUnmarshal(bytes, &netConfig); err != nil {
return nil, trace.BadParameter(err.Error())
}
err = netConfig.CheckAndSetDefaults()
if err != nil {
return nil, trace.Wrap(err)
}
if cfg.ID != 0 {
netConfig.SetResourceID(cfg.ID)
}
if cfg.Revision != "" {
netConfig.SetRevision(cfg.Revision)
}
if !cfg.Expires.IsZero() {
netConfig.SetExpiry(cfg.Expires)
}
return &netConfig, nil
}
// MarshalClusterNetworkingConfig marshals the ClusterNetworkingConfig resource to JSON.
func MarshalClusterNetworkingConfig(netConfig types.ClusterNetworkingConfig, opts ...MarshalOption) ([]byte, error) {
cfg, err := CollectOptions(opts)
if err != nil {
return nil, trace.Wrap(err)
}
switch netConfig := netConfig.(type) {
case *types.ClusterNetworkingConfigV2:
if err := netConfig.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return utils.FastMarshal(maybeResetProtoResourceID(cfg.PreserveResourceID, netConfig))
default:
return nil, trace.BadParameter("unrecognized cluster networking config version %T", netConfig)
}
}