1// Copyright 2019 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 gapic-generator. DO NOT EDIT.
16
17package logging
18
19import (
20	"context"
21	"fmt"
22	"math"
23	"net/url"
24	"time"
25
26	"github.com/golang/protobuf/proto"
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/transport"
31	loggingpb "google.golang.org/genproto/googleapis/logging/v2"
32	"google.golang.org/grpc"
33	"google.golang.org/grpc/codes"
34	"google.golang.org/grpc/metadata"
35)
36
37// ConfigCallOptions contains the retry settings for each method of ConfigClient.
38type ConfigCallOptions struct {
39	ListSinks       []gax.CallOption
40	GetSink         []gax.CallOption
41	CreateSink      []gax.CallOption
42	UpdateSink      []gax.CallOption
43	DeleteSink      []gax.CallOption
44	ListExclusions  []gax.CallOption
45	GetExclusion    []gax.CallOption
46	CreateExclusion []gax.CallOption
47	UpdateExclusion []gax.CallOption
48	DeleteExclusion []gax.CallOption
49}
50
51func defaultConfigClientOptions() []option.ClientOption {
52	return []option.ClientOption{
53		option.WithEndpoint("logging.googleapis.com:443"),
54		option.WithScopes(DefaultAuthScopes()...),
55	}
56}
57
58func defaultConfigCallOptions() *ConfigCallOptions {
59	retry := map[[2]string][]gax.CallOption{
60		{"default", "idempotent"}: {
61			gax.WithRetry(func() gax.Retryer {
62				return gax.OnCodes([]codes.Code{
63					codes.DeadlineExceeded,
64					codes.Internal,
65					codes.Unavailable,
66				}, gax.Backoff{
67					Initial:    100 * time.Millisecond,
68					Max:        60000 * time.Millisecond,
69					Multiplier: 1.3,
70				})
71			}),
72		},
73	}
74	return &ConfigCallOptions{
75		ListSinks:       retry[[2]string{"default", "idempotent"}],
76		GetSink:         retry[[2]string{"default", "idempotent"}],
77		CreateSink:      retry[[2]string{"default", "non_idempotent"}],
78		UpdateSink:      retry[[2]string{"default", "idempotent"}],
79		DeleteSink:      retry[[2]string{"default", "idempotent"}],
80		ListExclusions:  retry[[2]string{"default", "idempotent"}],
81		GetExclusion:    retry[[2]string{"default", "idempotent"}],
82		CreateExclusion: retry[[2]string{"default", "non_idempotent"}],
83		UpdateExclusion: retry[[2]string{"default", "non_idempotent"}],
84		DeleteExclusion: retry[[2]string{"default", "idempotent"}],
85	}
86}
87
88// ConfigClient is a client for interacting with Stackdriver Logging API.
89//
90// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
91type ConfigClient struct {
92	// The connection to the service.
93	conn *grpc.ClientConn
94
95	// The gRPC API client.
96	configClient loggingpb.ConfigServiceV2Client
97
98	// The call options for this service.
99	CallOptions *ConfigCallOptions
100
101	// The x-goog-* metadata to be sent with each request.
102	xGoogMetadata metadata.MD
103}
104
105// NewConfigClient creates a new config service v2 client.
106//
107// Service for configuring sinks used to export log entries out of
108// Logging.
109func NewConfigClient(ctx context.Context, opts ...option.ClientOption) (*ConfigClient, error) {
110	conn, err := transport.DialGRPC(ctx, append(defaultConfigClientOptions(), opts...)...)
111	if err != nil {
112		return nil, err
113	}
114	c := &ConfigClient{
115		conn:        conn,
116		CallOptions: defaultConfigCallOptions(),
117
118		configClient: loggingpb.NewConfigServiceV2Client(conn),
119	}
120	c.SetGoogleClientInfo()
121	return c, nil
122}
123
124// Connection returns the client's connection to the API service.
125func (c *ConfigClient) 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 *ConfigClient) 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 *ConfigClient) 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// ListSinks lists sinks.
145func (c *ConfigClient) ListSinks(ctx context.Context, req *loggingpb.ListSinksRequest, opts ...gax.CallOption) *LogSinkIterator {
146	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
147	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
148	opts = append(c.CallOptions.ListSinks[0:len(c.CallOptions.ListSinks):len(c.CallOptions.ListSinks)], opts...)
149	it := &LogSinkIterator{}
150	req = proto.Clone(req).(*loggingpb.ListSinksRequest)
151	it.InternalFetch = func(pageSize int, pageToken string) ([]*loggingpb.LogSink, string, error) {
152		var resp *loggingpb.ListSinksResponse
153		req.PageToken = pageToken
154		if pageSize > math.MaxInt32 {
155			req.PageSize = math.MaxInt32
156		} else {
157			req.PageSize = int32(pageSize)
158		}
159		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
160			var err error
161			resp, err = c.configClient.ListSinks(ctx, req, settings.GRPC...)
162			return err
163		}, opts...)
164		if err != nil {
165			return nil, "", err
166		}
167		return resp.Sinks, resp.NextPageToken, nil
168	}
169	fetch := func(pageSize int, pageToken string) (string, error) {
170		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
171		if err != nil {
172			return "", err
173		}
174		it.items = append(it.items, items...)
175		return nextPageToken, nil
176	}
177	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
178	it.pageInfo.MaxSize = int(req.PageSize)
179	it.pageInfo.Token = req.PageToken
180	return it
181}
182
183// GetSink gets a sink.
184func (c *ConfigClient) GetSink(ctx context.Context, req *loggingpb.GetSinkRequest, opts ...gax.CallOption) (*loggingpb.LogSink, error) {
185	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "sink_name", url.QueryEscape(req.GetSinkName())))
186	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
187	opts = append(c.CallOptions.GetSink[0:len(c.CallOptions.GetSink):len(c.CallOptions.GetSink)], opts...)
188	var resp *loggingpb.LogSink
189	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
190		var err error
191		resp, err = c.configClient.GetSink(ctx, req, settings.GRPC...)
192		return err
193	}, opts...)
194	if err != nil {
195		return nil, err
196	}
197	return resp, nil
198}
199
200// CreateSink creates a sink that exports specified log entries to a destination.  The
201// export of newly-ingested log entries begins immediately, unless the sink's
202// writer_identity is not permitted to write to the destination.  A sink can
203// export log entries only from the resource owning the sink.
204func (c *ConfigClient) CreateSink(ctx context.Context, req *loggingpb.CreateSinkRequest, opts ...gax.CallOption) (*loggingpb.LogSink, error) {
205	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
206	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
207	opts = append(c.CallOptions.CreateSink[0:len(c.CallOptions.CreateSink):len(c.CallOptions.CreateSink)], opts...)
208	var resp *loggingpb.LogSink
209	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
210		var err error
211		resp, err = c.configClient.CreateSink(ctx, req, settings.GRPC...)
212		return err
213	}, opts...)
214	if err != nil {
215		return nil, err
216	}
217	return resp, nil
218}
219
220// UpdateSink updates a sink.  This method replaces the following fields in the existing
221// sink with values from the new sink: destination, and filter.
222// The updated sink might also have a new writer_identity; see the
223// unique_writer_identity field.
224func (c *ConfigClient) UpdateSink(ctx context.Context, req *loggingpb.UpdateSinkRequest, opts ...gax.CallOption) (*loggingpb.LogSink, error) {
225	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "sink_name", url.QueryEscape(req.GetSinkName())))
226	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
227	opts = append(c.CallOptions.UpdateSink[0:len(c.CallOptions.UpdateSink):len(c.CallOptions.UpdateSink)], opts...)
228	var resp *loggingpb.LogSink
229	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
230		var err error
231		resp, err = c.configClient.UpdateSink(ctx, req, settings.GRPC...)
232		return err
233	}, opts...)
234	if err != nil {
235		return nil, err
236	}
237	return resp, nil
238}
239
240// DeleteSink deletes a sink. If the sink has a unique writer_identity, then that
241// service account is also deleted.
242func (c *ConfigClient) DeleteSink(ctx context.Context, req *loggingpb.DeleteSinkRequest, opts ...gax.CallOption) error {
243	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "sink_name", url.QueryEscape(req.GetSinkName())))
244	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
245	opts = append(c.CallOptions.DeleteSink[0:len(c.CallOptions.DeleteSink):len(c.CallOptions.DeleteSink)], opts...)
246	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
247		var err error
248		_, err = c.configClient.DeleteSink(ctx, req, settings.GRPC...)
249		return err
250	}, opts...)
251	return err
252}
253
254// ListExclusions lists all the exclusions in a parent resource.
255func (c *ConfigClient) ListExclusions(ctx context.Context, req *loggingpb.ListExclusionsRequest, opts ...gax.CallOption) *LogExclusionIterator {
256	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
257	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
258	opts = append(c.CallOptions.ListExclusions[0:len(c.CallOptions.ListExclusions):len(c.CallOptions.ListExclusions)], opts...)
259	it := &LogExclusionIterator{}
260	req = proto.Clone(req).(*loggingpb.ListExclusionsRequest)
261	it.InternalFetch = func(pageSize int, pageToken string) ([]*loggingpb.LogExclusion, string, error) {
262		var resp *loggingpb.ListExclusionsResponse
263		req.PageToken = pageToken
264		if pageSize > math.MaxInt32 {
265			req.PageSize = math.MaxInt32
266		} else {
267			req.PageSize = int32(pageSize)
268		}
269		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
270			var err error
271			resp, err = c.configClient.ListExclusions(ctx, req, settings.GRPC...)
272			return err
273		}, opts...)
274		if err != nil {
275			return nil, "", err
276		}
277		return resp.Exclusions, resp.NextPageToken, nil
278	}
279	fetch := func(pageSize int, pageToken string) (string, error) {
280		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
281		if err != nil {
282			return "", err
283		}
284		it.items = append(it.items, items...)
285		return nextPageToken, nil
286	}
287	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
288	it.pageInfo.MaxSize = int(req.PageSize)
289	it.pageInfo.Token = req.PageToken
290	return it
291}
292
293// GetExclusion gets the description of an exclusion.
294func (c *ConfigClient) GetExclusion(ctx context.Context, req *loggingpb.GetExclusionRequest, opts ...gax.CallOption) (*loggingpb.LogExclusion, error) {
295	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
296	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
297	opts = append(c.CallOptions.GetExclusion[0:len(c.CallOptions.GetExclusion):len(c.CallOptions.GetExclusion)], opts...)
298	var resp *loggingpb.LogExclusion
299	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
300		var err error
301		resp, err = c.configClient.GetExclusion(ctx, req, settings.GRPC...)
302		return err
303	}, opts...)
304	if err != nil {
305		return nil, err
306	}
307	return resp, nil
308}
309
310// CreateExclusion creates a new exclusion in a specified parent resource.
311// Only log entries belonging to that resource can be excluded.
312// You can have up to 10 exclusions in a resource.
313func (c *ConfigClient) CreateExclusion(ctx context.Context, req *loggingpb.CreateExclusionRequest, opts ...gax.CallOption) (*loggingpb.LogExclusion, error) {
314	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
315	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
316	opts = append(c.CallOptions.CreateExclusion[0:len(c.CallOptions.CreateExclusion):len(c.CallOptions.CreateExclusion)], opts...)
317	var resp *loggingpb.LogExclusion
318	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
319		var err error
320		resp, err = c.configClient.CreateExclusion(ctx, req, settings.GRPC...)
321		return err
322	}, opts...)
323	if err != nil {
324		return nil, err
325	}
326	return resp, nil
327}
328
329// UpdateExclusion changes one or more properties of an existing exclusion.
330func (c *ConfigClient) UpdateExclusion(ctx context.Context, req *loggingpb.UpdateExclusionRequest, opts ...gax.CallOption) (*loggingpb.LogExclusion, error) {
331	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
332	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
333	opts = append(c.CallOptions.UpdateExclusion[0:len(c.CallOptions.UpdateExclusion):len(c.CallOptions.UpdateExclusion)], opts...)
334	var resp *loggingpb.LogExclusion
335	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
336		var err error
337		resp, err = c.configClient.UpdateExclusion(ctx, req, settings.GRPC...)
338		return err
339	}, opts...)
340	if err != nil {
341		return nil, err
342	}
343	return resp, nil
344}
345
346// DeleteExclusion deletes an exclusion.
347func (c *ConfigClient) DeleteExclusion(ctx context.Context, req *loggingpb.DeleteExclusionRequest, opts ...gax.CallOption) error {
348	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
349	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
350	opts = append(c.CallOptions.DeleteExclusion[0:len(c.CallOptions.DeleteExclusion):len(c.CallOptions.DeleteExclusion)], opts...)
351	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
352		var err error
353		_, err = c.configClient.DeleteExclusion(ctx, req, settings.GRPC...)
354		return err
355	}, opts...)
356	return err
357}
358
359// LogExclusionIterator manages a stream of *loggingpb.LogExclusion.
360type LogExclusionIterator struct {
361	items    []*loggingpb.LogExclusion
362	pageInfo *iterator.PageInfo
363	nextFunc func() error
364
365	// InternalFetch is for use by the Google Cloud Libraries only.
366	// It is not part of the stable interface of this package.
367	//
368	// InternalFetch returns results from a single call to the underlying RPC.
369	// The number of results is no greater than pageSize.
370	// If there are no more results, nextPageToken is empty and err is nil.
371	InternalFetch func(pageSize int, pageToken string) (results []*loggingpb.LogExclusion, nextPageToken string, err error)
372}
373
374// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
375func (it *LogExclusionIterator) PageInfo() *iterator.PageInfo {
376	return it.pageInfo
377}
378
379// Next returns the next result. Its second return value is iterator.Done if there are no more
380// results. Once Next returns Done, all subsequent calls will return Done.
381func (it *LogExclusionIterator) Next() (*loggingpb.LogExclusion, error) {
382	var item *loggingpb.LogExclusion
383	if err := it.nextFunc(); err != nil {
384		return item, err
385	}
386	item = it.items[0]
387	it.items = it.items[1:]
388	return item, nil
389}
390
391func (it *LogExclusionIterator) bufLen() int {
392	return len(it.items)
393}
394
395func (it *LogExclusionIterator) takeBuf() interface{} {
396	b := it.items
397	it.items = nil
398	return b
399}
400
401// LogSinkIterator manages a stream of *loggingpb.LogSink.
402type LogSinkIterator struct {
403	items    []*loggingpb.LogSink
404	pageInfo *iterator.PageInfo
405	nextFunc func() error
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 []*loggingpb.LogSink, nextPageToken string, err error)
414}
415
416// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
417func (it *LogSinkIterator) 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 *LogSinkIterator) Next() (*loggingpb.LogSink, error) {
424	var item *loggingpb.LogSink
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 *LogSinkIterator) bufLen() int {
434	return len(it.items)
435}
436
437func (it *LogSinkIterator) takeBuf() interface{} {
438	b := it.items
439	it.items = nil
440	return b
441}
442