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