1// Copyright 2019 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 datatransfer
18
19import (
20	"context"
21	"fmt"
22	"math"
23	"net/url"
24
25	"github.com/golang/protobuf/proto"
26	gax "github.com/googleapis/gax-go/v2"
27	"google.golang.org/api/iterator"
28	"google.golang.org/api/option"
29	gtransport "google.golang.org/api/transport/grpc"
30	datatransferpb "google.golang.org/genproto/googleapis/cloud/bigquery/datatransfer/v1"
31	"google.golang.org/grpc"
32	"google.golang.org/grpc/metadata"
33)
34
35// DataSourceCallOptions contains the retry settings for each method of DataSourceClient.
36type DataSourceCallOptions struct {
37	UpdateTransferRun          []gax.CallOption
38	LogTransferRunMessages     []gax.CallOption
39	StartBigQueryJobs          []gax.CallOption
40	FinishRun                  []gax.CallOption
41	CreateDataSourceDefinition []gax.CallOption
42	UpdateDataSourceDefinition []gax.CallOption
43	DeleteDataSourceDefinition []gax.CallOption
44	GetDataSourceDefinition    []gax.CallOption
45	ListDataSourceDefinitions  []gax.CallOption
46}
47
48func defaultDataSourceClientOptions() []option.ClientOption {
49	return []option.ClientOption{
50		option.WithEndpoint("bigquerydatatransfer.googleapis.com:443"),
51		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
52		option.WithScopes(DefaultAuthScopes()...),
53		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
54			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
55	}
56}
57
58func defaultDataSourceCallOptions() *DataSourceCallOptions {
59	return &DataSourceCallOptions{
60		UpdateTransferRun:          []gax.CallOption{},
61		LogTransferRunMessages:     []gax.CallOption{},
62		StartBigQueryJobs:          []gax.CallOption{},
63		FinishRun:                  []gax.CallOption{},
64		CreateDataSourceDefinition: []gax.CallOption{},
65		UpdateDataSourceDefinition: []gax.CallOption{},
66		DeleteDataSourceDefinition: []gax.CallOption{},
67		GetDataSourceDefinition:    []gax.CallOption{},
68		ListDataSourceDefinitions:  []gax.CallOption{},
69	}
70}
71
72// DataSourceClient is a client for interacting with BigQuery Data Transfer API.
73//
74// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
75type DataSourceClient struct {
76	// The connection to the service.
77	connPool gtransport.ConnPool
78
79	// The gRPC API client.
80	dataSourceClient datatransferpb.DataSourceServiceClient
81
82	// The call options for this service.
83	CallOptions *DataSourceCallOptions
84
85	// The x-goog-* metadata to be sent with each request.
86	xGoogMetadata metadata.MD
87}
88
89// NewDataSourceClient creates a new data source service client.
90//
91// The Google BigQuery Data Transfer API allows BigQuery users to
92// configure transfer of their data from other Google Products into BigQuery.
93// This service exposes methods that should be used by data source backend.
94func NewDataSourceClient(ctx context.Context, opts ...option.ClientOption) (*DataSourceClient, error) {
95	connPool, err := gtransport.DialPool(ctx, append(defaultDataSourceClientOptions(), opts...)...)
96	if err != nil {
97		return nil, err
98	}
99	c := &DataSourceClient{
100		connPool:    connPool,
101		CallOptions: defaultDataSourceCallOptions(),
102
103		dataSourceClient: datatransferpb.NewDataSourceServiceClient(connPool),
104	}
105	c.setGoogleClientInfo()
106
107	return c, nil
108}
109
110// Connection returns the client's connection to the API service.
111func (c *DataSourceClient) Connection() *grpc.ClientConn {
112	return c.connPool.Conn()
113}
114
115// Close closes the connection to the API service. The user should invoke this when
116// the client is no longer required.
117func (c *DataSourceClient) Close() error {
118	return c.connPool.Close()
119}
120
121// setGoogleClientInfo sets the name and version of the application in
122// the `x-goog-api-client` header passed on each request. Intended for
123// use by Google-written clients.
124func (c *DataSourceClient) setGoogleClientInfo(keyval ...string) {
125	kv := append([]string{"gl-go", versionGo()}, keyval...)
126	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
127	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
128}
129
130// UpdateTransferRun update a transfer run. If successful, resets
131// data_source.update_deadline_seconds timer.
132func (c *DataSourceClient) UpdateTransferRun(ctx context.Context, req *datatransferpb.UpdateTransferRunRequest, opts ...gax.CallOption) (*datatransferpb.TransferRun, error) {
133	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "transfer_run.name", url.QueryEscape(req.GetTransferRun().GetName())))
134	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
135	opts = append(c.CallOptions.UpdateTransferRun[0:len(c.CallOptions.UpdateTransferRun):len(c.CallOptions.UpdateTransferRun)], opts...)
136	var resp *datatransferpb.TransferRun
137	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
138		var err error
139		resp, err = c.dataSourceClient.UpdateTransferRun(ctx, req, settings.GRPC...)
140		return err
141	}, opts...)
142	if err != nil {
143		return nil, err
144	}
145	return resp, nil
146}
147
148// LogTransferRunMessages log messages for a transfer run. If successful (at least 1 message), resets
149// data_source.update_deadline_seconds timer.
150func (c *DataSourceClient) LogTransferRunMessages(ctx context.Context, req *datatransferpb.LogTransferRunMessagesRequest, opts ...gax.CallOption) error {
151	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
152	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
153	opts = append(c.CallOptions.LogTransferRunMessages[0:len(c.CallOptions.LogTransferRunMessages):len(c.CallOptions.LogTransferRunMessages)], opts...)
154	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
155		var err error
156		_, err = c.dataSourceClient.LogTransferRunMessages(ctx, req, settings.GRPC...)
157		return err
158	}, opts...)
159	return err
160}
161
162// StartBigQueryJobs notify the Data Transfer Service that data is ready for loading.
163// The Data Transfer Service will start and monitor multiple BigQuery Load
164// jobs for a transfer run. Monitored jobs will be automatically retried
165// and produce log messages when starting and finishing a job.
166// Can be called multiple times for the same transfer run.
167func (c *DataSourceClient) StartBigQueryJobs(ctx context.Context, req *datatransferpb.StartBigQueryJobsRequest, opts ...gax.CallOption) error {
168	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
169	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
170	opts = append(c.CallOptions.StartBigQueryJobs[0:len(c.CallOptions.StartBigQueryJobs):len(c.CallOptions.StartBigQueryJobs)], opts...)
171	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
172		var err error
173		_, err = c.dataSourceClient.StartBigQueryJobs(ctx, req, settings.GRPC...)
174		return err
175	}, opts...)
176	return err
177}
178
179// FinishRun notify the Data Transfer Service that the data source is done processing
180// the run. No more status updates or requests to start/monitor jobs will be
181// accepted. The run will be finalized by the Data Transfer Service when all
182// monitored jobs are completed.
183// Does not need to be called if the run is set to FAILED.
184func (c *DataSourceClient) FinishRun(ctx context.Context, req *datatransferpb.FinishRunRequest, opts ...gax.CallOption) error {
185	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
186	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
187	opts = append(c.CallOptions.FinishRun[0:len(c.CallOptions.FinishRun):len(c.CallOptions.FinishRun)], opts...)
188	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
189		var err error
190		_, err = c.dataSourceClient.FinishRun(ctx, req, settings.GRPC...)
191		return err
192	}, opts...)
193	return err
194}
195
196// CreateDataSourceDefinition creates a data source definition.  Calling this method will automatically
197// use your credentials to create the following Google Cloud resources in
198// YOUR Google Cloud project.
199//
200// OAuth client
201//
202// Pub/Sub Topics and Subscriptions in each supported_location_ids. e.g.,
203// projects/{project_id}/{topics|subscriptions}/bigquerydatatransfer.{data_source_id}.{location_id}.run
204// The field data_source.client_id should be left empty in the input request,
205// as the API will create a new OAuth client on behalf of the caller. On the
206// other hand data_source.scopes usually need to be set when there are OAuth
207// scopes that need to be granted by end users.
208//
209// We need a longer deadline due to the 60 seconds SLO from Pub/Sub admin
210// Operations. This also applies to update and delete data source definition.
211func (c *DataSourceClient) CreateDataSourceDefinition(ctx context.Context, req *datatransferpb.CreateDataSourceDefinitionRequest, opts ...gax.CallOption) (*datatransferpb.DataSourceDefinition, error) {
212	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
213	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
214	opts = append(c.CallOptions.CreateDataSourceDefinition[0:len(c.CallOptions.CreateDataSourceDefinition):len(c.CallOptions.CreateDataSourceDefinition)], opts...)
215	var resp *datatransferpb.DataSourceDefinition
216	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
217		var err error
218		resp, err = c.dataSourceClient.CreateDataSourceDefinition(ctx, req, settings.GRPC...)
219		return err
220	}, opts...)
221	if err != nil {
222		return nil, err
223	}
224	return resp, nil
225}
226
227// UpdateDataSourceDefinition updates an existing data source definition. If changing
228// supported_location_ids, triggers same effects as mentioned in “Create a
229// data source definition.”
230func (c *DataSourceClient) UpdateDataSourceDefinition(ctx context.Context, req *datatransferpb.UpdateDataSourceDefinitionRequest, opts ...gax.CallOption) (*datatransferpb.DataSourceDefinition, error) {
231	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "data_source_definition.name", url.QueryEscape(req.GetDataSourceDefinition().GetName())))
232	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
233	opts = append(c.CallOptions.UpdateDataSourceDefinition[0:len(c.CallOptions.UpdateDataSourceDefinition):len(c.CallOptions.UpdateDataSourceDefinition)], opts...)
234	var resp *datatransferpb.DataSourceDefinition
235	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
236		var err error
237		resp, err = c.dataSourceClient.UpdateDataSourceDefinition(ctx, req, settings.GRPC...)
238		return err
239	}, opts...)
240	if err != nil {
241		return nil, err
242	}
243	return resp, nil
244}
245
246// DeleteDataSourceDefinition deletes a data source definition, all of the transfer configs associated
247// with this data source definition (if any) must be deleted first by the user
248// in ALL regions, in order to delete the data source definition.
249// This method is primarily meant for deleting data sources created during
250// testing stage.
251// If the data source is referenced by transfer configs in the region
252// specified in the request URL, the method will fail immediately. If in the
253// current region (e.g., US) it’s not used by any transfer configs, but in
254// another region (e.g., EU) it is, then although the method will succeed in
255// region US, but it will fail when the deletion operation is replicated to
256// region EU. And eventually, the system will replicate the data source
257// definition back from EU to US, in order to bring all regions to
258// consistency. The final effect is that the data source appears to be
259// ‘undeleted’ in the US region.
260func (c *DataSourceClient) DeleteDataSourceDefinition(ctx context.Context, req *datatransferpb.DeleteDataSourceDefinitionRequest, opts ...gax.CallOption) error {
261	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
262	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
263	opts = append(c.CallOptions.DeleteDataSourceDefinition[0:len(c.CallOptions.DeleteDataSourceDefinition):len(c.CallOptions.DeleteDataSourceDefinition)], opts...)
264	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
265		var err error
266		_, err = c.dataSourceClient.DeleteDataSourceDefinition(ctx, req, settings.GRPC...)
267		return err
268	}, opts...)
269	return err
270}
271
272// GetDataSourceDefinition retrieves an existing data source definition.
273func (c *DataSourceClient) GetDataSourceDefinition(ctx context.Context, req *datatransferpb.GetDataSourceDefinitionRequest, opts ...gax.CallOption) (*datatransferpb.DataSourceDefinition, error) {
274	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
275	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
276	opts = append(c.CallOptions.GetDataSourceDefinition[0:len(c.CallOptions.GetDataSourceDefinition):len(c.CallOptions.GetDataSourceDefinition)], opts...)
277	var resp *datatransferpb.DataSourceDefinition
278	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
279		var err error
280		resp, err = c.dataSourceClient.GetDataSourceDefinition(ctx, req, settings.GRPC...)
281		return err
282	}, opts...)
283	if err != nil {
284		return nil, err
285	}
286	return resp, nil
287}
288
289// ListDataSourceDefinitions lists supported data source definitions.
290func (c *DataSourceClient) ListDataSourceDefinitions(ctx context.Context, req *datatransferpb.ListDataSourceDefinitionsRequest, opts ...gax.CallOption) *DataSourceDefinitionIterator {
291	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
292	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
293	opts = append(c.CallOptions.ListDataSourceDefinitions[0:len(c.CallOptions.ListDataSourceDefinitions):len(c.CallOptions.ListDataSourceDefinitions)], opts...)
294	it := &DataSourceDefinitionIterator{}
295	req = proto.Clone(req).(*datatransferpb.ListDataSourceDefinitionsRequest)
296	it.InternalFetch = func(pageSize int, pageToken string) ([]*datatransferpb.DataSourceDefinition, string, error) {
297		var resp *datatransferpb.ListDataSourceDefinitionsResponse
298		req.PageToken = pageToken
299		if pageSize > math.MaxInt32 {
300			req.PageSize = math.MaxInt32
301		} else {
302			req.PageSize = int32(pageSize)
303		}
304		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
305			var err error
306			resp, err = c.dataSourceClient.ListDataSourceDefinitions(ctx, req, settings.GRPC...)
307			return err
308		}, opts...)
309		if err != nil {
310			return nil, "", err
311		}
312
313		it.Response = resp
314		return resp.DataSourceDefinitions, resp.NextPageToken, nil
315	}
316	fetch := func(pageSize int, pageToken string) (string, error) {
317		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
318		if err != nil {
319			return "", err
320		}
321		it.items = append(it.items, items...)
322		return nextPageToken, nil
323	}
324	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
325	it.pageInfo.MaxSize = int(req.PageSize)
326	it.pageInfo.Token = req.PageToken
327	return it
328}
329
330// DataSourceDefinitionIterator manages a stream of *datatransferpb.DataSourceDefinition.
331type DataSourceDefinitionIterator struct {
332	items    []*datatransferpb.DataSourceDefinition
333	pageInfo *iterator.PageInfo
334	nextFunc func() error
335
336	// Response is the raw response for the current page.
337	// It must be cast to the RPC response type.
338	// Calling Next() or InternalFetch() updates this value.
339	Response interface{}
340
341	// InternalFetch is for use by the Google Cloud Libraries only.
342	// It is not part of the stable interface of this package.
343	//
344	// InternalFetch returns results from a single call to the underlying RPC.
345	// The number of results is no greater than pageSize.
346	// If there are no more results, nextPageToken is empty and err is nil.
347	InternalFetch func(pageSize int, pageToken string) (results []*datatransferpb.DataSourceDefinition, nextPageToken string, err error)
348}
349
350// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
351func (it *DataSourceDefinitionIterator) PageInfo() *iterator.PageInfo {
352	return it.pageInfo
353}
354
355// Next returns the next result. Its second return value is iterator.Done if there are no more
356// results. Once Next returns Done, all subsequent calls will return Done.
357func (it *DataSourceDefinitionIterator) Next() (*datatransferpb.DataSourceDefinition, error) {
358	var item *datatransferpb.DataSourceDefinition
359	if err := it.nextFunc(); err != nil {
360		return item, err
361	}
362	item = it.items[0]
363	it.items = it.items[1:]
364	return item, nil
365}
366
367func (it *DataSourceDefinitionIterator) bufLen() int {
368	return len(it.items)
369}
370
371func (it *DataSourceDefinitionIterator) takeBuf() interface{} {
372	b := it.items
373	it.items = nil
374	return b
375}
376