...

Source file src/cloud.google.com/go/monitoring/apiv3/alert_policy_client.go

Documentation: cloud.google.com/go/monitoring/apiv3

     1  // Copyright 2020 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  
    15  // Code generated by protoc-gen-go_gapic. DO NOT EDIT.
    16  
    17  package monitoring
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"math"
    23  	"net/url"
    24  	"time"
    25  
    26  	"cloud.google.com/go/monitoring/apiv3/v2/monitoringpb"
    27  	gax "github.com/googleapis/gax-go/v2"
    28  	"google.golang.org/api/iterator"
    29  	"google.golang.org/api/option"
    30  	gtransport "google.golang.org/api/transport/grpc"
    31  	"google.golang.org/grpc"
    32  	"google.golang.org/grpc/codes"
    33  	"google.golang.org/grpc/metadata"
    34  	"google.golang.org/protobuf/proto"
    35  )
    36  
    37  var newAlertPolicyClientHook clientHook
    38  
    39  // AlertPolicyCallOptions contains the retry settings for each method of AlertPolicyClient.
    40  type AlertPolicyCallOptions struct {
    41  	ListAlertPolicies []gax.CallOption
    42  	GetAlertPolicy    []gax.CallOption
    43  	CreateAlertPolicy []gax.CallOption
    44  	DeleteAlertPolicy []gax.CallOption
    45  	UpdateAlertPolicy []gax.CallOption
    46  }
    47  
    48  func defaultAlertPolicyClientOptions() []option.ClientOption {
    49  	return []option.ClientOption{
    50  		option.WithEndpoint("monitoring.googleapis.com:443"),
    51  		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
    52  		option.WithScopes(DefaultAuthScopes()...),
    53  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    54  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    55  	}
    56  }
    57  
    58  func defaultAlertPolicyCallOptions() *AlertPolicyCallOptions {
    59  	return &AlertPolicyCallOptions{
    60  		ListAlertPolicies: []gax.CallOption{
    61  			gax.WithRetry(func() gax.Retryer {
    62  				return gax.OnCodes([]codes.Code{
    63  					codes.DeadlineExceeded,
    64  					codes.Unavailable,
    65  				}, gax.Backoff{
    66  					Initial:    100 * time.Millisecond,
    67  					Max:        30000 * time.Millisecond,
    68  					Multiplier: 1.30,
    69  				})
    70  			}),
    71  		},
    72  		GetAlertPolicy: []gax.CallOption{
    73  			gax.WithRetry(func() gax.Retryer {
    74  				return gax.OnCodes([]codes.Code{
    75  					codes.DeadlineExceeded,
    76  					codes.Unavailable,
    77  				}, gax.Backoff{
    78  					Initial:    100 * time.Millisecond,
    79  					Max:        30000 * time.Millisecond,
    80  					Multiplier: 1.30,
    81  				})
    82  			}),
    83  		},
    84  		CreateAlertPolicy: []gax.CallOption{},
    85  		DeleteAlertPolicy: []gax.CallOption{
    86  			gax.WithRetry(func() gax.Retryer {
    87  				return gax.OnCodes([]codes.Code{
    88  					codes.DeadlineExceeded,
    89  					codes.Unavailable,
    90  				}, gax.Backoff{
    91  					Initial:    100 * time.Millisecond,
    92  					Max:        30000 * time.Millisecond,
    93  					Multiplier: 1.30,
    94  				})
    95  			}),
    96  		},
    97  		UpdateAlertPolicy: []gax.CallOption{},
    98  	}
    99  }
   100  
   101  // AlertPolicyClient is a client for interacting with Cloud Monitoring API.
   102  //
   103  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   104  type AlertPolicyClient struct {
   105  	// Connection pool of gRPC connections to the service.
   106  	connPool gtransport.ConnPool
   107  
   108  	// The gRPC API client.
   109  	alertPolicyClient monitoringpb.AlertPolicyServiceClient
   110  
   111  	// The call options for this service.
   112  	CallOptions *AlertPolicyCallOptions
   113  
   114  	// The x-goog-* metadata to be sent with each request.
   115  	xGoogMetadata metadata.MD
   116  }
   117  
   118  // NewAlertPolicyClient creates a new alert policy service client.
   119  //
   120  // The AlertPolicyService API is used to manage (list, create, delete,
   121  // edit) alert policies in Stackdriver Monitoring. An alerting policy is
   122  // a description of the conditions under which some aspect of your
   123  // system is considered to be “unhealthy” and the ways to notify
   124  // people or services about this state. In addition to using this API, alert
   125  // policies can also be managed through
   126  // Stackdriver Monitoring (at https://cloud.google.com/monitoring/docs/),
   127  // which can be reached by clicking the “Monitoring” tab in
   128  // Cloud Console (at https://console.cloud.google.com/).
   129  func NewAlertPolicyClient(ctx context.Context, opts ...option.ClientOption) (*AlertPolicyClient, error) {
   130  	clientOpts := defaultAlertPolicyClientOptions()
   131  
   132  	if newAlertPolicyClientHook != nil {
   133  		hookOpts, err := newAlertPolicyClientHook(ctx, clientHookParams{})
   134  		if err != nil {
   135  			return nil, err
   136  		}
   137  		clientOpts = append(clientOpts, hookOpts...)
   138  	}
   139  
   140  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   141  	if err != nil {
   142  		return nil, err
   143  	}
   144  	c := &AlertPolicyClient{
   145  		connPool:    connPool,
   146  		CallOptions: defaultAlertPolicyCallOptions(),
   147  
   148  		alertPolicyClient: monitoringpb.NewAlertPolicyServiceClient(connPool),
   149  	}
   150  	c.setGoogleClientInfo()
   151  
   152  	return c, nil
   153  }
   154  
   155  // Connection returns a connection to the API service.
   156  //
   157  // Deprecated.
   158  func (c *AlertPolicyClient) Connection() *grpc.ClientConn {
   159  	return c.connPool.Conn()
   160  }
   161  
   162  // Close closes the connection to the API service. The user should invoke this when
   163  // the client is no longer required.
   164  func (c *AlertPolicyClient) Close() error {
   165  	return c.connPool.Close()
   166  }
   167  
   168  // setGoogleClientInfo sets the name and version of the application in
   169  // the `x-goog-api-client` header passed on each request. Intended for
   170  // use by Google-written clients.
   171  func (c *AlertPolicyClient) setGoogleClientInfo(keyval ...string) {
   172  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   173  	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
   174  	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
   175  }
   176  
   177  // ListAlertPolicies lists the existing alerting policies for the project.
   178  func (c *AlertPolicyClient) ListAlertPolicies(ctx context.Context, req *monitoringpb.ListAlertPoliciesRequest, opts ...gax.CallOption) *AlertPolicyIterator {
   179  	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
   180  	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
   181  	opts = append(c.CallOptions.ListAlertPolicies[0:len(c.CallOptions.ListAlertPolicies):len(c.CallOptions.ListAlertPolicies)], opts...)
   182  	it := &AlertPolicyIterator{}
   183  	req = proto.Clone(req).(*monitoringpb.ListAlertPoliciesRequest)
   184  	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.AlertPolicy, string, error) {
   185  		var resp *monitoringpb.ListAlertPoliciesResponse
   186  		req.PageToken = pageToken
   187  		if pageSize > math.MaxInt32 {
   188  			req.PageSize = math.MaxInt32
   189  		} else {
   190  			req.PageSize = int32(pageSize)
   191  		}
   192  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   193  			var err error
   194  			resp, err = c.alertPolicyClient.ListAlertPolicies(ctx, req, settings.GRPC...)
   195  			return err
   196  		}, opts...)
   197  		if err != nil {
   198  			return nil, "", err
   199  		}
   200  
   201  		it.Response = resp
   202  		return resp.AlertPolicies, resp.NextPageToken, nil
   203  	}
   204  	fetch := func(pageSize int, pageToken string) (string, error) {
   205  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   206  		if err != nil {
   207  			return "", err
   208  		}
   209  		it.items = append(it.items, items...)
   210  		return nextPageToken, nil
   211  	}
   212  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   213  	it.pageInfo.MaxSize = int(req.PageSize)
   214  	it.pageInfo.Token = req.PageToken
   215  	return it
   216  }
   217  
   218  // GetAlertPolicy gets a single alerting policy.
   219  func (c *AlertPolicyClient) GetAlertPolicy(ctx context.Context, req *monitoringpb.GetAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
   220  	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
   221  	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
   222  	opts = append(c.CallOptions.GetAlertPolicy[0:len(c.CallOptions.GetAlertPolicy):len(c.CallOptions.GetAlertPolicy)], opts...)
   223  	var resp *monitoringpb.AlertPolicy
   224  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   225  		var err error
   226  		resp, err = c.alertPolicyClient.GetAlertPolicy(ctx, req, settings.GRPC...)
   227  		return err
   228  	}, opts...)
   229  	if err != nil {
   230  		return nil, err
   231  	}
   232  	return resp, nil
   233  }
   234  
   235  // CreateAlertPolicy creates a new alerting policy.
   236  func (c *AlertPolicyClient) CreateAlertPolicy(ctx context.Context, req *monitoringpb.CreateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
   237  	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
   238  	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
   239  	opts = append(c.CallOptions.CreateAlertPolicy[0:len(c.CallOptions.CreateAlertPolicy):len(c.CallOptions.CreateAlertPolicy)], opts...)
   240  	var resp *monitoringpb.AlertPolicy
   241  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   242  		var err error
   243  		resp, err = c.alertPolicyClient.CreateAlertPolicy(ctx, req, settings.GRPC...)
   244  		return err
   245  	}, opts...)
   246  	if err != nil {
   247  		return nil, err
   248  	}
   249  	return resp, nil
   250  }
   251  
   252  // DeleteAlertPolicy deletes an alerting policy.
   253  func (c *AlertPolicyClient) DeleteAlertPolicy(ctx context.Context, req *monitoringpb.DeleteAlertPolicyRequest, opts ...gax.CallOption) error {
   254  	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
   255  	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
   256  	opts = append(c.CallOptions.DeleteAlertPolicy[0:len(c.CallOptions.DeleteAlertPolicy):len(c.CallOptions.DeleteAlertPolicy)], opts...)
   257  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   258  		var err error
   259  		_, err = c.alertPolicyClient.DeleteAlertPolicy(ctx, req, settings.GRPC...)
   260  		return err
   261  	}, opts...)
   262  	return err
   263  }
   264  
   265  // UpdateAlertPolicy updates an alerting policy. You can either replace the entire policy with
   266  // a new one or replace only certain fields in the current alerting policy by
   267  // specifying the fields to be updated via updateMask. Returns the
   268  // updated alerting policy.
   269  func (c *AlertPolicyClient) UpdateAlertPolicy(ctx context.Context, req *monitoringpb.UpdateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
   270  	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "alert_policy.name", url.QueryEscape(req.GetAlertPolicy().GetName())))
   271  	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
   272  	opts = append(c.CallOptions.UpdateAlertPolicy[0:len(c.CallOptions.UpdateAlertPolicy):len(c.CallOptions.UpdateAlertPolicy)], opts...)
   273  	var resp *monitoringpb.AlertPolicy
   274  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   275  		var err error
   276  		resp, err = c.alertPolicyClient.UpdateAlertPolicy(ctx, req, settings.GRPC...)
   277  		return err
   278  	}, opts...)
   279  	if err != nil {
   280  		return nil, err
   281  	}
   282  	return resp, nil
   283  }
   284  
   285  // AlertPolicyIterator manages a stream of *monitoringpb.AlertPolicy.
   286  type AlertPolicyIterator struct {
   287  	items    []*monitoringpb.AlertPolicy
   288  	pageInfo *iterator.PageInfo
   289  	nextFunc func() error
   290  
   291  	// Response is the raw response for the current page.
   292  	// It must be cast to the RPC response type.
   293  	// Calling Next() or InternalFetch() updates this value.
   294  	Response interface{}
   295  
   296  	// InternalFetch is for use by the Google Cloud Libraries only.
   297  	// It is not part of the stable interface of this package.
   298  	//
   299  	// InternalFetch returns results from a single call to the underlying RPC.
   300  	// The number of results is no greater than pageSize.
   301  	// If there are no more results, nextPageToken is empty and err is nil.
   302  	InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.AlertPolicy, nextPageToken string, err error)
   303  }
   304  
   305  // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
   306  func (it *AlertPolicyIterator) PageInfo() *iterator.PageInfo {
   307  	return it.pageInfo
   308  }
   309  
   310  // Next returns the next result. Its second return value is iterator.Done if there are no more
   311  // results. Once Next returns Done, all subsequent calls will return Done.
   312  func (it *AlertPolicyIterator) Next() (*monitoringpb.AlertPolicy, error) {
   313  	var item *monitoringpb.AlertPolicy
   314  	if err := it.nextFunc(); err != nil {
   315  		return item, err
   316  	}
   317  	item = it.items[0]
   318  	it.items = it.items[1:]
   319  	return item, nil
   320  }
   321  
   322  func (it *AlertPolicyIterator) bufLen() int {
   323  	return len(it.items)
   324  }
   325  
   326  func (it *AlertPolicyIterator) takeBuf() interface{} {
   327  	b := it.items
   328  	it.items = nil
   329  	return b
   330  }
   331  

View as plain text