1// Copyright 2020 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Code generated by protoc-gen-go_gapic. DO NOT EDIT.
16
17package monitoring
18
19import (
20	"context"
21	"fmt"
22	"math"
23	"net/url"
24	"time"
25
26	"github.com/golang/protobuf/proto"
27	gax "github.com/googleapis/gax-go/v2"
28	"google.golang.org/api/iterator"
29	"google.golang.org/api/option"
30	gtransport "google.golang.org/api/transport/grpc"
31	monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
32	"google.golang.org/grpc"
33	"google.golang.org/grpc/codes"
34	"google.golang.org/grpc/metadata"
35)
36
37var newUptimeCheckClientHook clientHook
38
39// UptimeCheckCallOptions contains the retry settings for each method of UptimeCheckClient.
40type UptimeCheckCallOptions struct {
41	ListUptimeCheckConfigs  []gax.CallOption
42	GetUptimeCheckConfig    []gax.CallOption
43	CreateUptimeCheckConfig []gax.CallOption
44	UpdateUptimeCheckConfig []gax.CallOption
45	DeleteUptimeCheckConfig []gax.CallOption
46	ListUptimeCheckIps      []gax.CallOption
47}
48
49func defaultUptimeCheckClientOptions() []option.ClientOption {
50	return []option.ClientOption{
51		option.WithEndpoint("monitoring.googleapis.com:443"),
52		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
53		option.WithScopes(DefaultAuthScopes()...),
54		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
55			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
56	}
57}
58
59func defaultUptimeCheckCallOptions() *UptimeCheckCallOptions {
60	return &UptimeCheckCallOptions{
61		ListUptimeCheckConfigs: []gax.CallOption{
62			gax.WithRetry(func() gax.Retryer {
63				return gax.OnCodes([]codes.Code{
64					codes.DeadlineExceeded,
65					codes.Unavailable,
66				}, gax.Backoff{
67					Initial:    100 * time.Millisecond,
68					Max:        30000 * time.Millisecond,
69					Multiplier: 1.30,
70				})
71			}),
72		},
73		GetUptimeCheckConfig: []gax.CallOption{
74			gax.WithRetry(func() gax.Retryer {
75				return gax.OnCodes([]codes.Code{
76					codes.DeadlineExceeded,
77					codes.Unavailable,
78				}, gax.Backoff{
79					Initial:    100 * time.Millisecond,
80					Max:        30000 * time.Millisecond,
81					Multiplier: 1.30,
82				})
83			}),
84		},
85		CreateUptimeCheckConfig: []gax.CallOption{},
86		UpdateUptimeCheckConfig: []gax.CallOption{},
87		DeleteUptimeCheckConfig: []gax.CallOption{
88			gax.WithRetry(func() gax.Retryer {
89				return gax.OnCodes([]codes.Code{
90					codes.DeadlineExceeded,
91					codes.Unavailable,
92				}, gax.Backoff{
93					Initial:    100 * time.Millisecond,
94					Max:        30000 * time.Millisecond,
95					Multiplier: 1.30,
96				})
97			}),
98		},
99		ListUptimeCheckIps: []gax.CallOption{
100			gax.WithRetry(func() gax.Retryer {
101				return gax.OnCodes([]codes.Code{
102					codes.DeadlineExceeded,
103					codes.Unavailable,
104				}, gax.Backoff{
105					Initial:    100 * time.Millisecond,
106					Max:        30000 * time.Millisecond,
107					Multiplier: 1.30,
108				})
109			}),
110		},
111	}
112}
113
114// UptimeCheckClient is a client for interacting with Cloud Monitoring API.
115//
116// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
117type UptimeCheckClient struct {
118	// Connection pool of gRPC connections to the service.
119	connPool gtransport.ConnPool
120
121	// The gRPC API client.
122	uptimeCheckClient monitoringpb.UptimeCheckServiceClient
123
124	// The call options for this service.
125	CallOptions *UptimeCheckCallOptions
126
127	// The x-goog-* metadata to be sent with each request.
128	xGoogMetadata metadata.MD
129}
130
131// NewUptimeCheckClient creates a new uptime check service client.
132//
133// The UptimeCheckService API is used to manage (list, create, delete, edit)
134// Uptime check configurations in the Stackdriver Monitoring product. An Uptime
135// check is a piece of configuration that determines which resources and
136// services to monitor for availability. These configurations can also be
137// configured interactively by navigating to the [Cloud Console]
138// (http://console.cloud.google.com (at http://console.cloud.google.com)), selecting the appropriate project,
139// clicking on “Monitoring” on the left-hand side to navigate to Stackdriver,
140// and then clicking on “Uptime”.
141func NewUptimeCheckClient(ctx context.Context, opts ...option.ClientOption) (*UptimeCheckClient, error) {
142	clientOpts := defaultUptimeCheckClientOptions()
143
144	if newUptimeCheckClientHook != nil {
145		hookOpts, err := newUptimeCheckClientHook(ctx, clientHookParams{})
146		if err != nil {
147			return nil, err
148		}
149		clientOpts = append(clientOpts, hookOpts...)
150	}
151
152	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
153	if err != nil {
154		return nil, err
155	}
156	c := &UptimeCheckClient{
157		connPool:    connPool,
158		CallOptions: defaultUptimeCheckCallOptions(),
159
160		uptimeCheckClient: monitoringpb.NewUptimeCheckServiceClient(connPool),
161	}
162	c.setGoogleClientInfo()
163
164	return c, nil
165}
166
167// Connection returns a connection to the API service.
168//
169// Deprecated.
170func (c *UptimeCheckClient) Connection() *grpc.ClientConn {
171	return c.connPool.Conn()
172}
173
174// Close closes the connection to the API service. The user should invoke this when
175// the client is no longer required.
176func (c *UptimeCheckClient) Close() error {
177	return c.connPool.Close()
178}
179
180// setGoogleClientInfo sets the name and version of the application in
181// the `x-goog-api-client` header passed on each request. Intended for
182// use by Google-written clients.
183func (c *UptimeCheckClient) setGoogleClientInfo(keyval ...string) {
184	kv := append([]string{"gl-go", versionGo()}, keyval...)
185	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
186	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
187}
188
189// ListUptimeCheckConfigs lists the existing valid Uptime check configurations for the project
190// (leaving out any invalid configurations).
191func (c *UptimeCheckClient) ListUptimeCheckConfigs(ctx context.Context, req *monitoringpb.ListUptimeCheckConfigsRequest, opts ...gax.CallOption) *UptimeCheckConfigIterator {
192	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
193	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
194	opts = append(c.CallOptions.ListUptimeCheckConfigs[0:len(c.CallOptions.ListUptimeCheckConfigs):len(c.CallOptions.ListUptimeCheckConfigs)], opts...)
195	it := &UptimeCheckConfigIterator{}
196	req = proto.Clone(req).(*monitoringpb.ListUptimeCheckConfigsRequest)
197	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.UptimeCheckConfig, string, error) {
198		var resp *monitoringpb.ListUptimeCheckConfigsResponse
199		req.PageToken = pageToken
200		if pageSize > math.MaxInt32 {
201			req.PageSize = math.MaxInt32
202		} else {
203			req.PageSize = int32(pageSize)
204		}
205		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
206			var err error
207			resp, err = c.uptimeCheckClient.ListUptimeCheckConfigs(ctx, req, settings.GRPC...)
208			return err
209		}, opts...)
210		if err != nil {
211			return nil, "", err
212		}
213
214		it.Response = resp
215		return resp.UptimeCheckConfigs, resp.NextPageToken, nil
216	}
217	fetch := func(pageSize int, pageToken string) (string, error) {
218		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
219		if err != nil {
220			return "", err
221		}
222		it.items = append(it.items, items...)
223		return nextPageToken, nil
224	}
225	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
226	it.pageInfo.MaxSize = int(req.PageSize)
227	it.pageInfo.Token = req.PageToken
228	return it
229}
230
231// GetUptimeCheckConfig gets a single Uptime check configuration.
232func (c *UptimeCheckClient) GetUptimeCheckConfig(ctx context.Context, req *monitoringpb.GetUptimeCheckConfigRequest, opts ...gax.CallOption) (*monitoringpb.UptimeCheckConfig, error) {
233	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
234	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
235	opts = append(c.CallOptions.GetUptimeCheckConfig[0:len(c.CallOptions.GetUptimeCheckConfig):len(c.CallOptions.GetUptimeCheckConfig)], opts...)
236	var resp *monitoringpb.UptimeCheckConfig
237	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
238		var err error
239		resp, err = c.uptimeCheckClient.GetUptimeCheckConfig(ctx, req, settings.GRPC...)
240		return err
241	}, opts...)
242	if err != nil {
243		return nil, err
244	}
245	return resp, nil
246}
247
248// CreateUptimeCheckConfig creates a new Uptime check configuration.
249func (c *UptimeCheckClient) CreateUptimeCheckConfig(ctx context.Context, req *monitoringpb.CreateUptimeCheckConfigRequest, opts ...gax.CallOption) (*monitoringpb.UptimeCheckConfig, error) {
250	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
251	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
252	opts = append(c.CallOptions.CreateUptimeCheckConfig[0:len(c.CallOptions.CreateUptimeCheckConfig):len(c.CallOptions.CreateUptimeCheckConfig)], opts...)
253	var resp *monitoringpb.UptimeCheckConfig
254	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
255		var err error
256		resp, err = c.uptimeCheckClient.CreateUptimeCheckConfig(ctx, req, settings.GRPC...)
257		return err
258	}, opts...)
259	if err != nil {
260		return nil, err
261	}
262	return resp, nil
263}
264
265// UpdateUptimeCheckConfig updates an Uptime check configuration. You can either replace the entire
266// configuration with a new one or replace only certain fields in the current
267// configuration by specifying the fields to be updated via updateMask.
268// Returns the updated configuration.
269func (c *UptimeCheckClient) UpdateUptimeCheckConfig(ctx context.Context, req *monitoringpb.UpdateUptimeCheckConfigRequest, opts ...gax.CallOption) (*monitoringpb.UptimeCheckConfig, error) {
270	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "uptime_check_config.name", url.QueryEscape(req.GetUptimeCheckConfig().GetName())))
271	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
272	opts = append(c.CallOptions.UpdateUptimeCheckConfig[0:len(c.CallOptions.UpdateUptimeCheckConfig):len(c.CallOptions.UpdateUptimeCheckConfig)], opts...)
273	var resp *monitoringpb.UptimeCheckConfig
274	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
275		var err error
276		resp, err = c.uptimeCheckClient.UpdateUptimeCheckConfig(ctx, req, settings.GRPC...)
277		return err
278	}, opts...)
279	if err != nil {
280		return nil, err
281	}
282	return resp, nil
283}
284
285// DeleteUptimeCheckConfig deletes an Uptime check configuration. Note that this method will fail
286// if the Uptime check configuration is referenced by an alert policy or
287// other dependent configs that would be rendered invalid by the deletion.
288func (c *UptimeCheckClient) DeleteUptimeCheckConfig(ctx context.Context, req *monitoringpb.DeleteUptimeCheckConfigRequest, 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.DeleteUptimeCheckConfig[0:len(c.CallOptions.DeleteUptimeCheckConfig):len(c.CallOptions.DeleteUptimeCheckConfig)], opts...)
292	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
293		var err error
294		_, err = c.uptimeCheckClient.DeleteUptimeCheckConfig(ctx, req, settings.GRPC...)
295		return err
296	}, opts...)
297	return err
298}
299
300// ListUptimeCheckIps returns the list of IP addresses that checkers run from
301func (c *UptimeCheckClient) ListUptimeCheckIps(ctx context.Context, req *monitoringpb.ListUptimeCheckIpsRequest, opts ...gax.CallOption) *UptimeCheckIpIterator {
302	ctx = insertMetadata(ctx, c.xGoogMetadata)
303	opts = append(c.CallOptions.ListUptimeCheckIps[0:len(c.CallOptions.ListUptimeCheckIps):len(c.CallOptions.ListUptimeCheckIps)], opts...)
304	it := &UptimeCheckIpIterator{}
305	req = proto.Clone(req).(*monitoringpb.ListUptimeCheckIpsRequest)
306	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.UptimeCheckIp, string, error) {
307		var resp *monitoringpb.ListUptimeCheckIpsResponse
308		req.PageToken = pageToken
309		if pageSize > math.MaxInt32 {
310			req.PageSize = math.MaxInt32
311		} else {
312			req.PageSize = int32(pageSize)
313		}
314		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
315			var err error
316			resp, err = c.uptimeCheckClient.ListUptimeCheckIps(ctx, req, settings.GRPC...)
317			return err
318		}, opts...)
319		if err != nil {
320			return nil, "", err
321		}
322
323		it.Response = resp
324		return resp.UptimeCheckIps, resp.NextPageToken, nil
325	}
326	fetch := func(pageSize int, pageToken string) (string, error) {
327		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
328		if err != nil {
329			return "", err
330		}
331		it.items = append(it.items, items...)
332		return nextPageToken, nil
333	}
334	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
335	it.pageInfo.MaxSize = int(req.PageSize)
336	it.pageInfo.Token = req.PageToken
337	return it
338}
339
340// UptimeCheckConfigIterator manages a stream of *monitoringpb.UptimeCheckConfig.
341type UptimeCheckConfigIterator struct {
342	items    []*monitoringpb.UptimeCheckConfig
343	pageInfo *iterator.PageInfo
344	nextFunc func() error
345
346	// Response is the raw response for the current page.
347	// It must be cast to the RPC response type.
348	// Calling Next() or InternalFetch() updates this value.
349	Response interface{}
350
351	// InternalFetch is for use by the Google Cloud Libraries only.
352	// It is not part of the stable interface of this package.
353	//
354	// InternalFetch returns results from a single call to the underlying RPC.
355	// The number of results is no greater than pageSize.
356	// If there are no more results, nextPageToken is empty and err is nil.
357	InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.UptimeCheckConfig, nextPageToken string, err error)
358}
359
360// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
361func (it *UptimeCheckConfigIterator) PageInfo() *iterator.PageInfo {
362	return it.pageInfo
363}
364
365// Next returns the next result. Its second return value is iterator.Done if there are no more
366// results. Once Next returns Done, all subsequent calls will return Done.
367func (it *UptimeCheckConfigIterator) Next() (*monitoringpb.UptimeCheckConfig, error) {
368	var item *monitoringpb.UptimeCheckConfig
369	if err := it.nextFunc(); err != nil {
370		return item, err
371	}
372	item = it.items[0]
373	it.items = it.items[1:]
374	return item, nil
375}
376
377func (it *UptimeCheckConfigIterator) bufLen() int {
378	return len(it.items)
379}
380
381func (it *UptimeCheckConfigIterator) takeBuf() interface{} {
382	b := it.items
383	it.items = nil
384	return b
385}
386
387// UptimeCheckIpIterator manages a stream of *monitoringpb.UptimeCheckIp.
388type UptimeCheckIpIterator struct {
389	items    []*monitoringpb.UptimeCheckIp
390	pageInfo *iterator.PageInfo
391	nextFunc func() error
392
393	// Response is the raw response for the current page.
394	// It must be cast to the RPC response type.
395	// Calling Next() or InternalFetch() updates this value.
396	Response interface{}
397
398	// InternalFetch is for use by the Google Cloud Libraries only.
399	// It is not part of the stable interface of this package.
400	//
401	// InternalFetch returns results from a single call to the underlying RPC.
402	// The number of results is no greater than pageSize.
403	// If there are no more results, nextPageToken is empty and err is nil.
404	InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.UptimeCheckIp, nextPageToken string, err error)
405}
406
407// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
408func (it *UptimeCheckIpIterator) PageInfo() *iterator.PageInfo {
409	return it.pageInfo
410}
411
412// Next returns the next result. Its second return value is iterator.Done if there are no more
413// results. Once Next returns Done, all subsequent calls will return Done.
414func (it *UptimeCheckIpIterator) Next() (*monitoringpb.UptimeCheckIp, error) {
415	var item *monitoringpb.UptimeCheckIp
416	if err := it.nextFunc(); err != nil {
417		return item, err
418	}
419	item = it.items[0]
420	it.items = it.items[1:]
421	return item, nil
422}
423
424func (it *UptimeCheckIpIterator) bufLen() int {
425	return len(it.items)
426}
427
428func (it *UptimeCheckIpIterator) takeBuf() interface{} {
429	b := it.items
430	it.items = nil
431	return b
432}
433