...

Source file src/cloud.google.com/go/resourcemanager/apiv3/tag_holds_client.go

Documentation: cloud.google.com/go/resourcemanager/apiv3

     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 resourcemanager
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"fmt"
    23  	"io"
    24  	"math"
    25  	"net/http"
    26  	"net/url"
    27  
    28  	"cloud.google.com/go/longrunning"
    29  	lroauto "cloud.google.com/go/longrunning/autogen"
    30  	longrunningpb "cloud.google.com/go/longrunning/autogen/longrunningpb"
    31  	resourcemanagerpb "cloud.google.com/go/resourcemanager/apiv3/resourcemanagerpb"
    32  	gax "github.com/googleapis/gax-go/v2"
    33  	"google.golang.org/api/googleapi"
    34  	"google.golang.org/api/iterator"
    35  	"google.golang.org/api/option"
    36  	"google.golang.org/api/option/internaloption"
    37  	gtransport "google.golang.org/api/transport/grpc"
    38  	httptransport "google.golang.org/api/transport/http"
    39  	"google.golang.org/grpc"
    40  	"google.golang.org/protobuf/encoding/protojson"
    41  	"google.golang.org/protobuf/proto"
    42  )
    43  
    44  var newTagHoldsClientHook clientHook
    45  
    46  // TagHoldsCallOptions contains the retry settings for each method of TagHoldsClient.
    47  type TagHoldsCallOptions struct {
    48  	CreateTagHold []gax.CallOption
    49  	DeleteTagHold []gax.CallOption
    50  	ListTagHolds  []gax.CallOption
    51  	GetOperation  []gax.CallOption
    52  }
    53  
    54  func defaultTagHoldsGRPCClientOptions() []option.ClientOption {
    55  	return []option.ClientOption{
    56  		internaloption.WithDefaultEndpoint("cloudresourcemanager.googleapis.com:443"),
    57  		internaloption.WithDefaultEndpointTemplate("cloudresourcemanager.UNIVERSE_DOMAIN:443"),
    58  		internaloption.WithDefaultMTLSEndpoint("cloudresourcemanager.mtls.googleapis.com:443"),
    59  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
    60  		internaloption.WithDefaultAudience("https://cloudresourcemanager.googleapis.com/"),
    61  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
    62  		internaloption.EnableJwtWithScope(),
    63  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    64  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    65  	}
    66  }
    67  
    68  func defaultTagHoldsCallOptions() *TagHoldsCallOptions {
    69  	return &TagHoldsCallOptions{
    70  		CreateTagHold: []gax.CallOption{},
    71  		DeleteTagHold: []gax.CallOption{},
    72  		ListTagHolds:  []gax.CallOption{},
    73  		GetOperation:  []gax.CallOption{},
    74  	}
    75  }
    76  
    77  func defaultTagHoldsRESTCallOptions() *TagHoldsCallOptions {
    78  	return &TagHoldsCallOptions{
    79  		CreateTagHold: []gax.CallOption{},
    80  		DeleteTagHold: []gax.CallOption{},
    81  		ListTagHolds:  []gax.CallOption{},
    82  		GetOperation:  []gax.CallOption{},
    83  	}
    84  }
    85  
    86  // internalTagHoldsClient is an interface that defines the methods available from Cloud Resource Manager API.
    87  type internalTagHoldsClient interface {
    88  	Close() error
    89  	setGoogleClientInfo(...string)
    90  	Connection() *grpc.ClientConn
    91  	CreateTagHold(context.Context, *resourcemanagerpb.CreateTagHoldRequest, ...gax.CallOption) (*CreateTagHoldOperation, error)
    92  	CreateTagHoldOperation(name string) *CreateTagHoldOperation
    93  	DeleteTagHold(context.Context, *resourcemanagerpb.DeleteTagHoldRequest, ...gax.CallOption) (*DeleteTagHoldOperation, error)
    94  	DeleteTagHoldOperation(name string) *DeleteTagHoldOperation
    95  	ListTagHolds(context.Context, *resourcemanagerpb.ListTagHoldsRequest, ...gax.CallOption) *TagHoldIterator
    96  	GetOperation(context.Context, *longrunningpb.GetOperationRequest, ...gax.CallOption) (*longrunningpb.Operation, error)
    97  }
    98  
    99  // TagHoldsClient is a client for interacting with Cloud Resource Manager API.
   100  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   101  //
   102  // Allow users to create and manage TagHolds for TagValues. TagHolds represent
   103  // the use of a Tag Value that is not captured by TagBindings but
   104  // should still block TagValue deletion (such as a reference in a policy
   105  // condition). This service provides isolated failure domains by cloud location
   106  // so that TagHolds can be managed in the same location as their usage.
   107  type TagHoldsClient struct {
   108  	// The internal transport-dependent client.
   109  	internalClient internalTagHoldsClient
   110  
   111  	// The call options for this service.
   112  	CallOptions *TagHoldsCallOptions
   113  
   114  	// LROClient is used internally to handle long-running operations.
   115  	// It is exposed so that its CallOptions can be modified if required.
   116  	// Users should not Close this client.
   117  	LROClient *lroauto.OperationsClient
   118  }
   119  
   120  // Wrapper methods routed to the internal client.
   121  
   122  // Close closes the connection to the API service. The user should invoke this when
   123  // the client is no longer required.
   124  func (c *TagHoldsClient) Close() error {
   125  	return c.internalClient.Close()
   126  }
   127  
   128  // setGoogleClientInfo sets the name and version of the application in
   129  // the `x-goog-api-client` header passed on each request. Intended for
   130  // use by Google-written clients.
   131  func (c *TagHoldsClient) setGoogleClientInfo(keyval ...string) {
   132  	c.internalClient.setGoogleClientInfo(keyval...)
   133  }
   134  
   135  // Connection returns a connection to the API service.
   136  //
   137  // Deprecated: Connections are now pooled so this method does not always
   138  // return the same resource.
   139  func (c *TagHoldsClient) Connection() *grpc.ClientConn {
   140  	return c.internalClient.Connection()
   141  }
   142  
   143  // CreateTagHold creates a TagHold. Returns ALREADY_EXISTS if a TagHold with the same
   144  // resource and origin exists under the same TagValue.
   145  func (c *TagHoldsClient) CreateTagHold(ctx context.Context, req *resourcemanagerpb.CreateTagHoldRequest, opts ...gax.CallOption) (*CreateTagHoldOperation, error) {
   146  	return c.internalClient.CreateTagHold(ctx, req, opts...)
   147  }
   148  
   149  // CreateTagHoldOperation returns a new CreateTagHoldOperation from a given name.
   150  // The name must be that of a previously created CreateTagHoldOperation, possibly from a different process.
   151  func (c *TagHoldsClient) CreateTagHoldOperation(name string) *CreateTagHoldOperation {
   152  	return c.internalClient.CreateTagHoldOperation(name)
   153  }
   154  
   155  // DeleteTagHold deletes a TagHold.
   156  func (c *TagHoldsClient) DeleteTagHold(ctx context.Context, req *resourcemanagerpb.DeleteTagHoldRequest, opts ...gax.CallOption) (*DeleteTagHoldOperation, error) {
   157  	return c.internalClient.DeleteTagHold(ctx, req, opts...)
   158  }
   159  
   160  // DeleteTagHoldOperation returns a new DeleteTagHoldOperation from a given name.
   161  // The name must be that of a previously created DeleteTagHoldOperation, possibly from a different process.
   162  func (c *TagHoldsClient) DeleteTagHoldOperation(name string) *DeleteTagHoldOperation {
   163  	return c.internalClient.DeleteTagHoldOperation(name)
   164  }
   165  
   166  // ListTagHolds lists TagHolds under a TagValue.
   167  func (c *TagHoldsClient) ListTagHolds(ctx context.Context, req *resourcemanagerpb.ListTagHoldsRequest, opts ...gax.CallOption) *TagHoldIterator {
   168  	return c.internalClient.ListTagHolds(ctx, req, opts...)
   169  }
   170  
   171  // GetOperation is a utility method from google.longrunning.Operations.
   172  func (c *TagHoldsClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
   173  	return c.internalClient.GetOperation(ctx, req, opts...)
   174  }
   175  
   176  // tagHoldsGRPCClient is a client for interacting with Cloud Resource Manager API over gRPC transport.
   177  //
   178  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   179  type tagHoldsGRPCClient struct {
   180  	// Connection pool of gRPC connections to the service.
   181  	connPool gtransport.ConnPool
   182  
   183  	// Points back to the CallOptions field of the containing TagHoldsClient
   184  	CallOptions **TagHoldsCallOptions
   185  
   186  	// The gRPC API client.
   187  	tagHoldsClient resourcemanagerpb.TagHoldsClient
   188  
   189  	// LROClient is used internally to handle long-running operations.
   190  	// It is exposed so that its CallOptions can be modified if required.
   191  	// Users should not Close this client.
   192  	LROClient **lroauto.OperationsClient
   193  
   194  	operationsClient longrunningpb.OperationsClient
   195  
   196  	// The x-goog-* metadata to be sent with each request.
   197  	xGoogHeaders []string
   198  }
   199  
   200  // NewTagHoldsClient creates a new tag holds client based on gRPC.
   201  // The returned client must be Closed when it is done being used to clean up its underlying connections.
   202  //
   203  // Allow users to create and manage TagHolds for TagValues. TagHolds represent
   204  // the use of a Tag Value that is not captured by TagBindings but
   205  // should still block TagValue deletion (such as a reference in a policy
   206  // condition). This service provides isolated failure domains by cloud location
   207  // so that TagHolds can be managed in the same location as their usage.
   208  func NewTagHoldsClient(ctx context.Context, opts ...option.ClientOption) (*TagHoldsClient, error) {
   209  	clientOpts := defaultTagHoldsGRPCClientOptions()
   210  	if newTagHoldsClientHook != nil {
   211  		hookOpts, err := newTagHoldsClientHook(ctx, clientHookParams{})
   212  		if err != nil {
   213  			return nil, err
   214  		}
   215  		clientOpts = append(clientOpts, hookOpts...)
   216  	}
   217  
   218  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   219  	if err != nil {
   220  		return nil, err
   221  	}
   222  	client := TagHoldsClient{CallOptions: defaultTagHoldsCallOptions()}
   223  
   224  	c := &tagHoldsGRPCClient{
   225  		connPool:         connPool,
   226  		tagHoldsClient:   resourcemanagerpb.NewTagHoldsClient(connPool),
   227  		CallOptions:      &client.CallOptions,
   228  		operationsClient: longrunningpb.NewOperationsClient(connPool),
   229  	}
   230  	c.setGoogleClientInfo()
   231  
   232  	client.internalClient = c
   233  
   234  	client.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool))
   235  	if err != nil {
   236  		// This error "should not happen", since we are just reusing old connection pool
   237  		// and never actually need to dial.
   238  		// If this does happen, we could leak connp. However, we cannot close conn:
   239  		// If the user invoked the constructor with option.WithGRPCConn,
   240  		// we would close a connection that's still in use.
   241  		// TODO: investigate error conditions.
   242  		return nil, err
   243  	}
   244  	c.LROClient = &client.LROClient
   245  	return &client, nil
   246  }
   247  
   248  // Connection returns a connection to the API service.
   249  //
   250  // Deprecated: Connections are now pooled so this method does not always
   251  // return the same resource.
   252  func (c *tagHoldsGRPCClient) Connection() *grpc.ClientConn {
   253  	return c.connPool.Conn()
   254  }
   255  
   256  // setGoogleClientInfo sets the name and version of the application in
   257  // the `x-goog-api-client` header passed on each request. Intended for
   258  // use by Google-written clients.
   259  func (c *tagHoldsGRPCClient) setGoogleClientInfo(keyval ...string) {
   260  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   261  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
   262  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   263  }
   264  
   265  // Close closes the connection to the API service. The user should invoke this when
   266  // the client is no longer required.
   267  func (c *tagHoldsGRPCClient) Close() error {
   268  	return c.connPool.Close()
   269  }
   270  
   271  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   272  type tagHoldsRESTClient struct {
   273  	// The http endpoint to connect to.
   274  	endpoint string
   275  
   276  	// The http client.
   277  	httpClient *http.Client
   278  
   279  	// LROClient is used internally to handle long-running operations.
   280  	// It is exposed so that its CallOptions can be modified if required.
   281  	// Users should not Close this client.
   282  	LROClient **lroauto.OperationsClient
   283  
   284  	// The x-goog-* headers to be sent with each request.
   285  	xGoogHeaders []string
   286  
   287  	// Points back to the CallOptions field of the containing TagHoldsClient
   288  	CallOptions **TagHoldsCallOptions
   289  }
   290  
   291  // NewTagHoldsRESTClient creates a new tag holds rest client.
   292  //
   293  // Allow users to create and manage TagHolds for TagValues. TagHolds represent
   294  // the use of a Tag Value that is not captured by TagBindings but
   295  // should still block TagValue deletion (such as a reference in a policy
   296  // condition). This service provides isolated failure domains by cloud location
   297  // so that TagHolds can be managed in the same location as their usage.
   298  func NewTagHoldsRESTClient(ctx context.Context, opts ...option.ClientOption) (*TagHoldsClient, error) {
   299  	clientOpts := append(defaultTagHoldsRESTClientOptions(), opts...)
   300  	httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
   301  	if err != nil {
   302  		return nil, err
   303  	}
   304  
   305  	callOpts := defaultTagHoldsRESTCallOptions()
   306  	c := &tagHoldsRESTClient{
   307  		endpoint:    endpoint,
   308  		httpClient:  httpClient,
   309  		CallOptions: &callOpts,
   310  	}
   311  	c.setGoogleClientInfo()
   312  
   313  	lroOpts := []option.ClientOption{
   314  		option.WithHTTPClient(httpClient),
   315  		option.WithEndpoint(endpoint),
   316  	}
   317  	opClient, err := lroauto.NewOperationsRESTClient(ctx, lroOpts...)
   318  	if err != nil {
   319  		return nil, err
   320  	}
   321  	c.LROClient = &opClient
   322  
   323  	return &TagHoldsClient{internalClient: c, CallOptions: callOpts}, nil
   324  }
   325  
   326  func defaultTagHoldsRESTClientOptions() []option.ClientOption {
   327  	return []option.ClientOption{
   328  		internaloption.WithDefaultEndpoint("https://cloudresourcemanager.googleapis.com"),
   329  		internaloption.WithDefaultEndpointTemplate("https://cloudresourcemanager.UNIVERSE_DOMAIN"),
   330  		internaloption.WithDefaultMTLSEndpoint("https://cloudresourcemanager.mtls.googleapis.com"),
   331  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
   332  		internaloption.WithDefaultAudience("https://cloudresourcemanager.googleapis.com/"),
   333  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
   334  	}
   335  }
   336  
   337  // setGoogleClientInfo sets the name and version of the application in
   338  // the `x-goog-api-client` header passed on each request. Intended for
   339  // use by Google-written clients.
   340  func (c *tagHoldsRESTClient) setGoogleClientInfo(keyval ...string) {
   341  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   342  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN")
   343  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   344  }
   345  
   346  // Close closes the connection to the API service. The user should invoke this when
   347  // the client is no longer required.
   348  func (c *tagHoldsRESTClient) Close() error {
   349  	// Replace httpClient with nil to force cleanup.
   350  	c.httpClient = nil
   351  	return nil
   352  }
   353  
   354  // Connection returns a connection to the API service.
   355  //
   356  // Deprecated: This method always returns nil.
   357  func (c *tagHoldsRESTClient) Connection() *grpc.ClientConn {
   358  	return nil
   359  }
   360  func (c *tagHoldsGRPCClient) CreateTagHold(ctx context.Context, req *resourcemanagerpb.CreateTagHoldRequest, opts ...gax.CallOption) (*CreateTagHoldOperation, error) {
   361  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   362  
   363  	hds = append(c.xGoogHeaders, hds...)
   364  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   365  	opts = append((*c.CallOptions).CreateTagHold[0:len((*c.CallOptions).CreateTagHold):len((*c.CallOptions).CreateTagHold)], opts...)
   366  	var resp *longrunningpb.Operation
   367  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   368  		var err error
   369  		resp, err = c.tagHoldsClient.CreateTagHold(ctx, req, settings.GRPC...)
   370  		return err
   371  	}, opts...)
   372  	if err != nil {
   373  		return nil, err
   374  	}
   375  	return &CreateTagHoldOperation{
   376  		lro: longrunning.InternalNewOperation(*c.LROClient, resp),
   377  	}, nil
   378  }
   379  
   380  func (c *tagHoldsGRPCClient) DeleteTagHold(ctx context.Context, req *resourcemanagerpb.DeleteTagHoldRequest, opts ...gax.CallOption) (*DeleteTagHoldOperation, error) {
   381  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   382  
   383  	hds = append(c.xGoogHeaders, hds...)
   384  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   385  	opts = append((*c.CallOptions).DeleteTagHold[0:len((*c.CallOptions).DeleteTagHold):len((*c.CallOptions).DeleteTagHold)], opts...)
   386  	var resp *longrunningpb.Operation
   387  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   388  		var err error
   389  		resp, err = c.tagHoldsClient.DeleteTagHold(ctx, req, settings.GRPC...)
   390  		return err
   391  	}, opts...)
   392  	if err != nil {
   393  		return nil, err
   394  	}
   395  	return &DeleteTagHoldOperation{
   396  		lro: longrunning.InternalNewOperation(*c.LROClient, resp),
   397  	}, nil
   398  }
   399  
   400  func (c *tagHoldsGRPCClient) ListTagHolds(ctx context.Context, req *resourcemanagerpb.ListTagHoldsRequest, opts ...gax.CallOption) *TagHoldIterator {
   401  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   402  
   403  	hds = append(c.xGoogHeaders, hds...)
   404  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   405  	opts = append((*c.CallOptions).ListTagHolds[0:len((*c.CallOptions).ListTagHolds):len((*c.CallOptions).ListTagHolds)], opts...)
   406  	it := &TagHoldIterator{}
   407  	req = proto.Clone(req).(*resourcemanagerpb.ListTagHoldsRequest)
   408  	it.InternalFetch = func(pageSize int, pageToken string) ([]*resourcemanagerpb.TagHold, string, error) {
   409  		resp := &resourcemanagerpb.ListTagHoldsResponse{}
   410  		if pageToken != "" {
   411  			req.PageToken = pageToken
   412  		}
   413  		if pageSize > math.MaxInt32 {
   414  			req.PageSize = math.MaxInt32
   415  		} else if pageSize != 0 {
   416  			req.PageSize = int32(pageSize)
   417  		}
   418  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   419  			var err error
   420  			resp, err = c.tagHoldsClient.ListTagHolds(ctx, req, settings.GRPC...)
   421  			return err
   422  		}, opts...)
   423  		if err != nil {
   424  			return nil, "", err
   425  		}
   426  
   427  		it.Response = resp
   428  		return resp.GetTagHolds(), resp.GetNextPageToken(), nil
   429  	}
   430  	fetch := func(pageSize int, pageToken string) (string, error) {
   431  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   432  		if err != nil {
   433  			return "", err
   434  		}
   435  		it.items = append(it.items, items...)
   436  		return nextPageToken, nil
   437  	}
   438  
   439  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   440  	it.pageInfo.MaxSize = int(req.GetPageSize())
   441  	it.pageInfo.Token = req.GetPageToken()
   442  
   443  	return it
   444  }
   445  
   446  func (c *tagHoldsGRPCClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
   447  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   448  
   449  	hds = append(c.xGoogHeaders, hds...)
   450  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   451  	opts = append((*c.CallOptions).GetOperation[0:len((*c.CallOptions).GetOperation):len((*c.CallOptions).GetOperation)], opts...)
   452  	var resp *longrunningpb.Operation
   453  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   454  		var err error
   455  		resp, err = c.operationsClient.GetOperation(ctx, req, settings.GRPC...)
   456  		return err
   457  	}, opts...)
   458  	if err != nil {
   459  		return nil, err
   460  	}
   461  	return resp, nil
   462  }
   463  
   464  // CreateTagHold creates a TagHold. Returns ALREADY_EXISTS if a TagHold with the same
   465  // resource and origin exists under the same TagValue.
   466  func (c *tagHoldsRESTClient) CreateTagHold(ctx context.Context, req *resourcemanagerpb.CreateTagHoldRequest, opts ...gax.CallOption) (*CreateTagHoldOperation, error) {
   467  	m := protojson.MarshalOptions{AllowPartial: true, UseEnumNumbers: true}
   468  	body := req.GetTagHold()
   469  	jsonReq, err := m.Marshal(body)
   470  	if err != nil {
   471  		return nil, err
   472  	}
   473  
   474  	baseUrl, err := url.Parse(c.endpoint)
   475  	if err != nil {
   476  		return nil, err
   477  	}
   478  	baseUrl.Path += fmt.Sprintf("/v3/%v/tagHolds", req.GetParent())
   479  
   480  	params := url.Values{}
   481  	params.Add("$alt", "json;enum-encoding=int")
   482  	if req.GetValidateOnly() {
   483  		params.Add("validateOnly", fmt.Sprintf("%v", req.GetValidateOnly()))
   484  	}
   485  
   486  	baseUrl.RawQuery = params.Encode()
   487  
   488  	// Build HTTP headers from client and context metadata.
   489  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))}
   490  
   491  	hds = append(c.xGoogHeaders, hds...)
   492  	hds = append(hds, "Content-Type", "application/json")
   493  	headers := gax.BuildHeaders(ctx, hds...)
   494  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   495  	resp := &longrunningpb.Operation{}
   496  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   497  		if settings.Path != "" {
   498  			baseUrl.Path = settings.Path
   499  		}
   500  		httpReq, err := http.NewRequest("POST", baseUrl.String(), bytes.NewReader(jsonReq))
   501  		if err != nil {
   502  			return err
   503  		}
   504  		httpReq = httpReq.WithContext(ctx)
   505  		httpReq.Header = headers
   506  
   507  		httpRsp, err := c.httpClient.Do(httpReq)
   508  		if err != nil {
   509  			return err
   510  		}
   511  		defer httpRsp.Body.Close()
   512  
   513  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   514  			return err
   515  		}
   516  
   517  		buf, err := io.ReadAll(httpRsp.Body)
   518  		if err != nil {
   519  			return err
   520  		}
   521  
   522  		if err := unm.Unmarshal(buf, resp); err != nil {
   523  			return err
   524  		}
   525  
   526  		return nil
   527  	}, opts...)
   528  	if e != nil {
   529  		return nil, e
   530  	}
   531  
   532  	override := fmt.Sprintf("/v3/%s", resp.GetName())
   533  	return &CreateTagHoldOperation{
   534  		lro:      longrunning.InternalNewOperation(*c.LROClient, resp),
   535  		pollPath: override,
   536  	}, nil
   537  }
   538  
   539  // DeleteTagHold deletes a TagHold.
   540  func (c *tagHoldsRESTClient) DeleteTagHold(ctx context.Context, req *resourcemanagerpb.DeleteTagHoldRequest, opts ...gax.CallOption) (*DeleteTagHoldOperation, error) {
   541  	baseUrl, err := url.Parse(c.endpoint)
   542  	if err != nil {
   543  		return nil, err
   544  	}
   545  	baseUrl.Path += fmt.Sprintf("/v3/%v", req.GetName())
   546  
   547  	params := url.Values{}
   548  	params.Add("$alt", "json;enum-encoding=int")
   549  	if req.GetValidateOnly() {
   550  		params.Add("validateOnly", fmt.Sprintf("%v", req.GetValidateOnly()))
   551  	}
   552  
   553  	baseUrl.RawQuery = params.Encode()
   554  
   555  	// Build HTTP headers from client and context metadata.
   556  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   557  
   558  	hds = append(c.xGoogHeaders, hds...)
   559  	hds = append(hds, "Content-Type", "application/json")
   560  	headers := gax.BuildHeaders(ctx, hds...)
   561  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   562  	resp := &longrunningpb.Operation{}
   563  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   564  		if settings.Path != "" {
   565  			baseUrl.Path = settings.Path
   566  		}
   567  		httpReq, err := http.NewRequest("DELETE", baseUrl.String(), nil)
   568  		if err != nil {
   569  			return err
   570  		}
   571  		httpReq = httpReq.WithContext(ctx)
   572  		httpReq.Header = headers
   573  
   574  		httpRsp, err := c.httpClient.Do(httpReq)
   575  		if err != nil {
   576  			return err
   577  		}
   578  		defer httpRsp.Body.Close()
   579  
   580  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   581  			return err
   582  		}
   583  
   584  		buf, err := io.ReadAll(httpRsp.Body)
   585  		if err != nil {
   586  			return err
   587  		}
   588  
   589  		if err := unm.Unmarshal(buf, resp); err != nil {
   590  			return err
   591  		}
   592  
   593  		return nil
   594  	}, opts...)
   595  	if e != nil {
   596  		return nil, e
   597  	}
   598  
   599  	override := fmt.Sprintf("/v3/%s", resp.GetName())
   600  	return &DeleteTagHoldOperation{
   601  		lro:      longrunning.InternalNewOperation(*c.LROClient, resp),
   602  		pollPath: override,
   603  	}, nil
   604  }
   605  
   606  // ListTagHolds lists TagHolds under a TagValue.
   607  func (c *tagHoldsRESTClient) ListTagHolds(ctx context.Context, req *resourcemanagerpb.ListTagHoldsRequest, opts ...gax.CallOption) *TagHoldIterator {
   608  	it := &TagHoldIterator{}
   609  	req = proto.Clone(req).(*resourcemanagerpb.ListTagHoldsRequest)
   610  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   611  	it.InternalFetch = func(pageSize int, pageToken string) ([]*resourcemanagerpb.TagHold, string, error) {
   612  		resp := &resourcemanagerpb.ListTagHoldsResponse{}
   613  		if pageToken != "" {
   614  			req.PageToken = pageToken
   615  		}
   616  		if pageSize > math.MaxInt32 {
   617  			req.PageSize = math.MaxInt32
   618  		} else if pageSize != 0 {
   619  			req.PageSize = int32(pageSize)
   620  		}
   621  		baseUrl, err := url.Parse(c.endpoint)
   622  		if err != nil {
   623  			return nil, "", err
   624  		}
   625  		baseUrl.Path += fmt.Sprintf("/v3/%v/tagHolds", req.GetParent())
   626  
   627  		params := url.Values{}
   628  		params.Add("$alt", "json;enum-encoding=int")
   629  		if req.GetFilter() != "" {
   630  			params.Add("filter", fmt.Sprintf("%v", req.GetFilter()))
   631  		}
   632  		if req.GetPageSize() != 0 {
   633  			params.Add("pageSize", fmt.Sprintf("%v", req.GetPageSize()))
   634  		}
   635  		if req.GetPageToken() != "" {
   636  			params.Add("pageToken", fmt.Sprintf("%v", req.GetPageToken()))
   637  		}
   638  
   639  		baseUrl.RawQuery = params.Encode()
   640  
   641  		// Build HTTP headers from client and context metadata.
   642  		hds := append(c.xGoogHeaders, "Content-Type", "application/json")
   643  		headers := gax.BuildHeaders(ctx, hds...)
   644  		e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   645  			if settings.Path != "" {
   646  				baseUrl.Path = settings.Path
   647  			}
   648  			httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
   649  			if err != nil {
   650  				return err
   651  			}
   652  			httpReq.Header = headers
   653  
   654  			httpRsp, err := c.httpClient.Do(httpReq)
   655  			if err != nil {
   656  				return err
   657  			}
   658  			defer httpRsp.Body.Close()
   659  
   660  			if err = googleapi.CheckResponse(httpRsp); err != nil {
   661  				return err
   662  			}
   663  
   664  			buf, err := io.ReadAll(httpRsp.Body)
   665  			if err != nil {
   666  				return err
   667  			}
   668  
   669  			if err := unm.Unmarshal(buf, resp); err != nil {
   670  				return err
   671  			}
   672  
   673  			return nil
   674  		}, opts...)
   675  		if e != nil {
   676  			return nil, "", e
   677  		}
   678  		it.Response = resp
   679  		return resp.GetTagHolds(), resp.GetNextPageToken(), nil
   680  	}
   681  
   682  	fetch := func(pageSize int, pageToken string) (string, error) {
   683  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   684  		if err != nil {
   685  			return "", err
   686  		}
   687  		it.items = append(it.items, items...)
   688  		return nextPageToken, nil
   689  	}
   690  
   691  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   692  	it.pageInfo.MaxSize = int(req.GetPageSize())
   693  	it.pageInfo.Token = req.GetPageToken()
   694  
   695  	return it
   696  }
   697  
   698  // GetOperation is a utility method from google.longrunning.Operations.
   699  func (c *tagHoldsRESTClient) GetOperation(ctx context.Context, req *longrunningpb.GetOperationRequest, opts ...gax.CallOption) (*longrunningpb.Operation, error) {
   700  	baseUrl, err := url.Parse(c.endpoint)
   701  	if err != nil {
   702  		return nil, err
   703  	}
   704  	baseUrl.Path += fmt.Sprintf("/v3/%v", req.GetName())
   705  
   706  	params := url.Values{}
   707  	params.Add("$alt", "json;enum-encoding=int")
   708  
   709  	baseUrl.RawQuery = params.Encode()
   710  
   711  	// Build HTTP headers from client and context metadata.
   712  	hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
   713  
   714  	hds = append(c.xGoogHeaders, hds...)
   715  	hds = append(hds, "Content-Type", "application/json")
   716  	headers := gax.BuildHeaders(ctx, hds...)
   717  	opts = append((*c.CallOptions).GetOperation[0:len((*c.CallOptions).GetOperation):len((*c.CallOptions).GetOperation)], opts...)
   718  	unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
   719  	resp := &longrunningpb.Operation{}
   720  	e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   721  		if settings.Path != "" {
   722  			baseUrl.Path = settings.Path
   723  		}
   724  		httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
   725  		if err != nil {
   726  			return err
   727  		}
   728  		httpReq = httpReq.WithContext(ctx)
   729  		httpReq.Header = headers
   730  
   731  		httpRsp, err := c.httpClient.Do(httpReq)
   732  		if err != nil {
   733  			return err
   734  		}
   735  		defer httpRsp.Body.Close()
   736  
   737  		if err = googleapi.CheckResponse(httpRsp); err != nil {
   738  			return err
   739  		}
   740  
   741  		buf, err := io.ReadAll(httpRsp.Body)
   742  		if err != nil {
   743  			return err
   744  		}
   745  
   746  		if err := unm.Unmarshal(buf, resp); err != nil {
   747  			return err
   748  		}
   749  
   750  		return nil
   751  	}, opts...)
   752  	if e != nil {
   753  		return nil, e
   754  	}
   755  	return resp, nil
   756  }
   757  
   758  // CreateTagHoldOperation returns a new CreateTagHoldOperation from a given name.
   759  // The name must be that of a previously created CreateTagHoldOperation, possibly from a different process.
   760  func (c *tagHoldsGRPCClient) CreateTagHoldOperation(name string) *CreateTagHoldOperation {
   761  	return &CreateTagHoldOperation{
   762  		lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
   763  	}
   764  }
   765  
   766  // CreateTagHoldOperation returns a new CreateTagHoldOperation from a given name.
   767  // The name must be that of a previously created CreateTagHoldOperation, possibly from a different process.
   768  func (c *tagHoldsRESTClient) CreateTagHoldOperation(name string) *CreateTagHoldOperation {
   769  	override := fmt.Sprintf("/v3/%s", name)
   770  	return &CreateTagHoldOperation{
   771  		lro:      longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
   772  		pollPath: override,
   773  	}
   774  }
   775  
   776  // DeleteTagHoldOperation returns a new DeleteTagHoldOperation from a given name.
   777  // The name must be that of a previously created DeleteTagHoldOperation, possibly from a different process.
   778  func (c *tagHoldsGRPCClient) DeleteTagHoldOperation(name string) *DeleteTagHoldOperation {
   779  	return &DeleteTagHoldOperation{
   780  		lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
   781  	}
   782  }
   783  
   784  // DeleteTagHoldOperation returns a new DeleteTagHoldOperation from a given name.
   785  // The name must be that of a previously created DeleteTagHoldOperation, possibly from a different process.
   786  func (c *tagHoldsRESTClient) DeleteTagHoldOperation(name string) *DeleteTagHoldOperation {
   787  	override := fmt.Sprintf("/v3/%s", name)
   788  	return &DeleteTagHoldOperation{
   789  		lro:      longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
   790  		pollPath: override,
   791  	}
   792  }
   793  

View as plain text