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 appengine
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	"github.com/golang/protobuf/proto"
29	gax "github.com/googleapis/gax-go/v2"
30	"google.golang.org/api/iterator"
31	"google.golang.org/api/option"
32	"google.golang.org/api/option/internaloption"
33	gtransport "google.golang.org/api/transport/grpc"
34	appenginepb "google.golang.org/genproto/googleapis/appengine/v1"
35	longrunningpb "google.golang.org/genproto/googleapis/longrunning"
36	"google.golang.org/grpc"
37	"google.golang.org/grpc/metadata"
38)
39
40var newServicesClientHook clientHook
41
42// ServicesCallOptions contains the retry settings for each method of ServicesClient.
43type ServicesCallOptions struct {
44	ListServices  []gax.CallOption
45	GetService    []gax.CallOption
46	UpdateService []gax.CallOption
47	DeleteService []gax.CallOption
48}
49
50func defaultServicesClientOptions() []option.ClientOption {
51	return []option.ClientOption{
52		internaloption.WithDefaultEndpoint("appengine.googleapis.com:443"),
53		internaloption.WithDefaultMTLSEndpoint("appengine.mtls.googleapis.com:443"),
54		internaloption.WithDefaultAudience("https://appengine.googleapis.com/"),
55		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
56		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
57		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
58			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
59	}
60}
61
62func defaultServicesCallOptions() *ServicesCallOptions {
63	return &ServicesCallOptions{
64		ListServices:  []gax.CallOption{},
65		GetService:    []gax.CallOption{},
66		UpdateService: []gax.CallOption{},
67		DeleteService: []gax.CallOption{},
68	}
69}
70
71// ServicesClient is a client for interacting with App Engine Audit Data.
72//
73// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
74type ServicesClient struct {
75	// Connection pool of gRPC connections to the service.
76	connPool gtransport.ConnPool
77
78	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
79	disableDeadlines bool
80
81	// The gRPC API client.
82	servicesClient appenginepb.ServicesClient
83
84	// LROClient is used internally to handle longrunning operations.
85	// It is exposed so that its CallOptions can be modified if required.
86	// Users should not Close this client.
87	LROClient *lroauto.OperationsClient
88
89	// The call options for this service.
90	CallOptions *ServicesCallOptions
91
92	// The x-goog-* metadata to be sent with each request.
93	xGoogMetadata metadata.MD
94}
95
96// NewServicesClient creates a new services client.
97//
98// Manages services of an application.
99func NewServicesClient(ctx context.Context, opts ...option.ClientOption) (*ServicesClient, error) {
100	clientOpts := defaultServicesClientOptions()
101
102	if newServicesClientHook != nil {
103		hookOpts, err := newServicesClientHook(ctx, clientHookParams{})
104		if err != nil {
105			return nil, err
106		}
107		clientOpts = append(clientOpts, hookOpts...)
108	}
109
110	disableDeadlines, err := checkDisableDeadlines()
111	if err != nil {
112		return nil, err
113	}
114
115	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
116	if err != nil {
117		return nil, err
118	}
119	c := &ServicesClient{
120		connPool:         connPool,
121		disableDeadlines: disableDeadlines,
122		CallOptions:      defaultServicesCallOptions(),
123
124		servicesClient: appenginepb.NewServicesClient(connPool),
125	}
126	c.setGoogleClientInfo()
127
128	c.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool))
129	if err != nil {
130		// This error "should not happen", since we are just reusing old connection pool
131		// and never actually need to dial.
132		// If this does happen, we could leak connp. However, we cannot close conn:
133		// If the user invoked the constructor with option.WithGRPCConn,
134		// we would close a connection that's still in use.
135		// TODO: investigate error conditions.
136		return nil, err
137	}
138	return c, nil
139}
140
141// Connection returns a connection to the API service.
142//
143// Deprecated.
144func (c *ServicesClient) Connection() *grpc.ClientConn {
145	return c.connPool.Conn()
146}
147
148// Close closes the connection to the API service. The user should invoke this when
149// the client is no longer required.
150func (c *ServicesClient) Close() error {
151	return c.connPool.Close()
152}
153
154// setGoogleClientInfo sets the name and version of the application in
155// the `x-goog-api-client` header passed on each request. Intended for
156// use by Google-written clients.
157func (c *ServicesClient) setGoogleClientInfo(keyval ...string) {
158	kv := append([]string{"gl-go", versionGo()}, keyval...)
159	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
160	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
161}
162
163// ListServices lists all the services in the application.
164func (c *ServicesClient) ListServices(ctx context.Context, req *appenginepb.ListServicesRequest, opts ...gax.CallOption) *ServiceIterator {
165	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
166	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
167	opts = append(c.CallOptions.ListServices[0:len(c.CallOptions.ListServices):len(c.CallOptions.ListServices)], opts...)
168	it := &ServiceIterator{}
169	req = proto.Clone(req).(*appenginepb.ListServicesRequest)
170	it.InternalFetch = func(pageSize int, pageToken string) ([]*appenginepb.Service, string, error) {
171		var resp *appenginepb.ListServicesResponse
172		req.PageToken = pageToken
173		if pageSize > math.MaxInt32 {
174			req.PageSize = math.MaxInt32
175		} else {
176			req.PageSize = int32(pageSize)
177		}
178		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
179			var err error
180			resp, err = c.servicesClient.ListServices(ctx, req, settings.GRPC...)
181			return err
182		}, opts...)
183		if err != nil {
184			return nil, "", err
185		}
186
187		it.Response = resp
188		return resp.GetServices(), resp.GetNextPageToken(), nil
189	}
190	fetch := func(pageSize int, pageToken string) (string, error) {
191		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
192		if err != nil {
193			return "", err
194		}
195		it.items = append(it.items, items...)
196		return nextPageToken, nil
197	}
198	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
199	it.pageInfo.MaxSize = int(req.GetPageSize())
200	it.pageInfo.Token = req.GetPageToken()
201	return it
202}
203
204// GetService gets the current configuration of the specified service.
205func (c *ServicesClient) GetService(ctx context.Context, req *appenginepb.GetServiceRequest, opts ...gax.CallOption) (*appenginepb.Service, error) {
206	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
207	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
208	opts = append(c.CallOptions.GetService[0:len(c.CallOptions.GetService):len(c.CallOptions.GetService)], opts...)
209	var resp *appenginepb.Service
210	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
211		var err error
212		resp, err = c.servicesClient.GetService(ctx, req, settings.GRPC...)
213		return err
214	}, opts...)
215	if err != nil {
216		return nil, err
217	}
218	return resp, nil
219}
220
221// UpdateService updates the configuration of the specified service.
222func (c *ServicesClient) UpdateService(ctx context.Context, req *appenginepb.UpdateServiceRequest, opts ...gax.CallOption) (*UpdateServiceOperation, error) {
223	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
224	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
225	opts = append(c.CallOptions.UpdateService[0:len(c.CallOptions.UpdateService):len(c.CallOptions.UpdateService)], opts...)
226	var resp *longrunningpb.Operation
227	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
228		var err error
229		resp, err = c.servicesClient.UpdateService(ctx, req, settings.GRPC...)
230		return err
231	}, opts...)
232	if err != nil {
233		return nil, err
234	}
235	return &UpdateServiceOperation{
236		lro: longrunning.InternalNewOperation(c.LROClient, resp),
237	}, nil
238}
239
240// DeleteService deletes the specified service and all enclosed versions.
241func (c *ServicesClient) DeleteService(ctx context.Context, req *appenginepb.DeleteServiceRequest, opts ...gax.CallOption) (*DeleteServiceOperation, error) {
242	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
243	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
244	opts = append(c.CallOptions.DeleteService[0:len(c.CallOptions.DeleteService):len(c.CallOptions.DeleteService)], opts...)
245	var resp *longrunningpb.Operation
246	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
247		var err error
248		resp, err = c.servicesClient.DeleteService(ctx, req, settings.GRPC...)
249		return err
250	}, opts...)
251	if err != nil {
252		return nil, err
253	}
254	return &DeleteServiceOperation{
255		lro: longrunning.InternalNewOperation(c.LROClient, resp),
256	}, nil
257}
258
259// DeleteServiceOperation manages a long-running operation from DeleteService.
260type DeleteServiceOperation struct {
261	lro *longrunning.Operation
262}
263
264// DeleteServiceOperation returns a new DeleteServiceOperation from a given name.
265// The name must be that of a previously created DeleteServiceOperation, possibly from a different process.
266func (c *ServicesClient) DeleteServiceOperation(name string) *DeleteServiceOperation {
267	return &DeleteServiceOperation{
268		lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
269	}
270}
271
272// Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
273//
274// See documentation of Poll for error-handling information.
275func (op *DeleteServiceOperation) Wait(ctx context.Context, opts ...gax.CallOption) error {
276	return op.lro.WaitWithInterval(ctx, nil, time.Minute, opts...)
277}
278
279// Poll fetches the latest state of the long-running operation.
280//
281// Poll also fetches the latest metadata, which can be retrieved by Metadata.
282//
283// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
284// the operation has completed with failure, the error is returned and op.Done will return true.
285// If Poll succeeds and the operation has completed successfully,
286// op.Done will return true, and the response of the operation is returned.
287// If Poll succeeds and the operation has not completed, the returned response and error are both nil.
288func (op *DeleteServiceOperation) Poll(ctx context.Context, opts ...gax.CallOption) error {
289	return op.lro.Poll(ctx, nil, opts...)
290}
291
292// Metadata returns metadata associated with the long-running operation.
293// Metadata itself does not contact the server, but Poll does.
294// To get the latest metadata, call this method after a successful call to Poll.
295// If the metadata is not available, the returned metadata and error are both nil.
296func (op *DeleteServiceOperation) Metadata() (*appenginepb.OperationMetadataV1, error) {
297	var meta appenginepb.OperationMetadataV1
298	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
299		return nil, nil
300	} else if err != nil {
301		return nil, err
302	}
303	return &meta, nil
304}
305
306// Done reports whether the long-running operation has completed.
307func (op *DeleteServiceOperation) Done() bool {
308	return op.lro.Done()
309}
310
311// Name returns the name of the long-running operation.
312// The name is assigned by the server and is unique within the service from which the operation is created.
313func (op *DeleteServiceOperation) Name() string {
314	return op.lro.Name()
315}
316
317// UpdateServiceOperation manages a long-running operation from UpdateService.
318type UpdateServiceOperation struct {
319	lro *longrunning.Operation
320}
321
322// UpdateServiceOperation returns a new UpdateServiceOperation from a given name.
323// The name must be that of a previously created UpdateServiceOperation, possibly from a different process.
324func (c *ServicesClient) UpdateServiceOperation(name string) *UpdateServiceOperation {
325	return &UpdateServiceOperation{
326		lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}),
327	}
328}
329
330// Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
331//
332// See documentation of Poll for error-handling information.
333func (op *UpdateServiceOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*appenginepb.Service, error) {
334	var resp appenginepb.Service
335	if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil {
336		return nil, err
337	}
338	return &resp, nil
339}
340
341// Poll fetches the latest state of the long-running operation.
342//
343// Poll also fetches the latest metadata, which can be retrieved by Metadata.
344//
345// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
346// the operation has completed with failure, the error is returned and op.Done will return true.
347// If Poll succeeds and the operation has completed successfully,
348// op.Done will return true, and the response of the operation is returned.
349// If Poll succeeds and the operation has not completed, the returned response and error are both nil.
350func (op *UpdateServiceOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*appenginepb.Service, error) {
351	var resp appenginepb.Service
352	if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
353		return nil, err
354	}
355	if !op.Done() {
356		return nil, nil
357	}
358	return &resp, nil
359}
360
361// Metadata returns metadata associated with the long-running operation.
362// Metadata itself does not contact the server, but Poll does.
363// To get the latest metadata, call this method after a successful call to Poll.
364// If the metadata is not available, the returned metadata and error are both nil.
365func (op *UpdateServiceOperation) Metadata() (*appenginepb.OperationMetadataV1, error) {
366	var meta appenginepb.OperationMetadataV1
367	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
368		return nil, nil
369	} else if err != nil {
370		return nil, err
371	}
372	return &meta, nil
373}
374
375// Done reports whether the long-running operation has completed.
376func (op *UpdateServiceOperation) Done() bool {
377	return op.lro.Done()
378}
379
380// Name returns the name of the long-running operation.
381// The name is assigned by the server and is unique within the service from which the operation is created.
382func (op *UpdateServiceOperation) Name() string {
383	return op.lro.Name()
384}
385
386// ServiceIterator manages a stream of *appenginepb.Service.
387type ServiceIterator struct {
388	items    []*appenginepb.Service
389	pageInfo *iterator.PageInfo
390	nextFunc func() error
391
392	// Response is the raw response for the current page.
393	// It must be cast to the RPC response type.
394	// Calling Next() or InternalFetch() updates this value.
395	Response interface{}
396
397	// InternalFetch is for use by the Google Cloud Libraries only.
398	// It is not part of the stable interface of this package.
399	//
400	// InternalFetch returns results from a single call to the underlying RPC.
401	// The number of results is no greater than pageSize.
402	// If there are no more results, nextPageToken is empty and err is nil.
403	InternalFetch func(pageSize int, pageToken string) (results []*appenginepb.Service, nextPageToken string, err error)
404}
405
406// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
407func (it *ServiceIterator) PageInfo() *iterator.PageInfo {
408	return it.pageInfo
409}
410
411// Next returns the next result. Its second return value is iterator.Done if there are no more
412// results. Once Next returns Done, all subsequent calls will return Done.
413func (it *ServiceIterator) Next() (*appenginepb.Service, error) {
414	var item *appenginepb.Service
415	if err := it.nextFunc(); err != nil {
416		return item, err
417	}
418	item = it.items[0]
419	it.items = it.items[1:]
420	return item, nil
421}
422
423func (it *ServiceIterator) bufLen() int {
424	return len(it.items)
425}
426
427func (it *ServiceIterator) takeBuf() interface{} {
428	b := it.items
429	it.items = nil
430	return b
431}
432