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 monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
33 "google.golang.org/grpc"
34 "google.golang.org/grpc/codes"
35 "google.golang.org/protobuf/proto"
36 )
37
38 var newGroupClientHook clientHook
39
40
41 type GroupCallOptions struct {
42 ListGroups []gax.CallOption
43 GetGroup []gax.CallOption
44 CreateGroup []gax.CallOption
45 UpdateGroup []gax.CallOption
46 DeleteGroup []gax.CallOption
47 ListGroupMembers []gax.CallOption
48 }
49
50 func defaultGroupGRPCClientOptions() []option.ClientOption {
51 return []option.ClientOption{
52 internaloption.WithDefaultEndpoint("monitoring.googleapis.com:443"),
53 internaloption.WithDefaultEndpointTemplate("monitoring.UNIVERSE_DOMAIN:443"),
54 internaloption.WithDefaultMTLSEndpoint("monitoring.mtls.googleapis.com:443"),
55 internaloption.WithDefaultUniverseDomain("googleapis.com"),
56 internaloption.WithDefaultAudience("https://monitoring.googleapis.com/"),
57 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
58 internaloption.EnableJwtWithScope(),
59 option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
60 grpc.MaxCallRecvMsgSize(math.MaxInt32))),
61 }
62 }
63
64 func defaultGroupCallOptions() *GroupCallOptions {
65 return &GroupCallOptions{
66 ListGroups: []gax.CallOption{
67 gax.WithTimeout(30000 * time.Millisecond),
68 gax.WithRetry(func() gax.Retryer {
69 return gax.OnCodes([]codes.Code{
70 codes.Unavailable,
71 }, gax.Backoff{
72 Initial: 100 * time.Millisecond,
73 Max: 30000 * time.Millisecond,
74 Multiplier: 1.30,
75 })
76 }),
77 },
78 GetGroup: []gax.CallOption{
79 gax.WithTimeout(30000 * time.Millisecond),
80 gax.WithRetry(func() gax.Retryer {
81 return gax.OnCodes([]codes.Code{
82 codes.Unavailable,
83 }, gax.Backoff{
84 Initial: 100 * time.Millisecond,
85 Max: 30000 * time.Millisecond,
86 Multiplier: 1.30,
87 })
88 }),
89 },
90 CreateGroup: []gax.CallOption{
91 gax.WithTimeout(30000 * time.Millisecond),
92 },
93 UpdateGroup: []gax.CallOption{
94 gax.WithTimeout(180000 * time.Millisecond),
95 gax.WithRetry(func() gax.Retryer {
96 return gax.OnCodes([]codes.Code{
97 codes.Unavailable,
98 }, gax.Backoff{
99 Initial: 100 * time.Millisecond,
100 Max: 30000 * time.Millisecond,
101 Multiplier: 1.30,
102 })
103 }),
104 },
105 DeleteGroup: []gax.CallOption{
106 gax.WithTimeout(30000 * time.Millisecond),
107 gax.WithRetry(func() gax.Retryer {
108 return gax.OnCodes([]codes.Code{
109 codes.Unavailable,
110 }, gax.Backoff{
111 Initial: 100 * time.Millisecond,
112 Max: 30000 * time.Millisecond,
113 Multiplier: 1.30,
114 })
115 }),
116 },
117 ListGroupMembers: []gax.CallOption{
118 gax.WithTimeout(30000 * time.Millisecond),
119 gax.WithRetry(func() gax.Retryer {
120 return gax.OnCodes([]codes.Code{
121 codes.Unavailable,
122 }, gax.Backoff{
123 Initial: 100 * time.Millisecond,
124 Max: 30000 * time.Millisecond,
125 Multiplier: 1.30,
126 })
127 }),
128 },
129 }
130 }
131
132
133 type internalGroupClient interface {
134 Close() error
135 setGoogleClientInfo(...string)
136 Connection() *grpc.ClientConn
137 ListGroups(context.Context, *monitoringpb.ListGroupsRequest, ...gax.CallOption) *GroupIterator
138 GetGroup(context.Context, *monitoringpb.GetGroupRequest, ...gax.CallOption) (*monitoringpb.Group, error)
139 CreateGroup(context.Context, *monitoringpb.CreateGroupRequest, ...gax.CallOption) (*monitoringpb.Group, error)
140 UpdateGroup(context.Context, *monitoringpb.UpdateGroupRequest, ...gax.CallOption) (*monitoringpb.Group, error)
141 DeleteGroup(context.Context, *monitoringpb.DeleteGroupRequest, ...gax.CallOption) error
142 ListGroupMembers(context.Context, *monitoringpb.ListGroupMembersRequest, ...gax.CallOption) *MonitoredResourceIterator
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160 type GroupClient struct {
161
162 internalClient internalGroupClient
163
164
165 CallOptions *GroupCallOptions
166 }
167
168
169
170
171
172 func (c *GroupClient) Close() error {
173 return c.internalClient.Close()
174 }
175
176
177
178
179 func (c *GroupClient) setGoogleClientInfo(keyval ...string) {
180 c.internalClient.setGoogleClientInfo(keyval...)
181 }
182
183
184
185
186
187 func (c *GroupClient) Connection() *grpc.ClientConn {
188 return c.internalClient.Connection()
189 }
190
191
192 func (c *GroupClient) ListGroups(ctx context.Context, req *monitoringpb.ListGroupsRequest, opts ...gax.CallOption) *GroupIterator {
193 return c.internalClient.ListGroups(ctx, req, opts...)
194 }
195
196
197 func (c *GroupClient) GetGroup(ctx context.Context, req *monitoringpb.GetGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
198 return c.internalClient.GetGroup(ctx, req, opts...)
199 }
200
201
202 func (c *GroupClient) CreateGroup(ctx context.Context, req *monitoringpb.CreateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
203 return c.internalClient.CreateGroup(ctx, req, opts...)
204 }
205
206
207
208 func (c *GroupClient) UpdateGroup(ctx context.Context, req *monitoringpb.UpdateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
209 return c.internalClient.UpdateGroup(ctx, req, opts...)
210 }
211
212
213 func (c *GroupClient) DeleteGroup(ctx context.Context, req *monitoringpb.DeleteGroupRequest, opts ...gax.CallOption) error {
214 return c.internalClient.DeleteGroup(ctx, req, opts...)
215 }
216
217
218 func (c *GroupClient) ListGroupMembers(ctx context.Context, req *monitoringpb.ListGroupMembersRequest, opts ...gax.CallOption) *MonitoredResourceIterator {
219 return c.internalClient.ListGroupMembers(ctx, req, opts...)
220 }
221
222
223
224
225 type groupGRPCClient struct {
226
227 connPool gtransport.ConnPool
228
229
230 CallOptions **GroupCallOptions
231
232
233 groupClient monitoringpb.GroupServiceClient
234
235
236 xGoogHeaders []string
237 }
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 func NewGroupClient(ctx context.Context, opts ...option.ClientOption) (*GroupClient, error) {
255 clientOpts := defaultGroupGRPCClientOptions()
256 if newGroupClientHook != nil {
257 hookOpts, err := newGroupClientHook(ctx, clientHookParams{})
258 if err != nil {
259 return nil, err
260 }
261 clientOpts = append(clientOpts, hookOpts...)
262 }
263
264 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
265 if err != nil {
266 return nil, err
267 }
268 client := GroupClient{CallOptions: defaultGroupCallOptions()}
269
270 c := &groupGRPCClient{
271 connPool: connPool,
272 groupClient: monitoringpb.NewGroupServiceClient(connPool),
273 CallOptions: &client.CallOptions,
274 }
275 c.setGoogleClientInfo()
276
277 client.internalClient = c
278
279 return &client, nil
280 }
281
282
283
284
285
286 func (c *groupGRPCClient) Connection() *grpc.ClientConn {
287 return c.connPool.Conn()
288 }
289
290
291
292
293 func (c *groupGRPCClient) setGoogleClientInfo(keyval ...string) {
294 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
295 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
296 c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
297 }
298
299
300
301 func (c *groupGRPCClient) Close() error {
302 return c.connPool.Close()
303 }
304
305 func (c *groupGRPCClient) ListGroups(ctx context.Context, req *monitoringpb.ListGroupsRequest, opts ...gax.CallOption) *GroupIterator {
306 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
307
308 hds = append(c.xGoogHeaders, hds...)
309 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
310 opts = append((*c.CallOptions).ListGroups[0:len((*c.CallOptions).ListGroups):len((*c.CallOptions).ListGroups)], opts...)
311 it := &GroupIterator{}
312 req = proto.Clone(req).(*monitoringpb.ListGroupsRequest)
313 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.Group, string, error) {
314 resp := &monitoringpb.ListGroupsResponse{}
315 if pageToken != "" {
316 req.PageToken = pageToken
317 }
318 if pageSize > math.MaxInt32 {
319 req.PageSize = math.MaxInt32
320 } else if pageSize != 0 {
321 req.PageSize = int32(pageSize)
322 }
323 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
324 var err error
325 resp, err = c.groupClient.ListGroups(ctx, req, settings.GRPC...)
326 return err
327 }, opts...)
328 if err != nil {
329 return nil, "", err
330 }
331
332 it.Response = resp
333 return resp.GetGroup(), resp.GetNextPageToken(), nil
334 }
335 fetch := func(pageSize int, pageToken string) (string, error) {
336 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
337 if err != nil {
338 return "", err
339 }
340 it.items = append(it.items, items...)
341 return nextPageToken, nil
342 }
343
344 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
345 it.pageInfo.MaxSize = int(req.GetPageSize())
346 it.pageInfo.Token = req.GetPageToken()
347
348 return it
349 }
350
351 func (c *groupGRPCClient) GetGroup(ctx context.Context, req *monitoringpb.GetGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
352 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
353
354 hds = append(c.xGoogHeaders, hds...)
355 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
356 opts = append((*c.CallOptions).GetGroup[0:len((*c.CallOptions).GetGroup):len((*c.CallOptions).GetGroup)], opts...)
357 var resp *monitoringpb.Group
358 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
359 var err error
360 resp, err = c.groupClient.GetGroup(ctx, req, settings.GRPC...)
361 return err
362 }, opts...)
363 if err != nil {
364 return nil, err
365 }
366 return resp, nil
367 }
368
369 func (c *groupGRPCClient) CreateGroup(ctx context.Context, req *monitoringpb.CreateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
370 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
371
372 hds = append(c.xGoogHeaders, hds...)
373 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
374 opts = append((*c.CallOptions).CreateGroup[0:len((*c.CallOptions).CreateGroup):len((*c.CallOptions).CreateGroup)], opts...)
375 var resp *monitoringpb.Group
376 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
377 var err error
378 resp, err = c.groupClient.CreateGroup(ctx, req, settings.GRPC...)
379 return err
380 }, opts...)
381 if err != nil {
382 return nil, err
383 }
384 return resp, nil
385 }
386
387 func (c *groupGRPCClient) UpdateGroup(ctx context.Context, req *monitoringpb.UpdateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
388 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "group.name", url.QueryEscape(req.GetGroup().GetName()))}
389
390 hds = append(c.xGoogHeaders, hds...)
391 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
392 opts = append((*c.CallOptions).UpdateGroup[0:len((*c.CallOptions).UpdateGroup):len((*c.CallOptions).UpdateGroup)], opts...)
393 var resp *monitoringpb.Group
394 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
395 var err error
396 resp, err = c.groupClient.UpdateGroup(ctx, req, settings.GRPC...)
397 return err
398 }, opts...)
399 if err != nil {
400 return nil, err
401 }
402 return resp, nil
403 }
404
405 func (c *groupGRPCClient) DeleteGroup(ctx context.Context, req *monitoringpb.DeleteGroupRequest, opts ...gax.CallOption) error {
406 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
407
408 hds = append(c.xGoogHeaders, hds...)
409 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
410 opts = append((*c.CallOptions).DeleteGroup[0:len((*c.CallOptions).DeleteGroup):len((*c.CallOptions).DeleteGroup)], opts...)
411 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
412 var err error
413 _, err = c.groupClient.DeleteGroup(ctx, req, settings.GRPC...)
414 return err
415 }, opts...)
416 return err
417 }
418
419 func (c *groupGRPCClient) ListGroupMembers(ctx context.Context, req *monitoringpb.ListGroupMembersRequest, opts ...gax.CallOption) *MonitoredResourceIterator {
420 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
421
422 hds = append(c.xGoogHeaders, hds...)
423 ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
424 opts = append((*c.CallOptions).ListGroupMembers[0:len((*c.CallOptions).ListGroupMembers):len((*c.CallOptions).ListGroupMembers)], opts...)
425 it := &MonitoredResourceIterator{}
426 req = proto.Clone(req).(*monitoringpb.ListGroupMembersRequest)
427 it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoredrespb.MonitoredResource, string, error) {
428 resp := &monitoringpb.ListGroupMembersResponse{}
429 if pageToken != "" {
430 req.PageToken = pageToken
431 }
432 if pageSize > math.MaxInt32 {
433 req.PageSize = math.MaxInt32
434 } else if pageSize != 0 {
435 req.PageSize = int32(pageSize)
436 }
437 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
438 var err error
439 resp, err = c.groupClient.ListGroupMembers(ctx, req, settings.GRPC...)
440 return err
441 }, opts...)
442 if err != nil {
443 return nil, "", err
444 }
445
446 it.Response = resp
447 return resp.GetMembers(), resp.GetNextPageToken(), nil
448 }
449 fetch := func(pageSize int, pageToken string) (string, error) {
450 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
451 if err != nil {
452 return "", err
453 }
454 it.items = append(it.items, items...)
455 return nextPageToken, nil
456 }
457
458 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
459 it.pageInfo.MaxSize = int(req.GetPageSize())
460 it.pageInfo.Token = req.GetPageToken()
461
462 return it
463 }
464
View as plain text