...

Source file src/cloud.google.com/go/storage/control/apiv2/storage_control_client.go

Documentation: cloud.google.com/go/storage/control/apiv2

     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 control
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"math"
    23  	"net/url"
    24  	"regexp"
    25  	"strings"
    26  	"time"
    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  	controlpb "cloud.google.com/go/storage/control/apiv2/controlpb"
    32  	"github.com/google/uuid"
    33  	gax "github.com/googleapis/gax-go/v2"
    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  	"google.golang.org/grpc"
    39  	"google.golang.org/grpc/codes"
    40  	"google.golang.org/protobuf/proto"
    41  )
    42  
    43  var newStorageControlClientHook clientHook
    44  
    45  // StorageControlCallOptions contains the retry settings for each method of StorageControlClient.
    46  type StorageControlCallOptions struct {
    47  	CreateFolder        []gax.CallOption
    48  	DeleteFolder        []gax.CallOption
    49  	GetFolder           []gax.CallOption
    50  	ListFolders         []gax.CallOption
    51  	RenameFolder        []gax.CallOption
    52  	GetStorageLayout    []gax.CallOption
    53  	CreateManagedFolder []gax.CallOption
    54  	DeleteManagedFolder []gax.CallOption
    55  	GetManagedFolder    []gax.CallOption
    56  	ListManagedFolders  []gax.CallOption
    57  }
    58  
    59  func defaultStorageControlGRPCClientOptions() []option.ClientOption {
    60  	return []option.ClientOption{
    61  		internaloption.WithDefaultEndpoint("storage.googleapis.com:443"),
    62  		internaloption.WithDefaultEndpointTemplate("storage.UNIVERSE_DOMAIN:443"),
    63  		internaloption.WithDefaultMTLSEndpoint("storage.mtls.googleapis.com:443"),
    64  		internaloption.WithDefaultUniverseDomain("googleapis.com"),
    65  		internaloption.WithDefaultAudience("https://storage.googleapis.com/"),
    66  		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
    67  		internaloption.EnableJwtWithScope(),
    68  		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
    69  			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
    70  	}
    71  }
    72  
    73  func defaultStorageControlCallOptions() *StorageControlCallOptions {
    74  	return &StorageControlCallOptions{
    75  		CreateFolder: []gax.CallOption{
    76  			gax.WithTimeout(60000 * time.Millisecond),
    77  		},
    78  		DeleteFolder: []gax.CallOption{
    79  			gax.WithTimeout(60000 * time.Millisecond),
    80  		},
    81  		GetFolder: []gax.CallOption{
    82  			gax.WithTimeout(60000 * time.Millisecond),
    83  			gax.WithRetry(func() gax.Retryer {
    84  				return gax.OnCodes([]codes.Code{
    85  					codes.ResourceExhausted,
    86  					codes.Unavailable,
    87  					codes.DeadlineExceeded,
    88  					codes.Internal,
    89  					codes.Unknown,
    90  				}, gax.Backoff{
    91  					Initial:    1000 * time.Millisecond,
    92  					Max:        60000 * time.Millisecond,
    93  					Multiplier: 2.00,
    94  				})
    95  			}),
    96  		},
    97  		ListFolders: []gax.CallOption{
    98  			gax.WithTimeout(60000 * time.Millisecond),
    99  			gax.WithRetry(func() gax.Retryer {
   100  				return gax.OnCodes([]codes.Code{
   101  					codes.ResourceExhausted,
   102  					codes.Unavailable,
   103  					codes.DeadlineExceeded,
   104  					codes.Internal,
   105  					codes.Unknown,
   106  				}, gax.Backoff{
   107  					Initial:    1000 * time.Millisecond,
   108  					Max:        60000 * time.Millisecond,
   109  					Multiplier: 2.00,
   110  				})
   111  			}),
   112  		},
   113  		RenameFolder: []gax.CallOption{
   114  			gax.WithTimeout(60000 * time.Millisecond),
   115  			gax.WithRetry(func() gax.Retryer {
   116  				return gax.OnCodes([]codes.Code{
   117  					codes.ResourceExhausted,
   118  					codes.Unavailable,
   119  					codes.DeadlineExceeded,
   120  					codes.Internal,
   121  					codes.Unknown,
   122  				}, gax.Backoff{
   123  					Initial:    1000 * time.Millisecond,
   124  					Max:        60000 * time.Millisecond,
   125  					Multiplier: 2.00,
   126  				})
   127  			}),
   128  		},
   129  		GetStorageLayout: []gax.CallOption{
   130  			gax.WithTimeout(60000 * time.Millisecond),
   131  			gax.WithRetry(func() gax.Retryer {
   132  				return gax.OnCodes([]codes.Code{
   133  					codes.ResourceExhausted,
   134  					codes.Unavailable,
   135  					codes.DeadlineExceeded,
   136  					codes.Internal,
   137  					codes.Unknown,
   138  				}, gax.Backoff{
   139  					Initial:    1000 * time.Millisecond,
   140  					Max:        60000 * time.Millisecond,
   141  					Multiplier: 2.00,
   142  				})
   143  			}),
   144  		},
   145  		CreateManagedFolder: []gax.CallOption{
   146  			gax.WithTimeout(60000 * time.Millisecond),
   147  		},
   148  		DeleteManagedFolder: []gax.CallOption{
   149  			gax.WithTimeout(60000 * time.Millisecond),
   150  		},
   151  		GetManagedFolder: []gax.CallOption{
   152  			gax.WithTimeout(60000 * time.Millisecond),
   153  			gax.WithRetry(func() gax.Retryer {
   154  				return gax.OnCodes([]codes.Code{
   155  					codes.ResourceExhausted,
   156  					codes.Unavailable,
   157  					codes.DeadlineExceeded,
   158  					codes.Internal,
   159  					codes.Unknown,
   160  				}, gax.Backoff{
   161  					Initial:    1000 * time.Millisecond,
   162  					Max:        60000 * time.Millisecond,
   163  					Multiplier: 2.00,
   164  				})
   165  			}),
   166  		},
   167  		ListManagedFolders: []gax.CallOption{
   168  			gax.WithTimeout(60000 * time.Millisecond),
   169  			gax.WithRetry(func() gax.Retryer {
   170  				return gax.OnCodes([]codes.Code{
   171  					codes.ResourceExhausted,
   172  					codes.Unavailable,
   173  					codes.DeadlineExceeded,
   174  					codes.Internal,
   175  					codes.Unknown,
   176  				}, gax.Backoff{
   177  					Initial:    1000 * time.Millisecond,
   178  					Max:        60000 * time.Millisecond,
   179  					Multiplier: 2.00,
   180  				})
   181  			}),
   182  		},
   183  	}
   184  }
   185  
   186  // internalStorageControlClient is an interface that defines the methods available from Storage Control API.
   187  type internalStorageControlClient interface {
   188  	Close() error
   189  	setGoogleClientInfo(...string)
   190  	Connection() *grpc.ClientConn
   191  	CreateFolder(context.Context, *controlpb.CreateFolderRequest, ...gax.CallOption) (*controlpb.Folder, error)
   192  	DeleteFolder(context.Context, *controlpb.DeleteFolderRequest, ...gax.CallOption) error
   193  	GetFolder(context.Context, *controlpb.GetFolderRequest, ...gax.CallOption) (*controlpb.Folder, error)
   194  	ListFolders(context.Context, *controlpb.ListFoldersRequest, ...gax.CallOption) *FolderIterator
   195  	RenameFolder(context.Context, *controlpb.RenameFolderRequest, ...gax.CallOption) (*RenameFolderOperation, error)
   196  	RenameFolderOperation(name string) *RenameFolderOperation
   197  	GetStorageLayout(context.Context, *controlpb.GetStorageLayoutRequest, ...gax.CallOption) (*controlpb.StorageLayout, error)
   198  	CreateManagedFolder(context.Context, *controlpb.CreateManagedFolderRequest, ...gax.CallOption) (*controlpb.ManagedFolder, error)
   199  	DeleteManagedFolder(context.Context, *controlpb.DeleteManagedFolderRequest, ...gax.CallOption) error
   200  	GetManagedFolder(context.Context, *controlpb.GetManagedFolderRequest, ...gax.CallOption) (*controlpb.ManagedFolder, error)
   201  	ListManagedFolders(context.Context, *controlpb.ListManagedFoldersRequest, ...gax.CallOption) *ManagedFolderIterator
   202  }
   203  
   204  // StorageControlClient is a client for interacting with Storage Control API.
   205  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   206  //
   207  // StorageControl service includes selected control plane operations.
   208  type StorageControlClient struct {
   209  	// The internal transport-dependent client.
   210  	internalClient internalStorageControlClient
   211  
   212  	// The call options for this service.
   213  	CallOptions *StorageControlCallOptions
   214  
   215  	// LROClient is used internally to handle long-running operations.
   216  	// It is exposed so that its CallOptions can be modified if required.
   217  	// Users should not Close this client.
   218  	LROClient *lroauto.OperationsClient
   219  }
   220  
   221  // Wrapper methods routed to the internal client.
   222  
   223  // Close closes the connection to the API service. The user should invoke this when
   224  // the client is no longer required.
   225  func (c *StorageControlClient) Close() error {
   226  	return c.internalClient.Close()
   227  }
   228  
   229  // setGoogleClientInfo sets the name and version of the application in
   230  // the `x-goog-api-client` header passed on each request. Intended for
   231  // use by Google-written clients.
   232  func (c *StorageControlClient) setGoogleClientInfo(keyval ...string) {
   233  	c.internalClient.setGoogleClientInfo(keyval...)
   234  }
   235  
   236  // Connection returns a connection to the API service.
   237  //
   238  // Deprecated: Connections are now pooled so this method does not always
   239  // return the same resource.
   240  func (c *StorageControlClient) Connection() *grpc.ClientConn {
   241  	return c.internalClient.Connection()
   242  }
   243  
   244  // CreateFolder creates a new folder. This operation is only applicable to a hierarchical
   245  // namespace enabled bucket.
   246  // Hierarchical namespace buckets are in allowlist preview.
   247  func (c *StorageControlClient) CreateFolder(ctx context.Context, req *controlpb.CreateFolderRequest, opts ...gax.CallOption) (*controlpb.Folder, error) {
   248  	return c.internalClient.CreateFolder(ctx, req, opts...)
   249  }
   250  
   251  // DeleteFolder permanently deletes an empty folder. This operation is only applicable to a
   252  // hierarchical namespace enabled bucket.
   253  // Hierarchical namespace buckets are in allowlist preview.
   254  func (c *StorageControlClient) DeleteFolder(ctx context.Context, req *controlpb.DeleteFolderRequest, opts ...gax.CallOption) error {
   255  	return c.internalClient.DeleteFolder(ctx, req, opts...)
   256  }
   257  
   258  // GetFolder returns metadata for the specified folder. This operation is only
   259  // applicable to a hierarchical namespace enabled bucket.
   260  // Hierarchical namespace buckets are in allowlist preview.
   261  func (c *StorageControlClient) GetFolder(ctx context.Context, req *controlpb.GetFolderRequest, opts ...gax.CallOption) (*controlpb.Folder, error) {
   262  	return c.internalClient.GetFolder(ctx, req, opts...)
   263  }
   264  
   265  // ListFolders retrieves a list of folders. This operation is only applicable to a
   266  // hierarchical namespace enabled bucket.
   267  // Hierarchical namespace buckets are in allowlist preview.
   268  func (c *StorageControlClient) ListFolders(ctx context.Context, req *controlpb.ListFoldersRequest, opts ...gax.CallOption) *FolderIterator {
   269  	return c.internalClient.ListFolders(ctx, req, opts...)
   270  }
   271  
   272  // RenameFolder renames a source folder to a destination folder. This operation is only
   273  // applicable to a hierarchical namespace enabled bucket. During a rename, the
   274  // source and destination folders are locked until the long running operation
   275  // completes.
   276  // Hierarchical namespace buckets are in allowlist preview.
   277  func (c *StorageControlClient) RenameFolder(ctx context.Context, req *controlpb.RenameFolderRequest, opts ...gax.CallOption) (*RenameFolderOperation, error) {
   278  	return c.internalClient.RenameFolder(ctx, req, opts...)
   279  }
   280  
   281  // RenameFolderOperation returns a new RenameFolderOperation from a given name.
   282  // The name must be that of a previously created RenameFolderOperation, possibly from a different process.
   283  func (c *StorageControlClient) RenameFolderOperation(name string) *RenameFolderOperation {
   284  	return c.internalClient.RenameFolderOperation(name)
   285  }
   286  
   287  // GetStorageLayout returns the storage layout configuration for a given bucket.
   288  func (c *StorageControlClient) GetStorageLayout(ctx context.Context, req *controlpb.GetStorageLayoutRequest, opts ...gax.CallOption) (*controlpb.StorageLayout, error) {
   289  	return c.internalClient.GetStorageLayout(ctx, req, opts...)
   290  }
   291  
   292  // CreateManagedFolder creates a new managed folder.
   293  func (c *StorageControlClient) CreateManagedFolder(ctx context.Context, req *controlpb.CreateManagedFolderRequest, opts ...gax.CallOption) (*controlpb.ManagedFolder, error) {
   294  	return c.internalClient.CreateManagedFolder(ctx, req, opts...)
   295  }
   296  
   297  // DeleteManagedFolder permanently deletes an empty managed folder.
   298  func (c *StorageControlClient) DeleteManagedFolder(ctx context.Context, req *controlpb.DeleteManagedFolderRequest, opts ...gax.CallOption) error {
   299  	return c.internalClient.DeleteManagedFolder(ctx, req, opts...)
   300  }
   301  
   302  // GetManagedFolder returns metadata for the specified managed folder.
   303  func (c *StorageControlClient) GetManagedFolder(ctx context.Context, req *controlpb.GetManagedFolderRequest, opts ...gax.CallOption) (*controlpb.ManagedFolder, error) {
   304  	return c.internalClient.GetManagedFolder(ctx, req, opts...)
   305  }
   306  
   307  // ListManagedFolders retrieves a list of managed folders for a given bucket.
   308  func (c *StorageControlClient) ListManagedFolders(ctx context.Context, req *controlpb.ListManagedFoldersRequest, opts ...gax.CallOption) *ManagedFolderIterator {
   309  	return c.internalClient.ListManagedFolders(ctx, req, opts...)
   310  }
   311  
   312  // storageControlGRPCClient is a client for interacting with Storage Control API over gRPC transport.
   313  //
   314  // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
   315  type storageControlGRPCClient struct {
   316  	// Connection pool of gRPC connections to the service.
   317  	connPool gtransport.ConnPool
   318  
   319  	// Points back to the CallOptions field of the containing StorageControlClient
   320  	CallOptions **StorageControlCallOptions
   321  
   322  	// The gRPC API client.
   323  	storageControlClient controlpb.StorageControlClient
   324  
   325  	// LROClient is used internally to handle long-running operations.
   326  	// It is exposed so that its CallOptions can be modified if required.
   327  	// Users should not Close this client.
   328  	LROClient **lroauto.OperationsClient
   329  
   330  	// The x-goog-* metadata to be sent with each request.
   331  	xGoogHeaders []string
   332  }
   333  
   334  // NewStorageControlClient creates a new storage control client based on gRPC.
   335  // The returned client must be Closed when it is done being used to clean up its underlying connections.
   336  //
   337  // StorageControl service includes selected control plane operations.
   338  func NewStorageControlClient(ctx context.Context, opts ...option.ClientOption) (*StorageControlClient, error) {
   339  	clientOpts := defaultStorageControlGRPCClientOptions()
   340  	if newStorageControlClientHook != nil {
   341  		hookOpts, err := newStorageControlClientHook(ctx, clientHookParams{})
   342  		if err != nil {
   343  			return nil, err
   344  		}
   345  		clientOpts = append(clientOpts, hookOpts...)
   346  	}
   347  
   348  	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
   349  	if err != nil {
   350  		return nil, err
   351  	}
   352  	client := StorageControlClient{CallOptions: defaultStorageControlCallOptions()}
   353  
   354  	c := &storageControlGRPCClient{
   355  		connPool:             connPool,
   356  		storageControlClient: controlpb.NewStorageControlClient(connPool),
   357  		CallOptions:          &client.CallOptions,
   358  	}
   359  	c.setGoogleClientInfo()
   360  
   361  	client.internalClient = c
   362  
   363  	client.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool))
   364  	if err != nil {
   365  		// This error "should not happen", since we are just reusing old connection pool
   366  		// and never actually need to dial.
   367  		// If this does happen, we could leak connp. However, we cannot close conn:
   368  		// If the user invoked the constructor with option.WithGRPCConn,
   369  		// we would close a connection that's still in use.
   370  		// TODO: investigate error conditions.
   371  		return nil, err
   372  	}
   373  	c.LROClient = &client.LROClient
   374  	return &client, nil
   375  }
   376  
   377  // Connection returns a connection to the API service.
   378  //
   379  // Deprecated: Connections are now pooled so this method does not always
   380  // return the same resource.
   381  func (c *storageControlGRPCClient) Connection() *grpc.ClientConn {
   382  	return c.connPool.Conn()
   383  }
   384  
   385  // setGoogleClientInfo sets the name and version of the application in
   386  // the `x-goog-api-client` header passed on each request. Intended for
   387  // use by Google-written clients.
   388  func (c *storageControlGRPCClient) setGoogleClientInfo(keyval ...string) {
   389  	kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
   390  	kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "grpc", grpc.Version)
   391  	c.xGoogHeaders = []string{"x-goog-api-client", gax.XGoogHeader(kv...)}
   392  }
   393  
   394  // Close closes the connection to the API service. The user should invoke this when
   395  // the client is no longer required.
   396  func (c *storageControlGRPCClient) Close() error {
   397  	return c.connPool.Close()
   398  }
   399  
   400  func (c *storageControlGRPCClient) CreateFolder(ctx context.Context, req *controlpb.CreateFolderRequest, opts ...gax.CallOption) (*controlpb.Folder, error) {
   401  	routingHeaders := ""
   402  	routingHeadersMap := make(map[string]string)
   403  	if reg := regexp.MustCompile("(?P<bucket>.*)"); reg.MatchString(req.GetParent()) && len(url.QueryEscape(reg.FindStringSubmatch(req.GetParent())[1])) > 0 {
   404  		routingHeadersMap["bucket"] = url.QueryEscape(reg.FindStringSubmatch(req.GetParent())[1])
   405  	}
   406  	for headerName, headerValue := range routingHeadersMap {
   407  		routingHeaders = fmt.Sprintf("%s%s=%s&", routingHeaders, headerName, headerValue)
   408  	}
   409  	routingHeaders = strings.TrimSuffix(routingHeaders, "&")
   410  	hds := []string{"x-goog-request-params", routingHeaders}
   411  
   412  	hds = append(c.xGoogHeaders, hds...)
   413  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   414  	if req != nil && req.GetRequestId() == "" {
   415  		req.RequestId = uuid.NewString()
   416  	}
   417  	opts = append((*c.CallOptions).CreateFolder[0:len((*c.CallOptions).CreateFolder):len((*c.CallOptions).CreateFolder)], opts...)
   418  	var resp *controlpb.Folder
   419  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   420  		var err error
   421  		resp, err = c.storageControlClient.CreateFolder(ctx, req, settings.GRPC...)
   422  		return err
   423  	}, opts...)
   424  	if err != nil {
   425  		return nil, err
   426  	}
   427  	return resp, nil
   428  }
   429  
   430  func (c *storageControlGRPCClient) DeleteFolder(ctx context.Context, req *controlpb.DeleteFolderRequest, opts ...gax.CallOption) error {
   431  	routingHeaders := ""
   432  	routingHeadersMap := make(map[string]string)
   433  	if reg := regexp.MustCompile("(?P<bucket>projects/[^/]+/buckets/[^/]+)(?:/.*)?"); reg.MatchString(req.GetName()) && len(url.QueryEscape(reg.FindStringSubmatch(req.GetName())[1])) > 0 {
   434  		routingHeadersMap["bucket"] = url.QueryEscape(reg.FindStringSubmatch(req.GetName())[1])
   435  	}
   436  	for headerName, headerValue := range routingHeadersMap {
   437  		routingHeaders = fmt.Sprintf("%s%s=%s&", routingHeaders, headerName, headerValue)
   438  	}
   439  	routingHeaders = strings.TrimSuffix(routingHeaders, "&")
   440  	hds := []string{"x-goog-request-params", routingHeaders}
   441  
   442  	hds = append(c.xGoogHeaders, hds...)
   443  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   444  	if req != nil && req.GetRequestId() == "" {
   445  		req.RequestId = uuid.NewString()
   446  	}
   447  	opts = append((*c.CallOptions).DeleteFolder[0:len((*c.CallOptions).DeleteFolder):len((*c.CallOptions).DeleteFolder)], opts...)
   448  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   449  		var err error
   450  		_, err = c.storageControlClient.DeleteFolder(ctx, req, settings.GRPC...)
   451  		return err
   452  	}, opts...)
   453  	return err
   454  }
   455  
   456  func (c *storageControlGRPCClient) GetFolder(ctx context.Context, req *controlpb.GetFolderRequest, opts ...gax.CallOption) (*controlpb.Folder, error) {
   457  	routingHeaders := ""
   458  	routingHeadersMap := make(map[string]string)
   459  	if reg := regexp.MustCompile("(?P<bucket>projects/[^/]+/buckets/[^/]+)(?:/.*)?"); reg.MatchString(req.GetName()) && len(url.QueryEscape(reg.FindStringSubmatch(req.GetName())[1])) > 0 {
   460  		routingHeadersMap["bucket"] = url.QueryEscape(reg.FindStringSubmatch(req.GetName())[1])
   461  	}
   462  	for headerName, headerValue := range routingHeadersMap {
   463  		routingHeaders = fmt.Sprintf("%s%s=%s&", routingHeaders, headerName, headerValue)
   464  	}
   465  	routingHeaders = strings.TrimSuffix(routingHeaders, "&")
   466  	hds := []string{"x-goog-request-params", routingHeaders}
   467  
   468  	hds = append(c.xGoogHeaders, hds...)
   469  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   470  	if req != nil && req.GetRequestId() == "" {
   471  		req.RequestId = uuid.NewString()
   472  	}
   473  	opts = append((*c.CallOptions).GetFolder[0:len((*c.CallOptions).GetFolder):len((*c.CallOptions).GetFolder)], opts...)
   474  	var resp *controlpb.Folder
   475  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   476  		var err error
   477  		resp, err = c.storageControlClient.GetFolder(ctx, req, settings.GRPC...)
   478  		return err
   479  	}, opts...)
   480  	if err != nil {
   481  		return nil, err
   482  	}
   483  	return resp, nil
   484  }
   485  
   486  func (c *storageControlGRPCClient) ListFolders(ctx context.Context, req *controlpb.ListFoldersRequest, opts ...gax.CallOption) *FolderIterator {
   487  	routingHeaders := ""
   488  	routingHeadersMap := make(map[string]string)
   489  	if reg := regexp.MustCompile("(?P<bucket>.*)"); reg.MatchString(req.GetParent()) && len(url.QueryEscape(reg.FindStringSubmatch(req.GetParent())[1])) > 0 {
   490  		routingHeadersMap["bucket"] = url.QueryEscape(reg.FindStringSubmatch(req.GetParent())[1])
   491  	}
   492  	for headerName, headerValue := range routingHeadersMap {
   493  		routingHeaders = fmt.Sprintf("%s%s=%s&", routingHeaders, headerName, headerValue)
   494  	}
   495  	routingHeaders = strings.TrimSuffix(routingHeaders, "&")
   496  	hds := []string{"x-goog-request-params", routingHeaders}
   497  
   498  	hds = append(c.xGoogHeaders, hds...)
   499  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   500  	opts = append((*c.CallOptions).ListFolders[0:len((*c.CallOptions).ListFolders):len((*c.CallOptions).ListFolders)], opts...)
   501  	it := &FolderIterator{}
   502  	req = proto.Clone(req).(*controlpb.ListFoldersRequest)
   503  	it.InternalFetch = func(pageSize int, pageToken string) ([]*controlpb.Folder, string, error) {
   504  		resp := &controlpb.ListFoldersResponse{}
   505  		if pageToken != "" {
   506  			req.PageToken = pageToken
   507  		}
   508  		if pageSize > math.MaxInt32 {
   509  			req.PageSize = math.MaxInt32
   510  		} else if pageSize != 0 {
   511  			req.PageSize = int32(pageSize)
   512  		}
   513  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   514  			var err error
   515  			resp, err = c.storageControlClient.ListFolders(ctx, req, settings.GRPC...)
   516  			return err
   517  		}, opts...)
   518  		if err != nil {
   519  			return nil, "", err
   520  		}
   521  
   522  		it.Response = resp
   523  		return resp.GetFolders(), resp.GetNextPageToken(), nil
   524  	}
   525  	fetch := func(pageSize int, pageToken string) (string, error) {
   526  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   527  		if err != nil {
   528  			return "", err
   529  		}
   530  		it.items = append(it.items, items...)
   531  		return nextPageToken, nil
   532  	}
   533  
   534  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   535  	it.pageInfo.MaxSize = int(req.GetPageSize())
   536  	it.pageInfo.Token = req.GetPageToken()
   537  
   538  	return it
   539  }
   540  
   541  func (c *storageControlGRPCClient) RenameFolder(ctx context.Context, req *controlpb.RenameFolderRequest, opts ...gax.CallOption) (*RenameFolderOperation, error) {
   542  	routingHeaders := ""
   543  	routingHeadersMap := make(map[string]string)
   544  	if reg := regexp.MustCompile("(?P<bucket>projects/[^/]+/buckets/[^/]+)(?:/.*)?"); reg.MatchString(req.GetName()) && len(url.QueryEscape(reg.FindStringSubmatch(req.GetName())[1])) > 0 {
   545  		routingHeadersMap["bucket"] = url.QueryEscape(reg.FindStringSubmatch(req.GetName())[1])
   546  	}
   547  	for headerName, headerValue := range routingHeadersMap {
   548  		routingHeaders = fmt.Sprintf("%s%s=%s&", routingHeaders, headerName, headerValue)
   549  	}
   550  	routingHeaders = strings.TrimSuffix(routingHeaders, "&")
   551  	hds := []string{"x-goog-request-params", routingHeaders}
   552  
   553  	hds = append(c.xGoogHeaders, hds...)
   554  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   555  	opts = append((*c.CallOptions).RenameFolder[0:len((*c.CallOptions).RenameFolder):len((*c.CallOptions).RenameFolder)], opts...)
   556  	var resp *longrunningpb.Operation
   557  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   558  		var err error
   559  		resp, err = c.storageControlClient.RenameFolder(ctx, req, settings.GRPC...)
   560  		return err
   561  	}, opts...)
   562  	if err != nil {
   563  		return nil, err
   564  	}
   565  	return &RenameFolderOperation{
   566  		lro: longrunning.InternalNewOperation(*c.LROClient, resp),
   567  	}, nil
   568  }
   569  
   570  func (c *storageControlGRPCClient) GetStorageLayout(ctx context.Context, req *controlpb.GetStorageLayoutRequest, opts ...gax.CallOption) (*controlpb.StorageLayout, error) {
   571  	routingHeaders := ""
   572  	routingHeadersMap := make(map[string]string)
   573  	if reg := regexp.MustCompile("(?P<bucket>projects/[^/]+/buckets/[^/]+)(?:/.*)?"); reg.MatchString(req.GetName()) && len(url.QueryEscape(reg.FindStringSubmatch(req.GetName())[1])) > 0 {
   574  		routingHeadersMap["bucket"] = url.QueryEscape(reg.FindStringSubmatch(req.GetName())[1])
   575  	}
   576  	for headerName, headerValue := range routingHeadersMap {
   577  		routingHeaders = fmt.Sprintf("%s%s=%s&", routingHeaders, headerName, headerValue)
   578  	}
   579  	routingHeaders = strings.TrimSuffix(routingHeaders, "&")
   580  	hds := []string{"x-goog-request-params", routingHeaders}
   581  
   582  	hds = append(c.xGoogHeaders, hds...)
   583  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   584  	if req != nil && req.GetRequestId() == "" {
   585  		req.RequestId = uuid.NewString()
   586  	}
   587  	opts = append((*c.CallOptions).GetStorageLayout[0:len((*c.CallOptions).GetStorageLayout):len((*c.CallOptions).GetStorageLayout)], opts...)
   588  	var resp *controlpb.StorageLayout
   589  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   590  		var err error
   591  		resp, err = c.storageControlClient.GetStorageLayout(ctx, req, settings.GRPC...)
   592  		return err
   593  	}, opts...)
   594  	if err != nil {
   595  		return nil, err
   596  	}
   597  	return resp, nil
   598  }
   599  
   600  func (c *storageControlGRPCClient) CreateManagedFolder(ctx context.Context, req *controlpb.CreateManagedFolderRequest, opts ...gax.CallOption) (*controlpb.ManagedFolder, error) {
   601  	routingHeaders := ""
   602  	routingHeadersMap := make(map[string]string)
   603  	if reg := regexp.MustCompile("(?P<bucket>.*)"); reg.MatchString(req.GetParent()) && len(url.QueryEscape(reg.FindStringSubmatch(req.GetParent())[1])) > 0 {
   604  		routingHeadersMap["bucket"] = url.QueryEscape(reg.FindStringSubmatch(req.GetParent())[1])
   605  	}
   606  	for headerName, headerValue := range routingHeadersMap {
   607  		routingHeaders = fmt.Sprintf("%s%s=%s&", routingHeaders, headerName, headerValue)
   608  	}
   609  	routingHeaders = strings.TrimSuffix(routingHeaders, "&")
   610  	hds := []string{"x-goog-request-params", routingHeaders}
   611  
   612  	hds = append(c.xGoogHeaders, hds...)
   613  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   614  	if req != nil && req.GetRequestId() == "" {
   615  		req.RequestId = uuid.NewString()
   616  	}
   617  	opts = append((*c.CallOptions).CreateManagedFolder[0:len((*c.CallOptions).CreateManagedFolder):len((*c.CallOptions).CreateManagedFolder)], opts...)
   618  	var resp *controlpb.ManagedFolder
   619  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   620  		var err error
   621  		resp, err = c.storageControlClient.CreateManagedFolder(ctx, req, settings.GRPC...)
   622  		return err
   623  	}, opts...)
   624  	if err != nil {
   625  		return nil, err
   626  	}
   627  	return resp, nil
   628  }
   629  
   630  func (c *storageControlGRPCClient) DeleteManagedFolder(ctx context.Context, req *controlpb.DeleteManagedFolderRequest, opts ...gax.CallOption) error {
   631  	routingHeaders := ""
   632  	routingHeadersMap := make(map[string]string)
   633  	if reg := regexp.MustCompile("(?P<bucket>projects/[^/]+/buckets/[^/]+)(?:/.*)?"); reg.MatchString(req.GetName()) && len(url.QueryEscape(reg.FindStringSubmatch(req.GetName())[1])) > 0 {
   634  		routingHeadersMap["bucket"] = url.QueryEscape(reg.FindStringSubmatch(req.GetName())[1])
   635  	}
   636  	for headerName, headerValue := range routingHeadersMap {
   637  		routingHeaders = fmt.Sprintf("%s%s=%s&", routingHeaders, headerName, headerValue)
   638  	}
   639  	routingHeaders = strings.TrimSuffix(routingHeaders, "&")
   640  	hds := []string{"x-goog-request-params", routingHeaders}
   641  
   642  	hds = append(c.xGoogHeaders, hds...)
   643  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   644  	if req != nil && req.GetRequestId() == "" {
   645  		req.RequestId = uuid.NewString()
   646  	}
   647  	opts = append((*c.CallOptions).DeleteManagedFolder[0:len((*c.CallOptions).DeleteManagedFolder):len((*c.CallOptions).DeleteManagedFolder)], opts...)
   648  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   649  		var err error
   650  		_, err = c.storageControlClient.DeleteManagedFolder(ctx, req, settings.GRPC...)
   651  		return err
   652  	}, opts...)
   653  	return err
   654  }
   655  
   656  func (c *storageControlGRPCClient) GetManagedFolder(ctx context.Context, req *controlpb.GetManagedFolderRequest, opts ...gax.CallOption) (*controlpb.ManagedFolder, error) {
   657  	routingHeaders := ""
   658  	routingHeadersMap := make(map[string]string)
   659  	if reg := regexp.MustCompile("(?P<bucket>projects/[^/]+/buckets/[^/]+)(?:/.*)?"); reg.MatchString(req.GetName()) && len(url.QueryEscape(reg.FindStringSubmatch(req.GetName())[1])) > 0 {
   660  		routingHeadersMap["bucket"] = url.QueryEscape(reg.FindStringSubmatch(req.GetName())[1])
   661  	}
   662  	for headerName, headerValue := range routingHeadersMap {
   663  		routingHeaders = fmt.Sprintf("%s%s=%s&", routingHeaders, headerName, headerValue)
   664  	}
   665  	routingHeaders = strings.TrimSuffix(routingHeaders, "&")
   666  	hds := []string{"x-goog-request-params", routingHeaders}
   667  
   668  	hds = append(c.xGoogHeaders, hds...)
   669  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   670  	if req != nil && req.GetRequestId() == "" {
   671  		req.RequestId = uuid.NewString()
   672  	}
   673  	opts = append((*c.CallOptions).GetManagedFolder[0:len((*c.CallOptions).GetManagedFolder):len((*c.CallOptions).GetManagedFolder)], opts...)
   674  	var resp *controlpb.ManagedFolder
   675  	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   676  		var err error
   677  		resp, err = c.storageControlClient.GetManagedFolder(ctx, req, settings.GRPC...)
   678  		return err
   679  	}, opts...)
   680  	if err != nil {
   681  		return nil, err
   682  	}
   683  	return resp, nil
   684  }
   685  
   686  func (c *storageControlGRPCClient) ListManagedFolders(ctx context.Context, req *controlpb.ListManagedFoldersRequest, opts ...gax.CallOption) *ManagedFolderIterator {
   687  	routingHeaders := ""
   688  	routingHeadersMap := make(map[string]string)
   689  	if reg := regexp.MustCompile("(?P<bucket>.*)"); reg.MatchString(req.GetParent()) && len(url.QueryEscape(reg.FindStringSubmatch(req.GetParent())[1])) > 0 {
   690  		routingHeadersMap["bucket"] = url.QueryEscape(reg.FindStringSubmatch(req.GetParent())[1])
   691  	}
   692  	for headerName, headerValue := range routingHeadersMap {
   693  		routingHeaders = fmt.Sprintf("%s%s=%s&", routingHeaders, headerName, headerValue)
   694  	}
   695  	routingHeaders = strings.TrimSuffix(routingHeaders, "&")
   696  	hds := []string{"x-goog-request-params", routingHeaders}
   697  
   698  	hds = append(c.xGoogHeaders, hds...)
   699  	ctx = gax.InsertMetadataIntoOutgoingContext(ctx, hds...)
   700  	opts = append((*c.CallOptions).ListManagedFolders[0:len((*c.CallOptions).ListManagedFolders):len((*c.CallOptions).ListManagedFolders)], opts...)
   701  	it := &ManagedFolderIterator{}
   702  	req = proto.Clone(req).(*controlpb.ListManagedFoldersRequest)
   703  	it.InternalFetch = func(pageSize int, pageToken string) ([]*controlpb.ManagedFolder, string, error) {
   704  		resp := &controlpb.ListManagedFoldersResponse{}
   705  		if pageToken != "" {
   706  			req.PageToken = pageToken
   707  		}
   708  		if pageSize > math.MaxInt32 {
   709  			req.PageSize = math.MaxInt32
   710  		} else if pageSize != 0 {
   711  			req.PageSize = int32(pageSize)
   712  		}
   713  		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
   714  			var err error
   715  			resp, err = c.storageControlClient.ListManagedFolders(ctx, req, settings.GRPC...)
   716  			return err
   717  		}, opts...)
   718  		if err != nil {
   719  			return nil, "", err
   720  		}
   721  
   722  		it.Response = resp
   723  		return resp.GetManagedFolders(), resp.GetNextPageToken(), nil
   724  	}
   725  	fetch := func(pageSize int, pageToken string) (string, error) {
   726  		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
   727  		if err != nil {
   728  			return "", err
   729  		}
   730  		it.items = append(it.items, items...)
   731  		return nextPageToken, nil
   732  	}
   733  
   734  	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
   735  	it.pageInfo.MaxSize = int(req.GetPageSize())
   736  	it.pageInfo.Token = req.GetPageToken()
   737  
   738  	return it
   739  }
   740  
   741  // RenameFolderOperation returns a new RenameFolderOperation from a given name.
   742  // The name must be that of a previously created RenameFolderOperation, possibly from a different process.
   743  func (c *storageControlGRPCClient) RenameFolderOperation(name string) *RenameFolderOperation {
   744  	return &RenameFolderOperation{
   745  		lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}),
   746  	}
   747  }
   748  

View as plain text