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 functions
18
19import (
20	"context"
21	"fmt"
22	"math"
23	"net/url"
24	"time"
25
26	"cloud.google.com/go/longrunning"
27	lroauto "cloud.google.com/go/longrunning/autogen"
28	gax "github.com/googleapis/gax-go/v2"
29	"google.golang.org/api/iterator"
30	"google.golang.org/api/option"
31	"google.golang.org/api/option/internaloption"
32	gtransport "google.golang.org/api/transport/grpc"
33	functionspb "google.golang.org/genproto/googleapis/cloud/functions/v1"
34	iampb "google.golang.org/genproto/googleapis/iam/v1"
35	longrunningpb "google.golang.org/genproto/googleapis/longrunning"
36	"google.golang.org/grpc"
37	"google.golang.org/grpc/codes"
38	"google.golang.org/grpc/metadata"
39	"google.golang.org/protobuf/proto"
40)
41
42var newCloudFunctionsClientHook clientHook
43
44// CloudFunctionsCallOptions contains the retry settings for each method of CloudFunctionsClient.
45type CloudFunctionsCallOptions struct {
46	ListFunctions       []gax.CallOption
47	GetFunction         []gax.CallOption
48	CreateFunction      []gax.CallOption
49	UpdateFunction      []gax.CallOption
50	DeleteFunction      []gax.CallOption
51	CallFunction        []gax.CallOption
52	GenerateUploadUrl   []gax.CallOption
53	GenerateDownloadUrl []gax.CallOption
54	SetIamPolicy        []gax.CallOption
55	GetIamPolicy        []gax.CallOption
56	TestIamPermissions  []gax.CallOption
57}
58
59func defaultCloudFunctionsGRPCClientOptions() []option.ClientOption {
60	return []option.ClientOption{
61		internaloption.WithDefaultEndpoint("cloudfunctions.googleapis.com:443"),
62		internaloption.WithDefaultMTLSEndpoint("cloudfunctions.mtls.googleapis.com:443"),
63		internaloption.WithDefaultAudience("https://cloudfunctions.googleapis.com/"),
64		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
65		internaloption.EnableJwtWithScope(),
66		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
67		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
68			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
69	}
70}
71
72func defaultCloudFunctionsCallOptions() *CloudFunctionsCallOptions {
73	return &CloudFunctionsCallOptions{
74		ListFunctions: []gax.CallOption{
75			gax.WithRetry(func() gax.Retryer {
76				return gax.OnCodes([]codes.Code{
77					codes.Unavailable,
78					codes.DeadlineExceeded,
79				}, gax.Backoff{
80					Initial:    100 * time.Millisecond,
81					Max:        60000 * time.Millisecond,
82					Multiplier: 1.30,
83				})
84			}),
85		},
86		GetFunction: []gax.CallOption{
87			gax.WithRetry(func() gax.Retryer {
88				return gax.OnCodes([]codes.Code{
89					codes.Unavailable,
90					codes.DeadlineExceeded,
91				}, gax.Backoff{
92					Initial:    100 * time.Millisecond,
93					Max:        60000 * time.Millisecond,
94					Multiplier: 1.30,
95				})
96			}),
97		},
98		CreateFunction: []gax.CallOption{},
99		UpdateFunction: []gax.CallOption{
100			gax.WithRetry(func() gax.Retryer {
101				return gax.OnCodes([]codes.Code{
102					codes.Unavailable,
103					codes.DeadlineExceeded,
104				}, gax.Backoff{
105					Initial:    100 * time.Millisecond,
106					Max:        60000 * time.Millisecond,
107					Multiplier: 1.30,
108				})
109			}),
110		},
111		DeleteFunction: []gax.CallOption{
112			gax.WithRetry(func() gax.Retryer {
113				return gax.OnCodes([]codes.Code{
114					codes.Unavailable,
115					codes.DeadlineExceeded,
116				}, gax.Backoff{
117					Initial:    100 * time.Millisecond,
118					Max:        60000 * time.Millisecond,
119					Multiplier: 1.30,
120				})
121			}),
122		},
123		CallFunction:        []gax.CallOption{},
124		GenerateUploadUrl:   []gax.CallOption{},
125		GenerateDownloadUrl: []gax.CallOption{},
126		SetIamPolicy:        []gax.CallOption{},
127		GetIamPolicy:        []gax.CallOption{},
128		TestIamPermissions:  []gax.CallOption{},
129	}
130}
131
132// internalCloudFunctionsClient is an interface that defines the methods availaible from Cloud Functions API.
133type internalCloudFunctionsClient interface {
134	Close() error
135	setGoogleClientInfo(...string)
136	Connection() *grpc.ClientConn
137	ListFunctions(context.Context, *functionspb.ListFunctionsRequest, ...gax.CallOption) *CloudFunctionIterator
138	GetFunction(context.Context, *functionspb.GetFunctionRequest, ...gax.CallOption) (*functionspb.CloudFunction, error)
139	CreateFunction(context.Context, *functionspb.CreateFunctionRequest, ...gax.CallOption) (*CreateFunctionOperation, error)
140	CreateFunctionOperation(name string) *CreateFunctionOperation
141	UpdateFunction(context.Context, *functionspb.UpdateFunctionRequest, ...gax.CallOption) (*UpdateFunctionOperation, error)
142	UpdateFunctionOperation(name string) *UpdateFunctionOperation
143	DeleteFunction(context.Context, *functionspb.DeleteFunctionRequest, ...gax.CallOption) (*DeleteFunctionOperation, error)
144	DeleteFunctionOperation(name string) *DeleteFunctionOperation
145	CallFunction(context.Context, *functionspb.CallFunctionRequest, ...gax.CallOption) (*functionspb.CallFunctionResponse, error)
146	GenerateUploadUrl(context.Context, *functionspb.GenerateUploadUrlRequest, ...gax.CallOption) (*functionspb.GenerateUploadUrlResponse, error)
147	GenerateDownloadUrl(context.Context, *functionspb.GenerateDownloadUrlRequest, ...gax.CallOption) (*functionspb.GenerateDownloadUrlResponse, error)
148	SetIamPolicy(context.Context, *iampb.SetIamPolicyRequest, ...gax.CallOption) (*iampb.Policy, error)
149	GetIamPolicy(context.Context, *iampb.GetIamPolicyRequest, ...gax.CallOption) (*iampb.Policy, error)
150	TestIamPermissions(context.Context, *iampb.TestIamPermissionsRequest, ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error)
151}
152
153// CloudFunctionsClient is a client for interacting with Cloud Functions API.
154// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
155//
156// A service that application uses to manipulate triggers and functions.
157type CloudFunctionsClient struct {
158	// The internal transport-dependent client.
159	internalClient internalCloudFunctionsClient
160
161	// The call options for this service.
162	CallOptions *CloudFunctionsCallOptions
163
164	// LROClient is used internally to handle long-running operations.
165	// It is exposed so that its CallOptions can be modified if required.
166	// Users should not Close this client.
167	LROClient *lroauto.OperationsClient
168}
169
170// Wrapper methods routed to the internal client.
171
172// Close closes the connection to the API service. The user should invoke this when
173// the client is no longer required.
174func (c *CloudFunctionsClient) Close() error {
175	return c.internalClient.Close()
176}
177
178// setGoogleClientInfo sets the name and version of the application in
179// the `x-goog-api-client` header passed on each request. Intended for
180// use by Google-written clients.
181func (c *CloudFunctionsClient) setGoogleClientInfo(keyval ...string) {
182	c.internalClient.setGoogleClientInfo(keyval...)
183}
184
185// Connection returns a connection to the API service.
186//
187// Deprecated.
188func (c *CloudFunctionsClient) Connection() *grpc.ClientConn {
189	return c.internalClient.Connection()
190}
191
192// ListFunctions returns a list of functions that belong to the requested project.
193func (c *CloudFunctionsClient) ListFunctions(ctx context.Context, req *functionspb.ListFunctionsRequest, opts ...gax.CallOption) *CloudFunctionIterator {
194	return c.internalClient.ListFunctions(ctx, req, opts...)
195}
196
197// GetFunction returns a function with the given name from the requested project.
198func (c *CloudFunctionsClient) GetFunction(ctx context.Context, req *functionspb.GetFunctionRequest, opts ...gax.CallOption) (*functionspb.CloudFunction, error) {
199	return c.internalClient.GetFunction(ctx, req, opts...)
200}
201
202// CreateFunction creates a new function. If a function with the given name already exists in
203// the specified project, the long running operation will return
204// ALREADY_EXISTS error.
205func (c *CloudFunctionsClient) CreateFunction(ctx context.Context, req *functionspb.CreateFunctionRequest, opts ...gax.CallOption) (*CreateFunctionOperation, error) {
206	return c.internalClient.CreateFunction(ctx, req, opts...)
207}
208
209// CreateFunctionOperation returns a new CreateFunctionOperation from a given name.
210// The name must be that of a previously created CreateFunctionOperation, possibly from a different process.
211func (c *CloudFunctionsClient) CreateFunctionOperation(name string) *CreateFunctionOperation {
212	return c.internalClient.CreateFunctionOperation(name)
213}
214
215// UpdateFunction updates existing function.
216func (c *CloudFunctionsClient) UpdateFunction(ctx context.Context, req *functionspb.UpdateFunctionRequest, opts ...gax.CallOption) (*UpdateFunctionOperation, error) {
217	return c.internalClient.UpdateFunction(ctx, req, opts...)
218}
219
220// UpdateFunctionOperation returns a new UpdateFunctionOperation from a given name.
221// The name must be that of a previously created UpdateFunctionOperation, possibly from a different process.
222func (c *CloudFunctionsClient) UpdateFunctionOperation(name string) *UpdateFunctionOperation {
223	return c.internalClient.UpdateFunctionOperation(name)
224}
225
226// DeleteFunction deletes a function with the given name from the specified project. If the
227// given function is used by some trigger, the trigger will be updated to
228// remove this function.
229func (c *CloudFunctionsClient) DeleteFunction(ctx context.Context, req *functionspb.DeleteFunctionRequest, opts ...gax.CallOption) (*DeleteFunctionOperation, error) {
230	return c.internalClient.DeleteFunction(ctx, req, opts...)
231}
232
233// DeleteFunctionOperation returns a new DeleteFunctionOperation from a given name.
234// The name must be that of a previously created DeleteFunctionOperation, possibly from a different process.
235func (c *CloudFunctionsClient) DeleteFunctionOperation(name string) *DeleteFunctionOperation {
236	return c.internalClient.DeleteFunctionOperation(name)
237}
238
239// CallFunction synchronously invokes a deployed Cloud Function. To be used for testing
240// purposes as very limited traffic is allowed. For more information on
241// the actual limits, refer to
242// Rate Limits (at https://cloud.google.com/functions/quotas#rate_limits).
243func (c *CloudFunctionsClient) CallFunction(ctx context.Context, req *functionspb.CallFunctionRequest, opts ...gax.CallOption) (*functionspb.CallFunctionResponse, error) {
244	return c.internalClient.CallFunction(ctx, req, opts...)
245}
246
247// GenerateUploadUrl returns a signed URL for uploading a function source code.
248// For more information about the signed URL usage see:
249// https://cloud.google.com/storage/docs/access-control/signed-urls (at https://cloud.google.com/storage/docs/access-control/signed-urls).
250// Once the function source code upload is complete, the used signed
251// URL should be provided in CreateFunction or UpdateFunction request
252// as a reference to the function source code.
253//
254// When uploading source code to the generated signed URL, please follow
255// these restrictions:
256//
257//   Source file type should be a zip file.
258//
259//   Source file size should not exceed 100MB limit.
260//
261//   No credentials should be attached - the signed URLs provide access to the
262//   target bucket using internal service identity; if credentials were
263//   attached, the identity from the credentials would be used, but that
264//   identity does not have permissions to upload files to the URL.
265//
266// When making a HTTP PUT request, these two headers need to be specified:
267//
268//   content-type: application/zip
269//
270//   x-goog-content-length-range: 0,104857600
271//
272// And this header SHOULD NOT be specified:
273//
274//   Authorization: Bearer YOUR_TOKEN
275func (c *CloudFunctionsClient) GenerateUploadUrl(ctx context.Context, req *functionspb.GenerateUploadUrlRequest, opts ...gax.CallOption) (*functionspb.GenerateUploadUrlResponse, error) {
276	return c.internalClient.GenerateUploadUrl(ctx, req, opts...)
277}
278
279// GenerateDownloadUrl returns a signed URL for downloading deployed function source code.
280// The URL is only valid for a limited period and should be used within
281// minutes after generation.
282// For more information about the signed URL usage see:
283// https://cloud.google.com/storage/docs/access-control/signed-urls (at https://cloud.google.com/storage/docs/access-control/signed-urls)
284func (c *CloudFunctionsClient) GenerateDownloadUrl(ctx context.Context, req *functionspb.GenerateDownloadUrlRequest, opts ...gax.CallOption) (*functionspb.GenerateDownloadUrlResponse, error) {
285	return c.internalClient.GenerateDownloadUrl(ctx, req, opts...)
286}
287
288// SetIamPolicy sets the IAM access control policy on the specified function.
289// Replaces any existing policy.
290func (c *CloudFunctionsClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
291	return c.internalClient.SetIamPolicy(ctx, req, opts...)
292}
293
294// GetIamPolicy gets the IAM access control policy for a function.
295// Returns an empty policy if the function exists and does not have a policy
296// set.
297func (c *CloudFunctionsClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
298	return c.internalClient.GetIamPolicy(ctx, req, opts...)
299}
300
301// TestIamPermissions tests the specified permissions against the IAM access control policy
302// for a function.
303// If the function does not exist, this will return an empty set of
304// permissions, not a NOT_FOUND error.
305func (c *CloudFunctionsClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
306	return c.internalClient.TestIamPermissions(ctx, req, opts...)
307}
308
309// cloudFunctionsGRPCClient is a client for interacting with Cloud Functions API over gRPC transport.
310//
311// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
312type cloudFunctionsGRPCClient struct {
313	// Connection pool of gRPC connections to the service.
314	connPool gtransport.ConnPool
315
316	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
317	disableDeadlines bool
318
319	// Points back to the CallOptions field of the containing CloudFunctionsClient
320	CallOptions **CloudFunctionsCallOptions
321
322	// The gRPC API client.
323	cloudFunctionsClient functionspb.CloudFunctionsServiceClient
324
325	// LROClient is used internally to handle long-running operations.
326	// It is exposed so that its CallOptions can be modified if required.
327	// Users should not Close this client.
328	LROClient **lroauto.OperationsClient
329
330	// The x-goog-* metadata to be sent with each request.
331	xGoogMetadata metadata.MD
332}
333
334// NewCloudFunctionsClient creates a new cloud functions service client based on gRPC.
335// The returned client must be Closed when it is done being used to clean up its underlying connections.
336//
337// A service that application uses to manipulate triggers and functions.
338func NewCloudFunctionsClient(ctx context.Context, opts ...option.ClientOption) (*CloudFunctionsClient, error) {
339	clientOpts := defaultCloudFunctionsGRPCClientOptions()
340	if newCloudFunctionsClientHook != nil {
341		hookOpts, err := newCloudFunctionsClientHook(ctx, clientHookParams{})
342		if err != nil {
343			return nil, err
344		}
345		clientOpts = append(clientOpts, hookOpts...)
346	}
347
348	disableDeadlines, err := checkDisableDeadlines()
349	if err != nil {
350		return nil, err
351	}
352
353	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
354	if err != nil {
355		return nil, err
356	}
357	client := CloudFunctionsClient{CallOptions: defaultCloudFunctionsCallOptions()}
358
359	c := &cloudFunctionsGRPCClient{
360		connPool:             connPool,
361		disableDeadlines:     disableDeadlines,
362		cloudFunctionsClient: functionspb.NewCloudFunctionsServiceClient(connPool),
363		CallOptions:          &client.CallOptions,
364	}
365	c.setGoogleClientInfo()
366
367	client.internalClient = c
368
369	client.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool))
370	if err != nil {
371		// This error "should not happen", since we are just reusing old connection pool
372		// and never actually need to dial.
373		// If this does happen, we could leak connp. However, we cannot close conn:
374		// If the user invoked the constructor with option.WithGRPCConn,
375		// we would close a connection that's still in use.
376		// TODO: investigate error conditions.
377		return nil, err
378	}
379	c.LROClient = &client.LROClient
380	return &client, nil
381}
382
383// Connection returns a connection to the API service.
384//
385// Deprecated.
386func (c *cloudFunctionsGRPCClient) Connection() *grpc.ClientConn {
387	return c.connPool.Conn()
388}
389
390// setGoogleClientInfo sets the name and version of the application in
391// the `x-goog-api-client` header passed on each request. Intended for
392// use by Google-written clients.
393func (c *cloudFunctionsGRPCClient) setGoogleClientInfo(keyval ...string) {
394	kv := append([]string{"gl-go", versionGo()}, keyval...)
395	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
396	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
397}
398
399// Close closes the connection to the API service. The user should invoke this when
400// the client is no longer required.
401func (c *cloudFunctionsGRPCClient) Close() error {
402	return c.connPool.Close()
403}
404
405func (c *cloudFunctionsGRPCClient) ListFunctions(ctx context.Context, req *functionspb.ListFunctionsRequest, opts ...gax.CallOption) *CloudFunctionIterator {
406	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
407	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
408	opts = append((*c.CallOptions).ListFunctions[0:len((*c.CallOptions).ListFunctions):len((*c.CallOptions).ListFunctions)], opts...)
409	it := &CloudFunctionIterator{}
410	req = proto.Clone(req).(*functionspb.ListFunctionsRequest)
411	it.InternalFetch = func(pageSize int, pageToken string) ([]*functionspb.CloudFunction, string, error) {
412		resp := &functionspb.ListFunctionsResponse{}
413		if pageToken != "" {
414			req.PageToken = pageToken
415		}
416		if pageSize > math.MaxInt32 {
417			req.PageSize = math.MaxInt32
418		} else if pageSize != 0 {
419			req.PageSize = int32(pageSize)
420		}
421		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
422			var err error
423			resp, err = c.cloudFunctionsClient.ListFunctions(ctx, req, settings.GRPC...)
424			return err
425		}, opts...)
426		if err != nil {
427			return nil, "", err
428		}
429
430		it.Response = resp
431		return resp.GetFunctions(), resp.GetNextPageToken(), nil
432	}
433	fetch := func(pageSize int, pageToken string) (string, error) {
434		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
435		if err != nil {
436			return "", err
437		}
438		it.items = append(it.items, items...)
439		return nextPageToken, nil
440	}
441
442	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
443	it.pageInfo.MaxSize = int(req.GetPageSize())
444	it.pageInfo.Token = req.GetPageToken()
445
446	return it
447}
448
449func (c *cloudFunctionsGRPCClient) GetFunction(ctx context.Context, req *functionspb.GetFunctionRequest, opts ...gax.CallOption) (*functionspb.CloudFunction, error) {
450	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
451		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
452		defer cancel()
453		ctx = cctx
454	}
455	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
456	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
457	opts = append((*c.CallOptions).GetFunction[0:len((*c.CallOptions).GetFunction):len((*c.CallOptions).GetFunction)], opts...)
458	var resp *functionspb.CloudFunction
459	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
460		var err error
461		resp, err = c.cloudFunctionsClient.GetFunction(ctx, req, settings.GRPC...)
462		return err
463	}, opts...)
464	if err != nil {
465		return nil, err
466	}
467	return resp, nil
468}
469
470func (c *cloudFunctionsGRPCClient) CreateFunction(ctx context.Context, req *functionspb.CreateFunctionRequest, opts ...gax.CallOption) (*CreateFunctionOperation, error) {
471	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
472		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
473		defer cancel()
474		ctx = cctx
475	}
476	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "location", url.QueryEscape(req.GetLocation())))
477	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
478	opts = append((*c.CallOptions).CreateFunction[0:len((*c.CallOptions).CreateFunction):len((*c.CallOptions).CreateFunction)], opts...)
479	var resp *longrunningpb.Operation
480	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
481		var err error
482		resp, err = c.cloudFunctionsClient.CreateFunction(ctx, req, settings.GRPC...)
483		return err
484	}, opts...)
485	if err != nil {
486		return nil, err
487	}
488	return &CreateFunctionOperation{
489		lro: longrunning.InternalNewOperation(*c.LROClient, resp),
490	}, nil
491}
492
493func (c *cloudFunctionsGRPCClient) UpdateFunction(ctx context.Context, req *functionspb.UpdateFunctionRequest, opts ...gax.CallOption) (*UpdateFunctionOperation, error) {
494	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
495		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
496		defer cancel()
497		ctx = cctx
498	}
499	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "function.name", url.QueryEscape(req.GetFunction().GetName())))
500	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
501	opts = append((*c.CallOptions).UpdateFunction[0:len((*c.CallOptions).UpdateFunction):len((*c.CallOptions).UpdateFunction)], opts...)
502	var resp *longrunningpb.Operation
503	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
504		var err error
505		resp, err = c.cloudFunctionsClient.UpdateFunction(ctx, req, settings.GRPC...)
506		return err
507	}, opts...)
508	if err != nil {
509		return nil, err
510	}
511	return &UpdateFunctionOperation{
512		lro: longrunning.InternalNewOperation(*c.LROClient, resp),
513	}, nil
514}
515
516func (c *cloudFunctionsGRPCClient) DeleteFunction(ctx context.Context, req *functionspb.DeleteFunctionRequest, opts ...gax.CallOption) (*DeleteFunctionOperation, error) {
517	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
518		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
519		defer cancel()
520		ctx = cctx
521	}
522	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
523	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
524	opts = append((*c.CallOptions).DeleteFunction[0:len((*c.CallOptions).DeleteFunction):len((*c.CallOptions).DeleteFunction)], opts...)
525	var resp *longrunningpb.Operation
526	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
527		var err error
528		resp, err = c.cloudFunctionsClient.DeleteFunction(ctx, req, settings.GRPC...)
529		return err
530	}, opts...)
531	if err != nil {
532		return nil, err
533	}
534	return &DeleteFunctionOperation{
535		lro: longrunning.InternalNewOperation(*c.LROClient, resp),
536	}, nil
537}
538
539func (c *cloudFunctionsGRPCClient) CallFunction(ctx context.Context, req *functionspb.CallFunctionRequest, opts ...gax.CallOption) (*functionspb.CallFunctionResponse, error) {
540	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
541		cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond)
542		defer cancel()
543		ctx = cctx
544	}
545	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
546	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
547	opts = append((*c.CallOptions).CallFunction[0:len((*c.CallOptions).CallFunction):len((*c.CallOptions).CallFunction)], opts...)
548	var resp *functionspb.CallFunctionResponse
549	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
550		var err error
551		resp, err = c.cloudFunctionsClient.CallFunction(ctx, req, settings.GRPC...)
552		return err
553	}, opts...)
554	if err != nil {
555		return nil, err
556	}
557	return resp, nil
558}
559
560func (c *cloudFunctionsGRPCClient) GenerateUploadUrl(ctx context.Context, req *functionspb.GenerateUploadUrlRequest, opts ...gax.CallOption) (*functionspb.GenerateUploadUrlResponse, error) {
561	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
562	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
563	opts = append((*c.CallOptions).GenerateUploadUrl[0:len((*c.CallOptions).GenerateUploadUrl):len((*c.CallOptions).GenerateUploadUrl)], opts...)
564	var resp *functionspb.GenerateUploadUrlResponse
565	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
566		var err error
567		resp, err = c.cloudFunctionsClient.GenerateUploadUrl(ctx, req, settings.GRPC...)
568		return err
569	}, opts...)
570	if err != nil {
571		return nil, err
572	}
573	return resp, nil
574}
575
576func (c *cloudFunctionsGRPCClient) GenerateDownloadUrl(ctx context.Context, req *functionspb.GenerateDownloadUrlRequest, opts ...gax.CallOption) (*functionspb.GenerateDownloadUrlResponse, error) {
577	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
578	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
579	opts = append((*c.CallOptions).GenerateDownloadUrl[0:len((*c.CallOptions).GenerateDownloadUrl):len((*c.CallOptions).GenerateDownloadUrl)], opts...)
580	var resp *functionspb.GenerateDownloadUrlResponse
581	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
582		var err error
583		resp, err = c.cloudFunctionsClient.GenerateDownloadUrl(ctx, req, settings.GRPC...)
584		return err
585	}, opts...)
586	if err != nil {
587		return nil, err
588	}
589	return resp, nil
590}
591
592func (c *cloudFunctionsGRPCClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
593	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource())))
594	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
595	opts = append((*c.CallOptions).SetIamPolicy[0:len((*c.CallOptions).SetIamPolicy):len((*c.CallOptions).SetIamPolicy)], opts...)
596	var resp *iampb.Policy
597	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
598		var err error
599		resp, err = c.cloudFunctionsClient.SetIamPolicy(ctx, req, settings.GRPC...)
600		return err
601	}, opts...)
602	if err != nil {
603		return nil, err
604	}
605	return resp, nil
606}
607
608func (c *cloudFunctionsGRPCClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
609	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource())))
610	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
611	opts = append((*c.CallOptions).GetIamPolicy[0:len((*c.CallOptions).GetIamPolicy):len((*c.CallOptions).GetIamPolicy)], opts...)
612	var resp *iampb.Policy
613	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
614		var err error
615		resp, err = c.cloudFunctionsClient.GetIamPolicy(ctx, req, settings.GRPC...)
616		return err
617	}, opts...)
618	if err != nil {
619		return nil, err
620	}
621	return resp, nil
622}
623
624func (c *cloudFunctionsGRPCClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
625	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource())))
626	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
627	opts = append((*c.CallOptions).TestIamPermissions[0:len((*c.CallOptions).TestIamPermissions):len((*c.CallOptions).TestIamPermissions)], opts...)
628	var resp *iampb.TestIamPermissionsResponse
629	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
630		var err error
631		resp, err = c.cloudFunctionsClient.TestIamPermissions(ctx, req, settings.GRPC...)
632		return err
633	}, opts...)
634	if err != nil {
635		return nil, err
636	}
637	return resp, nil
638}
639
640// CreateFunctionOperation manages a long-running operation from CreateFunction.
641type CreateFunctionOperation struct {
642	lro *longrunning.Operation
643}
644
645// CreateFunctionOperation returns a new CreateFunctionOperation from a given name.
646// The name must be that of a previously created CreateFunctionOperation, possibly from a different process.
647func (c *cloudFunctionsGRPCClient) CreateFunctionOperation(name string) *CreateFunctionOperation {
648	return &CreateFunctionOperation{
649		lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
650	}
651}
652
653// Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
654//
655// See documentation of Poll for error-handling information.
656func (op *CreateFunctionOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*functionspb.CloudFunction, error) {
657	var resp functionspb.CloudFunction
658	if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil {
659		return nil, err
660	}
661	return &resp, nil
662}
663
664// Poll fetches the latest state of the long-running operation.
665//
666// Poll also fetches the latest metadata, which can be retrieved by Metadata.
667//
668// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
669// the operation has completed with failure, the error is returned and op.Done will return true.
670// If Poll succeeds and the operation has completed successfully,
671// op.Done will return true, and the response of the operation is returned.
672// If Poll succeeds and the operation has not completed, the returned response and error are both nil.
673func (op *CreateFunctionOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*functionspb.CloudFunction, error) {
674	var resp functionspb.CloudFunction
675	if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
676		return nil, err
677	}
678	if !op.Done() {
679		return nil, nil
680	}
681	return &resp, nil
682}
683
684// Metadata returns metadata associated with the long-running operation.
685// Metadata itself does not contact the server, but Poll does.
686// To get the latest metadata, call this method after a successful call to Poll.
687// If the metadata is not available, the returned metadata and error are both nil.
688func (op *CreateFunctionOperation) Metadata() (*functionspb.OperationMetadataV1, error) {
689	var meta functionspb.OperationMetadataV1
690	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
691		return nil, nil
692	} else if err != nil {
693		return nil, err
694	}
695	return &meta, nil
696}
697
698// Done reports whether the long-running operation has completed.
699func (op *CreateFunctionOperation) Done() bool {
700	return op.lro.Done()
701}
702
703// Name returns the name of the long-running operation.
704// The name is assigned by the server and is unique within the service from which the operation is created.
705func (op *CreateFunctionOperation) Name() string {
706	return op.lro.Name()
707}
708
709// DeleteFunctionOperation manages a long-running operation from DeleteFunction.
710type DeleteFunctionOperation struct {
711	lro *longrunning.Operation
712}
713
714// DeleteFunctionOperation returns a new DeleteFunctionOperation from a given name.
715// The name must be that of a previously created DeleteFunctionOperation, possibly from a different process.
716func (c *cloudFunctionsGRPCClient) DeleteFunctionOperation(name string) *DeleteFunctionOperation {
717	return &DeleteFunctionOperation{
718		lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
719	}
720}
721
722// Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
723//
724// See documentation of Poll for error-handling information.
725func (op *DeleteFunctionOperation) Wait(ctx context.Context, opts ...gax.CallOption) error {
726	return op.lro.WaitWithInterval(ctx, nil, time.Minute, opts...)
727}
728
729// Poll fetches the latest state of the long-running operation.
730//
731// Poll also fetches the latest metadata, which can be retrieved by Metadata.
732//
733// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
734// the operation has completed with failure, the error is returned and op.Done will return true.
735// If Poll succeeds and the operation has completed successfully,
736// op.Done will return true, and the response of the operation is returned.
737// If Poll succeeds and the operation has not completed, the returned response and error are both nil.
738func (op *DeleteFunctionOperation) Poll(ctx context.Context, opts ...gax.CallOption) error {
739	return op.lro.Poll(ctx, nil, opts...)
740}
741
742// Metadata returns metadata associated with the long-running operation.
743// Metadata itself does not contact the server, but Poll does.
744// To get the latest metadata, call this method after a successful call to Poll.
745// If the metadata is not available, the returned metadata and error are both nil.
746func (op *DeleteFunctionOperation) Metadata() (*functionspb.OperationMetadataV1, error) {
747	var meta functionspb.OperationMetadataV1
748	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
749		return nil, nil
750	} else if err != nil {
751		return nil, err
752	}
753	return &meta, nil
754}
755
756// Done reports whether the long-running operation has completed.
757func (op *DeleteFunctionOperation) Done() bool {
758	return op.lro.Done()
759}
760
761// Name returns the name of the long-running operation.
762// The name is assigned by the server and is unique within the service from which the operation is created.
763func (op *DeleteFunctionOperation) Name() string {
764	return op.lro.Name()
765}
766
767// UpdateFunctionOperation manages a long-running operation from UpdateFunction.
768type UpdateFunctionOperation struct {
769	lro *longrunning.Operation
770}
771
772// UpdateFunctionOperation returns a new UpdateFunctionOperation from a given name.
773// The name must be that of a previously created UpdateFunctionOperation, possibly from a different process.
774func (c *cloudFunctionsGRPCClient) UpdateFunctionOperation(name string) *UpdateFunctionOperation {
775	return &UpdateFunctionOperation{
776		lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
777	}
778}
779
780// Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
781//
782// See documentation of Poll for error-handling information.
783func (op *UpdateFunctionOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*functionspb.CloudFunction, error) {
784	var resp functionspb.CloudFunction
785	if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil {
786		return nil, err
787	}
788	return &resp, nil
789}
790
791// Poll fetches the latest state of the long-running operation.
792//
793// Poll also fetches the latest metadata, which can be retrieved by Metadata.
794//
795// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
796// the operation has completed with failure, the error is returned and op.Done will return true.
797// If Poll succeeds and the operation has completed successfully,
798// op.Done will return true, and the response of the operation is returned.
799// If Poll succeeds and the operation has not completed, the returned response and error are both nil.
800func (op *UpdateFunctionOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*functionspb.CloudFunction, error) {
801	var resp functionspb.CloudFunction
802	if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
803		return nil, err
804	}
805	if !op.Done() {
806		return nil, nil
807	}
808	return &resp, nil
809}
810
811// Metadata returns metadata associated with the long-running operation.
812// Metadata itself does not contact the server, but Poll does.
813// To get the latest metadata, call this method after a successful call to Poll.
814// If the metadata is not available, the returned metadata and error are both nil.
815func (op *UpdateFunctionOperation) Metadata() (*functionspb.OperationMetadataV1, error) {
816	var meta functionspb.OperationMetadataV1
817	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
818		return nil, nil
819	} else if err != nil {
820		return nil, err
821	}
822	return &meta, nil
823}
824
825// Done reports whether the long-running operation has completed.
826func (op *UpdateFunctionOperation) Done() bool {
827	return op.lro.Done()
828}
829
830// Name returns the name of the long-running operation.
831// The name is assigned by the server and is unique within the service from which the operation is created.
832func (op *UpdateFunctionOperation) Name() string {
833	return op.lro.Name()
834}
835
836// CloudFunctionIterator manages a stream of *functionspb.CloudFunction.
837type CloudFunctionIterator struct {
838	items    []*functionspb.CloudFunction
839	pageInfo *iterator.PageInfo
840	nextFunc func() error
841
842	// Response is the raw response for the current page.
843	// It must be cast to the RPC response type.
844	// Calling Next() or InternalFetch() updates this value.
845	Response interface{}
846
847	// InternalFetch is for use by the Google Cloud Libraries only.
848	// It is not part of the stable interface of this package.
849	//
850	// InternalFetch returns results from a single call to the underlying RPC.
851	// The number of results is no greater than pageSize.
852	// If there are no more results, nextPageToken is empty and err is nil.
853	InternalFetch func(pageSize int, pageToken string) (results []*functionspb.CloudFunction, nextPageToken string, err error)
854}
855
856// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
857func (it *CloudFunctionIterator) PageInfo() *iterator.PageInfo {
858	return it.pageInfo
859}
860
861// Next returns the next result. Its second return value is iterator.Done if there are no more
862// results. Once Next returns Done, all subsequent calls will return Done.
863func (it *CloudFunctionIterator) Next() (*functionspb.CloudFunction, error) {
864	var item *functionspb.CloudFunction
865	if err := it.nextFunc(); err != nil {
866		return item, err
867	}
868	item = it.items[0]
869	it.items = it.items[1:]
870	return item, nil
871}
872
873func (it *CloudFunctionIterator) bufLen() int {
874	return len(it.items)
875}
876
877func (it *CloudFunctionIterator) takeBuf() interface{} {
878	b := it.items
879	it.items = nil
880	return b
881}
882