-
Notifications
You must be signed in to change notification settings - Fork 193
/
tagvalues.go
224 lines (186 loc) · 6.57 KB
/
tagvalues.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
// 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"
"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"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common/projects"
pb "github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/generated/mockgcp/cloud/resourcemanager/v3"
longrunningpb "google.golang.org/genproto/googleapis/longrunning"
)
type TagValues struct {
*MockService
pb.UnimplementedTagValuesServer
}
func (s *TagValues) GetTagValue(ctx context.Context, req *pb.GetTagValueRequest) (*pb.TagValue, error) {
name, err := s.parseTagValueName(req.Name)
if err != nil {
return nil, err
}
fqn := name.String()
obj := &pb.TagValue{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
if apierrors.IsNotFound(err) {
return nil, status.Errorf(codes.NotFound, "tagValue %q not found", name)
} else {
return nil, status.Errorf(codes.Internal, "error reading tagValue: %v", err)
}
}
// We should verify that this is part of on of our projects, but ... it's a mock
return obj, nil
}
func (s *TagValues) CreateTagValue(ctx context.Context, req *pb.CreateTagValueRequest) (*longrunningpb.Operation, error) {
parentName, err := s.parseTagKeyName(req.GetTagValue().GetParent())
if err != nil {
return nil, err
}
parentTagKey := &pb.TagKey{}
if err := s.storage.Get(ctx, parentName.String(), parentTagKey); err != nil {
if apierrors.IsNotFound(err) {
return nil, status.Errorf(codes.NotFound, "tagKey %q not found", req.GetTagValue().GetParent())
} else {
return nil, status.Errorf(codes.Internal, "error reading tagKey: %v", err)
}
}
namespacedName := ""
// We should verify that this is part of on of our projects, but ... it's a mock
tagKeyParent := parentTagKey.GetParent()
if strings.HasPrefix(tagKeyParent, "projects/") {
projectName, err := projects.ParseProjectName(tagKeyParent)
if err != nil {
return nil, err
}
project, err := s.projectsInternal.GetProject(projectName)
if err != nil {
return nil, err
}
namespacedName = project.ID + "/" + parentTagKey.GetShortName() + "/" + req.GetTagValue().GetShortName()
} else if strings.HasPrefix(tagKeyParent, "organizations/") {
// TODO: Set namespacedName: {organizationId}/{tag_key_short_name}/{tag_value_short_name}
} else {
return nil, status.Errorf(codes.InvalidArgument, "parent %q is not valid", tagKeyParent)
}
if req.ValidateOnly {
return nil, fmt.Errorf("ValidateOnly not yet implemented")
}
name := &tagValueName{
ID: time.Now().UnixNano(),
}
fqn := name.String()
now := timestamppb.Now()
obj := proto.Clone(req.TagValue).(*pb.TagValue)
obj.CreateTime = now
obj.UpdateTime = now
obj.NamespacedName = namespacedName
obj.Etag = base64.StdEncoding.EncodeToString(computeEtag(obj))
obj.Name = fqn
obj.Parent = parentTagKey.Name
if err := s.storage.Create(ctx, fqn, obj); err != nil {
return nil, status.Errorf(codes.Internal, "error creating tagValue: %v", err)
}
metadata := &pb.CreateTagValueMetadata{}
return s.operations.StartLRO(ctx, "", metadata, func() (proto.Message, error) {
return obj, nil
})
}
func (s *TagValues) UpdateTagValue(ctx context.Context, req *pb.UpdateTagValueRequest) (*longrunningpb.Operation, error) {
reqName := req.GetTagValue().GetName()
name, err := s.parseTagValueName(reqName)
if err != nil {
return nil, err
}
if req.ValidateOnly {
return nil, fmt.Errorf("ValidateOnly not yet implemented")
}
fqn := name.String()
obj := &pb.TagValue{}
if err := s.storage.Get(ctx, fqn, obj); err != nil {
if apierrors.IsNotFound(err) {
return nil, status.Errorf(codes.NotFound, "tagValue %q not found", reqName)
}
return nil, status.Errorf(codes.Internal, "error reading tagValue: %v", err)
}
// We should verify that this is part of on of our projects, but ... it's a mock
paths := req.GetUpdateMask().GetPaths()
for _, path := range paths {
switch path {
case "description":
obj.Description = req.GetTagValue().GetDescription()
default:
return nil, status.Errorf(codes.InvalidArgument, "update_mask path %q not valid", path)
}
}
if len(paths) == 0 {
obj.Description = req.GetTagValue().GetDescription()
}
if err := s.storage.Update(ctx, fqn, obj); err != nil {
return nil, status.Errorf(codes.Internal, "error updating tagValue: %v", err)
}
metadata := &pb.UpdateTagValueMetadata{}
return s.operations.StartLRO(ctx, "", metadata, func() (proto.Message, error) {
return obj, nil
})
}
func (s *TagValues) DeleteTagValue(ctx context.Context, req *pb.DeleteTagValueRequest) (*longrunningpb.Operation, error) {
name, err := s.parseTagValueName(req.Name)
if err != nil {
return nil, err
}
fqn := name.String()
deleted := &pb.TagValue{}
if err := s.storage.Delete(ctx, fqn, deleted); err != nil {
if apierrors.IsNotFound(err) {
return nil, status.Errorf(codes.NotFound, "tagValue %q not found", name)
} else {
return nil, status.Errorf(codes.Internal, "error deleting tagValue: %v", err)
}
}
// We should verify that this is part of on of our projects, but ... it's a mock
metadata := &pb.DeleteTagValueMetadata{}
return s.operations.StartLRO(ctx, "", metadata, func() (proto.Message, error) {
return deleted, nil
})
}
type tagValueName struct {
ID int64
}
func (n *tagValueName) String() string {
return fmt.Sprintf("tagValues/%d", n.ID)
}
// parseTagValueName parses a string into a tagValueName.
// The expected form is tagValues/<tagvalueName>
func (s *MockService) parseTagValueName(name string) (*tagValueName, error) {
tokens := strings.Split(name, "/")
if len(tokens) == 2 && tokens[0] == "tagValues" {
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 := &tagValueName{
ID: n,
}
return name, nil
} else {
return nil, status.Errorf(codes.InvalidArgument, "name %q is not valid", name)
}
}