Debian Codegraph @

policysimulator/apiv1/simulator_client.go

raw symbols SCIP
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 // Code generated by protoc-gen-go_gapic. DO NOT EDIT.
16
17 package policysimulator
18
19 import (
20 "bytes"
21 "context"
22 "fmt"
23 "log/slog"
24 "math"
25 "net/http"
26 "net/url"
27 "time"
28
29 "cloud.google.com/go/longrunning"
30 lroauto "cloud.google.com/go/longrunning/autogen"
31 longrunningpb "cloud.google.com/go/longrunning/autogen/longrunningpb"
32 policysimulatorpb "cloud.google.com/go/policysimulator/apiv1/policysimulatorpb"
33 gax "github.com/googleapis/gax-go/v2"
34 "google.golang.org/api/iterator"
35 "google.golang.org/api/option"
36 "google.golang.org/api/option/internaloption"
37 gtransport "google.golang.org/api/transport/grpc"
38 httptransport "google.golang.org/api/transport/http"
39 "google.golang.org/grpc"
40 "google.golang.org/grpc/codes"
41 "google.golang.org/protobuf/encoding/protojson"
42 "google.golang.org/protobuf/proto"
43 )
44
45 var newSimulatorClientHook clientHook
46
47 // SimulatorCallOptions contains the retry settings for each method of SimulatorClient.
48 type SimulatorCallOptions struct {
49 GetReplay []gax.CallOption
50 CreateReplay []gax.CallOption
51 ListReplayResults []gax.CallOption
52 GetOperation []gax.CallOption
53 ListOperations []gax.CallOption
54 }
55
56 func defaultSimulatorGRPCClientOptions() []option.ClientOption {
57 return []option.ClientOption{
58 internaloption.WithDefaultEndpoint("policysimulator.googleapis.com:443"),
59 internaloption.WithDefaultEndpointTemplate("policysimulator.UNIVERSE_DOMAIN:443"),
60 internaloption.WithDefaultMTLSEndpoint("policysimulator.mtls.googleapis.com:443"),
61 internaloption.WithDefaultUniverseDomain("googleapis.com"),
62 internaloption.WithDefaultAudience("https://policysimulator.googleapis.com/"),
63 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
64 internaloption.EnableJwtWithScope(),
65 internaloption.EnableNewAuthLibrary(),
66 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
67 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
68 }
69 }
70
71 func defaultSimulatorCallOptions() *SimulatorCallOptions {
72 return &SimulatorCallOptions{
73 GetReplay: []gax.CallOption{
74 gax.WithTimeout(60000 * time.Millisecond),
75 gax.WithRetry(func() gax.Retryer {
76 return gax.OnCodes([]codes.Code{
77 codes.Unavailable,
78 }, gax.Backoff{
79 Initial: 1000 * time.Millisecond,
80 Max: 10000 * time.Millisecond,
81 Multiplier: 1.30,
82 })
83 }),
84 },
85 CreateReplay: []gax.CallOption{
86 gax.WithTimeout(60000 * time.Millisecond),
87 },
88 ListReplayResults: []gax.CallOption{
89 gax.WithTimeout(60000 * time.Millisecond),
90 gax.WithRetry(func() gax.Retryer {
91 return gax.OnCodes([]codes.Code{
92 codes.Unavailable,
93 }, gax.Backoff{
94 Initial: 1000 * time.Millisecond,
95 Max: 10000 * time.Millisecond,
96 Multiplier: 1.30,
97 })
98 }),
99 },
100 GetOperation: []gax.CallOption{},
101 ListOperations: []gax.CallOption{},
102 }
103 }
104
105 func defaultSimulatorRESTCallOptions() *SimulatorCallOptions {
106 return &SimulatorCallOptions{
107 GetReplay: []gax.CallOption{
108 gax.WithTimeout(60000 * time.Millisecond),
109 gax.WithRetry(func() gax.Retryer {
110 return gax.OnHTTPCodes(gax.Backoff{
111 Initial: 1000 * time.Millisecond,
112 Max: 10000 * time.Millisecond,
113 Multiplier: 1.30,
114 },
115 http.StatusServiceUnavailable)
116 }),
117 },
118 CreateReplay: []gax.CallOption{
119 gax.WithTimeout(60000 * time.Millisecond),
120 },
121 ListReplayResults: []gax.CallOption{
122 gax.WithTimeout(60000 * time.Millisecond),
123 gax.WithRetry(func() gax.Retryer {
124 return gax.OnHTTPCodes(gax.Backoff{
125 Initial: 1000 * time.Millisecond,
126 Max: 10000 * time.Millisecond,
127 Multiplier: 1.30,
128 },
129 http.StatusServiceUnavailable)
130 }),
131 },
132 GetOperation: []gax.CallOption{},
133 ListOperations: []gax.CallOption{},
134 }
135 }
136
137 // internalSimulatorClient is an interface that defines the methods available from Policy Simulator API.
138 type internalSimulatorClient interface {
139 Close() error
140 setGoogleClientInfo(...string)
141 Connection() *grpc.ClientConn
142 GetReplay(context.Context, *policysimulatorpb.GetReplayRequest, ...gax.CallOption) (*policysimulatorpb.Replay, error)
143 CreateReplay(context.Context, *policysimulatorpb.CreateReplayRequest, ...gax.CallOption) (*CreateReplayOperation, error)
144 CreateReplayOperation(name string) *CreateReplayOperation
145 ListReplayResults(context.Context, *policysimulatorpb.ListReplayResultsRequest, ...gax.CallOption) *ReplayResultIterator
146 GetOperation(context.Context, *longrunningpb.GetOperationRequest, ...gax.CallOption) (*longrunningpb.Operation, error)
147 ListOperations(context.Context, *longrunningpb.ListOperationsRequest, ...gax.CallOption) *OperationIterator
148 }
149
150 // SimulatorClient is a client for interacting with Policy Simulator API.
151 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
152 //
153 // Policy Simulator API service.
154 //
155 // Policy Simulator is a collection of endpoints for creating, running, and
156 // viewing a Replay. A
157 // Replay is a type of simulation that
158 // lets you see how your principals’ access to resources might change if you
159 // changed your IAM policy.
160 //
161 // During a Replay, Policy Simulator
162 // re-evaluates, or replays, past access attempts under both the current policy
163 // and your proposed policy, and compares those results to determine how your
164 // principals’ access might change under the proposed policy.
165 type SimulatorClient struct {
166 // The internal transport-dependent client.
167 internalClient internalSimulatorClient
168
169 // The call options for this service.
170 CallOptions *SimulatorCallOptions
171
172 // LROClient is used internally to handle long-running operations.
173 // It is exposed so that its CallOptions can be modified if required.
174 // Users should not Close this client.
175 LROClient *lroauto.OperationsClient
176 }
177
178 // Wrapper methods routed to the internal client.
179
180 // Close closes the connection to the API service. The user should invoke this when
181 // the client is no longer required.
182 func (c *SimulatorClient) Close() error {
183 return c.internalClient.Close()
184 }
185
186 // setGoogleClientInfo sets the name and version of the application in
187 // the `x-goog-api-client` header passed on each request. Intended for
188 // use by Google-written clients.
189 func (c *SimulatorClient) setGoogleClientInfo(keyval ...string) {
190 c.internalClient.setGoogleClientInfo(keyval...)
191 }
192
193 // Connection returns a connection to the API service.
194 //
195 // Deprecated: Connections are now pooled so this method does not always
196 // return the same resource.
197 func (c *SimulatorClient) Connection() *grpc.ClientConn {
198 return c.internalClient.Connection()
199 }
200
201 // GetReplay gets the specified Replay. Each
202 // Replay is available for at least 7 days.
203 func (c *SimulatorClient) GetReplay(ctx context.Context, req *policysimulatorpb.GetReplayRequest, opts ...gax.CallOption) (*policysimulatorpb.Replay, error) {
204 return c.internalClient.GetReplay(ctx, req, opts...)
205 }
206
207 // CreateReplay creates and starts a Replay using
208 // the given ReplayConfig.
209 func (c *SimulatorClient) CreateReplay(ctx context.Context, req *policysimulatorpb.CreateReplayRequest, opts ...gax.CallOption) (*CreateReplayOperation, error) {
210 return c.internalClient.CreateReplay(ctx, req, opts...)
211 }
212
213 // CreateReplayOperation returns a new CreateReplayOperation from a given name.
214 // The name must be that of a previously created CreateReplayOperation, possibly from a different process.
215 func (c *SimulatorClient) CreateReplayOperation(name string) *CreateReplayOperation {
216 return c.internalClient.CreateReplayOperation(name)
217 }
218
219 // ListReplayResults lists the results of running a
220 // Replay.
221 func (c *SimulatorClient) ListReplayResults(ctx context.Context, req *policysimulatorpb.ListReplayResultsRequest, opts ...gax.CallOption) *ReplayResultIterator {
222 return c.internalClient.ListReplayResults(ctx, req, opts...)
223 }
224
225 // GetOperation is a utility method from google.longrunning.Operations.
226 func (c *SimulatorClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
227 return c.internalClient.GetOperation(ctx, req, opts...)
228 }
229
230 // ListOperations is a utility method from google.longrunning.Operations.
231 func (c *SimulatorClient) ListOperations(ctx context.Context, req *longrunningpb.ListOperationsRequest, opts ...gax.CallOption) *OperationIterator {
232 return c.internalClient.ListOperations(ctx, req, opts...)
233 }
234
235 // simulatorGRPCClient is a client for interacting with Policy Simulator API over gRPC transport.
236 //
237 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
238 type simulatorGRPCClient struct {
239 // Connection pool of gRPC connections to the service.
240 connPool gtransport.ConnPool
241
242 // Points back to the CallOptions field of the containing SimulatorClient
243 CallOptions **SimulatorCallOptions
244
245 // The gRPC API client.
246 simulatorClient policysimulatorpb.SimulatorClient
247
248 // LROClient is used internally to handle long-running operations.
249 // It is exposed so that its CallOptions can be modified if required.
250 // Users should not Close this client.
251 LROClient **lroauto.OperationsClient
252
253 operationsClient longrunningpb.OperationsClient
254
255 // The x-goog-* metadata to be sent with each request.
256 xGoogHeaders []string
257
258 logger *slog.Logger
259 }
260
261 // NewSimulatorClient creates a new simulator client based on gRPC.
262 // The returned client must be Closed when it is done being used to clean up its underlying connections.
263 //
264 // Policy Simulator API service.
265 //
266 // Policy Simulator is a collection of endpoints for creating, running, and
267 // viewing a Replay. A
268 // Replay is a type of simulation that
269 // lets you see how your principals’ access to resources might change if you
270 // changed your IAM policy.
271 //
272 // During a Replay, Policy Simulator
273 // re-evaluates, or replays, past access attempts under both the current policy
274 // and your proposed policy, and compares those results to determine how your
275 // principals’ access might change under the proposed policy.
276 func NewSimulatorClient(ctx context.Context, opts ...option.ClientOption) (*SimulatorClient, error) {
277 clientOpts := defaultSimulatorGRPCClientOptions()
278 if newSimulatorClientHook != nil {
279 hookOpts, err := newSimulatorClientHook(ctx, clientHookParams{})
280 if err != nil {
281 return nil, err
282 }
283 clientOpts = append(clientOpts, hookOpts...)
284 }
285
286 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
287 if err != nil {
288 return nil, err
289 }
290 client := SimulatorClient{CallOptions: defaultSimulatorCallOptions()}
291
292 c := &simulatorGRPCClient{
293 connPool: connPool,
294 simulatorClient: policysimulatorpb.NewSimulatorClient(connPool),
295 CallOptions: &client.CallOptions,
296 logger: internaloption.GetLogger(opts),
297 operationsClient: longrunningpb.NewOperationsClient(connPool),
298 }
299 c.setGoogleClientInfo()
300
301 client.internalClient = c
302
303 client.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool))
304 if err != nil {
305 // This error "should not happen", since we are just reusing old connection pool
306 // and never actually need to dial.
307 // If this does happen, we could leak connp. However, we cannot close conn:
308 // If the user invoked the constructor with option.WithGRPCConn,
309 // we would close a connection that's still in use.
310 // TODO: investigate error conditions.
311 return nil, err
312 }
313 c.LROClient = &client.LROClient
314 return &client, nil
315 }
316
317 // Connection returns a connection to the API service.
318 //
319 // Deprecated: Connections are now pooled so this method does not always
320 // return the same resource.
321 func (c *simulatorGRPCClient) Connection() *grpc.ClientConn {
322 return c.connPool.Conn()
323 }
324
325 // setGoogleClientInfo sets the name and version of the application in
326 // the `x-goog-api-client` header passed on each request. Intended for
327 // use by Google-written clients.
328 func (c *simulatorGRPCClient) setGoogleClientInfo(keyval ...string) {
329 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
330 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version, "pb", protoVersion)
331 c.xGoogHeaders = []string{
332 "x-goog-api-client", gax.XGoogHeader(kv...),
333 }
334 }
335
336 // Close closes the connection to the API service. The user should invoke this when
337 // the client is no longer required.
338 func (c *simulatorGRPCClient) Close() error {
339 return c.connPool.Close()
340 }
341
342 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
343 type simulatorRESTClient struct {
344 // The http endpoint to connect to.
345 endpoint string
346
347 // The http client.
348 httpClient *http.Client
349
350 // LROClient is used internally to handle long-running operations.
351 // It is exposed so that its CallOptions can be modified if required.
352 // Users should not Close this client.
353 LROClient **lroauto.OperationsClient
354
355 // The x-goog-* headers to be sent with each request.
356 xGoogHeaders []string
357
358 // Points back to the CallOptions field of the containing SimulatorClient
359 CallOptions **SimulatorCallOptions
360
361 logger *slog.Logger
362 }
363
364 // NewSimulatorRESTClient creates a new simulator rest client.
365 //
366 // Policy Simulator API service.
367 //
368 // Policy Simulator is a collection of endpoints for creating, running, and
369 // viewing a Replay. A
370 // Replay is a type of simulation that
371 // lets you see how your principals’ access to resources might change if you
372 // changed your IAM policy.
373 //
374 // During a Replay, Policy Simulator
375 // re-evaluates, or replays, past access attempts under both the current policy
376 // and your proposed policy, and compares those results to determine how your
377 // principals’ access might change under the proposed policy.
378 func NewSimulatorRESTClient(ctx context.Context, opts ...option.ClientOption) (*SimulatorClient, error) {
379 clientOpts := append(defaultSimulatorRESTClientOptions(), opts...)
380 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
381 if err != nil {
382 return nil, err
383 }
384
385 callOpts := defaultSimulatorRESTCallOptions()
386 c := &simulatorRESTClient{
387 endpoint: endpoint,
388 httpClient: httpClient,
389 CallOptions: &callOpts,
390 logger: internaloption.GetLogger(opts),
391 }
392 c.setGoogleClientInfo()
393
394 lroOpts := []option.ClientOption{
395 option.WithHTTPClient(httpClient),
396 option.WithEndpoint(endpoint),
397 }
398 opClient, err := lroauto.NewOperationsRESTClient(ctx, lroOpts...)
399 if err != nil {
400 return nil, err
401 }
402 c.LROClient = &opClient
403
404 return &SimulatorClient{internalClient: c, CallOptions: callOpts}, nil
405 }
406
407 func defaultSimulatorRESTClientOptions() []option.ClientOption {
408 return []option.ClientOption{
409 internaloption.WithDefaultEndpoint("https://policysimulator.googleapis.com"),
410 internaloption.WithDefaultEndpointTemplate("https://policysimulator.UNIVERSE_DOMAIN"),
411 internaloption.WithDefaultMTLSEndpoint("https://policysimulator.mtls.googleapis.com"),
412 internaloption.WithDefaultUniverseDomain("googleapis.com"),
413 internaloption.WithDefaultAudience("https://policysimulator.googleapis.com/"),
414 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
415 internaloption.EnableNewAuthLibrary(),
416 }
417 }
418
419 // setGoogleClientInfo sets the name and version of the application in
420 // the `x-goog-api-client` header passed on each request. Intended for
421 // use by Google-written clients.
422 func (c *simulatorRESTClient) setGoogleClientInfo(keyval ...string) {
423 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
424 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
425 c.xGoogHeaders = []string{
426 "x-goog-api-client", gax.XGoogHeader(kv...),
427 }
428 }
429
430 // Close closes the connection to the API service. The user should invoke this when
431 // the client is no longer required.
432 func (c *simulatorRESTClient) Close() error {
433 // Replace httpClient with nil to force cleanup.
434 c.httpClient = nil
435 return nil
436 }
437
438 // Connection returns a connection to the API service.
439 //
440 // Deprecated: This method always returns nil.
441 func (c *simulatorRESTClient) Connection() *grpc.ClientConn {
442 return nil
443 }
444 func (c *simulatorGRPCClient) GetReplay(ctx context.Context, req *policysimulatorpb.GetReplayRequest, opts ...gax.CallOption) (*policysimulatorpb.Replay, error) {
445 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
446
447 hds = append(c.xGoogHeaders, hds...)
448 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
449 opts = append((*c.CallOptions).GetReplay[0:len((*c.CallOptions).GetReplay):len((*c.CallOptions).GetReplay)], opts...)
450 var resp *policysimulatorpb.Replay
451 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
452 var err error
453 resp, err = executeRPC(ctx, c.simulatorClient.GetReplay, req, settings.GRPC, c.logger, "GetReplay")
454 return err
455 }, opts...)
456 if err != nil {
457 return nil, err
458 }
459 return resp, nil
460 }
461
462 func (c *simulatorGRPCClient) CreateReplay(ctx context.Context, req *policysimulatorpb.CreateReplayRequest, opts ...gax.CallOption) (*CreateReplayOperation, error) {
463 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
464
465 hds = append(c.xGoogHeaders, hds...)
466 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
467 opts = append((*c.CallOptions).CreateReplay[0:len((*c.CallOptions).CreateReplay):len((*c.CallOptions).CreateReplay)], opts...)
468 var resp *longrunningpb.Operation
469 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
470 var err error
471 resp, err = executeRPC(ctx, c.simulatorClient.CreateReplay, req, settings.GRPC, c.logger, "CreateReplay")
472 return err
473 }, opts...)
474 if err != nil {
475 return nil, err
476 }
477 return &CreateReplayOperation{
478 lro: longrunning.InternalNewOperation(*c.LROClient, resp),
479 }, nil
480 }
481
482 func (c *simulatorGRPCClient) ListReplayResults(ctx context.Context, req *policysimulatorpb.ListReplayResultsRequest, opts ...gax.CallOption) *ReplayResultIterator {
483 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
484
485 hds = append(c.xGoogHeaders, hds...)
486 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
487 opts = append((*c.CallOptions).ListReplayResults[0:len((*c.CallOptions).ListReplayResults):len((*c.CallOptions).ListReplayResults)], opts...)
488 it := &ReplayResultIterator{}
489 req = proto.Clone(req).(*policysimulatorpb.ListReplayResultsRequest)
490 it.InternalFetch = func(pageSize int, pageToken string) ([]*policysimulatorpb.ReplayResult, string, error) {
491 resp := &policysimulatorpb.ListReplayResultsResponse{}
492 if pageToken != "" {
493 req.PageToken = pageToken
494 }
495 if pageSize > math.MaxInt32 {
496 req.PageSize = math.MaxInt32
497 } else if pageSize != 0 {
498 req.PageSize = int32(pageSize)
499 }
500 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
501 var err error
502 resp, err = executeRPC(ctx, c.simulatorClient.ListReplayResults, req, settings.GRPC, c.logger, "ListReplayResults")
503 return err
504 }, opts...)
505 if err != nil {
506 return nil, "", err
507 }
508
509 it.Response = resp
510 return resp.GetReplayResults(), resp.GetNextPageToken(), nil
511 }
512 fetch := func(pageSize int, pageToken string) (string, error) {
513 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
514 if err != nil {
515 return "", err
516 }
517 it.items = append(it.items, items...)
518 return nextPageToken, nil
519 }
520
521 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
522 it.pageInfo.MaxSize = int(req.GetPageSize())
523 it.pageInfo.Token = req.GetPageToken()
524
525 return it
526 }
527
528 func (c *simulatorGRPCClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
529 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
530
531 hds = append(c.xGoogHeaders, hds...)
532 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
533 opts = append((*c.CallOptions).GetOperation[0:len((*c.CallOptions).GetOperation):len((*c.CallOptions).GetOperation)], opts...)
534 var resp *longrunningpb.Operation
535 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
536 var err error
537 resp, err = executeRPC(ctx, c.operationsClient.GetOperation, req, settings.GRPC, c.logger, "GetOperation")
538 return err
539 }, opts...)
540 if err != nil {
541 return nil, err
542 }
543 return resp, nil
544 }
545
546 func (c *simulatorGRPCClient) ListOperations(ctx context.Context, req *longrunningpb.ListOperationsRequest, opts ...gax.CallOption) *OperationIterator {
547 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
548
549 hds = append(c.xGoogHeaders, hds...)
550 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
551 opts = append((*c.CallOptions).ListOperations[0:len((*c.CallOptions).ListOperations):len((*c.CallOptions).ListOperations)], opts...)
552 it := &OperationIterator{}
553 req = proto.Clone(req).(*longrunningpb.ListOperationsRequest)
554 it.InternalFetch = func(pageSize int, pageToken string) ([]*longrunningpb.Operation, string, error) {
555 resp := &longrunningpb.ListOperationsResponse{}
556 if pageToken != "" {
557 req.PageToken = pageToken
558 }
559 if pageSize > math.MaxInt32 {
560 req.PageSize = math.MaxInt32
561 } else if pageSize != 0 {
562 req.PageSize = int32(pageSize)
563 }
564 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
565 var err error
566 resp, err = executeRPC(ctx, c.operationsClient.ListOperations, req, settings.GRPC, c.logger, "ListOperations")
567 return err
568 }, opts...)
569 if err != nil {
570 return nil, "", err
571 }
572
573 it.Response = resp
574 return resp.GetOperations(), resp.GetNextPageToken(), nil
575 }
576 fetch := func(pageSize int, pageToken string) (string, error) {
577 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
578 if err != nil {
579 return "", err
580 }
581 it.items = append(it.items, items...)
582 return nextPageToken, nil
583 }
584
585 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
586 it.pageInfo.MaxSize = int(req.GetPageSize())
587 it.pageInfo.Token = req.GetPageToken()
588
589 return it
590 }
591
592 // GetReplay gets the specified Replay. Each
593 // Replay is available for at least 7 days.
594 func (c *simulatorRESTClient) GetReplay(ctx context.Context, req *policysimulatorpb.GetReplayRequest, opts ...gax.CallOption) (*policysimulatorpb.Replay, error) {
595 baseUrl, err := url.Parse(c.endpoint)
596 if err != nil {
597 return nil, err
598 }
599 baseUrl.Path += fmt.Sprintf("/v1/%v", req.GetName())
600
601 params := url.Values{}
602 params.Add("$alt", "json;enum-encoding=int")
603
604 baseUrl.RawQuery = params.Encode()
605
606 // Build HTTP headers from client and context metadata.
607 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
608
609 hds = append(c.xGoogHeaders, hds...)
610 hds = append(hds, "Content-Type", "application/json")
611 headers := gax.BuildHeaders(ctx, hds...)
612 opts = append((*c.CallOptions).GetReplay[0:len((*c.CallOptions).GetReplay):len((*c.CallOptions).GetReplay)], opts...)
613 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
614 resp := &policysimulatorpb.Replay{}
615 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
616 if settings.Path != "" {
617 baseUrl.Path = settings.Path
618 }
619 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
620 if err != nil {
621 return err
622 }
623 httpReq = httpReq.WithContext(ctx)
624 httpReq.Header = headers
625
626 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetReplay")
627 if err != nil {
628 return err
629 }
630
631 if err := unm.Unmarshal(buf, resp); err != nil {
632 return err
633 }
634
635 return nil
636 }, opts...)
637 if e != nil {
638 return nil, e
639 }
640 return resp, nil
641 }
642
643 // CreateReplay creates and starts a Replay using
644 // the given ReplayConfig.
645 func (c *simulatorRESTClient) CreateReplay(ctx context.Context, req *policysimulatorpb.CreateReplayRequest, opts ...gax.CallOption) (*CreateReplayOperation, error) {
646 m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
647 body := req.GetReplay()
648 jsonReq, err := m.Marshal(body)
649 if err != nil {
650 return nil, err
651 }
652
653 baseUrl, err := url.Parse(c.endpoint)
654 if err != nil {
655 return nil, err
656 }
657 baseUrl.Path += fmt.Sprintf("/v1/%v/replays", req.GetParent())
658
659 params := url.Values{}
660 params.Add("$alt", "json;enum-encoding=int")
661
662 baseUrl.RawQuery = params.Encode()
663
664 // Build HTTP headers from client and context metadata.
665 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
666
667 hds = append(c.xGoogHeaders, hds...)
668 hds = append(hds, "Content-Type", "application/json")
669 headers := gax.BuildHeaders(ctx, hds...)
670 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
671 resp := &longrunningpb.Operation{}
672 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
673 if settings.Path != "" {
674 baseUrl.Path = settings.Path
675 }
676 httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
677 if err != nil {
678 return err
679 }
680 httpReq = httpReq.WithContext(ctx)
681 httpReq.Header = headers
682
683 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, jsonReq, "CreateReplay")
684 if err != nil {
685 return err
686 }
687 if err := unm.Unmarshal(buf, resp); err != nil {
688 return err
689 }
690
691 return nil
692 }, opts...)
693 if e != nil {
694 return nil, e
695 }
696
697 override := fmt.Sprintf("/v1/%s", resp.GetName())
698 return &CreateReplayOperation{
699 lro: longrunning.InternalNewOperation(*c.LROClient, resp),
700 pollPath: override,
701 }, nil
702 }
703
704 // ListReplayResults lists the results of running a
705 // Replay.
706 func (c *simulatorRESTClient) ListReplayResults(ctx context.Context, req *policysimulatorpb.ListReplayResultsRequest, opts ...gax.CallOption) *ReplayResultIterator {
707 it := &ReplayResultIterator{}
708 req = proto.Clone(req).(*policysimulatorpb.ListReplayResultsRequest)
709 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
710 it.InternalFetch = func(pageSize int, pageToken string) ([]*policysimulatorpb.ReplayResult, string, error) {
711 resp := &policysimulatorpb.ListReplayResultsResponse{}
712 if pageToken != "" {
713 req.PageToken = pageToken
714 }
715 if pageSize > math.MaxInt32 {
716 req.PageSize = math.MaxInt32
717 } else if pageSize != 0 {
718 req.PageSize = int32(pageSize)
719 }
720 baseUrl, err := url.Parse(c.endpoint)
721 if err != nil {
722 return nil, "", err
723 }
724 baseUrl.Path += fmt.Sprintf("/v1/%v/results", req.GetParent())
725
726 params := url.Values{}
727 params.Add("$alt", "json;enum-encoding=int")
728 if req.GetPageSize() != 0 {
729 params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize()))
730 }
731 if req.GetPageToken() != "" {
732 params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken()))
733 }
734
735 baseUrl.RawQuery = params.Encode()
736
737 // Build HTTP headers from client and context metadata.
738 hds := append(c.xGoogHeaders, "Content-Type", "application/json")
739 headers := gax.BuildHeaders(ctx, hds...)
740 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
741 if settings.Path != "" {
742 baseUrl.Path = settings.Path
743 }
744 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
745 if err != nil {
746 return err
747 }
748 httpReq.Header = headers
749
750 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "ListReplayResults")
751 if err != nil {
752 return err
753 }
754 if err := unm.Unmarshal(buf, resp); err != nil {
755 return err
756 }
757
758 return nil
759 }, opts...)
760 if e != nil {
761 return nil, "", e
762 }
763 it.Response = resp
764 return resp.GetReplayResults(), resp.GetNextPageToken(), nil
765 }
766
767 fetch := func(pageSize int, pageToken string) (string, error) {
768 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
769 if err != nil {
770 return "", err
771 }
772 it.items = append(it.items, items...)
773 return nextPageToken, nil
774 }
775
776 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
777 it.pageInfo.MaxSize = int(req.GetPageSize())
778 it.pageInfo.Token = req.GetPageToken()
779
780 return it
781 }
782
783 // GetOperation is a utility method from google.longrunning.Operations.
784 func (c *simulatorRESTClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
785 baseUrl, err := url.Parse(c.endpoint)
786 if err != nil {
787 return nil, err
788 }
789 baseUrl.Path += fmt.Sprintf("/v1/%v", req.GetName())
790
791 params := url.Values{}
792 params.Add("$alt", "json;enum-encoding=int")
793
794 baseUrl.RawQuery = params.Encode()
795
796 // Build HTTP headers from client and context metadata.
797 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
798
799 hds = append(c.xGoogHeaders, hds...)
800 hds = append(hds, "Content-Type", "application/json")
801 headers := gax.BuildHeaders(ctx, hds...)
802 opts = append((*c.CallOptions).GetOperation[0:len((*c.CallOptions).GetOperation):len((*c.CallOptions).GetOperation)], opts...)
803 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
804 resp := &longrunningpb.Operation{}
805 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
806 if settings.Path != "" {
807 baseUrl.Path = settings.Path
808 }
809 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
810 if err != nil {
811 return err
812 }
813 httpReq = httpReq.WithContext(ctx)
814 httpReq.Header = headers
815
816 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GetOperation")
817 if err != nil {
818 return err
819 }
820
821 if err := unm.Unmarshal(buf, resp); err != nil {
822 return err
823 }
824
825 return nil
826 }, opts...)
827 if e != nil {
828 return nil, e
829 }
830 return resp, nil
831 }
832
833 // ListOperations is a utility method from google.longrunning.Operations.
834 func (c *simulatorRESTClient) ListOperations(ctx context.Context, req *longrunningpb.ListOperationsRequest, opts ...gax.CallOption) *OperationIterator {
835 it := &OperationIterator{}
836 req = proto.Clone(req).(*longrunningpb.ListOperationsRequest)
837 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
838 it.InternalFetch = func(pageSize int, pageToken string) ([]*longrunningpb.Operation, string, error) {
839 resp := &longrunningpb.ListOperationsResponse{}
840 if pageToken != "" {
841 req.PageToken = pageToken
842 }
843 if pageSize > math.MaxInt32 {
844 req.PageSize = math.MaxInt32
845 } else if pageSize != 0 {
846 req.PageSize = int32(pageSize)
847 }
848 baseUrl, err := url.Parse(c.endpoint)
849 if err != nil {
850 return nil, "", err
851 }
852 baseUrl.Path += fmt.Sprintf("/v1/%v", req.GetName())
853
854 params := url.Values{}
855 params.Add("$alt", "json;enum-encoding=int")
856 if req.GetFilter() != "" {
857 params.Add("filter", fmt.Sprintf("%v", req.GetFilter()))
858 }
859 if req.GetPageSize() != 0 {
860 params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize()))
861 }
862 if req.GetPageToken() != "" {
863 params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken()))
864 }
865
866 baseUrl.RawQuery = params.Encode()
867
868 // Build HTTP headers from client and context metadata.
869 hds := append(c.xGoogHeaders, "Content-Type", "application/json")
870 headers := gax.BuildHeaders(ctx, hds...)
871 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
872 if settings.Path != "" {
873 baseUrl.Path = settings.Path
874 }
875 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
876 if err != nil {
877 return err
878 }
879 httpReq.Header = headers
880
881 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "ListOperations")
882 if err != nil {
883 return err
884 }
885 if err := unm.Unmarshal(buf, resp); err != nil {
886 return err
887 }
888
889 return nil
890 }, opts...)
891 if e != nil {
892 return nil, "", e
893 }
894 it.Response = resp
895 return resp.GetOperations(), resp.GetNextPageToken(), nil
896 }
897
898 fetch := func(pageSize int, pageToken string) (string, error) {
899 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
900 if err != nil {
901 return "", err
902 }
903 it.items = append(it.items, items...)
904 return nextPageToken, nil
905 }
906
907 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
908 it.pageInfo.MaxSize = int(req.GetPageSize())
909 it.pageInfo.Token = req.GetPageToken()
910
911 return it
912 }
913
914 // CreateReplayOperation returns a new CreateReplayOperation from a given name.
915 // The name must be that of a previously created CreateReplayOperation, possibly from a different process.
916 func (c *simulatorGRPCClient) CreateReplayOperation(name string) *CreateReplayOperation {
917 return &CreateReplayOperation{
918 lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
919 }
920 }
921
922 // CreateReplayOperation returns a new CreateReplayOperation from a given name.
923 // The name must be that of a previously created CreateReplayOperation, possibly from a different process.
924 func (c *simulatorRESTClient) CreateReplayOperation(name string) *CreateReplayOperation {
925 override := fmt.Sprintf("/v1/%s", name)
926 return &CreateReplayOperation{
927 lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
928 pollPath: override,
929 }
930 }