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