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