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 database
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	iampb "google.golang.org/genproto/googleapis/iam/v1"
34	longrunningpb "google.golang.org/genproto/googleapis/longrunning"
35	databasepb "google.golang.org/genproto/googleapis/spanner/admin/database/v1"
36	"google.golang.org/grpc"
37	"google.golang.org/grpc/codes"
38	"google.golang.org/grpc/metadata"
39	"google.golang.org/protobuf/proto"
40)
41
42var newDatabaseAdminClientHook clientHook
43
44// DatabaseAdminCallOptions contains the retry settings for each method of DatabaseAdminClient.
45type DatabaseAdminCallOptions struct {
46	ListDatabases          []gax.CallOption
47	CreateDatabase         []gax.CallOption
48	GetDatabase            []gax.CallOption
49	UpdateDatabaseDdl      []gax.CallOption
50	DropDatabase           []gax.CallOption
51	GetDatabaseDdl         []gax.CallOption
52	SetIamPolicy           []gax.CallOption
53	GetIamPolicy           []gax.CallOption
54	TestIamPermissions     []gax.CallOption
55	CreateBackup           []gax.CallOption
56	GetBackup              []gax.CallOption
57	UpdateBackup           []gax.CallOption
58	DeleteBackup           []gax.CallOption
59	ListBackups            []gax.CallOption
60	RestoreDatabase        []gax.CallOption
61	ListDatabaseOperations []gax.CallOption
62	ListBackupOperations   []gax.CallOption
63}
64
65func defaultDatabaseAdminGRPCClientOptions() []option.ClientOption {
66	return []option.ClientOption{
67		internaloption.WithDefaultEndpoint("spanner.googleapis.com:443"),
68		internaloption.WithDefaultMTLSEndpoint("spanner.mtls.googleapis.com:443"),
69		internaloption.WithDefaultAudience("https://spanner.googleapis.com/"),
70		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
71		internaloption.EnableJwtWithScope(),
72		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
73		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
74			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
75	}
76}
77
78func defaultDatabaseAdminCallOptions() *DatabaseAdminCallOptions {
79	return &DatabaseAdminCallOptions{
80		ListDatabases: []gax.CallOption{
81			gax.WithRetry(func() gax.Retryer {
82				return gax.OnCodes([]codes.Code{
83					codes.Unavailable,
84					codes.DeadlineExceeded,
85				}, gax.Backoff{
86					Initial:    1000 * time.Millisecond,
87					Max:        32000 * time.Millisecond,
88					Multiplier: 1.30,
89				})
90			}),
91		},
92		CreateDatabase: []gax.CallOption{},
93		GetDatabase: []gax.CallOption{
94			gax.WithRetry(func() gax.Retryer {
95				return gax.OnCodes([]codes.Code{
96					codes.Unavailable,
97					codes.DeadlineExceeded,
98				}, gax.Backoff{
99					Initial:    1000 * time.Millisecond,
100					Max:        32000 * time.Millisecond,
101					Multiplier: 1.30,
102				})
103			}),
104		},
105		UpdateDatabaseDdl: []gax.CallOption{
106			gax.WithRetry(func() gax.Retryer {
107				return gax.OnCodes([]codes.Code{
108					codes.Unavailable,
109					codes.DeadlineExceeded,
110				}, gax.Backoff{
111					Initial:    1000 * time.Millisecond,
112					Max:        32000 * time.Millisecond,
113					Multiplier: 1.30,
114				})
115			}),
116		},
117		DropDatabase: []gax.CallOption{
118			gax.WithRetry(func() gax.Retryer {
119				return gax.OnCodes([]codes.Code{
120					codes.Unavailable,
121					codes.DeadlineExceeded,
122				}, gax.Backoff{
123					Initial:    1000 * time.Millisecond,
124					Max:        32000 * time.Millisecond,
125					Multiplier: 1.30,
126				})
127			}),
128		},
129		GetDatabaseDdl: []gax.CallOption{
130			gax.WithRetry(func() gax.Retryer {
131				return gax.OnCodes([]codes.Code{
132					codes.Unavailable,
133					codes.DeadlineExceeded,
134				}, gax.Backoff{
135					Initial:    1000 * time.Millisecond,
136					Max:        32000 * time.Millisecond,
137					Multiplier: 1.30,
138				})
139			}),
140		},
141		SetIamPolicy: []gax.CallOption{},
142		GetIamPolicy: []gax.CallOption{
143			gax.WithRetry(func() gax.Retryer {
144				return gax.OnCodes([]codes.Code{
145					codes.Unavailable,
146					codes.DeadlineExceeded,
147				}, gax.Backoff{
148					Initial:    1000 * time.Millisecond,
149					Max:        32000 * time.Millisecond,
150					Multiplier: 1.30,
151				})
152			}),
153		},
154		TestIamPermissions: []gax.CallOption{},
155		CreateBackup:       []gax.CallOption{},
156		GetBackup: []gax.CallOption{
157			gax.WithRetry(func() gax.Retryer {
158				return gax.OnCodes([]codes.Code{
159					codes.Unavailable,
160					codes.DeadlineExceeded,
161				}, gax.Backoff{
162					Initial:    1000 * time.Millisecond,
163					Max:        32000 * time.Millisecond,
164					Multiplier: 1.30,
165				})
166			}),
167		},
168		UpdateBackup: []gax.CallOption{
169			gax.WithRetry(func() gax.Retryer {
170				return gax.OnCodes([]codes.Code{
171					codes.Unavailable,
172					codes.DeadlineExceeded,
173				}, gax.Backoff{
174					Initial:    1000 * time.Millisecond,
175					Max:        32000 * time.Millisecond,
176					Multiplier: 1.30,
177				})
178			}),
179		},
180		DeleteBackup: []gax.CallOption{
181			gax.WithRetry(func() gax.Retryer {
182				return gax.OnCodes([]codes.Code{
183					codes.Unavailable,
184					codes.DeadlineExceeded,
185				}, gax.Backoff{
186					Initial:    1000 * time.Millisecond,
187					Max:        32000 * time.Millisecond,
188					Multiplier: 1.30,
189				})
190			}),
191		},
192		ListBackups: []gax.CallOption{
193			gax.WithRetry(func() gax.Retryer {
194				return gax.OnCodes([]codes.Code{
195					codes.Unavailable,
196					codes.DeadlineExceeded,
197				}, gax.Backoff{
198					Initial:    1000 * time.Millisecond,
199					Max:        32000 * time.Millisecond,
200					Multiplier: 1.30,
201				})
202			}),
203		},
204		RestoreDatabase: []gax.CallOption{},
205		ListDatabaseOperations: []gax.CallOption{
206			gax.WithRetry(func() gax.Retryer {
207				return gax.OnCodes([]codes.Code{
208					codes.Unavailable,
209					codes.DeadlineExceeded,
210				}, gax.Backoff{
211					Initial:    1000 * time.Millisecond,
212					Max:        32000 * time.Millisecond,
213					Multiplier: 1.30,
214				})
215			}),
216		},
217		ListBackupOperations: []gax.CallOption{
218			gax.WithRetry(func() gax.Retryer {
219				return gax.OnCodes([]codes.Code{
220					codes.Unavailable,
221					codes.DeadlineExceeded,
222				}, gax.Backoff{
223					Initial:    1000 * time.Millisecond,
224					Max:        32000 * time.Millisecond,
225					Multiplier: 1.30,
226				})
227			}),
228		},
229	}
230}
231
232// internalDatabaseAdminClient is an interface that defines the methods availaible from Cloud Spanner Database Admin API.
233type internalDatabaseAdminClient interface {
234	Close() error
235	setGoogleClientInfo(...string)
236	Connection() *grpc.ClientConn
237	ListDatabases(context.Context, *databasepb.ListDatabasesRequest, ...gax.CallOption) *DatabaseIterator
238	CreateDatabase(context.Context, *databasepb.CreateDatabaseRequest, ...gax.CallOption) (*CreateDatabaseOperation, error)
239	CreateDatabaseOperation(name string) *CreateDatabaseOperation
240	GetDatabase(context.Context, *databasepb.GetDatabaseRequest, ...gax.CallOption) (*databasepb.Database, error)
241	UpdateDatabaseDdl(context.Context, *databasepb.UpdateDatabaseDdlRequest, ...gax.CallOption) (*UpdateDatabaseDdlOperation, error)
242	UpdateDatabaseDdlOperation(name string) *UpdateDatabaseDdlOperation
243	DropDatabase(context.Context, *databasepb.DropDatabaseRequest, ...gax.CallOption) error
244	GetDatabaseDdl(context.Context, *databasepb.GetDatabaseDdlRequest, ...gax.CallOption) (*databasepb.GetDatabaseDdlResponse, error)
245	SetIamPolicy(context.Context, *iampb.SetIamPolicyRequest, ...gax.CallOption) (*iampb.Policy, error)
246	GetIamPolicy(context.Context, *iampb.GetIamPolicyRequest, ...gax.CallOption) (*iampb.Policy, error)
247	TestIamPermissions(context.Context, *iampb.TestIamPermissionsRequest, ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error)
248	CreateBackup(context.Context, *databasepb.CreateBackupRequest, ...gax.CallOption) (*CreateBackupOperation, error)
249	CreateBackupOperation(name string) *CreateBackupOperation
250	GetBackup(context.Context, *databasepb.GetBackupRequest, ...gax.CallOption) (*databasepb.Backup, error)
251	UpdateBackup(context.Context, *databasepb.UpdateBackupRequest, ...gax.CallOption) (*databasepb.Backup, error)
252	DeleteBackup(context.Context, *databasepb.DeleteBackupRequest, ...gax.CallOption) error
253	ListBackups(context.Context, *databasepb.ListBackupsRequest, ...gax.CallOption) *BackupIterator
254	RestoreDatabase(context.Context, *databasepb.RestoreDatabaseRequest, ...gax.CallOption) (*RestoreDatabaseOperation, error)
255	RestoreDatabaseOperation(name string) *RestoreDatabaseOperation
256	ListDatabaseOperations(context.Context, *databasepb.ListDatabaseOperationsRequest, ...gax.CallOption) *OperationIterator
257	ListBackupOperations(context.Context, *databasepb.ListBackupOperationsRequest, ...gax.CallOption) *OperationIterator
258}
259
260// DatabaseAdminClient is a client for interacting with Cloud Spanner Database Admin API.
261// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
262//
263// Cloud Spanner Database Admin API
264//
265// The Cloud Spanner Database Admin API can be used to create, drop, and
266// list databases. It also enables updating the schema of pre-existing
267// databases. It can be also used to create, delete and list backups for a
268// database and to restore from an existing backup.
269type DatabaseAdminClient struct {
270	// The internal transport-dependent client.
271	internalClient internalDatabaseAdminClient
272
273	// The call options for this service.
274	CallOptions *DatabaseAdminCallOptions
275
276	// LROClient is used internally to handle long-running operations.
277	// It is exposed so that its CallOptions can be modified if required.
278	// Users should not Close this client.
279	LROClient *lroauto.OperationsClient
280}
281
282// Wrapper methods routed to the internal client.
283
284// Close closes the connection to the API service. The user should invoke this when
285// the client is no longer required.
286func (c *DatabaseAdminClient) Close() error {
287	return c.internalClient.Close()
288}
289
290// setGoogleClientInfo sets the name and version of the application in
291// the `x-goog-api-client` header passed on each request. Intended for
292// use by Google-written clients.
293func (c *DatabaseAdminClient) setGoogleClientInfo(keyval ...string) {
294	c.internalClient.setGoogleClientInfo(keyval...)
295}
296
297// Connection returns a connection to the API service.
298//
299// Deprecated.
300func (c *DatabaseAdminClient) Connection() *grpc.ClientConn {
301	return c.internalClient.Connection()
302}
303
304// ListDatabases lists Cloud Spanner databases.
305func (c *DatabaseAdminClient) ListDatabases(ctx context.Context, req *databasepb.ListDatabasesRequest, opts ...gax.CallOption) *DatabaseIterator {
306	return c.internalClient.ListDatabases(ctx, req, opts...)
307}
308
309// CreateDatabase creates a new Cloud Spanner database and starts to prepare it for serving.
310// The returned [long-running operation][google.longrunning.Operation] will
311// have a name of the format <database_name>/operations/<operation_id> and
312// can be used to track preparation of the database. The
313// metadata field type is
314// CreateDatabaseMetadata. The
315// response field type is
316// Database, if successful.
317func (c *DatabaseAdminClient) CreateDatabase(ctx context.Context, req *databasepb.CreateDatabaseRequest, opts ...gax.CallOption) (*CreateDatabaseOperation, error) {
318	return c.internalClient.CreateDatabase(ctx, req, opts...)
319}
320
321// CreateDatabaseOperation returns a new CreateDatabaseOperation from a given name.
322// The name must be that of a previously created CreateDatabaseOperation, possibly from a different process.
323func (c *DatabaseAdminClient) CreateDatabaseOperation(name string) *CreateDatabaseOperation {
324	return c.internalClient.CreateDatabaseOperation(name)
325}
326
327// GetDatabase gets the state of a Cloud Spanner database.
328func (c *DatabaseAdminClient) GetDatabase(ctx context.Context, req *databasepb.GetDatabaseRequest, opts ...gax.CallOption) (*databasepb.Database, error) {
329	return c.internalClient.GetDatabase(ctx, req, opts...)
330}
331
332// UpdateDatabaseDdl updates the schema of a Cloud Spanner database by
333// creating/altering/dropping tables, columns, indexes, etc. The returned
334// [long-running operation][google.longrunning.Operation] will have a name of
335// the format <database_name>/operations/<operation_id> and can be used to
336// track execution of the schema change(s). The
337// metadata field type is
338// UpdateDatabaseDdlMetadata.  The operation has no response.
339func (c *DatabaseAdminClient) UpdateDatabaseDdl(ctx context.Context, req *databasepb.UpdateDatabaseDdlRequest, opts ...gax.CallOption) (*UpdateDatabaseDdlOperation, error) {
340	return c.internalClient.UpdateDatabaseDdl(ctx, req, opts...)
341}
342
343// UpdateDatabaseDdlOperation returns a new UpdateDatabaseDdlOperation from a given name.
344// The name must be that of a previously created UpdateDatabaseDdlOperation, possibly from a different process.
345func (c *DatabaseAdminClient) UpdateDatabaseDdlOperation(name string) *UpdateDatabaseDdlOperation {
346	return c.internalClient.UpdateDatabaseDdlOperation(name)
347}
348
349// DropDatabase drops (aka deletes) a Cloud Spanner database.
350// Completed backups for the database will be retained according to their
351// expire_time.
352func (c *DatabaseAdminClient) DropDatabase(ctx context.Context, req *databasepb.DropDatabaseRequest, opts ...gax.CallOption) error {
353	return c.internalClient.DropDatabase(ctx, req, opts...)
354}
355
356// GetDatabaseDdl returns the schema of a Cloud Spanner database as a list of formatted
357// DDL statements. This method does not show pending schema updates, those may
358// be queried using the Operations API.
359func (c *DatabaseAdminClient) GetDatabaseDdl(ctx context.Context, req *databasepb.GetDatabaseDdlRequest, opts ...gax.CallOption) (*databasepb.GetDatabaseDdlResponse, error) {
360	return c.internalClient.GetDatabaseDdl(ctx, req, opts...)
361}
362
363// SetIamPolicy sets the access control policy on a database or backup resource.
364// Replaces any existing policy.
365//
366// Authorization requires spanner.databases.setIamPolicy
367// permission on resource.
368// For backups, authorization requires spanner.backups.setIamPolicy
369// permission on resource.
370func (c *DatabaseAdminClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
371	return c.internalClient.SetIamPolicy(ctx, req, opts...)
372}
373
374// GetIamPolicy gets the access control policy for a database or backup resource.
375// Returns an empty policy if a database or backup exists but does not have a
376// policy set.
377//
378// Authorization requires spanner.databases.getIamPolicy permission on
379// resource.
380// For backups, authorization requires spanner.backups.getIamPolicy
381// permission on resource.
382func (c *DatabaseAdminClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
383	return c.internalClient.GetIamPolicy(ctx, req, opts...)
384}
385
386// TestIamPermissions returns permissions that the caller has on the specified database or backup
387// resource.
388//
389// Attempting this RPC on a non-existent Cloud Spanner database will
390// result in a NOT_FOUND error if the user has
391// spanner.databases.list permission on the containing Cloud
392// Spanner instance. Otherwise returns an empty set of permissions.
393// Calling this method on a backup that does not exist will
394// result in a NOT_FOUND error if the user has
395// spanner.backups.list permission on the containing instance.
396func (c *DatabaseAdminClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
397	return c.internalClient.TestIamPermissions(ctx, req, opts...)
398}
399
400// CreateBackup starts creating a new Cloud Spanner Backup.
401// The returned backup [long-running operation][google.longrunning.Operation]
402// will have a name of the format
403// projects/<project>/instances/<instance>/backups/<backup>/operations/<operation_id>
404// and can be used to track creation of the backup. The
405// metadata field type is
406// CreateBackupMetadata. The
407// response field type is
408// Backup, if successful. Cancelling the returned operation will stop the
409// creation and delete the backup.
410// There can be only one pending backup creation per database. Backup creation
411// of different databases can run concurrently.
412func (c *DatabaseAdminClient) CreateBackup(ctx context.Context, req *databasepb.CreateBackupRequest, opts ...gax.CallOption) (*CreateBackupOperation, error) {
413	return c.internalClient.CreateBackup(ctx, req, opts...)
414}
415
416// CreateBackupOperation returns a new CreateBackupOperation from a given name.
417// The name must be that of a previously created CreateBackupOperation, possibly from a different process.
418func (c *DatabaseAdminClient) CreateBackupOperation(name string) *CreateBackupOperation {
419	return c.internalClient.CreateBackupOperation(name)
420}
421
422// GetBackup gets metadata on a pending or completed Backup.
423func (c *DatabaseAdminClient) GetBackup(ctx context.Context, req *databasepb.GetBackupRequest, opts ...gax.CallOption) (*databasepb.Backup, error) {
424	return c.internalClient.GetBackup(ctx, req, opts...)
425}
426
427// UpdateBackup updates a pending or completed Backup.
428func (c *DatabaseAdminClient) UpdateBackup(ctx context.Context, req *databasepb.UpdateBackupRequest, opts ...gax.CallOption) (*databasepb.Backup, error) {
429	return c.internalClient.UpdateBackup(ctx, req, opts...)
430}
431
432// DeleteBackup deletes a pending or completed Backup.
433func (c *DatabaseAdminClient) DeleteBackup(ctx context.Context, req *databasepb.DeleteBackupRequest, opts ...gax.CallOption) error {
434	return c.internalClient.DeleteBackup(ctx, req, opts...)
435}
436
437// ListBackups lists completed and pending backups.
438// Backups returned are ordered by create_time in descending order,
439// starting from the most recent create_time.
440func (c *DatabaseAdminClient) ListBackups(ctx context.Context, req *databasepb.ListBackupsRequest, opts ...gax.CallOption) *BackupIterator {
441	return c.internalClient.ListBackups(ctx, req, opts...)
442}
443
444// RestoreDatabase create a new database by restoring from a completed backup. The new
445// database must be in the same project and in an instance with the same
446// instance configuration as the instance containing
447// the backup. The returned database [long-running
448// operation][google.longrunning.Operation] has a name of the format
449// projects/<project>/instances/<instance>/databases/<database>/operations/<operation_id>,
450// and can be used to track the progress of the operation, and to cancel it.
451// The metadata field type is
452// RestoreDatabaseMetadata.
453// The response type
454// is Database, if
455// successful. Cancelling the returned operation will stop the restore and
456// delete the database.
457// There can be only one database being restored into an instance at a time.
458// Once the restore operation completes, a new restore operation can be
459// initiated, without waiting for the optimize operation associated with the
460// first restore to complete.
461func (c *DatabaseAdminClient) RestoreDatabase(ctx context.Context, req *databasepb.RestoreDatabaseRequest, opts ...gax.CallOption) (*RestoreDatabaseOperation, error) {
462	return c.internalClient.RestoreDatabase(ctx, req, opts...)
463}
464
465// RestoreDatabaseOperation returns a new RestoreDatabaseOperation from a given name.
466// The name must be that of a previously created RestoreDatabaseOperation, possibly from a different process.
467func (c *DatabaseAdminClient) RestoreDatabaseOperation(name string) *RestoreDatabaseOperation {
468	return c.internalClient.RestoreDatabaseOperation(name)
469}
470
471// ListDatabaseOperations lists database [longrunning-operations][google.longrunning.Operation].
472// A database operation has a name of the form
473// projects/<project>/instances/<instance>/databases/<database>/operations/<operation>.
474// The long-running operation
475// metadata field type
476// metadata.type_url describes the type of the metadata. Operations returned
477// include those that have completed/failed/canceled within the last 7 days,
478// and pending operations.
479func (c *DatabaseAdminClient) ListDatabaseOperations(ctx context.Context, req *databasepb.ListDatabaseOperationsRequest, opts ...gax.CallOption) *OperationIterator {
480	return c.internalClient.ListDatabaseOperations(ctx, req, opts...)
481}
482
483// ListBackupOperations lists the backup [long-running operations][google.longrunning.Operation] in
484// the given instance. A backup operation has a name of the form
485// projects/<project>/instances/<instance>/backups/<backup>/operations/<operation>.
486// The long-running operation
487// metadata field type
488// metadata.type_url describes the type of the metadata. Operations returned
489// include those that have completed/failed/canceled within the last 7 days,
490// and pending operations. Operations returned are ordered by
491// operation.metadata.value.progress.start_time in descending order starting
492// from the most recently started operation.
493func (c *DatabaseAdminClient) ListBackupOperations(ctx context.Context, req *databasepb.ListBackupOperationsRequest, opts ...gax.CallOption) *OperationIterator {
494	return c.internalClient.ListBackupOperations(ctx, req, opts...)
495}
496
497// databaseAdminGRPCClient is a client for interacting with Cloud Spanner Database Admin API over gRPC transport.
498//
499// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
500type databaseAdminGRPCClient struct {
501	// Connection pool of gRPC connections to the service.
502	connPool gtransport.ConnPool
503
504	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
505	disableDeadlines bool
506
507	// Points back to the CallOptions field of the containing DatabaseAdminClient
508	CallOptions **DatabaseAdminCallOptions
509
510	// The gRPC API client.
511	databaseAdminClient databasepb.DatabaseAdminClient
512
513	// LROClient is used internally to handle long-running operations.
514	// It is exposed so that its CallOptions can be modified if required.
515	// Users should not Close this client.
516	LROClient **lroauto.OperationsClient
517
518	// The x-goog-* metadata to be sent with each request.
519	xGoogMetadata metadata.MD
520}
521
522// NewDatabaseAdminClient creates a new database admin client based on gRPC.
523// The returned client must be Closed when it is done being used to clean up its underlying connections.
524//
525// Cloud Spanner Database Admin API
526//
527// The Cloud Spanner Database Admin API can be used to create, drop, and
528// list databases. It also enables updating the schema of pre-existing
529// databases. It can be also used to create, delete and list backups for a
530// database and to restore from an existing backup.
531func NewDatabaseAdminClient(ctx context.Context, opts ...option.ClientOption) (*DatabaseAdminClient, error) {
532	clientOpts := defaultDatabaseAdminGRPCClientOptions()
533	if newDatabaseAdminClientHook != nil {
534		hookOpts, err := newDatabaseAdminClientHook(ctx, clientHookParams{})
535		if err != nil {
536			return nil, err
537		}
538		clientOpts = append(clientOpts, hookOpts...)
539	}
540
541	disableDeadlines, err := checkDisableDeadlines()
542	if err != nil {
543		return nil, err
544	}
545
546	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
547	if err != nil {
548		return nil, err
549	}
550	client := DatabaseAdminClient{CallOptions: defaultDatabaseAdminCallOptions()}
551
552	c := &databaseAdminGRPCClient{
553		connPool:            connPool,
554		disableDeadlines:    disableDeadlines,
555		databaseAdminClient: databasepb.NewDatabaseAdminClient(connPool),
556		CallOptions:         &client.CallOptions,
557	}
558	c.setGoogleClientInfo()
559
560	client.internalClient = c
561
562	client.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool))
563	if err != nil {
564		// This error "should not happen", since we are just reusing old connection pool
565		// and never actually need to dial.
566		// If this does happen, we could leak connp. However, we cannot close conn:
567		// If the user invoked the constructor with option.WithGRPCConn,
568		// we would close a connection that's still in use.
569		// TODO: investigate error conditions.
570		return nil, err
571	}
572	c.LROClient = &client.LROClient
573	return &client, nil
574}
575
576// Connection returns a connection to the API service.
577//
578// Deprecated.
579func (c *databaseAdminGRPCClient) Connection() *grpc.ClientConn {
580	return c.connPool.Conn()
581}
582
583// setGoogleClientInfo sets the name and version of the application in
584// the `x-goog-api-client` header passed on each request. Intended for
585// use by Google-written clients.
586func (c *databaseAdminGRPCClient) setGoogleClientInfo(keyval ...string) {
587	kv := append([]string{"gl-go", versionGo()}, keyval...)
588	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
589	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
590}
591
592// Close closes the connection to the API service. The user should invoke this when
593// the client is no longer required.
594func (c *databaseAdminGRPCClient) Close() error {
595	return c.connPool.Close()
596}
597
598func (c *databaseAdminGRPCClient) ListDatabases(ctx context.Context, req *databasepb.ListDatabasesRequest, opts ...gax.CallOption) *DatabaseIterator {
599	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
600	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
601	opts = append((*c.CallOptions).ListDatabases[0:len((*c.CallOptions).ListDatabases):len((*c.CallOptions).ListDatabases)], opts...)
602	it := &DatabaseIterator{}
603	req = proto.Clone(req).(*databasepb.ListDatabasesRequest)
604	it.InternalFetch = func(pageSize int, pageToken string) ([]*databasepb.Database, string, error) {
605		resp := &databasepb.ListDatabasesResponse{}
606		if pageToken != "" {
607			req.PageToken = pageToken
608		}
609		if pageSize > math.MaxInt32 {
610			req.PageSize = math.MaxInt32
611		} else if pageSize != 0 {
612			req.PageSize = int32(pageSize)
613		}
614		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
615			var err error
616			resp, err = c.databaseAdminClient.ListDatabases(ctx, req, settings.GRPC...)
617			return err
618		}, opts...)
619		if err != nil {
620			return nil, "", err
621		}
622
623		it.Response = resp
624		return resp.GetDatabases(), resp.GetNextPageToken(), nil
625	}
626	fetch := func(pageSize int, pageToken string) (string, error) {
627		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
628		if err != nil {
629			return "", err
630		}
631		it.items = append(it.items, items...)
632		return nextPageToken, nil
633	}
634
635	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
636	it.pageInfo.MaxSize = int(req.GetPageSize())
637	it.pageInfo.Token = req.GetPageToken()
638
639	return it
640}
641
642func (c *databaseAdminGRPCClient) CreateDatabase(ctx context.Context, req *databasepb.CreateDatabaseRequest, opts ...gax.CallOption) (*CreateDatabaseOperation, error) {
643	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
644		cctx, cancel := context.WithTimeout(ctx, 3600000*time.Millisecond)
645		defer cancel()
646		ctx = cctx
647	}
648	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
649	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
650	opts = append((*c.CallOptions).CreateDatabase[0:len((*c.CallOptions).CreateDatabase):len((*c.CallOptions).CreateDatabase)], opts...)
651	var resp *longrunningpb.Operation
652	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
653		var err error
654		resp, err = c.databaseAdminClient.CreateDatabase(ctx, req, settings.GRPC...)
655		return err
656	}, opts...)
657	if err != nil {
658		return nil, err
659	}
660	return &CreateDatabaseOperation{
661		lro: longrunning.InternalNewOperation(*c.LROClient, resp),
662	}, nil
663}
664
665func (c *databaseAdminGRPCClient) GetDatabase(ctx context.Context, req *databasepb.GetDatabaseRequest, opts ...gax.CallOption) (*databasepb.Database, error) {
666	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
667		cctx, cancel := context.WithTimeout(ctx, 3600000*time.Millisecond)
668		defer cancel()
669		ctx = cctx
670	}
671	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
672	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
673	opts = append((*c.CallOptions).GetDatabase[0:len((*c.CallOptions).GetDatabase):len((*c.CallOptions).GetDatabase)], opts...)
674	var resp *databasepb.Database
675	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
676		var err error
677		resp, err = c.databaseAdminClient.GetDatabase(ctx, req, settings.GRPC...)
678		return err
679	}, opts...)
680	if err != nil {
681		return nil, err
682	}
683	return resp, nil
684}
685
686func (c *databaseAdminGRPCClient) UpdateDatabaseDdl(ctx context.Context, req *databasepb.UpdateDatabaseDdlRequest, opts ...gax.CallOption) (*UpdateDatabaseDdlOperation, error) {
687	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
688		cctx, cancel := context.WithTimeout(ctx, 3600000*time.Millisecond)
689		defer cancel()
690		ctx = cctx
691	}
692	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "database", url.QueryEscape(req.GetDatabase())))
693	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
694	opts = append((*c.CallOptions).UpdateDatabaseDdl[0:len((*c.CallOptions).UpdateDatabaseDdl):len((*c.CallOptions).UpdateDatabaseDdl)], opts...)
695	var resp *longrunningpb.Operation
696	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
697		var err error
698		resp, err = c.databaseAdminClient.UpdateDatabaseDdl(ctx, req, settings.GRPC...)
699		return err
700	}, opts...)
701	if err != nil {
702		return nil, err
703	}
704	return &UpdateDatabaseDdlOperation{
705		lro: longrunning.InternalNewOperation(*c.LROClient, resp),
706	}, nil
707}
708
709func (c *databaseAdminGRPCClient) DropDatabase(ctx context.Context, req *databasepb.DropDatabaseRequest, opts ...gax.CallOption) error {
710	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
711		cctx, cancel := context.WithTimeout(ctx, 3600000*time.Millisecond)
712		defer cancel()
713		ctx = cctx
714	}
715	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "database", url.QueryEscape(req.GetDatabase())))
716	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
717	opts = append((*c.CallOptions).DropDatabase[0:len((*c.CallOptions).DropDatabase):len((*c.CallOptions).DropDatabase)], opts...)
718	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
719		var err error
720		_, err = c.databaseAdminClient.DropDatabase(ctx, req, settings.GRPC...)
721		return err
722	}, opts...)
723	return err
724}
725
726func (c *databaseAdminGRPCClient) GetDatabaseDdl(ctx context.Context, req *databasepb.GetDatabaseDdlRequest, opts ...gax.CallOption) (*databasepb.GetDatabaseDdlResponse, error) {
727	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
728		cctx, cancel := context.WithTimeout(ctx, 3600000*time.Millisecond)
729		defer cancel()
730		ctx = cctx
731	}
732	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "database", url.QueryEscape(req.GetDatabase())))
733	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
734	opts = append((*c.CallOptions).GetDatabaseDdl[0:len((*c.CallOptions).GetDatabaseDdl):len((*c.CallOptions).GetDatabaseDdl)], opts...)
735	var resp *databasepb.GetDatabaseDdlResponse
736	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
737		var err error
738		resp, err = c.databaseAdminClient.GetDatabaseDdl(ctx, req, settings.GRPC...)
739		return err
740	}, opts...)
741	if err != nil {
742		return nil, err
743	}
744	return resp, nil
745}
746
747func (c *databaseAdminGRPCClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
748	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
749		cctx, cancel := context.WithTimeout(ctx, 30000*time.Millisecond)
750		defer cancel()
751		ctx = cctx
752	}
753	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource())))
754	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
755	opts = append((*c.CallOptions).SetIamPolicy[0:len((*c.CallOptions).SetIamPolicy):len((*c.CallOptions).SetIamPolicy)], opts...)
756	var resp *iampb.Policy
757	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
758		var err error
759		resp, err = c.databaseAdminClient.SetIamPolicy(ctx, req, settings.GRPC...)
760		return err
761	}, opts...)
762	if err != nil {
763		return nil, err
764	}
765	return resp, nil
766}
767
768func (c *databaseAdminGRPCClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
769	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
770		cctx, cancel := context.WithTimeout(ctx, 30000*time.Millisecond)
771		defer cancel()
772		ctx = cctx
773	}
774	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource())))
775	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
776	opts = append((*c.CallOptions).GetIamPolicy[0:len((*c.CallOptions).GetIamPolicy):len((*c.CallOptions).GetIamPolicy)], opts...)
777	var resp *iampb.Policy
778	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
779		var err error
780		resp, err = c.databaseAdminClient.GetIamPolicy(ctx, req, settings.GRPC...)
781		return err
782	}, opts...)
783	if err != nil {
784		return nil, err
785	}
786	return resp, nil
787}
788
789func (c *databaseAdminGRPCClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
790	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
791		cctx, cancel := context.WithTimeout(ctx, 30000*time.Millisecond)
792		defer cancel()
793		ctx = cctx
794	}
795	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource())))
796	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
797	opts = append((*c.CallOptions).TestIamPermissions[0:len((*c.CallOptions).TestIamPermissions):len((*c.CallOptions).TestIamPermissions)], opts...)
798	var resp *iampb.TestIamPermissionsResponse
799	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
800		var err error
801		resp, err = c.databaseAdminClient.TestIamPermissions(ctx, req, settings.GRPC...)
802		return err
803	}, opts...)
804	if err != nil {
805		return nil, err
806	}
807	return resp, nil
808}
809
810func (c *databaseAdminGRPCClient) CreateBackup(ctx context.Context, req *databasepb.CreateBackupRequest, opts ...gax.CallOption) (*CreateBackupOperation, error) {
811	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
812		cctx, cancel := context.WithTimeout(ctx, 3600000*time.Millisecond)
813		defer cancel()
814		ctx = cctx
815	}
816	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
817	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
818	opts = append((*c.CallOptions).CreateBackup[0:len((*c.CallOptions).CreateBackup):len((*c.CallOptions).CreateBackup)], opts...)
819	var resp *longrunningpb.Operation
820	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
821		var err error
822		resp, err = c.databaseAdminClient.CreateBackup(ctx, req, settings.GRPC...)
823		return err
824	}, opts...)
825	if err != nil {
826		return nil, err
827	}
828	return &CreateBackupOperation{
829		lro: longrunning.InternalNewOperation(*c.LROClient, resp),
830	}, nil
831}
832
833func (c *databaseAdminGRPCClient) GetBackup(ctx context.Context, req *databasepb.GetBackupRequest, opts ...gax.CallOption) (*databasepb.Backup, error) {
834	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
835		cctx, cancel := context.WithTimeout(ctx, 3600000*time.Millisecond)
836		defer cancel()
837		ctx = cctx
838	}
839	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
840	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
841	opts = append((*c.CallOptions).GetBackup[0:len((*c.CallOptions).GetBackup):len((*c.CallOptions).GetBackup)], opts...)
842	var resp *databasepb.Backup
843	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
844		var err error
845		resp, err = c.databaseAdminClient.GetBackup(ctx, req, settings.GRPC...)
846		return err
847	}, opts...)
848	if err != nil {
849		return nil, err
850	}
851	return resp, nil
852}
853
854func (c *databaseAdminGRPCClient) UpdateBackup(ctx context.Context, req *databasepb.UpdateBackupRequest, opts ...gax.CallOption) (*databasepb.Backup, error) {
855	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
856		cctx, cancel := context.WithTimeout(ctx, 3600000*time.Millisecond)
857		defer cancel()
858		ctx = cctx
859	}
860	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "backup.name", url.QueryEscape(req.GetBackup().GetName())))
861	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
862	opts = append((*c.CallOptions).UpdateBackup[0:len((*c.CallOptions).UpdateBackup):len((*c.CallOptions).UpdateBackup)], opts...)
863	var resp *databasepb.Backup
864	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
865		var err error
866		resp, err = c.databaseAdminClient.UpdateBackup(ctx, req, settings.GRPC...)
867		return err
868	}, opts...)
869	if err != nil {
870		return nil, err
871	}
872	return resp, nil
873}
874
875func (c *databaseAdminGRPCClient) DeleteBackup(ctx context.Context, req *databasepb.DeleteBackupRequest, opts ...gax.CallOption) error {
876	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
877		cctx, cancel := context.WithTimeout(ctx, 3600000*time.Millisecond)
878		defer cancel()
879		ctx = cctx
880	}
881	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
882	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
883	opts = append((*c.CallOptions).DeleteBackup[0:len((*c.CallOptions).DeleteBackup):len((*c.CallOptions).DeleteBackup)], opts...)
884	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
885		var err error
886		_, err = c.databaseAdminClient.DeleteBackup(ctx, req, settings.GRPC...)
887		return err
888	}, opts...)
889	return err
890}
891
892func (c *databaseAdminGRPCClient) ListBackups(ctx context.Context, req *databasepb.ListBackupsRequest, opts ...gax.CallOption) *BackupIterator {
893	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
894	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
895	opts = append((*c.CallOptions).ListBackups[0:len((*c.CallOptions).ListBackups):len((*c.CallOptions).ListBackups)], opts...)
896	it := &BackupIterator{}
897	req = proto.Clone(req).(*databasepb.ListBackupsRequest)
898	it.InternalFetch = func(pageSize int, pageToken string) ([]*databasepb.Backup, string, error) {
899		resp := &databasepb.ListBackupsResponse{}
900		if pageToken != "" {
901			req.PageToken = pageToken
902		}
903		if pageSize > math.MaxInt32 {
904			req.PageSize = math.MaxInt32
905		} else if pageSize != 0 {
906			req.PageSize = int32(pageSize)
907		}
908		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
909			var err error
910			resp, err = c.databaseAdminClient.ListBackups(ctx, req, settings.GRPC...)
911			return err
912		}, opts...)
913		if err != nil {
914			return nil, "", err
915		}
916
917		it.Response = resp
918		return resp.GetBackups(), resp.GetNextPageToken(), nil
919	}
920	fetch := func(pageSize int, pageToken string) (string, error) {
921		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
922		if err != nil {
923			return "", err
924		}
925		it.items = append(it.items, items...)
926		return nextPageToken, nil
927	}
928
929	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
930	it.pageInfo.MaxSize = int(req.GetPageSize())
931	it.pageInfo.Token = req.GetPageToken()
932
933	return it
934}
935
936func (c *databaseAdminGRPCClient) RestoreDatabase(ctx context.Context, req *databasepb.RestoreDatabaseRequest, opts ...gax.CallOption) (*RestoreDatabaseOperation, error) {
937	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
938		cctx, cancel := context.WithTimeout(ctx, 3600000*time.Millisecond)
939		defer cancel()
940		ctx = cctx
941	}
942	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
943	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
944	opts = append((*c.CallOptions).RestoreDatabase[0:len((*c.CallOptions).RestoreDatabase):len((*c.CallOptions).RestoreDatabase)], opts...)
945	var resp *longrunningpb.Operation
946	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
947		var err error
948		resp, err = c.databaseAdminClient.RestoreDatabase(ctx, req, settings.GRPC...)
949		return err
950	}, opts...)
951	if err != nil {
952		return nil, err
953	}
954	return &RestoreDatabaseOperation{
955		lro: longrunning.InternalNewOperation(*c.LROClient, resp),
956	}, nil
957}
958
959func (c *databaseAdminGRPCClient) ListDatabaseOperations(ctx context.Context, req *databasepb.ListDatabaseOperationsRequest, opts ...gax.CallOption) *OperationIterator {
960	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
961	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
962	opts = append((*c.CallOptions).ListDatabaseOperations[0:len((*c.CallOptions).ListDatabaseOperations):len((*c.CallOptions).ListDatabaseOperations)], opts...)
963	it := &OperationIterator{}
964	req = proto.Clone(req).(*databasepb.ListDatabaseOperationsRequest)
965	it.InternalFetch = func(pageSize int, pageToken string) ([]*longrunningpb.Operation, string, error) {
966		resp := &databasepb.ListDatabaseOperationsResponse{}
967		if pageToken != "" {
968			req.PageToken = pageToken
969		}
970		if pageSize > math.MaxInt32 {
971			req.PageSize = math.MaxInt32
972		} else if pageSize != 0 {
973			req.PageSize = int32(pageSize)
974		}
975		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
976			var err error
977			resp, err = c.databaseAdminClient.ListDatabaseOperations(ctx, req, settings.GRPC...)
978			return err
979		}, opts...)
980		if err != nil {
981			return nil, "", err
982		}
983
984		it.Response = resp
985		return resp.GetOperations(), resp.GetNextPageToken(), nil
986	}
987	fetch := func(pageSize int, pageToken string) (string, error) {
988		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
989		if err != nil {
990			return "", err
991		}
992		it.items = append(it.items, items...)
993		return nextPageToken, nil
994	}
995
996	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
997	it.pageInfo.MaxSize = int(req.GetPageSize())
998	it.pageInfo.Token = req.GetPageToken()
999
1000	return it
1001}
1002
1003func (c *databaseAdminGRPCClient) ListBackupOperations(ctx context.Context, req *databasepb.ListBackupOperationsRequest, opts ...gax.CallOption) *OperationIterator {
1004	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
1005	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
1006	opts = append((*c.CallOptions).ListBackupOperations[0:len((*c.CallOptions).ListBackupOperations):len((*c.CallOptions).ListBackupOperations)], opts...)
1007	it := &OperationIterator{}
1008	req = proto.Clone(req).(*databasepb.ListBackupOperationsRequest)
1009	it.InternalFetch = func(pageSize int, pageToken string) ([]*longrunningpb.Operation, string, error) {
1010		resp := &databasepb.ListBackupOperationsResponse{}
1011		if pageToken != "" {
1012			req.PageToken = pageToken
1013		}
1014		if pageSize > math.MaxInt32 {
1015			req.PageSize = math.MaxInt32
1016		} else if pageSize != 0 {
1017			req.PageSize = int32(pageSize)
1018		}
1019		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
1020			var err error
1021			resp, err = c.databaseAdminClient.ListBackupOperations(ctx, req, settings.GRPC...)
1022			return err
1023		}, opts...)
1024		if err != nil {
1025			return nil, "", err
1026		}
1027
1028		it.Response = resp
1029		return resp.GetOperations(), resp.GetNextPageToken(), nil
1030	}
1031	fetch := func(pageSize int, pageToken string) (string, error) {
1032		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
1033		if err != nil {
1034			return "", err
1035		}
1036		it.items = append(it.items, items...)
1037		return nextPageToken, nil
1038	}
1039
1040	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
1041	it.pageInfo.MaxSize = int(req.GetPageSize())
1042	it.pageInfo.Token = req.GetPageToken()
1043
1044	return it
1045}
1046
1047// CreateBackupOperation manages a long-running operation from CreateBackup.
1048type CreateBackupOperation struct {
1049	lro *longrunning.Operation
1050}
1051
1052// CreateBackupOperation returns a new CreateBackupOperation from a given name.
1053// The name must be that of a previously created CreateBackupOperation, possibly from a different process.
1054func (c *databaseAdminGRPCClient) CreateBackupOperation(name string) *CreateBackupOperation {
1055	return &CreateBackupOperation{
1056		lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
1057	}
1058}
1059
1060// Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
1061//
1062// See documentation of Poll for error-handling information.
1063func (op *CreateBackupOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*databasepb.Backup, error) {
1064	var resp databasepb.Backup
1065	if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil {
1066		return nil, err
1067	}
1068	return &resp, nil
1069}
1070
1071// Poll fetches the latest state of the long-running operation.
1072//
1073// Poll also fetches the latest metadata, which can be retrieved by Metadata.
1074//
1075// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
1076// the operation has completed with failure, the error is returned and op.Done will return true.
1077// If Poll succeeds and the operation has completed successfully,
1078// op.Done will return true, and the response of the operation is returned.
1079// If Poll succeeds and the operation has not completed, the returned response and error are both nil.
1080func (op *CreateBackupOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*databasepb.Backup, error) {
1081	var resp databasepb.Backup
1082	if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
1083		return nil, err
1084	}
1085	if !op.Done() {
1086		return nil, nil
1087	}
1088	return &resp, nil
1089}
1090
1091// Metadata returns metadata associated with the long-running operation.
1092// Metadata itself does not contact the server, but Poll does.
1093// To get the latest metadata, call this method after a successful call to Poll.
1094// If the metadata is not available, the returned metadata and error are both nil.
1095func (op *CreateBackupOperation) Metadata() (*databasepb.CreateBackupMetadata, error) {
1096	var meta databasepb.CreateBackupMetadata
1097	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
1098		return nil, nil
1099	} else if err != nil {
1100		return nil, err
1101	}
1102	return &meta, nil
1103}
1104
1105// Done reports whether the long-running operation has completed.
1106func (op *CreateBackupOperation) Done() bool {
1107	return op.lro.Done()
1108}
1109
1110// Name returns the name of the long-running operation.
1111// The name is assigned by the server and is unique within the service from which the operation is created.
1112func (op *CreateBackupOperation) Name() string {
1113	return op.lro.Name()
1114}
1115
1116// CreateDatabaseOperation manages a long-running operation from CreateDatabase.
1117type CreateDatabaseOperation struct {
1118	lro *longrunning.Operation
1119}
1120
1121// CreateDatabaseOperation returns a new CreateDatabaseOperation from a given name.
1122// The name must be that of a previously created CreateDatabaseOperation, possibly from a different process.
1123func (c *databaseAdminGRPCClient) CreateDatabaseOperation(name string) *CreateDatabaseOperation {
1124	return &CreateDatabaseOperation{
1125		lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
1126	}
1127}
1128
1129// Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
1130//
1131// See documentation of Poll for error-handling information.
1132func (op *CreateDatabaseOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*databasepb.Database, error) {
1133	var resp databasepb.Database
1134	if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil {
1135		return nil, err
1136	}
1137	return &resp, nil
1138}
1139
1140// Poll fetches the latest state of the long-running operation.
1141//
1142// Poll also fetches the latest metadata, which can be retrieved by Metadata.
1143//
1144// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
1145// the operation has completed with failure, the error is returned and op.Done will return true.
1146// If Poll succeeds and the operation has completed successfully,
1147// op.Done will return true, and the response of the operation is returned.
1148// If Poll succeeds and the operation has not completed, the returned response and error are both nil.
1149func (op *CreateDatabaseOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*databasepb.Database, error) {
1150	var resp databasepb.Database
1151	if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
1152		return nil, err
1153	}
1154	if !op.Done() {
1155		return nil, nil
1156	}
1157	return &resp, nil
1158}
1159
1160// Metadata returns metadata associated with the long-running operation.
1161// Metadata itself does not contact the server, but Poll does.
1162// To get the latest metadata, call this method after a successful call to Poll.
1163// If the metadata is not available, the returned metadata and error are both nil.
1164func (op *CreateDatabaseOperation) Metadata() (*databasepb.CreateDatabaseMetadata, error) {
1165	var meta databasepb.CreateDatabaseMetadata
1166	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
1167		return nil, nil
1168	} else if err != nil {
1169		return nil, err
1170	}
1171	return &meta, nil
1172}
1173
1174// Done reports whether the long-running operation has completed.
1175func (op *CreateDatabaseOperation) Done() bool {
1176	return op.lro.Done()
1177}
1178
1179// Name returns the name of the long-running operation.
1180// The name is assigned by the server and is unique within the service from which the operation is created.
1181func (op *CreateDatabaseOperation) Name() string {
1182	return op.lro.Name()
1183}
1184
1185// RestoreDatabaseOperation manages a long-running operation from RestoreDatabase.
1186type RestoreDatabaseOperation struct {
1187	lro *longrunning.Operation
1188}
1189
1190// RestoreDatabaseOperation returns a new RestoreDatabaseOperation from a given name.
1191// The name must be that of a previously created RestoreDatabaseOperation, possibly from a different process.
1192func (c *databaseAdminGRPCClient) RestoreDatabaseOperation(name string) *RestoreDatabaseOperation {
1193	return &RestoreDatabaseOperation{
1194		lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
1195	}
1196}
1197
1198// Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
1199//
1200// See documentation of Poll for error-handling information.
1201func (op *RestoreDatabaseOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*databasepb.Database, error) {
1202	var resp databasepb.Database
1203	if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil {
1204		return nil, err
1205	}
1206	return &resp, nil
1207}
1208
1209// Poll fetches the latest state of the long-running operation.
1210//
1211// Poll also fetches the latest metadata, which can be retrieved by Metadata.
1212//
1213// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
1214// the operation has completed with failure, the error is returned and op.Done will return true.
1215// If Poll succeeds and the operation has completed successfully,
1216// op.Done will return true, and the response of the operation is returned.
1217// If Poll succeeds and the operation has not completed, the returned response and error are both nil.
1218func (op *RestoreDatabaseOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*databasepb.Database, error) {
1219	var resp databasepb.Database
1220	if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
1221		return nil, err
1222	}
1223	if !op.Done() {
1224		return nil, nil
1225	}
1226	return &resp, nil
1227}
1228
1229// Metadata returns metadata associated with the long-running operation.
1230// Metadata itself does not contact the server, but Poll does.
1231// To get the latest metadata, call this method after a successful call to Poll.
1232// If the metadata is not available, the returned metadata and error are both nil.
1233func (op *RestoreDatabaseOperation) Metadata() (*databasepb.RestoreDatabaseMetadata, error) {
1234	var meta databasepb.RestoreDatabaseMetadata
1235	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
1236		return nil, nil
1237	} else if err != nil {
1238		return nil, err
1239	}
1240	return &meta, nil
1241}
1242
1243// Done reports whether the long-running operation has completed.
1244func (op *RestoreDatabaseOperation) Done() bool {
1245	return op.lro.Done()
1246}
1247
1248// Name returns the name of the long-running operation.
1249// The name is assigned by the server and is unique within the service from which the operation is created.
1250func (op *RestoreDatabaseOperation) Name() string {
1251	return op.lro.Name()
1252}
1253
1254// UpdateDatabaseDdlOperation manages a long-running operation from UpdateDatabaseDdl.
1255type UpdateDatabaseDdlOperation struct {
1256	lro *longrunning.Operation
1257}
1258
1259// UpdateDatabaseDdlOperation returns a new UpdateDatabaseDdlOperation from a given name.
1260// The name must be that of a previously created UpdateDatabaseDdlOperation, possibly from a different process.
1261func (c *databaseAdminGRPCClient) UpdateDatabaseDdlOperation(name string) *UpdateDatabaseDdlOperation {
1262	return &UpdateDatabaseDdlOperation{
1263		lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
1264	}
1265}
1266
1267// Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
1268//
1269// See documentation of Poll for error-handling information.
1270func (op *UpdateDatabaseDdlOperation) Wait(ctx context.Context, opts ...gax.CallOption) error {
1271	return op.lro.WaitWithInterval(ctx, nil, time.Minute, opts...)
1272}
1273
1274// Poll fetches the latest state of the long-running operation.
1275//
1276// Poll also fetches the latest metadata, which can be retrieved by Metadata.
1277//
1278// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
1279// the operation has completed with failure, the error is returned and op.Done will return true.
1280// If Poll succeeds and the operation has completed successfully,
1281// op.Done will return true, and the response of the operation is returned.
1282// If Poll succeeds and the operation has not completed, the returned response and error are both nil.
1283func (op *UpdateDatabaseDdlOperation) Poll(ctx context.Context, opts ...gax.CallOption) error {
1284	return op.lro.Poll(ctx, nil, opts...)
1285}
1286
1287// Metadata returns metadata associated with the long-running operation.
1288// Metadata itself does not contact the server, but Poll does.
1289// To get the latest metadata, call this method after a successful call to Poll.
1290// If the metadata is not available, the returned metadata and error are both nil.
1291func (op *UpdateDatabaseDdlOperation) Metadata() (*databasepb.UpdateDatabaseDdlMetadata, error) {
1292	var meta databasepb.UpdateDatabaseDdlMetadata
1293	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
1294		return nil, nil
1295	} else if err != nil {
1296		return nil, err
1297	}
1298	return &meta, nil
1299}
1300
1301// Done reports whether the long-running operation has completed.
1302func (op *UpdateDatabaseDdlOperation) Done() bool {
1303	return op.lro.Done()
1304}
1305
1306// Name returns the name of the long-running operation.
1307// The name is assigned by the server and is unique within the service from which the operation is created.
1308func (op *UpdateDatabaseDdlOperation) Name() string {
1309	return op.lro.Name()
1310}
1311
1312// BackupIterator manages a stream of *databasepb.Backup.
1313type BackupIterator struct {
1314	items    []*databasepb.Backup
1315	pageInfo *iterator.PageInfo
1316	nextFunc func() error
1317
1318	// Response is the raw response for the current page.
1319	// It must be cast to the RPC response type.
1320	// Calling Next() or InternalFetch() updates this value.
1321	Response interface{}
1322
1323	// InternalFetch is for use by the Google Cloud Libraries only.
1324	// It is not part of the stable interface of this package.
1325	//
1326	// InternalFetch returns results from a single call to the underlying RPC.
1327	// The number of results is no greater than pageSize.
1328	// If there are no more results, nextPageToken is empty and err is nil.
1329	InternalFetch func(pageSize int, pageToken string) (results []*databasepb.Backup, nextPageToken string, err error)
1330}
1331
1332// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
1333func (it *BackupIterator) PageInfo() *iterator.PageInfo {
1334	return it.pageInfo
1335}
1336
1337// Next returns the next result. Its second return value is iterator.Done if there are no more
1338// results. Once Next returns Done, all subsequent calls will return Done.
1339func (it *BackupIterator) Next() (*databasepb.Backup, error) {
1340	var item *databasepb.Backup
1341	if err := it.nextFunc(); err != nil {
1342		return item, err
1343	}
1344	item = it.items[0]
1345	it.items = it.items[1:]
1346	return item, nil
1347}
1348
1349func (it *BackupIterator) bufLen() int {
1350	return len(it.items)
1351}
1352
1353func (it *BackupIterator) takeBuf() interface{} {
1354	b := it.items
1355	it.items = nil
1356	return b
1357}
1358
1359// DatabaseIterator manages a stream of *databasepb.Database.
1360type DatabaseIterator struct {
1361	items    []*databasepb.Database
1362	pageInfo *iterator.PageInfo
1363	nextFunc func() error
1364
1365	// Response is the raw response for the current page.
1366	// It must be cast to the RPC response type.
1367	// Calling Next() or InternalFetch() updates this value.
1368	Response interface{}
1369
1370	// InternalFetch is for use by the Google Cloud Libraries only.
1371	// It is not part of the stable interface of this package.
1372	//
1373	// InternalFetch returns results from a single call to the underlying RPC.
1374	// The number of results is no greater than pageSize.
1375	// If there are no more results, nextPageToken is empty and err is nil.
1376	InternalFetch func(pageSize int, pageToken string) (results []*databasepb.Database, nextPageToken string, err error)
1377}
1378
1379// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
1380func (it *DatabaseIterator) PageInfo() *iterator.PageInfo {
1381	return it.pageInfo
1382}
1383
1384// Next returns the next result. Its second return value is iterator.Done if there are no more
1385// results. Once Next returns Done, all subsequent calls will return Done.
1386func (it *DatabaseIterator) Next() (*databasepb.Database, error) {
1387	var item *databasepb.Database
1388	if err := it.nextFunc(); err != nil {
1389		return item, err
1390	}
1391	item = it.items[0]
1392	it.items = it.items[1:]
1393	return item, nil
1394}
1395
1396func (it *DatabaseIterator) bufLen() int {
1397	return len(it.items)
1398}
1399
1400func (it *DatabaseIterator) takeBuf() interface{} {
1401	b := it.items
1402	it.items = nil
1403	return b
1404}
1405
1406// OperationIterator manages a stream of *longrunningpb.Operation.
1407type OperationIterator struct {
1408	items    []*longrunningpb.Operation
1409	pageInfo *iterator.PageInfo
1410	nextFunc func() error
1411
1412	// Response is the raw response for the current page.
1413	// It must be cast to the RPC response type.
1414	// Calling Next() or InternalFetch() updates this value.
1415	Response interface{}
1416
1417	// InternalFetch is for use by the Google Cloud Libraries only.
1418	// It is not part of the stable interface of this package.
1419	//
1420	// InternalFetch returns results from a single call to the underlying RPC.
1421	// The number of results is no greater than pageSize.
1422	// If there are no more results, nextPageToken is empty and err is nil.
1423	InternalFetch func(pageSize int, pageToken string) (results []*longrunningpb.Operation, nextPageToken string, err error)
1424}
1425
1426// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
1427func (it *OperationIterator) PageInfo() *iterator.PageInfo {
1428	return it.pageInfo
1429}
1430
1431// Next returns the next result. Its second return value is iterator.Done if there are no more
1432// results. Once Next returns Done, all subsequent calls will return Done.
1433func (it *OperationIterator) Next() (*longrunningpb.Operation, error) {
1434	var item *longrunningpb.Operation
1435	if err := it.nextFunc(); err != nil {
1436		return item, err
1437	}
1438	item = it.items[0]
1439	it.items = it.items[1:]
1440	return item, nil
1441}
1442
1443func (it *OperationIterator) bufLen() int {
1444	return len(it.items)
1445}
1446
1447func (it *OperationIterator) takeBuf() interface{} {
1448	b := it.items
1449	it.items = nil
1450	return b
1451}
1452