You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

362 lines
13 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 monitoring
  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. 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", versionGo()}, keyval...)
  124. kv = append(kv, "gapic", versionClient, "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. req = proto.Clone(req).(*monitoringpb.ListGroupsRequest)
  133. it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.Group, string, error) {
  134. var resp *monitoringpb.ListGroupsResponse
  135. req.PageToken = pageToken
  136. if pageSize > math.MaxInt32 {
  137. req.PageSize = math.MaxInt32
  138. } else {
  139. req.PageSize = int32(pageSize)
  140. }
  141. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  142. var err error
  143. resp, err = c.groupClient.ListGroups(ctx, req, settings.GRPC...)
  144. return err
  145. }, opts...)
  146. if err != nil {
  147. return nil, "", err
  148. }
  149. return resp.Group, resp.NextPageToken, nil
  150. }
  151. fetch := func(pageSize int, pageToken string) (string, error) {
  152. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  153. if err != nil {
  154. return "", err
  155. }
  156. it.items = append(it.items, items...)
  157. return nextPageToken, nil
  158. }
  159. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  160. it.pageInfo.MaxSize = int(req.PageSize)
  161. return it
  162. }
  163. // GetGroup gets a single group.
  164. func (c *GroupClient) GetGroup(ctx context.Context, req *monitoringpb.GetGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
  165. ctx = insertMetadata(ctx, c.xGoogMetadata)
  166. opts = append(c.CallOptions.GetGroup[0:len(c.CallOptions.GetGroup):len(c.CallOptions.GetGroup)], opts...)
  167. var resp *monitoringpb.Group
  168. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  169. var err error
  170. resp, err = c.groupClient.GetGroup(ctx, req, settings.GRPC...)
  171. return err
  172. }, opts...)
  173. if err != nil {
  174. return nil, err
  175. }
  176. return resp, nil
  177. }
  178. // CreateGroup creates a new group.
  179. func (c *GroupClient) CreateGroup(ctx context.Context, req *monitoringpb.CreateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
  180. ctx = insertMetadata(ctx, c.xGoogMetadata)
  181. opts = append(c.CallOptions.CreateGroup[0:len(c.CallOptions.CreateGroup):len(c.CallOptions.CreateGroup)], opts...)
  182. var resp *monitoringpb.Group
  183. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  184. var err error
  185. resp, err = c.groupClient.CreateGroup(ctx, req, settings.GRPC...)
  186. return err
  187. }, opts...)
  188. if err != nil {
  189. return nil, err
  190. }
  191. return resp, nil
  192. }
  193. // UpdateGroup updates an existing group.
  194. // You can change any group attributes except name.
  195. func (c *GroupClient) UpdateGroup(ctx context.Context, req *monitoringpb.UpdateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
  196. ctx = insertMetadata(ctx, c.xGoogMetadata)
  197. opts = append(c.CallOptions.UpdateGroup[0:len(c.CallOptions.UpdateGroup):len(c.CallOptions.UpdateGroup)], opts...)
  198. var resp *monitoringpb.Group
  199. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  200. var err error
  201. resp, err = c.groupClient.UpdateGroup(ctx, req, settings.GRPC...)
  202. return err
  203. }, opts...)
  204. if err != nil {
  205. return nil, err
  206. }
  207. return resp, nil
  208. }
  209. // DeleteGroup deletes an existing group.
  210. func (c *GroupClient) DeleteGroup(ctx context.Context, req *monitoringpb.DeleteGroupRequest, opts ...gax.CallOption) error {
  211. ctx = insertMetadata(ctx, c.xGoogMetadata)
  212. opts = append(c.CallOptions.DeleteGroup[0:len(c.CallOptions.DeleteGroup):len(c.CallOptions.DeleteGroup)], opts...)
  213. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  214. var err error
  215. _, err = c.groupClient.DeleteGroup(ctx, req, settings.GRPC...)
  216. return err
  217. }, opts...)
  218. return err
  219. }
  220. // ListGroupMembers lists the monitored resources that are members of a group.
  221. func (c *GroupClient) ListGroupMembers(ctx context.Context, req *monitoringpb.ListGroupMembersRequest, opts ...gax.CallOption) *MonitoredResourceIterator {
  222. ctx = insertMetadata(ctx, c.xGoogMetadata)
  223. opts = append(c.CallOptions.ListGroupMembers[0:len(c.CallOptions.ListGroupMembers):len(c.CallOptions.ListGroupMembers)], opts...)
  224. it := &MonitoredResourceIterator{}
  225. req = proto.Clone(req).(*monitoringpb.ListGroupMembersRequest)
  226. it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoredrespb.MonitoredResource, string, error) {
  227. var resp *monitoringpb.ListGroupMembersResponse
  228. req.PageToken = pageToken
  229. if pageSize > math.MaxInt32 {
  230. req.PageSize = math.MaxInt32
  231. } else {
  232. req.PageSize = int32(pageSize)
  233. }
  234. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  235. var err error
  236. resp, err = c.groupClient.ListGroupMembers(ctx, req, settings.GRPC...)
  237. return err
  238. }, opts...)
  239. if err != nil {
  240. return nil, "", err
  241. }
  242. return resp.Members, resp.NextPageToken, nil
  243. }
  244. fetch := func(pageSize int, pageToken string) (string, error) {
  245. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  246. if err != nil {
  247. return "", err
  248. }
  249. it.items = append(it.items, items...)
  250. return nextPageToken, nil
  251. }
  252. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  253. it.pageInfo.MaxSize = int(req.PageSize)
  254. return it
  255. }
  256. // GroupIterator manages a stream of *monitoringpb.Group.
  257. type GroupIterator struct {
  258. items []*monitoringpb.Group
  259. pageInfo *iterator.PageInfo
  260. nextFunc func() error
  261. // InternalFetch is for use by the Google Cloud Libraries only.
  262. // It is not part of the stable interface of this package.
  263. //
  264. // InternalFetch returns results from a single call to the underlying RPC.
  265. // The number of results is no greater than pageSize.
  266. // If there are no more results, nextPageToken is empty and err is nil.
  267. InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.Group, nextPageToken string, err error)
  268. }
  269. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  270. func (it *GroupIterator) PageInfo() *iterator.PageInfo {
  271. return it.pageInfo
  272. }
  273. // Next returns the next result. Its second return value is iterator.Done if there are no more
  274. // results. Once Next returns Done, all subsequent calls will return Done.
  275. func (it *GroupIterator) Next() (*monitoringpb.Group, error) {
  276. var item *monitoringpb.Group
  277. if err := it.nextFunc(); err != nil {
  278. return item, err
  279. }
  280. item = it.items[0]
  281. it.items = it.items[1:]
  282. return item, nil
  283. }
  284. func (it *GroupIterator) bufLen() int {
  285. return len(it.items)
  286. }
  287. func (it *GroupIterator) takeBuf() interface{} {
  288. b := it.items
  289. it.items = nil
  290. return b
  291. }
  292. // MonitoredResourceIterator manages a stream of *monitoredrespb.MonitoredResource.
  293. type MonitoredResourceIterator struct {
  294. items []*monitoredrespb.MonitoredResource
  295. pageInfo *iterator.PageInfo
  296. nextFunc func() error
  297. // InternalFetch is for use by the Google Cloud Libraries only.
  298. // It is not part of the stable interface of this package.
  299. //
  300. // InternalFetch returns results from a single call to the underlying RPC.
  301. // The number of results is no greater than pageSize.
  302. // If there are no more results, nextPageToken is empty and err is nil.
  303. InternalFetch func(pageSize int, pageToken string) (results []*monitoredrespb.MonitoredResource, nextPageToken string, err error)
  304. }
  305. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  306. func (it *MonitoredResourceIterator) PageInfo() *iterator.PageInfo {
  307. return it.pageInfo
  308. }
  309. // Next returns the next result. Its second return value is iterator.Done if there are no more
  310. // results. Once Next returns Done, all subsequent calls will return Done.
  311. func (it *MonitoredResourceIterator) Next() (*monitoredrespb.MonitoredResource, error) {
  312. var item *monitoredrespb.MonitoredResource
  313. if err := it.nextFunc(); err != nil {
  314. return item, err
  315. }
  316. item = it.items[0]
  317. it.items = it.items[1:]
  318. return item, nil
  319. }
  320. func (it *MonitoredResourceIterator) bufLen() int {
  321. return len(it.items)
  322. }
  323. func (it *MonitoredResourceIterator) takeBuf() interface{} {
  324. b := it.items
  325. it.items = nil
  326. return b
  327. }