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 scheduler
18
19import (
20	"context"
21	"fmt"
22	"math"
23	"net/url"
24	"time"
25
26	gax "github.com/googleapis/gax-go/v2"
27	"google.golang.org/api/iterator"
28	"google.golang.org/api/option"
29	"google.golang.org/api/option/internaloption"
30	gtransport "google.golang.org/api/transport/grpc"
31	schedulerpb "google.golang.org/genproto/googleapis/cloud/scheduler/v1"
32	"google.golang.org/grpc"
33	"google.golang.org/grpc/codes"
34	"google.golang.org/grpc/metadata"
35	"google.golang.org/protobuf/proto"
36)
37
38var newCloudSchedulerClientHook clientHook
39
40// CloudSchedulerCallOptions contains the retry settings for each method of CloudSchedulerClient.
41type CloudSchedulerCallOptions struct {
42	ListJobs  []gax.CallOption
43	GetJob    []gax.CallOption
44	CreateJob []gax.CallOption
45	UpdateJob []gax.CallOption
46	DeleteJob []gax.CallOption
47	PauseJob  []gax.CallOption
48	ResumeJob []gax.CallOption
49	RunJob    []gax.CallOption
50}
51
52func defaultCloudSchedulerGRPCClientOptions() []option.ClientOption {
53	return []option.ClientOption{
54		internaloption.WithDefaultEndpoint("cloudscheduler.googleapis.com:443"),
55		internaloption.WithDefaultMTLSEndpoint("cloudscheduler.mtls.googleapis.com:443"),
56		internaloption.WithDefaultAudience("https://cloudscheduler.googleapis.com/"),
57		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
58		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
59		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
60			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
61	}
62}
63
64func defaultCloudSchedulerCallOptions() *CloudSchedulerCallOptions {
65	return &CloudSchedulerCallOptions{
66		ListJobs: []gax.CallOption{
67			gax.WithRetry(func() gax.Retryer {
68				return gax.OnCodes([]codes.Code{
69					codes.DeadlineExceeded,
70					codes.Unavailable,
71				}, gax.Backoff{
72					Initial:    100 * time.Millisecond,
73					Max:        60000 * time.Millisecond,
74					Multiplier: 1.30,
75				})
76			}),
77		},
78		GetJob: []gax.CallOption{
79			gax.WithRetry(func() gax.Retryer {
80				return gax.OnCodes([]codes.Code{
81					codes.DeadlineExceeded,
82					codes.Unavailable,
83				}, gax.Backoff{
84					Initial:    100 * time.Millisecond,
85					Max:        60000 * time.Millisecond,
86					Multiplier: 1.30,
87				})
88			}),
89		},
90		CreateJob: []gax.CallOption{},
91		UpdateJob: []gax.CallOption{},
92		DeleteJob: []gax.CallOption{
93			gax.WithRetry(func() gax.Retryer {
94				return gax.OnCodes([]codes.Code{
95					codes.DeadlineExceeded,
96					codes.Unavailable,
97				}, gax.Backoff{
98					Initial:    100 * time.Millisecond,
99					Max:        60000 * time.Millisecond,
100					Multiplier: 1.30,
101				})
102			}),
103		},
104		PauseJob:  []gax.CallOption{},
105		ResumeJob: []gax.CallOption{},
106		RunJob:    []gax.CallOption{},
107	}
108}
109
110// internalCloudSchedulerClient is an interface that defines the methods availaible from Cloud Scheduler API.
111type internalCloudSchedulerClient interface {
112	Close() error
113	setGoogleClientInfo(...string)
114	Connection() *grpc.ClientConn
115	ListJobs(context.Context, *schedulerpb.ListJobsRequest, ...gax.CallOption) *JobIterator
116	GetJob(context.Context, *schedulerpb.GetJobRequest, ...gax.CallOption) (*schedulerpb.Job, error)
117	CreateJob(context.Context, *schedulerpb.CreateJobRequest, ...gax.CallOption) (*schedulerpb.Job, error)
118	UpdateJob(context.Context, *schedulerpb.UpdateJobRequest, ...gax.CallOption) (*schedulerpb.Job, error)
119	DeleteJob(context.Context, *schedulerpb.DeleteJobRequest, ...gax.CallOption) error
120	PauseJob(context.Context, *schedulerpb.PauseJobRequest, ...gax.CallOption) (*schedulerpb.Job, error)
121	ResumeJob(context.Context, *schedulerpb.ResumeJobRequest, ...gax.CallOption) (*schedulerpb.Job, error)
122	RunJob(context.Context, *schedulerpb.RunJobRequest, ...gax.CallOption) (*schedulerpb.Job, error)
123}
124
125// CloudSchedulerClient is a client for interacting with Cloud Scheduler API.
126// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
127//
128// The Cloud Scheduler API allows external entities to reliably
129// schedule asynchronous jobs.
130type CloudSchedulerClient struct {
131	// The internal transport-dependent client.
132	internalClient internalCloudSchedulerClient
133
134	// The call options for this service.
135	CallOptions *CloudSchedulerCallOptions
136}
137
138// Wrapper methods routed to the internal client.
139
140// Close closes the connection to the API service. The user should invoke this when
141// the client is no longer required.
142func (c *CloudSchedulerClient) Close() error {
143	return c.internalClient.Close()
144}
145
146// setGoogleClientInfo sets the name and version of the application in
147// the `x-goog-api-client` header passed on each request. Intended for
148// use by Google-written clients.
149func (c *CloudSchedulerClient) setGoogleClientInfo(keyval ...string) {
150	c.internalClient.setGoogleClientInfo(keyval...)
151}
152
153// Connection returns a connection to the API service.
154//
155// Deprecated.
156func (c *CloudSchedulerClient) Connection() *grpc.ClientConn {
157	return c.internalClient.Connection()
158}
159
160// ListJobs lists jobs.
161func (c *CloudSchedulerClient) ListJobs(ctx context.Context, req *schedulerpb.ListJobsRequest, opts ...gax.CallOption) *JobIterator {
162	return c.internalClient.ListJobs(ctx, req, opts...)
163}
164
165// GetJob gets a job.
166func (c *CloudSchedulerClient) GetJob(ctx context.Context, req *schedulerpb.GetJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
167	return c.internalClient.GetJob(ctx, req, opts...)
168}
169
170// CreateJob creates a job.
171func (c *CloudSchedulerClient) CreateJob(ctx context.Context, req *schedulerpb.CreateJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
172	return c.internalClient.CreateJob(ctx, req, opts...)
173}
174
175// UpdateJob updates a job.
176//
177// If successful, the updated Job is returned. If the job does
178// not exist, NOT_FOUND is returned.
179//
180// If UpdateJob does not successfully return, it is possible for the
181// job to be in an Job.State.UPDATE_FAILED state. A job in this state may
182// not be executed. If this happens, retry the UpdateJob request
183// until a successful response is received.
184func (c *CloudSchedulerClient) UpdateJob(ctx context.Context, req *schedulerpb.UpdateJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
185	return c.internalClient.UpdateJob(ctx, req, opts...)
186}
187
188// DeleteJob deletes a job.
189func (c *CloudSchedulerClient) DeleteJob(ctx context.Context, req *schedulerpb.DeleteJobRequest, opts ...gax.CallOption) error {
190	return c.internalClient.DeleteJob(ctx, req, opts...)
191}
192
193// PauseJob pauses a job.
194//
195// If a job is paused then the system will stop executing the job
196// until it is re-enabled via ResumeJob. The
197// state of the job is stored in state; if paused it
198// will be set to Job.State.PAUSED. A job must be in Job.State.ENABLED
199// to be paused.
200func (c *CloudSchedulerClient) PauseJob(ctx context.Context, req *schedulerpb.PauseJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
201	return c.internalClient.PauseJob(ctx, req, opts...)
202}
203
204// ResumeJob resume a job.
205//
206// This method reenables a job after it has been Job.State.PAUSED. The
207// state of a job is stored in Job.state; after calling this method it
208// will be set to Job.State.ENABLED. A job must be in
209// Job.State.PAUSED to be resumed.
210func (c *CloudSchedulerClient) ResumeJob(ctx context.Context, req *schedulerpb.ResumeJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
211	return c.internalClient.ResumeJob(ctx, req, opts...)
212}
213
214// RunJob forces a job to run now.
215//
216// When this method is called, Cloud Scheduler will dispatch the job, even
217// if the job is already running.
218func (c *CloudSchedulerClient) RunJob(ctx context.Context, req *schedulerpb.RunJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
219	return c.internalClient.RunJob(ctx, req, opts...)
220}
221
222// cloudSchedulerGRPCClient is a client for interacting with Cloud Scheduler API over gRPC transport.
223//
224// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
225type cloudSchedulerGRPCClient struct {
226	// Connection pool of gRPC connections to the service.
227	connPool gtransport.ConnPool
228
229	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
230	disableDeadlines bool
231
232	// Points back to the CallOptions field of the containing CloudSchedulerClient
233	CallOptions **CloudSchedulerCallOptions
234
235	// The gRPC API client.
236	cloudSchedulerClient schedulerpb.CloudSchedulerClient
237
238	// The x-goog-* metadata to be sent with each request.
239	xGoogMetadata metadata.MD
240}
241
242// NewCloudSchedulerClient creates a new cloud scheduler client based on gRPC.
243// The returned client must be Closed when it is done being used to clean up its underlying connections.
244//
245// The Cloud Scheduler API allows external entities to reliably
246// schedule asynchronous jobs.
247func NewCloudSchedulerClient(ctx context.Context, opts ...option.ClientOption) (*CloudSchedulerClient, error) {
248	clientOpts := defaultCloudSchedulerGRPCClientOptions()
249	if newCloudSchedulerClientHook != nil {
250		hookOpts, err := newCloudSchedulerClientHook(ctx, clientHookParams{})
251		if err != nil {
252			return nil, err
253		}
254		clientOpts = append(clientOpts, hookOpts...)
255	}
256
257	disableDeadlines, err := checkDisableDeadlines()
258	if err != nil {
259		return nil, err
260	}
261
262	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
263	if err != nil {
264		return nil, err
265	}
266	client := CloudSchedulerClient{CallOptions: defaultCloudSchedulerCallOptions()}
267
268	c := &cloudSchedulerGRPCClient{
269		connPool:             connPool,
270		disableDeadlines:     disableDeadlines,
271		cloudSchedulerClient: schedulerpb.NewCloudSchedulerClient(connPool),
272		CallOptions:          &client.CallOptions,
273	}
274	c.setGoogleClientInfo()
275
276	client.internalClient = c
277
278	return &client, nil
279}
280
281// Connection returns a connection to the API service.
282//
283// Deprecated.
284func (c *cloudSchedulerGRPCClient) Connection() *grpc.ClientConn {
285	return c.connPool.Conn()
286}
287
288// setGoogleClientInfo sets the name and version of the application in
289// the `x-goog-api-client` header passed on each request. Intended for
290// use by Google-written clients.
291func (c *cloudSchedulerGRPCClient) setGoogleClientInfo(keyval ...string) {
292	kv := append([]string{"gl-go", versionGo()}, keyval...)
293	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
294	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
295}
296
297// Close closes the connection to the API service. The user should invoke this when
298// the client is no longer required.
299func (c *cloudSchedulerGRPCClient) Close() error {
300	return c.connPool.Close()
301}
302
303func (c *cloudSchedulerGRPCClient) ListJobs(ctx context.Context, req *schedulerpb.ListJobsRequest, opts ...gax.CallOption) *JobIterator {
304	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
305	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
306	opts = append((*c.CallOptions).ListJobs[0:len((*c.CallOptions).ListJobs):len((*c.CallOptions).ListJobs)], opts...)
307	it := &JobIterator{}
308	req = proto.Clone(req).(*schedulerpb.ListJobsRequest)
309	it.InternalFetch = func(pageSize int, pageToken string) ([]*schedulerpb.Job, string, error) {
310		var resp *schedulerpb.ListJobsResponse
311		req.PageToken = pageToken
312		if pageSize > math.MaxInt32 {
313			req.PageSize = math.MaxInt32
314		} else {
315			req.PageSize = int32(pageSize)
316		}
317		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
318			var err error
319			resp, err = c.cloudSchedulerClient.ListJobs(ctx, req, settings.GRPC...)
320			return err
321		}, opts...)
322		if err != nil {
323			return nil, "", err
324		}
325
326		it.Response = resp
327		return resp.GetJobs(), resp.GetNextPageToken(), nil
328	}
329	fetch := func(pageSize int, pageToken string) (string, error) {
330		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
331		if err != nil {
332			return "", err
333		}
334		it.items = append(it.items, items...)
335		return nextPageToken, nil
336	}
337	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
338	it.pageInfo.MaxSize = int(req.GetPageSize())
339	it.pageInfo.Token = req.GetPageToken()
340	return it
341}
342
343func (c *cloudSchedulerGRPCClient) GetJob(ctx context.Context, req *schedulerpb.GetJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
344	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
345		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
346		defer cancel()
347		ctx = cctx
348	}
349	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
350	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
351	opts = append((*c.CallOptions).GetJob[0:len((*c.CallOptions).GetJob):len((*c.CallOptions).GetJob)], opts...)
352	var resp *schedulerpb.Job
353	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
354		var err error
355		resp, err = c.cloudSchedulerClient.GetJob(ctx, req, settings.GRPC...)
356		return err
357	}, opts...)
358	if err != nil {
359		return nil, err
360	}
361	return resp, nil
362}
363
364func (c *cloudSchedulerGRPCClient) CreateJob(ctx context.Context, req *schedulerpb.CreateJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
365	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
366		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
367		defer cancel()
368		ctx = cctx
369	}
370	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
371	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
372	opts = append((*c.CallOptions).CreateJob[0:len((*c.CallOptions).CreateJob):len((*c.CallOptions).CreateJob)], opts...)
373	var resp *schedulerpb.Job
374	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
375		var err error
376		resp, err = c.cloudSchedulerClient.CreateJob(ctx, req, settings.GRPC...)
377		return err
378	}, opts...)
379	if err != nil {
380		return nil, err
381	}
382	return resp, nil
383}
384
385func (c *cloudSchedulerGRPCClient) UpdateJob(ctx context.Context, req *schedulerpb.UpdateJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
386	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
387		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
388		defer cancel()
389		ctx = cctx
390	}
391	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "job.name", url.QueryEscape(req.GetJob().GetName())))
392	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
393	opts = append((*c.CallOptions).UpdateJob[0:len((*c.CallOptions).UpdateJob):len((*c.CallOptions).UpdateJob)], opts...)
394	var resp *schedulerpb.Job
395	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
396		var err error
397		resp, err = c.cloudSchedulerClient.UpdateJob(ctx, req, settings.GRPC...)
398		return err
399	}, opts...)
400	if err != nil {
401		return nil, err
402	}
403	return resp, nil
404}
405
406func (c *cloudSchedulerGRPCClient) DeleteJob(ctx context.Context, req *schedulerpb.DeleteJobRequest, opts ...gax.CallOption) error {
407	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
408		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
409		defer cancel()
410		ctx = cctx
411	}
412	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
413	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
414	opts = append((*c.CallOptions).DeleteJob[0:len((*c.CallOptions).DeleteJob):len((*c.CallOptions).DeleteJob)], opts...)
415	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
416		var err error
417		_, err = c.cloudSchedulerClient.DeleteJob(ctx, req, settings.GRPC...)
418		return err
419	}, opts...)
420	return err
421}
422
423func (c *cloudSchedulerGRPCClient) PauseJob(ctx context.Context, req *schedulerpb.PauseJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
424	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
425		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
426		defer cancel()
427		ctx = cctx
428	}
429	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
430	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
431	opts = append((*c.CallOptions).PauseJob[0:len((*c.CallOptions).PauseJob):len((*c.CallOptions).PauseJob)], opts...)
432	var resp *schedulerpb.Job
433	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
434		var err error
435		resp, err = c.cloudSchedulerClient.PauseJob(ctx, req, settings.GRPC...)
436		return err
437	}, opts...)
438	if err != nil {
439		return nil, err
440	}
441	return resp, nil
442}
443
444func (c *cloudSchedulerGRPCClient) ResumeJob(ctx context.Context, req *schedulerpb.ResumeJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
445	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
446		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
447		defer cancel()
448		ctx = cctx
449	}
450	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
451	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
452	opts = append((*c.CallOptions).ResumeJob[0:len((*c.CallOptions).ResumeJob):len((*c.CallOptions).ResumeJob)], opts...)
453	var resp *schedulerpb.Job
454	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
455		var err error
456		resp, err = c.cloudSchedulerClient.ResumeJob(ctx, req, settings.GRPC...)
457		return err
458	}, opts...)
459	if err != nil {
460		return nil, err
461	}
462	return resp, nil
463}
464
465func (c *cloudSchedulerGRPCClient) RunJob(ctx context.Context, req *schedulerpb.RunJobRequest, opts ...gax.CallOption) (*schedulerpb.Job, error) {
466	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
467		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
468		defer cancel()
469		ctx = cctx
470	}
471	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
472	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
473	opts = append((*c.CallOptions).RunJob[0:len((*c.CallOptions).RunJob):len((*c.CallOptions).RunJob)], opts...)
474	var resp *schedulerpb.Job
475	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
476		var err error
477		resp, err = c.cloudSchedulerClient.RunJob(ctx, req, settings.GRPC...)
478		return err
479	}, opts...)
480	if err != nil {
481		return nil, err
482	}
483	return resp, nil
484}
485
486// JobIterator manages a stream of *schedulerpb.Job.
487type JobIterator struct {
488	items    []*schedulerpb.Job
489	pageInfo *iterator.PageInfo
490	nextFunc func() error
491
492	// Response is the raw response for the current page.
493	// It must be cast to the RPC response type.
494	// Calling Next() or InternalFetch() updates this value.
495	Response interface{}
496
497	// InternalFetch is for use by the Google Cloud Libraries only.
498	// It is not part of the stable interface of this package.
499	//
500	// InternalFetch returns results from a single call to the underlying RPC.
501	// The number of results is no greater than pageSize.
502	// If there are no more results, nextPageToken is empty and err is nil.
503	InternalFetch func(pageSize int, pageToken string) (results []*schedulerpb.Job, nextPageToken string, err error)
504}
505
506// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
507func (it *JobIterator) PageInfo() *iterator.PageInfo {
508	return it.pageInfo
509}
510
511// Next returns the next result. Its second return value is iterator.Done if there are no more
512// results. Once Next returns Done, all subsequent calls will return Done.
513func (it *JobIterator) Next() (*schedulerpb.Job, error) {
514	var item *schedulerpb.Job
515	if err := it.nextFunc(); err != nil {
516		return item, err
517	}
518	item = it.items[0]
519	it.items = it.items[1:]
520	return item, nil
521}
522
523func (it *JobIterator) bufLen() int {
524	return len(it.items)
525}
526
527func (it *JobIterator) takeBuf() interface{} {
528	b := it.items
529	it.items = nil
530	return b
531}
532