...

Source file src/cloud.google.com/go/monitoring/dashboard/apiv1/dashboards_client.go

Documentation: cloud.google.com/go/monitoring/dashboard/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 dashboard
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"fmt"
    23  	"io"
    24  	"math"
    25  	"net/http"
    26  	"net/url"
    27  	"time"
    28  
    29  	dashboardpb "cloud.google.com/go/monitoring/dashboard/apiv1/dashboardpb"
    30  	gax "github.com/googleapis/gax-go/v2"
    31  	"google.golang.org/api/googleapi"
    32  	"google.golang.org/api/iterator"
    33  	"google.golang.org/api/option"
    34  	"google.golang.org/api/option/internaloption"
    35  	gtransport "google.golang.org/api/transport/grpc"
    36  	httptransport "google.golang.org/api/transport/http"
    37  	"google.golang.org/grpc"
    38  	"google.golang.org/grpc/codes"
    39  	"google.golang.org/protobuf/encoding/protojson"
    40  	"google.golang.org/protobuf/proto"
    41  )
    42  
    43  var newDashboardsClientHook clientHook
    44  
    45  // DashboardsCallOptions contains the retry settings for each method of DashboardsClient.
    46  type DashboardsCallOptions struct {
    47  	CreateDashboard []gax.CallOption
    48  	ListDashboards  []gax.CallOption
    49  	GetDashboard    []gax.CallOption
    50  	DeleteDashboard []gax.CallOption
    51  	UpdateDashboard []gax.CallOption
    52  }
    53  
    54  func defaultDashboardsGRPCClientOptions() []option.ClientOption {
    55  	return []option.ClientOption{
    56  		internaloption.WithDefaultEndpoint("monitoring.googleapis.com:443"),
    57  		internaloption.WithDefaultEndpointTemplate("monitoring.UNIVERSE_DOMAIN:443"),
    58  		internaloption.WithDefaultMTLSEndpoint("monitoring.mtls.googleapis.com:443"),
    59  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
    60  		internaloption.WithDefaultAudience("https://monitoring.googleapis.com/"),
    61  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
    62  		internaloption.EnableJwtWithScope(),
    63  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    64  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    65  	}
    66  }
    67  
    68  func defaultDashboardsCallOptions() *DashboardsCallOptions {
    69  	return &DashboardsCallOptions{
    70  		CreateDashboard: []gax.CallOption{
    71  			gax.WithTimeout(30000 * time.Millisecond),
    72  		},
    73  		ListDashboards: []gax.CallOption{
    74  			gax.WithTimeout(30000 * time.Millisecond),
    75  			gax.WithRetry(func() gax.Retryer {
    76  				return gax.OnCodes([]codes.Code{
    77  					codes.Unavailable,
    78  					codes.Unknown,
    79  				}, gax.Backoff{
    80  					Initial:    1000 * time.Millisecond,
    81  					Max:        10000 * time.Millisecond,
    82  					Multiplier: 1.30,
    83  				})
    84  			}),
    85  		},
    86  		GetDashboard: []gax.CallOption{
    87  			gax.WithTimeout(30000 * time.Millisecond),
    88  			gax.WithRetry(func() gax.Retryer {
    89  				return gax.OnCodes([]codes.Code{
    90  					codes.Unavailable,
    91  					codes.Unknown,
    92  				}, gax.Backoff{
    93  					Initial:    1000 * time.Millisecond,
    94  					Max:        10000 * time.Millisecond,
    95  					Multiplier: 1.30,
    96  				})
    97  			}),
    98  		},
    99  		DeleteDashboard: []gax.CallOption{
   100  			gax.WithTimeout(30000 * time.Millisecond),
   101  		},
   102  		UpdateDashboard: []gax.CallOption{
   103  			gax.WithTimeout(30000 * time.Millisecond),
   104  		},
   105  	}
   106  }
   107  
   108  func defaultDashboardsRESTCallOptions() *DashboardsCallOptions {
   109  	return &DashboardsCallOptions{
   110  		CreateDashboard: []gax.CallOption{
   111  			gax.WithTimeout(30000 * time.Millisecond),
   112  		},
   113  		ListDashboards: []gax.CallOption{
   114  			gax.WithTimeout(30000 * time.Millisecond),
   115  			gax.WithRetry(func() gax.Retryer {
   116  				return gax.OnHTTPCodes(gax.Backoff{
   117  					Initial:    1000 * time.Millisecond,
   118  					Max:        10000 * time.Millisecond,
   119  					Multiplier: 1.30,
   120  				},
   121  					http.StatusServiceUnavailable,
   122  					http.StatusInternalServerError)
   123  			}),
   124  		},
   125  		GetDashboard: []gax.CallOption{
   126  			gax.WithTimeout(30000 * time.Millisecond),
   127  			gax.WithRetry(func() gax.Retryer {
   128  				return gax.OnHTTPCodes(gax.Backoff{
   129  					Initial:    1000 * time.Millisecond,
   130  					Max:        10000 * time.Millisecond,
   131  					Multiplier: 1.30,
   132  				},
   133  					http.StatusServiceUnavailable,
   134  					http.StatusInternalServerError)
   135  			}),
   136  		},
   137  		DeleteDashboard: []gax.CallOption{
   138  			gax.WithTimeout(30000 * time.Millisecond),
   139  		},
   140  		UpdateDashboard: []gax.CallOption{
   141  			gax.WithTimeout(30000 * time.Millisecond),
   142  		},
   143  	}
   144  }
   145  
   146  // internalDashboardsClient is an interface that defines the methods available from Cloud Monitoring API.
   147  type internalDashboardsClient interface {
   148  	Close() error
   149  	setGoogleClientInfo(...string)
   150  	Connection() *grpc.ClientConn
   151  	CreateDashboard(context.Context, *dashboardpb.CreateDashboardRequest, ...gax.CallOption) (*dashboardpb.Dashboard, error)
   152  	ListDashboards(context.Context, *dashboardpb.ListDashboardsRequest, ...gax.CallOption) *DashboardIterator
   153  	GetDashboard(context.Context, *dashboardpb.GetDashboardRequest, ...gax.CallOption) (*dashboardpb.Dashboard, error)
   154  	DeleteDashboard(context.Context, *dashboardpb.DeleteDashboardRequest, ...gax.CallOption) error
   155  	UpdateDashboard(context.Context, *dashboardpb.UpdateDashboardRequest, ...gax.CallOption) (*dashboardpb.Dashboard, error)
   156  }
   157  
   158  // DashboardsClient is a client for interacting with Cloud Monitoring API.
   159  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   160  //
   161  // Manages Stackdriver dashboards. A dashboard is an arrangement of data display
   162  // widgets in a specific layout.
   163  type DashboardsClient struct {
   164  	// The internal transport-dependent client.
   165  	internalClient internalDashboardsClient
   166  
   167  	// The call options for this service.
   168  	CallOptions *DashboardsCallOptions
   169  }
   170  
   171  // Wrapper methods routed to the internal client.
   172  
   173  // Close closes the connection to the API service. The user should invoke this when
   174  // the client is no longer required.
   175  func (c *DashboardsClient) Close() error {
   176  	return c.internalClient.Close()
   177  }
   178  
   179  // setGoogleClientInfo sets the name and version of the application in
   180  // the `x-goog-api-client` header passed on each request. Intended for
   181  // use by Google-written clients.
   182  func (c *DashboardsClient) setGoogleClientInfo(keyval ...string) {
   183  	c.internalClient.setGoogleClientInfo(keyval...)
   184  }
   185  
   186  // Connection returns a connection to the API service.
   187  //
   188  // Deprecated: Connections are now pooled so this method does not always
   189  // return the same resource.
   190  func (c *DashboardsClient) Connection() *grpc.ClientConn {
   191  	return c.internalClient.Connection()
   192  }
   193  
   194  // CreateDashboard creates a new custom dashboard. For examples on how you can use this API to
   195  // create dashboards, see Managing dashboards by
   196  // API (at https://cloud.google.com/monitoring/dashboards/api-dashboard). This
   197  // method requires the monitoring.dashboards.create permission on the
   198  // specified project. For more information about permissions, see Cloud
   199  // Identity and Access Management (at https://cloud.google.com/iam).
   200  func (c *DashboardsClient) CreateDashboard(ctx context.Context, req *dashboardpb.CreateDashboardRequest, opts ...gax.CallOption) (*dashboardpb.Dashboard, error) {
   201  	return c.internalClient.CreateDashboard(ctx, req, opts...)
   202  }
   203  
   204  // ListDashboards lists the existing dashboards.
   205  //
   206  // This method requires the monitoring.dashboards.list permission
   207  // on the specified project. For more information, see
   208  // Cloud Identity and Access Management (at https://cloud.google.com/iam).
   209  func (c *DashboardsClient) ListDashboards(ctx context.Context, req *dashboardpb.ListDashboardsRequest, opts ...gax.CallOption) *DashboardIterator {
   210  	return c.internalClient.ListDashboards(ctx, req, opts...)
   211  }
   212  
   213  // GetDashboard fetches a specific dashboard.
   214  //
   215  // This method requires the monitoring.dashboards.get permission
   216  // on the specified dashboard. For more information, see
   217  // Cloud Identity and Access Management (at https://cloud.google.com/iam).
   218  func (c *DashboardsClient) GetDashboard(ctx context.Context, req *dashboardpb.GetDashboardRequest, opts ...gax.CallOption) (*dashboardpb.Dashboard, error) {
   219  	return c.internalClient.GetDashboard(ctx, req, opts...)
   220  }
   221  
   222  // DeleteDashboard deletes an existing custom dashboard.
   223  //
   224  // This method requires the monitoring.dashboards.delete permission
   225  // on the specified dashboard. For more information, see
   226  // Cloud Identity and Access Management (at https://cloud.google.com/iam).
   227  func (c *DashboardsClient) DeleteDashboard(ctx context.Context, req *dashboardpb.DeleteDashboardRequest, opts ...gax.CallOption) error {
   228  	return c.internalClient.DeleteDashboard(ctx, req, opts...)
   229  }
   230  
   231  // UpdateDashboard replaces an existing custom dashboard with a new definition.
   232  //
   233  // This method requires the monitoring.dashboards.update permission
   234  // on the specified dashboard. For more information, see
   235  // Cloud Identity and Access Management (at https://cloud.google.com/iam).
   236  func (c *DashboardsClient) UpdateDashboard(ctx context.Context, req *dashboardpb.UpdateDashboardRequest, opts ...gax.CallOption) (*dashboardpb.Dashboard, error) {
   237  	return c.internalClient.UpdateDashboard(ctx, req, opts...)
   238  }
   239  
   240  // dashboardsGRPCClient is a client for interacting with Cloud Monitoring API over gRPC transport.
   241  //
   242  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   243  type dashboardsGRPCClient struct {
   244  	// Connection pool of gRPC connections to the service.
   245  	connPool gtransport.ConnPool
   246  
   247  	// Points back to the CallOptions field of the containing DashboardsClient
   248  	CallOptions **DashboardsCallOptions
   249  
   250  	// The gRPC API client.
   251  	dashboardsClient dashboardpb.DashboardsServiceClient
   252  
   253  	// The x-goog-* metadata to be sent with each request.
   254  	xGoogHeaders []string
   255  }
   256  
   257  // NewDashboardsClient creates a new dashboards service client based on gRPC.
   258  // The returned client must be Closed when it is done being used to clean up its underlying connections.
   259  //
   260  // Manages Stackdriver dashboards. A dashboard is an arrangement of data display
   261  // widgets in a specific layout.
   262  func NewDashboardsClient(ctx context.Context, opts ...option.ClientOption) (*DashboardsClient, error) {
   263  	clientOpts := defaultDashboardsGRPCClientOptions()
   264  	if newDashboardsClientHook != nil {
   265  		hookOpts, err := newDashboardsClientHook(ctx, clientHookParams{})
   266  		if err != nil {
   267  			return nil, err
   268  		}
   269  		clientOpts = append(clientOpts, hookOpts...)
   270  	}
   271  
   272  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   273  	if err != nil {
   274  		return nil, err
   275  	}
   276  	client := DashboardsClient{CallOptions: defaultDashboardsCallOptions()}
   277  
   278  	c := &dashboardsGRPCClient{
   279  		connPool:         connPool,
   280  		dashboardsClient: dashboardpb.NewDashboardsServiceClient(connPool),
   281  		CallOptions:      &client.CallOptions,
   282  	}
   283  	c.setGoogleClientInfo()
   284  
   285  	client.internalClient = c
   286  
   287  	return &client, nil
   288  }
   289  
   290  // Connection returns a connection to the API service.
   291  //
   292  // Deprecated: Connections are now pooled so this method does not always
   293  // return the same resource.
   294  func (c *dashboardsGRPCClient) Connection() *grpc.ClientConn {
   295  	return c.connPool.Conn()
   296  }
   297  
   298  // setGoogleClientInfo sets the name and version of the application in
   299  // the `x-goog-api-client` header passed on each request. Intended for
   300  // use by Google-written clients.
   301  func (c *dashboardsGRPCClient) setGoogleClientInfo(keyval ...string) {
   302  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   303  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
   304  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   305  }
   306  
   307  // Close closes the connection to the API service. The user should invoke this when
   308  // the client is no longer required.
   309  func (c *dashboardsGRPCClient) Close() error {
   310  	return c.connPool.Close()
   311  }
   312  
   313  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   314  type dashboardsRESTClient struct {
   315  	// The http endpoint to connect to.
   316  	endpoint string
   317  
   318  	// The http client.
   319  	httpClient *http.Client
   320  
   321  	// The x-goog-* headers to be sent with each request.
   322  	xGoogHeaders []string
   323  
   324  	// Points back to the CallOptions field of the containing DashboardsClient
   325  	CallOptions **DashboardsCallOptions
   326  }
   327  
   328  // NewDashboardsRESTClient creates a new dashboards service rest client.
   329  //
   330  // Manages Stackdriver dashboards. A dashboard is an arrangement of data display
   331  // widgets in a specific layout.
   332  func NewDashboardsRESTClient(ctx context.Context, opts ...option.ClientOption) (*DashboardsClient, error) {
   333  	clientOpts := append(defaultDashboardsRESTClientOptions(), opts...)
   334  	httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
   335  	if err != nil {
   336  		return nil, err
   337  	}
   338  
   339  	callOpts := defaultDashboardsRESTCallOptions()
   340  	c := &dashboardsRESTClient{
   341  		endpoint:    endpoint,
   342  		httpClient:  httpClient,
   343  		CallOptions: &callOpts,
   344  	}
   345  	c.setGoogleClientInfo()
   346  
   347  	return &DashboardsClient{internalClient: c, CallOptions: callOpts}, nil
   348  }
   349  
   350  func defaultDashboardsRESTClientOptions() []option.ClientOption {
   351  	return []option.ClientOption{
   352  		internaloption.WithDefaultEndpoint("https://monitoring.googleapis.com"),
   353  		internaloption.WithDefaultEndpointTemplate("https://monitoring.UNIVERSE_DOMAIN"),
   354  		internaloption.WithDefaultMTLSEndpoint("https://monitoring.mtls.googleapis.com"),
   355  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
   356  		internaloption.WithDefaultAudience("https://monitoring.googleapis.com/"),
   357  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
   358  	}
   359  }
   360  
   361  // setGoogleClientInfo sets the name and version of the application in
   362  // the `x-goog-api-client` header passed on each request. Intended for
   363  // use by Google-written clients.
   364  func (c *dashboardsRESTClient) setGoogleClientInfo(keyval ...string) {
   365  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   366  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN")
   367  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   368  }
   369  
   370  // Close closes the connection to the API service. The user should invoke this when
   371  // the client is no longer required.
   372  func (c *dashboardsRESTClient) Close() error {
   373  	// Replace httpClient with nil to force cleanup.
   374  	c.httpClient = nil
   375  	return nil
   376  }
   377  
   378  // Connection returns a connection to the API service.
   379  //
   380  // Deprecated: This method always returns nil.
   381  func (c *dashboardsRESTClient) Connection() *grpc.ClientConn {
   382  	return nil
   383  }
   384  func (c *dashboardsGRPCClient) CreateDashboard(ctx context.Context, req *dashboardpb.CreateDashboardRequest, opts ...gax.CallOption) (*dashboardpb.Dashboard, error) {
   385  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   386  
   387  	hds = append(c.xGoogHeaders, hds...)
   388  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   389  	opts = append((*c.CallOptions).CreateDashboard[0:len((*c.CallOptions).CreateDashboard):len((*c.CallOptions).CreateDashboard)], opts...)
   390  	var resp *dashboardpb.Dashboard
   391  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   392  		var err error
   393  		resp, err = c.dashboardsClient.CreateDashboard(ctx, req, settings.GRPC...)
   394  		return err
   395  	}, opts...)
   396  	if err != nil {
   397  		return nil, err
   398  	}
   399  	return resp, nil
   400  }
   401  
   402  func (c *dashboardsGRPCClient) ListDashboards(ctx context.Context, req *dashboardpb.ListDashboardsRequest, opts ...gax.CallOption) *DashboardIterator {
   403  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   404  
   405  	hds = append(c.xGoogHeaders, hds...)
   406  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   407  	opts = append((*c.CallOptions).ListDashboards[0:len((*c.CallOptions).ListDashboards):len((*c.CallOptions).ListDashboards)], opts...)
   408  	it := &DashboardIterator{}
   409  	req = proto.Clone(req).(*dashboardpb.ListDashboardsRequest)
   410  	it.InternalFetch = func(pageSize int, pageToken string) ([]*dashboardpb.Dashboard, string, error) {
   411  		resp := &dashboardpb.ListDashboardsResponse{}
   412  		if pageToken != "" {
   413  			req.PageToken = pageToken
   414  		}
   415  		if pageSize > math.MaxInt32 {
   416  			req.PageSize = math.MaxInt32
   417  		} else if pageSize != 0 {
   418  			req.PageSize = int32(pageSize)
   419  		}
   420  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   421  			var err error
   422  			resp, err = c.dashboardsClient.ListDashboards(ctx, req, settings.GRPC...)
   423  			return err
   424  		}, opts...)
   425  		if err != nil {
   426  			return nil, "", err
   427  		}
   428  
   429  		it.Response = resp
   430  		return resp.GetDashboards(), resp.GetNextPageToken(), nil
   431  	}
   432  	fetch := func(pageSize int, pageToken string) (string, error) {
   433  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   434  		if err != nil {
   435  			return "", err
   436  		}
   437  		it.items = append(it.items, items...)
   438  		return nextPageToken, nil
   439  	}
   440  
   441  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   442  	it.pageInfo.MaxSize = int(req.GetPageSize())
   443  	it.pageInfo.Token = req.GetPageToken()
   444  
   445  	return it
   446  }
   447  
   448  func (c *dashboardsGRPCClient) GetDashboard(ctx context.Context, req *dashboardpb.GetDashboardRequest, opts ...gax.CallOption) (*dashboardpb.Dashboard, error) {
   449  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   450  
   451  	hds = append(c.xGoogHeaders, hds...)
   452  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   453  	opts = append((*c.CallOptions).GetDashboard[0:len((*c.CallOptions).GetDashboard):len((*c.CallOptions).GetDashboard)], opts...)
   454  	var resp *dashboardpb.Dashboard
   455  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   456  		var err error
   457  		resp, err = c.dashboardsClient.GetDashboard(ctx, req, settings.GRPC...)
   458  		return err
   459  	}, opts...)
   460  	if err != nil {
   461  		return nil, err
   462  	}
   463  	return resp, nil
   464  }
   465  
   466  func (c *dashboardsGRPCClient) DeleteDashboard(ctx context.Context, req *dashboardpb.DeleteDashboardRequest, opts ...gax.CallOption) error {
   467  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   468  
   469  	hds = append(c.xGoogHeaders, hds...)
   470  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   471  	opts = append((*c.CallOptions).DeleteDashboard[0:len((*c.CallOptions).DeleteDashboard):len((*c.CallOptions).DeleteDashboard)], opts...)
   472  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   473  		var err error
   474  		_, err = c.dashboardsClient.DeleteDashboard(ctx, req, settings.GRPC...)
   475  		return err
   476  	}, opts...)
   477  	return err
   478  }
   479  
   480  func (c *dashboardsGRPCClient) UpdateDashboard(ctx context.Context, req *dashboardpb.UpdateDashboardRequest, opts ...gax.CallOption) (*dashboardpb.Dashboard, error) {
   481  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "dashboard.name", url.QueryEscape(req.GetDashboard().GetName()))}
   482  
   483  	hds = append(c.xGoogHeaders, hds...)
   484  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   485  	opts = append((*c.CallOptions).UpdateDashboard[0:len((*c.CallOptions).UpdateDashboard):len((*c.CallOptions).UpdateDashboard)], opts...)
   486  	var resp *dashboardpb.Dashboard
   487  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   488  		var err error
   489  		resp, err = c.dashboardsClient.UpdateDashboard(ctx, req, settings.GRPC...)
   490  		return err
   491  	}, opts...)
   492  	if err != nil {
   493  		return nil, err
   494  	}
   495  	return resp, nil
   496  }
   497  
   498  // CreateDashboard creates a new custom dashboard. For examples on how you can use this API to
   499  // create dashboards, see Managing dashboards by
   500  // API (at https://cloud.google.com/monitoring/dashboards/api-dashboard). This
   501  // method requires the monitoring.dashboards.create permission on the
   502  // specified project. For more information about permissions, see Cloud
   503  // Identity and Access Management (at https://cloud.google.com/iam).
   504  func (c *dashboardsRESTClient) CreateDashboard(ctx context.Context, req *dashboardpb.CreateDashboardRequest, opts ...gax.CallOption) (*dashboardpb.Dashboard, error) {
   505  	m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
   506  	body := req.GetDashboard()
   507  	jsonReq, err := m.Marshal(body)
   508  	if err != nil {
   509  		return nil, err
   510  	}
   511  
   512  	baseUrl, err := url.Parse(c.endpoint)
   513  	if err != nil {
   514  		return nil, err
   515  	}
   516  	baseUrl.Path += fmt.Sprintf("/v1/%v/dashboards", req.GetParent())
   517  
   518  	params := url.Values{}
   519  	params.Add("$alt", "json;enum-encoding=int")
   520  	if req.GetValidateOnly() {
   521  		params.Add("validateOnly", fmt.Sprintf("%v", req.GetValidateOnly()))
   522  	}
   523  
   524  	baseUrl.RawQuery = params.Encode()
   525  
   526  	// Build HTTP headers from client and context metadata.
   527  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   528  
   529  	hds = append(c.xGoogHeaders, hds...)
   530  	hds = append(hds, "Content-Type", "application/json")
   531  	headers := gax.BuildHeaders(ctx, hds...)
   532  	opts = append((*c.CallOptions).CreateDashboard[0:len((*c.CallOptions).CreateDashboard):len((*c.CallOptions).CreateDashboard)], opts...)
   533  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   534  	resp := &dashboardpb.Dashboard{}
   535  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   536  		if settings.Path != "" {
   537  			baseUrl.Path = settings.Path
   538  		}
   539  		httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
   540  		if err != nil {
   541  			return err
   542  		}
   543  		httpReq = httpReq.WithContext(ctx)
   544  		httpReq.Header = headers
   545  
   546  		httpRsp, err := c.httpClient.Do(httpReq)
   547  		if err != nil {
   548  			return err
   549  		}
   550  		defer httpRsp.Body.Close()
   551  
   552  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   553  			return err
   554  		}
   555  
   556  		buf, err := io.ReadAll(httpRsp.Body)
   557  		if err != nil {
   558  			return err
   559  		}
   560  
   561  		if err := unm.Unmarshal(buf, resp); err != nil {
   562  			return err
   563  		}
   564  
   565  		return nil
   566  	}, opts...)
   567  	if e != nil {
   568  		return nil, e
   569  	}
   570  	return resp, nil
   571  }
   572  
   573  // ListDashboards lists the existing dashboards.
   574  //
   575  // This method requires the monitoring.dashboards.list permission
   576  // on the specified project. For more information, see
   577  // Cloud Identity and Access Management (at https://cloud.google.com/iam).
   578  func (c *dashboardsRESTClient) ListDashboards(ctx context.Context, req *dashboardpb.ListDashboardsRequest, opts ...gax.CallOption) *DashboardIterator {
   579  	it := &DashboardIterator{}
   580  	req = proto.Clone(req).(*dashboardpb.ListDashboardsRequest)
   581  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   582  	it.InternalFetch = func(pageSize int, pageToken string) ([]*dashboardpb.Dashboard, string, error) {
   583  		resp := &dashboardpb.ListDashboardsResponse{}
   584  		if pageToken != "" {
   585  			req.PageToken = pageToken
   586  		}
   587  		if pageSize > math.MaxInt32 {
   588  			req.PageSize = math.MaxInt32
   589  		} else if pageSize != 0 {
   590  			req.PageSize = int32(pageSize)
   591  		}
   592  		baseUrl, err := url.Parse(c.endpoint)
   593  		if err != nil {
   594  			return nil, "", err
   595  		}
   596  		baseUrl.Path += fmt.Sprintf("/v1/%v/dashboards", req.GetParent())
   597  
   598  		params := url.Values{}
   599  		params.Add("$alt", "json;enum-encoding=int")
   600  		if req.GetPageSize() != 0 {
   601  			params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize()))
   602  		}
   603  		if req.GetPageToken() != "" {
   604  			params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken()))
   605  		}
   606  
   607  		baseUrl.RawQuery = params.Encode()
   608  
   609  		// Build HTTP headers from client and context metadata.
   610  		hds := append(c.xGoogHeaders, "Content-Type", "application/json")
   611  		headers := gax.BuildHeaders(ctx, hds...)
   612  		e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   613  			if settings.Path != "" {
   614  				baseUrl.Path = settings.Path
   615  			}
   616  			httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
   617  			if err != nil {
   618  				return err
   619  			}
   620  			httpReq.Header = headers
   621  
   622  			httpRsp, err := c.httpClient.Do(httpReq)
   623  			if err != nil {
   624  				return err
   625  			}
   626  			defer httpRsp.Body.Close()
   627  
   628  			if err = googleapi.CheckResponse(httpRsp); err != nil {
   629  				return err
   630  			}
   631  
   632  			buf, err := io.ReadAll(httpRsp.Body)
   633  			if err != nil {
   634  				return err
   635  			}
   636  
   637  			if err := unm.Unmarshal(buf, resp); err != nil {
   638  				return err
   639  			}
   640  
   641  			return nil
   642  		}, opts...)
   643  		if e != nil {
   644  			return nil, "", e
   645  		}
   646  		it.Response = resp
   647  		return resp.GetDashboards(), resp.GetNextPageToken(), nil
   648  	}
   649  
   650  	fetch := func(pageSize int, pageToken string) (string, error) {
   651  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   652  		if err != nil {
   653  			return "", err
   654  		}
   655  		it.items = append(it.items, items...)
   656  		return nextPageToken, nil
   657  	}
   658  
   659  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   660  	it.pageInfo.MaxSize = int(req.GetPageSize())
   661  	it.pageInfo.Token = req.GetPageToken()
   662  
   663  	return it
   664  }
   665  
   666  // GetDashboard fetches a specific dashboard.
   667  //
   668  // This method requires the monitoring.dashboards.get permission
   669  // on the specified dashboard. For more information, see
   670  // Cloud Identity and Access Management (at https://cloud.google.com/iam).
   671  func (c *dashboardsRESTClient) GetDashboard(ctx context.Context, req *dashboardpb.GetDashboardRequest, opts ...gax.CallOption) (*dashboardpb.Dashboard, error) {
   672  	baseUrl, err := url.Parse(c.endpoint)
   673  	if err != nil {
   674  		return nil, err
   675  	}
   676  	baseUrl.Path += fmt.Sprintf("/v1/%v", req.GetName())
   677  
   678  	params := url.Values{}
   679  	params.Add("$alt", "json;enum-encoding=int")
   680  
   681  	baseUrl.RawQuery = params.Encode()
   682  
   683  	// Build HTTP headers from client and context metadata.
   684  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   685  
   686  	hds = append(c.xGoogHeaders, hds...)
   687  	hds = append(hds, "Content-Type", "application/json")
   688  	headers := gax.BuildHeaders(ctx, hds...)
   689  	opts = append((*c.CallOptions).GetDashboard[0:len((*c.CallOptions).GetDashboard):len((*c.CallOptions).GetDashboard)], opts...)
   690  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   691  	resp := &dashboardpb.Dashboard{}
   692  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   693  		if settings.Path != "" {
   694  			baseUrl.Path = settings.Path
   695  		}
   696  		httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
   697  		if err != nil {
   698  			return err
   699  		}
   700  		httpReq = httpReq.WithContext(ctx)
   701  		httpReq.Header = headers
   702  
   703  		httpRsp, err := c.httpClient.Do(httpReq)
   704  		if err != nil {
   705  			return err
   706  		}
   707  		defer httpRsp.Body.Close()
   708  
   709  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   710  			return err
   711  		}
   712  
   713  		buf, err := io.ReadAll(httpRsp.Body)
   714  		if err != nil {
   715  			return err
   716  		}
   717  
   718  		if err := unm.Unmarshal(buf, resp); err != nil {
   719  			return err
   720  		}
   721  
   722  		return nil
   723  	}, opts...)
   724  	if e != nil {
   725  		return nil, e
   726  	}
   727  	return resp, nil
   728  }
   729  
   730  // DeleteDashboard deletes an existing custom dashboard.
   731  //
   732  // This method requires the monitoring.dashboards.delete permission
   733  // on the specified dashboard. For more information, see
   734  // Cloud Identity and Access Management (at https://cloud.google.com/iam).
   735  func (c *dashboardsRESTClient) DeleteDashboard(ctx context.Context, req *dashboardpb.DeleteDashboardRequest, opts ...gax.CallOption) error {
   736  	baseUrl, err := url.Parse(c.endpoint)
   737  	if err != nil {
   738  		return err
   739  	}
   740  	baseUrl.Path += fmt.Sprintf("/v1/%v", req.GetName())
   741  
   742  	params := url.Values{}
   743  	params.Add("$alt", "json;enum-encoding=int")
   744  
   745  	baseUrl.RawQuery = params.Encode()
   746  
   747  	// Build HTTP headers from client and context metadata.
   748  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   749  
   750  	hds = append(c.xGoogHeaders, hds...)
   751  	hds = append(hds, "Content-Type", "application/json")
   752  	headers := gax.BuildHeaders(ctx, hds...)
   753  	return gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   754  		if settings.Path != "" {
   755  			baseUrl.Path = settings.Path
   756  		}
   757  		httpReq, err := http.NewRequest("DELETE", baseUrl.String(), nil)
   758  		if err != nil {
   759  			return err
   760  		}
   761  		httpReq = httpReq.WithContext(ctx)
   762  		httpReq.Header = headers
   763  
   764  		httpRsp, err := c.httpClient.Do(httpReq)
   765  		if err != nil {
   766  			return err
   767  		}
   768  		defer httpRsp.Body.Close()
   769  
   770  		// Returns nil if there is no error, otherwise wraps
   771  		// the response code and body into a non-nil error
   772  		return googleapi.CheckResponse(httpRsp)
   773  	}, opts...)
   774  }
   775  
   776  // UpdateDashboard replaces an existing custom dashboard with a new definition.
   777  //
   778  // This method requires the monitoring.dashboards.update permission
   779  // on the specified dashboard. For more information, see
   780  // Cloud Identity and Access Management (at https://cloud.google.com/iam).
   781  func (c *dashboardsRESTClient) UpdateDashboard(ctx context.Context, req *dashboardpb.UpdateDashboardRequest, opts ...gax.CallOption) (*dashboardpb.Dashboard, error) {
   782  	m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
   783  	body := req.GetDashboard()
   784  	jsonReq, err := m.Marshal(body)
   785  	if err != nil {
   786  		return nil, err
   787  	}
   788  
   789  	baseUrl, err := url.Parse(c.endpoint)
   790  	if err != nil {
   791  		return nil, err
   792  	}
   793  	baseUrl.Path += fmt.Sprintf("/v1/%v", req.GetDashboard().GetName())
   794  
   795  	params := url.Values{}
   796  	params.Add("$alt", "json;enum-encoding=int")
   797  	if req.GetValidateOnly() {
   798  		params.Add("validateOnly", fmt.Sprintf("%v", req.GetValidateOnly()))
   799  	}
   800  
   801  	baseUrl.RawQuery = params.Encode()
   802  
   803  	// Build HTTP headers from client and context metadata.
   804  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "dashboard.name", url.QueryEscape(req.GetDashboard().GetName()))}
   805  
   806  	hds = append(c.xGoogHeaders, hds...)
   807  	hds = append(hds, "Content-Type", "application/json")
   808  	headers := gax.BuildHeaders(ctx, hds...)
   809  	opts = append((*c.CallOptions).UpdateDashboard[0:len((*c.CallOptions).UpdateDashboard):len((*c.CallOptions).UpdateDashboard)], opts...)
   810  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   811  	resp := &dashboardpb.Dashboard{}
   812  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   813  		if settings.Path != "" {
   814  			baseUrl.Path = settings.Path
   815  		}
   816  		httpReq, err := http.NewRequest("PATCH", baseUrl.String(), bytes.NewReader(jsonReq))
   817  		if err != nil {
   818  			return err
   819  		}
   820  		httpReq = httpReq.WithContext(ctx)
   821  		httpReq.Header = headers
   822  
   823  		httpRsp, err := c.httpClient.Do(httpReq)
   824  		if err != nil {
   825  			return err
   826  		}
   827  		defer httpRsp.Body.Close()
   828  
   829  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   830  			return err
   831  		}
   832  
   833  		buf, err := io.ReadAll(httpRsp.Body)
   834  		if err != nil {
   835  			return err
   836  		}
   837  
   838  		if err := unm.Unmarshal(buf, resp); err != nil {
   839  			return err
   840  		}
   841  
   842  		return nil
   843  	}, opts...)
   844  	if e != nil {
   845  		return nil, e
   846  	}
   847  	return resp, nil
   848  }
   849  

View as plain text