Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / cloud.google.com / go / logging / apiv2 / logging_client.go
1 // Copyright 2016, Google Inc. All rights reserved.
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 //     http://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
17 package logging
18
19 import (
20         "fmt"
21         "math"
22         "runtime"
23         "strings"
24         "time"
25
26         gax "github.com/googleapis/gax-go"
27         "golang.org/x/net/context"
28         "google.golang.org/api/iterator"
29         "google.golang.org/api/option"
30         "google.golang.org/api/transport"
31         monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
32         loggingpb "google.golang.org/genproto/googleapis/logging/v2"
33         "google.golang.org/grpc"
34         "google.golang.org/grpc/codes"
35         "google.golang.org/grpc/metadata"
36 )
37
38 var (
39         loggingParentPathTemplate = gax.MustCompilePathTemplate("projects/{project}")
40         loggingLogPathTemplate    = gax.MustCompilePathTemplate("projects/{project}/logs/{log}")
41 )
42
43 // CallOptions contains the retry settings for each method of Client.
44 type CallOptions struct {
45         DeleteLog                        []gax.CallOption
46         WriteLogEntries                  []gax.CallOption
47         ListLogEntries                   []gax.CallOption
48         ListMonitoredResourceDescriptors []gax.CallOption
49 }
50
51 func defaultClientOptions() []option.ClientOption {
52         return []option.ClientOption{
53                 option.WithEndpoint("logging.googleapis.com:443"),
54                 option.WithScopes(
55                         "https://www.googleapis.com/auth/cloud-platform",
56                         "https://www.googleapis.com/auth/cloud-platform.read-only",
57                         "https://www.googleapis.com/auth/logging.admin",
58                         "https://www.googleapis.com/auth/logging.read",
59                         "https://www.googleapis.com/auth/logging.write",
60                 ),
61         }
62 }
63
64 func defaultCallOptions() *CallOptions {
65         retry := map[[2]string][]gax.CallOption{
66                 {"default", "idempotent"}: {
67                         gax.WithRetry(func() gax.Retryer {
68                                 return gax.OnCodes([]codes.Code{
69                                         codes.DeadlineExceeded,
70                                         codes.Unavailable,
71                                 }, gax.Backoff{
72                                         Initial:    100 * time.Millisecond,
73                                         Max:        1000 * time.Millisecond,
74                                         Multiplier: 1.2,
75                                 })
76                         }),
77                 },
78                 {"list", "idempotent"}: {
79                         gax.WithRetry(func() gax.Retryer {
80                                 return gax.OnCodes([]codes.Code{
81                                         codes.DeadlineExceeded,
82                                         codes.Unavailable,
83                                 }, gax.Backoff{
84                                         Initial:    100 * time.Millisecond,
85                                         Max:        1000 * time.Millisecond,
86                                         Multiplier: 1.2,
87                                 })
88                         }),
89                 },
90         }
91         return &CallOptions{
92                 DeleteLog:                        retry[[2]string{"default", "idempotent"}],
93                 WriteLogEntries:                  retry[[2]string{"default", "non_idempotent"}],
94                 ListLogEntries:                   retry[[2]string{"list", "idempotent"}],
95                 ListMonitoredResourceDescriptors: retry[[2]string{"default", "idempotent"}],
96         }
97 }
98
99 // Client is a client for interacting with Stackdriver Logging API.
100 type Client struct {
101         // The connection to the service.
102         conn *grpc.ClientConn
103
104         // The gRPC API client.
105         client loggingpb.LoggingServiceV2Client
106
107         // The call options for this service.
108         CallOptions *CallOptions
109
110         // The metadata to be sent with each request.
111         metadata metadata.MD
112 }
113
114 // NewClient creates a new logging service v2 client.
115 //
116 // Service for ingesting and querying logs.
117 func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
118         conn, err := transport.DialGRPC(ctx, append(defaultClientOptions(), opts...)...)
119         if err != nil {
120                 return nil, err
121         }
122         c := &Client{
123                 conn:        conn,
124                 CallOptions: defaultCallOptions(),
125
126                 client: loggingpb.NewLoggingServiceV2Client(conn),
127         }
128         c.SetGoogleClientInfo("gax", gax.Version)
129         return c, nil
130 }
131
132 // Connection returns the client's connection to the API service.
133 func (c *Client) Connection() *grpc.ClientConn {
134         return c.conn
135 }
136
137 // Close closes the connection to the API service. The user should invoke this when
138 // the client is no longer required.
139 func (c *Client) Close() error {
140         return c.conn.Close()
141 }
142
143 // SetGoogleClientInfo sets the name and version of the application in
144 // the `x-goog-api-client` header passed on each request. Intended for
145 // use by Google-written clients.
146 func (c *Client) SetGoogleClientInfo(name, version string) {
147         goVersion := strings.Replace(runtime.Version(), " ", "_", -1)
148         v := fmt.Sprintf("%s/%s %s gax/%s go/%s", name, version, gapicNameVersion, gax.Version, goVersion)
149         c.metadata = metadata.Pairs("x-goog-api-client", v)
150 }
151
152 // LoggingParentPath returns the path for the parent resource.
153 func LoggingParentPath(project string) string {
154         path, err := loggingParentPathTemplate.Render(map[string]string{
155                 "project": project,
156         })
157         if err != nil {
158                 panic(err)
159         }
160         return path
161 }
162
163 // LoggingLogPath returns the path for the log resource.
164 func LoggingLogPath(project, log string) string {
165         path, err := loggingLogPathTemplate.Render(map[string]string{
166                 "project": project,
167                 "log":     log,
168         })
169         if err != nil {
170                 panic(err)
171         }
172         return path
173 }
174
175 // DeleteLog deletes all the log entries in a log.
176 // The log reappears if it receives new entries.
177 func (c *Client) DeleteLog(ctx context.Context, req *loggingpb.DeleteLogRequest) error {
178         md, _ := metadata.FromContext(ctx)
179         ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
180         err := gax.Invoke(ctx, func(ctx context.Context) error {
181                 var err error
182                 _, err = c.client.DeleteLog(ctx, req)
183                 return err
184         }, c.CallOptions.DeleteLog...)
185         return err
186 }
187
188 // WriteLogEntries writes log entries to Stackdriver Logging.  All log entries are
189 // written by this method.
190 func (c *Client) WriteLogEntries(ctx context.Context, req *loggingpb.WriteLogEntriesRequest) (*loggingpb.WriteLogEntriesResponse, error) {
191         md, _ := metadata.FromContext(ctx)
192         ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
193         var resp *loggingpb.WriteLogEntriesResponse
194         err := gax.Invoke(ctx, func(ctx context.Context) error {
195                 var err error
196                 resp, err = c.client.WriteLogEntries(ctx, req)
197                 return err
198         }, c.CallOptions.WriteLogEntries...)
199         if err != nil {
200                 return nil, err
201         }
202         return resp, nil
203 }
204
205 // ListLogEntries lists log entries.  Use this method to retrieve log entries from Cloud
206 // Logging.  For ways to export log entries, see
207 // [Exporting Logs](/logging/docs/export).
208 func (c *Client) ListLogEntries(ctx context.Context, req *loggingpb.ListLogEntriesRequest) *LogEntryIterator {
209         md, _ := metadata.FromContext(ctx)
210         ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
211         it := &LogEntryIterator{}
212         it.InternalFetch = func(pageSize int, pageToken string) ([]*loggingpb.LogEntry, string, error) {
213                 var resp *loggingpb.ListLogEntriesResponse
214                 req.PageToken = pageToken
215                 if pageSize > math.MaxInt32 {
216                         req.PageSize = math.MaxInt32
217                 } else {
218                         req.PageSize = int32(pageSize)
219                 }
220                 err := gax.Invoke(ctx, func(ctx context.Context) error {
221                         var err error
222                         resp, err = c.client.ListLogEntries(ctx, req)
223                         return err
224                 }, c.CallOptions.ListLogEntries...)
225                 if err != nil {
226                         return nil, "", err
227                 }
228                 return resp.Entries, resp.NextPageToken, nil
229         }
230         fetch := func(pageSize int, pageToken string) (string, error) {
231                 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
232                 if err != nil {
233                         return "", err
234                 }
235                 it.items = append(it.items, items...)
236                 return nextPageToken, nil
237         }
238         it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
239         return it
240 }
241
242 // ListMonitoredResourceDescriptors lists the monitored resource descriptors used by Stackdriver Logging.
243 func (c *Client) ListMonitoredResourceDescriptors(ctx context.Context, req *loggingpb.ListMonitoredResourceDescriptorsRequest) *MonitoredResourceDescriptorIterator {
244         md, _ := metadata.FromContext(ctx)
245         ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
246         it := &MonitoredResourceDescriptorIterator{}
247         it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoredrespb.MonitoredResourceDescriptor, string, error) {
248                 var resp *loggingpb.ListMonitoredResourceDescriptorsResponse
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) error {
256                         var err error
257                         resp, err = c.client.ListMonitoredResourceDescriptors(ctx, req)
258                         return err
259                 }, c.CallOptions.ListMonitoredResourceDescriptors...)
260                 if err != nil {
261                         return nil, "", err
262                 }
263                 return resp.ResourceDescriptors, 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 // LogEntryIterator manages a stream of *loggingpb.LogEntry.
278 type LogEntryIterator struct {
279         items    []*loggingpb.LogEntry
280         pageInfo *iterator.PageInfo
281         nextFunc func() error
282
283         // InternalFetch is for use by the Google Cloud Libraries only.
284         // It is not part of the stable interface of this package.
285         //
286         // InternalFetch returns results from a single call to the underlying RPC.
287         // The number of results is no greater than pageSize.
288         // If there are no more results, nextPageToken is empty and err is nil.
289         InternalFetch func(pageSize int, pageToken string) (results []*loggingpb.LogEntry, nextPageToken string, err error)
290 }
291
292 // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
293 func (it *LogEntryIterator) PageInfo() *iterator.PageInfo {
294         return it.pageInfo
295 }
296
297 // Next returns the next result. Its second return value is iterator.Done if there are no more
298 // results. Once Next returns Done, all subsequent calls will return Done.
299 func (it *LogEntryIterator) Next() (*loggingpb.LogEntry, error) {
300         var item *loggingpb.LogEntry
301         if err := it.nextFunc(); err != nil {
302                 return item, err
303         }
304         item = it.items[0]
305         it.items = it.items[1:]
306         return item, nil
307 }
308
309 func (it *LogEntryIterator) bufLen() int {
310         return len(it.items)
311 }
312
313 func (it *LogEntryIterator) takeBuf() interface{} {
314         b := it.items
315         it.items = nil
316         return b
317 }
318
319 // MonitoredResourceDescriptorIterator manages a stream of *monitoredrespb.MonitoredResourceDescriptor.
320 type MonitoredResourceDescriptorIterator struct {
321         items    []*monitoredrespb.MonitoredResourceDescriptor
322         pageInfo *iterator.PageInfo
323         nextFunc func() error
324
325         // InternalFetch is for use by the Google Cloud Libraries only.
326         // It is not part of the stable interface of this package.
327         //
328         // InternalFetch returns results from a single call to the underlying RPC.
329         // The number of results is no greater than pageSize.
330         // If there are no more results, nextPageToken is empty and err is nil.
331         InternalFetch func(pageSize int, pageToken string) (results []*monitoredrespb.MonitoredResourceDescriptor, nextPageToken string, err error)
332 }
333
334 // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
335 func (it *MonitoredResourceDescriptorIterator) PageInfo() *iterator.PageInfo {
336         return it.pageInfo
337 }
338
339 // Next returns the next result. Its second return value is iterator.Done if there are no more
340 // results. Once Next returns Done, all subsequent calls will return Done.
341 func (it *MonitoredResourceDescriptorIterator) Next() (*monitoredrespb.MonitoredResourceDescriptor, error) {
342         var item *monitoredrespb.MonitoredResourceDescriptor
343         if err := it.nextFunc(); err != nil {
344                 return item, err
345         }
346         item = it.items[0]
347         it.items = it.items[1:]
348         return item, nil
349 }
350
351 func (it *MonitoredResourceDescriptorIterator) bufLen() int {
352         return len(it.items)
353 }
354
355 func (it *MonitoredResourceDescriptorIterator) takeBuf() interface{} {
356         b := it.items
357         it.items = nil
358         return b
359 }