...

Source file src/cloud.google.com/go/bigquery/migration/apiv2/migration_client.go

Documentation: cloud.google.com/go/bigquery/migration/apiv2

     1  // Copyright 2024 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  
    17  package migration
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"math"
    23  	"net/url"
    24  	"time"
    25  
    26  	migrationpb "cloud.google.com/go/bigquery/migration/apiv2/migrationpb"
    27  	gax "github.com/googleapis/gax-go/v2"
    28  	"google.golang.org/api/iterator"
    29  	"google.golang.org/api/option"
    30  	"google.golang.org/api/option/internaloption"
    31  	gtransport "google.golang.org/api/transport/grpc"
    32  	"google.golang.org/grpc"
    33  	"google.golang.org/grpc/codes"
    34  	"google.golang.org/protobuf/proto"
    35  )
    36  
    37  var newClientHook clientHook
    38  
    39  // CallOptions contains the retry settings for each method of Client.
    40  type CallOptions struct {
    41  	CreateMigrationWorkflow []gax.CallOption
    42  	GetMigrationWorkflow    []gax.CallOption
    43  	ListMigrationWorkflows  []gax.CallOption
    44  	DeleteMigrationWorkflow []gax.CallOption
    45  	StartMigrationWorkflow  []gax.CallOption
    46  	GetMigrationSubtask     []gax.CallOption
    47  	ListMigrationSubtasks   []gax.CallOption
    48  }
    49  
    50  func defaultGRPCClientOptions() []option.ClientOption {
    51  	return []option.ClientOption{
    52  		internaloption.WithDefaultEndpoint("bigquerymigration.googleapis.com:443"),
    53  		internaloption.WithDefaultEndpointTemplate("bigquerymigration.UNIVERSE_DOMAIN:443"),
    54  		internaloption.WithDefaultMTLSEndpoint("bigquerymigration.mtls.googleapis.com:443"),
    55  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
    56  		internaloption.WithDefaultAudience("https://bigquerymigration.googleapis.com/"),
    57  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
    58  		internaloption.EnableJwtWithScope(),
    59  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    60  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    61  	}
    62  }
    63  
    64  func defaultCallOptions() *CallOptions {
    65  	return &CallOptions{
    66  		CreateMigrationWorkflow: []gax.CallOption{
    67  			gax.WithTimeout(60000 * time.Millisecond),
    68  		},
    69  		GetMigrationWorkflow: []gax.CallOption{
    70  			gax.WithTimeout(60000 * time.Millisecond),
    71  			gax.WithRetry(func() gax.Retryer {
    72  				return gax.OnCodes([]codes.Code{
    73  					codes.Unavailable,
    74  				}, gax.Backoff{
    75  					Initial:    1000 * time.Millisecond,
    76  					Max:        10000 * time.Millisecond,
    77  					Multiplier: 1.30,
    78  				})
    79  			}),
    80  		},
    81  		ListMigrationWorkflows: []gax.CallOption{
    82  			gax.WithTimeout(60000 * time.Millisecond),
    83  			gax.WithRetry(func() gax.Retryer {
    84  				return gax.OnCodes([]codes.Code{
    85  					codes.Unavailable,
    86  				}, gax.Backoff{
    87  					Initial:    1000 * time.Millisecond,
    88  					Max:        10000 * time.Millisecond,
    89  					Multiplier: 1.30,
    90  				})
    91  			}),
    92  		},
    93  		DeleteMigrationWorkflow: []gax.CallOption{
    94  			gax.WithTimeout(60000 * time.Millisecond),
    95  		},
    96  		StartMigrationWorkflow: []gax.CallOption{
    97  			gax.WithTimeout(60000 * time.Millisecond),
    98  			gax.WithRetry(func() gax.Retryer {
    99  				return gax.OnCodes([]codes.Code{
   100  					codes.Unavailable,
   101  				}, gax.Backoff{
   102  					Initial:    1000 * time.Millisecond,
   103  					Max:        10000 * time.Millisecond,
   104  					Multiplier: 1.30,
   105  				})
   106  			}),
   107  		},
   108  		GetMigrationSubtask:   []gax.CallOption{},
   109  		ListMigrationSubtasks: []gax.CallOption{},
   110  	}
   111  }
   112  
   113  // internalClient is an interface that defines the methods available from BigQuery Migration API.
   114  type internalClient interface {
   115  	Close() error
   116  	setGoogleClientInfo(...string)
   117  	Connection() *grpc.ClientConn
   118  	CreateMigrationWorkflow(context.Context, *migrationpb.CreateMigrationWorkflowRequest, ...gax.CallOption) (*migrationpb.MigrationWorkflow, error)
   119  	GetMigrationWorkflow(context.Context, *migrationpb.GetMigrationWorkflowRequest, ...gax.CallOption) (*migrationpb.MigrationWorkflow, error)
   120  	ListMigrationWorkflows(context.Context, *migrationpb.ListMigrationWorkflowsRequest, ...gax.CallOption) *MigrationWorkflowIterator
   121  	DeleteMigrationWorkflow(context.Context, *migrationpb.DeleteMigrationWorkflowRequest, ...gax.CallOption) error
   122  	StartMigrationWorkflow(context.Context, *migrationpb.StartMigrationWorkflowRequest, ...gax.CallOption) error
   123  	GetMigrationSubtask(context.Context, *migrationpb.GetMigrationSubtaskRequest, ...gax.CallOption) (*migrationpb.MigrationSubtask, error)
   124  	ListMigrationSubtasks(context.Context, *migrationpb.ListMigrationSubtasksRequest, ...gax.CallOption) *MigrationSubtaskIterator
   125  }
   126  
   127  // Client is a client for interacting with BigQuery Migration API.
   128  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   129  //
   130  // Service to handle EDW migrations.
   131  type Client struct {
   132  	// The internal transport-dependent client.
   133  	internalClient internalClient
   134  
   135  	// The call options for this service.
   136  	CallOptions *CallOptions
   137  }
   138  
   139  // Wrapper methods routed to the internal client.
   140  
   141  // Close closes the connection to the API service. The user should invoke this when
   142  // the client is no longer required.
   143  func (c *Client) Close() error {
   144  	return c.internalClient.Close()
   145  }
   146  
   147  // setGoogleClientInfo sets the name and version of the application in
   148  // the `x-goog-api-client` header passed on each request. Intended for
   149  // use by Google-written clients.
   150  func (c *Client) setGoogleClientInfo(keyval ...string) {
   151  	c.internalClient.setGoogleClientInfo(keyval...)
   152  }
   153  
   154  // Connection returns a connection to the API service.
   155  //
   156  // Deprecated: Connections are now pooled so this method does not always
   157  // return the same resource.
   158  func (c *Client) Connection() *grpc.ClientConn {
   159  	return c.internalClient.Connection()
   160  }
   161  
   162  // CreateMigrationWorkflow creates a migration workflow.
   163  func (c *Client) CreateMigrationWorkflow(ctx context.Context, req *migrationpb.CreateMigrationWorkflowRequest, opts ...gax.CallOption) (*migrationpb.MigrationWorkflow, error) {
   164  	return c.internalClient.CreateMigrationWorkflow(ctx, req, opts...)
   165  }
   166  
   167  // GetMigrationWorkflow gets a previously created migration workflow.
   168  func (c *Client) GetMigrationWorkflow(ctx context.Context, req *migrationpb.GetMigrationWorkflowRequest, opts ...gax.CallOption) (*migrationpb.MigrationWorkflow, error) {
   169  	return c.internalClient.GetMigrationWorkflow(ctx, req, opts...)
   170  }
   171  
   172  // ListMigrationWorkflows lists previously created migration workflow.
   173  func (c *Client) ListMigrationWorkflows(ctx context.Context, req *migrationpb.ListMigrationWorkflowsRequest, opts ...gax.CallOption) *MigrationWorkflowIterator {
   174  	return c.internalClient.ListMigrationWorkflows(ctx, req, opts...)
   175  }
   176  
   177  // DeleteMigrationWorkflow deletes a migration workflow by name.
   178  func (c *Client) DeleteMigrationWorkflow(ctx context.Context, req *migrationpb.DeleteMigrationWorkflowRequest, opts ...gax.CallOption) error {
   179  	return c.internalClient.DeleteMigrationWorkflow(ctx, req, opts...)
   180  }
   181  
   182  // StartMigrationWorkflow starts a previously created migration workflow. I.e., the state transitions
   183  // from DRAFT to RUNNING. This is a no-op if the state is already RUNNING.
   184  // An error will be signaled if the state is anything other than DRAFT or
   185  // RUNNING.
   186  func (c *Client) StartMigrationWorkflow(ctx context.Context, req *migrationpb.StartMigrationWorkflowRequest, opts ...gax.CallOption) error {
   187  	return c.internalClient.StartMigrationWorkflow(ctx, req, opts...)
   188  }
   189  
   190  // GetMigrationSubtask gets a previously created migration subtask.
   191  func (c *Client) GetMigrationSubtask(ctx context.Context, req *migrationpb.GetMigrationSubtaskRequest, opts ...gax.CallOption) (*migrationpb.MigrationSubtask, error) {
   192  	return c.internalClient.GetMigrationSubtask(ctx, req, opts...)
   193  }
   194  
   195  // ListMigrationSubtasks lists previously created migration subtasks.
   196  func (c *Client) ListMigrationSubtasks(ctx context.Context, req *migrationpb.ListMigrationSubtasksRequest, opts ...gax.CallOption) *MigrationSubtaskIterator {
   197  	return c.internalClient.ListMigrationSubtasks(ctx, req, opts...)
   198  }
   199  
   200  // gRPCClient is a client for interacting with BigQuery Migration API over gRPC transport.
   201  //
   202  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   203  type gRPCClient struct {
   204  	// Connection pool of gRPC connections to the service.
   205  	connPool gtransport.ConnPool
   206  
   207  	// Points back to the CallOptions field of the containing Client
   208  	CallOptions **CallOptions
   209  
   210  	// The gRPC API client.
   211  	client migrationpb.MigrationServiceClient
   212  
   213  	// The x-goog-* metadata to be sent with each request.
   214  	xGoogHeaders []string
   215  }
   216  
   217  // NewClient creates a new migration service client based on gRPC.
   218  // The returned client must be Closed when it is done being used to clean up its underlying connections.
   219  //
   220  // Service to handle EDW migrations.
   221  func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
   222  	clientOpts := defaultGRPCClientOptions()
   223  	if newClientHook != nil {
   224  		hookOpts, err := newClientHook(ctx, clientHookParams{})
   225  		if err != nil {
   226  			return nil, err
   227  		}
   228  		clientOpts = append(clientOpts, hookOpts...)
   229  	}
   230  
   231  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   232  	if err != nil {
   233  		return nil, err
   234  	}
   235  	client := Client{CallOptions: defaultCallOptions()}
   236  
   237  	c := &gRPCClient{
   238  		connPool:    connPool,
   239  		client:      migrationpb.NewMigrationServiceClient(connPool),
   240  		CallOptions: &client.CallOptions,
   241  	}
   242  	c.setGoogleClientInfo()
   243  
   244  	client.internalClient = c
   245  
   246  	return &client, nil
   247  }
   248  
   249  // Connection returns a connection to the API service.
   250  //
   251  // Deprecated: Connections are now pooled so this method does not always
   252  // return the same resource.
   253  func (c *gRPCClient) Connection() *grpc.ClientConn {
   254  	return c.connPool.Conn()
   255  }
   256  
   257  // setGoogleClientInfo sets the name and version of the application in
   258  // the `x-goog-api-client` header passed on each request. Intended for
   259  // use by Google-written clients.
   260  func (c *gRPCClient) setGoogleClientInfo(keyval ...string) {
   261  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   262  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
   263  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   264  }
   265  
   266  // Close closes the connection to the API service. The user should invoke this when
   267  // the client is no longer required.
   268  func (c *gRPCClient) Close() error {
   269  	return c.connPool.Close()
   270  }
   271  
   272  func (c *gRPCClient) CreateMigrationWorkflow(ctx context.Context, req *migrationpb.CreateMigrationWorkflowRequest, opts ...gax.CallOption) (*migrationpb.MigrationWorkflow, error) {
   273  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   274  
   275  	hds = append(c.xGoogHeaders, hds...)
   276  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   277  	opts = append((*c.CallOptions).CreateMigrationWorkflow[0:len((*c.CallOptions).CreateMigrationWorkflow):len((*c.CallOptions).CreateMigrationWorkflow)], opts...)
   278  	var resp *migrationpb.MigrationWorkflow
   279  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   280  		var err error
   281  		resp, err = c.client.CreateMigrationWorkflow(ctx, req, settings.GRPC...)
   282  		return err
   283  	}, opts...)
   284  	if err != nil {
   285  		return nil, err
   286  	}
   287  	return resp, nil
   288  }
   289  
   290  func (c *gRPCClient) GetMigrationWorkflow(ctx context.Context, req *migrationpb.GetMigrationWorkflowRequest, opts ...gax.CallOption) (*migrationpb.MigrationWorkflow, error) {
   291  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   292  
   293  	hds = append(c.xGoogHeaders, hds...)
   294  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   295  	opts = append((*c.CallOptions).GetMigrationWorkflow[0:len((*c.CallOptions).GetMigrationWorkflow):len((*c.CallOptions).GetMigrationWorkflow)], opts...)
   296  	var resp *migrationpb.MigrationWorkflow
   297  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   298  		var err error
   299  		resp, err = c.client.GetMigrationWorkflow(ctx, req, settings.GRPC...)
   300  		return err
   301  	}, opts...)
   302  	if err != nil {
   303  		return nil, err
   304  	}
   305  	return resp, nil
   306  }
   307  
   308  func (c *gRPCClient) ListMigrationWorkflows(ctx context.Context, req *migrationpb.ListMigrationWorkflowsRequest, opts ...gax.CallOption) *MigrationWorkflowIterator {
   309  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   310  
   311  	hds = append(c.xGoogHeaders, hds...)
   312  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   313  	opts = append((*c.CallOptions).ListMigrationWorkflows[0:len((*c.CallOptions).ListMigrationWorkflows):len((*c.CallOptions).ListMigrationWorkflows)], opts...)
   314  	it := &MigrationWorkflowIterator{}
   315  	req = proto.Clone(req).(*migrationpb.ListMigrationWorkflowsRequest)
   316  	it.InternalFetch = func(pageSize int, pageToken string) ([]*migrationpb.MigrationWorkflow, string, error) {
   317  		resp := &migrationpb.ListMigrationWorkflowsResponse{}
   318  		if pageToken != "" {
   319  			req.PageToken = pageToken
   320  		}
   321  		if pageSize > math.MaxInt32 {
   322  			req.PageSize = math.MaxInt32
   323  		} else if pageSize != 0 {
   324  			req.PageSize = int32(pageSize)
   325  		}
   326  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   327  			var err error
   328  			resp, err = c.client.ListMigrationWorkflows(ctx, req, settings.GRPC...)
   329  			return err
   330  		}, opts...)
   331  		if err != nil {
   332  			return nil, "", err
   333  		}
   334  
   335  		it.Response = resp
   336  		return resp.GetMigrationWorkflows(), resp.GetNextPageToken(), nil
   337  	}
   338  	fetch := func(pageSize int, pageToken string) (string, error) {
   339  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   340  		if err != nil {
   341  			return "", err
   342  		}
   343  		it.items = append(it.items, items...)
   344  		return nextPageToken, nil
   345  	}
   346  
   347  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   348  	it.pageInfo.MaxSize = int(req.GetPageSize())
   349  	it.pageInfo.Token = req.GetPageToken()
   350  
   351  	return it
   352  }
   353  
   354  func (c *gRPCClient) DeleteMigrationWorkflow(ctx context.Context, req *migrationpb.DeleteMigrationWorkflowRequest, opts ...gax.CallOption) error {
   355  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   356  
   357  	hds = append(c.xGoogHeaders, hds...)
   358  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   359  	opts = append((*c.CallOptions).DeleteMigrationWorkflow[0:len((*c.CallOptions).DeleteMigrationWorkflow):len((*c.CallOptions).DeleteMigrationWorkflow)], opts...)
   360  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   361  		var err error
   362  		_, err = c.client.DeleteMigrationWorkflow(ctx, req, settings.GRPC...)
   363  		return err
   364  	}, opts...)
   365  	return err
   366  }
   367  
   368  func (c *gRPCClient) StartMigrationWorkflow(ctx context.Context, req *migrationpb.StartMigrationWorkflowRequest, opts ...gax.CallOption) error {
   369  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   370  
   371  	hds = append(c.xGoogHeaders, hds...)
   372  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   373  	opts = append((*c.CallOptions).StartMigrationWorkflow[0:len((*c.CallOptions).StartMigrationWorkflow):len((*c.CallOptions).StartMigrationWorkflow)], opts...)
   374  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   375  		var err error
   376  		_, err = c.client.StartMigrationWorkflow(ctx, req, settings.GRPC...)
   377  		return err
   378  	}, opts...)
   379  	return err
   380  }
   381  
   382  func (c *gRPCClient) GetMigrationSubtask(ctx context.Context, req *migrationpb.GetMigrationSubtaskRequest, opts ...gax.CallOption) (*migrationpb.MigrationSubtask, error) {
   383  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   384  
   385  	hds = append(c.xGoogHeaders, hds...)
   386  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   387  	opts = append((*c.CallOptions).GetMigrationSubtask[0:len((*c.CallOptions).GetMigrationSubtask):len((*c.CallOptions).GetMigrationSubtask)], opts...)
   388  	var resp *migrationpb.MigrationSubtask
   389  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   390  		var err error
   391  		resp, err = c.client.GetMigrationSubtask(ctx, req, settings.GRPC...)
   392  		return err
   393  	}, opts...)
   394  	if err != nil {
   395  		return nil, err
   396  	}
   397  	return resp, nil
   398  }
   399  
   400  func (c *gRPCClient) ListMigrationSubtasks(ctx context.Context, req *migrationpb.ListMigrationSubtasksRequest, opts ...gax.CallOption) *MigrationSubtaskIterator {
   401  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   402  
   403  	hds = append(c.xGoogHeaders, hds...)
   404  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   405  	opts = append((*c.CallOptions).ListMigrationSubtasks[0:len((*c.CallOptions).ListMigrationSubtasks):len((*c.CallOptions).ListMigrationSubtasks)], opts...)
   406  	it := &MigrationSubtaskIterator{}
   407  	req = proto.Clone(req).(*migrationpb.ListMigrationSubtasksRequest)
   408  	it.InternalFetch = func(pageSize int, pageToken string) ([]*migrationpb.MigrationSubtask, string, error) {
   409  		resp := &migrationpb.ListMigrationSubtasksResponse{}
   410  		if pageToken != "" {
   411  			req.PageToken = pageToken
   412  		}
   413  		if pageSize > math.MaxInt32 {
   414  			req.PageSize = math.MaxInt32
   415  		} else if pageSize != 0 {
   416  			req.PageSize = int32(pageSize)
   417  		}
   418  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   419  			var err error
   420  			resp, err = c.client.ListMigrationSubtasks(ctx, req, settings.GRPC...)
   421  			return err
   422  		}, opts...)
   423  		if err != nil {
   424  			return nil, "", err
   425  		}
   426  
   427  		it.Response = resp
   428  		return resp.GetMigrationSubtasks(), resp.GetNextPageToken(), nil
   429  	}
   430  	fetch := func(pageSize int, pageToken string) (string, error) {
   431  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   432  		if err != nil {
   433  			return "", err
   434  		}
   435  		it.items = append(it.items, items...)
   436  		return nextPageToken, nil
   437  	}
   438  
   439  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   440  	it.pageInfo.MaxSize = int(req.GetPageSize())
   441  	it.pageInfo.Token = req.GetPageToken()
   442  
   443  	return it
   444  }
   445  

View as plain text