1// Copyright 2019 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 gapic-generator. DO NOT EDIT. 16 17package redis 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 "github.com/golang/protobuf/proto" 29 gax "github.com/googleapis/gax-go/v2" 30 "google.golang.org/api/iterator" 31 "google.golang.org/api/option" 32 "google.golang.org/api/transport" 33 redispb "google.golang.org/genproto/googleapis/cloud/redis/v1" 34 longrunningpb "google.golang.org/genproto/googleapis/longrunning" 35 "google.golang.org/grpc" 36 "google.golang.org/grpc/metadata" 37) 38 39// CloudRedisCallOptions contains the retry settings for each method of CloudRedisClient. 40type CloudRedisCallOptions struct { 41 ListInstances []gax.CallOption 42 GetInstance []gax.CallOption 43 CreateInstance []gax.CallOption 44 UpdateInstance []gax.CallOption 45 ImportInstance []gax.CallOption 46 ExportInstance []gax.CallOption 47 FailoverInstance []gax.CallOption 48 DeleteInstance []gax.CallOption 49} 50 51func defaultCloudRedisClientOptions() []option.ClientOption { 52 return []option.ClientOption{ 53 option.WithEndpoint("redis.googleapis.com:443"), 54 option.WithScopes(DefaultAuthScopes()...), 55 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 56 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 57 } 58} 59 60func defaultCloudRedisCallOptions() *CloudRedisCallOptions { 61 retry := map[[2]string][]gax.CallOption{} 62 return &CloudRedisCallOptions{ 63 ListInstances: retry[[2]string{"default", "non_idempotent"}], 64 GetInstance: retry[[2]string{"default", "non_idempotent"}], 65 CreateInstance: retry[[2]string{"default", "non_idempotent"}], 66 UpdateInstance: retry[[2]string{"default", "non_idempotent"}], 67 ImportInstance: retry[[2]string{"default", "non_idempotent"}], 68 ExportInstance: retry[[2]string{"default", "non_idempotent"}], 69 FailoverInstance: retry[[2]string{"default", "non_idempotent"}], 70 DeleteInstance: retry[[2]string{"default", "non_idempotent"}], 71 } 72} 73 74// CloudRedisClient is a client for interacting with Google Cloud Memorystore for Redis API. 75// 76// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 77type CloudRedisClient struct { 78 // The connection to the service. 79 conn *grpc.ClientConn 80 81 // The gRPC API client. 82 cloudRedisClient redispb.CloudRedisClient 83 84 // LROClient is used internally to handle longrunning operations. 85 // It is exposed so that its CallOptions can be modified if required. 86 // Users should not Close this client. 87 LROClient *lroauto.OperationsClient 88 89 // The call options for this service. 90 CallOptions *CloudRedisCallOptions 91 92 // The x-goog-* metadata to be sent with each request. 93 xGoogMetadata metadata.MD 94} 95 96// NewCloudRedisClient creates a new cloud redis client. 97// 98// Configures and manages Cloud Memorystore for Redis instances 99// 100// Google Cloud Memorystore for Redis v1 101// 102// The redis.googleapis.com service implements the Google Cloud Memorystore 103// for Redis API and defines the following resource model for managing Redis 104// instances: 105// 106// The service works with a collection of cloud projects, named: /projects/* 107// 108// Each project has a collection of available locations, named: /locations/* 109// 110// Each location has a collection of Redis instances, named: /instances/* 111// 112// As such, Redis instances are resources of the form: 113// /projects/{project_id}/locations/{location_id}/instances/{instance_id} 114// 115// Note that location_id must be referring to a GCP region; for example: 116// 117// projects/redpepper-1290/locations/us-central1/instances/my-redis 118func NewCloudRedisClient(ctx context.Context, opts ...option.ClientOption) (*CloudRedisClient, error) { 119 conn, err := transport.DialGRPC(ctx, append(defaultCloudRedisClientOptions(), opts...)...) 120 if err != nil { 121 return nil, err 122 } 123 c := &CloudRedisClient{ 124 conn: conn, 125 CallOptions: defaultCloudRedisCallOptions(), 126 127 cloudRedisClient: redispb.NewCloudRedisClient(conn), 128 } 129 c.setGoogleClientInfo() 130 131 c.LROClient, err = lroauto.NewOperationsClient(ctx, option.WithGRPCConn(conn)) 132 if err != nil { 133 // This error "should not happen", since we are just reusing old connection 134 // and never actually need to dial. 135 // If this does happen, we could leak conn. However, we cannot close conn: 136 // If the user invoked the function with option.WithGRPCConn, 137 // we would close a connection that's still in use. 138 // TODO(pongad): investigate error conditions. 139 return nil, err 140 } 141 return c, nil 142} 143 144// Connection returns the client's connection to the API service. 145func (c *CloudRedisClient) Connection() *grpc.ClientConn { 146 return c.conn 147} 148 149// Close closes the connection to the API service. The user should invoke this when 150// the client is no longer required. 151func (c *CloudRedisClient) Close() error { 152 return c.conn.Close() 153} 154 155// setGoogleClientInfo sets the name and version of the application in 156// the `x-goog-api-client` header passed on each request. Intended for 157// use by Google-written clients. 158func (c *CloudRedisClient) setGoogleClientInfo(keyval ...string) { 159 kv := append([]string{"gl-go", versionGo()}, keyval...) 160 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 161 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 162} 163 164// ListInstances lists all Redis instances owned by a project in either the specified 165// location (region) or all locations. 166// 167// The location should have the following format: 168// 169// projects/{project_id}/locations/{location_id} 170// 171// If location_id is specified as - (wildcard), then all regions 172// available to the project are queried, and the results are aggregated. 173func (c *CloudRedisClient) ListInstances(ctx context.Context, req *redispb.ListInstancesRequest, opts ...gax.CallOption) *InstanceIterator { 174 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 175 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 176 opts = append(c.CallOptions.ListInstances[0:len(c.CallOptions.ListInstances):len(c.CallOptions.ListInstances)], opts...) 177 it := &InstanceIterator{} 178 req = proto.Clone(req).(*redispb.ListInstancesRequest) 179 it.InternalFetch = func(pageSize int, pageToken string) ([]*redispb.Instance, string, error) { 180 var resp *redispb.ListInstancesResponse 181 req.PageToken = pageToken 182 if pageSize > math.MaxInt32 { 183 req.PageSize = math.MaxInt32 184 } else { 185 req.PageSize = int32(pageSize) 186 } 187 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 188 var err error 189 resp, err = c.cloudRedisClient.ListInstances(ctx, req, settings.GRPC...) 190 return err 191 }, opts...) 192 if err != nil { 193 return nil, "", err 194 } 195 return resp.Instances, resp.NextPageToken, nil 196 } 197 fetch := func(pageSize int, pageToken string) (string, error) { 198 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 199 if err != nil { 200 return "", err 201 } 202 it.items = append(it.items, items...) 203 return nextPageToken, nil 204 } 205 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 206 it.pageInfo.MaxSize = int(req.PageSize) 207 it.pageInfo.Token = req.PageToken 208 return it 209} 210 211// GetInstance gets the details of a specific Redis instance. 212func (c *CloudRedisClient) GetInstance(ctx context.Context, req *redispb.GetInstanceRequest, opts ...gax.CallOption) (*redispb.Instance, error) { 213 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 214 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 215 opts = append(c.CallOptions.GetInstance[0:len(c.CallOptions.GetInstance):len(c.CallOptions.GetInstance)], opts...) 216 var resp *redispb.Instance 217 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 218 var err error 219 resp, err = c.cloudRedisClient.GetInstance(ctx, req, settings.GRPC...) 220 return err 221 }, opts...) 222 if err != nil { 223 return nil, err 224 } 225 return resp, nil 226} 227 228// CreateInstance creates a Redis instance based on the specified tier and memory size. 229// 230// By default, the instance is accessible from the project's 231// default network (at /compute/docs/networks-and-firewalls#networks). 232// 233// The creation is executed asynchronously and callers may check the returned 234// operation to track its progress. Once the operation is completed the Redis 235// instance will be fully functional. Completed longrunning.Operation will 236// contain the new instance object in the response field. 237// 238// The returned operation is automatically deleted after a few hours, so there 239// is no need to call DeleteOperation. 240func (c *CloudRedisClient) CreateInstance(ctx context.Context, req *redispb.CreateInstanceRequest, opts ...gax.CallOption) (*CreateInstanceOperation, error) { 241 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 242 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 243 opts = append(c.CallOptions.CreateInstance[0:len(c.CallOptions.CreateInstance):len(c.CallOptions.CreateInstance)], opts...) 244 var resp *longrunningpb.Operation 245 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 246 var err error 247 resp, err = c.cloudRedisClient.CreateInstance(ctx, req, settings.GRPC...) 248 return err 249 }, opts...) 250 if err != nil { 251 return nil, err 252 } 253 return &CreateInstanceOperation{ 254 lro: longrunning.InternalNewOperation(c.LROClient, resp), 255 }, nil 256} 257 258// UpdateInstance updates the metadata and configuration of a specific Redis instance. 259// 260// Completed longrunning.Operation will contain the new instance object 261// in the response field. The returned operation is automatically deleted 262// after a few hours, so there is no need to call DeleteOperation. 263func (c *CloudRedisClient) UpdateInstance(ctx context.Context, req *redispb.UpdateInstanceRequest, opts ...gax.CallOption) (*UpdateInstanceOperation, error) { 264 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "instance.name", url.QueryEscape(req.GetInstance().GetName()))) 265 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 266 opts = append(c.CallOptions.UpdateInstance[0:len(c.CallOptions.UpdateInstance):len(c.CallOptions.UpdateInstance)], opts...) 267 var resp *longrunningpb.Operation 268 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 269 var err error 270 resp, err = c.cloudRedisClient.UpdateInstance(ctx, req, settings.GRPC...) 271 return err 272 }, opts...) 273 if err != nil { 274 return nil, err 275 } 276 return &UpdateInstanceOperation{ 277 lro: longrunning.InternalNewOperation(c.LROClient, resp), 278 }, nil 279} 280 281// ImportInstance import a Redis RDB snapshot file from Cloud Storage into a Redis instance. 282// 283// Redis may stop serving during this operation. Instance state will be 284// IMPORTING for entire operation. When complete, the instance will contain 285// only data from the imported file. 286// 287// The returned operation is automatically deleted after a few hours, so 288// there is no need to call DeleteOperation. 289func (c *CloudRedisClient) ImportInstance(ctx context.Context, req *redispb.ImportInstanceRequest, opts ...gax.CallOption) (*ImportInstanceOperation, error) { 290 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 291 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 292 opts = append(c.CallOptions.ImportInstance[0:len(c.CallOptions.ImportInstance):len(c.CallOptions.ImportInstance)], opts...) 293 var resp *longrunningpb.Operation 294 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 295 var err error 296 resp, err = c.cloudRedisClient.ImportInstance(ctx, req, settings.GRPC...) 297 return err 298 }, opts...) 299 if err != nil { 300 return nil, err 301 } 302 return &ImportInstanceOperation{ 303 lro: longrunning.InternalNewOperation(c.LROClient, resp), 304 }, nil 305} 306 307// ExportInstance export Redis instance data into a Redis RDB format file in Cloud Storage. 308// 309// Redis will continue serving during this operation. 310// 311// The returned operation is automatically deleted after a few hours, so 312// there is no need to call DeleteOperation. 313func (c *CloudRedisClient) ExportInstance(ctx context.Context, req *redispb.ExportInstanceRequest, opts ...gax.CallOption) (*ExportInstanceOperation, error) { 314 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 315 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 316 opts = append(c.CallOptions.ExportInstance[0:len(c.CallOptions.ExportInstance):len(c.CallOptions.ExportInstance)], opts...) 317 var resp *longrunningpb.Operation 318 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 319 var err error 320 resp, err = c.cloudRedisClient.ExportInstance(ctx, req, settings.GRPC...) 321 return err 322 }, opts...) 323 if err != nil { 324 return nil, err 325 } 326 return &ExportInstanceOperation{ 327 lro: longrunning.InternalNewOperation(c.LROClient, resp), 328 }, nil 329} 330 331// FailoverInstance initiates a failover of the master node to current replica node for a 332// specific STANDARD tier Cloud Memorystore for Redis instance. 333func (c *CloudRedisClient) FailoverInstance(ctx context.Context, req *redispb.FailoverInstanceRequest, opts ...gax.CallOption) (*FailoverInstanceOperation, error) { 334 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 335 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 336 opts = append(c.CallOptions.FailoverInstance[0:len(c.CallOptions.FailoverInstance):len(c.CallOptions.FailoverInstance)], opts...) 337 var resp *longrunningpb.Operation 338 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 339 var err error 340 resp, err = c.cloudRedisClient.FailoverInstance(ctx, req, settings.GRPC...) 341 return err 342 }, opts...) 343 if err != nil { 344 return nil, err 345 } 346 return &FailoverInstanceOperation{ 347 lro: longrunning.InternalNewOperation(c.LROClient, resp), 348 }, nil 349} 350 351// DeleteInstance deletes a specific Redis instance. Instance stops serving and data is 352// deleted. 353func (c *CloudRedisClient) DeleteInstance(ctx context.Context, req *redispb.DeleteInstanceRequest, opts ...gax.CallOption) (*DeleteInstanceOperation, error) { 354 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 355 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 356 opts = append(c.CallOptions.DeleteInstance[0:len(c.CallOptions.DeleteInstance):len(c.CallOptions.DeleteInstance)], opts...) 357 var resp *longrunningpb.Operation 358 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 359 var err error 360 resp, err = c.cloudRedisClient.DeleteInstance(ctx, req, settings.GRPC...) 361 return err 362 }, opts...) 363 if err != nil { 364 return nil, err 365 } 366 return &DeleteInstanceOperation{ 367 lro: longrunning.InternalNewOperation(c.LROClient, resp), 368 }, nil 369} 370 371// InstanceIterator manages a stream of *redispb.Instance. 372type InstanceIterator struct { 373 items []*redispb.Instance 374 pageInfo *iterator.PageInfo 375 nextFunc func() error 376 377 // InternalFetch is for use by the Google Cloud Libraries only. 378 // It is not part of the stable interface of this package. 379 // 380 // InternalFetch returns results from a single call to the underlying RPC. 381 // The number of results is no greater than pageSize. 382 // If there are no more results, nextPageToken is empty and err is nil. 383 InternalFetch func(pageSize int, pageToken string) (results []*redispb.Instance, nextPageToken string, err error) 384} 385 386// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 387func (it *InstanceIterator) PageInfo() *iterator.PageInfo { 388 return it.pageInfo 389} 390 391// Next returns the next result. Its second return value is iterator.Done if there are no more 392// results. Once Next returns Done, all subsequent calls will return Done. 393func (it *InstanceIterator) Next() (*redispb.Instance, error) { 394 var item *redispb.Instance 395 if err := it.nextFunc(); err != nil { 396 return item, err 397 } 398 item = it.items[0] 399 it.items = it.items[1:] 400 return item, nil 401} 402 403func (it *InstanceIterator) bufLen() int { 404 return len(it.items) 405} 406 407func (it *InstanceIterator) takeBuf() interface{} { 408 b := it.items 409 it.items = nil 410 return b 411} 412 413// CreateInstanceOperation manages a long-running operation from CreateInstance. 414type CreateInstanceOperation struct { 415 lro *longrunning.Operation 416} 417 418// CreateInstanceOperation returns a new CreateInstanceOperation from a given name. 419// The name must be that of a previously created CreateInstanceOperation, possibly from a different process. 420func (c *CloudRedisClient) CreateInstanceOperation(name string) *CreateInstanceOperation { 421 return &CreateInstanceOperation{ 422 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 423 } 424} 425 426// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 427// 428// See documentation of Poll for error-handling information. 429func (op *CreateInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) { 430 var resp redispb.Instance 431 if err := op.lro.WaitWithInterval(ctx, &resp, 360000*time.Millisecond, opts...); err != nil { 432 return nil, err 433 } 434 return &resp, nil 435} 436 437// Poll fetches the latest state of the long-running operation. 438// 439// Poll also fetches the latest metadata, which can be retrieved by Metadata. 440// 441// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 442// the operation has completed with failure, the error is returned and op.Done will return true. 443// If Poll succeeds and the operation has completed successfully, 444// op.Done will return true, and the response of the operation is returned. 445// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 446func (op *CreateInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) { 447 var resp redispb.Instance 448 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 449 return nil, err 450 } 451 if !op.Done() { 452 return nil, nil 453 } 454 return &resp, nil 455} 456 457// Metadata returns metadata associated with the long-running operation. 458// Metadata itself does not contact the server, but Poll does. 459// To get the latest metadata, call this method after a successful call to Poll. 460// If the metadata is not available, the returned metadata and error are both nil. 461func (op *CreateInstanceOperation) Metadata() (*redispb.OperationMetadata, error) { 462 var meta redispb.OperationMetadata 463 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 464 return nil, nil 465 } else if err != nil { 466 return nil, err 467 } 468 return &meta, nil 469} 470 471// Done reports whether the long-running operation has completed. 472func (op *CreateInstanceOperation) Done() bool { 473 return op.lro.Done() 474} 475 476// Name returns the name of the long-running operation. 477// The name is assigned by the server and is unique within the service from which the operation is created. 478func (op *CreateInstanceOperation) Name() string { 479 return op.lro.Name() 480} 481 482// DeleteInstanceOperation manages a long-running operation from DeleteInstance. 483type DeleteInstanceOperation struct { 484 lro *longrunning.Operation 485} 486 487// DeleteInstanceOperation returns a new DeleteInstanceOperation from a given name. 488// The name must be that of a previously created DeleteInstanceOperation, possibly from a different process. 489func (c *CloudRedisClient) DeleteInstanceOperation(name string) *DeleteInstanceOperation { 490 return &DeleteInstanceOperation{ 491 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 492 } 493} 494 495// Wait blocks until the long-running operation is completed, returning any error encountered. 496// 497// See documentation of Poll for error-handling information. 498func (op *DeleteInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) error { 499 return op.lro.WaitWithInterval(ctx, nil, 360000*time.Millisecond, opts...) 500} 501 502// Poll fetches the latest state of the long-running operation. 503// 504// Poll also fetches the latest metadata, which can be retrieved by Metadata. 505// 506// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 507// the operation has completed with failure, the error is returned and op.Done will return true. 508// If Poll succeeds and the operation has completed successfully, op.Done will return true. 509func (op *DeleteInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) error { 510 return op.lro.Poll(ctx, nil, opts...) 511} 512 513// Metadata returns metadata associated with the long-running operation. 514// Metadata itself does not contact the server, but Poll does. 515// To get the latest metadata, call this method after a successful call to Poll. 516// If the metadata is not available, the returned metadata and error are both nil. 517func (op *DeleteInstanceOperation) Metadata() (*redispb.OperationMetadata, error) { 518 var meta redispb.OperationMetadata 519 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 520 return nil, nil 521 } else if err != nil { 522 return nil, err 523 } 524 return &meta, nil 525} 526 527// Done reports whether the long-running operation has completed. 528func (op *DeleteInstanceOperation) Done() bool { 529 return op.lro.Done() 530} 531 532// Name returns the name of the long-running operation. 533// The name is assigned by the server and is unique within the service from which the operation is created. 534func (op *DeleteInstanceOperation) Name() string { 535 return op.lro.Name() 536} 537 538// ExportInstanceOperation manages a long-running operation from ExportInstance. 539type ExportInstanceOperation struct { 540 lro *longrunning.Operation 541} 542 543// ExportInstanceOperation returns a new ExportInstanceOperation from a given name. 544// The name must be that of a previously created ExportInstanceOperation, possibly from a different process. 545func (c *CloudRedisClient) ExportInstanceOperation(name string) *ExportInstanceOperation { 546 return &ExportInstanceOperation{ 547 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 548 } 549} 550 551// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 552// 553// See documentation of Poll for error-handling information. 554func (op *ExportInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) { 555 var resp redispb.Instance 556 if err := op.lro.WaitWithInterval(ctx, &resp, 360000*time.Millisecond, opts...); err != nil { 557 return nil, err 558 } 559 return &resp, nil 560} 561 562// Poll fetches the latest state of the long-running operation. 563// 564// Poll also fetches the latest metadata, which can be retrieved by Metadata. 565// 566// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 567// the operation has completed with failure, the error is returned and op.Done will return true. 568// If Poll succeeds and the operation has completed successfully, 569// op.Done will return true, and the response of the operation is returned. 570// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 571func (op *ExportInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) { 572 var resp redispb.Instance 573 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 574 return nil, err 575 } 576 if !op.Done() { 577 return nil, nil 578 } 579 return &resp, nil 580} 581 582// Metadata returns metadata associated with the long-running operation. 583// Metadata itself does not contact the server, but Poll does. 584// To get the latest metadata, call this method after a successful call to Poll. 585// If the metadata is not available, the returned metadata and error are both nil. 586func (op *ExportInstanceOperation) Metadata() (*redispb.OperationMetadata, error) { 587 var meta redispb.OperationMetadata 588 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 589 return nil, nil 590 } else if err != nil { 591 return nil, err 592 } 593 return &meta, nil 594} 595 596// Done reports whether the long-running operation has completed. 597func (op *ExportInstanceOperation) Done() bool { 598 return op.lro.Done() 599} 600 601// Name returns the name of the long-running operation. 602// The name is assigned by the server and is unique within the service from which the operation is created. 603func (op *ExportInstanceOperation) Name() string { 604 return op.lro.Name() 605} 606 607// FailoverInstanceOperation manages a long-running operation from FailoverInstance. 608type FailoverInstanceOperation struct { 609 lro *longrunning.Operation 610} 611 612// FailoverInstanceOperation returns a new FailoverInstanceOperation from a given name. 613// The name must be that of a previously created FailoverInstanceOperation, possibly from a different process. 614func (c *CloudRedisClient) FailoverInstanceOperation(name string) *FailoverInstanceOperation { 615 return &FailoverInstanceOperation{ 616 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 617 } 618} 619 620// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 621// 622// See documentation of Poll for error-handling information. 623func (op *FailoverInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) { 624 var resp redispb.Instance 625 if err := op.lro.WaitWithInterval(ctx, &resp, 360000*time.Millisecond, opts...); err != nil { 626 return nil, err 627 } 628 return &resp, nil 629} 630 631// Poll fetches the latest state of the long-running operation. 632// 633// Poll also fetches the latest metadata, which can be retrieved by Metadata. 634// 635// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 636// the operation has completed with failure, the error is returned and op.Done will return true. 637// If Poll succeeds and the operation has completed successfully, 638// op.Done will return true, and the response of the operation is returned. 639// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 640func (op *FailoverInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) { 641 var resp redispb.Instance 642 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 643 return nil, err 644 } 645 if !op.Done() { 646 return nil, nil 647 } 648 return &resp, nil 649} 650 651// Metadata returns metadata associated with the long-running operation. 652// Metadata itself does not contact the server, but Poll does. 653// To get the latest metadata, call this method after a successful call to Poll. 654// If the metadata is not available, the returned metadata and error are both nil. 655func (op *FailoverInstanceOperation) Metadata() (*redispb.OperationMetadata, error) { 656 var meta redispb.OperationMetadata 657 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 658 return nil, nil 659 } else if err != nil { 660 return nil, err 661 } 662 return &meta, nil 663} 664 665// Done reports whether the long-running operation has completed. 666func (op *FailoverInstanceOperation) Done() bool { 667 return op.lro.Done() 668} 669 670// Name returns the name of the long-running operation. 671// The name is assigned by the server and is unique within the service from which the operation is created. 672func (op *FailoverInstanceOperation) Name() string { 673 return op.lro.Name() 674} 675 676// ImportInstanceOperation manages a long-running operation from ImportInstance. 677type ImportInstanceOperation struct { 678 lro *longrunning.Operation 679} 680 681// ImportInstanceOperation returns a new ImportInstanceOperation from a given name. 682// The name must be that of a previously created ImportInstanceOperation, possibly from a different process. 683func (c *CloudRedisClient) ImportInstanceOperation(name string) *ImportInstanceOperation { 684 return &ImportInstanceOperation{ 685 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 686 } 687} 688 689// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 690// 691// See documentation of Poll for error-handling information. 692func (op *ImportInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) { 693 var resp redispb.Instance 694 if err := op.lro.WaitWithInterval(ctx, &resp, 360000*time.Millisecond, opts...); err != nil { 695 return nil, err 696 } 697 return &resp, nil 698} 699 700// Poll fetches the latest state of the long-running operation. 701// 702// Poll also fetches the latest metadata, which can be retrieved by Metadata. 703// 704// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 705// the operation has completed with failure, the error is returned and op.Done will return true. 706// If Poll succeeds and the operation has completed successfully, 707// op.Done will return true, and the response of the operation is returned. 708// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 709func (op *ImportInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) { 710 var resp redispb.Instance 711 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 712 return nil, err 713 } 714 if !op.Done() { 715 return nil, nil 716 } 717 return &resp, nil 718} 719 720// Metadata returns metadata associated with the long-running operation. 721// Metadata itself does not contact the server, but Poll does. 722// To get the latest metadata, call this method after a successful call to Poll. 723// If the metadata is not available, the returned metadata and error are both nil. 724func (op *ImportInstanceOperation) Metadata() (*redispb.OperationMetadata, error) { 725 var meta redispb.OperationMetadata 726 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 727 return nil, nil 728 } else if err != nil { 729 return nil, err 730 } 731 return &meta, nil 732} 733 734// Done reports whether the long-running operation has completed. 735func (op *ImportInstanceOperation) Done() bool { 736 return op.lro.Done() 737} 738 739// Name returns the name of the long-running operation. 740// The name is assigned by the server and is unique within the service from which the operation is created. 741func (op *ImportInstanceOperation) Name() string { 742 return op.lro.Name() 743} 744 745// UpdateInstanceOperation manages a long-running operation from UpdateInstance. 746type UpdateInstanceOperation struct { 747 lro *longrunning.Operation 748} 749 750// UpdateInstanceOperation returns a new UpdateInstanceOperation from a given name. 751// The name must be that of a previously created UpdateInstanceOperation, possibly from a different process. 752func (c *CloudRedisClient) UpdateInstanceOperation(name string) *UpdateInstanceOperation { 753 return &UpdateInstanceOperation{ 754 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 755 } 756} 757 758// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 759// 760// See documentation of Poll for error-handling information. 761func (op *UpdateInstanceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) { 762 var resp redispb.Instance 763 if err := op.lro.WaitWithInterval(ctx, &resp, 360000*time.Millisecond, opts...); err != nil { 764 return nil, err 765 } 766 return &resp, nil 767} 768 769// Poll fetches the latest state of the long-running operation. 770// 771// Poll also fetches the latest metadata, which can be retrieved by Metadata. 772// 773// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 774// the operation has completed with failure, the error is returned and op.Done will return true. 775// If Poll succeeds and the operation has completed successfully, 776// op.Done will return true, and the response of the operation is returned. 777// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 778func (op *UpdateInstanceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*redispb.Instance, error) { 779 var resp redispb.Instance 780 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 781 return nil, err 782 } 783 if !op.Done() { 784 return nil, nil 785 } 786 return &resp, nil 787} 788 789// Metadata returns metadata associated with the long-running operation. 790// Metadata itself does not contact the server, but Poll does. 791// To get the latest metadata, call this method after a successful call to Poll. 792// If the metadata is not available, the returned metadata and error are both nil. 793func (op *UpdateInstanceOperation) Metadata() (*redispb.OperationMetadata, error) { 794 var meta redispb.OperationMetadata 795 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 796 return nil, nil 797 } else if err != nil { 798 return nil, err 799 } 800 return &meta, nil 801} 802 803// Done reports whether the long-running operation has completed. 804func (op *UpdateInstanceOperation) Done() bool { 805 return op.lro.Done() 806} 807 808// Name returns the name of the long-running operation. 809// The name is assigned by the server and is unique within the service from which the operation is created. 810func (op *UpdateInstanceOperation) Name() string { 811 return op.lro.Name() 812} 813