...

Source file src/cloud.google.com/go/resourcemanager/apiv2/auxiliary.go

Documentation: cloud.google.com/go/resourcemanager/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 resourcemanager
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"cloud.google.com/go/longrunning"
    24  	resourcemanagerpb "cloud.google.com/go/resourcemanager/apiv2/resourcemanagerpb"
    25  	gax "github.com/googleapis/gax-go/v2"
    26  	"google.golang.org/api/iterator"
    27  )
    28  
    29  // CreateFolderOperation manages a long-running operation from CreateFolder.
    30  type CreateFolderOperation struct {
    31  	lro      *longrunning.Operation
    32  	pollPath string
    33  }
    34  
    35  // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
    36  //
    37  // See documentation of Poll for error-handling information.
    38  func (op *CreateFolderOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*resourcemanagerpb.Folder, error) {
    39  	opts = append([]gax.CallOption{gax.WithPath(op.pollPath)}, opts...)
    40  	var resp resourcemanagerpb.Folder
    41  	if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil {
    42  		return nil, err
    43  	}
    44  	return &resp, nil
    45  }
    46  
    47  // Poll fetches the latest state of the long-running operation.
    48  //
    49  // Poll also fetches the latest metadata, which can be retrieved by Metadata.
    50  //
    51  // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
    52  // the operation has completed with failure, the error is returned and op.Done will return true.
    53  // If Poll succeeds and the operation has completed successfully,
    54  // op.Done will return true, and the response of the operation is returned.
    55  // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
    56  func (op *CreateFolderOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*resourcemanagerpb.Folder, error) {
    57  	opts = append([]gax.CallOption{gax.WithPath(op.pollPath)}, opts...)
    58  	var resp resourcemanagerpb.Folder
    59  	if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
    60  		return nil, err
    61  	}
    62  	if !op.Done() {
    63  		return nil, nil
    64  	}
    65  	return &resp, nil
    66  }
    67  
    68  // Metadata returns metadata associated with the long-running operation.
    69  // Metadata itself does not contact the server, but Poll does.
    70  // To get the latest metadata, call this method after a successful call to Poll.
    71  // If the metadata is not available, the returned metadata and error are both nil.
    72  func (op *CreateFolderOperation) Metadata() (*resourcemanagerpb.FolderOperation, error) {
    73  	var meta resourcemanagerpb.FolderOperation
    74  	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
    75  		return nil, nil
    76  	} else if err != nil {
    77  		return nil, err
    78  	}
    79  	return &meta, nil
    80  }
    81  
    82  // Done reports whether the long-running operation has completed.
    83  func (op *CreateFolderOperation) Done() bool {
    84  	return op.lro.Done()
    85  }
    86  
    87  // Name returns the name of the long-running operation.
    88  // The name is assigned by the server and is unique within the service from which the operation is created.
    89  func (op *CreateFolderOperation) Name() string {
    90  	return op.lro.Name()
    91  }
    92  
    93  // MoveFolderOperation manages a long-running operation from MoveFolder.
    94  type MoveFolderOperation struct {
    95  	lro      *longrunning.Operation
    96  	pollPath string
    97  }
    98  
    99  // Wait blocks until the long-running operation is completed, returning the response and any errors encountered.
   100  //
   101  // See documentation of Poll for error-handling information.
   102  func (op *MoveFolderOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*resourcemanagerpb.Folder, error) {
   103  	opts = append([]gax.CallOption{gax.WithPath(op.pollPath)}, opts...)
   104  	var resp resourcemanagerpb.Folder
   105  	if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil {
   106  		return nil, err
   107  	}
   108  	return &resp, nil
   109  }
   110  
   111  // Poll fetches the latest state of the long-running operation.
   112  //
   113  // Poll also fetches the latest metadata, which can be retrieved by Metadata.
   114  //
   115  // If Poll fails, the error is returned and op is unmodified. If Poll succeeds and
   116  // the operation has completed with failure, the error is returned and op.Done will return true.
   117  // If Poll succeeds and the operation has completed successfully,
   118  // op.Done will return true, and the response of the operation is returned.
   119  // If Poll succeeds and the operation has not completed, the returned response and error are both nil.
   120  func (op *MoveFolderOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*resourcemanagerpb.Folder, error) {
   121  	opts = append([]gax.CallOption{gax.WithPath(op.pollPath)}, opts...)
   122  	var resp resourcemanagerpb.Folder
   123  	if err := op.lro.Poll(ctx, &resp, opts...); err != nil {
   124  		return nil, err
   125  	}
   126  	if !op.Done() {
   127  		return nil, nil
   128  	}
   129  	return &resp, nil
   130  }
   131  
   132  // Metadata returns metadata associated with the long-running operation.
   133  // Metadata itself does not contact the server, but Poll does.
   134  // To get the latest metadata, call this method after a successful call to Poll.
   135  // If the metadata is not available, the returned metadata and error are both nil.
   136  func (op *MoveFolderOperation) Metadata() (*resourcemanagerpb.FolderOperation, error) {
   137  	var meta resourcemanagerpb.FolderOperation
   138  	if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata {
   139  		return nil, nil
   140  	} else if err != nil {
   141  		return nil, err
   142  	}
   143  	return &meta, nil
   144  }
   145  
   146  // Done reports whether the long-running operation has completed.
   147  func (op *MoveFolderOperation) Done() bool {
   148  	return op.lro.Done()
   149  }
   150  
   151  // Name returns the name of the long-running operation.
   152  // The name is assigned by the server and is unique within the service from which the operation is created.
   153  func (op *MoveFolderOperation) Name() string {
   154  	return op.lro.Name()
   155  }
   156  
   157  // FolderIterator manages a stream of *resourcemanagerpb.Folder.
   158  type FolderIterator struct {
   159  	items    []*resourcemanagerpb.Folder
   160  	pageInfo *iterator.PageInfo
   161  	nextFunc func() error
   162  
   163  	// Response is the raw response for the current page.
   164  	// It must be cast to the RPC response type.
   165  	// Calling Next() or InternalFetch() updates this value.
   166  	Response interface{}
   167  
   168  	// InternalFetch is for use by the Google Cloud Libraries only.
   169  	// It is not part of the stable interface of this package.
   170  	//
   171  	// InternalFetch returns results from a single call to the underlying RPC.
   172  	// The number of results is no greater than pageSize.
   173  	// If there are no more results, nextPageToken is empty and err is nil.
   174  	InternalFetch func(pageSize int, pageToken string) (results []*resourcemanagerpb.Folder, nextPageToken string, err error)
   175  }
   176  
   177  // PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
   178  func (it *FolderIterator) PageInfo() *iterator.PageInfo {
   179  	return it.pageInfo
   180  }
   181  
   182  // Next returns the next result. Its second return value is iterator.Done if there are no more
   183  // results. Once Next returns Done, all subsequent calls will return Done.
   184  func (it *FolderIterator) Next() (*resourcemanagerpb.Folder, error) {
   185  	var item *resourcemanagerpb.Folder
   186  	if err := it.nextFunc(); err != nil {
   187  		return item, err
   188  	}
   189  	item = it.items[0]
   190  	it.items = it.items[1:]
   191  	return item, nil
   192  }
   193  
   194  func (it *FolderIterator) bufLen() int {
   195  	return len(it.items)
   196  }
   197  
   198  func (it *FolderIterator) takeBuf() interface{} {
   199  	b := it.items
   200  	it.items = nil
   201  	return b
   202  }
   203  

View as plain text