1// Copyright 2021 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 17package serviceusage 18 19import ( 20 "context" 21 "fmt" 22 "math" 23 "net/url" 24 "time" 25 26 "cloud.google.com/go/longrunning" 27 lroauto "cloud.google.com/go/longrunning/autogen" 28 gax "github.com/googleapis/gax-go/v2" 29 "google.golang.org/api/iterator" 30 "google.golang.org/api/option" 31 "google.golang.org/api/option/internaloption" 32 gtransport "google.golang.org/api/transport/grpc" 33 serviceusagepb "google.golang.org/genproto/googleapis/api/serviceusage/v1" 34 longrunningpb "google.golang.org/genproto/googleapis/longrunning" 35 "google.golang.org/grpc" 36 "google.golang.org/grpc/metadata" 37 "google.golang.org/protobuf/proto" 38) 39 40var newClientHook clientHook 41 42// CallOptions contains the retry settings for each method of Client. 43type CallOptions struct { 44 EnableService []gax.CallOption 45 DisableService []gax.CallOption 46 GetService []gax.CallOption 47 ListServices []gax.CallOption 48 BatchEnableServices []gax.CallOption 49 BatchGetServices []gax.CallOption 50} 51 52func defaultGRPCClientOptions() []option.ClientOption { 53 return []option.ClientOption{ 54 internaloption.WithDefaultEndpoint("serviceusage.googleapis.com:443"), 55 internaloption.WithDefaultMTLSEndpoint("serviceusage.mtls.googleapis.com:443"), 56 internaloption.WithDefaultAudience("https://serviceusage.googleapis.com/"), 57 internaloption.WithDefaultScopes(DefaultAuthScopes()...), 58 internaloption.EnableJwtWithScope(), 59 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), 60 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 61 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 62 } 63} 64 65func defaultCallOptions() *CallOptions { 66 return &CallOptions{ 67 EnableService: []gax.CallOption{}, 68 DisableService: []gax.CallOption{}, 69 GetService: []gax.CallOption{}, 70 ListServices: []gax.CallOption{}, 71 BatchEnableServices: []gax.CallOption{}, 72 BatchGetServices: []gax.CallOption{}, 73 } 74} 75 76// internalClient is an interface that defines the methods availaible from Service Usage API. 77type internalClient interface { 78 Close() error 79 setGoogleClientInfo(...string) 80 Connection() *grpc.ClientConn 81 EnableService(context.Context, *serviceusagepb.EnableServiceRequest, ...gax.CallOption) (*EnableServiceOperation, error) 82 EnableServiceOperation(name string) *EnableServiceOperation 83 DisableService(context.Context, *serviceusagepb.DisableServiceRequest, ...gax.CallOption) (*DisableServiceOperation, error) 84 DisableServiceOperation(name string) *DisableServiceOperation 85 GetService(context.Context, *serviceusagepb.GetServiceRequest, ...gax.CallOption) (*serviceusagepb.Service, error) 86 ListServices(context.Context, *serviceusagepb.ListServicesRequest, ...gax.CallOption) *ServiceIterator 87 BatchEnableServices(context.Context, *serviceusagepb.BatchEnableServicesRequest, ...gax.CallOption) (*BatchEnableServicesOperation, error) 88 BatchEnableServicesOperation(name string) *BatchEnableServicesOperation 89 BatchGetServices(context.Context, *serviceusagepb.BatchGetServicesRequest, ...gax.CallOption) (*serviceusagepb.BatchGetServicesResponse, error) 90} 91 92// Client is a client for interacting with Service Usage API. 93// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 94// 95// Enables services that service consumers want to use on Google Cloud Platform, 96// lists the available or enabled services, or disables services that service 97// consumers no longer use. 98// 99// See Service Usage API (at https://cloud.google.com/service-usage/docs/overview) 100type Client struct { 101 // The internal transport-dependent client. 102 internalClient internalClient 103 104 // The call options for this service. 105 CallOptions *CallOptions 106 107 // LROClient is used internally to handle long-running operations. 108 // It is exposed so that its CallOptions can be modified if required. 109 // Users should not Close this client. 110 LROClient *lroauto.OperationsClient 111} 112 113// Wrapper methods routed to the internal client. 114 115// Close closes the connection to the API service. The user should invoke this when 116// the client is no longer required. 117func (c *Client) Close() error { 118 return c.internalClient.Close() 119} 120 121// setGoogleClientInfo sets the name and version of the application in 122// the `x-goog-api-client` header passed on each request. Intended for 123// use by Google-written clients. 124func (c *Client) setGoogleClientInfo(keyval ...string) { 125 c.internalClient.setGoogleClientInfo(keyval...) 126} 127 128// Connection returns a connection to the API service. 129// 130// Deprecated. 131func (c *Client) Connection() *grpc.ClientConn { 132 return c.internalClient.Connection() 133} 134 135// EnableService enable a service so that it can be used with a project. 136func (c *Client) EnableService(ctx context.Context, req *serviceusagepb.EnableServiceRequest, opts ...gax.CallOption) (*EnableServiceOperation, error) { 137 return c.internalClient.EnableService(ctx, req, opts...) 138} 139 140// EnableServiceOperation returns a new EnableServiceOperation from a given name. 141// The name must be that of a previously created EnableServiceOperation, possibly from a different process. 142func (c *Client) EnableServiceOperation(name string) *EnableServiceOperation { 143 return c.internalClient.EnableServiceOperation(name) 144} 145 146// DisableService disable a service so that it can no longer be used with a project. 147// This prevents unintended usage that may cause unexpected billing 148// charges or security leaks. 149// 150// It is not valid to call the disable method on a service that is not 151// currently enabled. Callers will receive a FAILED_PRECONDITION status if 152// the target service is not currently enabled. 153func (c *Client) DisableService(ctx context.Context, req *serviceusagepb.DisableServiceRequest, opts ...gax.CallOption) (*DisableServiceOperation, error) { 154 return c.internalClient.DisableService(ctx, req, opts...) 155} 156 157// DisableServiceOperation returns a new DisableServiceOperation from a given name. 158// The name must be that of a previously created DisableServiceOperation, possibly from a different process. 159func (c *Client) DisableServiceOperation(name string) *DisableServiceOperation { 160 return c.internalClient.DisableServiceOperation(name) 161} 162 163// GetService returns the service configuration and enabled state for a given service. 164func (c *Client) GetService(ctx context.Context, req *serviceusagepb.GetServiceRequest, opts ...gax.CallOption) (*serviceusagepb.Service, error) { 165 return c.internalClient.GetService(ctx, req, opts...) 166} 167 168// ListServices list all services available to the specified project, and the current 169// state of those services with respect to the project. The list includes 170// all public services, all services for which the calling user has the 171// servicemanagement.services.bind permission, and all services that have 172// already been enabled on the project. The list can be filtered to 173// only include services in a specific state, for example to only include 174// services enabled on the project. 175// 176// WARNING: If you need to query enabled services frequently or across 177// an organization, you should use 178// Cloud Asset Inventory 179// API (at https://cloud.google.com/asset-inventory/docs/apis), which provides 180// higher throughput and richer filtering capability. 181func (c *Client) ListServices(ctx context.Context, req *serviceusagepb.ListServicesRequest, opts ...gax.CallOption) *ServiceIterator { 182 return c.internalClient.ListServices(ctx, req, opts...) 183} 184 185// BatchEnableServices enable multiple services on a project. The operation is atomic: if enabling 186// any service fails, then the entire batch fails, and no state changes occur. 187// To enable a single service, use the EnableService method instead. 188func (c *Client) BatchEnableServices(ctx context.Context, req *serviceusagepb.BatchEnableServicesRequest, opts ...gax.CallOption) (*BatchEnableServicesOperation, error) { 189 return c.internalClient.BatchEnableServices(ctx, req, opts...) 190} 191 192// BatchEnableServicesOperation returns a new BatchEnableServicesOperation from a given name. 193// The name must be that of a previously created BatchEnableServicesOperation, possibly from a different process. 194func (c *Client) BatchEnableServicesOperation(name string) *BatchEnableServicesOperation { 195 return c.internalClient.BatchEnableServicesOperation(name) 196} 197 198// BatchGetServices returns the service configurations and enabled states for a given list of 199// services. 200func (c *Client) BatchGetServices(ctx context.Context, req *serviceusagepb.BatchGetServicesRequest, opts ...gax.CallOption) (*serviceusagepb.BatchGetServicesResponse, error) { 201 return c.internalClient.BatchGetServices(ctx, req, opts...) 202} 203 204// gRPCClient is a client for interacting with Service Usage API over gRPC transport. 205// 206// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 207type gRPCClient struct { 208 // Connection pool of gRPC connections to the service. 209 connPool gtransport.ConnPool 210 211 // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE 212 disableDeadlines bool 213 214 // Points back to the CallOptions field of the containing Client 215 CallOptions **CallOptions 216 217 // The gRPC API client. 218 client serviceusagepb.ServiceUsageClient 219 220 // LROClient is used internally to handle long-running operations. 221 // It is exposed so that its CallOptions can be modified if required. 222 // Users should not Close this client. 223 LROClient **lroauto.OperationsClient 224 225 // The x-goog-* metadata to be sent with each request. 226 xGoogMetadata metadata.MD 227} 228 229// NewClient creates a new service usage client based on gRPC. 230// The returned client must be Closed when it is done being used to clean up its underlying connections. 231// 232// Enables services that service consumers want to use on Google Cloud Platform, 233// lists the available or enabled services, or disables services that service 234// consumers no longer use. 235// 236// See Service Usage API (at https://cloud.google.com/service-usage/docs/overview) 237func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) { 238 clientOpts := defaultGRPCClientOptions() 239 if newClientHook != nil { 240 hookOpts, err := newClientHook(ctx, clientHookParams{}) 241 if err != nil { 242 return nil, err 243 } 244 clientOpts = append(clientOpts, hookOpts...) 245 } 246 247 disableDeadlines, err := checkDisableDeadlines() 248 if err != nil { 249 return nil, err 250 } 251 252 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 253 if err != nil { 254 return nil, err 255 } 256 client := Client{CallOptions: defaultCallOptions()} 257 258 c := &gRPCClient{ 259 connPool: connPool, 260 disableDeadlines: disableDeadlines, 261 client: serviceusagepb.NewServiceUsageClient(connPool), 262 CallOptions: &client.CallOptions, 263 } 264 c.setGoogleClientInfo() 265 266 client.internalClient = c 267 268 client.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool)) 269 if err != nil { 270 // This error "should not happen", since we are just reusing old connection pool 271 // and never actually need to dial. 272 // If this does happen, we could leak connp. However, we cannot close conn: 273 // If the user invoked the constructor with option.WithGRPCConn, 274 // we would close a connection that's still in use. 275 // TODO: investigate error conditions. 276 return nil, err 277 } 278 c.LROClient = &client.LROClient 279 return &client, nil 280} 281 282// Connection returns a connection to the API service. 283// 284// Deprecated. 285func (c *gRPCClient) Connection() *grpc.ClientConn { 286 return c.connPool.Conn() 287} 288 289// setGoogleClientInfo sets the name and version of the application in 290// the `x-goog-api-client` header passed on each request. Intended for 291// use by Google-written clients. 292func (c *gRPCClient) setGoogleClientInfo(keyval ...string) { 293 kv := append([]string{"gl-go", versionGo()}, keyval...) 294 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 295 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 296} 297 298// Close closes the connection to the API service. The user should invoke this when 299// the client is no longer required. 300func (c *gRPCClient) Close() error { 301 return c.connPool.Close() 302} 303 304func (c *gRPCClient) EnableService(ctx context.Context, req *serviceusagepb.EnableServiceRequest, opts ...gax.CallOption) (*EnableServiceOperation, error) { 305 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 306 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 307 defer cancel() 308 ctx = cctx 309 } 310 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 311 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 312 opts = append((*c.CallOptions).EnableService[0:len((*c.CallOptions).EnableService):len((*c.CallOptions).EnableService)], opts...) 313 var resp *longrunningpb.Operation 314 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 315 var err error 316 resp, err = c.client.EnableService(ctx, req, settings.GRPC...) 317 return err 318 }, opts...) 319 if err != nil { 320 return nil, err 321 } 322 return &EnableServiceOperation{ 323 lro: longrunning.InternalNewOperation(*c.LROClient, resp), 324 }, nil 325} 326 327func (c *gRPCClient) DisableService(ctx context.Context, req *serviceusagepb.DisableServiceRequest, opts ...gax.CallOption) (*DisableServiceOperation, error) { 328 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 329 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 330 defer cancel() 331 ctx = cctx 332 } 333 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 334 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 335 opts = append((*c.CallOptions).DisableService[0:len((*c.CallOptions).DisableService):len((*c.CallOptions).DisableService)], opts...) 336 var resp *longrunningpb.Operation 337 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 338 var err error 339 resp, err = c.client.DisableService(ctx, req, settings.GRPC...) 340 return err 341 }, opts...) 342 if err != nil { 343 return nil, err 344 } 345 return &DisableServiceOperation{ 346 lro: longrunning.InternalNewOperation(*c.LROClient, resp), 347 }, nil 348} 349 350func (c *gRPCClient) GetService(ctx context.Context, req *serviceusagepb.GetServiceRequest, opts ...gax.CallOption) (*serviceusagepb.Service, error) { 351 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 352 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 353 defer cancel() 354 ctx = cctx 355 } 356 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 357 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 358 opts = append((*c.CallOptions).GetService[0:len((*c.CallOptions).GetService):len((*c.CallOptions).GetService)], opts...) 359 var resp *serviceusagepb.Service 360 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 361 var err error 362 resp, err = c.client.GetService(ctx, req, settings.GRPC...) 363 return err 364 }, opts...) 365 if err != nil { 366 return nil, err 367 } 368 return resp, nil 369} 370 371func (c *gRPCClient) ListServices(ctx context.Context, req *serviceusagepb.ListServicesRequest, opts ...gax.CallOption) *ServiceIterator { 372 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 373 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 374 opts = append((*c.CallOptions).ListServices[0:len((*c.CallOptions).ListServices):len((*c.CallOptions).ListServices)], opts...) 375 it := &ServiceIterator{} 376 req = proto.Clone(req).(*serviceusagepb.ListServicesRequest) 377 it.InternalFetch = func(pageSize int, pageToken string) ([]*serviceusagepb.Service, string, error) { 378 resp := &serviceusagepb.ListServicesResponse{} 379 if pageToken != "" { 380 req.PageToken = pageToken 381 } 382 if pageSize > math.MaxInt32 { 383 req.PageSize = math.MaxInt32 384 } else if pageSize != 0 { 385 req.PageSize = int32(pageSize) 386 } 387 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 388 var err error 389 resp, err = c.client.ListServices(ctx, req, settings.GRPC...) 390 return err 391 }, opts...) 392 if err != nil { 393 return nil, "", err 394 } 395 396 it.Response = resp 397 return resp.GetServices(), resp.GetNextPageToken(), nil 398 } 399 fetch := func(pageSize int, pageToken string) (string, error) { 400 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 401 if err != nil { 402 return "", err 403 } 404 it.items = append(it.items, items...) 405 return nextPageToken, nil 406 } 407 408 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 409 it.pageInfo.MaxSize = int(req.GetPageSize()) 410 it.pageInfo.Token = req.GetPageToken() 411 412 return it 413} 414 415func (c *gRPCClient) BatchEnableServices(ctx context.Context, req *serviceusagepb.BatchEnableServicesRequest, opts ...gax.CallOption) (*BatchEnableServicesOperation, error) { 416 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 417 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 418 defer cancel() 419 ctx = cctx 420 } 421 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 422 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 423 opts = append((*c.CallOptions).BatchEnableServices[0:len((*c.CallOptions).BatchEnableServices):len((*c.CallOptions).BatchEnableServices)], opts...) 424 var resp *longrunningpb.Operation 425 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 426 var err error 427 resp, err = c.client.BatchEnableServices(ctx, req, settings.GRPC...) 428 return err 429 }, opts...) 430 if err != nil { 431 return nil, err 432 } 433 return &BatchEnableServicesOperation{ 434 lro: longrunning.InternalNewOperation(*c.LROClient, resp), 435 }, nil 436} 437 438func (c *gRPCClient) BatchGetServices(ctx context.Context, req *serviceusagepb.BatchGetServicesRequest, opts ...gax.CallOption) (*serviceusagepb.BatchGetServicesResponse, error) { 439 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 440 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 441 defer cancel() 442 ctx = cctx 443 } 444 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 445 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 446 opts = append((*c.CallOptions).BatchGetServices[0:len((*c.CallOptions).BatchGetServices):len((*c.CallOptions).BatchGetServices)], opts...) 447 var resp *serviceusagepb.BatchGetServicesResponse 448 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 449 var err error 450 resp, err = c.client.BatchGetServices(ctx, req, settings.GRPC...) 451 return err 452 }, opts...) 453 if err != nil { 454 return nil, err 455 } 456 return resp, nil 457} 458 459// BatchEnableServicesOperation manages a long-running operation from BatchEnableServices. 460type BatchEnableServicesOperation struct { 461 lro *longrunning.Operation 462} 463 464// BatchEnableServicesOperation returns a new BatchEnableServicesOperation from a given name. 465// The name must be that of a previously created BatchEnableServicesOperation, possibly from a different process. 466func (c *gRPCClient) BatchEnableServicesOperation(name string) *BatchEnableServicesOperation { 467 return &BatchEnableServicesOperation{ 468 lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), 469 } 470} 471 472// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 473// 474// See documentation of Poll for error-handling information. 475func (op *BatchEnableServicesOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*serviceusagepb.BatchEnableServicesResponse, error) { 476 var resp serviceusagepb.BatchEnableServicesResponse 477 if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { 478 return nil, err 479 } 480 return &resp, nil 481} 482 483// Poll fetches the latest state of the long-running operation. 484// 485// Poll also fetches the latest metadata, which can be retrieved by Metadata. 486// 487// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 488// the operation has completed with failure, the error is returned and op.Done will return true. 489// If Poll succeeds and the operation has completed successfully, 490// op.Done will return true, and the response of the operation is returned. 491// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 492func (op *BatchEnableServicesOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*serviceusagepb.BatchEnableServicesResponse, error) { 493 var resp serviceusagepb.BatchEnableServicesResponse 494 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 495 return nil, err 496 } 497 if !op.Done() { 498 return nil, nil 499 } 500 return &resp, nil 501} 502 503// Metadata returns metadata associated with the long-running operation. 504// Metadata itself does not contact the server, but Poll does. 505// To get the latest metadata, call this method after a successful call to Poll. 506// If the metadata is not available, the returned metadata and error are both nil. 507func (op *BatchEnableServicesOperation) Metadata() (*serviceusagepb.OperationMetadata, error) { 508 var meta serviceusagepb.OperationMetadata 509 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 510 return nil, nil 511 } else if err != nil { 512 return nil, err 513 } 514 return &meta, nil 515} 516 517// Done reports whether the long-running operation has completed. 518func (op *BatchEnableServicesOperation) Done() bool { 519 return op.lro.Done() 520} 521 522// Name returns the name of the long-running operation. 523// The name is assigned by the server and is unique within the service from which the operation is created. 524func (op *BatchEnableServicesOperation) Name() string { 525 return op.lro.Name() 526} 527 528// DisableServiceOperation manages a long-running operation from DisableService. 529type DisableServiceOperation struct { 530 lro *longrunning.Operation 531} 532 533// DisableServiceOperation returns a new DisableServiceOperation from a given name. 534// The name must be that of a previously created DisableServiceOperation, possibly from a different process. 535func (c *gRPCClient) DisableServiceOperation(name string) *DisableServiceOperation { 536 return &DisableServiceOperation{ 537 lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), 538 } 539} 540 541// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 542// 543// See documentation of Poll for error-handling information. 544func (op *DisableServiceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*serviceusagepb.DisableServiceResponse, error) { 545 var resp serviceusagepb.DisableServiceResponse 546 if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { 547 return nil, err 548 } 549 return &resp, nil 550} 551 552// Poll fetches the latest state of the long-running operation. 553// 554// Poll also fetches the latest metadata, which can be retrieved by Metadata. 555// 556// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 557// the operation has completed with failure, the error is returned and op.Done will return true. 558// If Poll succeeds and the operation has completed successfully, 559// op.Done will return true, and the response of the operation is returned. 560// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 561func (op *DisableServiceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*serviceusagepb.DisableServiceResponse, error) { 562 var resp serviceusagepb.DisableServiceResponse 563 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 564 return nil, err 565 } 566 if !op.Done() { 567 return nil, nil 568 } 569 return &resp, nil 570} 571 572// Metadata returns metadata associated with the long-running operation. 573// Metadata itself does not contact the server, but Poll does. 574// To get the latest metadata, call this method after a successful call to Poll. 575// If the metadata is not available, the returned metadata and error are both nil. 576func (op *DisableServiceOperation) Metadata() (*serviceusagepb.OperationMetadata, error) { 577 var meta serviceusagepb.OperationMetadata 578 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 579 return nil, nil 580 } else if err != nil { 581 return nil, err 582 } 583 return &meta, nil 584} 585 586// Done reports whether the long-running operation has completed. 587func (op *DisableServiceOperation) Done() bool { 588 return op.lro.Done() 589} 590 591// Name returns the name of the long-running operation. 592// The name is assigned by the server and is unique within the service from which the operation is created. 593func (op *DisableServiceOperation) Name() string { 594 return op.lro.Name() 595} 596 597// EnableServiceOperation manages a long-running operation from EnableService. 598type EnableServiceOperation struct { 599 lro *longrunning.Operation 600} 601 602// EnableServiceOperation returns a new EnableServiceOperation from a given name. 603// The name must be that of a previously created EnableServiceOperation, possibly from a different process. 604func (c *gRPCClient) EnableServiceOperation(name string) *EnableServiceOperation { 605 return &EnableServiceOperation{ 606 lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), 607 } 608} 609 610// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 611// 612// See documentation of Poll for error-handling information. 613func (op *EnableServiceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*serviceusagepb.EnableServiceResponse, error) { 614 var resp serviceusagepb.EnableServiceResponse 615 if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { 616 return nil, err 617 } 618 return &resp, nil 619} 620 621// Poll fetches the latest state of the long-running operation. 622// 623// Poll also fetches the latest metadata, which can be retrieved by Metadata. 624// 625// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 626// the operation has completed with failure, the error is returned and op.Done will return true. 627// If Poll succeeds and the operation has completed successfully, 628// op.Done will return true, and the response of the operation is returned. 629// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 630func (op *EnableServiceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*serviceusagepb.EnableServiceResponse, error) { 631 var resp serviceusagepb.EnableServiceResponse 632 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 633 return nil, err 634 } 635 if !op.Done() { 636 return nil, nil 637 } 638 return &resp, nil 639} 640 641// Metadata returns metadata associated with the long-running operation. 642// Metadata itself does not contact the server, but Poll does. 643// To get the latest metadata, call this method after a successful call to Poll. 644// If the metadata is not available, the returned metadata and error are both nil. 645func (op *EnableServiceOperation) Metadata() (*serviceusagepb.OperationMetadata, error) { 646 var meta serviceusagepb.OperationMetadata 647 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 648 return nil, nil 649 } else if err != nil { 650 return nil, err 651 } 652 return &meta, nil 653} 654 655// Done reports whether the long-running operation has completed. 656func (op *EnableServiceOperation) Done() bool { 657 return op.lro.Done() 658} 659 660// Name returns the name of the long-running operation. 661// The name is assigned by the server and is unique within the service from which the operation is created. 662func (op *EnableServiceOperation) Name() string { 663 return op.lro.Name() 664} 665 666// ServiceIterator manages a stream of *serviceusagepb.Service. 667type ServiceIterator struct { 668 items []*serviceusagepb.Service 669 pageInfo *iterator.PageInfo 670 nextFunc func() error 671 672 // Response is the raw response for the current page. 673 // It must be cast to the RPC response type. 674 // Calling Next() or InternalFetch() updates this value. 675 Response interface{} 676 677 // InternalFetch is for use by the Google Cloud Libraries only. 678 // It is not part of the stable interface of this package. 679 // 680 // InternalFetch returns results from a single call to the underlying RPC. 681 // The number of results is no greater than pageSize. 682 // If there are no more results, nextPageToken is empty and err is nil. 683 InternalFetch func(pageSize int, pageToken string) (results []*serviceusagepb.Service, nextPageToken string, err error) 684} 685 686// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 687func (it *ServiceIterator) PageInfo() *iterator.PageInfo { 688 return it.pageInfo 689} 690 691// Next returns the next result. Its second return value is iterator.Done if there are no more 692// results. Once Next returns Done, all subsequent calls will return Done. 693func (it *ServiceIterator) Next() (*serviceusagepb.Service, error) { 694 var item *serviceusagepb.Service 695 if err := it.nextFunc(); err != nil { 696 return item, err 697 } 698 item = it.items[0] 699 it.items = it.items[1:] 700 return item, nil 701} 702 703func (it *ServiceIterator) bufLen() int { 704 return len(it.items) 705} 706 707func (it *ServiceIterator) takeBuf() interface{} { 708 b := it.items 709 it.items = nil 710 return b 711} 712