-
Notifications
You must be signed in to change notification settings - Fork 193
/
tagkeys.go
229 lines (191 loc) · 6.44 KB
/
tagkeys.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mockresourcemanager
import (
"context"
"crypto/md5"
"encoding/base64"
"fmt"
"strconv"
"strings"
"time"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
"k8s.io/klog/v2"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/resourcemanager/v3"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/pkg/storage"
longrunningpb "google.golang.org/genproto/googleapis/longrunning"
)
type TagKeys struct {
*MockService
pb.UnimplementedTagKeysServer
}
func (s *TagKeys) GetTagKey(ctx context.Context, req *pb.GetTagKeyRequest) (*pb.TagKey, error) {
name, err := s.parseTagKeyName(req.Name)
if err != nil {
return nil, err
}
fqn := name.String()
obj := &pb.TagKey{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
return nil, err
}
// We should verify that this is part of on of our projects, but ... it's a mock
return obj, nil
}
func (s *TagKeys) CreateTagKey(ctx context.Context, req *pb.CreateTagKeyRequest) (*longrunningpb.Operation, error) {
var namespacedName string
parent := req.GetTagKey().GetParent()
if strings.HasPrefix(parent, "projects/") {
projectName, err := projects.ParseProjectName(parent)
if err != nil {
return nil, err
}
project, err := s.projectsInternal.GetProject(projectName)
if err != nil {
return nil, err
}
namespacedName = project.ID + "/" + req.GetTagKey().GetShortName()
// Parent is normalized to the project number
req.GetTagKey().Parent = fmt.Sprintf("projects/%d", project.Number)
} else if strings.HasPrefix(parent, "organizations/") {
// We should check that the org exists, permissions etc, but ... it's a mock
namespacedName = strings.TrimPrefix(parent, "organizations/") + "/" + req.GetTagKey().GetShortName()
} else {
return nil, status.Errorf(codes.InvalidArgument, "parent %q is not valid", parent)
}
if req.ValidateOnly {
return nil, fmt.Errorf("ValidateOnly not yet implemented")
}
name := &tagKeyName{
ID: time.Now().UnixNano(),
}
fqn := name.String()
now := timestamppb.Now()
obj := proto.Clone(req.TagKey).(*pb.TagKey)
obj.CreateTime = now
obj.UpdateTime = now
obj.NamespacedName = namespacedName
obj.Etag = base64.StdEncoding.EncodeToString(computeEtag(obj))
obj.Name = fqn
if err := s.storage.Create(ctx, fqn, obj); err != nil {
return nil, err
}
metadata := &pb.CreateTagKeyMetadata{}
return s.operations.StartLRO(ctx, "", metadata, func() (proto.Message, error) {
return obj, nil
})
}
func (s *TagKeys) UpdateTagKey(ctx context.Context, req *pb.UpdateTagKeyRequest) (*longrunningpb.Operation, error) {
reqName := req.GetTagKey().GetName()
name, err := s.parseTagKeyName(reqName)
if err != nil {
return nil, err
}
if req.ValidateOnly {
return nil, fmt.Errorf("ValidateOnly not yet implemented")
}
fqn := name.String()
obj := &pb.TagKey{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
return nil, err
}
// We should verify that this is part of on of our projects, but ... it's a mock
// Fields to be updated. The mask may only contain `description` or
// `etag`. If omitted entirely, both `description` and `etag` are assumed to
// be significant.
paths := req.GetUpdateMask().GetPaths()
for _, path := range paths {
switch path {
case "description":
obj.Description = req.GetTagKey().GetDescription()
default:
return nil, status.Errorf(codes.InvalidArgument, "update_mask path %q not valid", path)
}
}
if len(paths) == 0 {
obj.Description = req.GetTagKey().GetDescription()
}
if err := s.storage.Update(ctx, fqn, obj); err != nil {
return nil, err
}
metadata := &pb.UpdateTagKeyMetadata{}
return s.operations.StartLRO(ctx, "", metadata, func() (proto.Message, error) {
return obj, nil
})
}
func (s *TagKeys) DeleteTagKey(ctx context.Context, req *pb.DeleteTagKeyRequest) (*longrunningpb.Operation, error) {
name, err := s.parseTagKeyName(req.Name)
if err != nil {
return nil, err
}
fqn := name.String()
// We should verify that this is part of on of our projects, but ... it's a mock
metadata := &pb.DeleteTagKeyMetadata{}
return s.operations.StartLRO(ctx, "", metadata, func() (proto.Message, error) {
// TagKey must not have any child TagValues
tagValueKind := (&pb.TagValue{}).ProtoReflect().Descriptor()
if err := s.storage.List(ctx, tagValueKind, storage.ListOptions{}, func(obj proto.Message) error {
tagValue := obj.(*pb.TagValue)
if tagValue.GetParent() == fqn {
return status.Errorf(codes.FailedPrecondition,
"TagKey: %s has child TagValues. Please list all TagValues under this key and delete them before retrying TagKey deletion.",
fqn)
}
return nil
}); err != nil {
return nil, err
}
deleted := &pb.TagKey{}
if err := s.storage.Delete(ctx, fqn, deleted); err != nil {
return nil, err
}
return deleted, nil
})
}
type tagKeyName struct {
ID int64
}
func (n *tagKeyName) String() string {
return fmt.Sprintf("tagKeys/%d", n.ID)
}
// parseTagKeyName parses a string into a tagKeyName.
// The expected form is tagKeys/<tagkeyName>
func (s *MockService) parseTagKeyName(name string) (*tagKeyName, error) {
tokens := strings.Split(name, "/")
if len(tokens) == 2 && tokens[0] == "tagKeys" {
n, err := strconv.ParseInt(tokens[1], 10, 64)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid (bad id)", name)
}
name := &tagKeyName{
ID: n,
}
return name, nil
} else {
return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name)
}
}
func computeEtag(obj proto.Message) []byte {
// TODO: Do we risk exposing internal fields? Doesn't matter on a mock, I guess
b, err := proto.Marshal(obj)
if err != nil {
klog.Fatalf("failed to marshal proto object: %v", err)
}
hash := md5.Sum(b)
return hash[:]
}