| 409 | } |
| 410 | |
| 411 | func getResourceFromRequest(ctx context.Context, request any, method string) ([]string, error) { |
| 412 | pm, ok := request.(proto.Message) |
| 413 | if !ok { |
| 414 | return nil, errors.Errorf("invalid request for method %q", method) |
| 415 | } |
| 416 | mr := pm.ProtoReflect() |
| 417 | |
| 418 | methodTokens := strings.Split(method, "/") |
| 419 | if len(methodTokens) != 3 { |
| 420 | return nil, errors.Errorf("invalid method %q", method) |
| 421 | } |
| 422 | shortMethod := methodTokens[2] |
| 423 | |
| 424 | var resources []string |
| 425 | |
| 426 | // Transferring database projects needs to check both projects. |
| 427 | var updateDatabaseRequests []*v1pb.UpdateDatabaseRequest |
| 428 | switch r := request.(type) { |
| 429 | case *v1pb.UpdateDatabaseRequest: |
| 430 | updateDatabaseRequests = append(updateDatabaseRequests, r) |
| 431 | case *v1pb.BatchUpdateDatabasesRequest: |
| 432 | updateDatabaseRequests = append(updateDatabaseRequests, r.Requests...) |
| 433 | default: |
| 434 | } |
| 435 | for _, r := range updateDatabaseRequests { |
| 436 | if hasPath(r.GetUpdateMask(), "project") { |
| 437 | projectID, err := common.GetProjectID(r.GetDatabase().GetProject()) |
| 438 | if err != nil { |
| 439 | return nil, errors.Wrapf(err, "failed to get projectID from %q", r.GetDatabase().GetProject()) |
| 440 | } |
| 441 | // Allow to transfer databases to the default project. |
| 442 | if common.IsDefaultProject(common.GetWorkspaceIDFromContext(ctx), projectID) { |
| 443 | continue |
| 444 | } |
| 445 | resources = append(resources, r.GetDatabase().GetProject()) |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | // HACK(p0ny): unfortunately, BatchUpdateIssuesStatus doesn't comply to aip. |
| 450 | if r, ok := request.(*v1pb.BatchUpdateIssuesStatusRequest); ok { |
| 451 | resources = append(resources, r.Issues...) |
| 452 | return resources, nil |
| 453 | } |
| 454 | |
| 455 | if strings.HasPrefix(shortMethod, "Batch") { |
| 456 | // Handle batch get requests. |
| 457 | if strings.HasPrefix(shortMethod, "BatchGet") { |
| 458 | namesDesc := mr.Descriptor().Fields().ByName("names") |
| 459 | if namesDesc != nil { |
| 460 | namesValue := mr.Get(namesDesc) |
| 461 | namesValueList := namesValue.List() |
| 462 | for i := 0; i < namesValueList.Len(); i++ { |
| 463 | v := namesValueList.Get(i) |
| 464 | resources = append(resources, v.String()) |
| 465 | } |
| 466 | return resources, nil |
| 467 | } |
| 468 | } |