go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/gce/appengine/rpc/memory/projects.go (about)

     1  // Copyright 2019 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/projects/v1"
    26  )
    27  
    28  // Ensure Projects implements projects.ProjectsServer.
    29  var _ projects.ProjectsServer = &Projects{}
    30  
    31  // Projects implements projects.ProjectsServer.
    32  type Projects struct {
    33  	// cfg is a map of IDs to known projects.
    34  	cfg sync.Map
    35  }
    36  
    37  // Delete handles a request to delete a project.
    38  func (srv *Projects) Delete(c context.Context, req *projects.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 project.
    44  func (srv *Projects) Ensure(c context.Context, req *projects.EnsureRequest) (*projects.Config, error) {
    45  	srv.cfg.Store(req.GetId(), req.GetProject())
    46  	return req.GetProject(), nil
    47  }
    48  
    49  // Get handles a request to get a project.
    50  func (srv *Projects) Get(c context.Context, req *projects.GetRequest) (*projects.Config, error) {
    51  	cfg, ok := srv.cfg.Load(req.GetId())
    52  	if !ok {
    53  		return nil, status.Errorf(codes.NotFound, "no project found with ID %q", req.GetId())
    54  	}
    55  	return cfg.(*projects.Config), nil
    56  }
    57  
    58  // List handles a request to list all projects.
    59  func (srv *Projects) List(c context.Context, req *projects.ListRequest) (*projects.ListResponse, error) {
    60  	rsp := &projects.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.Projects = append(rsp.Projects, val.(*projects.Config))
    67  		return true
    68  	})
    69  	return rsp, nil
    70  }