1// Copyright 2018 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// AUTO-GENERATED CODE. DO NOT EDIT.
16
17package monitoring
18
19import (
20	"context"
21	"math"
22	"time"
23
24	"github.com/golang/protobuf/proto"
25	gax "github.com/googleapis/gax-go"
26	"google.golang.org/api/iterator"
27	"google.golang.org/api/option"
28	"google.golang.org/api/transport"
29	monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
30	monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
31	"google.golang.org/grpc"
32	"google.golang.org/grpc/codes"
33	"google.golang.org/grpc/metadata"
34)
35
36// GroupCallOptions contains the retry settings for each method of GroupClient.
37type GroupCallOptions struct {
38	ListGroups       []gax.CallOption
39	GetGroup         []gax.CallOption
40	CreateGroup      []gax.CallOption
41	UpdateGroup      []gax.CallOption
42	DeleteGroup      []gax.CallOption
43	ListGroupMembers []gax.CallOption
44}
45
46func defaultGroupClientOptions() []option.ClientOption {
47	return []option.ClientOption{
48		option.WithEndpoint("monitoring.googleapis.com:443"),
49		option.WithScopes(DefaultAuthScopes()...),
50	}
51}
52
53func defaultGroupCallOptions() *GroupCallOptions {
54	retry := map[[2]string][]gax.CallOption{
55		{"default", "idempotent"}: {
56			gax.WithRetry(func() gax.Retryer {
57				return gax.OnCodes([]codes.Code{
58					codes.DeadlineExceeded,
59					codes.Unavailable,
60				}, gax.Backoff{
61					Initial:    100 * time.Millisecond,
62					Max:        60000 * time.Millisecond,
63					Multiplier: 1.3,
64				})
65			}),
66		},
67	}
68	return &GroupCallOptions{
69		ListGroups:       retry[[2]string{"default", "idempotent"}],
70		GetGroup:         retry[[2]string{"default", "idempotent"}],
71		CreateGroup:      retry[[2]string{"default", "non_idempotent"}],
72		UpdateGroup:      retry[[2]string{"default", "idempotent"}],
73		DeleteGroup:      retry[[2]string{"default", "idempotent"}],
74		ListGroupMembers: retry[[2]string{"default", "idempotent"}],
75	}
76}
77
78// GroupClient is a client for interacting with Stackdriver Monitoring API.
79//
80// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
81type GroupClient struct {
82	// The connection to the service.
83	conn *grpc.ClientConn
84
85	// The gRPC API client.
86	groupClient monitoringpb.GroupServiceClient
87
88	// The call options for this service.
89	CallOptions *GroupCallOptions
90
91	// The x-goog-* metadata to be sent with each request.
92	xGoogMetadata metadata.MD
93}
94
95// NewGroupClient creates a new group service client.
96//
97// The Group API lets you inspect and manage your
98// groups (at #google.monitoring.v3.Group).
99//
100// A group is a named filter that is used to identify
101// a collection of monitored resources. Groups are typically used to
102// mirror the physical and/or logical topology of the environment.
103// Because group membership is computed dynamically, monitored
104// resources that are started in the future are automatically placed
105// in matching groups. By using a group to name monitored resources in,
106// for example, an alert policy, the target of that alert policy is
107// updated automatically as monitored resources are added and removed
108// from the infrastructure.
109func NewGroupClient(ctx context.Context, opts ...option.ClientOption) (*GroupClient, error) {
110	conn, err := transport.DialGRPC(ctx, append(defaultGroupClientOptions(), opts...)...)
111	if err != nil {
112		return nil, err
113	}
114	c := &GroupClient{
115		conn:        conn,
116		CallOptions: defaultGroupCallOptions(),
117
118		groupClient: monitoringpb.NewGroupServiceClient(conn),
119	}
120	c.setGoogleClientInfo()
121	return c, nil
122}
123
124// Connection returns the client's connection to the API service.
125func (c *GroupClient) Connection() *grpc.ClientConn {
126	return c.conn
127}
128
129// Close closes the connection to the API service. The user should invoke this when
130// the client is no longer required.
131func (c *GroupClient) Close() error {
132	return c.conn.Close()
133}
134
135// setGoogleClientInfo sets the name and version of the application in
136// the `x-goog-api-client` header passed on each request. Intended for
137// use by Google-written clients.
138func (c *GroupClient) setGoogleClientInfo(keyval ...string) {
139	kv := append([]string{"gl-go", versionGo()}, keyval...)
140	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
141	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
142}
143
144// ListGroups lists the existing groups.
145func (c *GroupClient) ListGroups(ctx context.Context, req *monitoringpb.ListGroupsRequest, opts ...gax.CallOption) *GroupIterator {
146	ctx = insertMetadata(ctx, c.xGoogMetadata)
147	opts = append(c.CallOptions.ListGroups[0:len(c.CallOptions.ListGroups):len(c.CallOptions.ListGroups)], opts...)
148	it := &GroupIterator{}
149	req = proto.Clone(req).(*monitoringpb.ListGroupsRequest)
150	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.Group, string, error) {
151		var resp *monitoringpb.ListGroupsResponse
152		req.PageToken = pageToken
153		if pageSize > math.MaxInt32 {
154			req.PageSize = math.MaxInt32
155		} else {
156			req.PageSize = int32(pageSize)
157		}
158		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
159			var err error
160			resp, err = c.groupClient.ListGroups(ctx, req, settings.GRPC...)
161			return err
162		}, opts...)
163		if err != nil {
164			return nil, "", err
165		}
166		return resp.Group, resp.NextPageToken, nil
167	}
168	fetch := func(pageSize int, pageToken string) (string, error) {
169		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
170		if err != nil {
171			return "", err
172		}
173		it.items = append(it.items, items...)
174		return nextPageToken, nil
175	}
176	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
177	it.pageInfo.MaxSize = int(req.PageSize)
178	return it
179}
180
181// GetGroup gets a single group.
182func (c *GroupClient) GetGroup(ctx context.Context, req *monitoringpb.GetGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
183	ctx = insertMetadata(ctx, c.xGoogMetadata)
184	opts = append(c.CallOptions.GetGroup[0:len(c.CallOptions.GetGroup):len(c.CallOptions.GetGroup)], opts...)
185	var resp *monitoringpb.Group
186	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
187		var err error
188		resp, err = c.groupClient.GetGroup(ctx, req, settings.GRPC...)
189		return err
190	}, opts...)
191	if err != nil {
192		return nil, err
193	}
194	return resp, nil
195}
196
197// CreateGroup creates a new group.
198func (c *GroupClient) CreateGroup(ctx context.Context, req *monitoringpb.CreateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
199	ctx = insertMetadata(ctx, c.xGoogMetadata)
200	opts = append(c.CallOptions.CreateGroup[0:len(c.CallOptions.CreateGroup):len(c.CallOptions.CreateGroup)], opts...)
201	var resp *monitoringpb.Group
202	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
203		var err error
204		resp, err = c.groupClient.CreateGroup(ctx, req, settings.GRPC...)
205		return err
206	}, opts...)
207	if err != nil {
208		return nil, err
209	}
210	return resp, nil
211}
212
213// UpdateGroup updates an existing group.
214// You can change any group attributes except name.
215func (c *GroupClient) UpdateGroup(ctx context.Context, req *monitoringpb.UpdateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
216	ctx = insertMetadata(ctx, c.xGoogMetadata)
217	opts = append(c.CallOptions.UpdateGroup[0:len(c.CallOptions.UpdateGroup):len(c.CallOptions.UpdateGroup)], opts...)
218	var resp *monitoringpb.Group
219	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
220		var err error
221		resp, err = c.groupClient.UpdateGroup(ctx, req, settings.GRPC...)
222		return err
223	}, opts...)
224	if err != nil {
225		return nil, err
226	}
227	return resp, nil
228}
229
230// DeleteGroup deletes an existing group.
231func (c *GroupClient) DeleteGroup(ctx context.Context, req *monitoringpb.DeleteGroupRequest, opts ...gax.CallOption) error {
232	ctx = insertMetadata(ctx, c.xGoogMetadata)
233	opts = append(c.CallOptions.DeleteGroup[0:len(c.CallOptions.DeleteGroup):len(c.CallOptions.DeleteGroup)], opts...)
234	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
235		var err error
236		_, err = c.groupClient.DeleteGroup(ctx, req, settings.GRPC...)
237		return err
238	}, opts...)
239	return err
240}
241
242// ListGroupMembers lists the monitored resources that are members of a group.
243func (c *GroupClient) ListGroupMembers(ctx context.Context, req *monitoringpb.ListGroupMembersRequest, opts ...gax.CallOption) *MonitoredResourceIterator {
244	ctx = insertMetadata(ctx, c.xGoogMetadata)
245	opts = append(c.CallOptions.ListGroupMembers[0:len(c.CallOptions.ListGroupMembers):len(c.CallOptions.ListGroupMembers)], opts...)
246	it := &MonitoredResourceIterator{}
247	req = proto.Clone(req).(*monitoringpb.ListGroupMembersRequest)
248	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoredrespb.MonitoredResource, string, error) {
249		var resp *monitoringpb.ListGroupMembersResponse
250		req.PageToken = pageToken
251		if pageSize > math.MaxInt32 {
252			req.PageSize = math.MaxInt32
253		} else {
254			req.PageSize = int32(pageSize)
255		}
256		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
257			var err error
258			resp, err = c.groupClient.ListGroupMembers(ctx, req, settings.GRPC...)
259			return err
260		}, opts...)
261		if err != nil {
262			return nil, "", err
263		}
264		return resp.Members, resp.NextPageToken, nil
265	}
266	fetch := func(pageSize int, pageToken string) (string, error) {
267		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
268		if err != nil {
269			return "", err
270		}
271		it.items = append(it.items, items...)
272		return nextPageToken, nil
273	}
274	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
275	it.pageInfo.MaxSize = int(req.PageSize)
276	return it
277}
278
279// GroupIterator manages a stream of *monitoringpb.Group.
280type GroupIterator struct {
281	items    []*monitoringpb.Group
282	pageInfo *iterator.PageInfo
283	nextFunc func() error
284
285	// InternalFetch is for use by the Google Cloud Libraries only.
286	// It is not part of the stable interface of this package.
287	//
288	// InternalFetch returns results from a single call to the underlying RPC.
289	// The number of results is no greater than pageSize.
290	// If there are no more results, nextPageToken is empty and err is nil.
291	InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.Group, nextPageToken string, err error)
292}
293
294// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
295func (it *GroupIterator) PageInfo() *iterator.PageInfo {
296	return it.pageInfo
297}
298
299// Next returns the next result. Its second return value is iterator.Done if there are no more
300// results. Once Next returns Done, all subsequent calls will return Done.
301func (it *GroupIterator) Next() (*monitoringpb.Group, error) {
302	var item *monitoringpb.Group
303	if err := it.nextFunc(); err != nil {
304		return item, err
305	}
306	item = it.items[0]
307	it.items = it.items[1:]
308	return item, nil
309}
310
311func (it *GroupIterator) bufLen() int {
312	return len(it.items)
313}
314
315func (it *GroupIterator) takeBuf() interface{} {
316	b := it.items
317	it.items = nil
318	return b
319}
320
321// MonitoredResourceIterator manages a stream of *monitoredrespb.MonitoredResource.
322type MonitoredResourceIterator struct {
323	items    []*monitoredrespb.MonitoredResource
324	pageInfo *iterator.PageInfo
325	nextFunc func() error
326
327	// InternalFetch is for use by the Google Cloud Libraries only.
328	// It is not part of the stable interface of this package.
329	//
330	// InternalFetch returns results from a single call to the underlying RPC.
331	// The number of results is no greater than pageSize.
332	// If there are no more results, nextPageToken is empty and err is nil.
333	InternalFetch func(pageSize int, pageToken string) (results []*monitoredrespb.MonitoredResource, nextPageToken string, err error)
334}
335
336// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
337func (it *MonitoredResourceIterator) PageInfo() *iterator.PageInfo {
338	return it.pageInfo
339}
340
341// Next returns the next result. Its second return value is iterator.Done if there are no more
342// results. Once Next returns Done, all subsequent calls will return Done.
343func (it *MonitoredResourceIterator) Next() (*monitoredrespb.MonitoredResource, error) {
344	var item *monitoredrespb.MonitoredResource
345	if err := it.nextFunc(); err != nil {
346		return item, err
347	}
348	item = it.items[0]
349	it.items = it.items[1:]
350	return item, nil
351}
352
353func (it *MonitoredResourceIterator) bufLen() int {
354	return len(it.items)
355}
356
357func (it *MonitoredResourceIterator) takeBuf() interface{} {
358	b := it.items
359	it.items = nil
360	return b
361}
362