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 monitoring
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	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	"google.golang.org/protobuf/proto"
36)
37
38var newAlertPolicyClientHook clientHook
39
40// AlertPolicyCallOptions contains the retry settings for each method of AlertPolicyClient.
41type AlertPolicyCallOptions struct {
42	ListAlertPolicies []gax.CallOption
43	GetAlertPolicy    []gax.CallOption
44	CreateAlertPolicy []gax.CallOption
45	DeleteAlertPolicy []gax.CallOption
46	UpdateAlertPolicy []gax.CallOption
47}
48
49func defaultAlertPolicyGRPCClientOptions() []option.ClientOption {
50	return []option.ClientOption{
51		internaloption.WithDefaultEndpoint("monitoring.googleapis.com:443"),
52		internaloption.WithDefaultMTLSEndpoint("monitoring.mtls.googleapis.com:443"),
53		internaloption.WithDefaultAudience("https://monitoring.googleapis.com/"),
54		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
55		internaloption.EnableJwtWithScope(),
56		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
57		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
58			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
59	}
60}
61
62func defaultAlertPolicyCallOptions() *AlertPolicyCallOptions {
63	return &AlertPolicyCallOptions{
64		ListAlertPolicies: []gax.CallOption{
65			gax.WithRetry(func() gax.Retryer {
66				return gax.OnCodes([]codes.Code{
67					codes.Unavailable,
68				}, gax.Backoff{
69					Initial:    100 * time.Millisecond,
70					Max:        30000 * time.Millisecond,
71					Multiplier: 1.30,
72				})
73			}),
74		},
75		GetAlertPolicy: []gax.CallOption{
76			gax.WithRetry(func() gax.Retryer {
77				return gax.OnCodes([]codes.Code{
78					codes.Unavailable,
79				}, gax.Backoff{
80					Initial:    100 * time.Millisecond,
81					Max:        30000 * time.Millisecond,
82					Multiplier: 1.30,
83				})
84			}),
85		},
86		CreateAlertPolicy: []gax.CallOption{},
87		DeleteAlertPolicy: []gax.CallOption{
88			gax.WithRetry(func() gax.Retryer {
89				return gax.OnCodes([]codes.Code{
90					codes.Unavailable,
91				}, gax.Backoff{
92					Initial:    100 * time.Millisecond,
93					Max:        30000 * time.Millisecond,
94					Multiplier: 1.30,
95				})
96			}),
97		},
98		UpdateAlertPolicy: []gax.CallOption{},
99	}
100}
101
102// internalAlertPolicyClient is an interface that defines the methods availaible from Cloud Monitoring API.
103type internalAlertPolicyClient interface {
104	Close() error
105	setGoogleClientInfo(...string)
106	Connection() *grpc.ClientConn
107	ListAlertPolicies(context.Context, *monitoringpb.ListAlertPoliciesRequest, ...gax.CallOption) *AlertPolicyIterator
108	GetAlertPolicy(context.Context, *monitoringpb.GetAlertPolicyRequest, ...gax.CallOption) (*monitoringpb.AlertPolicy, error)
109	CreateAlertPolicy(context.Context, *monitoringpb.CreateAlertPolicyRequest, ...gax.CallOption) (*monitoringpb.AlertPolicy, error)
110	DeleteAlertPolicy(context.Context, *monitoringpb.DeleteAlertPolicyRequest, ...gax.CallOption) error
111	UpdateAlertPolicy(context.Context, *monitoringpb.UpdateAlertPolicyRequest, ...gax.CallOption) (*monitoringpb.AlertPolicy, error)
112}
113
114// AlertPolicyClient is a client for interacting with Cloud Monitoring API.
115// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
116//
117// The AlertPolicyService API is used to manage (list, create, delete,
118// edit) alert policies in Stackdriver Monitoring. An alerting policy is
119// a description of the conditions under which some aspect of your
120// system is considered to be “unhealthy” and the ways to notify
121// people or services about this state. In addition to using this API, alert
122// policies can also be managed through
123// Stackdriver Monitoring (at https://cloud.google.com/monitoring/docs/),
124// which can be reached by clicking the “Monitoring” tab in
125// Cloud Console (at https://console.cloud.google.com/).
126type AlertPolicyClient struct {
127	// The internal transport-dependent client.
128	internalClient internalAlertPolicyClient
129
130	// The call options for this service.
131	CallOptions *AlertPolicyCallOptions
132}
133
134// Wrapper methods routed to the internal client.
135
136// Close closes the connection to the API service. The user should invoke this when
137// the client is no longer required.
138func (c *AlertPolicyClient) Close() error {
139	return c.internalClient.Close()
140}
141
142// setGoogleClientInfo sets the name and version of the application in
143// the `x-goog-api-client` header passed on each request. Intended for
144// use by Google-written clients.
145func (c *AlertPolicyClient) setGoogleClientInfo(keyval ...string) {
146	c.internalClient.setGoogleClientInfo(keyval...)
147}
148
149// Connection returns a connection to the API service.
150//
151// Deprecated.
152func (c *AlertPolicyClient) Connection() *grpc.ClientConn {
153	return c.internalClient.Connection()
154}
155
156// ListAlertPolicies lists the existing alerting policies for the workspace.
157func (c *AlertPolicyClient) ListAlertPolicies(ctx context.Context, req *monitoringpb.ListAlertPoliciesRequest, opts ...gax.CallOption) *AlertPolicyIterator {
158	return c.internalClient.ListAlertPolicies(ctx, req, opts...)
159}
160
161// GetAlertPolicy gets a single alerting policy.
162func (c *AlertPolicyClient) GetAlertPolicy(ctx context.Context, req *monitoringpb.GetAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
163	return c.internalClient.GetAlertPolicy(ctx, req, opts...)
164}
165
166// CreateAlertPolicy creates a new alerting policy.
167func (c *AlertPolicyClient) CreateAlertPolicy(ctx context.Context, req *monitoringpb.CreateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
168	return c.internalClient.CreateAlertPolicy(ctx, req, opts...)
169}
170
171// DeleteAlertPolicy deletes an alerting policy.
172func (c *AlertPolicyClient) DeleteAlertPolicy(ctx context.Context, req *monitoringpb.DeleteAlertPolicyRequest, opts ...gax.CallOption) error {
173	return c.internalClient.DeleteAlertPolicy(ctx, req, opts...)
174}
175
176// UpdateAlertPolicy updates an alerting policy. You can either replace the entire policy with
177// a new one or replace only certain fields in the current alerting policy by
178// specifying the fields to be updated via updateMask. Returns the
179// updated alerting policy.
180func (c *AlertPolicyClient) UpdateAlertPolicy(ctx context.Context, req *monitoringpb.UpdateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
181	return c.internalClient.UpdateAlertPolicy(ctx, req, opts...)
182}
183
184// alertPolicyGRPCClient is a client for interacting with Cloud Monitoring API over gRPC transport.
185//
186// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
187type alertPolicyGRPCClient struct {
188	// Connection pool of gRPC connections to the service.
189	connPool gtransport.ConnPool
190
191	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
192	disableDeadlines bool
193
194	// Points back to the CallOptions field of the containing AlertPolicyClient
195	CallOptions **AlertPolicyCallOptions
196
197	// The gRPC API client.
198	alertPolicyClient monitoringpb.AlertPolicyServiceClient
199
200	// The x-goog-* metadata to be sent with each request.
201	xGoogMetadata metadata.MD
202}
203
204// NewAlertPolicyClient creates a new alert policy service client based on gRPC.
205// The returned client must be Closed when it is done being used to clean up its underlying connections.
206//
207// The AlertPolicyService API is used to manage (list, create, delete,
208// edit) alert policies in Stackdriver Monitoring. An alerting policy is
209// a description of the conditions under which some aspect of your
210// system is considered to be “unhealthy” and the ways to notify
211// people or services about this state. In addition to using this API, alert
212// policies can also be managed through
213// Stackdriver Monitoring (at https://cloud.google.com/monitoring/docs/),
214// which can be reached by clicking the “Monitoring” tab in
215// Cloud Console (at https://console.cloud.google.com/).
216func NewAlertPolicyClient(ctx context.Context, opts ...option.ClientOption) (*AlertPolicyClient, error) {
217	clientOpts := defaultAlertPolicyGRPCClientOptions()
218	if newAlertPolicyClientHook != nil {
219		hookOpts, err := newAlertPolicyClientHook(ctx, clientHookParams{})
220		if err != nil {
221			return nil, err
222		}
223		clientOpts = append(clientOpts, hookOpts...)
224	}
225
226	disableDeadlines, err := checkDisableDeadlines()
227	if err != nil {
228		return nil, err
229	}
230
231	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
232	if err != nil {
233		return nil, err
234	}
235	client := AlertPolicyClient{CallOptions: defaultAlertPolicyCallOptions()}
236
237	c := &alertPolicyGRPCClient{
238		connPool:          connPool,
239		disableDeadlines:  disableDeadlines,
240		alertPolicyClient: monitoringpb.NewAlertPolicyServiceClient(connPool),
241		CallOptions:       &client.CallOptions,
242	}
243	c.setGoogleClientInfo()
244
245	client.internalClient = c
246
247	return &client, nil
248}
249
250// Connection returns a connection to the API service.
251//
252// Deprecated.
253func (c *alertPolicyGRPCClient) Connection() *grpc.ClientConn {
254	return c.connPool.Conn()
255}
256
257// setGoogleClientInfo sets the name and version of the application in
258// the `x-goog-api-client` header passed on each request. Intended for
259// use by Google-written clients.
260func (c *alertPolicyGRPCClient) setGoogleClientInfo(keyval ...string) {
261	kv := append([]string{"gl-go", versionGo()}, keyval...)
262	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
263	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
264}
265
266// Close closes the connection to the API service. The user should invoke this when
267// the client is no longer required.
268func (c *alertPolicyGRPCClient) Close() error {
269	return c.connPool.Close()
270}
271
272func (c *alertPolicyGRPCClient) ListAlertPolicies(ctx context.Context, req *monitoringpb.ListAlertPoliciesRequest, opts ...gax.CallOption) *AlertPolicyIterator {
273	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
274	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
275	opts = append((*c.CallOptions).ListAlertPolicies[0:len((*c.CallOptions).ListAlertPolicies):len((*c.CallOptions).ListAlertPolicies)], opts...)
276	it := &AlertPolicyIterator{}
277	req = proto.Clone(req).(*monitoringpb.ListAlertPoliciesRequest)
278	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.AlertPolicy, string, error) {
279		resp := &monitoringpb.ListAlertPoliciesResponse{}
280		if pageToken != "" {
281			req.PageToken = pageToken
282		}
283		if pageSize > math.MaxInt32 {
284			req.PageSize = math.MaxInt32
285		} else if pageSize != 0 {
286			req.PageSize = int32(pageSize)
287		}
288		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
289			var err error
290			resp, err = c.alertPolicyClient.ListAlertPolicies(ctx, req, settings.GRPC...)
291			return err
292		}, opts...)
293		if err != nil {
294			return nil, "", err
295		}
296
297		it.Response = resp
298		return resp.GetAlertPolicies(), resp.GetNextPageToken(), nil
299	}
300	fetch := func(pageSize int, pageToken string) (string, error) {
301		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
302		if err != nil {
303			return "", err
304		}
305		it.items = append(it.items, items...)
306		return nextPageToken, nil
307	}
308
309	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
310	it.pageInfo.MaxSize = int(req.GetPageSize())
311	it.pageInfo.Token = req.GetPageToken()
312
313	return it
314}
315
316func (c *alertPolicyGRPCClient) GetAlertPolicy(ctx context.Context, req *monitoringpb.GetAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
317	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
318		cctx, cancel := context.WithTimeout(ctx, 30000*time.Millisecond)
319		defer cancel()
320		ctx = cctx
321	}
322	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
323	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
324	opts = append((*c.CallOptions).GetAlertPolicy[0:len((*c.CallOptions).GetAlertPolicy):len((*c.CallOptions).GetAlertPolicy)], opts...)
325	var resp *monitoringpb.AlertPolicy
326	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
327		var err error
328		resp, err = c.alertPolicyClient.GetAlertPolicy(ctx, req, settings.GRPC...)
329		return err
330	}, opts...)
331	if err != nil {
332		return nil, err
333	}
334	return resp, nil
335}
336
337func (c *alertPolicyGRPCClient) CreateAlertPolicy(ctx context.Context, req *monitoringpb.CreateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
338	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
339		cctx, cancel := context.WithTimeout(ctx, 30000*time.Millisecond)
340		defer cancel()
341		ctx = cctx
342	}
343	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
344	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
345	opts = append((*c.CallOptions).CreateAlertPolicy[0:len((*c.CallOptions).CreateAlertPolicy):len((*c.CallOptions).CreateAlertPolicy)], opts...)
346	var resp *monitoringpb.AlertPolicy
347	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
348		var err error
349		resp, err = c.alertPolicyClient.CreateAlertPolicy(ctx, req, settings.GRPC...)
350		return err
351	}, opts...)
352	if err != nil {
353		return nil, err
354	}
355	return resp, nil
356}
357
358func (c *alertPolicyGRPCClient) DeleteAlertPolicy(ctx context.Context, req *monitoringpb.DeleteAlertPolicyRequest, opts ...gax.CallOption) error {
359	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
360		cctx, cancel := context.WithTimeout(ctx, 30000*time.Millisecond)
361		defer cancel()
362		ctx = cctx
363	}
364	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
365	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
366	opts = append((*c.CallOptions).DeleteAlertPolicy[0:len((*c.CallOptions).DeleteAlertPolicy):len((*c.CallOptions).DeleteAlertPolicy)], opts...)
367	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
368		var err error
369		_, err = c.alertPolicyClient.DeleteAlertPolicy(ctx, req, settings.GRPC...)
370		return err
371	}, opts...)
372	return err
373}
374
375func (c *alertPolicyGRPCClient) UpdateAlertPolicy(ctx context.Context, req *monitoringpb.UpdateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
376	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
377		cctx, cancel := context.WithTimeout(ctx, 30000*time.Millisecond)
378		defer cancel()
379		ctx = cctx
380	}
381	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "alert_policy.name", url.QueryEscape(req.GetAlertPolicy().GetName())))
382	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
383	opts = append((*c.CallOptions).UpdateAlertPolicy[0:len((*c.CallOptions).UpdateAlertPolicy):len((*c.CallOptions).UpdateAlertPolicy)], opts...)
384	var resp *monitoringpb.AlertPolicy
385	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
386		var err error
387		resp, err = c.alertPolicyClient.UpdateAlertPolicy(ctx, req, settings.GRPC...)
388		return err
389	}, opts...)
390	if err != nil {
391		return nil, err
392	}
393	return resp, nil
394}
395
396// AlertPolicyIterator manages a stream of *monitoringpb.AlertPolicy.
397type AlertPolicyIterator struct {
398	items    []*monitoringpb.AlertPolicy
399	pageInfo *iterator.PageInfo
400	nextFunc func() error
401
402	// Response is the raw response for the current page.
403	// It must be cast to the RPC response type.
404	// Calling Next() or InternalFetch() updates this value.
405	Response interface{}
406
407	// InternalFetch is for use by the Google Cloud Libraries only.
408	// It is not part of the stable interface of this package.
409	//
410	// InternalFetch returns results from a single call to the underlying RPC.
411	// The number of results is no greater than pageSize.
412	// If there are no more results, nextPageToken is empty and err is nil.
413	InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.AlertPolicy, nextPageToken string, err error)
414}
415
416// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
417func (it *AlertPolicyIterator) PageInfo() *iterator.PageInfo {
418	return it.pageInfo
419}
420
421// Next returns the next result. Its second return value is iterator.Done if there are no more
422// results. Once Next returns Done, all subsequent calls will return Done.
423func (it *AlertPolicyIterator) Next() (*monitoringpb.AlertPolicy, error) {
424	var item *monitoringpb.AlertPolicy
425	if err := it.nextFunc(); err != nil {
426		return item, err
427	}
428	item = it.items[0]
429	it.items = it.items[1:]
430	return item, nil
431}
432
433func (it *AlertPolicyIterator) bufLen() int {
434	return len(it.items)
435}
436
437func (it *AlertPolicyIterator) takeBuf() interface{} {
438	b := it.items
439	it.items = nil
440	return b
441}
442