go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/swarming/server/rpcs/bots_list_bots.go (about) 1 // Copyright 2024 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 rpcs 16 17 import ( 18 "context" 19 20 "google.golang.org/grpc/codes" 21 "google.golang.org/grpc/status" 22 "google.golang.org/protobuf/types/known/timestamppb" 23 24 "go.chromium.org/luci/common/clock" 25 "go.chromium.org/luci/common/logging" 26 "go.chromium.org/luci/gae/service/datastore" 27 28 apipb "go.chromium.org/luci/swarming/proto/api_v2" 29 "go.chromium.org/luci/swarming/server/acls" 30 "go.chromium.org/luci/swarming/server/model" 31 ) 32 33 // ListBots implements the corresponding RPC method. 34 func (srv *BotsServer) ListBots(ctx context.Context, req *apipb.BotsRequest) (*apipb.BotInfoListResponse, error) { 35 var err error 36 if req.Limit, err = ValidateLimit(req.Limit); err != nil { 37 return nil, status.Errorf(codes.InvalidArgument, "invalid limit: %s", err) 38 } 39 40 if req.Cursor != "" && !datastore.IsMultiCursorString(req.Cursor) { 41 return nil, status.Errorf(codes.InvalidArgument, "invalid cursor: wrong format") 42 } 43 44 dims, err := model.NewFilter(req.Dimensions) 45 if err != nil { 46 return nil, status.Errorf(codes.InvalidArgument, "invalid dimensions: %s", err) 47 } 48 49 // If the query is restricted to some set of pools, need the permission in all 50 // of them. Otherwise need the global server permissions, since an 51 // unrestricted query can return bots from any pool. 52 var res acls.CheckResult 53 state := State(ctx) 54 if pools := dims.Pools(); len(pools) != 0 { 55 res = state.ACL.CheckAllPoolsPerm(ctx, pools, acls.PermPoolsListBots) 56 } else { 57 res = state.ACL.CheckServerPerm(ctx, acls.PermPoolsListBots) 58 } 59 if !res.Permitted { 60 return nil, res.ToGrpcErr() 61 } 62 63 // Split the dimensions filter into simpler filters supported natively by 64 // the datastore with existing indices. In most cases there will be only one. 65 q := model.BotInfoQuery().Limit(req.Limit) 66 q = model.FilterBotsByState(q, model.StateFilter{ 67 Quarantined: req.Quarantined, 68 InMaintenance: req.InMaintenance, 69 IsDead: req.IsDead, 70 IsBusy: req.IsBusy, 71 }) 72 multi := model.FilterBotsByDimensions(q, srv.BotQuerySplitMode, dims) 73 if req.Cursor != "" { 74 multi, err = datastore.ApplyCursorString(ctx, multi, req.Cursor) 75 if err != nil { 76 return nil, status.Errorf(codes.InvalidArgument, "invalid cursor: %s", err) 77 } 78 } 79 80 out := &apipb.BotInfoListResponse{ 81 DeathTimeout: state.Config.Settings().BotDeathTimeoutSecs, 82 } 83 err = datastore.RunMulti(ctx, multi, func(bot *model.BotInfo, cb datastore.CursorCB) error { 84 out.Items = append(out.Items, bot.ToProto()) 85 if len(out.Items) == int(req.Limit) { 86 cursor, err := cb() 87 if err != nil { 88 return err 89 } 90 out.Cursor = cursor.String() 91 return datastore.Stop 92 } 93 return nil 94 }) 95 if err != nil { 96 logging.Errorf(ctx, "Error querying BotInfo: %s", err) 97 return nil, status.Errorf(codes.Internal, "datastore error fetching bots") 98 } 99 100 out.Now = timestamppb.New(clock.Now(ctx)) 101 return out, nil 102 }