go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gce/appengine/rpc/memory/config.go (about) 1 // Copyright 2018 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package memory 16 17 import ( 18 "context" 19 "sync" 20 21 "google.golang.org/grpc/codes" 22 "google.golang.org/grpc/status" 23 "google.golang.org/protobuf/types/known/emptypb" 24 25 "go.chromium.org/luci/gce/api/config/v1" 26 ) 27 28 // Ensure Config implements config.ConfigurationServer. 29 var _ config.ConfigurationServer = &Config{} 30 31 // Config implements config.ConfigurationServer. 32 type Config struct { 33 // cfg is a map of IDs to known configs. 34 cfg sync.Map 35 } 36 37 // Delete handles a request to delete a config. 38 func (srv *Config) Delete(c context.Context, req *config.DeleteRequest) (*emptypb.Empty, error) { 39 srv.cfg.Delete(req.GetId()) 40 return &emptypb.Empty{}, nil 41 } 42 43 // Ensure handles a request to create or update a config. 44 func (srv *Config) Ensure(c context.Context, req *config.EnsureRequest) (*config.Config, error) { 45 srv.cfg.Store(req.GetId(), req.GetConfig()) 46 return req.GetConfig(), nil 47 } 48 49 // Get handles a request to get a config. 50 func (srv *Config) Get(c context.Context, req *config.GetRequest) (*config.Config, error) { 51 cfg, ok := srv.cfg.Load(req.GetId()) 52 if !ok { 53 return nil, status.Errorf(codes.NotFound, "no config found with ID %q", req.GetId()) 54 } 55 return cfg.(*config.Config), nil 56 } 57 58 // List handles a request to list all configs. 59 func (srv *Config) List(c context.Context, req *config.ListRequest) (*config.ListResponse, error) { 60 rsp := &config.ListResponse{} 61 // TODO(smut): Handle page tokens. 62 if req.GetPageToken() != "" { 63 return rsp, nil 64 } 65 srv.cfg.Range(func(_, val any) bool { 66 rsp.Configs = append(rsp.Configs, val.(*config.Config)) 67 return true 68 }) 69 return rsp, nil 70 } 71 72 // Update handles a request to update a config. 73 func (srv *Config) Update(c context.Context, req *config.UpdateRequest) (*config.Config, error) { 74 _, ok := srv.cfg.Load(req.GetId()) 75 if !ok { 76 return nil, status.Errorf(codes.NotFound, "no config found with ID %q", req.GetId()) 77 } 78 // TODO(smut): Handle updates. 79 return nil, status.Errorf(codes.InvalidArgument, "configs are immutable") 80 }