...

Source file src/cloud.google.com/go/monitoring/apiv3/group_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  	monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
    32  	"google.golang.org/grpc"
    33  	"google.golang.org/grpc/codes"
    34  	"google.golang.org/grpc/metadata"
    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 defaultGroupClientOptions() []option.ClientOption {
    51  	return []option.ClientOption{
    52  		option.WithEndpoint("monitoring.googleapis.com:443"),
    53  		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
    54  		option.WithScopes(DefaultAuthScopes()...),
    55  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    56  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    57  	}
    58  }
    59  
    60  func defaultGroupCallOptions() *GroupCallOptions {
    61  	return &GroupCallOptions{
    62  		ListGroups: []gax.CallOption{
    63  			gax.WithRetry(func() gax.Retryer {
    64  				return gax.OnCodes([]codes.Code{
    65  					codes.DeadlineExceeded,
    66  					codes.Unavailable,
    67  				}, gax.Backoff{
    68  					Initial:    100 * time.Millisecond,
    69  					Max:        30000 * time.Millisecond,
    70  					Multiplier: 1.30,
    71  				})
    72  			}),
    73  		},
    74  		GetGroup: []gax.CallOption{
    75  			gax.WithRetry(func() gax.Retryer {
    76  				return gax.OnCodes([]codes.Code{
    77  					codes.DeadlineExceeded,
    78  					codes.Unavailable,
    79  				}, gax.Backoff{
    80  					Initial:    100 * time.Millisecond,
    81  					Max:        30000 * time.Millisecond,
    82  					Multiplier: 1.30,
    83  				})
    84  			}),
    85  		},
    86  		CreateGroup: []gax.CallOption{},
    87  		UpdateGroup: []gax.CallOption{
    88  			gax.WithRetry(func() gax.Retryer {
    89  				return gax.OnCodes([]codes.Code{
    90  					codes.DeadlineExceeded,
    91  					codes.Unavailable,
    92  				}, gax.Backoff{
    93  					Initial:    100 * time.Millisecond,
    94  					Max:        30000 * time.Millisecond,
    95  					Multiplier: 1.30,
    96  				})
    97  			}),
    98  		},
    99  		DeleteGroup: []gax.CallOption{
   100  			gax.WithRetry(func() gax.Retryer {
   101  				return gax.OnCodes([]codes.Code{
   102  					codes.DeadlineExceeded,
   103  					codes.Unavailable,
   104  				}, gax.Backoff{
   105  					Initial:    100 * time.Millisecond,
   106  					Max:        30000 * time.Millisecond,
   107  					Multiplier: 1.30,
   108  				})
   109  			}),
   110  		},
   111  		ListGroupMembers: []gax.CallOption{
   112  			gax.WithRetry(func() gax.Retryer {
   113  				return gax.OnCodes([]codes.Code{
   114  					codes.DeadlineExceeded,
   115  					codes.Unavailable,
   116  				}, gax.Backoff{
   117  					Initial:    100 * time.Millisecond,
   118  					Max:        30000 * time.Millisecond,
   119  					Multiplier: 1.30,
   120  				})
   121  			}),
   122  		},
   123  	}
   124  }
   125  
   126  // GroupClient is a client for interacting with Cloud Monitoring API.
   127  //
   128  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   129  type GroupClient struct {
   130  	// Connection pool of gRPC connections to the service.
   131  	connPool gtransport.ConnPool
   132  
   133  	// The gRPC API client.
   134  	groupClient monitoringpb.GroupServiceClient
   135  
   136  	// The call options for this service.
   137  	CallOptions *GroupCallOptions
   138  
   139  	// The x-goog-* metadata to be sent with each request.
   140  	xGoogMetadata metadata.MD
   141  }
   142  
   143  // NewGroupClient creates a new group service client.
   144  //
   145  // The Group API lets you inspect and manage your
   146  // groups (at #google.monitoring.v3.Group).
   147  //
   148  // A group is a named filter that is used to identify
   149  // a collection of monitored resources. Groups are typically used to
   150  // mirror the physical and/or logical topology of the environment.
   151  // Because group membership is computed dynamically, monitored
   152  // resources that are started in the future are automatically placed
   153  // in matching groups. By using a group to name monitored resources in,
   154  // for example, an alert policy, the target of that alert policy is
   155  // updated automatically as monitored resources are added and removed
   156  // from the infrastructure.
   157  func NewGroupClient(ctx context.Context, opts ...option.ClientOption) (*GroupClient, error) {
   158  	clientOpts := defaultGroupClientOptions()
   159  
   160  	if newGroupClientHook != nil {
   161  		hookOpts, err := newGroupClientHook(ctx, clientHookParams{})
   162  		if err != nil {
   163  			return nil, err
   164  		}
   165  		clientOpts = append(clientOpts, hookOpts...)
   166  	}
   167  
   168  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   169  	if err != nil {
   170  		return nil, err
   171  	}
   172  	c := &GroupClient{
   173  		connPool:    connPool,
   174  		CallOptions: defaultGroupCallOptions(),
   175  
   176  		groupClient: monitoringpb.NewGroupServiceClient(connPool),
   177  	}
   178  	c.setGoogleClientInfo()
   179  
   180  	return c, nil
   181  }
   182  
   183  // Connection returns a connection to the API service.
   184  //
   185  // Deprecated.
   186  func (c *GroupClient) Connection() *grpc.ClientConn {
   187  	return c.connPool.Conn()
   188  }
   189  
   190  // Close closes the connection to the API service. The user should invoke this when
   191  // the client is no longer required.
   192  func (c *GroupClient) Close() error {
   193  	return c.connPool.Close()
   194  }
   195  
   196  // setGoogleClientInfo sets the name and version of the application in
   197  // the `x-goog-api-client` header passed on each request. Intended for
   198  // use by Google-written clients.
   199  func (c *GroupClient) setGoogleClientInfo(keyval ...string) {
   200  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   201  	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
   202  	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
   203  }
   204  
   205  // ListGroups lists the existing groups.
   206  func (c *GroupClient) ListGroups(ctx context.Context, req *monitoringpb.ListGroupsRequest, opts ...gax.CallOption) *GroupIterator {
   207  	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
   208  	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
   209  	opts = append(c.CallOptions.ListGroups[0:len(c.CallOptions.ListGroups):len(c.CallOptions.ListGroups)], opts...)
   210  	it := &GroupIterator{}
   211  	req = proto.Clone(req).(*monitoringpb.ListGroupsRequest)
   212  	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoringpb.Group, string, error) {
   213  		var resp *monitoringpb.ListGroupsResponse
   214  		req.PageToken = pageToken
   215  		if pageSize > math.MaxInt32 {
   216  			req.PageSize = math.MaxInt32
   217  		} else {
   218  			req.PageSize = int32(pageSize)
   219  		}
   220  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   221  			var err error
   222  			resp, err = c.groupClient.ListGroups(ctx, req, settings.GRPC...)
   223  			return err
   224  		}, opts...)
   225  		if err != nil {
   226  			return nil, "", err
   227  		}
   228  
   229  		it.Response = resp
   230  		return resp.Group, resp.NextPageToken, nil
   231  	}
   232  	fetch := func(pageSize int, pageToken string) (string, error) {
   233  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   234  		if err != nil {
   235  			return "", err
   236  		}
   237  		it.items = append(it.items, items...)
   238  		return nextPageToken, nil
   239  	}
   240  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   241  	it.pageInfo.MaxSize = int(req.PageSize)
   242  	it.pageInfo.Token = req.PageToken
   243  	return it
   244  }
   245  
   246  // GetGroup gets a single group.
   247  func (c *GroupClient) GetGroup(ctx context.Context, req *monitoringpb.GetGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
   248  	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
   249  	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
   250  	opts = append(c.CallOptions.GetGroup[0:len(c.CallOptions.GetGroup):len(c.CallOptions.GetGroup)], opts...)
   251  	var resp *monitoringpb.Group
   252  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   253  		var err error
   254  		resp, err = c.groupClient.GetGroup(ctx, req, settings.GRPC...)
   255  		return err
   256  	}, opts...)
   257  	if err != nil {
   258  		return nil, err
   259  	}
   260  	return resp, nil
   261  }
   262  
   263  // CreateGroup creates a new group.
   264  func (c *GroupClient) CreateGroup(ctx context.Context, req *monitoringpb.CreateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
   265  	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
   266  	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
   267  	opts = append(c.CallOptions.CreateGroup[0:len(c.CallOptions.CreateGroup):len(c.CallOptions.CreateGroup)], opts...)
   268  	var resp *monitoringpb.Group
   269  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   270  		var err error
   271  		resp, err = c.groupClient.CreateGroup(ctx, req, settings.GRPC...)
   272  		return err
   273  	}, opts...)
   274  	if err != nil {
   275  		return nil, err
   276  	}
   277  	return resp, nil
   278  }
   279  
   280  // UpdateGroup updates an existing group.
   281  // You can change any group attributes except name.
   282  func (c *GroupClient) UpdateGroup(ctx context.Context, req *monitoringpb.UpdateGroupRequest, opts ...gax.CallOption) (*monitoringpb.Group, error) {
   283  	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "group.name", url.QueryEscape(req.GetGroup().GetName())))
   284  	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
   285  	opts = append(c.CallOptions.UpdateGroup[0:len(c.CallOptions.UpdateGroup):len(c.CallOptions.UpdateGroup)], opts...)
   286  	var resp *monitoringpb.Group
   287  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   288  		var err error
   289  		resp, err = c.groupClient.UpdateGroup(ctx, req, settings.GRPC...)
   290  		return err
   291  	}, opts...)
   292  	if err != nil {
   293  		return nil, err
   294  	}
   295  	return resp, nil
   296  }
   297  
   298  // DeleteGroup deletes an existing group.
   299  func (c *GroupClient) DeleteGroup(ctx context.Context, req *monitoringpb.DeleteGroupRequest, opts ...gax.CallOption) error {
   300  	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
   301  	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
   302  	opts = append(c.CallOptions.DeleteGroup[0:len(c.CallOptions.DeleteGroup):len(c.CallOptions.DeleteGroup)], opts...)
   303  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   304  		var err error
   305  		_, err = c.groupClient.DeleteGroup(ctx, req, settings.GRPC...)
   306  		return err
   307  	}, opts...)
   308  	return err
   309  }
   310  
   311  // ListGroupMembers lists the monitored resources that are members of a group.
   312  func (c *GroupClient) ListGroupMembers(ctx context.Context, req *monitoringpb.ListGroupMembersRequest, opts ...gax.CallOption) *MonitoredResourceIterator {
   313  	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
   314  	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
   315  	opts = append(c.CallOptions.ListGroupMembers[0:len(c.CallOptions.ListGroupMembers):len(c.CallOptions.ListGroupMembers)], opts...)
   316  	it := &MonitoredResourceIterator{}
   317  	req = proto.Clone(req).(*monitoringpb.ListGroupMembersRequest)
   318  	it.InternalFetch = func(pageSize int, pageToken string) ([]*monitoredrespb.MonitoredResource, string, error) {
   319  		var resp *monitoringpb.ListGroupMembersResponse
   320  		req.PageToken = pageToken
   321  		if pageSize > math.MaxInt32 {
   322  			req.PageSize = math.MaxInt32
   323  		} else {
   324  			req.PageSize = int32(pageSize)
   325  		}
   326  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   327  			var err error
   328  			resp, err = c.groupClient.ListGroupMembers(ctx, req, settings.GRPC...)
   329  			return err
   330  		}, opts...)
   331  		if err != nil {
   332  			return nil, "", err
   333  		}
   334  
   335  		it.Response = resp
   336  		return resp.Members, resp.NextPageToken, nil
   337  	}
   338  	fetch := func(pageSize int, pageToken string) (string, error) {
   339  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   340  		if err != nil {
   341  			return "", err
   342  		}
   343  		it.items = append(it.items, items...)
   344  		return nextPageToken, nil
   345  	}
   346  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   347  	it.pageInfo.MaxSize = int(req.PageSize)
   348  	it.pageInfo.Token = req.PageToken
   349  	return it
   350  }
   351  
   352  // GroupIterator manages a stream of *monitoringpb.Group.
   353  type GroupIterator struct {
   354  	items    []*monitoringpb.Group
   355  	pageInfo *iterator.PageInfo
   356  	nextFunc func() error
   357  
   358  	// Response is the raw response for the current page.
   359  	// It must be cast to the RPC response type.
   360  	// Calling Next() or InternalFetch() updates this value.
   361  	Response interface{}
   362  
   363  	// InternalFetch is for use by the Google Cloud Libraries only.
   364  	// It is not part of the stable interface of this package.
   365  	//
   366  	// InternalFetch returns results from a single call to the underlying RPC.
   367  	// The number of results is no greater than pageSize.
   368  	// If there are no more results, nextPageToken is empty and err is nil.
   369  	InternalFetch func(pageSize int, pageToken string) (results []*monitoringpb.Group, nextPageToken string, err error)
   370  }
   371  
   372  // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
   373  func (it *GroupIterator) PageInfo() *iterator.PageInfo {
   374  	return it.pageInfo
   375  }
   376  
   377  // Next returns the next result. Its second return value is iterator.Done if there are no more
   378  // results. Once Next returns Done, all subsequent calls will return Done.
   379  func (it *GroupIterator) Next() (*monitoringpb.Group, error) {
   380  	var item *monitoringpb.Group
   381  	if err := it.nextFunc(); err != nil {
   382  		return item, err
   383  	}
   384  	item = it.items[0]
   385  	it.items = it.items[1:]
   386  	return item, nil
   387  }
   388  
   389  func (it *GroupIterator) bufLen() int {
   390  	return len(it.items)
   391  }
   392  
   393  func (it *GroupIterator) takeBuf() interface{} {
   394  	b := it.items
   395  	it.items = nil
   396  	return b
   397  }
   398  
   399  // MonitoredResourceIterator manages a stream of *monitoredrespb.MonitoredResource.
   400  type MonitoredResourceIterator struct {
   401  	items    []*monitoredrespb.MonitoredResource
   402  	pageInfo *iterator.PageInfo
   403  	nextFunc func() error
   404  
   405  	// Response is the raw response for the current page.
   406  	// It must be cast to the RPC response type.
   407  	// Calling Next() or InternalFetch() updates this value.
   408  	Response interface{}
   409  
   410  	// InternalFetch is for use by the Google Cloud Libraries only.
   411  	// It is not part of the stable interface of this package.
   412  	//
   413  	// InternalFetch returns results from a single call to the underlying RPC.
   414  	// The number of results is no greater than pageSize.
   415  	// If there are no more results, nextPageToken is empty and err is nil.
   416  	InternalFetch func(pageSize int, pageToken string) (results []*monitoredrespb.MonitoredResource, nextPageToken string, err error)
   417  }
   418  
   419  // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
   420  func (it *MonitoredResourceIterator) PageInfo() *iterator.PageInfo {
   421  	return it.pageInfo
   422  }
   423  
   424  // Next returns the next result. Its second return value is iterator.Done if there are no more
   425  // results. Once Next returns Done, all subsequent calls will return Done.
   426  func (it *MonitoredResourceIterator) Next() (*monitoredrespb.MonitoredResource, error) {
   427  	var item *monitoredrespb.MonitoredResource
   428  	if err := it.nextFunc(); err != nil {
   429  		return item, err
   430  	}
   431  	item = it.items[0]
   432  	it.items = it.items[1:]
   433  	return item, nil
   434  }
   435  
   436  func (it *MonitoredResourceIterator) bufLen() int {
   437  	return len(it.items)
   438  }
   439  
   440  func (it *MonitoredResourceIterator) takeBuf() interface{} {
   441  	b := it.items
   442  	it.items = nil
   443  	return b
   444  }
   445  

View as plain text