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 "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 gtransport "google.golang.org/api/transport/grpc"
31 "google.golang.org/grpc"
32 "google.golang.org/grpc/codes"
33 "google.golang.org/grpc/metadata"
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 defaultAlertPolicyClientOptions() []option.ClientOption {
49 return []option.ClientOption{
50 option.WithEndpoint("monitoring.googleapis.com:443"),
51 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
52 option.WithScopes(DefaultAuthScopes()...),
53 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
54 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
55 }
56 }
57
58 func defaultAlertPolicyCallOptions() *AlertPolicyCallOptions {
59 return &AlertPolicyCallOptions{
60 ListAlertPolicies: []gax.CallOption{
61 gax.WithRetry(func() gax.Retryer {
62 return gax.OnCodes([]codes.Code{
63 codes.DeadlineExceeded,
64 codes.Unavailable,
65 }, gax.Backoff{
66 Initial: 100 * time.Millisecond,
67 Max: 30000 * time.Millisecond,
68 Multiplier: 1.30,
69 })
70 }),
71 },
72 GetAlertPolicy: []gax.CallOption{
73 gax.WithRetry(func() gax.Retryer {
74 return gax.OnCodes([]codes.Code{
75 codes.DeadlineExceeded,
76 codes.Unavailable,
77 }, gax.Backoff{
78 Initial: 100 * time.Millisecond,
79 Max: 30000 * time.Millisecond,
80 Multiplier: 1.30,
81 })
82 }),
83 },
84 CreateAlertPolicy: []gax.CallOption{},
85 DeleteAlertPolicy: []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 UpdateAlertPolicy: []gax.CallOption{},
98 }
99 }
100
101
102
103
104 type AlertPolicyClient struct {
105
106 connPool gtransport.ConnPool
107
108
109 alertPolicyClient monitoringpb.AlertPolicyServiceClient
110
111
112 CallOptions *AlertPolicyCallOptions
113
114
115 xGoogMetadata metadata.MD
116 }
117
118
119
120
121
122
123
124
125
126
127
128
129 func NewAlertPolicyClient(ctx context.Context, opts ...option.ClientOption) (*AlertPolicyClient, error) {
130 clientOpts := defaultAlertPolicyClientOptions()
131
132 if newAlertPolicyClientHook != nil {
133 hookOpts, err := newAlertPolicyClientHook(ctx, clientHookParams{})
134 if err != nil {
135 return nil, err
136 }
137 clientOpts = append(clientOpts, hookOpts...)
138 }
139
140 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
141 if err != nil {
142 return nil, err
143 }
144 c := &AlertPolicyClient{
145 connPool: connPool,
146 CallOptions: defaultAlertPolicyCallOptions(),
147
148 alertPolicyClient: monitoringpb.NewAlertPolicyServiceClient(connPool),
149 }
150 c.setGoogleClientInfo()
151
152 return c, nil
153 }
154
155
156
157
158 func (c *AlertPolicyClient) Connection() *grpc.ClientConn {
159 return c.connPool.Conn()
160 }
161
162
163
164 func (c *AlertPolicyClient) Close() error {
165 return c.connPool.Close()
166 }
167
168
169
170
171 func (c *AlertPolicyClient) setGoogleClientInfo(keyval ...string) {
172 kv := append([]string{"gl-go", gax.GoVersion}, 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
178 func (c *AlertPolicyClient) ListAlertPolicies(ctx context.Context, req *monitoringpb.ListAlertPoliciesRequest, opts ...gax.CallOption) *AlertPolicyIterator {
179 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
180 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
181 opts = append(c.CallOptions.ListAlertPolicies[0:len(c.CallOptions.ListAlertPolicies):len(c.CallOptions.ListAlertPolicies)], opts...)
182 it := &AlertPolicyIterator{}
183 req = proto.Clone(req).(*monitoringpb.ListAlertPoliciesRequest)
184 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.AlertPolicy, string, error) {
185 var resp *monitoringpb.ListAlertPoliciesResponse
186 req.PageToken = pageToken
187 if pageSize > math.MaxInt32 {
188 req.PageSize = math.MaxInt32
189 } else {
190 req.PageSize = int32(pageSize)
191 }
192 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
193 var err error
194 resp, err = c.alertPolicyClient.ListAlertPolicies(ctx, req, settings.GRPC...)
195 return err
196 }, opts...)
197 if err != nil {
198 return nil, "", err
199 }
200
201 it.Response = resp
202 return resp.AlertPolicies, resp.NextPageToken, nil
203 }
204 fetch := func(pageSize int, pageToken string) (string, error) {
205 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
206 if err != nil {
207 return "", err
208 }
209 it.items = append(it.items, items...)
210 return nextPageToken, nil
211 }
212 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
213 it.pageInfo.MaxSize = int(req.PageSize)
214 it.pageInfo.Token = req.PageToken
215 return it
216 }
217
218
219 func (c *AlertPolicyClient) GetAlertPolicy(ctx context.Context, req *monitoringpb.GetAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
220 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
221 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
222 opts = append(c.CallOptions.GetAlertPolicy[0:len(c.CallOptions.GetAlertPolicy):len(c.CallOptions.GetAlertPolicy)], opts...)
223 var resp *monitoringpb.AlertPolicy
224 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
225 var err error
226 resp, err = c.alertPolicyClient.GetAlertPolicy(ctx, req, settings.GRPC...)
227 return err
228 }, opts...)
229 if err != nil {
230 return nil, err
231 }
232 return resp, nil
233 }
234
235
236 func (c *AlertPolicyClient) CreateAlertPolicy(ctx context.Context, req *monitoringpb.CreateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
237 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
238 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
239 opts = append(c.CallOptions.CreateAlertPolicy[0:len(c.CallOptions.CreateAlertPolicy):len(c.CallOptions.CreateAlertPolicy)], opts...)
240 var resp *monitoringpb.AlertPolicy
241 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
242 var err error
243 resp, err = c.alertPolicyClient.CreateAlertPolicy(ctx, req, settings.GRPC...)
244 return err
245 }, opts...)
246 if err != nil {
247 return nil, err
248 }
249 return resp, nil
250 }
251
252
253 func (c *AlertPolicyClient) DeleteAlertPolicy(ctx context.Context, req *monitoringpb.DeleteAlertPolicyRequest, opts ...gax.CallOption) error {
254 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
255 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
256 opts = append(c.CallOptions.DeleteAlertPolicy[0:len(c.CallOptions.DeleteAlertPolicy):len(c.CallOptions.DeleteAlertPolicy)], opts...)
257 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
258 var err error
259 _, err = c.alertPolicyClient.DeleteAlertPolicy(ctx, req, settings.GRPC...)
260 return err
261 }, opts...)
262 return err
263 }
264
265
266
267
268
269 func (c *AlertPolicyClient) UpdateAlertPolicy(ctx context.Context, req *monitoringpb.UpdateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
270 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "alert_policy.name", url.QueryEscape(req.GetAlertPolicy().GetName())))
271 ctx = insertMetadata(ctx, c.xGoogMetadata, md)
272 opts = append(c.CallOptions.UpdateAlertPolicy[0:len(c.CallOptions.UpdateAlertPolicy):len(c.CallOptions.UpdateAlertPolicy)], opts...)
273 var resp *monitoringpb.AlertPolicy
274 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
275 var err error
276 resp, err = c.alertPolicyClient.UpdateAlertPolicy(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
286 type AlertPolicyIterator struct {
287 items []*monitoringpb.AlertPolicy
288 pageInfo *iterator.PageInfo
289 nextFunc func() error
290
291
292
293
294 Response interface{}
295
296
297
298
299
300
301
302 InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.AlertPolicy, nextPageToken string, err error)
303 }
304
305
306 func (it *AlertPolicyIterator) PageInfo() *iterator.PageInfo {
307 return it.pageInfo
308 }
309
310
311
312 func (it *AlertPolicyIterator) Next() (*monitoringpb.AlertPolicy, error) {
313 var item *monitoringpb.AlertPolicy
314 if err := it.nextFunc(); err != nil {
315 return item, err
316 }
317 item = it.items[0]
318 it.items = it.items[1:]
319 return item, nil
320 }
321
322 func (it *AlertPolicyIterator) bufLen() int {
323 return len(it.items)
324 }
325
326 func (it *AlertPolicyIterator) takeBuf() interface{} {
327 b := it.items
328 it.items = nil
329 return b
330 }
331
View as plain text