1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package monitoring
18
19 import (
20 "context"
21 "fmt"
22 "math"
23 "net/url"
24 "time"
25
26 monitoringpb "cloud.google.com/go/monitoring/apiv3/v2/monitoringpb"
27 gax "github.com/googleapis/gax-go/v2"
28 "google.golang.org/api/iterator"
29 "google.golang.org/api/option"
30 "google.golang.org/api/option/internaloption"
31 gtransport "google.golang.org/api/transport/grpc"
32 "google.golang.org/grpc"
33 "google.golang.org/grpc/codes"
34 "google.golang.org/protobuf/proto"
35 )
36
37 var newAlertPolicyClientHook clientHook
38
39
40 type AlertPolicyCallOptions struct {
41 ListAlertPolicies []gax.CallOption
42 GetAlertPolicy []gax.CallOption
43 CreateAlertPolicy []gax.CallOption
44 DeleteAlertPolicy []gax.CallOption
45 UpdateAlertPolicy []gax.CallOption
46 }
47
48 func defaultAlertPolicyGRPCClientOptions() []option.ClientOption {
49 return []option.ClientOption{
50 internaloption.WithDefaultEndpoint("monitoring.googleapis.com:443"),
51 internaloption.WithDefaultEndpointTemplate("monitoring.UNIVERSE_DOMAIN:443"),
52 internaloption.WithDefaultMTLSEndpoint("monitoring.mtls.googleapis.com:443"),
53 internaloption.WithDefaultUniverseDomain("googleapis.com"),
54 internaloption.WithDefaultAudience("https://monitoring.googleapis.com/"),
55 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
56 internaloption.EnableJwtWithScope(),
57 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
58 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
59 }
60 }
61
62 func defaultAlertPolicyCallOptions() *AlertPolicyCallOptions {
63 return &AlertPolicyCallOptions{
64 ListAlertPolicies: []gax.CallOption{
65 gax.WithTimeout(30000 * time.Millisecond),
66 gax.WithRetry(func() gax.Retryer {
67 return gax.OnCodes([]codes.Code{
68 codes.Unavailable,
69 }, gax.Backoff{
70 Initial: 100 * time.Millisecond,
71 Max: 30000 * time.Millisecond,
72 Multiplier: 1.30,
73 })
74 }),
75 },
76 GetAlertPolicy: []gax.CallOption{
77 gax.WithTimeout(30000 * time.Millisecond),
78 gax.WithRetry(func() gax.Retryer {
79 return gax.OnCodes([]codes.Code{
80 codes.Unavailable,
81 }, gax.Backoff{
82 Initial: 100 * time.Millisecond,
83 Max: 30000 * time.Millisecond,
84 Multiplier: 1.30,
85 })
86 }),
87 },
88 CreateAlertPolicy: []gax.CallOption{
89 gax.WithTimeout(30000 * time.Millisecond),
90 },
91 DeleteAlertPolicy: []gax.CallOption{
92 gax.WithTimeout(30000 * time.Millisecond),
93 gax.WithRetry(func() gax.Retryer {
94 return gax.OnCodes([]codes.Code{
95 codes.Unavailable,
96 }, gax.Backoff{
97 Initial: 100 * time.Millisecond,
98 Max: 30000 * time.Millisecond,
99 Multiplier: 1.30,
100 })
101 }),
102 },
103 UpdateAlertPolicy: []gax.CallOption{
104 gax.WithTimeout(30000 * time.Millisecond),
105 },
106 }
107 }
108
109
110 type internalAlertPolicyClient interface {
111 Close() error
112 setGoogleClientInfo(...string)
113 Connection() *grpc.ClientConn
114 ListAlertPolicies(context.Context, *monitoringpb.ListAlertPoliciesRequest, ...gax.CallOption) *AlertPolicyIterator
115 GetAlertPolicy(context.Context, *monitoringpb.GetAlertPolicyRequest, ...gax.CallOption) (*monitoringpb.AlertPolicy, error)
116 CreateAlertPolicy(context.Context, *monitoringpb.CreateAlertPolicyRequest, ...gax.CallOption) (*monitoringpb.AlertPolicy, error)
117 DeleteAlertPolicy(context.Context, *monitoringpb.DeleteAlertPolicyRequest, ...gax.CallOption) error
118 UpdateAlertPolicy(context.Context, *monitoringpb.UpdateAlertPolicyRequest, ...gax.CallOption) (*monitoringpb.AlertPolicy, error)
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133 type AlertPolicyClient struct {
134
135 internalClient internalAlertPolicyClient
136
137
138 CallOptions *AlertPolicyCallOptions
139 }
140
141
142
143
144
145 func (c *AlertPolicyClient) Close() error {
146 return c.internalClient.Close()
147 }
148
149
150
151
152 func (c *AlertPolicyClient) setGoogleClientInfo(keyval ...string) {
153 c.internalClient.setGoogleClientInfo(keyval...)
154 }
155
156
157
158
159
160 func (c *AlertPolicyClient) Connection() *grpc.ClientConn {
161 return c.internalClient.Connection()
162 }
163
164
165 func (c *AlertPolicyClient) ListAlertPolicies(ctx context.Context, req *monitoringpb.ListAlertPoliciesRequest, opts ...gax.CallOption) *AlertPolicyIterator {
166 return c.internalClient.ListAlertPolicies(ctx, req, opts...)
167 }
168
169
170 func (c *AlertPolicyClient) GetAlertPolicy(ctx context.Context, req *monitoringpb.GetAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
171 return c.internalClient.GetAlertPolicy(ctx, req, opts...)
172 }
173
174
175
176
177
178
179 func (c *AlertPolicyClient) CreateAlertPolicy(ctx context.Context, req *monitoringpb.CreateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
180 return c.internalClient.CreateAlertPolicy(ctx, req, opts...)
181 }
182
183
184
185
186
187
188 func (c *AlertPolicyClient) DeleteAlertPolicy(ctx context.Context, req *monitoringpb.DeleteAlertPolicyRequest, opts ...gax.CallOption) error {
189 return c.internalClient.DeleteAlertPolicy(ctx, req, opts...)
190 }
191
192
193
194
195
196
197
198
199
200 func (c *AlertPolicyClient) UpdateAlertPolicy(ctx context.Context, req *monitoringpb.UpdateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
201 return c.internalClient.UpdateAlertPolicy(ctx, req, opts...)
202 }
203
204
205
206
207 type alertPolicyGRPCClient struct {
208
209 connPool gtransport.ConnPool
210
211
212 CallOptions **AlertPolicyCallOptions
213
214
215 alertPolicyClient monitoringpb.AlertPolicyServiceClient
216
217
218 xGoogHeaders []string
219 }
220
221
222
223
224
225
226
227
228
229
230
231
232
233 func NewAlertPolicyClient(ctx context.Context, opts ...option.ClientOption) (*AlertPolicyClient, error) {
234 clientOpts := defaultAlertPolicyGRPCClientOptions()
235 if newAlertPolicyClientHook != nil {
236 hookOpts, err := newAlertPolicyClientHook(ctx, clientHookParams{})
237 if err != nil {
238 return nil, err
239 }
240 clientOpts = append(clientOpts, hookOpts...)
241 }
242
243 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
244 if err != nil {
245 return nil, err
246 }
247 client := AlertPolicyClient{CallOptions: defaultAlertPolicyCallOptions()}
248
249 c := &alertPolicyGRPCClient{
250 connPool: connPool,
251 alertPolicyClient: monitoringpb.NewAlertPolicyServiceClient(connPool),
252 CallOptions: &client.CallOptions,
253 }
254 c.setGoogleClientInfo()
255
256 client.internalClient = c
257
258 return &client, nil
259 }
260
261
262
263
264
265 func (c *alertPolicyGRPCClient) Connection() *grpc.ClientConn {
266 return c.connPool.Conn()
267 }
268
269
270
271
272 func (c *alertPolicyGRPCClient) setGoogleClientInfo(keyval ...string) {
273 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
274 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
275 c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
276 }
277
278
279
280 func (c *alertPolicyGRPCClient) Close() error {
281 return c.connPool.Close()
282 }
283
284 func (c *alertPolicyGRPCClient) ListAlertPolicies(ctx context.Context, req *monitoringpb.ListAlertPoliciesRequest, opts ...gax.CallOption) *AlertPolicyIterator {
285 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
286
287 hds = append(c.xGoogHeaders, hds...)
288 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
289 opts = append((*c.CallOptions).ListAlertPolicies[0:len((*c.CallOptions).ListAlertPolicies):len((*c.CallOptions).ListAlertPolicies)], opts...)
290 it := &AlertPolicyIterator{}
291 req = proto.Clone(req).(*monitoringpb.ListAlertPoliciesRequest)
292 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.AlertPolicy, string, error) {
293 resp := &monitoringpb.ListAlertPoliciesResponse{}
294 if pageToken != "" {
295 req.PageToken = pageToken
296 }
297 if pageSize > math.MaxInt32 {
298 req.PageSize = math.MaxInt32
299 } else if pageSize != 0 {
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.alertPolicyClient.ListAlertPolicies(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.GetAlertPolicies(), resp.GetNextPageToken(), 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
323 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
324 it.pageInfo.MaxSize = int(req.GetPageSize())
325 it.pageInfo.Token = req.GetPageToken()
326
327 return it
328 }
329
330 func (c *alertPolicyGRPCClient) GetAlertPolicy(ctx context.Context, req *monitoringpb.GetAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
331 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
332
333 hds = append(c.xGoogHeaders, hds...)
334 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
335 opts = append((*c.CallOptions).GetAlertPolicy[0:len((*c.CallOptions).GetAlertPolicy):len((*c.CallOptions).GetAlertPolicy)], opts...)
336 var resp *monitoringpb.AlertPolicy
337 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
338 var err error
339 resp, err = c.alertPolicyClient.GetAlertPolicy(ctx, req, settings.GRPC...)
340 return err
341 }, opts...)
342 if err != nil {
343 return nil, err
344 }
345 return resp, nil
346 }
347
348 func (c *alertPolicyGRPCClient) CreateAlertPolicy(ctx context.Context, req *monitoringpb.CreateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
349 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
350
351 hds = append(c.xGoogHeaders, hds...)
352 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
353 opts = append((*c.CallOptions).CreateAlertPolicy[0:len((*c.CallOptions).CreateAlertPolicy):len((*c.CallOptions).CreateAlertPolicy)], opts...)
354 var resp *monitoringpb.AlertPolicy
355 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
356 var err error
357 resp, err = c.alertPolicyClient.CreateAlertPolicy(ctx, req, settings.GRPC...)
358 return err
359 }, opts...)
360 if err != nil {
361 return nil, err
362 }
363 return resp, nil
364 }
365
366 func (c *alertPolicyGRPCClient) DeleteAlertPolicy(ctx context.Context, req *monitoringpb.DeleteAlertPolicyRequest, opts ...gax.CallOption) error {
367 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
368
369 hds = append(c.xGoogHeaders, hds...)
370 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
371 opts = append((*c.CallOptions).DeleteAlertPolicy[0:len((*c.CallOptions).DeleteAlertPolicy):len((*c.CallOptions).DeleteAlertPolicy)], opts...)
372 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
373 var err error
374 _, err = c.alertPolicyClient.DeleteAlertPolicy(ctx, req, settings.GRPC...)
375 return err
376 }, opts...)
377 return err
378 }
379
380 func (c *alertPolicyGRPCClient) UpdateAlertPolicy(ctx context.Context, req *monitoringpb.UpdateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
381 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "alert_policy.name", url.QueryEscape(req.GetAlertPolicy().GetName()))}
382
383 hds = append(c.xGoogHeaders, hds...)
384 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
385 opts = append((*c.CallOptions).UpdateAlertPolicy[0:len((*c.CallOptions).UpdateAlertPolicy):len((*c.CallOptions).UpdateAlertPolicy)], opts...)
386 var resp *monitoringpb.AlertPolicy
387 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
388 var err error
389 resp, err = c.alertPolicyClient.UpdateAlertPolicy(ctx, req, settings.GRPC...)
390 return err
391 }, opts...)
392 if err != nil {
393 return nil, err
394 }
395 return resp, nil
396 }
397
View as plain text