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 admin 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/datastore/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 newDatastoreAdminClientHook clientHook 42 43// DatastoreAdminCallOptions contains the retry settings for each method of DatastoreAdminClient. 44type DatastoreAdminCallOptions struct { 45 ExportEntities []gax.CallOption 46 ImportEntities []gax.CallOption 47 CreateIndex []gax.CallOption 48 DeleteIndex []gax.CallOption 49 GetIndex []gax.CallOption 50 ListIndexes []gax.CallOption 51} 52 53func defaultDatastoreAdminClientOptions() []option.ClientOption { 54 return []option.ClientOption{ 55 internaloption.WithDefaultEndpoint("datastore.googleapis.com:443"), 56 internaloption.WithDefaultMTLSEndpoint("datastore.mtls.googleapis.com:443"), 57 internaloption.WithDefaultAudience("https://datastore.googleapis.com/"), 58 internaloption.WithDefaultScopes(DefaultAuthScopes()...), 59 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), 60 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 61 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 62 } 63} 64 65func defaultDatastoreAdminCallOptions() *DatastoreAdminCallOptions { 66 return &DatastoreAdminCallOptions{ 67 ExportEntities: []gax.CallOption{}, 68 ImportEntities: []gax.CallOption{}, 69 CreateIndex: []gax.CallOption{}, 70 DeleteIndex: []gax.CallOption{}, 71 GetIndex: []gax.CallOption{ 72 gax.WithRetry(func() gax.Retryer { 73 return gax.OnCodes([]codes.Code{ 74 codes.Unavailable, 75 codes.DeadlineExceeded, 76 }, gax.Backoff{ 77 Initial: 100 * time.Millisecond, 78 Max: 60000 * time.Millisecond, 79 Multiplier: 1.30, 80 }) 81 }), 82 }, 83 ListIndexes: []gax.CallOption{ 84 gax.WithRetry(func() gax.Retryer { 85 return gax.OnCodes([]codes.Code{ 86 codes.Unavailable, 87 codes.DeadlineExceeded, 88 }, gax.Backoff{ 89 Initial: 100 * time.Millisecond, 90 Max: 60000 * time.Millisecond, 91 Multiplier: 1.30, 92 }) 93 }), 94 }, 95 } 96} 97 98// DatastoreAdminClient is a client for interacting with Cloud Datastore API. 99// 100// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 101type DatastoreAdminClient struct { 102 // Connection pool of gRPC connections to the service. 103 connPool gtransport.ConnPool 104 105 // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE 106 disableDeadlines bool 107 108 // The gRPC API client. 109 datastoreAdminClient adminpb.DatastoreAdminClient 110 111 // LROClient is used internally to handle longrunning operations. 112 // It is exposed so that its CallOptions can be modified if required. 113 // Users should not Close this client. 114 LROClient *lroauto.OperationsClient 115 116 // The call options for this service. 117 CallOptions *DatastoreAdminCallOptions 118 119 // The x-goog-* metadata to be sent with each request. 120 xGoogMetadata metadata.MD 121} 122 123// NewDatastoreAdminClient creates a new datastore admin client. 124// 125// Google Cloud Datastore Admin API 126// 127// The Datastore Admin API provides several admin services for Cloud Datastore. 128// 129// ConceptsProject, namespace, kind, and entity as defined in the Google Cloud Datastore 130// API. 131// 132// Operation: An Operation represents work being performed in the background. 133// 134// EntityFilter: Allows specifying a subset of entities in a project. This is 135// specified as a combination of kinds and namespaces (either or both of which 136// may be all). 137// 138// ServicesExport/ImportThe Export/Import service provides the ability to copy all or a subset of 139// entities to/from Google Cloud Storage. 140// 141// Exported data may be imported into Cloud Datastore for any Google Cloud 142// Platform project. It is not restricted to the export source project. It is 143// possible to export from one project and then import into another. 144// 145// Exported data can also be loaded into Google BigQuery for analysis. 146// 147// Exports and imports are performed asynchronously. An Operation resource is 148// created for each export/import. The state (including any errors encountered) 149// of the export/import may be queried via the Operation resource. 150// 151// IndexThe index service manages Cloud Datastore composite indexes. 152// 153// Index creation and deletion are performed asynchronously. 154// An Operation resource is created for each such asynchronous operation. 155// The state of the operation (including any errors encountered) 156// may be queried via the Operation resource. 157// 158// OperationThe Operations collection provides a record of actions performed for the 159// specified project (including any operations in progress). Operations are not 160// created directly but through calls on other collections or resources. 161// 162// An operation that is not yet done may be cancelled. The request to cancel is 163// asynchronous and the operation may continue to run for some time after the 164// request to cancel is made. 165// 166// An operation that is done may be deleted so that it is no longer listed as 167// part of the Operation collection. 168// 169// ListOperations returns all pending operations, but not completed operations. 170// 171// Operations are created by service DatastoreAdmin, 172// but are accessed via service google.longrunning.Operations. 173func NewDatastoreAdminClient(ctx context.Context, opts ...option.ClientOption) (*DatastoreAdminClient, error) { 174 clientOpts := defaultDatastoreAdminClientOptions() 175 176 if newDatastoreAdminClientHook != nil { 177 hookOpts, err := newDatastoreAdminClientHook(ctx, clientHookParams{}) 178 if err != nil { 179 return nil, err 180 } 181 clientOpts = append(clientOpts, hookOpts...) 182 } 183 184 disableDeadlines, err := checkDisableDeadlines() 185 if err != nil { 186 return nil, err 187 } 188 189 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 190 if err != nil { 191 return nil, err 192 } 193 c := &DatastoreAdminClient{ 194 connPool: connPool, 195 disableDeadlines: disableDeadlines, 196 CallOptions: defaultDatastoreAdminCallOptions(), 197 198 datastoreAdminClient: adminpb.NewDatastoreAdminClient(connPool), 199 } 200 c.setGoogleClientInfo() 201 202 c.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool)) 203 if err != nil { 204 // This error "should not happen", since we are just reusing old connection pool 205 // and never actually need to dial. 206 // If this does happen, we could leak connp. However, we cannot close conn: 207 // If the user invoked the constructor with option.WithGRPCConn, 208 // we would close a connection that's still in use. 209 // TODO: investigate error conditions. 210 return nil, err 211 } 212 return c, nil 213} 214 215// Connection returns a connection to the API service. 216// 217// Deprecated. 218func (c *DatastoreAdminClient) Connection() *grpc.ClientConn { 219 return c.connPool.Conn() 220} 221 222// Close closes the connection to the API service. The user should invoke this when 223// the client is no longer required. 224func (c *DatastoreAdminClient) Close() error { 225 return c.connPool.Close() 226} 227 228// setGoogleClientInfo sets the name and version of the application in 229// the `x-goog-api-client` header passed on each request. Intended for 230// use by Google-written clients. 231func (c *DatastoreAdminClient) setGoogleClientInfo(keyval ...string) { 232 kv := append([]string{"gl-go", versionGo()}, keyval...) 233 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 234 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 235} 236 237// ExportEntities exports a copy of all or a subset of entities from Google Cloud Datastore 238// to another storage system, such as Google Cloud Storage. Recent updates to 239// entities may not be reflected in the export. The export occurs in the 240// background and its progress can be monitored and managed via the 241// Operation resource that is created. The output of an export may only be 242// used once the associated operation is done. If an export operation is 243// cancelled before completion it may leave partial data behind in Google 244// Cloud Storage. 245func (c *DatastoreAdminClient) ExportEntities(ctx context.Context, req *adminpb.ExportEntitiesRequest, opts ...gax.CallOption) (*ExportEntitiesOperation, error) { 246 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 247 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 248 defer cancel() 249 ctx = cctx 250 } 251 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "project_id", url.QueryEscape(req.GetProjectId()))) 252 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 253 opts = append(c.CallOptions.ExportEntities[0:len(c.CallOptions.ExportEntities):len(c.CallOptions.ExportEntities)], opts...) 254 var resp *longrunningpb.Operation 255 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 256 var err error 257 resp, err = c.datastoreAdminClient.ExportEntities(ctx, req, settings.GRPC...) 258 return err 259 }, opts...) 260 if err != nil { 261 return nil, err 262 } 263 return &ExportEntitiesOperation{ 264 lro: longrunning.InternalNewOperation(c.LROClient, resp), 265 }, nil 266} 267 268// ImportEntities imports entities into Google Cloud Datastore. Existing entities with the 269// same key are overwritten. The import occurs in the background and its 270// progress can be monitored and managed via the Operation resource that is 271// created. If an ImportEntities operation is cancelled, it is possible 272// that a subset of the data has already been imported to Cloud Datastore. 273func (c *DatastoreAdminClient) ImportEntities(ctx context.Context, req *adminpb.ImportEntitiesRequest, opts ...gax.CallOption) (*ImportEntitiesOperation, error) { 274 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 275 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 276 defer cancel() 277 ctx = cctx 278 } 279 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "project_id", url.QueryEscape(req.GetProjectId()))) 280 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 281 opts = append(c.CallOptions.ImportEntities[0:len(c.CallOptions.ImportEntities):len(c.CallOptions.ImportEntities)], opts...) 282 var resp *longrunningpb.Operation 283 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 284 var err error 285 resp, err = c.datastoreAdminClient.ImportEntities(ctx, req, settings.GRPC...) 286 return err 287 }, opts...) 288 if err != nil { 289 return nil, err 290 } 291 return &ImportEntitiesOperation{ 292 lro: longrunning.InternalNewOperation(c.LROClient, resp), 293 }, nil 294} 295 296// CreateIndex creates the specified index. 297// A newly created index’s initial state is CREATING. On completion of the 298// returned google.longrunning.Operation, the state will be READY. 299// If the index already exists, the call will return an ALREADY_EXISTS 300// status. 301// 302// During index creation, the process could result in an error, in which 303// case the index will move to the ERROR state. The process can be recovered 304// by fixing the data that caused the error, removing the index with 305// delete, then 306// re-creating the index with [create] 307// [google.datastore.admin.v1.DatastoreAdmin.CreateIndex]. 308// 309// Indexes with a single property cannot be created. 310func (c *DatastoreAdminClient) CreateIndex(ctx context.Context, req *adminpb.CreateIndexRequest, opts ...gax.CallOption) (*CreateIndexOperation, error) { 311 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 312 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 313 defer cancel() 314 ctx = cctx 315 } 316 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "project_id", url.QueryEscape(req.GetProjectId()))) 317 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 318 opts = append(c.CallOptions.CreateIndex[0:len(c.CallOptions.CreateIndex):len(c.CallOptions.CreateIndex)], opts...) 319 var resp *longrunningpb.Operation 320 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 321 var err error 322 resp, err = c.datastoreAdminClient.CreateIndex(ctx, req, settings.GRPC...) 323 return err 324 }, opts...) 325 if err != nil { 326 return nil, err 327 } 328 return &CreateIndexOperation{ 329 lro: longrunning.InternalNewOperation(c.LROClient, resp), 330 }, nil 331} 332 333// DeleteIndex deletes an existing index. 334// An index can only be deleted if it is in a READY or ERROR state. On 335// successful execution of the request, the index will be in a DELETING 336// state. And on completion of the 337// returned google.longrunning.Operation, the index will be removed. 338// 339// During index deletion, the process could result in an error, in which 340// case the index will move to the ERROR state. The process can be recovered 341// by fixing the data that caused the error, followed by calling 342// delete again. 343func (c *DatastoreAdminClient) DeleteIndex(ctx context.Context, req *adminpb.DeleteIndexRequest, opts ...gax.CallOption) (*DeleteIndexOperation, 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&%s=%v", "project_id", url.QueryEscape(req.GetProjectId()), "index_id", url.QueryEscape(req.GetIndexId()))) 350 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 351 opts = append(c.CallOptions.DeleteIndex[0:len(c.CallOptions.DeleteIndex):len(c.CallOptions.DeleteIndex)], opts...) 352 var resp *longrunningpb.Operation 353 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 354 var err error 355 resp, err = c.datastoreAdminClient.DeleteIndex(ctx, req, settings.GRPC...) 356 return err 357 }, opts...) 358 if err != nil { 359 return nil, err 360 } 361 return &DeleteIndexOperation{ 362 lro: longrunning.InternalNewOperation(c.LROClient, resp), 363 }, nil 364} 365 366// GetIndex gets an index. 367func (c *DatastoreAdminClient) GetIndex(ctx context.Context, req *adminpb.GetIndexRequest, opts ...gax.CallOption) (*adminpb.Index, error) { 368 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 369 cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) 370 defer cancel() 371 ctx = cctx 372 } 373 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v&%s=%v", "project_id", url.QueryEscape(req.GetProjectId()), "index_id", url.QueryEscape(req.GetIndexId()))) 374 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 375 opts = append(c.CallOptions.GetIndex[0:len(c.CallOptions.GetIndex):len(c.CallOptions.GetIndex)], opts...) 376 var resp *adminpb.Index 377 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 378 var err error 379 resp, err = c.datastoreAdminClient.GetIndex(ctx, req, settings.GRPC...) 380 return err 381 }, opts...) 382 if err != nil { 383 return nil, err 384 } 385 return resp, nil 386} 387 388// ListIndexes lists the indexes that match the specified filters. Datastore uses an 389// eventually consistent query to fetch the list of indexes and may 390// occasionally return stale results. 391func (c *DatastoreAdminClient) ListIndexes(ctx context.Context, req *adminpb.ListIndexesRequest, opts ...gax.CallOption) *IndexIterator { 392 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "project_id", url.QueryEscape(req.GetProjectId()))) 393 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 394 opts = append(c.CallOptions.ListIndexes[0:len(c.CallOptions.ListIndexes):len(c.CallOptions.ListIndexes)], opts...) 395 it := &IndexIterator{} 396 req = proto.Clone(req).(*adminpb.ListIndexesRequest) 397 it.InternalFetch = func(pageSize int, pageToken string) ([]*adminpb.Index, string, error) { 398 var resp *adminpb.ListIndexesResponse 399 req.PageToken = pageToken 400 if pageSize > math.MaxInt32 { 401 req.PageSize = math.MaxInt32 402 } else { 403 req.PageSize = int32(pageSize) 404 } 405 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 406 var err error 407 resp, err = c.datastoreAdminClient.ListIndexes(ctx, req, settings.GRPC...) 408 return err 409 }, opts...) 410 if err != nil { 411 return nil, "", err 412 } 413 414 it.Response = resp 415 return resp.GetIndexes(), resp.GetNextPageToken(), nil 416 } 417 fetch := func(pageSize int, pageToken string) (string, error) { 418 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 419 if err != nil { 420 return "", err 421 } 422 it.items = append(it.items, items...) 423 return nextPageToken, nil 424 } 425 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 426 it.pageInfo.MaxSize = int(req.GetPageSize()) 427 it.pageInfo.Token = req.GetPageToken() 428 return it 429} 430 431// CreateIndexOperation manages a long-running operation from CreateIndex. 432type CreateIndexOperation struct { 433 lro *longrunning.Operation 434} 435 436// CreateIndexOperation returns a new CreateIndexOperation from a given name. 437// The name must be that of a previously created CreateIndexOperation, possibly from a different process. 438func (c *DatastoreAdminClient) CreateIndexOperation(name string) *CreateIndexOperation { 439 return &CreateIndexOperation{ 440 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 441 } 442} 443 444// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 445// 446// See documentation of Poll for error-handling information. 447func (op *CreateIndexOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*adminpb.Index, error) { 448 var resp adminpb.Index 449 if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { 450 return nil, err 451 } 452 return &resp, nil 453} 454 455// Poll fetches the latest state of the long-running operation. 456// 457// Poll also fetches the latest metadata, which can be retrieved by Metadata. 458// 459// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 460// the operation has completed with failure, the error is returned and op.Done will return true. 461// If Poll succeeds and the operation has completed successfully, 462// op.Done will return true, and the response of the operation is returned. 463// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 464func (op *CreateIndexOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*adminpb.Index, error) { 465 var resp adminpb.Index 466 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 467 return nil, err 468 } 469 if !op.Done() { 470 return nil, nil 471 } 472 return &resp, nil 473} 474 475// Metadata returns metadata associated with the long-running operation. 476// Metadata itself does not contact the server, but Poll does. 477// To get the latest metadata, call this method after a successful call to Poll. 478// If the metadata is not available, the returned metadata and error are both nil. 479func (op *CreateIndexOperation) Metadata() (*adminpb.IndexOperationMetadata, error) { 480 var meta adminpb.IndexOperationMetadata 481 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 482 return nil, nil 483 } else if err != nil { 484 return nil, err 485 } 486 return &meta, nil 487} 488 489// Done reports whether the long-running operation has completed. 490func (op *CreateIndexOperation) Done() bool { 491 return op.lro.Done() 492} 493 494// Name returns the name of the long-running operation. 495// The name is assigned by the server and is unique within the service from which the operation is created. 496func (op *CreateIndexOperation) Name() string { 497 return op.lro.Name() 498} 499 500// DeleteIndexOperation manages a long-running operation from DeleteIndex. 501type DeleteIndexOperation struct { 502 lro *longrunning.Operation 503} 504 505// DeleteIndexOperation returns a new DeleteIndexOperation from a given name. 506// The name must be that of a previously created DeleteIndexOperation, possibly from a different process. 507func (c *DatastoreAdminClient) DeleteIndexOperation(name string) *DeleteIndexOperation { 508 return &DeleteIndexOperation{ 509 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 510 } 511} 512 513// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 514// 515// See documentation of Poll for error-handling information. 516func (op *DeleteIndexOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*adminpb.Index, error) { 517 var resp adminpb.Index 518 if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { 519 return nil, err 520 } 521 return &resp, nil 522} 523 524// Poll fetches the latest state of the long-running operation. 525// 526// Poll also fetches the latest metadata, which can be retrieved by Metadata. 527// 528// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 529// the operation has completed with failure, the error is returned and op.Done will return true. 530// If Poll succeeds and the operation has completed successfully, 531// op.Done will return true, and the response of the operation is returned. 532// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 533func (op *DeleteIndexOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*adminpb.Index, error) { 534 var resp adminpb.Index 535 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 536 return nil, err 537 } 538 if !op.Done() { 539 return nil, nil 540 } 541 return &resp, nil 542} 543 544// Metadata returns metadata associated with the long-running operation. 545// Metadata itself does not contact the server, but Poll does. 546// To get the latest metadata, call this method after a successful call to Poll. 547// If the metadata is not available, the returned metadata and error are both nil. 548func (op *DeleteIndexOperation) Metadata() (*adminpb.IndexOperationMetadata, error) { 549 var meta adminpb.IndexOperationMetadata 550 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 551 return nil, nil 552 } else if err != nil { 553 return nil, err 554 } 555 return &meta, nil 556} 557 558// Done reports whether the long-running operation has completed. 559func (op *DeleteIndexOperation) Done() bool { 560 return op.lro.Done() 561} 562 563// Name returns the name of the long-running operation. 564// The name is assigned by the server and is unique within the service from which the operation is created. 565func (op *DeleteIndexOperation) Name() string { 566 return op.lro.Name() 567} 568 569// ExportEntitiesOperation manages a long-running operation from ExportEntities. 570type ExportEntitiesOperation struct { 571 lro *longrunning.Operation 572} 573 574// ExportEntitiesOperation returns a new ExportEntitiesOperation from a given name. 575// The name must be that of a previously created ExportEntitiesOperation, possibly from a different process. 576func (c *DatastoreAdminClient) ExportEntitiesOperation(name string) *ExportEntitiesOperation { 577 return &ExportEntitiesOperation{ 578 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 579 } 580} 581 582// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 583// 584// See documentation of Poll for error-handling information. 585func (op *ExportEntitiesOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*adminpb.ExportEntitiesResponse, error) { 586 var resp adminpb.ExportEntitiesResponse 587 if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { 588 return nil, err 589 } 590 return &resp, nil 591} 592 593// Poll fetches the latest state of the long-running operation. 594// 595// Poll also fetches the latest metadata, which can be retrieved by Metadata. 596// 597// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 598// the operation has completed with failure, the error is returned and op.Done will return true. 599// If Poll succeeds and the operation has completed successfully, 600// op.Done will return true, and the response of the operation is returned. 601// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 602func (op *ExportEntitiesOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*adminpb.ExportEntitiesResponse, error) { 603 var resp adminpb.ExportEntitiesResponse 604 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 605 return nil, err 606 } 607 if !op.Done() { 608 return nil, nil 609 } 610 return &resp, nil 611} 612 613// Metadata returns metadata associated with the long-running operation. 614// Metadata itself does not contact the server, but Poll does. 615// To get the latest metadata, call this method after a successful call to Poll. 616// If the metadata is not available, the returned metadata and error are both nil. 617func (op *ExportEntitiesOperation) Metadata() (*adminpb.ExportEntitiesMetadata, error) { 618 var meta adminpb.ExportEntitiesMetadata 619 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 620 return nil, nil 621 } else if err != nil { 622 return nil, err 623 } 624 return &meta, nil 625} 626 627// Done reports whether the long-running operation has completed. 628func (op *ExportEntitiesOperation) Done() bool { 629 return op.lro.Done() 630} 631 632// Name returns the name of the long-running operation. 633// The name is assigned by the server and is unique within the service from which the operation is created. 634func (op *ExportEntitiesOperation) Name() string { 635 return op.lro.Name() 636} 637 638// ImportEntitiesOperation manages a long-running operation from ImportEntities. 639type ImportEntitiesOperation struct { 640 lro *longrunning.Operation 641} 642 643// ImportEntitiesOperation returns a new ImportEntitiesOperation from a given name. 644// The name must be that of a previously created ImportEntitiesOperation, possibly from a different process. 645func (c *DatastoreAdminClient) ImportEntitiesOperation(name string) *ImportEntitiesOperation { 646 return &ImportEntitiesOperation{ 647 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 648 } 649} 650 651// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 652// 653// See documentation of Poll for error-handling information. 654func (op *ImportEntitiesOperation) Wait(ctx context.Context, opts ...gax.CallOption) error { 655 return op.lro.WaitWithInterval(ctx, nil, time.Minute, opts...) 656} 657 658// Poll fetches the latest state of the long-running operation. 659// 660// Poll also fetches the latest metadata, which can be retrieved by Metadata. 661// 662// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 663// the operation has completed with failure, the error is returned and op.Done will return true. 664// If Poll succeeds and the operation has completed successfully, 665// op.Done will return true, and the response of the operation is returned. 666// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 667func (op *ImportEntitiesOperation) Poll(ctx context.Context, opts ...gax.CallOption) error { 668 return op.lro.Poll(ctx, nil, opts...) 669} 670 671// Metadata returns metadata associated with the long-running operation. 672// Metadata itself does not contact the server, but Poll does. 673// To get the latest metadata, call this method after a successful call to Poll. 674// If the metadata is not available, the returned metadata and error are both nil. 675func (op *ImportEntitiesOperation) Metadata() (*adminpb.ImportEntitiesMetadata, error) { 676 var meta adminpb.ImportEntitiesMetadata 677 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 678 return nil, nil 679 } else if err != nil { 680 return nil, err 681 } 682 return &meta, nil 683} 684 685// Done reports whether the long-running operation has completed. 686func (op *ImportEntitiesOperation) Done() bool { 687 return op.lro.Done() 688} 689 690// Name returns the name of the long-running operation. 691// The name is assigned by the server and is unique within the service from which the operation is created. 692func (op *ImportEntitiesOperation) Name() string { 693 return op.lro.Name() 694} 695 696// IndexIterator manages a stream of *adminpb.Index. 697type IndexIterator struct { 698 items []*adminpb.Index 699 pageInfo *iterator.PageInfo 700 nextFunc func() error 701 702 // Response is the raw response for the current page. 703 // It must be cast to the RPC response type. 704 // Calling Next() or InternalFetch() updates this value. 705 Response interface{} 706 707 // InternalFetch is for use by the Google Cloud Libraries only. 708 // It is not part of the stable interface of this package. 709 // 710 // InternalFetch returns results from a single call to the underlying RPC. 711 // The number of results is no greater than pageSize. 712 // If there are no more results, nextPageToken is empty and err is nil. 713 InternalFetch func(pageSize int, pageToken string) (results []*adminpb.Index, nextPageToken string, err error) 714} 715 716// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 717func (it *IndexIterator) PageInfo() *iterator.PageInfo { 718 return it.pageInfo 719} 720 721// Next returns the next result. Its second return value is iterator.Done if there are no more 722// results. Once Next returns Done, all subsequent calls will return Done. 723func (it *IndexIterator) Next() (*adminpb.Index, error) { 724 var item *adminpb.Index 725 if err := it.nextFunc(); err != nil { 726 return item, err 727 } 728 item = it.items[0] 729 it.items = it.items[1:] 730 return item, nil 731} 732 733func (it *IndexIterator) bufLen() int { 734 return len(it.items) 735} 736 737func (it *IndexIterator) takeBuf() interface{} { 738 b := it.items 739 it.items = nil 740 return b 741} 742