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