...

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

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

     1  // Copyright 2024 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  	monitoringpb "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  	"google.golang.org/api/option/internaloption"
    31  	gtransport "google.golang.org/api/transport/grpc"
    32  	"google.golang.org/grpc"
    33  	"google.golang.org/grpc/codes"
    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 defaultAlertPolicyGRPCClientOptions() []option.ClientOption {
    49  	return []option.ClientOption{
    50  		internaloption.WithDefaultEndpoint("monitoring.googleapis.com:443"),
    51  		internaloption.WithDefaultEndpointTemplate("monitoring.UNIVERSE_DOMAIN:443"),
    52  		internaloption.WithDefaultMTLSEndpoint("monitoring.mtls.googleapis.com:443"),
    53  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
    54  		internaloption.WithDefaultAudience("https://monitoring.googleapis.com/"),
    55  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
    56  		internaloption.EnableJwtWithScope(),
    57  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    58  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    59  	}
    60  }
    61  
    62  func defaultAlertPolicyCallOptions() *AlertPolicyCallOptions {
    63  	return &AlertPolicyCallOptions{
    64  		ListAlertPolicies: []gax.CallOption{
    65  			gax.WithTimeout(30000 * time.Millisecond),
    66  			gax.WithRetry(func() gax.Retryer {
    67  				return gax.OnCodes([]codes.Code{
    68  					codes.Unavailable,
    69  				}, gax.Backoff{
    70  					Initial:    100 * time.Millisecond,
    71  					Max:        30000 * time.Millisecond,
    72  					Multiplier: 1.30,
    73  				})
    74  			}),
    75  		},
    76  		GetAlertPolicy: []gax.CallOption{
    77  			gax.WithTimeout(30000 * time.Millisecond),
    78  			gax.WithRetry(func() gax.Retryer {
    79  				return gax.OnCodes([]codes.Code{
    80  					codes.Unavailable,
    81  				}, gax.Backoff{
    82  					Initial:    100 * time.Millisecond,
    83  					Max:        30000 * time.Millisecond,
    84  					Multiplier: 1.30,
    85  				})
    86  			}),
    87  		},
    88  		CreateAlertPolicy: []gax.CallOption{
    89  			gax.WithTimeout(30000 * time.Millisecond),
    90  		},
    91  		DeleteAlertPolicy: []gax.CallOption{
    92  			gax.WithTimeout(30000 * time.Millisecond),
    93  			gax.WithRetry(func() gax.Retryer {
    94  				return gax.OnCodes([]codes.Code{
    95  					codes.Unavailable,
    96  				}, gax.Backoff{
    97  					Initial:    100 * time.Millisecond,
    98  					Max:        30000 * time.Millisecond,
    99  					Multiplier: 1.30,
   100  				})
   101  			}),
   102  		},
   103  		UpdateAlertPolicy: []gax.CallOption{
   104  			gax.WithTimeout(30000 * time.Millisecond),
   105  		},
   106  	}
   107  }
   108  
   109  // internalAlertPolicyClient is an interface that defines the methods available from Cloud Monitoring API.
   110  type internalAlertPolicyClient interface {
   111  	Close() error
   112  	setGoogleClientInfo(...string)
   113  	Connection() *grpc.ClientConn
   114  	ListAlertPolicies(context.Context, *monitoringpb.ListAlertPoliciesRequest, ...gax.CallOption) *AlertPolicyIterator
   115  	GetAlertPolicy(context.Context, *monitoringpb.GetAlertPolicyRequest, ...gax.CallOption) (*monitoringpb.AlertPolicy, error)
   116  	CreateAlertPolicy(context.Context, *monitoringpb.CreateAlertPolicyRequest, ...gax.CallOption) (*monitoringpb.AlertPolicy, error)
   117  	DeleteAlertPolicy(context.Context, *monitoringpb.DeleteAlertPolicyRequest, ...gax.CallOption) error
   118  	UpdateAlertPolicy(context.Context, *monitoringpb.UpdateAlertPolicyRequest, ...gax.CallOption) (*monitoringpb.AlertPolicy, error)
   119  }
   120  
   121  // AlertPolicyClient is a client for interacting with Cloud Monitoring API.
   122  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   123  //
   124  // The AlertPolicyService API is used to manage (list, create, delete,
   125  // edit) alert policies in Cloud Monitoring. An alerting policy is
   126  // a description of the conditions under which some aspect of your
   127  // system is considered to be “unhealthy” and the ways to notify
   128  // people or services about this state. In addition to using this API, alert
   129  // policies can also be managed through
   130  // Cloud Monitoring (at https://cloud.google.com/monitoring/docs/),
   131  // which can be reached by clicking the “Monitoring” tab in
   132  // Cloud console (at https://console.cloud.google.com/).
   133  type AlertPolicyClient struct {
   134  	// The internal transport-dependent client.
   135  	internalClient internalAlertPolicyClient
   136  
   137  	// The call options for this service.
   138  	CallOptions *AlertPolicyCallOptions
   139  }
   140  
   141  // Wrapper methods routed to the internal client.
   142  
   143  // Close closes the connection to the API service. The user should invoke this when
   144  // the client is no longer required.
   145  func (c *AlertPolicyClient) Close() error {
   146  	return c.internalClient.Close()
   147  }
   148  
   149  // setGoogleClientInfo sets the name and version of the application in
   150  // the `x-goog-api-client` header passed on each request. Intended for
   151  // use by Google-written clients.
   152  func (c *AlertPolicyClient) setGoogleClientInfo(keyval ...string) {
   153  	c.internalClient.setGoogleClientInfo(keyval...)
   154  }
   155  
   156  // Connection returns a connection to the API service.
   157  //
   158  // Deprecated: Connections are now pooled so this method does not always
   159  // return the same resource.
   160  func (c *AlertPolicyClient) Connection() *grpc.ClientConn {
   161  	return c.internalClient.Connection()
   162  }
   163  
   164  // ListAlertPolicies lists the existing alerting policies for the workspace.
   165  func (c *AlertPolicyClient) ListAlertPolicies(ctx context.Context, req *monitoringpb.ListAlertPoliciesRequest, opts ...gax.CallOption) *AlertPolicyIterator {
   166  	return c.internalClient.ListAlertPolicies(ctx, req, opts...)
   167  }
   168  
   169  // GetAlertPolicy gets a single alerting policy.
   170  func (c *AlertPolicyClient) GetAlertPolicy(ctx context.Context, req *monitoringpb.GetAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
   171  	return c.internalClient.GetAlertPolicy(ctx, req, opts...)
   172  }
   173  
   174  // CreateAlertPolicy creates a new alerting policy.
   175  //
   176  // Design your application to single-thread API calls that modify the state of
   177  // alerting policies in a single project. This includes calls to
   178  // CreateAlertPolicy, DeleteAlertPolicy and UpdateAlertPolicy.
   179  func (c *AlertPolicyClient) CreateAlertPolicy(ctx context.Context, req *monitoringpb.CreateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
   180  	return c.internalClient.CreateAlertPolicy(ctx, req, opts...)
   181  }
   182  
   183  // DeleteAlertPolicy deletes an alerting policy.
   184  //
   185  // Design your application to single-thread API calls that modify the state of
   186  // alerting policies in a single project. This includes calls to
   187  // CreateAlertPolicy, DeleteAlertPolicy and UpdateAlertPolicy.
   188  func (c *AlertPolicyClient) DeleteAlertPolicy(ctx context.Context, req *monitoringpb.DeleteAlertPolicyRequest, opts ...gax.CallOption) error {
   189  	return c.internalClient.DeleteAlertPolicy(ctx, req, opts...)
   190  }
   191  
   192  // UpdateAlertPolicy updates an alerting policy. You can either replace the entire policy with
   193  // a new one or replace only certain fields in the current alerting policy by
   194  // specifying the fields to be updated via updateMask. Returns the
   195  // updated alerting policy.
   196  //
   197  // Design your application to single-thread API calls that modify the state of
   198  // alerting policies in a single project. This includes calls to
   199  // CreateAlertPolicy, DeleteAlertPolicy and UpdateAlertPolicy.
   200  func (c *AlertPolicyClient) UpdateAlertPolicy(ctx context.Context, req *monitoringpb.UpdateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
   201  	return c.internalClient.UpdateAlertPolicy(ctx, req, opts...)
   202  }
   203  
   204  // alertPolicyGRPCClient is a client for interacting with Cloud Monitoring API over gRPC transport.
   205  //
   206  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   207  type alertPolicyGRPCClient struct {
   208  	// Connection pool of gRPC connections to the service.
   209  	connPool gtransport.ConnPool
   210  
   211  	// Points back to the CallOptions field of the containing AlertPolicyClient
   212  	CallOptions **AlertPolicyCallOptions
   213  
   214  	// The gRPC API client.
   215  	alertPolicyClient monitoringpb.AlertPolicyServiceClient
   216  
   217  	// The x-goog-* metadata to be sent with each request.
   218  	xGoogHeaders []string
   219  }
   220  
   221  // NewAlertPolicyClient creates a new alert policy service client based on gRPC.
   222  // The returned client must be Closed when it is done being used to clean up its underlying connections.
   223  //
   224  // The AlertPolicyService API is used to manage (list, create, delete,
   225  // edit) alert policies in Cloud Monitoring. An alerting policy is
   226  // a description of the conditions under which some aspect of your
   227  // system is considered to be “unhealthy” and the ways to notify
   228  // people or services about this state. In addition to using this API, alert
   229  // policies can also be managed through
   230  // Cloud Monitoring (at https://cloud.google.com/monitoring/docs/),
   231  // which can be reached by clicking the “Monitoring” tab in
   232  // Cloud console (at https://console.cloud.google.com/).
   233  func NewAlertPolicyClient(ctx context.Context, opts ...option.ClientOption) (*AlertPolicyClient, error) {
   234  	clientOpts := defaultAlertPolicyGRPCClientOptions()
   235  	if newAlertPolicyClientHook != nil {
   236  		hookOpts, err := newAlertPolicyClientHook(ctx, clientHookParams{})
   237  		if err != nil {
   238  			return nil, err
   239  		}
   240  		clientOpts = append(clientOpts, hookOpts...)
   241  	}
   242  
   243  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   244  	if err != nil {
   245  		return nil, err
   246  	}
   247  	client := AlertPolicyClient{CallOptions: defaultAlertPolicyCallOptions()}
   248  
   249  	c := &alertPolicyGRPCClient{
   250  		connPool:          connPool,
   251  		alertPolicyClient: monitoringpb.NewAlertPolicyServiceClient(connPool),
   252  		CallOptions:       &client.CallOptions,
   253  	}
   254  	c.setGoogleClientInfo()
   255  
   256  	client.internalClient = c
   257  
   258  	return &client, nil
   259  }
   260  
   261  // Connection returns a connection to the API service.
   262  //
   263  // Deprecated: Connections are now pooled so this method does not always
   264  // return the same resource.
   265  func (c *alertPolicyGRPCClient) Connection() *grpc.ClientConn {
   266  	return c.connPool.Conn()
   267  }
   268  
   269  // setGoogleClientInfo sets the name and version of the application in
   270  // the `x-goog-api-client` header passed on each request. Intended for
   271  // use by Google-written clients.
   272  func (c *alertPolicyGRPCClient) setGoogleClientInfo(keyval ...string) {
   273  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   274  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
   275  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   276  }
   277  
   278  // Close closes the connection to the API service. The user should invoke this when
   279  // the client is no longer required.
   280  func (c *alertPolicyGRPCClient) Close() error {
   281  	return c.connPool.Close()
   282  }
   283  
   284  func (c *alertPolicyGRPCClient) ListAlertPolicies(ctx context.Context, req *monitoringpb.ListAlertPoliciesRequest, opts ...gax.CallOption) *AlertPolicyIterator {
   285  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   286  
   287  	hds = append(c.xGoogHeaders, hds...)
   288  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   289  	opts = append((*c.CallOptions).ListAlertPolicies[0:len((*c.CallOptions).ListAlertPolicies):len((*c.CallOptions).ListAlertPolicies)], opts...)
   290  	it := &AlertPolicyIterator{}
   291  	req = proto.Clone(req).(*monitoringpb.ListAlertPoliciesRequest)
   292  	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.AlertPolicy, string, error) {
   293  		resp := &monitoringpb.ListAlertPoliciesResponse{}
   294  		if pageToken != "" {
   295  			req.PageToken = pageToken
   296  		}
   297  		if pageSize > math.MaxInt32 {
   298  			req.PageSize = math.MaxInt32
   299  		} else if pageSize != 0 {
   300  			req.PageSize = int32(pageSize)
   301  		}
   302  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   303  			var err error
   304  			resp, err = c.alertPolicyClient.ListAlertPolicies(ctx, req, settings.GRPC...)
   305  			return err
   306  		}, opts...)
   307  		if err != nil {
   308  			return nil, "", err
   309  		}
   310  
   311  		it.Response = resp
   312  		return resp.GetAlertPolicies(), resp.GetNextPageToken(), nil
   313  	}
   314  	fetch := func(pageSize int, pageToken string) (string, error) {
   315  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   316  		if err != nil {
   317  			return "", err
   318  		}
   319  		it.items = append(it.items, items...)
   320  		return nextPageToken, nil
   321  	}
   322  
   323  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   324  	it.pageInfo.MaxSize = int(req.GetPageSize())
   325  	it.pageInfo.Token = req.GetPageToken()
   326  
   327  	return it
   328  }
   329  
   330  func (c *alertPolicyGRPCClient) GetAlertPolicy(ctx context.Context, req *monitoringpb.GetAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
   331  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   332  
   333  	hds = append(c.xGoogHeaders, hds...)
   334  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   335  	opts = append((*c.CallOptions).GetAlertPolicy[0:len((*c.CallOptions).GetAlertPolicy):len((*c.CallOptions).GetAlertPolicy)], opts...)
   336  	var resp *monitoringpb.AlertPolicy
   337  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   338  		var err error
   339  		resp, err = c.alertPolicyClient.GetAlertPolicy(ctx, req, settings.GRPC...)
   340  		return err
   341  	}, opts...)
   342  	if err != nil {
   343  		return nil, err
   344  	}
   345  	return resp, nil
   346  }
   347  
   348  func (c *alertPolicyGRPCClient) CreateAlertPolicy(ctx context.Context, req *monitoringpb.CreateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
   349  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   350  
   351  	hds = append(c.xGoogHeaders, hds...)
   352  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   353  	opts = append((*c.CallOptions).CreateAlertPolicy[0:len((*c.CallOptions).CreateAlertPolicy):len((*c.CallOptions).CreateAlertPolicy)], opts...)
   354  	var resp *monitoringpb.AlertPolicy
   355  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   356  		var err error
   357  		resp, err = c.alertPolicyClient.CreateAlertPolicy(ctx, req, settings.GRPC...)
   358  		return err
   359  	}, opts...)
   360  	if err != nil {
   361  		return nil, err
   362  	}
   363  	return resp, nil
   364  }
   365  
   366  func (c *alertPolicyGRPCClient) DeleteAlertPolicy(ctx context.Context, req *monitoringpb.DeleteAlertPolicyRequest, opts ...gax.CallOption) error {
   367  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   368  
   369  	hds = append(c.xGoogHeaders, hds...)
   370  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   371  	opts = append((*c.CallOptions).DeleteAlertPolicy[0:len((*c.CallOptions).DeleteAlertPolicy):len((*c.CallOptions).DeleteAlertPolicy)], opts...)
   372  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   373  		var err error
   374  		_, err = c.alertPolicyClient.DeleteAlertPolicy(ctx, req, settings.GRPC...)
   375  		return err
   376  	}, opts...)
   377  	return err
   378  }
   379  
   380  func (c *alertPolicyGRPCClient) UpdateAlertPolicy(ctx context.Context, req *monitoringpb.UpdateAlertPolicyRequest, opts ...gax.CallOption) (*monitoringpb.AlertPolicy, error) {
   381  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "alert_policy.name", url.QueryEscape(req.GetAlertPolicy().GetName()))}
   382  
   383  	hds = append(c.xGoogHeaders, hds...)
   384  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   385  	opts = append((*c.CallOptions).UpdateAlertPolicy[0:len((*c.CallOptions).UpdateAlertPolicy):len((*c.CallOptions).UpdateAlertPolicy)], opts...)
   386  	var resp *monitoringpb.AlertPolicy
   387  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   388  		var err error
   389  		resp, err = c.alertPolicyClient.UpdateAlertPolicy(ctx, req, settings.GRPC...)
   390  		return err
   391  	}, opts...)
   392  	if err != nil {
   393  		return nil, err
   394  	}
   395  	return resp, nil
   396  }
   397  

View as plain text