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