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 apiv1 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/option/internaloption" 33 gtransport "google.golang.org/api/transport/grpc" 34 adminpb "google.golang.org/genproto/googleapis/firestore/admin/v1" 35 longrunningpb "google.golang.org/genproto/googleapis/longrunning" 36 "google.golang.org/grpc" 37 "google.golang.org/grpc/codes" 38 "google.golang.org/grpc/metadata" 39) 40 41var newFirestoreAdminClientHook clientHook 42 43// FirestoreAdminCallOptions contains the retry settings for each method of FirestoreAdminClient. 44type FirestoreAdminCallOptions struct { 45 CreateIndex []gax.CallOption 46 ListIndexes []gax.CallOption 47 GetIndex []gax.CallOption 48 DeleteIndex []gax.CallOption 49 GetField []gax.CallOption 50 UpdateField []gax.CallOption 51 ListFields []gax.CallOption 52 ExportDocuments []gax.CallOption 53 ImportDocuments []gax.CallOption 54} 55 56func defaultFirestoreAdminClientOptions() []option.ClientOption { 57 return []option.ClientOption{ 58 internaloption.WithDefaultEndpoint("firestore.googleapis.com:443"), 59 internaloption.WithDefaultMTLSEndpoint("firestore.mtls.googleapis.com:443"), 60 internaloption.WithDefaultAudience("https://firestore.googleapis.com/"), 61 internaloption.WithDefaultScopes(DefaultAuthScopes()...), 62 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), 63 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 64 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 65 } 66} 67 68func defaultFirestoreAdminCallOptions() *FirestoreAdminCallOptions { 69 return &FirestoreAdminCallOptions{ 70 CreateIndex: []gax.CallOption{}, 71 ListIndexes: []gax.CallOption{ 72 gax.WithRetry(func() gax.Retryer { 73 return gax.OnCodes([]codes.Code{ 74 codes.Unavailable, 75 codes.Internal, 76 codes.DeadlineExceeded, 77 }, gax.Backoff{ 78 Initial: 100 * time.Millisecond, 79 Max: 60000 * time.Millisecond, 80 Multiplier: 1.30, 81 }) 82 }), 83 }, 84 GetIndex: []gax.CallOption{ 85 gax.WithRetry(func() gax.Retryer { 86 return gax.OnCodes([]codes.Code{ 87 codes.Unavailable, 88 codes.Internal, 89 codes.DeadlineExceeded, 90 }, gax.Backoff{ 91 Initial: 100 * time.Millisecond, 92 Max: 60000 * time.Millisecond, 93 Multiplier: 1.30, 94 }) 95 }), 96 }, 97 DeleteIndex: []gax.CallOption{ 98 gax.WithRetry(func() gax.Retryer { 99 return gax.OnCodes([]codes.Code{ 100 codes.Unavailable, 101 codes.Internal, 102 codes.DeadlineExceeded, 103 }, gax.Backoff{ 104 Initial: 100 * time.Millisecond, 105 Max: 60000 * time.Millisecond, 106 Multiplier: 1.30, 107 }) 108 }), 109 }, 110 GetField: []gax.CallOption{ 111 gax.WithRetry(func() gax.Retryer { 112 return gax.OnCodes([]codes.Code{ 113 codes.Unavailable, 114 codes.Internal, 115 codes.DeadlineExceeded, 116 }, gax.Backoff{ 117 Initial: 100 * time.Millisecond, 118 Max: 60000 * time.Millisecond, 119 Multiplier: 1.30, 120 }) 121 }), 122 }, 123 UpdateField: []gax.CallOption{}, 124 ListFields: []gax.CallOption{ 125 gax.WithRetry(func() gax.Retryer { 126 return gax.OnCodes([]codes.Code{ 127 codes.Unavailable, 128 codes.Internal, 129 codes.DeadlineExceeded, 130 }, gax.Backoff{ 131 Initial: 100 * time.Millisecond, 132 Max: 60000 * time.Millisecond, 133 Multiplier: 1.30, 134 }) 135 }), 136 }, 137 ExportDocuments: []gax.CallOption{}, 138 ImportDocuments: []gax.CallOption{}, 139 } 140} 141 142// FirestoreAdminClient is a client for interacting with Google Cloud Firestore Admin API. 143// 144// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 145type FirestoreAdminClient struct { 146 // Connection pool of gRPC connections to the service. 147 connPool gtransport.ConnPool 148 149 // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE 150 disableDeadlines bool 151 152 // The gRPC API client. 153 firestoreAdminClient adminpb.FirestoreAdminClient 154 155 // LROClient is used internally to handle longrunning operations. 156 // It is exposed so that its CallOptions can be modified if required. 157 // Users should not Close this client. 158 LROClient *lroauto.OperationsClient 159 160 // The call options for this service. 161 CallOptions *FirestoreAdminCallOptions 162 163 // The x-goog-* metadata to be sent with each request. 164 xGoogMetadata metadata.MD 165} 166 167// NewFirestoreAdminClient creates a new firestore admin client. 168// 169// Operations are created by service FirestoreAdmin, but are accessed via 170// service google.longrunning.Operations. 171func NewFirestoreAdminClient(ctx context.Context, opts ...option.ClientOption) (*FirestoreAdminClient, error) { 172 clientOpts := defaultFirestoreAdminClientOptions() 173 174 if newFirestoreAdminClientHook != nil { 175 hookOpts, err := newFirestoreAdminClientHook(ctx, clientHookParams{}) 176 if err != nil { 177 return nil, err 178 } 179 clientOpts = append(clientOpts, hookOpts...) 180 } 181 182 disableDeadlines, err := checkDisableDeadlines() 183 if err != nil { 184 return nil, err 185 } 186 187 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 188 if err != nil { 189 return nil, err 190 } 191 c := &FirestoreAdminClient{ 192 connPool: connPool, 193 disableDeadlines: disableDeadlines, 194 CallOptions: defaultFirestoreAdminCallOptions(), 195 196 firestoreAdminClient: adminpb.NewFirestoreAdminClient(connPool), 197 } 198 c.setGoogleClientInfo() 199 200 c.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool)) 201 if err != nil { 202 // This error "should not happen", since we are just reusing old connection pool 203 // and never actually need to dial. 204 // If this does happen, we could leak connp. However, we cannot close conn: 205 // If the user invoked the constructor with option.WithGRPCConn, 206 // we would close a connection that's still in use. 207 // TODO: investigate error conditions. 208 return nil, err 209 } 210 return c, nil 211} 212 213// Connection returns a connection to the API service. 214// 215// Deprecated. 216func (c *FirestoreAdminClient) Connection() *grpc.ClientConn { 217 return c.connPool.Conn() 218} 219 220// Close closes the connection to the API service. The user should invoke this when 221// the client is no longer required. 222func (c *FirestoreAdminClient) Close() error { 223 return c.connPool.Close() 224} 225 226// setGoogleClientInfo sets the name and version of the application in 227// the `x-goog-api-client` header passed on each request. Intended for 228// use by Google-written clients. 229func (c *FirestoreAdminClient) setGoogleClientInfo(keyval ...string) { 230 kv := append([]string{"gl-go", versionGo()}, keyval...) 231 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 232 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 233} 234 235// CreateIndex creates a composite index. This returns a google.longrunning.Operation 236// which may be used to track the status of the creation. The metadata for 237// the operation will be the type IndexOperationMetadata. 238func (c *FirestoreAdminClient) CreateIndex(ctx context.Context, req *adminpb.CreateIndexRequest, opts ...gax.CallOption) (*CreateIndexOperation, error) { 239 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 240 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 241 defer cancel() 242 ctx = cctx 243 } 244 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 245 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 246 opts = append(c.CallOptions.CreateIndex[0:len(c.CallOptions.CreateIndex):len(c.CallOptions.CreateIndex)], opts...) 247 var resp *longrunningpb.Operation 248 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 249 var err error 250 resp, err = c.firestoreAdminClient.CreateIndex(ctx, req, settings.GRPC...) 251 return err 252 }, opts...) 253 if err != nil { 254 return nil, err 255 } 256 return &CreateIndexOperation{ 257 lro: longrunning.InternalNewOperation(c.LROClient, resp), 258 }, nil 259} 260 261// ListIndexes lists composite indexes. 262func (c *FirestoreAdminClient) ListIndexes(ctx context.Context, req *adminpb.ListIndexesRequest, opts ...gax.CallOption) *IndexIterator { 263 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 264 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 265 opts = append(c.CallOptions.ListIndexes[0:len(c.CallOptions.ListIndexes):len(c.CallOptions.ListIndexes)], opts...) 266 it := &IndexIterator{} 267 req = proto.Clone(req).(*adminpb.ListIndexesRequest) 268 it.InternalFetch = func(pageSize int, pageToken string) ([]*adminpb.Index, string, error) { 269 var resp *adminpb.ListIndexesResponse 270 req.PageToken = pageToken 271 if pageSize > math.MaxInt32 { 272 req.PageSize = math.MaxInt32 273 } else { 274 req.PageSize = int32(pageSize) 275 } 276 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 277 var err error 278 resp, err = c.firestoreAdminClient.ListIndexes(ctx, req, settings.GRPC...) 279 return err 280 }, opts...) 281 if err != nil { 282 return nil, "", err 283 } 284 285 it.Response = resp 286 return resp.GetIndexes(), resp.GetNextPageToken(), nil 287 } 288 fetch := func(pageSize int, pageToken string) (string, error) { 289 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 290 if err != nil { 291 return "", err 292 } 293 it.items = append(it.items, items...) 294 return nextPageToken, nil 295 } 296 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 297 it.pageInfo.MaxSize = int(req.GetPageSize()) 298 it.pageInfo.Token = req.GetPageToken() 299 return it 300} 301 302// GetIndex gets a composite index. 303func (c *FirestoreAdminClient) GetIndex(ctx context.Context, req *adminpb.GetIndexRequest, opts ...gax.CallOption) (*adminpb.Index, error) { 304 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 305 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 306 defer cancel() 307 ctx = cctx 308 } 309 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 310 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 311 opts = append(c.CallOptions.GetIndex[0:len(c.CallOptions.GetIndex):len(c.CallOptions.GetIndex)], opts...) 312 var resp *adminpb.Index 313 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 314 var err error 315 resp, err = c.firestoreAdminClient.GetIndex(ctx, req, settings.GRPC...) 316 return err 317 }, opts...) 318 if err != nil { 319 return nil, err 320 } 321 return resp, nil 322} 323 324// DeleteIndex deletes a composite index. 325func (c *FirestoreAdminClient) DeleteIndex(ctx context.Context, req *adminpb.DeleteIndexRequest, opts ...gax.CallOption) error { 326 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 327 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 328 defer cancel() 329 ctx = cctx 330 } 331 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 332 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 333 opts = append(c.CallOptions.DeleteIndex[0:len(c.CallOptions.DeleteIndex):len(c.CallOptions.DeleteIndex)], opts...) 334 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 335 var err error 336 _, err = c.firestoreAdminClient.DeleteIndex(ctx, req, settings.GRPC...) 337 return err 338 }, opts...) 339 return err 340} 341 342// GetField gets the metadata and configuration for a Field. 343func (c *FirestoreAdminClient) GetField(ctx context.Context, req *adminpb.GetFieldRequest, opts ...gax.CallOption) (*adminpb.Field, error) { 344 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 345 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 346 defer cancel() 347 ctx = cctx 348 } 349 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 350 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 351 opts = append(c.CallOptions.GetField[0:len(c.CallOptions.GetField):len(c.CallOptions.GetField)], opts...) 352 var resp *adminpb.Field 353 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 354 var err error 355 resp, err = c.firestoreAdminClient.GetField(ctx, req, settings.GRPC...) 356 return err 357 }, opts...) 358 if err != nil { 359 return nil, err 360 } 361 return resp, nil 362} 363 364// UpdateField updates a field configuration. Currently, field updates apply only to 365// single field index configuration. However, calls to 366// FirestoreAdmin.UpdateField should provide a field mask to avoid 367// changing any configuration that the caller isn’t aware of. The field mask 368// should be specified as: { paths: "index_config" }. 369// 370// This call returns a google.longrunning.Operation which may be used to 371// track the status of the field update. The metadata for 372// the operation will be the type FieldOperationMetadata. 373// 374// To configure the default field settings for the database, use 375// the special Field with resource name: 376// projects/{project_id}/databases/{database_id}/collectionGroups/__default__/fields/*. 377func (c *FirestoreAdminClient) UpdateField(ctx context.Context, req *adminpb.UpdateFieldRequest, opts ...gax.CallOption) (*UpdateFieldOperation, error) { 378 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 379 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 380 defer cancel() 381 ctx = cctx 382 } 383 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "field.name", url.QueryEscape(req.GetField().GetName()))) 384 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 385 opts = append(c.CallOptions.UpdateField[0:len(c.CallOptions.UpdateField):len(c.CallOptions.UpdateField)], opts...) 386 var resp *longrunningpb.Operation 387 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 388 var err error 389 resp, err = c.firestoreAdminClient.UpdateField(ctx, req, settings.GRPC...) 390 return err 391 }, opts...) 392 if err != nil { 393 return nil, err 394 } 395 return &UpdateFieldOperation{ 396 lro: longrunning.InternalNewOperation(c.LROClient, resp), 397 }, nil 398} 399 400// ListFields lists the field configuration and metadata for this database. 401// 402// Currently, FirestoreAdmin.ListFields only supports listing fields 403// that have been explicitly overridden. To issue this query, call 404// FirestoreAdmin.ListFields with the filter set to 405// indexConfig.usesAncestorConfig:false. 406func (c *FirestoreAdminClient) ListFields(ctx context.Context, req *adminpb.ListFieldsRequest, opts ...gax.CallOption) *FieldIterator { 407 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 408 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 409 opts = append(c.CallOptions.ListFields[0:len(c.CallOptions.ListFields):len(c.CallOptions.ListFields)], opts...) 410 it := &FieldIterator{} 411 req = proto.Clone(req).(*adminpb.ListFieldsRequest) 412 it.InternalFetch = func(pageSize int, pageToken string) ([]*adminpb.Field, string, error) { 413 var resp *adminpb.ListFieldsResponse 414 req.PageToken = pageToken 415 if pageSize > math.MaxInt32 { 416 req.PageSize = math.MaxInt32 417 } else { 418 req.PageSize = int32(pageSize) 419 } 420 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 421 var err error 422 resp, err = c.firestoreAdminClient.ListFields(ctx, req, settings.GRPC...) 423 return err 424 }, opts...) 425 if err != nil { 426 return nil, "", err 427 } 428 429 it.Response = resp 430 return resp.GetFields(), resp.GetNextPageToken(), nil 431 } 432 fetch := func(pageSize int, pageToken string) (string, error) { 433 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 434 if err != nil { 435 return "", err 436 } 437 it.items = append(it.items, items...) 438 return nextPageToken, nil 439 } 440 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 441 it.pageInfo.MaxSize = int(req.GetPageSize()) 442 it.pageInfo.Token = req.GetPageToken() 443 return it 444} 445 446// ExportDocuments exports a copy of all or a subset of documents from Google Cloud Firestore 447// to another storage system, such as Google Cloud Storage. Recent updates to 448// documents may not be reflected in the export. The export occurs in the 449// background and its progress can be monitored and managed via the 450// Operation resource that is created. The output of an export may only be 451// used once the associated operation is done. If an export operation is 452// cancelled before completion it may leave partial data behind in Google 453// Cloud Storage. 454func (c *FirestoreAdminClient) ExportDocuments(ctx context.Context, req *adminpb.ExportDocumentsRequest, opts ...gax.CallOption) (*ExportDocumentsOperation, error) { 455 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 456 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 457 defer cancel() 458 ctx = cctx 459 } 460 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 461 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 462 opts = append(c.CallOptions.ExportDocuments[0:len(c.CallOptions.ExportDocuments):len(c.CallOptions.ExportDocuments)], opts...) 463 var resp *longrunningpb.Operation 464 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 465 var err error 466 resp, err = c.firestoreAdminClient.ExportDocuments(ctx, req, settings.GRPC...) 467 return err 468 }, opts...) 469 if err != nil { 470 return nil, err 471 } 472 return &ExportDocumentsOperation{ 473 lro: longrunning.InternalNewOperation(c.LROClient, resp), 474 }, nil 475} 476 477// ImportDocuments imports documents into Google Cloud Firestore. Existing documents with the 478// same name are overwritten. The import occurs in the background and its 479// progress can be monitored and managed via the Operation resource that is 480// created. If an ImportDocuments operation is cancelled, it is possible 481// that a subset of the data has already been imported to Cloud Firestore. 482func (c *FirestoreAdminClient) ImportDocuments(ctx context.Context, req *adminpb.ImportDocumentsRequest, opts ...gax.CallOption) (*ImportDocumentsOperation, error) { 483 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 484 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 485 defer cancel() 486 ctx = cctx 487 } 488 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 489 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 490 opts = append(c.CallOptions.ImportDocuments[0:len(c.CallOptions.ImportDocuments):len(c.CallOptions.ImportDocuments)], opts...) 491 var resp *longrunningpb.Operation 492 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 493 var err error 494 resp, err = c.firestoreAdminClient.ImportDocuments(ctx, req, settings.GRPC...) 495 return err 496 }, opts...) 497 if err != nil { 498 return nil, err 499 } 500 return &ImportDocumentsOperation{ 501 lro: longrunning.InternalNewOperation(c.LROClient, resp), 502 }, nil 503} 504 505// CreateIndexOperation manages a long-running operation from CreateIndex. 506type CreateIndexOperation struct { 507 lro *longrunning.Operation 508} 509 510// CreateIndexOperation returns a new CreateIndexOperation from a given name. 511// The name must be that of a previously created CreateIndexOperation, possibly from a different process. 512func (c *FirestoreAdminClient) CreateIndexOperation(name string) *CreateIndexOperation { 513 return &CreateIndexOperation{ 514 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 515 } 516} 517 518// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 519// 520// See documentation of Poll for error-handling information. 521func (op *CreateIndexOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*adminpb.Index, error) { 522 var resp adminpb.Index 523 if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { 524 return nil, err 525 } 526 return &resp, nil 527} 528 529// Poll fetches the latest state of the long-running operation. 530// 531// Poll also fetches the latest metadata, which can be retrieved by Metadata. 532// 533// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 534// the operation has completed with failure, the error is returned and op.Done will return true. 535// If Poll succeeds and the operation has completed successfully, 536// op.Done will return true, and the response of the operation is returned. 537// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 538func (op *CreateIndexOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*adminpb.Index, error) { 539 var resp adminpb.Index 540 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 541 return nil, err 542 } 543 if !op.Done() { 544 return nil, nil 545 } 546 return &resp, nil 547} 548 549// Metadata returns metadata associated with the long-running operation. 550// Metadata itself does not contact the server, but Poll does. 551// To get the latest metadata, call this method after a successful call to Poll. 552// If the metadata is not available, the returned metadata and error are both nil. 553func (op *CreateIndexOperation) Metadata() (*adminpb.IndexOperationMetadata, error) { 554 var meta adminpb.IndexOperationMetadata 555 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 556 return nil, nil 557 } else if err != nil { 558 return nil, err 559 } 560 return &meta, nil 561} 562 563// Done reports whether the long-running operation has completed. 564func (op *CreateIndexOperation) Done() bool { 565 return op.lro.Done() 566} 567 568// Name returns the name of the long-running operation. 569// The name is assigned by the server and is unique within the service from which the operation is created. 570func (op *CreateIndexOperation) Name() string { 571 return op.lro.Name() 572} 573 574// ExportDocumentsOperation manages a long-running operation from ExportDocuments. 575type ExportDocumentsOperation struct { 576 lro *longrunning.Operation 577} 578 579// ExportDocumentsOperation returns a new ExportDocumentsOperation from a given name. 580// The name must be that of a previously created ExportDocumentsOperation, possibly from a different process. 581func (c *FirestoreAdminClient) ExportDocumentsOperation(name string) *ExportDocumentsOperation { 582 return &ExportDocumentsOperation{ 583 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 584 } 585} 586 587// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 588// 589// See documentation of Poll for error-handling information. 590func (op *ExportDocumentsOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*adminpb.ExportDocumentsResponse, error) { 591 var resp adminpb.ExportDocumentsResponse 592 if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { 593 return nil, err 594 } 595 return &resp, nil 596} 597 598// Poll fetches the latest state of the long-running operation. 599// 600// Poll also fetches the latest metadata, which can be retrieved by Metadata. 601// 602// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 603// the operation has completed with failure, the error is returned and op.Done will return true. 604// If Poll succeeds and the operation has completed successfully, 605// op.Done will return true, and the response of the operation is returned. 606// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 607func (op *ExportDocumentsOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*adminpb.ExportDocumentsResponse, error) { 608 var resp adminpb.ExportDocumentsResponse 609 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 610 return nil, err 611 } 612 if !op.Done() { 613 return nil, nil 614 } 615 return &resp, nil 616} 617 618// Metadata returns metadata associated with the long-running operation. 619// Metadata itself does not contact the server, but Poll does. 620// To get the latest metadata, call this method after a successful call to Poll. 621// If the metadata is not available, the returned metadata and error are both nil. 622func (op *ExportDocumentsOperation) Metadata() (*adminpb.ExportDocumentsMetadata, error) { 623 var meta adminpb.ExportDocumentsMetadata 624 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 625 return nil, nil 626 } else if err != nil { 627 return nil, err 628 } 629 return &meta, nil 630} 631 632// Done reports whether the long-running operation has completed. 633func (op *ExportDocumentsOperation) Done() bool { 634 return op.lro.Done() 635} 636 637// Name returns the name of the long-running operation. 638// The name is assigned by the server and is unique within the service from which the operation is created. 639func (op *ExportDocumentsOperation) Name() string { 640 return op.lro.Name() 641} 642 643// ImportDocumentsOperation manages a long-running operation from ImportDocuments. 644type ImportDocumentsOperation struct { 645 lro *longrunning.Operation 646} 647 648// ImportDocumentsOperation returns a new ImportDocumentsOperation from a given name. 649// The name must be that of a previously created ImportDocumentsOperation, possibly from a different process. 650func (c *FirestoreAdminClient) ImportDocumentsOperation(name string) *ImportDocumentsOperation { 651 return &ImportDocumentsOperation{ 652 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 653 } 654} 655 656// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 657// 658// See documentation of Poll for error-handling information. 659func (op *ImportDocumentsOperation) Wait(ctx context.Context, opts ...gax.CallOption) error { 660 return op.lro.WaitWithInterval(ctx, nil, time.Minute, opts...) 661} 662 663// Poll fetches the latest state of the long-running operation. 664// 665// Poll also fetches the latest metadata, which can be retrieved by Metadata. 666// 667// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 668// the operation has completed with failure, the error is returned and op.Done will return true. 669// If Poll succeeds and the operation has completed successfully, 670// op.Done will return true, and the response of the operation is returned. 671// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 672func (op *ImportDocumentsOperation) Poll(ctx context.Context, opts ...gax.CallOption) error { 673 return op.lro.Poll(ctx, nil, opts...) 674} 675 676// Metadata returns metadata associated with the long-running operation. 677// Metadata itself does not contact the server, but Poll does. 678// To get the latest metadata, call this method after a successful call to Poll. 679// If the metadata is not available, the returned metadata and error are both nil. 680func (op *ImportDocumentsOperation) Metadata() (*adminpb.ImportDocumentsMetadata, error) { 681 var meta adminpb.ImportDocumentsMetadata 682 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 683 return nil, nil 684 } else if err != nil { 685 return nil, err 686 } 687 return &meta, nil 688} 689 690// Done reports whether the long-running operation has completed. 691func (op *ImportDocumentsOperation) Done() bool { 692 return op.lro.Done() 693} 694 695// Name returns the name of the long-running operation. 696// The name is assigned by the server and is unique within the service from which the operation is created. 697func (op *ImportDocumentsOperation) Name() string { 698 return op.lro.Name() 699} 700 701// UpdateFieldOperation manages a long-running operation from UpdateField. 702type UpdateFieldOperation struct { 703 lro *longrunning.Operation 704} 705 706// UpdateFieldOperation returns a new UpdateFieldOperation from a given name. 707// The name must be that of a previously created UpdateFieldOperation, possibly from a different process. 708func (c *FirestoreAdminClient) UpdateFieldOperation(name string) *UpdateFieldOperation { 709 return &UpdateFieldOperation{ 710 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 711 } 712} 713 714// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 715// 716// See documentation of Poll for error-handling information. 717func (op *UpdateFieldOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*adminpb.Field, error) { 718 var resp adminpb.Field 719 if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { 720 return nil, err 721 } 722 return &resp, nil 723} 724 725// Poll fetches the latest state of the long-running operation. 726// 727// Poll also fetches the latest metadata, which can be retrieved by Metadata. 728// 729// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 730// the operation has completed with failure, the error is returned and op.Done will return true. 731// If Poll succeeds and the operation has completed successfully, 732// op.Done will return true, and the response of the operation is returned. 733// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 734func (op *UpdateFieldOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*adminpb.Field, error) { 735 var resp adminpb.Field 736 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 737 return nil, err 738 } 739 if !op.Done() { 740 return nil, nil 741 } 742 return &resp, nil 743} 744 745// Metadata returns metadata associated with the long-running operation. 746// Metadata itself does not contact the server, but Poll does. 747// To get the latest metadata, call this method after a successful call to Poll. 748// If the metadata is not available, the returned metadata and error are both nil. 749func (op *UpdateFieldOperation) Metadata() (*adminpb.FieldOperationMetadata, error) { 750 var meta adminpb.FieldOperationMetadata 751 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 752 return nil, nil 753 } else if err != nil { 754 return nil, err 755 } 756 return &meta, nil 757} 758 759// Done reports whether the long-running operation has completed. 760func (op *UpdateFieldOperation) Done() bool { 761 return op.lro.Done() 762} 763 764// Name returns the name of the long-running operation. 765// The name is assigned by the server and is unique within the service from which the operation is created. 766func (op *UpdateFieldOperation) Name() string { 767 return op.lro.Name() 768} 769 770// FieldIterator manages a stream of *adminpb.Field. 771type FieldIterator struct { 772 items []*adminpb.Field 773 pageInfo *iterator.PageInfo 774 nextFunc func() error 775 776 // Response is the raw response for the current page. 777 // It must be cast to the RPC response type. 778 // Calling Next() or InternalFetch() updates this value. 779 Response interface{} 780 781 // InternalFetch is for use by the Google Cloud Libraries only. 782 // It is not part of the stable interface of this package. 783 // 784 // InternalFetch returns results from a single call to the underlying RPC. 785 // The number of results is no greater than pageSize. 786 // If there are no more results, nextPageToken is empty and err is nil. 787 InternalFetch func(pageSize int, pageToken string) (results []*adminpb.Field, nextPageToken string, err error) 788} 789 790// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 791func (it *FieldIterator) PageInfo() *iterator.PageInfo { 792 return it.pageInfo 793} 794 795// Next returns the next result. Its second return value is iterator.Done if there are no more 796// results. Once Next returns Done, all subsequent calls will return Done. 797func (it *FieldIterator) Next() (*adminpb.Field, error) { 798 var item *adminpb.Field 799 if err := it.nextFunc(); err != nil { 800 return item, err 801 } 802 item = it.items[0] 803 it.items = it.items[1:] 804 return item, nil 805} 806 807func (it *FieldIterator) bufLen() int { 808 return len(it.items) 809} 810 811func (it *FieldIterator) takeBuf() interface{} { 812 b := it.items 813 it.items = nil 814 return b 815} 816 817// IndexIterator manages a stream of *adminpb.Index. 818type IndexIterator struct { 819 items []*adminpb.Index 820 pageInfo *iterator.PageInfo 821 nextFunc func() error 822 823 // Response is the raw response for the current page. 824 // It must be cast to the RPC response type. 825 // Calling Next() or InternalFetch() updates this value. 826 Response interface{} 827 828 // InternalFetch is for use by the Google Cloud Libraries only. 829 // It is not part of the stable interface of this package. 830 // 831 // InternalFetch returns results from a single call to the underlying RPC. 832 // The number of results is no greater than pageSize. 833 // If there are no more results, nextPageToken is empty and err is nil. 834 InternalFetch func(pageSize int, pageToken string) (results []*adminpb.Index, nextPageToken string, err error) 835} 836 837// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 838func (it *IndexIterator) PageInfo() *iterator.PageInfo { 839 return it.pageInfo 840} 841 842// Next returns the next result. Its second return value is iterator.Done if there are no more 843// results. Once Next returns Done, all subsequent calls will return Done. 844func (it *IndexIterator) Next() (*adminpb.Index, error) { 845 var item *adminpb.Index 846 if err := it.nextFunc(); err != nil { 847 return item, err 848 } 849 item = it.items[0] 850 it.items = it.items[1:] 851 return item, nil 852} 853 854func (it *IndexIterator) bufLen() int { 855 return len(it.items) 856} 857 858func (it *IndexIterator) takeBuf() interface{} { 859 b := it.items 860 it.items = nil 861 return b 862} 863