...

Source file src/cloud.google.com/go/monitoring/apiv3/v2/snooze_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 newSnoozeClientHook clientHook
    38  
    39  // SnoozeCallOptions contains the retry settings for each method of SnoozeClient.
    40  type SnoozeCallOptions struct {
    41  	CreateSnooze []gax.CallOption
    42  	ListSnoozes  []gax.CallOption
    43  	GetSnooze    []gax.CallOption
    44  	UpdateSnooze []gax.CallOption
    45  }
    46  
    47  func defaultSnoozeGRPCClientOptions() []option.ClientOption {
    48  	return []option.ClientOption{
    49  		internaloption.WithDefaultEndpoint("monitoring.googleapis.com:443"),
    50  		internaloption.WithDefaultEndpointTemplate("monitoring.UNIVERSE_DOMAIN:443"),
    51  		internaloption.WithDefaultMTLSEndpoint("monitoring.mtls.googleapis.com:443"),
    52  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
    53  		internaloption.WithDefaultAudience("https://monitoring.googleapis.com/"),
    54  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
    55  		internaloption.EnableJwtWithScope(),
    56  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    57  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    58  	}
    59  }
    60  
    61  func defaultSnoozeCallOptions() *SnoozeCallOptions {
    62  	return &SnoozeCallOptions{
    63  		CreateSnooze: []gax.CallOption{
    64  			gax.WithTimeout(30000 * time.Millisecond),
    65  		},
    66  		ListSnoozes: []gax.CallOption{
    67  			gax.WithTimeout(30000 * time.Millisecond),
    68  			gax.WithRetry(func() gax.Retryer {
    69  				return gax.OnCodes([]codes.Code{
    70  					codes.Unavailable,
    71  				}, gax.Backoff{
    72  					Initial:    100 * time.Millisecond,
    73  					Max:        30000 * time.Millisecond,
    74  					Multiplier: 1.30,
    75  				})
    76  			}),
    77  		},
    78  		GetSnooze: []gax.CallOption{
    79  			gax.WithTimeout(30000 * time.Millisecond),
    80  			gax.WithRetry(func() gax.Retryer {
    81  				return gax.OnCodes([]codes.Code{
    82  					codes.Unavailable,
    83  				}, gax.Backoff{
    84  					Initial:    100 * time.Millisecond,
    85  					Max:        30000 * time.Millisecond,
    86  					Multiplier: 1.30,
    87  				})
    88  			}),
    89  		},
    90  		UpdateSnooze: []gax.CallOption{
    91  			gax.WithTimeout(30000 * time.Millisecond),
    92  		},
    93  	}
    94  }
    95  
    96  // internalSnoozeClient is an interface that defines the methods available from Cloud Monitoring API.
    97  type internalSnoozeClient interface {
    98  	Close() error
    99  	setGoogleClientInfo(...string)
   100  	Connection() *grpc.ClientConn
   101  	CreateSnooze(context.Context, *monitoringpb.CreateSnoozeRequest, ...gax.CallOption) (*monitoringpb.Snooze, error)
   102  	ListSnoozes(context.Context, *monitoringpb.ListSnoozesRequest, ...gax.CallOption) *SnoozeIterator
   103  	GetSnooze(context.Context, *monitoringpb.GetSnoozeRequest, ...gax.CallOption) (*monitoringpb.Snooze, error)
   104  	UpdateSnooze(context.Context, *monitoringpb.UpdateSnoozeRequest, ...gax.CallOption) (*monitoringpb.Snooze, error)
   105  }
   106  
   107  // SnoozeClient is a client for interacting with Cloud Monitoring API.
   108  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   109  //
   110  // The SnoozeService API is used to temporarily prevent an alert policy from
   111  // generating alerts. A Snooze is a description of the criteria under which one
   112  // or more alert policies should not fire alerts for the specified duration.
   113  type SnoozeClient struct {
   114  	// The internal transport-dependent client.
   115  	internalClient internalSnoozeClient
   116  
   117  	// The call options for this service.
   118  	CallOptions *SnoozeCallOptions
   119  }
   120  
   121  // Wrapper methods routed to the internal client.
   122  
   123  // Close closes the connection to the API service. The user should invoke this when
   124  // the client is no longer required.
   125  func (c *SnoozeClient) Close() error {
   126  	return c.internalClient.Close()
   127  }
   128  
   129  // setGoogleClientInfo sets the name and version of the application in
   130  // the `x-goog-api-client` header passed on each request. Intended for
   131  // use by Google-written clients.
   132  func (c *SnoozeClient) setGoogleClientInfo(keyval ...string) {
   133  	c.internalClient.setGoogleClientInfo(keyval...)
   134  }
   135  
   136  // Connection returns a connection to the API service.
   137  //
   138  // Deprecated: Connections are now pooled so this method does not always
   139  // return the same resource.
   140  func (c *SnoozeClient) Connection() *grpc.ClientConn {
   141  	return c.internalClient.Connection()
   142  }
   143  
   144  // CreateSnooze creates a Snooze that will prevent alerts, which match the provided
   145  // criteria, from being opened. The Snooze applies for a specific time
   146  // interval.
   147  func (c *SnoozeClient) CreateSnooze(ctx context.Context, req *monitoringpb.CreateSnoozeRequest, opts ...gax.CallOption) (*monitoringpb.Snooze, error) {
   148  	return c.internalClient.CreateSnooze(ctx, req, opts...)
   149  }
   150  
   151  // ListSnoozes lists the Snoozes associated with a project. Can optionally pass in
   152  // filter, which specifies predicates to match Snoozes.
   153  func (c *SnoozeClient) ListSnoozes(ctx context.Context, req *monitoringpb.ListSnoozesRequest, opts ...gax.CallOption) *SnoozeIterator {
   154  	return c.internalClient.ListSnoozes(ctx, req, opts...)
   155  }
   156  
   157  // GetSnooze retrieves a Snooze by name.
   158  func (c *SnoozeClient) GetSnooze(ctx context.Context, req *monitoringpb.GetSnoozeRequest, opts ...gax.CallOption) (*monitoringpb.Snooze, error) {
   159  	return c.internalClient.GetSnooze(ctx, req, opts...)
   160  }
   161  
   162  // UpdateSnooze updates a Snooze, identified by its name, with the parameters in the
   163  // given Snooze object.
   164  func (c *SnoozeClient) UpdateSnooze(ctx context.Context, req *monitoringpb.UpdateSnoozeRequest, opts ...gax.CallOption) (*monitoringpb.Snooze, error) {
   165  	return c.internalClient.UpdateSnooze(ctx, req, opts...)
   166  }
   167  
   168  // snoozeGRPCClient is a client for interacting with Cloud Monitoring API over gRPC transport.
   169  //
   170  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   171  type snoozeGRPCClient struct {
   172  	// Connection pool of gRPC connections to the service.
   173  	connPool gtransport.ConnPool
   174  
   175  	// Points back to the CallOptions field of the containing SnoozeClient
   176  	CallOptions **SnoozeCallOptions
   177  
   178  	// The gRPC API client.
   179  	snoozeClient monitoringpb.SnoozeServiceClient
   180  
   181  	// The x-goog-* metadata to be sent with each request.
   182  	xGoogHeaders []string
   183  }
   184  
   185  // NewSnoozeClient creates a new snooze service client based on gRPC.
   186  // The returned client must be Closed when it is done being used to clean up its underlying connections.
   187  //
   188  // The SnoozeService API is used to temporarily prevent an alert policy from
   189  // generating alerts. A Snooze is a description of the criteria under which one
   190  // or more alert policies should not fire alerts for the specified duration.
   191  func NewSnoozeClient(ctx context.Context, opts ...option.ClientOption) (*SnoozeClient, error) {
   192  	clientOpts := defaultSnoozeGRPCClientOptions()
   193  	if newSnoozeClientHook != nil {
   194  		hookOpts, err := newSnoozeClientHook(ctx, clientHookParams{})
   195  		if err != nil {
   196  			return nil, err
   197  		}
   198  		clientOpts = append(clientOpts, hookOpts...)
   199  	}
   200  
   201  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   202  	if err != nil {
   203  		return nil, err
   204  	}
   205  	client := SnoozeClient{CallOptions: defaultSnoozeCallOptions()}
   206  
   207  	c := &snoozeGRPCClient{
   208  		connPool:     connPool,
   209  		snoozeClient: monitoringpb.NewSnoozeServiceClient(connPool),
   210  		CallOptions:  &client.CallOptions,
   211  	}
   212  	c.setGoogleClientInfo()
   213  
   214  	client.internalClient = c
   215  
   216  	return &client, nil
   217  }
   218  
   219  // Connection returns a connection to the API service.
   220  //
   221  // Deprecated: Connections are now pooled so this method does not always
   222  // return the same resource.
   223  func (c *snoozeGRPCClient) Connection() *grpc.ClientConn {
   224  	return c.connPool.Conn()
   225  }
   226  
   227  // setGoogleClientInfo sets the name and version of the application in
   228  // the `x-goog-api-client` header passed on each request. Intended for
   229  // use by Google-written clients.
   230  func (c *snoozeGRPCClient) setGoogleClientInfo(keyval ...string) {
   231  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   232  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
   233  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   234  }
   235  
   236  // Close closes the connection to the API service. The user should invoke this when
   237  // the client is no longer required.
   238  func (c *snoozeGRPCClient) Close() error {
   239  	return c.connPool.Close()
   240  }
   241  
   242  func (c *snoozeGRPCClient) CreateSnooze(ctx context.Context, req *monitoringpb.CreateSnoozeRequest, opts ...gax.CallOption) (*monitoringpb.Snooze, error) {
   243  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   244  
   245  	hds = append(c.xGoogHeaders, hds...)
   246  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   247  	opts = append((*c.CallOptions).CreateSnooze[0:len((*c.CallOptions).CreateSnooze):len((*c.CallOptions).CreateSnooze)], opts...)
   248  	var resp *monitoringpb.Snooze
   249  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   250  		var err error
   251  		resp, err = c.snoozeClient.CreateSnooze(ctx, req, settings.GRPC...)
   252  		return err
   253  	}, opts...)
   254  	if err != nil {
   255  		return nil, err
   256  	}
   257  	return resp, nil
   258  }
   259  
   260  func (c *snoozeGRPCClient) ListSnoozes(ctx context.Context, req *monitoringpb.ListSnoozesRequest, opts ...gax.CallOption) *SnoozeIterator {
   261  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   262  
   263  	hds = append(c.xGoogHeaders, hds...)
   264  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   265  	opts = append((*c.CallOptions).ListSnoozes[0:len((*c.CallOptions).ListSnoozes):len((*c.CallOptions).ListSnoozes)], opts...)
   266  	it := &SnoozeIterator{}
   267  	req = proto.Clone(req).(*monitoringpb.ListSnoozesRequest)
   268  	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.Snooze, string, error) {
   269  		resp := &monitoringpb.ListSnoozesResponse{}
   270  		if pageToken != "" {
   271  			req.PageToken = pageToken
   272  		}
   273  		if pageSize > math.MaxInt32 {
   274  			req.PageSize = math.MaxInt32
   275  		} else if pageSize != 0 {
   276  			req.PageSize = int32(pageSize)
   277  		}
   278  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   279  			var err error
   280  			resp, err = c.snoozeClient.ListSnoozes(ctx, req, settings.GRPC...)
   281  			return err
   282  		}, opts...)
   283  		if err != nil {
   284  			return nil, "", err
   285  		}
   286  
   287  		it.Response = resp
   288  		return resp.GetSnoozes(), resp.GetNextPageToken(), nil
   289  	}
   290  	fetch := func(pageSize int, pageToken string) (string, error) {
   291  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   292  		if err != nil {
   293  			return "", err
   294  		}
   295  		it.items = append(it.items, items...)
   296  		return nextPageToken, nil
   297  	}
   298  
   299  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   300  	it.pageInfo.MaxSize = int(req.GetPageSize())
   301  	it.pageInfo.Token = req.GetPageToken()
   302  
   303  	return it
   304  }
   305  
   306  func (c *snoozeGRPCClient) GetSnooze(ctx context.Context, req *monitoringpb.GetSnoozeRequest, opts ...gax.CallOption) (*monitoringpb.Snooze, error) {
   307  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   308  
   309  	hds = append(c.xGoogHeaders, hds...)
   310  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   311  	opts = append((*c.CallOptions).GetSnooze[0:len((*c.CallOptions).GetSnooze):len((*c.CallOptions).GetSnooze)], opts...)
   312  	var resp *monitoringpb.Snooze
   313  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   314  		var err error
   315  		resp, err = c.snoozeClient.GetSnooze(ctx, req, settings.GRPC...)
   316  		return err
   317  	}, opts...)
   318  	if err != nil {
   319  		return nil, err
   320  	}
   321  	return resp, nil
   322  }
   323  
   324  func (c *snoozeGRPCClient) UpdateSnooze(ctx context.Context, req *monitoringpb.UpdateSnoozeRequest, opts ...gax.CallOption) (*monitoringpb.Snooze, error) {
   325  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "snooze.name", url.QueryEscape(req.GetSnooze().GetName()))}
   326  
   327  	hds = append(c.xGoogHeaders, hds...)
   328  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   329  	opts = append((*c.CallOptions).UpdateSnooze[0:len((*c.CallOptions).UpdateSnooze):len((*c.CallOptions).UpdateSnooze)], opts...)
   330  	var resp *monitoringpb.Snooze
   331  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   332  		var err error
   333  		resp, err = c.snoozeClient.UpdateSnooze(ctx, req, settings.GRPC...)
   334  		return err
   335  	}, opts...)
   336  	if err != nil {
   337  		return nil, err
   338  	}
   339  	return resp, nil
   340  }
   341  

View as plain text