...

Source file src/cloud.google.com/go/bigquery/connection/apiv1/connection_client.go

Documentation: cloud.google.com/go/bigquery/connection/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 connection
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"fmt"
    23  	"io"
    24  	"math"
    25  	"net/http"
    26  	"net/url"
    27  	"time"
    28  
    29  	connectionpb "cloud.google.com/go/bigquery/connection/apiv1/connectionpb"
    30  	iampb "cloud.google.com/go/iam/apiv1/iampb"
    31  	gax "github.com/googleapis/gax-go/v2"
    32  	"google.golang.org/api/googleapi"
    33  	"google.golang.org/api/iterator"
    34  	"google.golang.org/api/option"
    35  	"google.golang.org/api/option/internaloption"
    36  	gtransport "google.golang.org/api/transport/grpc"
    37  	httptransport "google.golang.org/api/transport/http"
    38  	"google.golang.org/grpc"
    39  	"google.golang.org/grpc/codes"
    40  	"google.golang.org/protobuf/encoding/protojson"
    41  	"google.golang.org/protobuf/proto"
    42  )
    43  
    44  var newClientHook clientHook
    45  
    46  // CallOptions contains the retry settings for each method of Client.
    47  type CallOptions struct {
    48  	CreateConnection   []gax.CallOption
    49  	GetConnection      []gax.CallOption
    50  	ListConnections    []gax.CallOption
    51  	UpdateConnection   []gax.CallOption
    52  	DeleteConnection   []gax.CallOption
    53  	GetIamPolicy       []gax.CallOption
    54  	SetIamPolicy       []gax.CallOption
    55  	TestIamPermissions []gax.CallOption
    56  }
    57  
    58  func defaultGRPCClientOptions() []option.ClientOption {
    59  	return []option.ClientOption{
    60  		internaloption.WithDefaultEndpoint("bigqueryconnection.googleapis.com:443"),
    61  		internaloption.WithDefaultEndpointTemplate("bigqueryconnection.UNIVERSE_DOMAIN:443"),
    62  		internaloption.WithDefaultMTLSEndpoint("bigqueryconnection.mtls.googleapis.com:443"),
    63  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
    64  		internaloption.WithDefaultAudience("https://bigqueryconnection.googleapis.com/"),
    65  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
    66  		internaloption.EnableJwtWithScope(),
    67  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    68  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    69  	}
    70  }
    71  
    72  func defaultCallOptions() *CallOptions {
    73  	return &CallOptions{
    74  		CreateConnection: []gax.CallOption{
    75  			gax.WithTimeout(60000 * time.Millisecond),
    76  		},
    77  		GetConnection: []gax.CallOption{
    78  			gax.WithTimeout(60000 * time.Millisecond),
    79  			gax.WithRetry(func() gax.Retryer {
    80  				return gax.OnCodes([]codes.Code{
    81  					codes.DeadlineExceeded,
    82  					codes.Unavailable,
    83  				}, gax.Backoff{
    84  					Initial:    100 * time.Millisecond,
    85  					Max:        60000 * time.Millisecond,
    86  					Multiplier: 1.30,
    87  				})
    88  			}),
    89  		},
    90  		ListConnections: []gax.CallOption{
    91  			gax.WithTimeout(60000 * time.Millisecond),
    92  			gax.WithRetry(func() gax.Retryer {
    93  				return gax.OnCodes([]codes.Code{
    94  					codes.DeadlineExceeded,
    95  					codes.Unavailable,
    96  				}, gax.Backoff{
    97  					Initial:    100 * time.Millisecond,
    98  					Max:        60000 * time.Millisecond,
    99  					Multiplier: 1.30,
   100  				})
   101  			}),
   102  		},
   103  		UpdateConnection: []gax.CallOption{
   104  			gax.WithTimeout(60000 * time.Millisecond),
   105  		},
   106  		DeleteConnection: []gax.CallOption{
   107  			gax.WithTimeout(60000 * time.Millisecond),
   108  			gax.WithRetry(func() gax.Retryer {
   109  				return gax.OnCodes([]codes.Code{
   110  					codes.DeadlineExceeded,
   111  					codes.Unavailable,
   112  				}, gax.Backoff{
   113  					Initial:    100 * time.Millisecond,
   114  					Max:        60000 * time.Millisecond,
   115  					Multiplier: 1.30,
   116  				})
   117  			}),
   118  		},
   119  		GetIamPolicy: []gax.CallOption{
   120  			gax.WithTimeout(60000 * time.Millisecond),
   121  		},
   122  		SetIamPolicy: []gax.CallOption{
   123  			gax.WithTimeout(60000 * time.Millisecond),
   124  		},
   125  		TestIamPermissions: []gax.CallOption{
   126  			gax.WithTimeout(60000 * time.Millisecond),
   127  		},
   128  	}
   129  }
   130  
   131  func defaultRESTCallOptions() *CallOptions {
   132  	return &CallOptions{
   133  		CreateConnection: []gax.CallOption{
   134  			gax.WithTimeout(60000 * time.Millisecond),
   135  		},
   136  		GetConnection: []gax.CallOption{
   137  			gax.WithTimeout(60000 * time.Millisecond),
   138  			gax.WithRetry(func() gax.Retryer {
   139  				return gax.OnHTTPCodes(gax.Backoff{
   140  					Initial:    100 * time.Millisecond,
   141  					Max:        60000 * time.Millisecond,
   142  					Multiplier: 1.30,
   143  				},
   144  					http.StatusGatewayTimeout,
   145  					http.StatusServiceUnavailable)
   146  			}),
   147  		},
   148  		ListConnections: []gax.CallOption{
   149  			gax.WithTimeout(60000 * time.Millisecond),
   150  			gax.WithRetry(func() gax.Retryer {
   151  				return gax.OnHTTPCodes(gax.Backoff{
   152  					Initial:    100 * time.Millisecond,
   153  					Max:        60000 * time.Millisecond,
   154  					Multiplier: 1.30,
   155  				},
   156  					http.StatusGatewayTimeout,
   157  					http.StatusServiceUnavailable)
   158  			}),
   159  		},
   160  		UpdateConnection: []gax.CallOption{
   161  			gax.WithTimeout(60000 * time.Millisecond),
   162  		},
   163  		DeleteConnection: []gax.CallOption{
   164  			gax.WithTimeout(60000 * time.Millisecond),
   165  			gax.WithRetry(func() gax.Retryer {
   166  				return gax.OnHTTPCodes(gax.Backoff{
   167  					Initial:    100 * time.Millisecond,
   168  					Max:        60000 * time.Millisecond,
   169  					Multiplier: 1.30,
   170  				},
   171  					http.StatusGatewayTimeout,
   172  					http.StatusServiceUnavailable)
   173  			}),
   174  		},
   175  		GetIamPolicy: []gax.CallOption{
   176  			gax.WithTimeout(60000 * time.Millisecond),
   177  		},
   178  		SetIamPolicy: []gax.CallOption{
   179  			gax.WithTimeout(60000 * time.Millisecond),
   180  		},
   181  		TestIamPermissions: []gax.CallOption{
   182  			gax.WithTimeout(60000 * time.Millisecond),
   183  		},
   184  	}
   185  }
   186  
   187  // internalClient is an interface that defines the methods available from BigQuery Connection API.
   188  type internalClient interface {
   189  	Close() error
   190  	setGoogleClientInfo(...string)
   191  	Connection() *grpc.ClientConn
   192  	CreateConnection(context.Context, *connectionpb.CreateConnectionRequest, ...gax.CallOption) (*connectionpb.Connection, error)
   193  	GetConnection(context.Context, *connectionpb.GetConnectionRequest, ...gax.CallOption) (*connectionpb.Connection, error)
   194  	ListConnections(context.Context, *connectionpb.ListConnectionsRequest, ...gax.CallOption) *ConnectionIterator
   195  	UpdateConnection(context.Context, *connectionpb.UpdateConnectionRequest, ...gax.CallOption) (*connectionpb.Connection, error)
   196  	DeleteConnection(context.Context, *connectionpb.DeleteConnectionRequest, ...gax.CallOption) error
   197  	GetIamPolicy(context.Context, *iampb.GetIamPolicyRequest, ...gax.CallOption) (*iampb.Policy, error)
   198  	SetIamPolicy(context.Context, *iampb.SetIamPolicyRequest, ...gax.CallOption) (*iampb.Policy, error)
   199  	TestIamPermissions(context.Context, *iampb.TestIamPermissionsRequest, ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error)
   200  }
   201  
   202  // Client is a client for interacting with BigQuery Connection API.
   203  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   204  //
   205  // Manages external data source connections and credentials.
   206  type Client struct {
   207  	// The internal transport-dependent client.
   208  	internalClient internalClient
   209  
   210  	// The call options for this service.
   211  	CallOptions *CallOptions
   212  }
   213  
   214  // Wrapper methods routed to the internal client.
   215  
   216  // Close closes the connection to the API service. The user should invoke this when
   217  // the client is no longer required.
   218  func (c *Client) Close() error {
   219  	return c.internalClient.Close()
   220  }
   221  
   222  // setGoogleClientInfo sets the name and version of the application in
   223  // the `x-goog-api-client` header passed on each request. Intended for
   224  // use by Google-written clients.
   225  func (c *Client) setGoogleClientInfo(keyval ...string) {
   226  	c.internalClient.setGoogleClientInfo(keyval...)
   227  }
   228  
   229  // Connection returns a connection to the API service.
   230  //
   231  // Deprecated: Connections are now pooled so this method does not always
   232  // return the same resource.
   233  func (c *Client) Connection() *grpc.ClientConn {
   234  	return c.internalClient.Connection()
   235  }
   236  
   237  // CreateConnection creates a new connection.
   238  func (c *Client) CreateConnection(ctx context.Context, req *connectionpb.CreateConnectionRequest, opts ...gax.CallOption) (*connectionpb.Connection, error) {
   239  	return c.internalClient.CreateConnection(ctx, req, opts...)
   240  }
   241  
   242  // GetConnection returns specified connection.
   243  func (c *Client) GetConnection(ctx context.Context, req *connectionpb.GetConnectionRequest, opts ...gax.CallOption) (*connectionpb.Connection, error) {
   244  	return c.internalClient.GetConnection(ctx, req, opts...)
   245  }
   246  
   247  // ListConnections returns a list of connections in the given project.
   248  func (c *Client) ListConnections(ctx context.Context, req *connectionpb.ListConnectionsRequest, opts ...gax.CallOption) *ConnectionIterator {
   249  	return c.internalClient.ListConnections(ctx, req, opts...)
   250  }
   251  
   252  // UpdateConnection updates the specified connection. For security reasons, also resets
   253  // credential if connection properties are in the update field mask.
   254  func (c *Client) UpdateConnection(ctx context.Context, req *connectionpb.UpdateConnectionRequest, opts ...gax.CallOption) (*connectionpb.Connection, error) {
   255  	return c.internalClient.UpdateConnection(ctx, req, opts...)
   256  }
   257  
   258  // DeleteConnection deletes connection and associated credential.
   259  func (c *Client) DeleteConnection(ctx context.Context, req *connectionpb.DeleteConnectionRequest, opts ...gax.CallOption) error {
   260  	return c.internalClient.DeleteConnection(ctx, req, opts...)
   261  }
   262  
   263  // GetIamPolicy gets the access control policy for a resource.
   264  // Returns an empty policy if the resource exists and does not have a policy
   265  // set.
   266  func (c *Client) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
   267  	return c.internalClient.GetIamPolicy(ctx, req, opts...)
   268  }
   269  
   270  // SetIamPolicy sets the access control policy on the specified resource. Replaces any
   271  // existing policy.
   272  //
   273  // Can return NOT_FOUND, INVALID_ARGUMENT, and PERMISSION_DENIED errors.
   274  func (c *Client) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
   275  	return c.internalClient.SetIamPolicy(ctx, req, opts...)
   276  }
   277  
   278  // TestIamPermissions returns permissions that a caller has on the specified resource.
   279  // If the resource does not exist, this will return an empty set of
   280  // permissions, not a NOT_FOUND error.
   281  //
   282  // Note: This operation is designed to be used for building permission-aware
   283  // UIs and command-line tools, not for authorization checking. This operation
   284  // may “fail open” without warning.
   285  func (c *Client) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
   286  	return c.internalClient.TestIamPermissions(ctx, req, opts...)
   287  }
   288  
   289  // gRPCClient is a client for interacting with BigQuery Connection API over gRPC transport.
   290  //
   291  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   292  type gRPCClient struct {
   293  	// Connection pool of gRPC connections to the service.
   294  	connPool gtransport.ConnPool
   295  
   296  	// Points back to the CallOptions field of the containing Client
   297  	CallOptions **CallOptions
   298  
   299  	// The gRPC API client.
   300  	client connectionpb.ConnectionServiceClient
   301  
   302  	// The x-goog-* metadata to be sent with each request.
   303  	xGoogHeaders []string
   304  }
   305  
   306  // NewClient creates a new connection service client based on gRPC.
   307  // The returned client must be Closed when it is done being used to clean up its underlying connections.
   308  //
   309  // Manages external data source connections and credentials.
   310  func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
   311  	clientOpts := defaultGRPCClientOptions()
   312  	if newClientHook != nil {
   313  		hookOpts, err := newClientHook(ctx, clientHookParams{})
   314  		if err != nil {
   315  			return nil, err
   316  		}
   317  		clientOpts = append(clientOpts, hookOpts...)
   318  	}
   319  
   320  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   321  	if err != nil {
   322  		return nil, err
   323  	}
   324  	client := Client{CallOptions: defaultCallOptions()}
   325  
   326  	c := &gRPCClient{
   327  		connPool:    connPool,
   328  		client:      connectionpb.NewConnectionServiceClient(connPool),
   329  		CallOptions: &client.CallOptions,
   330  	}
   331  	c.setGoogleClientInfo()
   332  
   333  	client.internalClient = c
   334  
   335  	return &client, nil
   336  }
   337  
   338  // Connection returns a connection to the API service.
   339  //
   340  // Deprecated: Connections are now pooled so this method does not always
   341  // return the same resource.
   342  func (c *gRPCClient) Connection() *grpc.ClientConn {
   343  	return c.connPool.Conn()
   344  }
   345  
   346  // setGoogleClientInfo sets the name and version of the application in
   347  // the `x-goog-api-client` header passed on each request. Intended for
   348  // use by Google-written clients.
   349  func (c *gRPCClient) setGoogleClientInfo(keyval ...string) {
   350  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   351  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
   352  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   353  }
   354  
   355  // Close closes the connection to the API service. The user should invoke this when
   356  // the client is no longer required.
   357  func (c *gRPCClient) Close() error {
   358  	return c.connPool.Close()
   359  }
   360  
   361  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   362  type restClient struct {
   363  	// The http endpoint to connect to.
   364  	endpoint string
   365  
   366  	// The http client.
   367  	httpClient *http.Client
   368  
   369  	// The x-goog-* headers to be sent with each request.
   370  	xGoogHeaders []string
   371  
   372  	// Points back to the CallOptions field of the containing Client
   373  	CallOptions **CallOptions
   374  }
   375  
   376  // NewRESTClient creates a new connection service rest client.
   377  //
   378  // Manages external data source connections and credentials.
   379  func NewRESTClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
   380  	clientOpts := append(defaultRESTClientOptions(), opts...)
   381  	httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
   382  	if err != nil {
   383  		return nil, err
   384  	}
   385  
   386  	callOpts := defaultRESTCallOptions()
   387  	c := &restClient{
   388  		endpoint:    endpoint,
   389  		httpClient:  httpClient,
   390  		CallOptions: &callOpts,
   391  	}
   392  	c.setGoogleClientInfo()
   393  
   394  	return &Client{internalClient: c, CallOptions: callOpts}, nil
   395  }
   396  
   397  func defaultRESTClientOptions() []option.ClientOption {
   398  	return []option.ClientOption{
   399  		internaloption.WithDefaultEndpoint("https://bigqueryconnection.googleapis.com"),
   400  		internaloption.WithDefaultEndpointTemplate("https://bigqueryconnection.UNIVERSE_DOMAIN"),
   401  		internaloption.WithDefaultMTLSEndpoint("https://bigqueryconnection.mtls.googleapis.com"),
   402  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
   403  		internaloption.WithDefaultAudience("https://bigqueryconnection.googleapis.com/"),
   404  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
   405  	}
   406  }
   407  
   408  // setGoogleClientInfo sets the name and version of the application in
   409  // the `x-goog-api-client` header passed on each request. Intended for
   410  // use by Google-written clients.
   411  func (c *restClient) setGoogleClientInfo(keyval ...string) {
   412  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   413  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN")
   414  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   415  }
   416  
   417  // Close closes the connection to the API service. The user should invoke this when
   418  // the client is no longer required.
   419  func (c *restClient) Close() error {
   420  	// Replace httpClient with nil to force cleanup.
   421  	c.httpClient = nil
   422  	return nil
   423  }
   424  
   425  // Connection returns a connection to the API service.
   426  //
   427  // Deprecated: This method always returns nil.
   428  func (c *restClient) Connection() *grpc.ClientConn {
   429  	return nil
   430  }
   431  func (c *gRPCClient) CreateConnection(ctx context.Context, req *connectionpb.CreateConnectionRequest, opts ...gax.CallOption) (*connectionpb.Connection, error) {
   432  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   433  
   434  	hds = append(c.xGoogHeaders, hds...)
   435  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   436  	opts = append((*c.CallOptions).CreateConnection[0:len((*c.CallOptions).CreateConnection):len((*c.CallOptions).CreateConnection)], opts...)
   437  	var resp *connectionpb.Connection
   438  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   439  		var err error
   440  		resp, err = c.client.CreateConnection(ctx, req, settings.GRPC...)
   441  		return err
   442  	}, opts...)
   443  	if err != nil {
   444  		return nil, err
   445  	}
   446  	return resp, nil
   447  }
   448  
   449  func (c *gRPCClient) GetConnection(ctx context.Context, req *connectionpb.GetConnectionRequest, opts ...gax.CallOption) (*connectionpb.Connection, error) {
   450  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   451  
   452  	hds = append(c.xGoogHeaders, hds...)
   453  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   454  	opts = append((*c.CallOptions).GetConnection[0:len((*c.CallOptions).GetConnection):len((*c.CallOptions).GetConnection)], opts...)
   455  	var resp *connectionpb.Connection
   456  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   457  		var err error
   458  		resp, err = c.client.GetConnection(ctx, req, settings.GRPC...)
   459  		return err
   460  	}, opts...)
   461  	if err != nil {
   462  		return nil, err
   463  	}
   464  	return resp, nil
   465  }
   466  
   467  func (c *gRPCClient) ListConnections(ctx context.Context, req *connectionpb.ListConnectionsRequest, opts ...gax.CallOption) *ConnectionIterator {
   468  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   469  
   470  	hds = append(c.xGoogHeaders, hds...)
   471  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   472  	opts = append((*c.CallOptions).ListConnections[0:len((*c.CallOptions).ListConnections):len((*c.CallOptions).ListConnections)], opts...)
   473  	it := &ConnectionIterator{}
   474  	req = proto.Clone(req).(*connectionpb.ListConnectionsRequest)
   475  	it.InternalFetch = func(pageSize int, pageToken string) ([]*connectionpb.Connection, string, error) {
   476  		resp := &connectionpb.ListConnectionsResponse{}
   477  		if pageToken != "" {
   478  			req.PageToken = pageToken
   479  		}
   480  		if pageSize > math.MaxInt32 {
   481  			req.PageSize = math.MaxInt32
   482  		} else if pageSize != 0 {
   483  			req.PageSize = int32(pageSize)
   484  		}
   485  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   486  			var err error
   487  			resp, err = c.client.ListConnections(ctx, req, settings.GRPC...)
   488  			return err
   489  		}, opts...)
   490  		if err != nil {
   491  			return nil, "", err
   492  		}
   493  
   494  		it.Response = resp
   495  		return resp.GetConnections(), resp.GetNextPageToken(), nil
   496  	}
   497  	fetch := func(pageSize int, pageToken string) (string, error) {
   498  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   499  		if err != nil {
   500  			return "", err
   501  		}
   502  		it.items = append(it.items, items...)
   503  		return nextPageToken, nil
   504  	}
   505  
   506  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   507  	it.pageInfo.MaxSize = int(req.GetPageSize())
   508  	it.pageInfo.Token = req.GetPageToken()
   509  
   510  	return it
   511  }
   512  
   513  func (c *gRPCClient) UpdateConnection(ctx context.Context, req *connectionpb.UpdateConnectionRequest, opts ...gax.CallOption) (*connectionpb.Connection, error) {
   514  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   515  
   516  	hds = append(c.xGoogHeaders, hds...)
   517  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   518  	opts = append((*c.CallOptions).UpdateConnection[0:len((*c.CallOptions).UpdateConnection):len((*c.CallOptions).UpdateConnection)], opts...)
   519  	var resp *connectionpb.Connection
   520  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   521  		var err error
   522  		resp, err = c.client.UpdateConnection(ctx, req, settings.GRPC...)
   523  		return err
   524  	}, opts...)
   525  	if err != nil {
   526  		return nil, err
   527  	}
   528  	return resp, nil
   529  }
   530  
   531  func (c *gRPCClient) DeleteConnection(ctx context.Context, req *connectionpb.DeleteConnectionRequest, opts ...gax.CallOption) error {
   532  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   533  
   534  	hds = append(c.xGoogHeaders, hds...)
   535  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   536  	opts = append((*c.CallOptions).DeleteConnection[0:len((*c.CallOptions).DeleteConnection):len((*c.CallOptions).DeleteConnection)], opts...)
   537  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   538  		var err error
   539  		_, err = c.client.DeleteConnection(ctx, req, settings.GRPC...)
   540  		return err
   541  	}, opts...)
   542  	return err
   543  }
   544  
   545  func (c *gRPCClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
   546  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
   547  
   548  	hds = append(c.xGoogHeaders, hds...)
   549  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   550  	opts = append((*c.CallOptions).GetIamPolicy[0:len((*c.CallOptions).GetIamPolicy):len((*c.CallOptions).GetIamPolicy)], opts...)
   551  	var resp *iampb.Policy
   552  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   553  		var err error
   554  		resp, err = c.client.GetIamPolicy(ctx, req, settings.GRPC...)
   555  		return err
   556  	}, opts...)
   557  	if err != nil {
   558  		return nil, err
   559  	}
   560  	return resp, nil
   561  }
   562  
   563  func (c *gRPCClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
   564  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
   565  
   566  	hds = append(c.xGoogHeaders, hds...)
   567  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   568  	opts = append((*c.CallOptions).SetIamPolicy[0:len((*c.CallOptions).SetIamPolicy):len((*c.CallOptions).SetIamPolicy)], opts...)
   569  	var resp *iampb.Policy
   570  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   571  		var err error
   572  		resp, err = c.client.SetIamPolicy(ctx, req, settings.GRPC...)
   573  		return err
   574  	}, opts...)
   575  	if err != nil {
   576  		return nil, err
   577  	}
   578  	return resp, nil
   579  }
   580  
   581  func (c *gRPCClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
   582  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
   583  
   584  	hds = append(c.xGoogHeaders, hds...)
   585  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   586  	opts = append((*c.CallOptions).TestIamPermissions[0:len((*c.CallOptions).TestIamPermissions):len((*c.CallOptions).TestIamPermissions)], opts...)
   587  	var resp *iampb.TestIamPermissionsResponse
   588  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   589  		var err error
   590  		resp, err = c.client.TestIamPermissions(ctx, req, settings.GRPC...)
   591  		return err
   592  	}, opts...)
   593  	if err != nil {
   594  		return nil, err
   595  	}
   596  	return resp, nil
   597  }
   598  
   599  // CreateConnection creates a new connection.
   600  func (c *restClient) CreateConnection(ctx context.Context, req *connectionpb.CreateConnectionRequest, opts ...gax.CallOption) (*connectionpb.Connection, error) {
   601  	m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
   602  	body := req.GetConnection()
   603  	jsonReq, err := m.Marshal(body)
   604  	if err != nil {
   605  		return nil, err
   606  	}
   607  
   608  	baseUrl, err := url.Parse(c.endpoint)
   609  	if err != nil {
   610  		return nil, err
   611  	}
   612  	baseUrl.Path += fmt.Sprintf("/v1/%v/connections", req.GetParent())
   613  
   614  	params := url.Values{}
   615  	params.Add("$alt", "json;enum-encoding=int")
   616  	if req.GetConnectionId() != "" {
   617  		params.Add("connectionId", fmt.Sprintf("%v", req.GetConnectionId()))
   618  	}
   619  
   620  	baseUrl.RawQuery = params.Encode()
   621  
   622  	// Build HTTP headers from client and context metadata.
   623  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   624  
   625  	hds = append(c.xGoogHeaders, hds...)
   626  	hds = append(hds, "Content-Type", "application/json")
   627  	headers := gax.BuildHeaders(ctx, hds...)
   628  	opts = append((*c.CallOptions).CreateConnection[0:len((*c.CallOptions).CreateConnection):len((*c.CallOptions).CreateConnection)], opts...)
   629  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   630  	resp := &connectionpb.Connection{}
   631  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   632  		if settings.Path != "" {
   633  			baseUrl.Path = settings.Path
   634  		}
   635  		httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
   636  		if err != nil {
   637  			return err
   638  		}
   639  		httpReq = httpReq.WithContext(ctx)
   640  		httpReq.Header = headers
   641  
   642  		httpRsp, err := c.httpClient.Do(httpReq)
   643  		if err != nil {
   644  			return err
   645  		}
   646  		defer httpRsp.Body.Close()
   647  
   648  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   649  			return err
   650  		}
   651  
   652  		buf, err := io.ReadAll(httpRsp.Body)
   653  		if err != nil {
   654  			return err
   655  		}
   656  
   657  		if err := unm.Unmarshal(buf, resp); err != nil {
   658  			return err
   659  		}
   660  
   661  		return nil
   662  	}, opts...)
   663  	if e != nil {
   664  		return nil, e
   665  	}
   666  	return resp, nil
   667  }
   668  
   669  // GetConnection returns specified connection.
   670  func (c *restClient) GetConnection(ctx context.Context, req *connectionpb.GetConnectionRequest, opts ...gax.CallOption) (*connectionpb.Connection, error) {
   671  	baseUrl, err := url.Parse(c.endpoint)
   672  	if err != nil {
   673  		return nil, err
   674  	}
   675  	baseUrl.Path += fmt.Sprintf("/v1/%v", req.GetName())
   676  
   677  	params := url.Values{}
   678  	params.Add("$alt", "json;enum-encoding=int")
   679  
   680  	baseUrl.RawQuery = params.Encode()
   681  
   682  	// Build HTTP headers from client and context metadata.
   683  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   684  
   685  	hds = append(c.xGoogHeaders, hds...)
   686  	hds = append(hds, "Content-Type", "application/json")
   687  	headers := gax.BuildHeaders(ctx, hds...)
   688  	opts = append((*c.CallOptions).GetConnection[0:len((*c.CallOptions).GetConnection):len((*c.CallOptions).GetConnection)], opts...)
   689  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   690  	resp := &connectionpb.Connection{}
   691  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   692  		if settings.Path != "" {
   693  			baseUrl.Path = settings.Path
   694  		}
   695  		httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
   696  		if err != nil {
   697  			return err
   698  		}
   699  		httpReq = httpReq.WithContext(ctx)
   700  		httpReq.Header = headers
   701  
   702  		httpRsp, err := c.httpClient.Do(httpReq)
   703  		if err != nil {
   704  			return err
   705  		}
   706  		defer httpRsp.Body.Close()
   707  
   708  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   709  			return err
   710  		}
   711  
   712  		buf, err := io.ReadAll(httpRsp.Body)
   713  		if err != nil {
   714  			return err
   715  		}
   716  
   717  		if err := unm.Unmarshal(buf, resp); err != nil {
   718  			return err
   719  		}
   720  
   721  		return nil
   722  	}, opts...)
   723  	if e != nil {
   724  		return nil, e
   725  	}
   726  	return resp, nil
   727  }
   728  
   729  // ListConnections returns a list of connections in the given project.
   730  func (c *restClient) ListConnections(ctx context.Context, req *connectionpb.ListConnectionsRequest, opts ...gax.CallOption) *ConnectionIterator {
   731  	it := &ConnectionIterator{}
   732  	req = proto.Clone(req).(*connectionpb.ListConnectionsRequest)
   733  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   734  	it.InternalFetch = func(pageSize int, pageToken string) ([]*connectionpb.Connection, string, error) {
   735  		resp := &connectionpb.ListConnectionsResponse{}
   736  		if pageToken != "" {
   737  			req.PageToken = pageToken
   738  		}
   739  		if pageSize > math.MaxInt32 {
   740  			req.PageSize = math.MaxInt32
   741  		} else if pageSize != 0 {
   742  			req.PageSize = int32(pageSize)
   743  		}
   744  		baseUrl, err := url.Parse(c.endpoint)
   745  		if err != nil {
   746  			return nil, "", err
   747  		}
   748  		baseUrl.Path += fmt.Sprintf("/v1/%v/connections", req.GetParent())
   749  
   750  		params := url.Values{}
   751  		params.Add("$alt", "json;enum-encoding=int")
   752  		params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize()))
   753  		if req.GetPageToken() != "" {
   754  			params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken()))
   755  		}
   756  
   757  		baseUrl.RawQuery = params.Encode()
   758  
   759  		// Build HTTP headers from client and context metadata.
   760  		hds := append(c.xGoogHeaders, "Content-Type", "application/json")
   761  		headers := gax.BuildHeaders(ctx, hds...)
   762  		e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   763  			if settings.Path != "" {
   764  				baseUrl.Path = settings.Path
   765  			}
   766  			httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
   767  			if err != nil {
   768  				return err
   769  			}
   770  			httpReq.Header = headers
   771  
   772  			httpRsp, err := c.httpClient.Do(httpReq)
   773  			if err != nil {
   774  				return err
   775  			}
   776  			defer httpRsp.Body.Close()
   777  
   778  			if err = googleapi.CheckResponse(httpRsp); err != nil {
   779  				return err
   780  			}
   781  
   782  			buf, err := io.ReadAll(httpRsp.Body)
   783  			if err != nil {
   784  				return err
   785  			}
   786  
   787  			if err := unm.Unmarshal(buf, resp); err != nil {
   788  				return err
   789  			}
   790  
   791  			return nil
   792  		}, opts...)
   793  		if e != nil {
   794  			return nil, "", e
   795  		}
   796  		it.Response = resp
   797  		return resp.GetConnections(), resp.GetNextPageToken(), nil
   798  	}
   799  
   800  	fetch := func(pageSize int, pageToken string) (string, error) {
   801  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   802  		if err != nil {
   803  			return "", err
   804  		}
   805  		it.items = append(it.items, items...)
   806  		return nextPageToken, nil
   807  	}
   808  
   809  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   810  	it.pageInfo.MaxSize = int(req.GetPageSize())
   811  	it.pageInfo.Token = req.GetPageToken()
   812  
   813  	return it
   814  }
   815  
   816  // UpdateConnection updates the specified connection. For security reasons, also resets
   817  // credential if connection properties are in the update field mask.
   818  func (c *restClient) UpdateConnection(ctx context.Context, req *connectionpb.UpdateConnectionRequest, opts ...gax.CallOption) (*connectionpb.Connection, error) {
   819  	m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
   820  	body := req.GetConnection()
   821  	jsonReq, err := m.Marshal(body)
   822  	if err != nil {
   823  		return nil, err
   824  	}
   825  
   826  	baseUrl, err := url.Parse(c.endpoint)
   827  	if err != nil {
   828  		return nil, err
   829  	}
   830  	baseUrl.Path += fmt.Sprintf("/v1/%v", req.GetName())
   831  
   832  	params := url.Values{}
   833  	params.Add("$alt", "json;enum-encoding=int")
   834  	if req.GetUpdateMask() != nil {
   835  		updateMask, err := protojson.Marshal(req.GetUpdateMask())
   836  		if err != nil {
   837  			return nil, err
   838  		}
   839  		params.Add("updateMask", string(updateMask[1:len(updateMask)-1]))
   840  	}
   841  
   842  	baseUrl.RawQuery = params.Encode()
   843  
   844  	// Build HTTP headers from client and context metadata.
   845  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   846  
   847  	hds = append(c.xGoogHeaders, hds...)
   848  	hds = append(hds, "Content-Type", "application/json")
   849  	headers := gax.BuildHeaders(ctx, hds...)
   850  	opts = append((*c.CallOptions).UpdateConnection[0:len((*c.CallOptions).UpdateConnection):len((*c.CallOptions).UpdateConnection)], opts...)
   851  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   852  	resp := &connectionpb.Connection{}
   853  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   854  		if settings.Path != "" {
   855  			baseUrl.Path = settings.Path
   856  		}
   857  		httpReq, err := http.NewRequest("PATCH", baseUrl.String(), bytes.NewReader(jsonReq))
   858  		if err != nil {
   859  			return err
   860  		}
   861  		httpReq = httpReq.WithContext(ctx)
   862  		httpReq.Header = headers
   863  
   864  		httpRsp, err := c.httpClient.Do(httpReq)
   865  		if err != nil {
   866  			return err
   867  		}
   868  		defer httpRsp.Body.Close()
   869  
   870  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   871  			return err
   872  		}
   873  
   874  		buf, err := io.ReadAll(httpRsp.Body)
   875  		if err != nil {
   876  			return err
   877  		}
   878  
   879  		if err := unm.Unmarshal(buf, resp); err != nil {
   880  			return err
   881  		}
   882  
   883  		return nil
   884  	}, opts...)
   885  	if e != nil {
   886  		return nil, e
   887  	}
   888  	return resp, nil
   889  }
   890  
   891  // DeleteConnection deletes connection and associated credential.
   892  func (c *restClient) DeleteConnection(ctx context.Context, req *connectionpb.DeleteConnectionRequest, opts ...gax.CallOption) error {
   893  	baseUrl, err := url.Parse(c.endpoint)
   894  	if err != nil {
   895  		return err
   896  	}
   897  	baseUrl.Path += fmt.Sprintf("/v1/%v", req.GetName())
   898  
   899  	params := url.Values{}
   900  	params.Add("$alt", "json;enum-encoding=int")
   901  
   902  	baseUrl.RawQuery = params.Encode()
   903  
   904  	// Build HTTP headers from client and context metadata.
   905  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   906  
   907  	hds = append(c.xGoogHeaders, hds...)
   908  	hds = append(hds, "Content-Type", "application/json")
   909  	headers := gax.BuildHeaders(ctx, hds...)
   910  	return gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   911  		if settings.Path != "" {
   912  			baseUrl.Path = settings.Path
   913  		}
   914  		httpReq, err := http.NewRequest("DELETE", baseUrl.String(), nil)
   915  		if err != nil {
   916  			return err
   917  		}
   918  		httpReq = httpReq.WithContext(ctx)
   919  		httpReq.Header = headers
   920  
   921  		httpRsp, err := c.httpClient.Do(httpReq)
   922  		if err != nil {
   923  			return err
   924  		}
   925  		defer httpRsp.Body.Close()
   926  
   927  		// Returns nil if there is no error, otherwise wraps
   928  		// the response code and body into a non-nil error
   929  		return googleapi.CheckResponse(httpRsp)
   930  	}, opts...)
   931  }
   932  
   933  // GetIamPolicy gets the access control policy for a resource.
   934  // Returns an empty policy if the resource exists and does not have a policy
   935  // set.
   936  func (c *restClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
   937  	m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
   938  	jsonReq, err := m.Marshal(req)
   939  	if err != nil {
   940  		return nil, err
   941  	}
   942  
   943  	baseUrl, err := url.Parse(c.endpoint)
   944  	if err != nil {
   945  		return nil, err
   946  	}
   947  	baseUrl.Path += fmt.Sprintf("/v1/%v:getIamPolicy", req.GetResource())
   948  
   949  	params := url.Values{}
   950  	params.Add("$alt", "json;enum-encoding=int")
   951  
   952  	baseUrl.RawQuery = params.Encode()
   953  
   954  	// Build HTTP headers from client and context metadata.
   955  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
   956  
   957  	hds = append(c.xGoogHeaders, hds...)
   958  	hds = append(hds, "Content-Type", "application/json")
   959  	headers := gax.BuildHeaders(ctx, hds...)
   960  	opts = append((*c.CallOptions).GetIamPolicy[0:len((*c.CallOptions).GetIamPolicy):len((*c.CallOptions).GetIamPolicy)], opts...)
   961  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   962  	resp := &iampb.Policy{}
   963  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   964  		if settings.Path != "" {
   965  			baseUrl.Path = settings.Path
   966  		}
   967  		httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
   968  		if err != nil {
   969  			return err
   970  		}
   971  		httpReq = httpReq.WithContext(ctx)
   972  		httpReq.Header = headers
   973  
   974  		httpRsp, err := c.httpClient.Do(httpReq)
   975  		if err != nil {
   976  			return err
   977  		}
   978  		defer httpRsp.Body.Close()
   979  
   980  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   981  			return err
   982  		}
   983  
   984  		buf, err := io.ReadAll(httpRsp.Body)
   985  		if err != nil {
   986  			return err
   987  		}
   988  
   989  		if err := unm.Unmarshal(buf, resp); err != nil {
   990  			return err
   991  		}
   992  
   993  		return nil
   994  	}, opts...)
   995  	if e != nil {
   996  		return nil, e
   997  	}
   998  	return resp, nil
   999  }
  1000  
  1001  // SetIamPolicy sets the access control policy on the specified resource. Replaces any
  1002  // existing policy.
  1003  //
  1004  // Can return NOT_FOUND, INVALID_ARGUMENT, and PERMISSION_DENIED errors.
  1005  func (c *restClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
  1006  	m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
  1007  	jsonReq, err := m.Marshal(req)
  1008  	if err != nil {
  1009  		return nil, err
  1010  	}
  1011  
  1012  	baseUrl, err := url.Parse(c.endpoint)
  1013  	if err != nil {
  1014  		return nil, err
  1015  	}
  1016  	baseUrl.Path += fmt.Sprintf("/v1/%v:setIamPolicy", req.GetResource())
  1017  
  1018  	params := url.Values{}
  1019  	params.Add("$alt", "json;enum-encoding=int")
  1020  
  1021  	baseUrl.RawQuery = params.Encode()
  1022  
  1023  	// Build HTTP headers from client and context metadata.
  1024  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
  1025  
  1026  	hds = append(c.xGoogHeaders, hds...)
  1027  	hds = append(hds, "Content-Type", "application/json")
  1028  	headers := gax.BuildHeaders(ctx, hds...)
  1029  	opts = append((*c.CallOptions).SetIamPolicy[0:len((*c.CallOptions).SetIamPolicy):len((*c.CallOptions).SetIamPolicy)], opts...)
  1030  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
  1031  	resp := &iampb.Policy{}
  1032  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  1033  		if settings.Path != "" {
  1034  			baseUrl.Path = settings.Path
  1035  		}
  1036  		httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
  1037  		if err != nil {
  1038  			return err
  1039  		}
  1040  		httpReq = httpReq.WithContext(ctx)
  1041  		httpReq.Header = headers
  1042  
  1043  		httpRsp, err := c.httpClient.Do(httpReq)
  1044  		if err != nil {
  1045  			return err
  1046  		}
  1047  		defer httpRsp.Body.Close()
  1048  
  1049  		if err = googleapi.CheckResponse(httpRsp); err != nil {
  1050  			return err
  1051  		}
  1052  
  1053  		buf, err := io.ReadAll(httpRsp.Body)
  1054  		if err != nil {
  1055  			return err
  1056  		}
  1057  
  1058  		if err := unm.Unmarshal(buf, resp); err != nil {
  1059  			return err
  1060  		}
  1061  
  1062  		return nil
  1063  	}, opts...)
  1064  	if e != nil {
  1065  		return nil, e
  1066  	}
  1067  	return resp, nil
  1068  }
  1069  
  1070  // TestIamPermissions returns permissions that a caller has on the specified resource.
  1071  // If the resource does not exist, this will return an empty set of
  1072  // permissions, not a NOT_FOUND error.
  1073  //
  1074  // Note: This operation is designed to be used for building permission-aware
  1075  // UIs and command-line tools, not for authorization checking. This operation
  1076  // may “fail open” without warning.
  1077  func (c *restClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
  1078  	m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
  1079  	jsonReq, err := m.Marshal(req)
  1080  	if err != nil {
  1081  		return nil, err
  1082  	}
  1083  
  1084  	baseUrl, err := url.Parse(c.endpoint)
  1085  	if err != nil {
  1086  		return nil, err
  1087  	}
  1088  	baseUrl.Path += fmt.Sprintf("/v1/%v:testIamPermissions", req.GetResource())
  1089  
  1090  	params := url.Values{}
  1091  	params.Add("$alt", "json;enum-encoding=int")
  1092  
  1093  	baseUrl.RawQuery = params.Encode()
  1094  
  1095  	// Build HTTP headers from client and context metadata.
  1096  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "resource", url.QueryEscape(req.GetResource()))}
  1097  
  1098  	hds = append(c.xGoogHeaders, hds...)
  1099  	hds = append(hds, "Content-Type", "application/json")
  1100  	headers := gax.BuildHeaders(ctx, hds...)
  1101  	opts = append((*c.CallOptions).TestIamPermissions[0:len((*c.CallOptions).TestIamPermissions):len((*c.CallOptions).TestIamPermissions)], opts...)
  1102  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
  1103  	resp := &iampb.TestIamPermissionsResponse{}
  1104  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
  1105  		if settings.Path != "" {
  1106  			baseUrl.Path = settings.Path
  1107  		}
  1108  		httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
  1109  		if err != nil {
  1110  			return err
  1111  		}
  1112  		httpReq = httpReq.WithContext(ctx)
  1113  		httpReq.Header = headers
  1114  
  1115  		httpRsp, err := c.httpClient.Do(httpReq)
  1116  		if err != nil {
  1117  			return err
  1118  		}
  1119  		defer httpRsp.Body.Close()
  1120  
  1121  		if err = googleapi.CheckResponse(httpRsp); err != nil {
  1122  			return err
  1123  		}
  1124  
  1125  		buf, err := io.ReadAll(httpRsp.Body)
  1126  		if err != nil {
  1127  			return err
  1128  		}
  1129  
  1130  		if err := unm.Unmarshal(buf, resp); err != nil {
  1131  			return err
  1132  		}
  1133  
  1134  		return nil
  1135  	}, opts...)
  1136  	if e != nil {
  1137  		return nil, e
  1138  	}
  1139  	return resp, nil
  1140  }
  1141  

View as plain text