...

Source file src/cloud.google.com/go/monitoring/apiv3/v2/group_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  	monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
    33  	"google.golang.org/grpc"
    34  	"google.golang.org/grpc/codes"
    35  	"google.golang.org/protobuf/proto"
    36  )
    37  
    38  var newGroupClientHook clientHook
    39  
    40  // GroupCallOptions contains the retry settings for each method of GroupClient.
    41  type GroupCallOptions struct {
    42  	ListGroups       []gax.CallOption
    43  	GetGroup         []gax.CallOption
    44  	CreateGroup      []gax.CallOption
    45  	UpdateGroup      []gax.CallOption
    46  	DeleteGroup      []gax.CallOption
    47  	ListGroupMembers []gax.CallOption
    48  }
    49  
    50  func defaultGroupGRPCClientOptions() []option.ClientOption {
    51  	return []option.ClientOption{
    52  		internaloption.WithDefaultEndpoint("monitoring.googleapis.com:443"),
    53  		internaloption.WithDefaultEndpointTemplate("monitoring.UNIVERSE_DOMAIN:443"),
    54  		internaloption.WithDefaultMTLSEndpoint("monitoring.mtls.googleapis.com:443"),
    55  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
    56  		internaloption.WithDefaultAudience("https://monitoring.googleapis.com/"),
    57  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
    58  		internaloption.EnableJwtWithScope(),
    59  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    60  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    61  	}
    62  }
    63  
    64  func defaultGroupCallOptions() *GroupCallOptions {
    65  	return &GroupCallOptions{
    66  		ListGroups: []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  		GetGroup: []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  		CreateGroup: []gax.CallOption{
    91  			gax.WithTimeout(30000 * time.Millisecond),
    92  		},
    93  		UpdateGroup: []gax.CallOption{
    94  			gax.WithTimeout(180000 * time.Millisecond),
    95  			gax.WithRetry(func() gax.Retryer {
    96  				return gax.OnCodes([]codes.Code{
    97  					codes.Unavailable,
    98  				}, gax.Backoff{
    99  					Initial:    100 * time.Millisecond,
   100  					Max:        30000 * time.Millisecond,
   101  					Multiplier: 1.30,
   102  				})
   103  			}),
   104  		},
   105  		DeleteGroup: []gax.CallOption{
   106  			gax.WithTimeout(30000 * time.Millisecond),
   107  			gax.WithRetry(func() gax.Retryer {
   108  				return gax.OnCodes([]codes.Code{
   109  					codes.Unavailable,
   110  				}, gax.Backoff{
   111  					Initial:    100 * time.Millisecond,
   112  					Max:        30000 * time.Millisecond,
   113  					Multiplier: 1.30,
   114  				})
   115  			}),
   116  		},
   117  		ListGroupMembers: []gax.CallOption{
   118  			gax.WithTimeout(30000 * time.Millisecond),
   119  			gax.WithRetry(func() gax.Retryer {
   120  				return gax.OnCodes([]codes.Code{
   121  					codes.Unavailable,
   122  				}, gax.Backoff{
   123  					Initial:    100 * time.Millisecond,
   124  					Max:        30000 * time.Millisecond,
   125  					Multiplier: 1.30,
   126  				})
   127  			}),
   128  		},
   129  	}
   130  }
   131  
   132  // internalGroupClient is an interface that defines the methods available from Cloud Monitoring API.
   133  type internalGroupClient interface {
   134  	Close() error
   135  	setGoogleClientInfo(...string)
   136  	Connection() *grpc.ClientConn
   137  	ListGroups(context.Context, *monitoringpb.ListGroupsRequest, ...gax.CallOption) *GroupIterator
   138  	GetGroup(context.Context, *monitoringpb.GetGroupRequest, ...gax.CallOption) (*monitoringpb.Group, error)
   139  	CreateGroup(context.Context, *monitoringpb.CreateGroupRequest, ...gax.CallOption) (*monitoringpb.Group, error)
   140  	UpdateGroup(context.Context, *monitoringpb.UpdateGroupRequest, ...gax.CallOption) (*monitoringpb.Group, error)
   141  	DeleteGroup(context.Context, *monitoringpb.DeleteGroupRequest, ...gax.CallOption) error
   142  	ListGroupMembers(context.Context, *monitoringpb.ListGroupMembersRequest, ...gax.CallOption) *MonitoredResourceIterator
   143  }
   144  
   145  // GroupClient is a client for interacting with Cloud Monitoring API.
   146  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   147  //
   148  // The Group API lets you inspect and manage your
   149  // groups (at #google.monitoring.v3.Group).
   150  //
   151  // A group is a named filter that is used to identify
   152  // a collection of monitored resources. Groups are typically used to
   153  // mirror the physical and/or logical topology of the environment.
   154  // Because group membership is computed dynamically, monitored
   155  // resources that are started in the future are automatically placed
   156  // in matching groups. By using a group to name monitored resources in,
   157  // for example, an alert policy, the target of that alert policy is
   158  // updated automatically as monitored resources are added and removed
   159  // from the infrastructure.
   160  type GroupClient struct {
   161  	// The internal transport-dependent client.
   162  	internalClient internalGroupClient
   163  
   164  	// The call options for this service.
   165  	CallOptions *GroupCallOptions
   166  }
   167  
   168  // Wrapper methods routed to the internal client.
   169  
   170  // Close closes the connection to the API service. The user should invoke this when
   171  // the client is no longer required.
   172  func (c *GroupClient) Close() error {
   173  	return c.internalClient.Close()
   174  }
   175  
   176  // setGoogleClientInfo sets the name and version of the application in
   177  // the `x-goog-api-client` header passed on each request. Intended for
   178  // use by Google-written clients.
   179  func (c *GroupClient) setGoogleClientInfo(keyval ...string) {
   180  	c.internalClient.setGoogleClientInfo(keyval...)
   181  }
   182  
   183  // Connection returns a connection to the API service.
   184  //
   185  // Deprecated: Connections are now pooled so this method does not always
   186  // return the same resource.
   187  func (c *GroupClient) Connection() *grpc.ClientConn {
   188  	return c.internalClient.Connection()
   189  }
   190  
   191  // ListGroups lists the existing groups.
   192  func (c *GroupClient) ListGroups(ctx context.Context, req *monitoringpb.ListGroupsRequest, opts ...gax.CallOption) *GroupIterator {
   193  	return c.internalClient.ListGroups(ctx, req, opts...)
   194  }
   195  
   196  // GetGroup gets a single group.
   197  func (c *GroupClient) GetGroup(ctx context.Context, req *monitoringpb.GetGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
   198  	return c.internalClient.GetGroup(ctx, req, opts...)
   199  }
   200  
   201  // CreateGroup creates a new group.
   202  func (c *GroupClient) CreateGroup(ctx context.Context, req *monitoringpb.CreateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
   203  	return c.internalClient.CreateGroup(ctx, req, opts...)
   204  }
   205  
   206  // UpdateGroup updates an existing group.
   207  // You can change any group attributes except name.
   208  func (c *GroupClient) UpdateGroup(ctx context.Context, req *monitoringpb.UpdateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
   209  	return c.internalClient.UpdateGroup(ctx, req, opts...)
   210  }
   211  
   212  // DeleteGroup deletes an existing group.
   213  func (c *GroupClient) DeleteGroup(ctx context.Context, req *monitoringpb.DeleteGroupRequest, opts ...gax.CallOption) error {
   214  	return c.internalClient.DeleteGroup(ctx, req, opts...)
   215  }
   216  
   217  // ListGroupMembers lists the monitored resources that are members of a group.
   218  func (c *GroupClient) ListGroupMembers(ctx context.Context, req *monitoringpb.ListGroupMembersRequest, opts ...gax.CallOption) *MonitoredResourceIterator {
   219  	return c.internalClient.ListGroupMembers(ctx, req, opts...)
   220  }
   221  
   222  // groupGRPCClient is a client for interacting with Cloud Monitoring API over gRPC transport.
   223  //
   224  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   225  type groupGRPCClient struct {
   226  	// Connection pool of gRPC connections to the service.
   227  	connPool gtransport.ConnPool
   228  
   229  	// Points back to the CallOptions field of the containing GroupClient
   230  	CallOptions **GroupCallOptions
   231  
   232  	// The gRPC API client.
   233  	groupClient monitoringpb.GroupServiceClient
   234  
   235  	// The x-goog-* metadata to be sent with each request.
   236  	xGoogHeaders []string
   237  }
   238  
   239  // NewGroupClient creates a new group service client based on gRPC.
   240  // The returned client must be Closed when it is done being used to clean up its underlying connections.
   241  //
   242  // The Group API lets you inspect and manage your
   243  // groups (at #google.monitoring.v3.Group).
   244  //
   245  // A group is a named filter that is used to identify
   246  // a collection of monitored resources. Groups are typically used to
   247  // mirror the physical and/or logical topology of the environment.
   248  // Because group membership is computed dynamically, monitored
   249  // resources that are started in the future are automatically placed
   250  // in matching groups. By using a group to name monitored resources in,
   251  // for example, an alert policy, the target of that alert policy is
   252  // updated automatically as monitored resources are added and removed
   253  // from the infrastructure.
   254  func NewGroupClient(ctx context.Context, opts ...option.ClientOption) (*GroupClient, error) {
   255  	clientOpts := defaultGroupGRPCClientOptions()
   256  	if newGroupClientHook != nil {
   257  		hookOpts, err := newGroupClientHook(ctx, clientHookParams{})
   258  		if err != nil {
   259  			return nil, err
   260  		}
   261  		clientOpts = append(clientOpts, hookOpts...)
   262  	}
   263  
   264  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   265  	if err != nil {
   266  		return nil, err
   267  	}
   268  	client := GroupClient{CallOptions: defaultGroupCallOptions()}
   269  
   270  	c := &groupGRPCClient{
   271  		connPool:    connPool,
   272  		groupClient: monitoringpb.NewGroupServiceClient(connPool),
   273  		CallOptions: &client.CallOptions,
   274  	}
   275  	c.setGoogleClientInfo()
   276  
   277  	client.internalClient = c
   278  
   279  	return &client, nil
   280  }
   281  
   282  // Connection returns a connection to the API service.
   283  //
   284  // Deprecated: Connections are now pooled so this method does not always
   285  // return the same resource.
   286  func (c *groupGRPCClient) Connection() *grpc.ClientConn {
   287  	return c.connPool.Conn()
   288  }
   289  
   290  // setGoogleClientInfo sets the name and version of the application in
   291  // the `x-goog-api-client` header passed on each request. Intended for
   292  // use by Google-written clients.
   293  func (c *groupGRPCClient) setGoogleClientInfo(keyval ...string) {
   294  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   295  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
   296  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   297  }
   298  
   299  // Close closes the connection to the API service. The user should invoke this when
   300  // the client is no longer required.
   301  func (c *groupGRPCClient) Close() error {
   302  	return c.connPool.Close()
   303  }
   304  
   305  func (c *groupGRPCClient) ListGroups(ctx context.Context, req *monitoringpb.ListGroupsRequest, opts ...gax.CallOption) *GroupIterator {
   306  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   307  
   308  	hds = append(c.xGoogHeaders, hds...)
   309  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   310  	opts = append((*c.CallOptions).ListGroups[0:len((*c.CallOptions).ListGroups):len((*c.CallOptions).ListGroups)], opts...)
   311  	it := &GroupIterator{}
   312  	req = proto.Clone(req).(*monitoringpb.ListGroupsRequest)
   313  	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.Group, string, error) {
   314  		resp := &monitoringpb.ListGroupsResponse{}
   315  		if pageToken != "" {
   316  			req.PageToken = pageToken
   317  		}
   318  		if pageSize > math.MaxInt32 {
   319  			req.PageSize = math.MaxInt32
   320  		} else if pageSize != 0 {
   321  			req.PageSize = int32(pageSize)
   322  		}
   323  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   324  			var err error
   325  			resp, err = c.groupClient.ListGroups(ctx, req, settings.GRPC...)
   326  			return err
   327  		}, opts...)
   328  		if err != nil {
   329  			return nil, "", err
   330  		}
   331  
   332  		it.Response = resp
   333  		return resp.GetGroup(), resp.GetNextPageToken(), nil
   334  	}
   335  	fetch := func(pageSize int, pageToken string) (string, error) {
   336  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   337  		if err != nil {
   338  			return "", err
   339  		}
   340  		it.items = append(it.items, items...)
   341  		return nextPageToken, nil
   342  	}
   343  
   344  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   345  	it.pageInfo.MaxSize = int(req.GetPageSize())
   346  	it.pageInfo.Token = req.GetPageToken()
   347  
   348  	return it
   349  }
   350  
   351  func (c *groupGRPCClient) GetGroup(ctx context.Context, req *monitoringpb.GetGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
   352  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   353  
   354  	hds = append(c.xGoogHeaders, hds...)
   355  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   356  	opts = append((*c.CallOptions).GetGroup[0:len((*c.CallOptions).GetGroup):len((*c.CallOptions).GetGroup)], opts...)
   357  	var resp *monitoringpb.Group
   358  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   359  		var err error
   360  		resp, err = c.groupClient.GetGroup(ctx, req, settings.GRPC...)
   361  		return err
   362  	}, opts...)
   363  	if err != nil {
   364  		return nil, err
   365  	}
   366  	return resp, nil
   367  }
   368  
   369  func (c *groupGRPCClient) CreateGroup(ctx context.Context, req *monitoringpb.CreateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
   370  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   371  
   372  	hds = append(c.xGoogHeaders, hds...)
   373  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   374  	opts = append((*c.CallOptions).CreateGroup[0:len((*c.CallOptions).CreateGroup):len((*c.CallOptions).CreateGroup)], opts...)
   375  	var resp *monitoringpb.Group
   376  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   377  		var err error
   378  		resp, err = c.groupClient.CreateGroup(ctx, req, settings.GRPC...)
   379  		return err
   380  	}, opts...)
   381  	if err != nil {
   382  		return nil, err
   383  	}
   384  	return resp, nil
   385  }
   386  
   387  func (c *groupGRPCClient) UpdateGroup(ctx context.Context, req *monitoringpb.UpdateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
   388  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "group.name", url.QueryEscape(req.GetGroup().GetName()))}
   389  
   390  	hds = append(c.xGoogHeaders, hds...)
   391  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   392  	opts = append((*c.CallOptions).UpdateGroup[0:len((*c.CallOptions).UpdateGroup):len((*c.CallOptions).UpdateGroup)], opts...)
   393  	var resp *monitoringpb.Group
   394  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   395  		var err error
   396  		resp, err = c.groupClient.UpdateGroup(ctx, req, settings.GRPC...)
   397  		return err
   398  	}, opts...)
   399  	if err != nil {
   400  		return nil, err
   401  	}
   402  	return resp, nil
   403  }
   404  
   405  func (c *groupGRPCClient) DeleteGroup(ctx context.Context, req *monitoringpb.DeleteGroupRequest, opts ...gax.CallOption) error {
   406  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   407  
   408  	hds = append(c.xGoogHeaders, hds...)
   409  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   410  	opts = append((*c.CallOptions).DeleteGroup[0:len((*c.CallOptions).DeleteGroup):len((*c.CallOptions).DeleteGroup)], opts...)
   411  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   412  		var err error
   413  		_, err = c.groupClient.DeleteGroup(ctx, req, settings.GRPC...)
   414  		return err
   415  	}, opts...)
   416  	return err
   417  }
   418  
   419  func (c *groupGRPCClient) ListGroupMembers(ctx context.Context, req *monitoringpb.ListGroupMembersRequest, opts ...gax.CallOption) *MonitoredResourceIterator {
   420  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   421  
   422  	hds = append(c.xGoogHeaders, hds...)
   423  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   424  	opts = append((*c.CallOptions).ListGroupMembers[0:len((*c.CallOptions).ListGroupMembers):len((*c.CallOptions).ListGroupMembers)], opts...)
   425  	it := &MonitoredResourceIterator{}
   426  	req = proto.Clone(req).(*monitoringpb.ListGroupMembersRequest)
   427  	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoredrespb.MonitoredResource, string, error) {
   428  		resp := &monitoringpb.ListGroupMembersResponse{}
   429  		if pageToken != "" {
   430  			req.PageToken = pageToken
   431  		}
   432  		if pageSize > math.MaxInt32 {
   433  			req.PageSize = math.MaxInt32
   434  		} else if pageSize != 0 {
   435  			req.PageSize = int32(pageSize)
   436  		}
   437  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   438  			var err error
   439  			resp, err = c.groupClient.ListGroupMembers(ctx, req, settings.GRPC...)
   440  			return err
   441  		}, opts...)
   442  		if err != nil {
   443  			return nil, "", err
   444  		}
   445  
   446  		it.Response = resp
   447  		return resp.GetMembers(), resp.GetNextPageToken(), nil
   448  	}
   449  	fetch := func(pageSize int, pageToken string) (string, error) {
   450  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   451  		if err != nil {
   452  			return "", err
   453  		}
   454  		it.items = append(it.items, items...)
   455  		return nextPageToken, nil
   456  	}
   457  
   458  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   459  	it.pageInfo.MaxSize = int(req.GetPageSize())
   460  	it.pageInfo.Token = req.GetPageToken()
   461  
   462  	return it
   463  }
   464  

View as plain text