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