config_client.go 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. // AUTO-GENERATED CODE. DO NOT EDIT.
  15. package logging
  16. import (
  17. "fmt"
  18. "math"
  19. "runtime"
  20. "strings"
  21. "time"
  22. gax "github.com/googleapis/gax-go"
  23. "golang.org/x/net/context"
  24. "google.golang.org/api/iterator"
  25. "google.golang.org/api/option"
  26. "google.golang.org/api/transport"
  27. loggingpb "google.golang.org/genproto/googleapis/logging/v2"
  28. "google.golang.org/grpc"
  29. "google.golang.org/grpc/codes"
  30. "google.golang.org/grpc/metadata"
  31. )
  32. var (
  33. configParentPathTemplate = gax.MustCompilePathTemplate("projects/{project}")
  34. configSinkPathTemplate = gax.MustCompilePathTemplate("projects/{project}/sinks/{sink}")
  35. )
  36. // ConfigCallOptions contains the retry settings for each method of ConfigClient.
  37. type ConfigCallOptions struct {
  38. ListSinks []gax.CallOption
  39. GetSink []gax.CallOption
  40. CreateSink []gax.CallOption
  41. UpdateSink []gax.CallOption
  42. DeleteSink []gax.CallOption
  43. }
  44. func defaultConfigClientOptions() []option.ClientOption {
  45. return []option.ClientOption{
  46. option.WithEndpoint("logging.googleapis.com:443"),
  47. option.WithScopes(
  48. "https://www.googleapis.com/auth/cloud-platform",
  49. "https://www.googleapis.com/auth/cloud-platform.read-only",
  50. "https://www.googleapis.com/auth/logging.admin",
  51. "https://www.googleapis.com/auth/logging.read",
  52. "https://www.googleapis.com/auth/logging.write",
  53. ),
  54. }
  55. }
  56. func 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.Unavailable,
  63. }, gax.Backoff{
  64. Initial: 100 * time.Millisecond,
  65. Max: 1000 * time.Millisecond,
  66. Multiplier: 1.2,
  67. })
  68. }),
  69. },
  70. }
  71. return &ConfigCallOptions{
  72. ListSinks: retry[[2]string{"default", "idempotent"}],
  73. GetSink: retry[[2]string{"default", "idempotent"}],
  74. CreateSink: retry[[2]string{"default", "non_idempotent"}],
  75. UpdateSink: retry[[2]string{"default", "non_idempotent"}],
  76. DeleteSink: retry[[2]string{"default", "idempotent"}],
  77. }
  78. }
  79. // ConfigClient is a client for interacting with Stackdriver Logging API.
  80. type ConfigClient struct {
  81. // The connection to the service.
  82. conn *grpc.ClientConn
  83. // The gRPC API client.
  84. configClient loggingpb.ConfigServiceV2Client
  85. // The call options for this service.
  86. CallOptions *ConfigCallOptions
  87. // The metadata to be sent with each request.
  88. metadata metadata.MD
  89. }
  90. // NewConfigClient creates a new config service v2 client.
  91. //
  92. // Service for configuring sinks used to export log entries outside of
  93. // Stackdriver Logging.
  94. func NewConfigClient(ctx context.Context, opts ...option.ClientOption) (*ConfigClient, error) {
  95. conn, err := transport.DialGRPC(ctx, append(defaultConfigClientOptions(), opts...)...)
  96. if err != nil {
  97. return nil, err
  98. }
  99. c := &ConfigClient{
  100. conn: conn,
  101. CallOptions: defaultConfigCallOptions(),
  102. configClient: loggingpb.NewConfigServiceV2Client(conn),
  103. }
  104. c.SetGoogleClientInfo("gax", gax.Version)
  105. return c, nil
  106. }
  107. // Connection returns the client's connection to the API service.
  108. func (c *ConfigClient) Connection() *grpc.ClientConn {
  109. return c.conn
  110. }
  111. // Close closes the connection to the API service. The user should invoke this when
  112. // the client is no longer required.
  113. func (c *ConfigClient) Close() error {
  114. return c.conn.Close()
  115. }
  116. // SetGoogleClientInfo sets the name and version of the application in
  117. // the `x-goog-api-client` header passed on each request. Intended for
  118. // use by Google-written clients.
  119. func (c *ConfigClient) SetGoogleClientInfo(name, version string) {
  120. goVersion := strings.Replace(runtime.Version(), " ", "_", -1)
  121. v := fmt.Sprintf("%s/%s %s gax/%s go/%s", name, version, gapicNameVersion, gax.Version, goVersion)
  122. c.metadata = metadata.Pairs("x-goog-api-client", v)
  123. }
  124. // ConfigParentPath returns the path for the parent resource.
  125. func ConfigParentPath(project string) string {
  126. path, err := configParentPathTemplate.Render(map[string]string{
  127. "project": project,
  128. })
  129. if err != nil {
  130. panic(err)
  131. }
  132. return path
  133. }
  134. // ConfigSinkPath returns the path for the sink resource.
  135. func ConfigSinkPath(project, sink string) string {
  136. path, err := configSinkPathTemplate.Render(map[string]string{
  137. "project": project,
  138. "sink": sink,
  139. })
  140. if err != nil {
  141. panic(err)
  142. }
  143. return path
  144. }
  145. // ListSinks lists sinks.
  146. func (c *ConfigClient) ListSinks(ctx context.Context, req *loggingpb.ListSinksRequest) *LogSinkIterator {
  147. md, _ := metadata.FromContext(ctx)
  148. ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
  149. it := &LogSinkIterator{}
  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) error {
  159. var err error
  160. resp, err = c.configClient.ListSinks(ctx, req)
  161. return err
  162. }, c.CallOptions.ListSinks...)
  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. return it
  178. }
  179. // GetSink gets a sink.
  180. func (c *ConfigClient) GetSink(ctx context.Context, req *loggingpb.GetSinkRequest) (*loggingpb.LogSink, error) {
  181. md, _ := metadata.FromContext(ctx)
  182. ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
  183. var resp *loggingpb.LogSink
  184. err := gax.Invoke(ctx, func(ctx context.Context) error {
  185. var err error
  186. resp, err = c.configClient.GetSink(ctx, req)
  187. return err
  188. }, c.CallOptions.GetSink...)
  189. if err != nil {
  190. return nil, err
  191. }
  192. return resp, nil
  193. }
  194. // CreateSink creates a sink.
  195. func (c *ConfigClient) CreateSink(ctx context.Context, req *loggingpb.CreateSinkRequest) (*loggingpb.LogSink, error) {
  196. md, _ := metadata.FromContext(ctx)
  197. ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
  198. var resp *loggingpb.LogSink
  199. err := gax.Invoke(ctx, func(ctx context.Context) error {
  200. var err error
  201. resp, err = c.configClient.CreateSink(ctx, req)
  202. return err
  203. }, c.CallOptions.CreateSink...)
  204. if err != nil {
  205. return nil, err
  206. }
  207. return resp, nil
  208. }
  209. // UpdateSink updates or creates a sink.
  210. func (c *ConfigClient) UpdateSink(ctx context.Context, req *loggingpb.UpdateSinkRequest) (*loggingpb.LogSink, error) {
  211. md, _ := metadata.FromContext(ctx)
  212. ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
  213. var resp *loggingpb.LogSink
  214. err := gax.Invoke(ctx, func(ctx context.Context) error {
  215. var err error
  216. resp, err = c.configClient.UpdateSink(ctx, req)
  217. return err
  218. }, c.CallOptions.UpdateSink...)
  219. if err != nil {
  220. return nil, err
  221. }
  222. return resp, nil
  223. }
  224. // DeleteSink deletes a sink.
  225. func (c *ConfigClient) DeleteSink(ctx context.Context, req *loggingpb.DeleteSinkRequest) error {
  226. md, _ := metadata.FromContext(ctx)
  227. ctx = metadata.NewContext(ctx, metadata.Join(md, c.metadata))
  228. err := gax.Invoke(ctx, func(ctx context.Context) error {
  229. var err error
  230. _, err = c.configClient.DeleteSink(ctx, req)
  231. return err
  232. }, c.CallOptions.DeleteSink...)
  233. return err
  234. }
  235. // LogSinkIterator manages a stream of *loggingpb.LogSink.
  236. type LogSinkIterator struct {
  237. items []*loggingpb.LogSink
  238. pageInfo *iterator.PageInfo
  239. nextFunc func() error
  240. // InternalFetch is for use by the Google Cloud Libraries only.
  241. // It is not part of the stable interface of this package.
  242. //
  243. // InternalFetch returns results from a single call to the underlying RPC.
  244. // The number of results is no greater than pageSize.
  245. // If there are no more results, nextPageToken is empty and err is nil.
  246. InternalFetch func(pageSize int, pageToken string) (results []*loggingpb.LogSink, nextPageToken string, err error)
  247. }
  248. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  249. func (it *LogSinkIterator) PageInfo() *iterator.PageInfo {
  250. return it.pageInfo
  251. }
  252. // Next returns the next result. Its second return value is iterator.Done if there are no more
  253. // results. Once Next returns Done, all subsequent calls will return Done.
  254. func (it *LogSinkIterator) Next() (*loggingpb.LogSink, error) {
  255. var item *loggingpb.LogSink
  256. if err := it.nextFunc(); err != nil {
  257. return item, err
  258. }
  259. item = it.items[0]
  260. it.items = it.items[1:]
  261. return item, nil
  262. }
  263. func (it *LogSinkIterator) bufLen() int {
  264. return len(it.items)
  265. }
  266. func (it *LogSinkIterator) takeBuf() interface{} {
  267. b := it.items
  268. it.items = nil
  269. return b
  270. }