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.
 
 
 

279 lines
10 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. monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
  26. "google.golang.org/grpc"
  27. "google.golang.org/grpc/codes"
  28. "google.golang.org/grpc/metadata"
  29. )
  30. // AlertPolicyCallOptions contains the retry settings for each method of AlertPolicyClient.
  31. type AlertPolicyCallOptions struct {
  32. ListAlertPolicies []gax.CallOption
  33. GetAlertPolicy []gax.CallOption
  34. CreateAlertPolicy []gax.CallOption
  35. DeleteAlertPolicy []gax.CallOption
  36. UpdateAlertPolicy []gax.CallOption
  37. }
  38. func defaultAlertPolicyClientOptions() []option.ClientOption {
  39. return []option.ClientOption{
  40. option.WithEndpoint("monitoring.googleapis.com:443"),
  41. option.WithScopes(DefaultAuthScopes()...),
  42. }
  43. }
  44. func defaultAlertPolicyCallOptions() *AlertPolicyCallOptions {
  45. retry := map[[2]string][]gax.CallOption{
  46. {"default", "idempotent"}: {
  47. gax.WithRetry(func() gax.Retryer {
  48. return gax.OnCodes([]codes.Code{
  49. codes.DeadlineExceeded,
  50. codes.Unavailable,
  51. }, gax.Backoff{
  52. Initial: 100 * time.Millisecond,
  53. Max: 60000 * time.Millisecond,
  54. Multiplier: 1.3,
  55. })
  56. }),
  57. },
  58. }
  59. return &AlertPolicyCallOptions{
  60. ListAlertPolicies: retry[[2]string{"default", "idempotent"}],
  61. GetAlertPolicy: retry[[2]string{"default", "idempotent"}],
  62. CreateAlertPolicy: retry[[2]string{"default", "non_idempotent"}],
  63. DeleteAlertPolicy: retry[[2]string{"default", "idempotent"}],
  64. UpdateAlertPolicy: retry[[2]string{"default", "non_idempotent"}],
  65. }
  66. }
  67. // AlertPolicyClient is a client for interacting with Stackdriver Monitoring API.
  68. //
  69. // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
  70. type AlertPolicyClient struct {
  71. // The connection to the service.
  72. conn *grpc.ClientConn
  73. // The gRPC API client.
  74. alertPolicyClient monitoringpb.AlertPolicyServiceClient
  75. // The call options for this service.
  76. CallOptions *AlertPolicyCallOptions
  77. // The x-goog-* metadata to be sent with each request.
  78. xGoogMetadata metadata.MD
  79. }
  80. // NewAlertPolicyClient creates a new alert policy service client.
  81. //
  82. // The AlertPolicyService API is used to manage (list, create, delete,
  83. // edit) alert policies in Stackdriver Monitoring. An alerting policy is
  84. // a description of the conditions under which some aspect of your
  85. // system is considered to be "unhealthy" and the ways to notify
  86. // people or services about this state. In addition to using this API, alert
  87. // policies can also be managed through
  88. // Stackdriver Monitoring (at https://cloud.google.com/monitoring/docs/),
  89. // which can be reached by clicking the "Monitoring" tab in
  90. // Cloud Console (at https://console.cloud.google.com/).
  91. func NewAlertPolicyClient(ctx context.Context, opts ...option.ClientOption) (*AlertPolicyClient, error) {
  92. conn, err := transport.DialGRPC(ctx, append(defaultAlertPolicyClientOptions(), opts...)...)
  93. if err != nil {
  94. return nil, err
  95. }
  96. c := &AlertPolicyClient{
  97. conn: conn,
  98. CallOptions: defaultAlertPolicyCallOptions(),
  99. alertPolicyClient: monitoringpb.NewAlertPolicyServiceClient(conn),
  100. }
  101. c.setGoogleClientInfo()
  102. return c, nil
  103. }
  104. // Connection returns the client's connection to the API service.
  105. func (c *AlertPolicyClient) Connection() *grpc.ClientConn {
  106. return c.conn
  107. }
  108. // Close closes the connection to the API service. The user should invoke this when
  109. // the client is no longer required.
  110. func (c *AlertPolicyClient) Close() error {
  111. return c.conn.Close()
  112. }
  113. // setGoogleClientInfo sets the name and version of the application in
  114. // the `x-goog-api-client` header passed on each request. Intended for
  115. // use by Google-written clients.
  116. func (c *AlertPolicyClient) setGoogleClientInfo(keyval ...string) {
  117. kv := append([]string{"gl-go", versionGo()}, keyval...)
  118. kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
  119. c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
  120. }
  121. // ListAlertPolicies lists the existing alerting policies for the project.
  122. func (c *AlertPolicyClient) ListAlertPolicies(ctx context.Context, req *monitoringpb.ListAlertPoliciesRequest, opts ...gax.CallOption) *AlertPolicyIterator {
  123. ctx = insertMetadata(ctx, c.xGoogMetadata)
  124. opts = append(c.CallOptions.ListAlertPolicies[0:len(c.CallOptions.ListAlertPolicies):len(c.CallOptions.ListAlertPolicies)], opts...)
  125. it := &AlertPolicyIterator{}
  126. req = proto.Clone(req).(*monitoringpb.ListAlertPoliciesRequest)
  127. it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.AlertPolicy, string, error) {
  128. var resp *monitoringpb.ListAlertPoliciesResponse
  129. req.PageToken = pageToken
  130. if pageSize > math.MaxInt32 {
  131. req.PageSize = math.MaxInt32
  132. } else {
  133. req.PageSize = int32(pageSize)
  134. }
  135. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  136. var err error
  137. resp, err = c.alertPolicyClient.ListAlertPolicies(ctx, req, settings.GRPC...)
  138. return err
  139. }, opts...)
  140. if err != nil {
  141. return nil, "", err
  142. }
  143. return resp.AlertPolicies, resp.NextPageToken, nil
  144. }
  145. fetch := func(pageSize int, pageToken string) (string, error) {
  146. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  147. if err != nil {
  148. return "", err
  149. }
  150. it.items = append(it.items, items...)
  151. return nextPageToken, nil
  152. }
  153. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  154. it.pageInfo.MaxSize = int(req.PageSize)
  155. return it
  156. }
  157. // GetAlertPolicy gets a single alerting policy.
  158. func (c *AlertPolicyClient) GetAlertPolicy(ctx context.Context, req *monitoringpb.GetAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
  159. ctx = insertMetadata(ctx, c.xGoogMetadata)
  160. opts = append(c.CallOptions.GetAlertPolicy[0:len(c.CallOptions.GetAlertPolicy):len(c.CallOptions.GetAlertPolicy)], opts...)
  161. var resp *monitoringpb.AlertPolicy
  162. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  163. var err error
  164. resp, err = c.alertPolicyClient.GetAlertPolicy(ctx, req, settings.GRPC...)
  165. return err
  166. }, opts...)
  167. if err != nil {
  168. return nil, err
  169. }
  170. return resp, nil
  171. }
  172. // CreateAlertPolicy creates a new alerting policy.
  173. func (c *AlertPolicyClient) CreateAlertPolicy(ctx context.Context, req *monitoringpb.CreateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
  174. ctx = insertMetadata(ctx, c.xGoogMetadata)
  175. opts = append(c.CallOptions.CreateAlertPolicy[0:len(c.CallOptions.CreateAlertPolicy):len(c.CallOptions.CreateAlertPolicy)], opts...)
  176. var resp *monitoringpb.AlertPolicy
  177. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  178. var err error
  179. resp, err = c.alertPolicyClient.CreateAlertPolicy(ctx, req, settings.GRPC...)
  180. return err
  181. }, opts...)
  182. if err != nil {
  183. return nil, err
  184. }
  185. return resp, nil
  186. }
  187. // DeleteAlertPolicy deletes an alerting policy.
  188. func (c *AlertPolicyClient) DeleteAlertPolicy(ctx context.Context, req *monitoringpb.DeleteAlertPolicyRequest, opts ...gax.CallOption) error {
  189. ctx = insertMetadata(ctx, c.xGoogMetadata)
  190. opts = append(c.CallOptions.DeleteAlertPolicy[0:len(c.CallOptions.DeleteAlertPolicy):len(c.CallOptions.DeleteAlertPolicy)], opts...)
  191. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  192. var err error
  193. _, err = c.alertPolicyClient.DeleteAlertPolicy(ctx, req, settings.GRPC...)
  194. return err
  195. }, opts...)
  196. return err
  197. }
  198. // UpdateAlertPolicy updates an alerting policy. You can either replace the entire policy with
  199. // a new one or replace only certain fields in the current alerting policy by
  200. // specifying the fields to be updated via updateMask. Returns the
  201. // updated alerting policy.
  202. func (c *AlertPolicyClient) UpdateAlertPolicy(ctx context.Context, req *monitoringpb.UpdateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
  203. ctx = insertMetadata(ctx, c.xGoogMetadata)
  204. opts = append(c.CallOptions.UpdateAlertPolicy[0:len(c.CallOptions.UpdateAlertPolicy):len(c.CallOptions.UpdateAlertPolicy)], opts...)
  205. var resp *monitoringpb.AlertPolicy
  206. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  207. var err error
  208. resp, err = c.alertPolicyClient.UpdateAlertPolicy(ctx, req, settings.GRPC...)
  209. return err
  210. }, opts...)
  211. if err != nil {
  212. return nil, err
  213. }
  214. return resp, nil
  215. }
  216. // AlertPolicyIterator manages a stream of *monitoringpb.AlertPolicy.
  217. type AlertPolicyIterator struct {
  218. items []*monitoringpb.AlertPolicy
  219. pageInfo *iterator.PageInfo
  220. nextFunc func() error
  221. // InternalFetch is for use by the Google Cloud Libraries only.
  222. // It is not part of the stable interface of this package.
  223. //
  224. // InternalFetch returns results from a single call to the underlying RPC.
  225. // The number of results is no greater than pageSize.
  226. // If there are no more results, nextPageToken is empty and err is nil.
  227. InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.AlertPolicy, nextPageToken string, err error)
  228. }
  229. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  230. func (it *AlertPolicyIterator) PageInfo() *iterator.PageInfo {
  231. return it.pageInfo
  232. }
  233. // Next returns the next result. Its second return value is iterator.Done if there are no more
  234. // results. Once Next returns Done, all subsequent calls will return Done.
  235. func (it *AlertPolicyIterator) Next() (*monitoringpb.AlertPolicy, error) {
  236. var item *monitoringpb.AlertPolicy
  237. if err := it.nextFunc(); err != nil {
  238. return item, err
  239. }
  240. item = it.items[0]
  241. it.items = it.items[1:]
  242. return item, nil
  243. }
  244. func (it *AlertPolicyIterator) bufLen() int {
  245. return len(it.items)
  246. }
  247. func (it *AlertPolicyIterator) takeBuf() interface{} {
  248. b := it.items
  249. it.items = nil
  250. return b
  251. }