forked from gravitational/teleport
/
usergroup.go
99 lines (90 loc) · 3.16 KB
/
usergroup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* 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"
)
// UserGroups defines an interface for managing UserGroups.
type UserGroups interface {
// ListUserGroups returns a paginated list of all user group resources.
ListUserGroups(context.Context, int, string) ([]types.UserGroup, string, error)
// GetUserGroup returns the specified user group resources.
GetUserGroup(ctx context.Context, name string) (types.UserGroup, error)
// CreateUserGroup creates a new user group resource.
CreateUserGroup(context.Context, types.UserGroup) error
// UpdateUserGroup updates an existing user group resource.
UpdateUserGroup(context.Context, types.UserGroup) error
// DeleteUserGroup removes the specified user group resource.
DeleteUserGroup(ctx context.Context, name string) error
// DeleteAllUserGroups removes all user groups.
DeleteAllUserGroups(context.Context) error
}
// MarshalUserGroup marshals the user group resource to JSON.
func MarshalUserGroup(group types.UserGroup, opts ...MarshalOption) ([]byte, error) {
cfg, err := CollectOptions(opts)
if err != nil {
return nil, trace.Wrap(err)
}
switch g := group.(type) {
case *types.UserGroupV1:
if err := g.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
return utils.FastMarshal(maybeResetProtoResourceID(cfg.PreserveResourceID, g))
default:
return nil, trace.BadParameter("unsupported user group resource %T", g)
}
}
// UnmarshalUserGroup unmarshals user group resource from JSON.
func UnmarshalUserGroup(data []byte, opts ...MarshalOption) (types.UserGroup, error) {
if len(data) == 0 {
return nil, trace.BadParameter("missing group 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 g types.UserGroupV1
if err := utils.FastUnmarshal(data, &g); err != nil {
return nil, trace.BadParameter(err.Error())
}
if err := g.CheckAndSetDefaults(); err != nil {
return nil, trace.Wrap(err)
}
if cfg.ID != 0 {
g.SetResourceID(cfg.ID)
}
if cfg.Revision != "" {
g.SetRevision(cfg.Revision)
}
if !cfg.Expires.IsZero() {
g.SetExpiry(cfg.Expires)
}
return &g, nil
}
return nil, trace.BadParameter("unsupported user group resource version %q", h.Version)
}