-
Notifications
You must be signed in to change notification settings - Fork 53
/
tokens.go
68 lines (62 loc) · 1.86 KB
/
tokens.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
package management
import (
"context"
corev1 "github.com/rancher/opni/pkg/apis/core/v1"
managementv1 "github.com/rancher/opni/pkg/apis/management/v1"
"github.com/rancher/opni/pkg/storage"
"github.com/rancher/opni/pkg/validation"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)
func (m *Server) CreateBootstrapToken(
ctx context.Context,
req *managementv1.CreateBootstrapTokenRequest,
) (*corev1.BootstrapToken, error) {
if err := validation.Validate(req); err != nil {
return nil, err
}
token, err := m.coreDataSource.StorageBackend().CreateToken(ctx, req.Ttl.AsDuration(),
storage.WithLabels(req.GetLabels()),
storage.WithCapabilities(req.GetCapabilities()),
storage.WithMaxUsages(req.GetMaxUsages()),
)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return token, nil
}
func (m *Server) RevokeBootstrapToken(
ctx context.Context,
ref *corev1.Reference,
) (*emptypb.Empty, error) {
if err := validation.Validate(ref); err != nil {
return nil, err
}
return &emptypb.Empty{}, m.coreDataSource.StorageBackend().DeleteToken(ctx, ref)
}
func (m *Server) ListBootstrapTokens(
ctx context.Context,
_ *emptypb.Empty,
) (*corev1.BootstrapTokenList, error) {
tokens, err := m.coreDataSource.StorageBackend().ListTokens(ctx)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
tokenList := &corev1.BootstrapTokenList{}
tokenList.Items = append(tokenList.Items, tokens...)
return tokenList, nil
}
func (m *Server) GetBootstrapToken(
ctx context.Context,
ref *corev1.Reference,
) (*corev1.BootstrapToken, error) {
if err := validation.Validate(ref); err != nil {
return nil, err
}
token, err := m.coreDataSource.StorageBackend().GetToken(ctx, ref)
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return token, nil
}