您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

428 行
15 KiB

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