1// Copyright 2020 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 cloudtasks
18
19import (
20	"context"
21	"fmt"
22	"math"
23	"net/url"
24	"time"
25
26	"github.com/golang/protobuf/proto"
27	gax "github.com/googleapis/gax-go/v2"
28	"google.golang.org/api/iterator"
29	"google.golang.org/api/option"
30	gtransport "google.golang.org/api/transport/grpc"
31	taskspb "google.golang.org/genproto/googleapis/cloud/tasks/v2beta3"
32	iampb "google.golang.org/genproto/googleapis/iam/v1"
33	"google.golang.org/grpc"
34	"google.golang.org/grpc/codes"
35	"google.golang.org/grpc/metadata"
36)
37
38var newClientHook clientHook
39
40// CallOptions contains the retry settings for each method of Client.
41type CallOptions struct {
42	ListQueues         []gax.CallOption
43	GetQueue           []gax.CallOption
44	CreateQueue        []gax.CallOption
45	UpdateQueue        []gax.CallOption
46	DeleteQueue        []gax.CallOption
47	PurgeQueue         []gax.CallOption
48	PauseQueue         []gax.CallOption
49	ResumeQueue        []gax.CallOption
50	GetIamPolicy       []gax.CallOption
51	SetIamPolicy       []gax.CallOption
52	TestIamPermissions []gax.CallOption
53	ListTasks          []gax.CallOption
54	GetTask            []gax.CallOption
55	CreateTask         []gax.CallOption
56	DeleteTask         []gax.CallOption
57	RunTask            []gax.CallOption
58}
59
60func defaultClientOptions() []option.ClientOption {
61	return []option.ClientOption{
62		option.WithEndpoint("cloudtasks.googleapis.com:443"),
63		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
64		option.WithScopes(DefaultAuthScopes()...),
65		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
66			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
67	}
68}
69
70func defaultCallOptions() *CallOptions {
71	return &CallOptions{
72		ListQueues: []gax.CallOption{
73			gax.WithRetry(func() gax.Retryer {
74				return gax.OnCodes([]codes.Code{
75					codes.DeadlineExceeded,
76					codes.Unavailable,
77				}, gax.Backoff{
78					Initial:    100 * time.Millisecond,
79					Max:        10000 * time.Millisecond,
80					Multiplier: 1.30,
81				})
82			}),
83		},
84		GetQueue: []gax.CallOption{
85			gax.WithRetry(func() gax.Retryer {
86				return gax.OnCodes([]codes.Code{
87					codes.DeadlineExceeded,
88					codes.Unavailable,
89				}, gax.Backoff{
90					Initial:    100 * time.Millisecond,
91					Max:        10000 * time.Millisecond,
92					Multiplier: 1.30,
93				})
94			}),
95		},
96		CreateQueue: []gax.CallOption{},
97		UpdateQueue: []gax.CallOption{},
98		DeleteQueue: []gax.CallOption{
99			gax.WithRetry(func() gax.Retryer {
100				return gax.OnCodes([]codes.Code{
101					codes.DeadlineExceeded,
102					codes.Unavailable,
103				}, gax.Backoff{
104					Initial:    100 * time.Millisecond,
105					Max:        10000 * time.Millisecond,
106					Multiplier: 1.30,
107				})
108			}),
109		},
110		PurgeQueue:  []gax.CallOption{},
111		PauseQueue:  []gax.CallOption{},
112		ResumeQueue: []gax.CallOption{},
113		GetIamPolicy: []gax.CallOption{
114			gax.WithRetry(func() gax.Retryer {
115				return gax.OnCodes([]codes.Code{
116					codes.DeadlineExceeded,
117					codes.Unavailable,
118				}, gax.Backoff{
119					Initial:    100 * time.Millisecond,
120					Max:        10000 * time.Millisecond,
121					Multiplier: 1.30,
122				})
123			}),
124		},
125		SetIamPolicy: []gax.CallOption{},
126		TestIamPermissions: []gax.CallOption{
127			gax.WithRetry(func() gax.Retryer {
128				return gax.OnCodes([]codes.Code{
129					codes.DeadlineExceeded,
130					codes.Unavailable,
131				}, gax.Backoff{
132					Initial:    100 * time.Millisecond,
133					Max:        10000 * time.Millisecond,
134					Multiplier: 1.30,
135				})
136			}),
137		},
138		ListTasks: []gax.CallOption{
139			gax.WithRetry(func() gax.Retryer {
140				return gax.OnCodes([]codes.Code{
141					codes.DeadlineExceeded,
142					codes.Unavailable,
143				}, gax.Backoff{
144					Initial:    100 * time.Millisecond,
145					Max:        10000 * time.Millisecond,
146					Multiplier: 1.30,
147				})
148			}),
149		},
150		GetTask: []gax.CallOption{
151			gax.WithRetry(func() gax.Retryer {
152				return gax.OnCodes([]codes.Code{
153					codes.DeadlineExceeded,
154					codes.Unavailable,
155				}, gax.Backoff{
156					Initial:    100 * time.Millisecond,
157					Max:        10000 * time.Millisecond,
158					Multiplier: 1.30,
159				})
160			}),
161		},
162		CreateTask: []gax.CallOption{},
163		DeleteTask: []gax.CallOption{
164			gax.WithRetry(func() gax.Retryer {
165				return gax.OnCodes([]codes.Code{
166					codes.DeadlineExceeded,
167					codes.Unavailable,
168				}, gax.Backoff{
169					Initial:    100 * time.Millisecond,
170					Max:        10000 * time.Millisecond,
171					Multiplier: 1.30,
172				})
173			}),
174		},
175		RunTask: []gax.CallOption{},
176	}
177}
178
179// Client is a client for interacting with Cloud Tasks API.
180//
181// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
182type Client struct {
183	// Connection pool of gRPC connections to the service.
184	connPool gtransport.ConnPool
185
186	// The gRPC API client.
187	client taskspb.CloudTasksClient
188
189	// The call options for this service.
190	CallOptions *CallOptions
191
192	// The x-goog-* metadata to be sent with each request.
193	xGoogMetadata metadata.MD
194}
195
196// NewClient creates a new cloud tasks client.
197//
198// Cloud Tasks allows developers to manage the execution of background
199// work in their applications.
200func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
201	clientOpts := defaultClientOptions()
202
203	if newClientHook != nil {
204		hookOpts, err := newClientHook(ctx, clientHookParams{})
205		if err != nil {
206			return nil, err
207		}
208		clientOpts = append(clientOpts, hookOpts...)
209	}
210
211	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
212	if err != nil {
213		return nil, err
214	}
215	c := &Client{
216		connPool:    connPool,
217		CallOptions: defaultCallOptions(),
218
219		client: taskspb.NewCloudTasksClient(connPool),
220	}
221	c.setGoogleClientInfo()
222
223	return c, nil
224}
225
226// Connection returns a connection to the API service.
227//
228// Deprecated.
229func (c *Client) Connection() *grpc.ClientConn {
230	return c.connPool.Conn()
231}
232
233// Close closes the connection to the API service. The user should invoke this when
234// the client is no longer required.
235func (c *Client) Close() error {
236	return c.connPool.Close()
237}
238
239// setGoogleClientInfo sets the name and version of the application in
240// the `x-goog-api-client` header passed on each request. Intended for
241// use by Google-written clients.
242func (c *Client) setGoogleClientInfo(keyval ...string) {
243	kv := append([]string{"gl-go", versionGo()}, keyval...)
244	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
245	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
246}
247
248// ListQueues lists queues.
249//
250// Queues are returned in lexicographical order.
251func (c *Client) ListQueues(ctx context.Context, req *taskspb.ListQueuesRequest, opts ...gax.CallOption) *QueueIterator {
252	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
253	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
254	opts = append(c.CallOptions.ListQueues[0:len(c.CallOptions.ListQueues):len(c.CallOptions.ListQueues)], opts...)
255	it := &QueueIterator{}
256	req = proto.Clone(req).(*taskspb.ListQueuesRequest)
257	it.InternalFetch = func(pageSize int, pageToken string) ([]*taskspb.Queue, string, error) {
258		var resp *taskspb.ListQueuesResponse
259		req.PageToken = pageToken
260		if pageSize > math.MaxInt32 {
261			req.PageSize = math.MaxInt32
262		} else {
263			req.PageSize = int32(pageSize)
264		}
265		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
266			var err error
267			resp, err = c.client.ListQueues(ctx, req, settings.GRPC...)
268			return err
269		}, opts...)
270		if err != nil {
271			return nil, "", err
272		}
273
274		it.Response = resp
275		return resp.Queues, resp.NextPageToken, nil
276	}
277	fetch := func(pageSize int, pageToken string) (string, error) {
278		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
279		if err != nil {
280			return "", err
281		}
282		it.items = append(it.items, items...)
283		return nextPageToken, nil
284	}
285	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
286	it.pageInfo.MaxSize = int(req.PageSize)
287	it.pageInfo.Token = req.PageToken
288	return it
289}
290
291// GetQueue gets a queue.
292func (c *Client) GetQueue(ctx context.Context, req *taskspb.GetQueueRequest, opts ...gax.CallOption) (*taskspb.Queue, error) {
293	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
294	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
295	opts = append(c.CallOptions.GetQueue[0:len(c.CallOptions.GetQueue):len(c.CallOptions.GetQueue)], opts...)
296	var resp *taskspb.Queue
297	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
298		var err error
299		resp, err = c.client.GetQueue(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// CreateQueue creates a queue.
309//
310// Queues created with this method allow tasks to live for a maximum of 31
311// days. After a task is 31 days old, the task will be deleted regardless of whether
312// it was dispatched or not.
313//
314// WARNING: Using this method may have unintended side effects if you are
315// using an App Engine queue.yaml or queue.xml file to manage your queues.
316// Read
317// Overview of Queue Management and
318// queue.yaml (at https://cloud.google.com/tasks/docs/queue-yaml) before using
319// this method.
320func (c *Client) CreateQueue(ctx context.Context, req *taskspb.CreateQueueRequest, opts ...gax.CallOption) (*taskspb.Queue, error) {
321	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
322	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
323	opts = append(c.CallOptions.CreateQueue[0:len(c.CallOptions.CreateQueue):len(c.CallOptions.CreateQueue)], opts...)
324	var resp *taskspb.Queue
325	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
326		var err error
327		resp, err = c.client.CreateQueue(ctx, req, settings.GRPC...)
328		return err
329	}, opts...)
330	if err != nil {
331		return nil, err
332	}
333	return resp, nil
334}
335
336// UpdateQueue updates a queue.
337//
338// This method creates the queue if it does not exist and updates
339// the queue if it does exist.
340//
341// Queues created with this method allow tasks to live for a maximum of 31
342// days. After a task is 31 days old, the task will be deleted regardless of whether
343// it was dispatched or not.
344//
345// WARNING: Using this method may have unintended side effects if you are
346// using an App Engine queue.yaml or queue.xml file to manage your queues.
347// Read
348// Overview of Queue Management and
349// queue.yaml (at https://cloud.google.com/tasks/docs/queue-yaml) before using
350// this method.
351func (c *Client) UpdateQueue(ctx context.Context, req *taskspb.UpdateQueueRequest, opts ...gax.CallOption) (*taskspb.Queue, error) {
352	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "queue.name", url.QueryEscape(req.GetQueue().GetName())))
353	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
354	opts = append(c.CallOptions.UpdateQueue[0:len(c.CallOptions.UpdateQueue):len(c.CallOptions.UpdateQueue)], opts...)
355	var resp *taskspb.Queue
356	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
357		var err error
358		resp, err = c.client.UpdateQueue(ctx, req, settings.GRPC...)
359		return err
360	}, opts...)
361	if err != nil {
362		return nil, err
363	}
364	return resp, nil
365}
366
367// DeleteQueue deletes a queue.
368//
369// This command will delete the queue even if it has tasks in it.
370//
371// Note: If you delete a queue, a queue with the same name can’t be created
372// for 7 days.
373//
374// WARNING: Using this method may have unintended side effects if you are
375// using an App Engine queue.yaml or queue.xml file to manage your queues.
376// Read
377// Overview of Queue Management and
378// queue.yaml (at https://cloud.google.com/tasks/docs/queue-yaml) before using
379// this method.
380func (c *Client) DeleteQueue(ctx context.Context, req *taskspb.DeleteQueueRequest, opts ...gax.CallOption) error {
381	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
382	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
383	opts = append(c.CallOptions.DeleteQueue[0:len(c.CallOptions.DeleteQueue):len(c.CallOptions.DeleteQueue)], opts...)
384	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
385		var err error
386		_, err = c.client.DeleteQueue(ctx, req, settings.GRPC...)
387		return err
388	}, opts...)
389	return err
390}
391
392// PurgeQueue purges a queue by deleting all of its tasks.
393//
394// All tasks created before this method is called are permanently deleted.
395//
396// Purge operations can take up to one minute to take effect. Tasks
397// might be dispatched before the purge takes effect. A purge is irreversible.
398func (c *Client) PurgeQueue(ctx context.Context, req *taskspb.PurgeQueueRequest, opts ...gax.CallOption) (*taskspb.Queue, error) {
399	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
400	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
401	opts = append(c.CallOptions.PurgeQueue[0:len(c.CallOptions.PurgeQueue):len(c.CallOptions.PurgeQueue)], opts...)
402	var resp *taskspb.Queue
403	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
404		var err error
405		resp, err = c.client.PurgeQueue(ctx, req, settings.GRPC...)
406		return err
407	}, opts...)
408	if err != nil {
409		return nil, err
410	}
411	return resp, nil
412}
413
414// PauseQueue pauses the queue.
415//
416// If a queue is paused then the system will stop dispatching tasks
417// until the queue is resumed via
418// ResumeQueue. Tasks can still be added
419// when the queue is paused. A queue is paused if its
420// state is PAUSED.
421func (c *Client) PauseQueue(ctx context.Context, req *taskspb.PauseQueueRequest, opts ...gax.CallOption) (*taskspb.Queue, error) {
422	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
423	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
424	opts = append(c.CallOptions.PauseQueue[0:len(c.CallOptions.PauseQueue):len(c.CallOptions.PauseQueue)], opts...)
425	var resp *taskspb.Queue
426	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
427		var err error
428		resp, err = c.client.PauseQueue(ctx, req, settings.GRPC...)
429		return err
430	}, opts...)
431	if err != nil {
432		return nil, err
433	}
434	return resp, nil
435}
436
437// ResumeQueue resume a queue.
438//
439// This method resumes a queue after it has been
440// PAUSED or
441// DISABLED. The state of a queue is stored
442// in the queue’s state; after calling this method it
443// will be set to RUNNING.
444//
445// WARNING: Resuming many high-QPS queues at the same time can
446// lead to target overloading. If you are resuming high-QPS
447// queues, follow the 500/50/5 pattern described in
448// Managing Cloud Tasks Scaling
449// Risks (at https://cloud.google.com/tasks/docs/manage-cloud-task-scaling).
450func (c *Client) ResumeQueue(ctx context.Context, req *taskspb.ResumeQueueRequest, opts ...gax.CallOption) (*taskspb.Queue, error) {
451	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
452	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
453	opts = append(c.CallOptions.ResumeQueue[0:len(c.CallOptions.ResumeQueue):len(c.CallOptions.ResumeQueue)], opts...)
454	var resp *taskspb.Queue
455	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
456		var err error
457		resp, err = c.client.ResumeQueue(ctx, req, settings.GRPC...)
458		return err
459	}, opts...)
460	if err != nil {
461		return nil, err
462	}
463	return resp, nil
464}
465
466// GetIamPolicy gets the access control policy for a Queue.
467// Returns an empty policy if the resource exists and does not have a policy
468// set.
469//
470// Authorization requires the following
471// Google IAM (at https://cloud.google.com/iam) permission on the specified
472// resource parent:
473//
474//   cloudtasks.queues.getIamPolicy
475func (c *Client) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
476	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource())))
477	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
478	opts = append(c.CallOptions.GetIamPolicy[0:len(c.CallOptions.GetIamPolicy):len(c.CallOptions.GetIamPolicy)], opts...)
479	var resp *iampb.Policy
480	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
481		var err error
482		resp, err = c.client.GetIamPolicy(ctx, req, settings.GRPC...)
483		return err
484	}, opts...)
485	if err != nil {
486		return nil, err
487	}
488	return resp, nil
489}
490
491// SetIamPolicy sets the access control policy for a Queue. Replaces any existing
492// policy.
493//
494// Note: The Cloud Console does not check queue-level IAM permissions yet.
495// Project-level permissions are required to use the Cloud Console.
496//
497// Authorization requires the following
498// Google IAM (at https://cloud.google.com/iam) permission on the specified
499// resource parent:
500//
501//   cloudtasks.queues.setIamPolicy
502func (c *Client) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
503	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource())))
504	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
505	opts = append(c.CallOptions.SetIamPolicy[0:len(c.CallOptions.SetIamPolicy):len(c.CallOptions.SetIamPolicy)], opts...)
506	var resp *iampb.Policy
507	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
508		var err error
509		resp, err = c.client.SetIamPolicy(ctx, req, settings.GRPC...)
510		return err
511	}, opts...)
512	if err != nil {
513		return nil, err
514	}
515	return resp, nil
516}
517
518// TestIamPermissions returns permissions that a caller has on a Queue.
519// If the resource does not exist, this will return an empty set of
520// permissions, not a NOT_FOUND error.
521//
522// Note: This operation is designed to be used for building permission-aware
523// UIs and command-line tools, not for authorization checking. This operation
524// may “fail open” without warning.
525func (c *Client) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
526	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource())))
527	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
528	opts = append(c.CallOptions.TestIamPermissions[0:len(c.CallOptions.TestIamPermissions):len(c.CallOptions.TestIamPermissions)], opts...)
529	var resp *iampb.TestIamPermissionsResponse
530	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
531		var err error
532		resp, err = c.client.TestIamPermissions(ctx, req, settings.GRPC...)
533		return err
534	}, opts...)
535	if err != nil {
536		return nil, err
537	}
538	return resp, nil
539}
540
541// ListTasks lists the tasks in a queue.
542//
543// By default, only the BASIC view is retrieved
544// due to performance considerations;
545// response_view controls the
546// subset of information which is returned.
547//
548// The tasks may be returned in any order. The ordering may change at any
549// time.
550func (c *Client) ListTasks(ctx context.Context, req *taskspb.ListTasksRequest, opts ...gax.CallOption) *TaskIterator {
551	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
552	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
553	opts = append(c.CallOptions.ListTasks[0:len(c.CallOptions.ListTasks):len(c.CallOptions.ListTasks)], opts...)
554	it := &TaskIterator{}
555	req = proto.Clone(req).(*taskspb.ListTasksRequest)
556	it.InternalFetch = func(pageSize int, pageToken string) ([]*taskspb.Task, string, error) {
557		var resp *taskspb.ListTasksResponse
558		req.PageToken = pageToken
559		if pageSize > math.MaxInt32 {
560			req.PageSize = math.MaxInt32
561		} else {
562			req.PageSize = int32(pageSize)
563		}
564		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
565			var err error
566			resp, err = c.client.ListTasks(ctx, req, settings.GRPC...)
567			return err
568		}, opts...)
569		if err != nil {
570			return nil, "", err
571		}
572
573		it.Response = resp
574		return resp.Tasks, resp.NextPageToken, nil
575	}
576	fetch := func(pageSize int, pageToken string) (string, error) {
577		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
578		if err != nil {
579			return "", err
580		}
581		it.items = append(it.items, items...)
582		return nextPageToken, nil
583	}
584	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
585	it.pageInfo.MaxSize = int(req.PageSize)
586	it.pageInfo.Token = req.PageToken
587	return it
588}
589
590// GetTask gets a task.
591func (c *Client) GetTask(ctx context.Context, req *taskspb.GetTaskRequest, opts ...gax.CallOption) (*taskspb.Task, error) {
592	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
593	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
594	opts = append(c.CallOptions.GetTask[0:len(c.CallOptions.GetTask):len(c.CallOptions.GetTask)], opts...)
595	var resp *taskspb.Task
596	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
597		var err error
598		resp, err = c.client.GetTask(ctx, req, settings.GRPC...)
599		return err
600	}, opts...)
601	if err != nil {
602		return nil, err
603	}
604	return resp, nil
605}
606
607// CreateTask creates a task and adds it to a queue.
608//
609// Tasks cannot be updated after creation; there is no UpdateTask command.
610//
611//   The maximum task size is 100KB.
612func (c *Client) CreateTask(ctx context.Context, req *taskspb.CreateTaskRequest, opts ...gax.CallOption) (*taskspb.Task, error) {
613	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
614	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
615	opts = append(c.CallOptions.CreateTask[0:len(c.CallOptions.CreateTask):len(c.CallOptions.CreateTask)], opts...)
616	var resp *taskspb.Task
617	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
618		var err error
619		resp, err = c.client.CreateTask(ctx, req, settings.GRPC...)
620		return err
621	}, opts...)
622	if err != nil {
623		return nil, err
624	}
625	return resp, nil
626}
627
628// DeleteTask deletes a task.
629//
630// A task can be deleted if it is scheduled or dispatched. A task
631// cannot be deleted if it has executed successfully or permanently
632// failed.
633func (c *Client) DeleteTask(ctx context.Context, req *taskspb.DeleteTaskRequest, opts ...gax.CallOption) error {
634	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
635	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
636	opts = append(c.CallOptions.DeleteTask[0:len(c.CallOptions.DeleteTask):len(c.CallOptions.DeleteTask)], opts...)
637	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
638		var err error
639		_, err = c.client.DeleteTask(ctx, req, settings.GRPC...)
640		return err
641	}, opts...)
642	return err
643}
644
645// RunTask forces a task to run now.
646//
647// When this method is called, Cloud Tasks will dispatch the task, even if
648// the task is already running, the queue has reached its RateLimits or
649// is PAUSED.
650//
651// This command is meant to be used for manual debugging. For
652// example, RunTask can be used to retry a failed
653// task after a fix has been made or to manually force a task to be
654// dispatched now.
655//
656// The dispatched task is returned. That is, the task that is returned
657// contains the status after the task is dispatched but
658// before the task is received by its target.
659//
660// If Cloud Tasks receives a successful response from the task’s
661// target, then the task will be deleted; otherwise the task’s
662// schedule_time will be reset to the time that
663// RunTask was called plus the retry delay specified
664// in the queue’s RetryConfig.
665//
666// RunTask returns
667// NOT_FOUND when it is called on a
668// task that has already succeeded or permanently failed.
669func (c *Client) RunTask(ctx context.Context, req *taskspb.RunTaskRequest, opts ...gax.CallOption) (*taskspb.Task, error) {
670	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
671	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
672	opts = append(c.CallOptions.RunTask[0:len(c.CallOptions.RunTask):len(c.CallOptions.RunTask)], opts...)
673	var resp *taskspb.Task
674	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
675		var err error
676		resp, err = c.client.RunTask(ctx, req, settings.GRPC...)
677		return err
678	}, opts...)
679	if err != nil {
680		return nil, err
681	}
682	return resp, nil
683}
684
685// QueueIterator manages a stream of *taskspb.Queue.
686type QueueIterator struct {
687	items    []*taskspb.Queue
688	pageInfo *iterator.PageInfo
689	nextFunc func() error
690
691	// Response is the raw response for the current page.
692	// It must be cast to the RPC response type.
693	// Calling Next() or InternalFetch() updates this value.
694	Response interface{}
695
696	// InternalFetch is for use by the Google Cloud Libraries only.
697	// It is not part of the stable interface of this package.
698	//
699	// InternalFetch returns results from a single call to the underlying RPC.
700	// The number of results is no greater than pageSize.
701	// If there are no more results, nextPageToken is empty and err is nil.
702	InternalFetch func(pageSize int, pageToken string) (results []*taskspb.Queue, nextPageToken string, err error)
703}
704
705// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
706func (it *QueueIterator) PageInfo() *iterator.PageInfo {
707	return it.pageInfo
708}
709
710// Next returns the next result. Its second return value is iterator.Done if there are no more
711// results. Once Next returns Done, all subsequent calls will return Done.
712func (it *QueueIterator) Next() (*taskspb.Queue, error) {
713	var item *taskspb.Queue
714	if err := it.nextFunc(); err != nil {
715		return item, err
716	}
717	item = it.items[0]
718	it.items = it.items[1:]
719	return item, nil
720}
721
722func (it *QueueIterator) bufLen() int {
723	return len(it.items)
724}
725
726func (it *QueueIterator) takeBuf() interface{} {
727	b := it.items
728	it.items = nil
729	return b
730}
731
732// TaskIterator manages a stream of *taskspb.Task.
733type TaskIterator struct {
734	items    []*taskspb.Task
735	pageInfo *iterator.PageInfo
736	nextFunc func() error
737
738	// Response is the raw response for the current page.
739	// It must be cast to the RPC response type.
740	// Calling Next() or InternalFetch() updates this value.
741	Response interface{}
742
743	// InternalFetch is for use by the Google Cloud Libraries only.
744	// It is not part of the stable interface of this package.
745	//
746	// InternalFetch returns results from a single call to the underlying RPC.
747	// The number of results is no greater than pageSize.
748	// If there are no more results, nextPageToken is empty and err is nil.
749	InternalFetch func(pageSize int, pageToken string) (results []*taskspb.Task, nextPageToken string, err error)
750}
751
752// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
753func (it *TaskIterator) PageInfo() *iterator.PageInfo {
754	return it.pageInfo
755}
756
757// Next returns the next result. Its second return value is iterator.Done if there are no more
758// results. Once Next returns Done, all subsequent calls will return Done.
759func (it *TaskIterator) Next() (*taskspb.Task, error) {
760	var item *taskspb.Task
761	if err := it.nextFunc(); err != nil {
762		return item, err
763	}
764	item = it.items[0]
765	it.items = it.items[1:]
766	return item, nil
767}
768
769func (it *TaskIterator) bufLen() int {
770	return len(it.items)
771}
772
773func (it *TaskIterator) takeBuf() interface{} {
774	b := it.items
775	it.items = nil
776	return b
777}
778