|
| 1 | +// Copyright 2025 Google LLC |
| 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 | +// https://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 discovery |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/googleapis/librarian/internal/sidekick/internal/api" |
| 22 | + "github.com/googleapis/librarian/internal/sidekick/internal/config" |
| 23 | +) |
| 24 | + |
| 25 | +func lroAnnotations(model *api.API, cfg *config.Config) error { |
| 26 | + if cfg == nil || cfg.Discovery == nil { |
| 27 | + return nil |
| 28 | + } |
| 29 | + lroServices := cfg.Discovery.LroServices() |
| 30 | + for _, svc := range model.Services { |
| 31 | + if _, ok := lroServices[svc.ID]; ok { |
| 32 | + continue |
| 33 | + } |
| 34 | + var svcMixin *api.Method |
| 35 | + for _, method := range svc.Methods { |
| 36 | + if method.OutputTypeID != cfg.Discovery.OperationID { |
| 37 | + continue |
| 38 | + } |
| 39 | + mixin, pathParams := lroFindPoller(method, model, cfg.Discovery) |
| 40 | + if mixin == nil { |
| 41 | + continue |
| 42 | + } |
| 43 | + method.DiscoveryLro = &api.DiscoveryLro{ |
| 44 | + PollingPathParameters: pathParams, |
| 45 | + } |
| 46 | + if svcMixin != nil && mixin != svcMixin { |
| 47 | + return fmt.Errorf("mismatched LRO mixin, want=%v, got=%v", svcMixin, mixin) |
| 48 | + } |
| 49 | + svcMixin = mixin |
| 50 | + } |
| 51 | + if svcMixin == nil { |
| 52 | + continue |
| 53 | + } |
| 54 | + method := &api.Method{ |
| 55 | + Name: "getOperation", |
| 56 | + ID: fmt.Sprintf("%s.getOperation", svc.ID), |
| 57 | + Documentation: svcMixin.Documentation, |
| 58 | + InputTypeID: svcMixin.InputTypeID, |
| 59 | + OutputTypeID: svcMixin.OutputTypeID, |
| 60 | + ReturnsEmpty: svcMixin.ReturnsEmpty, |
| 61 | + PathInfo: svcMixin.PathInfo, |
| 62 | + Pagination: svcMixin.Pagination, |
| 63 | + Routing: svcMixin.Routing, |
| 64 | + AutoPopulated: svcMixin.AutoPopulated, |
| 65 | + Service: svc, |
| 66 | + SourceService: svcMixin.Service, |
| 67 | + SourceServiceID: svcMixin.SourceServiceID, |
| 68 | + } |
| 69 | + svc.Methods = append(svc.Methods, method) |
| 70 | + model.State.MethodByID[method.ID] = method |
| 71 | + } |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func lroFindPoller(method *api.Method, model *api.API, discoveryConfig *config.Discovery) (*api.Method, []string) { |
| 76 | + var flatPath []string |
| 77 | + for _, binding := range method.PathInfo.Bindings { |
| 78 | + flatPath = append(flatPath, binding.PathTemplate.FlatPath()) |
| 79 | + } |
| 80 | + for _, candidate := range discoveryConfig.Pollers { |
| 81 | + for _, path := range flatPath { |
| 82 | + if !strings.HasPrefix(path, candidate.Prefix) { |
| 83 | + continue |
| 84 | + } |
| 85 | + if method, ok := model.State.MethodByID[candidate.MethodID]; ok { |
| 86 | + return method, candidate.PathParameters() |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + return nil, nil |
| 91 | +} |
0 commit comments