Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

358 строки
12 KiB

  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. // AUTO-GENERATED CODE. DO NOT EDIT.
  15. package monitoring
  16. import (
  17. "math"
  18. "time"
  19. "cloud.google.com/go/internal/version"
  20. gax "github.com/googleapis/gax-go"
  21. "golang.org/x/net/context"
  22. "google.golang.org/api/iterator"
  23. "google.golang.org/api/option"
  24. "google.golang.org/api/transport"
  25. monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
  26. monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
  27. "google.golang.org/grpc"
  28. "google.golang.org/grpc/codes"
  29. "google.golang.org/grpc/metadata"
  30. )
  31. // GroupCallOptions contains the retry settings for each method of GroupClient.
  32. type GroupCallOptions struct {
  33. ListGroups []gax.CallOption
  34. GetGroup []gax.CallOption
  35. CreateGroup []gax.CallOption
  36. UpdateGroup []gax.CallOption
  37. DeleteGroup []gax.CallOption
  38. ListGroupMembers []gax.CallOption
  39. }
  40. func defaultGroupClientOptions() []option.ClientOption {
  41. return []option.ClientOption{
  42. option.WithEndpoint("monitoring.googleapis.com:443"),
  43. option.WithScopes(DefaultAuthScopes()...),
  44. }
  45. }
  46. func defaultGroupCallOptions() *GroupCallOptions {
  47. retry := map[[2]string][]gax.CallOption{
  48. {"default", "idempotent"}: {
  49. gax.WithRetry(func() gax.Retryer {
  50. return gax.OnCodes([]codes.Code{
  51. codes.DeadlineExceeded,
  52. codes.Unavailable,
  53. }, gax.Backoff{
  54. Initial: 100 * time.Millisecond,
  55. Max: 60000 * time.Millisecond,
  56. Multiplier: 1.3,
  57. })
  58. }),
  59. },
  60. }
  61. return &GroupCallOptions{
  62. ListGroups: retry[[2]string{"default", "idempotent"}],
  63. GetGroup: retry[[2]string{"default", "idempotent"}],
  64. CreateGroup: retry[[2]string{"default", "non_idempotent"}],
  65. UpdateGroup: retry[[2]string{"default", "idempotent"}],
  66. DeleteGroup: retry[[2]string{"default", "idempotent"}],
  67. ListGroupMembers: retry[[2]string{"default", "idempotent"}],
  68. }
  69. }
  70. // GroupClient is a client for interacting with Stackdriver Monitoring API.
  71. //
  72. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  73. type GroupClient struct {
  74. // The connection to the service.
  75. conn *grpc.ClientConn
  76. // The gRPC API client.
  77. groupClient monitoringpb.GroupServiceClient
  78. // The call options for this service.
  79. CallOptions *GroupCallOptions
  80. // The x-goog-* metadata to be sent with each request.
  81. xGoogMetadata metadata.MD
  82. }
  83. // NewGroupClient creates a new group service client.
  84. //
  85. // The Group API lets you inspect and manage your
  86. // groups (at google.monitoring.v3.Group).
  87. //
  88. // A group is a named filter that is used to identify
  89. // a collection of monitored resources. Groups are typically used to
  90. // mirror the physical and/or logical topology of the environment.
  91. // Because group membership is computed dynamically, monitored
  92. // resources that are started in the future are automatically placed
  93. // in matching groups. By using a group to name monitored resources in,
  94. // for example, an alert policy, the target of that alert policy is
  95. // updated automatically as monitored resources are added and removed
  96. // from the infrastructure.
  97. func NewGroupClient(ctx context.Context, opts ...option.ClientOption) (*GroupClient, error) {
  98. conn, err := transport.DialGRPC(ctx, append(defaultGroupClientOptions(), opts...)...)
  99. if err != nil {
  100. return nil, err
  101. }
  102. c := &GroupClient{
  103. conn: conn,
  104. CallOptions: defaultGroupCallOptions(),
  105. groupClient: monitoringpb.NewGroupServiceClient(conn),
  106. }
  107. c.setGoogleClientInfo()
  108. return c, nil
  109. }
  110. // Connection returns the client's connection to the API service.
  111. func (c *GroupClient) Connection() *grpc.ClientConn {
  112. return c.conn
  113. }
  114. // Close closes the connection to the API service. The user should invoke this when
  115. // the client is no longer required.
  116. func (c *GroupClient) Close() error {
  117. return c.conn.Close()
  118. }
  119. // setGoogleClientInfo sets the name and version of the application in
  120. // the `x-goog-api-client` header passed on each request. Intended for
  121. // use by Google-written clients.
  122. func (c *GroupClient) setGoogleClientInfo(keyval ...string) {
  123. kv := append([]string{"gl-go", version.Go()}, keyval...)
  124. kv = append(kv, "gapic", version.Repo, "gax", gax.Version, "grpc", grpc.Version)
  125. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  126. }
  127. // ListGroups lists the existing groups.
  128. func (c *GroupClient) ListGroups(ctx context.Context, req *monitoringpb.ListGroupsRequest, opts ...gax.CallOption) *GroupIterator {
  129. ctx = insertMetadata(ctx, c.xGoogMetadata)
  130. opts = append(c.CallOptions.ListGroups[0:len(c.CallOptions.ListGroups):len(c.CallOptions.ListGroups)], opts...)
  131. it := &GroupIterator{}
  132. it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.Group, string, error) {
  133. var resp *monitoringpb.ListGroupsResponse
  134. req.PageToken = pageToken
  135. if pageSize > math.MaxInt32 {
  136. req.PageSize = math.MaxInt32
  137. } else {
  138. req.PageSize = int32(pageSize)
  139. }
  140. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  141. var err error
  142. resp, err = c.groupClient.ListGroups(ctx, req, settings.GRPC...)
  143. return err
  144. }, opts...)
  145. if err != nil {
  146. return nil, "", err
  147. }
  148. return resp.Group, resp.NextPageToken, nil
  149. }
  150. fetch := func(pageSize int, pageToken string) (string, error) {
  151. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  152. if err != nil {
  153. return "", err
  154. }
  155. it.items = append(it.items, items...)
  156. return nextPageToken, nil
  157. }
  158. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  159. return it
  160. }
  161. // GetGroup gets a single group.
  162. func (c *GroupClient) GetGroup(ctx context.Context, req *monitoringpb.GetGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
  163. ctx = insertMetadata(ctx, c.xGoogMetadata)
  164. opts = append(c.CallOptions.GetGroup[0:len(c.CallOptions.GetGroup):len(c.CallOptions.GetGroup)], opts...)
  165. var resp *monitoringpb.Group
  166. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  167. var err error
  168. resp, err = c.groupClient.GetGroup(ctx, req, settings.GRPC...)
  169. return err
  170. }, opts...)
  171. if err != nil {
  172. return nil, err
  173. }
  174. return resp, nil
  175. }
  176. // CreateGroup creates a new group.
  177. func (c *GroupClient) CreateGroup(ctx context.Context, req *monitoringpb.CreateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
  178. ctx = insertMetadata(ctx, c.xGoogMetadata)
  179. opts = append(c.CallOptions.CreateGroup[0:len(c.CallOptions.CreateGroup):len(c.CallOptions.CreateGroup)], opts...)
  180. var resp *monitoringpb.Group
  181. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  182. var err error
  183. resp, err = c.groupClient.CreateGroup(ctx, req, settings.GRPC...)
  184. return err
  185. }, opts...)
  186. if err != nil {
  187. return nil, err
  188. }
  189. return resp, nil
  190. }
  191. // UpdateGroup updates an existing group.
  192. // You can change any group attributes except name.
  193. func (c *GroupClient) UpdateGroup(ctx context.Context, req *monitoringpb.UpdateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
  194. ctx = insertMetadata(ctx, c.xGoogMetadata)
  195. opts = append(c.CallOptions.UpdateGroup[0:len(c.CallOptions.UpdateGroup):len(c.CallOptions.UpdateGroup)], opts...)
  196. var resp *monitoringpb.Group
  197. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  198. var err error
  199. resp, err = c.groupClient.UpdateGroup(ctx, req, settings.GRPC...)
  200. return err
  201. }, opts...)
  202. if err != nil {
  203. return nil, err
  204. }
  205. return resp, nil
  206. }
  207. // DeleteGroup deletes an existing group.
  208. func (c *GroupClient) DeleteGroup(ctx context.Context, req *monitoringpb.DeleteGroupRequest, opts ...gax.CallOption) error {
  209. ctx = insertMetadata(ctx, c.xGoogMetadata)
  210. opts = append(c.CallOptions.DeleteGroup[0:len(c.CallOptions.DeleteGroup):len(c.CallOptions.DeleteGroup)], opts...)
  211. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  212. var err error
  213. _, err = c.groupClient.DeleteGroup(ctx, req, settings.GRPC...)
  214. return err
  215. }, opts...)
  216. return err
  217. }
  218. // ListGroupMembers lists the monitored resources that are members of a group.
  219. func (c *GroupClient) ListGroupMembers(ctx context.Context, req *monitoringpb.ListGroupMembersRequest, opts ...gax.CallOption) *MonitoredResourceIterator {
  220. ctx = insertMetadata(ctx, c.xGoogMetadata)
  221. opts = append(c.CallOptions.ListGroupMembers[0:len(c.CallOptions.ListGroupMembers):len(c.CallOptions.ListGroupMembers)], opts...)
  222. it := &MonitoredResourceIterator{}
  223. it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoredrespb.MonitoredResource, string, error) {
  224. var resp *monitoringpb.ListGroupMembersResponse
  225. req.PageToken = pageToken
  226. if pageSize > math.MaxInt32 {
  227. req.PageSize = math.MaxInt32
  228. } else {
  229. req.PageSize = int32(pageSize)
  230. }
  231. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  232. var err error
  233. resp, err = c.groupClient.ListGroupMembers(ctx, req, settings.GRPC...)
  234. return err
  235. }, opts...)
  236. if err != nil {
  237. return nil, "", err
  238. }
  239. return resp.Members, resp.NextPageToken, nil
  240. }
  241. fetch := func(pageSize int, pageToken string) (string, error) {
  242. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  243. if err != nil {
  244. return "", err
  245. }
  246. it.items = append(it.items, items...)
  247. return nextPageToken, nil
  248. }
  249. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  250. return it
  251. }
  252. // GroupIterator manages a stream of *monitoringpb.Group.
  253. type GroupIterator struct {
  254. items []*monitoringpb.Group
  255. pageInfo *iterator.PageInfo
  256. nextFunc func() error
  257. // InternalFetch is for use by the Google Cloud Libraries only.
  258. // It is not part of the stable interface of this package.
  259. //
  260. // InternalFetch returns results from a single call to the underlying RPC.
  261. // The number of results is no greater than pageSize.
  262. // If there are no more results, nextPageToken is empty and err is nil.
  263. InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.Group, nextPageToken string, err error)
  264. }
  265. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  266. func (it *GroupIterator) PageInfo() *iterator.PageInfo {
  267. return it.pageInfo
  268. }
  269. // Next returns the next result. Its second return value is iterator.Done if there are no more
  270. // results. Once Next returns Done, all subsequent calls will return Done.
  271. func (it *GroupIterator) Next() (*monitoringpb.Group, error) {
  272. var item *monitoringpb.Group
  273. if err := it.nextFunc(); err != nil {
  274. return item, err
  275. }
  276. item = it.items[0]
  277. it.items = it.items[1:]
  278. return item, nil
  279. }
  280. func (it *GroupIterator) bufLen() int {
  281. return len(it.items)
  282. }
  283. func (it *GroupIterator) takeBuf() interface{} {
  284. b := it.items
  285. it.items = nil
  286. return b
  287. }
  288. // MonitoredResourceIterator manages a stream of *monitoredrespb.MonitoredResource.
  289. type MonitoredResourceIterator struct {
  290. items []*monitoredrespb.MonitoredResource
  291. pageInfo *iterator.PageInfo
  292. nextFunc func() error
  293. // InternalFetch is for use by the Google Cloud Libraries only.
  294. // It is not part of the stable interface of this package.
  295. //
  296. // InternalFetch returns results from a single call to the underlying RPC.
  297. // The number of results is no greater than pageSize.
  298. // If there are no more results, nextPageToken is empty and err is nil.
  299. InternalFetch func(pageSize int, pageToken string) (results []*monitoredrespb.MonitoredResource, nextPageToken string, err error)
  300. }
  301. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  302. func (it *MonitoredResourceIterator) PageInfo() *iterator.PageInfo {
  303. return it.pageInfo
  304. }
  305. // Next returns the next result. Its second return value is iterator.Done if there are no more
  306. // results. Once Next returns Done, all subsequent calls will return Done.
  307. func (it *MonitoredResourceIterator) Next() (*monitoredrespb.MonitoredResource, error) {
  308. var item *monitoredrespb.MonitoredResource
  309. if err := it.nextFunc(); err != nil {
  310. return item, err
  311. }
  312. item = it.items[0]
  313. it.items = it.items[1:]
  314. return item, nil
  315. }
  316. func (it *MonitoredResourceIterator) bufLen() int {
  317. return len(it.items)
  318. }
  319. func (it *MonitoredResourceIterator) takeBuf() interface{} {
  320. b := it.items
  321. it.items = nil
  322. return b
  323. }