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