Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

277 рядки
10 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. 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", version.Go()}, keyval...)
  118. kv = append(kv, "gapic", version.Repo, "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. it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.AlertPolicy, string, error) {
  127. var resp *monitoringpb.ListAlertPoliciesResponse
  128. req.PageToken = pageToken
  129. if pageSize > math.MaxInt32 {
  130. req.PageSize = math.MaxInt32
  131. } else {
  132. req.PageSize = int32(pageSize)
  133. }
  134. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  135. var err error
  136. resp, err = c.alertPolicyClient.ListAlertPolicies(ctx, req, settings.GRPC...)
  137. return err
  138. }, opts...)
  139. if err != nil {
  140. return nil, "", err
  141. }
  142. return resp.AlertPolicies, resp.NextPageToken, nil
  143. }
  144. fetch := func(pageSize int, pageToken string) (string, error) {
  145. items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
  146. if err != nil {
  147. return "", err
  148. }
  149. it.items = append(it.items, items...)
  150. return nextPageToken, nil
  151. }
  152. it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
  153. return it
  154. }
  155. // GetAlertPolicy gets a single alerting policy.
  156. func (c *AlertPolicyClient) GetAlertPolicy(ctx context.Context, req *monitoringpb.GetAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
  157. ctx = insertMetadata(ctx, c.xGoogMetadata)
  158. opts = append(c.CallOptions.GetAlertPolicy[0:len(c.CallOptions.GetAlertPolicy):len(c.CallOptions.GetAlertPolicy)], opts...)
  159. var resp *monitoringpb.AlertPolicy
  160. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  161. var err error
  162. resp, err = c.alertPolicyClient.GetAlertPolicy(ctx, req, settings.GRPC...)
  163. return err
  164. }, opts...)
  165. if err != nil {
  166. return nil, err
  167. }
  168. return resp, nil
  169. }
  170. // CreateAlertPolicy creates a new alerting policy.
  171. func (c *AlertPolicyClient) CreateAlertPolicy(ctx context.Context, req *monitoringpb.CreateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
  172. ctx = insertMetadata(ctx, c.xGoogMetadata)
  173. opts = append(c.CallOptions.CreateAlertPolicy[0:len(c.CallOptions.CreateAlertPolicy):len(c.CallOptions.CreateAlertPolicy)], opts...)
  174. var resp *monitoringpb.AlertPolicy
  175. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  176. var err error
  177. resp, err = c.alertPolicyClient.CreateAlertPolicy(ctx, req, settings.GRPC...)
  178. return err
  179. }, opts...)
  180. if err != nil {
  181. return nil, err
  182. }
  183. return resp, nil
  184. }
  185. // DeleteAlertPolicy deletes an alerting policy.
  186. func (c *AlertPolicyClient) DeleteAlertPolicy(ctx context.Context, req *monitoringpb.DeleteAlertPolicyRequest, opts ...gax.CallOption) error {
  187. ctx = insertMetadata(ctx, c.xGoogMetadata)
  188. opts = append(c.CallOptions.DeleteAlertPolicy[0:len(c.CallOptions.DeleteAlertPolicy):len(c.CallOptions.DeleteAlertPolicy)], opts...)
  189. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  190. var err error
  191. _, err = c.alertPolicyClient.DeleteAlertPolicy(ctx, req, settings.GRPC...)
  192. return err
  193. }, opts...)
  194. return err
  195. }
  196. // UpdateAlertPolicy updates an alerting policy. You can either replace the entire policy with
  197. // a new one or replace only certain fields in the current alerting policy by
  198. // specifying the fields to be updated via updateMask. Returns the
  199. // updated alerting policy.
  200. func (c *AlertPolicyClient) UpdateAlertPolicy(ctx context.Context, req *monitoringpb.UpdateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
  201. ctx = insertMetadata(ctx, c.xGoogMetadata)
  202. opts = append(c.CallOptions.UpdateAlertPolicy[0:len(c.CallOptions.UpdateAlertPolicy):len(c.CallOptions.UpdateAlertPolicy)], opts...)
  203. var resp *monitoringpb.AlertPolicy
  204. err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  205. var err error
  206. resp, err = c.alertPolicyClient.UpdateAlertPolicy(ctx, req, settings.GRPC...)
  207. return err
  208. }, opts...)
  209. if err != nil {
  210. return nil, err
  211. }
  212. return resp, nil
  213. }
  214. // AlertPolicyIterator manages a stream of *monitoringpb.AlertPolicy.
  215. type AlertPolicyIterator struct {
  216. items []*monitoringpb.AlertPolicy
  217. pageInfo *iterator.PageInfo
  218. nextFunc func() error
  219. // InternalFetch is for use by the Google Cloud Libraries only.
  220. // It is not part of the stable interface of this package.
  221. //
  222. // InternalFetch returns results from a single call to the underlying RPC.
  223. // The number of results is no greater than pageSize.
  224. // If there are no more results, nextPageToken is empty and err is nil.
  225. InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.AlertPolicy, nextPageToken string, err error)
  226. }
  227. // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
  228. func (it *AlertPolicyIterator) PageInfo() *iterator.PageInfo {
  229. return it.pageInfo
  230. }
  231. // Next returns the next result. Its second return value is iterator.Done if there are no more
  232. // results. Once Next returns Done, all subsequent calls will return Done.
  233. func (it *AlertPolicyIterator) Next() (*monitoringpb.AlertPolicy, error) {
  234. var item *monitoringpb.AlertPolicy
  235. if err := it.nextFunc(); err != nil {
  236. return item, err
  237. }
  238. item = it.items[0]
  239. it.items = it.items[1:]
  240. return item, nil
  241. }
  242. func (it *AlertPolicyIterator) bufLen() int {
  243. return len(it.items)
  244. }
  245. func (it *AlertPolicyIterator) takeBuf() interface{} {
  246. b := it.items
  247. it.items = nil
  248. return b
  249. }