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 dataproc
18
19import (
20	"context"
21	"math"
22	"time"
23
24	"github.com/golang/protobuf/proto"
25	gax "github.com/googleapis/gax-go/v2"
26	"google.golang.org/api/iterator"
27	"google.golang.org/api/option"
28	"google.golang.org/api/transport"
29	dataprocpb "google.golang.org/genproto/googleapis/cloud/dataproc/v1"
30	"google.golang.org/grpc"
31	"google.golang.org/grpc/codes"
32	"google.golang.org/grpc/metadata"
33)
34
35// JobControllerCallOptions contains the retry settings for each method of JobControllerClient.
36type JobControllerCallOptions struct {
37	SubmitJob []gax.CallOption
38	GetJob    []gax.CallOption
39	ListJobs  []gax.CallOption
40	UpdateJob []gax.CallOption
41	CancelJob []gax.CallOption
42	DeleteJob []gax.CallOption
43}
44
45func defaultJobControllerClientOptions() []option.ClientOption {
46	return []option.ClientOption{
47		option.WithEndpoint("dataproc.googleapis.com:443"),
48		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
49		option.WithScopes(DefaultAuthScopes()...),
50		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
51			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
52	}
53}
54
55func defaultJobControllerCallOptions() *JobControllerCallOptions {
56	return &JobControllerCallOptions{
57		SubmitJob: []gax.CallOption{
58			gax.WithRetry(func() gax.Retryer {
59				return gax.OnCodes([]codes.Code{
60					codes.Unavailable,
61				}, gax.Backoff{
62					Initial:    100 * time.Millisecond,
63					Max:        60000 * time.Millisecond,
64					Multiplier: 1.30,
65				})
66			}),
67		},
68		GetJob: []gax.CallOption{
69			gax.WithRetry(func() gax.Retryer {
70				return gax.OnCodes([]codes.Code{
71					codes.DeadlineExceeded,
72					codes.Internal,
73					codes.Unavailable,
74				}, gax.Backoff{
75					Initial:    100 * time.Millisecond,
76					Max:        60000 * time.Millisecond,
77					Multiplier: 1.30,
78				})
79			}),
80		},
81		ListJobs: []gax.CallOption{
82			gax.WithRetry(func() gax.Retryer {
83				return gax.OnCodes([]codes.Code{
84					codes.DeadlineExceeded,
85					codes.Internal,
86					codes.Unavailable,
87				}, gax.Backoff{
88					Initial:    100 * time.Millisecond,
89					Max:        60000 * time.Millisecond,
90					Multiplier: 1.30,
91				})
92			}),
93		},
94		UpdateJob: []gax.CallOption{
95			gax.WithRetry(func() gax.Retryer {
96				return gax.OnCodes([]codes.Code{
97					codes.Unavailable,
98				}, gax.Backoff{
99					Initial:    100 * time.Millisecond,
100					Max:        60000 * time.Millisecond,
101					Multiplier: 1.30,
102				})
103			}),
104		},
105		CancelJob: []gax.CallOption{
106			gax.WithRetry(func() gax.Retryer {
107				return gax.OnCodes([]codes.Code{
108					codes.DeadlineExceeded,
109					codes.Internal,
110					codes.Unavailable,
111				}, gax.Backoff{
112					Initial:    100 * time.Millisecond,
113					Max:        60000 * time.Millisecond,
114					Multiplier: 1.30,
115				})
116			}),
117		},
118		DeleteJob: []gax.CallOption{
119			gax.WithRetry(func() gax.Retryer {
120				return gax.OnCodes([]codes.Code{
121					codes.Unavailable,
122				}, gax.Backoff{
123					Initial:    100 * time.Millisecond,
124					Max:        60000 * time.Millisecond,
125					Multiplier: 1.30,
126				})
127			}),
128		},
129	}
130}
131
132// JobControllerClient is a client for interacting with Cloud Dataproc API.
133//
134// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
135type JobControllerClient struct {
136	// The connection to the service.
137	conn *grpc.ClientConn
138
139	// The gRPC API client.
140	jobControllerClient dataprocpb.JobControllerClient
141
142	// The call options for this service.
143	CallOptions *JobControllerCallOptions
144
145	// The x-goog-* metadata to be sent with each request.
146	xGoogMetadata metadata.MD
147}
148
149// NewJobControllerClient creates a new job controller client.
150//
151// The JobController provides methods to manage jobs.
152func NewJobControllerClient(ctx context.Context, opts ...option.ClientOption) (*JobControllerClient, error) {
153	conn, err := transport.DialGRPC(ctx, append(defaultJobControllerClientOptions(), opts...)...)
154	if err != nil {
155		return nil, err
156	}
157	c := &JobControllerClient{
158		conn:        conn,
159		CallOptions: defaultJobControllerCallOptions(),
160
161		jobControllerClient: dataprocpb.NewJobControllerClient(conn),
162	}
163	c.setGoogleClientInfo()
164
165	return c, nil
166}
167
168// Connection returns the client's connection to the API service.
169func (c *JobControllerClient) Connection() *grpc.ClientConn {
170	return c.conn
171}
172
173// Close closes the connection to the API service. The user should invoke this when
174// the client is no longer required.
175func (c *JobControllerClient) Close() error {
176	return c.conn.Close()
177}
178
179// setGoogleClientInfo sets the name and version of the application in
180// the `x-goog-api-client` header passed on each request. Intended for
181// use by Google-written clients.
182func (c *JobControllerClient) setGoogleClientInfo(keyval ...string) {
183	kv := append([]string{"gl-go", versionGo()}, keyval...)
184	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
185	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
186}
187
188// SubmitJob submits a job to a cluster.
189func (c *JobControllerClient) SubmitJob(ctx context.Context, req *dataprocpb.SubmitJobRequest, opts ...gax.CallOption) (*dataprocpb.Job, error) {
190	ctx = insertMetadata(ctx, c.xGoogMetadata)
191	opts = append(c.CallOptions.SubmitJob[0:len(c.CallOptions.SubmitJob):len(c.CallOptions.SubmitJob)], opts...)
192	var resp *dataprocpb.Job
193	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
194		var err error
195		resp, err = c.jobControllerClient.SubmitJob(ctx, req, settings.GRPC...)
196		return err
197	}, opts...)
198	if err != nil {
199		return nil, err
200	}
201	return resp, nil
202}
203
204// GetJob gets the resource representation for a job in a project.
205func (c *JobControllerClient) GetJob(ctx context.Context, req *dataprocpb.GetJobRequest, opts ...gax.CallOption) (*dataprocpb.Job, error) {
206	ctx = insertMetadata(ctx, c.xGoogMetadata)
207	opts = append(c.CallOptions.GetJob[0:len(c.CallOptions.GetJob):len(c.CallOptions.GetJob)], opts...)
208	var resp *dataprocpb.Job
209	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
210		var err error
211		resp, err = c.jobControllerClient.GetJob(ctx, req, settings.GRPC...)
212		return err
213	}, opts...)
214	if err != nil {
215		return nil, err
216	}
217	return resp, nil
218}
219
220// ListJobs lists regions/{region}/jobs in a project.
221func (c *JobControllerClient) ListJobs(ctx context.Context, req *dataprocpb.ListJobsRequest, opts ...gax.CallOption) *JobIterator {
222	ctx = insertMetadata(ctx, c.xGoogMetadata)
223	opts = append(c.CallOptions.ListJobs[0:len(c.CallOptions.ListJobs):len(c.CallOptions.ListJobs)], opts...)
224	it := &JobIterator{}
225	req = proto.Clone(req).(*dataprocpb.ListJobsRequest)
226	it.InternalFetch = func(pageSize int, pageToken string) ([]*dataprocpb.Job, string, error) {
227		var resp *dataprocpb.ListJobsResponse
228		req.PageToken = pageToken
229		if pageSize > math.MaxInt32 {
230			req.PageSize = math.MaxInt32
231		} else {
232			req.PageSize = int32(pageSize)
233		}
234		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
235			var err error
236			resp, err = c.jobControllerClient.ListJobs(ctx, req, settings.GRPC...)
237			return err
238		}, opts...)
239		if err != nil {
240			return nil, "", err
241		}
242
243		it.Response = resp
244		return resp.Jobs, resp.NextPageToken, nil
245	}
246	fetch := func(pageSize int, pageToken string) (string, error) {
247		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
248		if err != nil {
249			return "", err
250		}
251		it.items = append(it.items, items...)
252		return nextPageToken, nil
253	}
254	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
255	it.pageInfo.MaxSize = int(req.PageSize)
256	it.pageInfo.Token = req.PageToken
257	return it
258}
259
260// UpdateJob updates a job in a project.
261func (c *JobControllerClient) UpdateJob(ctx context.Context, req *dataprocpb.UpdateJobRequest, opts ...gax.CallOption) (*dataprocpb.Job, error) {
262	ctx = insertMetadata(ctx, c.xGoogMetadata)
263	opts = append(c.CallOptions.UpdateJob[0:len(c.CallOptions.UpdateJob):len(c.CallOptions.UpdateJob)], opts...)
264	var resp *dataprocpb.Job
265	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
266		var err error
267		resp, err = c.jobControllerClient.UpdateJob(ctx, req, settings.GRPC...)
268		return err
269	}, opts...)
270	if err != nil {
271		return nil, err
272	}
273	return resp, nil
274}
275
276// CancelJob starts a job cancellation request. To access the job resource
277// after cancellation, call
278// regions/{region}/jobs.list (at /dataproc/docs/reference/rest/v1/projects.regions.jobs/list)
279// or
280// regions/{region}/jobs.get (at /dataproc/docs/reference/rest/v1/projects.regions.jobs/get).
281func (c *JobControllerClient) CancelJob(ctx context.Context, req *dataprocpb.CancelJobRequest, opts ...gax.CallOption) (*dataprocpb.Job, error) {
282	ctx = insertMetadata(ctx, c.xGoogMetadata)
283	opts = append(c.CallOptions.CancelJob[0:len(c.CallOptions.CancelJob):len(c.CallOptions.CancelJob)], opts...)
284	var resp *dataprocpb.Job
285	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
286		var err error
287		resp, err = c.jobControllerClient.CancelJob(ctx, req, settings.GRPC...)
288		return err
289	}, opts...)
290	if err != nil {
291		return nil, err
292	}
293	return resp, nil
294}
295
296// DeleteJob deletes the job from the project. If the job is active, the delete fails,
297// and the response returns FAILED_PRECONDITION.
298func (c *JobControllerClient) DeleteJob(ctx context.Context, req *dataprocpb.DeleteJobRequest, opts ...gax.CallOption) error {
299	ctx = insertMetadata(ctx, c.xGoogMetadata)
300	opts = append(c.CallOptions.DeleteJob[0:len(c.CallOptions.DeleteJob):len(c.CallOptions.DeleteJob)], opts...)
301	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
302		var err error
303		_, err = c.jobControllerClient.DeleteJob(ctx, req, settings.GRPC...)
304		return err
305	}, opts...)
306	return err
307}
308
309// JobIterator manages a stream of *dataprocpb.Job.
310type JobIterator struct {
311	items    []*dataprocpb.Job
312	pageInfo *iterator.PageInfo
313	nextFunc func() error
314
315	// Response is the raw response for the current page.
316	// It must be cast to the RPC response type.
317	// Calling Next() or InternalFetch() updates this value.
318	Response interface{}
319
320	// InternalFetch is for use by the Google Cloud Libraries only.
321	// It is not part of the stable interface of this package.
322	//
323	// InternalFetch returns results from a single call to the underlying RPC.
324	// The number of results is no greater than pageSize.
325	// If there are no more results, nextPageToken is empty and err is nil.
326	InternalFetch func(pageSize int, pageToken string) (results []*dataprocpb.Job, nextPageToken string, err error)
327}
328
329// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
330func (it *JobIterator) PageInfo() *iterator.PageInfo {
331	return it.pageInfo
332}
333
334// Next returns the next result. Its second return value is iterator.Done if there are no more
335// results. Once Next returns Done, all subsequent calls will return Done.
336func (it *JobIterator) Next() (*dataprocpb.Job, error) {
337	var item *dataprocpb.Job
338	if err := it.nextFunc(); err != nil {
339		return item, err
340	}
341	item = it.items[0]
342	it.items = it.items[1:]
343	return item, nil
344}
345
346func (it *JobIterator) bufLen() int {
347	return len(it.items)
348}
349
350func (it *JobIterator) takeBuf() interface{} {
351	b := it.items
352	it.items = nil
353	return b
354}
355