...

Source file src/cloud.google.com/go/monitoring/metricsscope/apiv1/metrics_scopes_client.go

Documentation: cloud.google.com/go/monitoring/metricsscope/apiv1

     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 metricsscope
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"math"
    23  	"net/url"
    24  
    25  	"cloud.google.com/go/longrunning"
    26  	lroauto "cloud.google.com/go/longrunning/autogen"
    27  	longrunningpb "cloud.google.com/go/longrunning/autogen/longrunningpb"
    28  	metricsscopepb "cloud.google.com/go/monitoring/metricsscope/apiv1/metricsscopepb"
    29  	gax "github.com/googleapis/gax-go/v2"
    30  	"google.golang.org/api/option"
    31  	"google.golang.org/api/option/internaloption"
    32  	gtransport "google.golang.org/api/transport/grpc"
    33  	"google.golang.org/grpc"
    34  )
    35  
    36  var newMetricsScopesClientHook clientHook
    37  
    38  // MetricsScopesCallOptions contains the retry settings for each method of MetricsScopesClient.
    39  type MetricsScopesCallOptions struct {
    40  	GetMetricsScope                     []gax.CallOption
    41  	ListMetricsScopesByMonitoredProject []gax.CallOption
    42  	CreateMonitoredProject              []gax.CallOption
    43  	DeleteMonitoredProject              []gax.CallOption
    44  }
    45  
    46  func defaultMetricsScopesGRPCClientOptions() []option.ClientOption {
    47  	return []option.ClientOption{
    48  		internaloption.WithDefaultEndpoint("monitoring.googleapis.com:443"),
    49  		internaloption.WithDefaultEndpointTemplate("monitoring.UNIVERSE_DOMAIN:443"),
    50  		internaloption.WithDefaultMTLSEndpoint("monitoring.mtls.googleapis.com:443"),
    51  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
    52  		internaloption.WithDefaultAudience("https://monitoring.googleapis.com/"),
    53  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
    54  		internaloption.EnableJwtWithScope(),
    55  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    56  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    57  	}
    58  }
    59  
    60  func defaultMetricsScopesCallOptions() *MetricsScopesCallOptions {
    61  	return &MetricsScopesCallOptions{
    62  		GetMetricsScope:                     []gax.CallOption{},
    63  		ListMetricsScopesByMonitoredProject: []gax.CallOption{},
    64  		CreateMonitoredProject:              []gax.CallOption{},
    65  		DeleteMonitoredProject:              []gax.CallOption{},
    66  	}
    67  }
    68  
    69  // internalMetricsScopesClient is an interface that defines the methods available from Cloud Monitoring API.
    70  type internalMetricsScopesClient interface {
    71  	Close() error
    72  	setGoogleClientInfo(...string)
    73  	Connection() *grpc.ClientConn
    74  	GetMetricsScope(context.Context, *metricsscopepb.GetMetricsScopeRequest, ...gax.CallOption) (*metricsscopepb.MetricsScope, error)
    75  	ListMetricsScopesByMonitoredProject(context.Context, *metricsscopepb.ListMetricsScopesByMonitoredProjectRequest, ...gax.CallOption) (*metricsscopepb.ListMetricsScopesByMonitoredProjectResponse, error)
    76  	CreateMonitoredProject(context.Context, *metricsscopepb.CreateMonitoredProjectRequest, ...gax.CallOption) (*CreateMonitoredProjectOperation, error)
    77  	CreateMonitoredProjectOperation(name string) *CreateMonitoredProjectOperation
    78  	DeleteMonitoredProject(context.Context, *metricsscopepb.DeleteMonitoredProjectRequest, ...gax.CallOption) (*DeleteMonitoredProjectOperation, error)
    79  	DeleteMonitoredProjectOperation(name string) *DeleteMonitoredProjectOperation
    80  }
    81  
    82  // MetricsScopesClient is a client for interacting with Cloud Monitoring API.
    83  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
    84  //
    85  // Manages Cloud Monitoring Metrics Scopes, and the monitoring of Google Cloud
    86  // projects and AWS accounts.
    87  type MetricsScopesClient struct {
    88  	// The internal transport-dependent client.
    89  	internalClient internalMetricsScopesClient
    90  
    91  	// The call options for this service.
    92  	CallOptions *MetricsScopesCallOptions
    93  
    94  	// LROClient is used internally to handle long-running operations.
    95  	// It is exposed so that its CallOptions can be modified if required.
    96  	// Users should not Close this client.
    97  	LROClient *lroauto.OperationsClient
    98  }
    99  
   100  // Wrapper methods routed to the internal client.
   101  
   102  // Close closes the connection to the API service. The user should invoke this when
   103  // the client is no longer required.
   104  func (c *MetricsScopesClient) Close() error {
   105  	return c.internalClient.Close()
   106  }
   107  
   108  // setGoogleClientInfo sets the name and version of the application in
   109  // the `x-goog-api-client` header passed on each request. Intended for
   110  // use by Google-written clients.
   111  func (c *MetricsScopesClient) setGoogleClientInfo(keyval ...string) {
   112  	c.internalClient.setGoogleClientInfo(keyval...)
   113  }
   114  
   115  // Connection returns a connection to the API service.
   116  //
   117  // Deprecated: Connections are now pooled so this method does not always
   118  // return the same resource.
   119  func (c *MetricsScopesClient) Connection() *grpc.ClientConn {
   120  	return c.internalClient.Connection()
   121  }
   122  
   123  // GetMetricsScope returns a specific Metrics Scope.
   124  func (c *MetricsScopesClient) GetMetricsScope(ctx context.Context, req *metricsscopepb.GetMetricsScopeRequest, opts ...gax.CallOption) (*metricsscopepb.MetricsScope, error) {
   125  	return c.internalClient.GetMetricsScope(ctx, req, opts...)
   126  }
   127  
   128  // ListMetricsScopesByMonitoredProject returns a list of every Metrics Scope that a specific MonitoredProject
   129  // has been added to. The metrics scope representing the specified monitored
   130  // project will always be the first entry in the response.
   131  func (c *MetricsScopesClient) ListMetricsScopesByMonitoredProject(ctx context.Context, req *metricsscopepb.ListMetricsScopesByMonitoredProjectRequest, opts ...gax.CallOption) (*metricsscopepb.ListMetricsScopesByMonitoredProjectResponse, error) {
   132  	return c.internalClient.ListMetricsScopesByMonitoredProject(ctx, req, opts...)
   133  }
   134  
   135  // CreateMonitoredProject adds a MonitoredProject with the given project ID
   136  // to the specified Metrics Scope.
   137  func (c *MetricsScopesClient) CreateMonitoredProject(ctx context.Context, req *metricsscopepb.CreateMonitoredProjectRequest, opts ...gax.CallOption) (*CreateMonitoredProjectOperation, error) {
   138  	return c.internalClient.CreateMonitoredProject(ctx, req, opts...)
   139  }
   140  
   141  // CreateMonitoredProjectOperation returns a new CreateMonitoredProjectOperation from a given name.
   142  // The name must be that of a previously created CreateMonitoredProjectOperation, possibly from a different process.
   143  func (c *MetricsScopesClient) CreateMonitoredProjectOperation(name string) *CreateMonitoredProjectOperation {
   144  	return c.internalClient.CreateMonitoredProjectOperation(name)
   145  }
   146  
   147  // DeleteMonitoredProject deletes a MonitoredProject from the specified Metrics Scope.
   148  func (c *MetricsScopesClient) DeleteMonitoredProject(ctx context.Context, req *metricsscopepb.DeleteMonitoredProjectRequest, opts ...gax.CallOption) (*DeleteMonitoredProjectOperation, error) {
   149  	return c.internalClient.DeleteMonitoredProject(ctx, req, opts...)
   150  }
   151  
   152  // DeleteMonitoredProjectOperation returns a new DeleteMonitoredProjectOperation from a given name.
   153  // The name must be that of a previously created DeleteMonitoredProjectOperation, possibly from a different process.
   154  func (c *MetricsScopesClient) DeleteMonitoredProjectOperation(name string) *DeleteMonitoredProjectOperation {
   155  	return c.internalClient.DeleteMonitoredProjectOperation(name)
   156  }
   157  
   158  // metricsScopesGRPCClient is a client for interacting with Cloud Monitoring API over gRPC transport.
   159  //
   160  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   161  type metricsScopesGRPCClient struct {
   162  	// Connection pool of gRPC connections to the service.
   163  	connPool gtransport.ConnPool
   164  
   165  	// Points back to the CallOptions field of the containing MetricsScopesClient
   166  	CallOptions **MetricsScopesCallOptions
   167  
   168  	// The gRPC API client.
   169  	metricsScopesClient metricsscopepb.MetricsScopesClient
   170  
   171  	// LROClient is used internally to handle long-running operations.
   172  	// It is exposed so that its CallOptions can be modified if required.
   173  	// Users should not Close this client.
   174  	LROClient **lroauto.OperationsClient
   175  
   176  	// The x-goog-* metadata to be sent with each request.
   177  	xGoogHeaders []string
   178  }
   179  
   180  // NewMetricsScopesClient creates a new metrics scopes client based on gRPC.
   181  // The returned client must be Closed when it is done being used to clean up its underlying connections.
   182  //
   183  // Manages Cloud Monitoring Metrics Scopes, and the monitoring of Google Cloud
   184  // projects and AWS accounts.
   185  func NewMetricsScopesClient(ctx context.Context, opts ...option.ClientOption) (*MetricsScopesClient, error) {
   186  	clientOpts := defaultMetricsScopesGRPCClientOptions()
   187  	if newMetricsScopesClientHook != nil {
   188  		hookOpts, err := newMetricsScopesClientHook(ctx, clientHookParams{})
   189  		if err != nil {
   190  			return nil, err
   191  		}
   192  		clientOpts = append(clientOpts, hookOpts...)
   193  	}
   194  
   195  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   196  	if err != nil {
   197  		return nil, err
   198  	}
   199  	client := MetricsScopesClient{CallOptions: defaultMetricsScopesCallOptions()}
   200  
   201  	c := &metricsScopesGRPCClient{
   202  		connPool:            connPool,
   203  		metricsScopesClient: metricsscopepb.NewMetricsScopesClient(connPool),
   204  		CallOptions:         &client.CallOptions,
   205  	}
   206  	c.setGoogleClientInfo()
   207  
   208  	client.internalClient = c
   209  
   210  	client.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool))
   211  	if err != nil {
   212  		// This error "should not happen", since we are just reusing old connection pool
   213  		// and never actually need to dial.
   214  		// If this does happen, we could leak connp. However, we cannot close conn:
   215  		// If the user invoked the constructor with option.WithGRPCConn,
   216  		// we would close a connection that's still in use.
   217  		// TODO: investigate error conditions.
   218  		return nil, err
   219  	}
   220  	c.LROClient = &client.LROClient
   221  	return &client, nil
   222  }
   223  
   224  // Connection returns a connection to the API service.
   225  //
   226  // Deprecated: Connections are now pooled so this method does not always
   227  // return the same resource.
   228  func (c *metricsScopesGRPCClient) Connection() *grpc.ClientConn {
   229  	return c.connPool.Conn()
   230  }
   231  
   232  // setGoogleClientInfo sets the name and version of the application in
   233  // the `x-goog-api-client` header passed on each request. Intended for
   234  // use by Google-written clients.
   235  func (c *metricsScopesGRPCClient) setGoogleClientInfo(keyval ...string) {
   236  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   237  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
   238  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   239  }
   240  
   241  // Close closes the connection to the API service. The user should invoke this when
   242  // the client is no longer required.
   243  func (c *metricsScopesGRPCClient) Close() error {
   244  	return c.connPool.Close()
   245  }
   246  
   247  func (c *metricsScopesGRPCClient) GetMetricsScope(ctx context.Context, req *metricsscopepb.GetMetricsScopeRequest, opts ...gax.CallOption) (*metricsscopepb.MetricsScope, error) {
   248  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   249  
   250  	hds = append(c.xGoogHeaders, hds...)
   251  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   252  	opts = append((*c.CallOptions).GetMetricsScope[0:len((*c.CallOptions).GetMetricsScope):len((*c.CallOptions).GetMetricsScope)], opts...)
   253  	var resp *metricsscopepb.MetricsScope
   254  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   255  		var err error
   256  		resp, err = c.metricsScopesClient.GetMetricsScope(ctx, req, settings.GRPC...)
   257  		return err
   258  	}, opts...)
   259  	if err != nil {
   260  		return nil, err
   261  	}
   262  	return resp, nil
   263  }
   264  
   265  func (c *metricsScopesGRPCClient) ListMetricsScopesByMonitoredProject(ctx context.Context, req *metricsscopepb.ListMetricsScopesByMonitoredProjectRequest, opts ...gax.CallOption) (*metricsscopepb.ListMetricsScopesByMonitoredProjectResponse, error) {
   266  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, c.xGoogHeaders...)
   267  	opts = append((*c.CallOptions).ListMetricsScopesByMonitoredProject[0:len((*c.CallOptions).ListMetricsScopesByMonitoredProject):len((*c.CallOptions).ListMetricsScopesByMonitoredProject)], opts...)
   268  	var resp *metricsscopepb.ListMetricsScopesByMonitoredProjectResponse
   269  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   270  		var err error
   271  		resp, err = c.metricsScopesClient.ListMetricsScopesByMonitoredProject(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  func (c *metricsScopesGRPCClient) CreateMonitoredProject(ctx context.Context, req *metricsscopepb.CreateMonitoredProjectRequest, opts ...gax.CallOption) (*CreateMonitoredProjectOperation, error) {
   281  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   282  
   283  	hds = append(c.xGoogHeaders, hds...)
   284  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   285  	opts = append((*c.CallOptions).CreateMonitoredProject[0:len((*c.CallOptions).CreateMonitoredProject):len((*c.CallOptions).CreateMonitoredProject)], opts...)
   286  	var resp *longrunningpb.Operation
   287  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   288  		var err error
   289  		resp, err = c.metricsScopesClient.CreateMonitoredProject(ctx, req, settings.GRPC...)
   290  		return err
   291  	}, opts...)
   292  	if err != nil {
   293  		return nil, err
   294  	}
   295  	return &CreateMonitoredProjectOperation{
   296  		lro: longrunning.InternalNewOperation(*c.LROClient, resp),
   297  	}, nil
   298  }
   299  
   300  func (c *metricsScopesGRPCClient) DeleteMonitoredProject(ctx context.Context, req *metricsscopepb.DeleteMonitoredProjectRequest, opts ...gax.CallOption) (*DeleteMonitoredProjectOperation, error) {
   301  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   302  
   303  	hds = append(c.xGoogHeaders, hds...)
   304  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   305  	opts = append((*c.CallOptions).DeleteMonitoredProject[0:len((*c.CallOptions).DeleteMonitoredProject):len((*c.CallOptions).DeleteMonitoredProject)], opts...)
   306  	var resp *longrunningpb.Operation
   307  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   308  		var err error
   309  		resp, err = c.metricsScopesClient.DeleteMonitoredProject(ctx, req, settings.GRPC...)
   310  		return err
   311  	}, opts...)
   312  	if err != nil {
   313  		return nil, err
   314  	}
   315  	return &DeleteMonitoredProjectOperation{
   316  		lro: longrunning.InternalNewOperation(*c.LROClient, resp),
   317  	}, nil
   318  }
   319  
   320  // CreateMonitoredProjectOperation returns a new CreateMonitoredProjectOperation from a given name.
   321  // The name must be that of a previously created CreateMonitoredProjectOperation, possibly from a different process.
   322  func (c *metricsScopesGRPCClient) CreateMonitoredProjectOperation(name string) *CreateMonitoredProjectOperation {
   323  	return &CreateMonitoredProjectOperation{
   324  		lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
   325  	}
   326  }
   327  
   328  // DeleteMonitoredProjectOperation returns a new DeleteMonitoredProjectOperation from a given name.
   329  // The name must be that of a previously created DeleteMonitoredProjectOperation, possibly from a different process.
   330  func (c *metricsScopesGRPCClient) DeleteMonitoredProjectOperation(name string) *DeleteMonitoredProjectOperation {
   331  	return &DeleteMonitoredProjectOperation{
   332  		lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
   333  	}
   334  }
   335  

View as plain text