go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/swarming/server/rpcs/tasks_get_request.go (about)

     1  // Copyright 2023 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/protobuf/types/known/timestamppb"
    21  
    22  	apipb "go.chromium.org/luci/swarming/proto/api_v2"
    23  	"go.chromium.org/luci/swarming/server/acls"
    24  	"go.chromium.org/luci/swarming/server/model"
    25  )
    26  
    27  // GetRequest fetches a model.TaskRequest for a given apipb.TaskIdRequest.
    28  func (*TasksServer) GetRequest(ctx context.Context, req *apipb.TaskIdRequest) (*apipb.TaskRequestResponse, error) {
    29  	taskRequest, err := FetchTaskRequest(ctx, req.TaskId)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	res := State(ctx).ACL.CheckTaskPerm(ctx, taskRequest.TaskAuthInfo(), acls.PermTasksGet)
    34  	if !res.Permitted {
    35  		return nil, res.ToGrpcErr()
    36  	}
    37  	return taskRequestToResponse(ctx, req, taskRequest)
    38  }
    39  
    40  // taskRequestToResponse converts a model.TaskRequest to apipb.TaskRequestResponse.
    41  func taskRequestToResponse(ctx context.Context, req *apipb.TaskIdRequest, taskRequest *model.TaskRequest) (*apipb.TaskRequestResponse, error) {
    42  	// Create the list of task slices.
    43  	taskSlices := make([]*apipb.TaskSlice, len(taskRequest.TaskSlices))
    44  	for i, slice := range taskRequest.TaskSlices {
    45  		taskSlices[i] = slice.ToProto()
    46  	}
    47  	// Ensure that there is a task slice to pull TaskProperties from.
    48  	properties := &apipb.TaskProperties{}
    49  	if len(taskSlices) != 0 {
    50  		properties = taskSlices[0].Properties
    51  	}
    52  	return &apipb.TaskRequestResponse{
    53  		TaskId:               req.TaskId,
    54  		ExpirationSecs:       int32(taskRequest.Expiration.Second()),
    55  		Name:                 taskRequest.Name,
    56  		ParentTaskId:         taskRequest.ParentTaskID.Get(),
    57  		Priority:             int32(taskRequest.Priority),
    58  		Properties:           properties,
    59  		Tags:                 taskRequest.Tags,
    60  		CreatedTs:            timestamppb.New(taskRequest.Created),
    61  		User:                 taskRequest.User,
    62  		Authenticated:        string(taskRequest.Authenticated),
    63  		TaskSlices:           taskSlices,
    64  		ServiceAccount:       taskRequest.ServiceAccount,
    65  		Realm:                taskRequest.Realm,
    66  		PubsubTopic:          taskRequest.PubSubTopic,
    67  		PubsubUserdata:       taskRequest.PubSubUserData,
    68  		BotPingToleranceSecs: int32(taskRequest.BotPingToleranceSecs),
    69  		RbeInstance:          taskRequest.RBEInstance,
    70  		Resultdb:             taskRequest.ResultDB.ToProto(),
    71  	}, nil
    72  }