diff --git a/internal/.repo-metadata-full.json b/internal/.repo-metadata-full.json index 70d9be08d77..ebe2876f351 100644 --- a/internal/.repo-metadata-full.json +++ b/internal/.repo-metadata-full.json @@ -1313,6 +1313,15 @@ "release_level": "alpha", "library_type": "" }, + "cloud.google.com/go/storagetransfer/apiv1": { + "distribution_name": "cloud.google.com/go/storagetransfer/apiv1", + "description": "Storage Transfer API", + "language": "Go", + "client_library_type": "generated", + "docs_url": "https://cloud.google.com/go/docs/reference/cloud.google.com/go/latest/storagetransfer/apiv1", + "release_level": "beta", + "library_type": "" + }, "cloud.google.com/go/talent/apiv4": { "distribution_name": "cloud.google.com/go/talent/apiv4", "description": "Cloud Talent Solution API", diff --git a/internal/gapicgen/execv/command.go b/internal/gapicgen/execv/command.go index 7a81fa8bf7e..e7b6a5d77d2 100644 --- a/internal/gapicgen/execv/command.go +++ b/internal/gapicgen/execv/command.go @@ -41,13 +41,7 @@ func Command(name string, arg ...string) *CmdWrapper { // Run a command. func (c *CmdWrapper) Run() error { - log.Printf("[%s] >>>> %v <<<<", c.Dir, strings.Join(c.Args, " ")) - err := c.Cmd.Run() - if err != nil { - if ee, ok := err.(*exec.ExitError); ok { - log.Println(string(ee.Stderr)) - } - } + _, err := c.Output() return err } diff --git a/internal/gapicgen/generator/config.go b/internal/gapicgen/generator/config.go index c20cbc243df..5d5f843eeab 100644 --- a/internal/gapicgen/generator/config.go +++ b/internal/gapicgen/generator/config.go @@ -1242,6 +1242,15 @@ var microgenGapicConfigs = []*microgenConfig{ // GA after 2021/08/22 releaseLevel: "beta", }, + { + inputDirectoryPath: "google/storagetransfer/v1", + pkg: "storagetransfer", + importPath: "cloud.google.com/go/storagetransfer/apiv1", + gRPCServiceConfigPath: "storagetransfer_grpc_service_config.json", + apiServiceConfigPath: "storagetransfer_v1.yaml", + // GA after 2021/08/27 + releaseLevel: "beta", + }, // Non-Cloud APIs { diff --git a/storagetransfer/apiv1/doc.go b/storagetransfer/apiv1/doc.go new file mode 100644 index 00000000000..5fd03b052c9 --- /dev/null +++ b/storagetransfer/apiv1/doc.go @@ -0,0 +1,119 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +// Package storagetransfer is an auto-generated package for the +// Storage Transfer API. +// +// Transfers data from external data sources to a Google Cloud Storage bucket +// or between Google Cloud Storage buckets. +// +// NOTE: This package is in beta. It is not stable, and may be subject to changes. +// +// Use of Context +// +// The ctx passed to NewClient is used for authentication requests and +// for creating the underlying connection, but is not used for subsequent calls. +// Individual methods on the client use the ctx given to them. +// +// To close the open connection, use the Close() method. +// +// For information about setting deadlines, reusing contexts, and more +// please visit https://pkg.go.dev/cloud.google.com/go. +package storagetransfer // import "cloud.google.com/go/storagetransfer/apiv1" + +import ( + "context" + "os" + "runtime" + "strconv" + "strings" + "unicode" + + "google.golang.org/api/option" + "google.golang.org/grpc/metadata" +) + +// For more information on implementing a client constructor hook, see +// https://github.com/googleapis/google-cloud-go/wiki/Customizing-constructors. +type clientHookParams struct{} +type clientHook func(context.Context, clientHookParams) ([]option.ClientOption, error) + +const versionClient = "20210727" + +func insertMetadata(ctx context.Context, mds ...metadata.MD) context.Context { + out, _ := metadata.FromOutgoingContext(ctx) + out = out.Copy() + for _, md := range mds { + for k, v := range md { + out[k] = append(out[k], v...) + } + } + return metadata.NewOutgoingContext(ctx, out) +} + +func checkDisableDeadlines() (bool, error) { + raw, ok := os.LookupEnv("GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE") + if !ok { + return false, nil + } + + b, err := strconv.ParseBool(raw) + return b, err +} + +// DefaultAuthScopes reports the default set of authentication scopes to use with this package. +func DefaultAuthScopes() []string { + return []string{ + "https://www.googleapis.com/auth/cloud-platform", + } +} + +// versionGo returns the Go runtime version. The returned string +// has no whitespace, suitable for reporting in header. +func versionGo() string { + const develPrefix = "devel +" + + s := runtime.Version() + if strings.HasPrefix(s, develPrefix) { + s = s[len(develPrefix):] + if p := strings.IndexFunc(s, unicode.IsSpace); p >= 0 { + s = s[:p] + } + return s + } + + notSemverRune := func(r rune) bool { + return !strings.ContainsRune("0123456789.", r) + } + + if strings.HasPrefix(s, "go1") { + s = s[2:] + var prerelease string + if p := strings.IndexFunc(s, notSemverRune); p >= 0 { + s, prerelease = s[:p], s[p:] + } + if strings.HasSuffix(s, ".") { + s += "0" + } else if strings.Count(s, ".") < 2 { + s += ".0" + } + if prerelease != "" { + s += "-" + prerelease + } + return s + } + return "UNKNOWN" +} diff --git a/storagetransfer/apiv1/gapic_metadata.json b/storagetransfer/apiv1/gapic_metadata.json new file mode 100644 index 00000000000..a02a46ffc2e --- /dev/null +++ b/storagetransfer/apiv1/gapic_metadata.json @@ -0,0 +1,58 @@ +{ + "schema": "1.0", + "comment": "This file maps proto services/RPCs to the corresponding library clients/methods.", + "language": "go", + "protoPackage": "google.storagetransfer.v1", + "libraryPackage": "cloud.google.com/go/storagetransfer/apiv1", + "services": { + "StorageTransferService": { + "clients": { + "grpc": { + "libraryClient": "Client", + "rpcs": { + "CreateTransferJob": { + "methods": [ + "CreateTransferJob" + ] + }, + "GetGoogleServiceAccount": { + "methods": [ + "GetGoogleServiceAccount" + ] + }, + "GetTransferJob": { + "methods": [ + "GetTransferJob" + ] + }, + "ListTransferJobs": { + "methods": [ + "ListTransferJobs" + ] + }, + "PauseTransferOperation": { + "methods": [ + "PauseTransferOperation" + ] + }, + "ResumeTransferOperation": { + "methods": [ + "ResumeTransferOperation" + ] + }, + "RunTransferJob": { + "methods": [ + "RunTransferJob" + ] + }, + "UpdateTransferJob": { + "methods": [ + "UpdateTransferJob" + ] + } + } + } + } + } + } +} diff --git a/storagetransfer/apiv1/storage_transfer_client.go b/storagetransfer/apiv1/storage_transfer_client.go new file mode 100644 index 00000000000..338e2e99bdc --- /dev/null +++ b/storagetransfer/apiv1/storage_transfer_client.go @@ -0,0 +1,579 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +package storagetransfer + +import ( + "context" + "fmt" + "math" + "net/url" + "time" + + "cloud.google.com/go/longrunning" + lroauto "cloud.google.com/go/longrunning/autogen" + gax "github.com/googleapis/gax-go/v2" + "google.golang.org/api/iterator" + "google.golang.org/api/option" + "google.golang.org/api/option/internaloption" + gtransport "google.golang.org/api/transport/grpc" + longrunningpb "google.golang.org/genproto/googleapis/longrunning" + storagetransferpb "google.golang.org/genproto/googleapis/storagetransfer/v1" + "google.golang.org/grpc" + "google.golang.org/grpc/metadata" + "google.golang.org/protobuf/proto" +) + +var newClientHook clientHook + +// CallOptions contains the retry settings for each method of Client. +type CallOptions struct { + GetGoogleServiceAccount []gax.CallOption + CreateTransferJob []gax.CallOption + UpdateTransferJob []gax.CallOption + GetTransferJob []gax.CallOption + ListTransferJobs []gax.CallOption + PauseTransferOperation []gax.CallOption + ResumeTransferOperation []gax.CallOption + RunTransferJob []gax.CallOption +} + +func defaultGRPCClientOptions() []option.ClientOption { + return []option.ClientOption{ + internaloption.WithDefaultEndpoint("storagetransfer.googleapis.com:443"), + internaloption.WithDefaultMTLSEndpoint("storagetransfer.mtls.googleapis.com:443"), + internaloption.WithDefaultAudience("https://storagetransfer.googleapis.com/"), + internaloption.WithDefaultScopes(DefaultAuthScopes()...), + internaloption.EnableJwtWithScope(), + option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), + option.WithGRPCDialOption(grpc.WithDefaultCallOptions( + grpc.MaxCallRecvMsgSize(math.MaxInt32))), + } +} + +func defaultCallOptions() *CallOptions { + return &CallOptions{ + GetGoogleServiceAccount: []gax.CallOption{}, + CreateTransferJob: []gax.CallOption{}, + UpdateTransferJob: []gax.CallOption{}, + GetTransferJob: []gax.CallOption{}, + ListTransferJobs: []gax.CallOption{}, + PauseTransferOperation: []gax.CallOption{}, + ResumeTransferOperation: []gax.CallOption{}, + RunTransferJob: []gax.CallOption{}, + } +} + +// internalClient is an interface that defines the methods availaible from Storage Transfer API. +type internalClient interface { + Close() error + setGoogleClientInfo(...string) + Connection() *grpc.ClientConn + GetGoogleServiceAccount(context.Context, *storagetransferpb.GetGoogleServiceAccountRequest, ...gax.CallOption) (*storagetransferpb.GoogleServiceAccount, error) + CreateTransferJob(context.Context, *storagetransferpb.CreateTransferJobRequest, ...gax.CallOption) (*storagetransferpb.TransferJob, error) + UpdateTransferJob(context.Context, *storagetransferpb.UpdateTransferJobRequest, ...gax.CallOption) (*storagetransferpb.TransferJob, error) + GetTransferJob(context.Context, *storagetransferpb.GetTransferJobRequest, ...gax.CallOption) (*storagetransferpb.TransferJob, error) + ListTransferJobs(context.Context, *storagetransferpb.ListTransferJobsRequest, ...gax.CallOption) *TransferJobIterator + PauseTransferOperation(context.Context, *storagetransferpb.PauseTransferOperationRequest, ...gax.CallOption) error + ResumeTransferOperation(context.Context, *storagetransferpb.ResumeTransferOperationRequest, ...gax.CallOption) error + RunTransferJob(context.Context, *storagetransferpb.RunTransferJobRequest, ...gax.CallOption) (*RunTransferJobOperation, error) + RunTransferJobOperation(name string) *RunTransferJobOperation +} + +// Client is a client for interacting with Storage Transfer API. +// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. +// +// Storage Transfer Service and its protos. +// Transfers data between between Google Cloud Storage buckets or from a data +// source external to Google to a Cloud Storage bucket. +type Client struct { + // The internal transport-dependent client. + internalClient internalClient + + // The call options for this service. + CallOptions *CallOptions + + // LROClient is used internally to handle long-running operations. + // It is exposed so that its CallOptions can be modified if required. + // Users should not Close this client. + LROClient *lroauto.OperationsClient +} + +// Wrapper methods routed to the internal client. + +// Close closes the connection to the API service. The user should invoke this when +// the client is no longer required. +func (c *Client) Close() error { + return c.internalClient.Close() +} + +// setGoogleClientInfo sets the name and version of the application in +// the `x-goog-api-client` header passed on each request. Intended for +// use by Google-written clients. +func (c *Client) setGoogleClientInfo(keyval ...string) { + c.internalClient.setGoogleClientInfo(keyval...) +} + +// Connection returns a connection to the API service. +// +// Deprecated. +func (c *Client) Connection() *grpc.ClientConn { + return c.internalClient.Connection() +} + +// GetGoogleServiceAccount returns the Google service account that is used by Storage Transfer +// Service to access buckets in the project where transfers +// run or in other projects. Each Google service account is associated +// with one Google Cloud Platform Console project. Users +// should add this service account to the Google Cloud Storage bucket +// ACLs to grant access to Storage Transfer Service. This service +// account is created and owned by Storage Transfer Service and can +// only be used by Storage Transfer Service. +func (c *Client) GetGoogleServiceAccount(ctx context.Context, req *storagetransferpb.GetGoogleServiceAccountRequest, opts ...gax.CallOption) (*storagetransferpb.GoogleServiceAccount, error) { + return c.internalClient.GetGoogleServiceAccount(ctx, req, opts...) +} + +// CreateTransferJob creates a transfer job that runs periodically. +func (c *Client) CreateTransferJob(ctx context.Context, req *storagetransferpb.CreateTransferJobRequest, opts ...gax.CallOption) (*storagetransferpb.TransferJob, error) { + return c.internalClient.CreateTransferJob(ctx, req, opts...) +} + +// UpdateTransferJob updates a transfer job. Updating a job’s transfer spec does not affect +// transfer operations that are running already. +// +// Note: The job’s status +// field can be modified using this RPC (for example, to set a job’s status to +// DELETED, +// DISABLED, or +// ENABLED). +func (c *Client) UpdateTransferJob(ctx context.Context, req *storagetransferpb.UpdateTransferJobRequest, opts ...gax.CallOption) (*storagetransferpb.TransferJob, error) { + return c.internalClient.UpdateTransferJob(ctx, req, opts...) +} + +// GetTransferJob gets a transfer job. +func (c *Client) GetTransferJob(ctx context.Context, req *storagetransferpb.GetTransferJobRequest, opts ...gax.CallOption) (*storagetransferpb.TransferJob, error) { + return c.internalClient.GetTransferJob(ctx, req, opts...) +} + +// ListTransferJobs lists transfer jobs. +func (c *Client) ListTransferJobs(ctx context.Context, req *storagetransferpb.ListTransferJobsRequest, opts ...gax.CallOption) *TransferJobIterator { + return c.internalClient.ListTransferJobs(ctx, req, opts...) +} + +// PauseTransferOperation pauses a transfer operation. +func (c *Client) PauseTransferOperation(ctx context.Context, req *storagetransferpb.PauseTransferOperationRequest, opts ...gax.CallOption) error { + return c.internalClient.PauseTransferOperation(ctx, req, opts...) +} + +// ResumeTransferOperation resumes a transfer operation that is paused. +func (c *Client) ResumeTransferOperation(ctx context.Context, req *storagetransferpb.ResumeTransferOperationRequest, opts ...gax.CallOption) error { + return c.internalClient.ResumeTransferOperation(ctx, req, opts...) +} + +// RunTransferJob attempts to start a new TransferOperation for the current TransferJob. A +// TransferJob has a maximum of one active TransferOperation. If this method +// is called while a TransferOperation is active, an error wil be returned. +func (c *Client) RunTransferJob(ctx context.Context, req *storagetransferpb.RunTransferJobRequest, opts ...gax.CallOption) (*RunTransferJobOperation, error) { + return c.internalClient.RunTransferJob(ctx, req, opts...) +} + +// RunTransferJobOperation returns a new RunTransferJobOperation from a given name. +// The name must be that of a previously created RunTransferJobOperation, possibly from a different process. +func (c *Client) RunTransferJobOperation(name string) *RunTransferJobOperation { + return c.internalClient.RunTransferJobOperation(name) +} + +// gRPCClient is a client for interacting with Storage Transfer API over gRPC transport. +// +// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. +type gRPCClient struct { + // Connection pool of gRPC connections to the service. + connPool gtransport.ConnPool + + // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE + disableDeadlines bool + + // Points back to the CallOptions field of the containing Client + CallOptions **CallOptions + + // The gRPC API client. + client storagetransferpb.StorageTransferServiceClient + + // LROClient is used internally to handle long-running operations. + // It is exposed so that its CallOptions can be modified if required. + // Users should not Close this client. + LROClient **lroauto.OperationsClient + + // The x-goog-* metadata to be sent with each request. + xGoogMetadata metadata.MD +} + +// NewClient creates a new storage transfer service client based on gRPC. +// The returned client must be Closed when it is done being used to clean up its underlying connections. +// +// Storage Transfer Service and its protos. +// Transfers data between between Google Cloud Storage buckets or from a data +// source external to Google to a Cloud Storage bucket. +func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) { + clientOpts := defaultGRPCClientOptions() + if newClientHook != nil { + hookOpts, err := newClientHook(ctx, clientHookParams{}) + if err != nil { + return nil, err + } + clientOpts = append(clientOpts, hookOpts...) + } + + disableDeadlines, err := checkDisableDeadlines() + if err != nil { + return nil, err + } + + connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) + if err != nil { + return nil, err + } + client := Client{CallOptions: defaultCallOptions()} + + c := &gRPCClient{ + connPool: connPool, + disableDeadlines: disableDeadlines, + client: storagetransferpb.NewStorageTransferServiceClient(connPool), + CallOptions: &client.CallOptions, + } + c.setGoogleClientInfo() + + client.internalClient = c + + client.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool)) + if err != nil { + // This error "should not happen", since we are just reusing old connection pool + // and never actually need to dial. + // If this does happen, we could leak connp. However, we cannot close conn: + // If the user invoked the constructor with option.WithGRPCConn, + // we would close a connection that's still in use. + // TODO: investigate error conditions. + return nil, err + } + c.LROClient = &client.LROClient + return &client, nil +} + +// Connection returns a connection to the API service. +// +// Deprecated. +func (c *gRPCClient) Connection() *grpc.ClientConn { + return c.connPool.Conn() +} + +// setGoogleClientInfo sets the name and version of the application in +// the `x-goog-api-client` header passed on each request. Intended for +// use by Google-written clients. +func (c *gRPCClient) setGoogleClientInfo(keyval ...string) { + kv := append([]string{"gl-go", versionGo()}, keyval...) + kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) + c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) +} + +// Close closes the connection to the API service. The user should invoke this when +// the client is no longer required. +func (c *gRPCClient) Close() error { + return c.connPool.Close() +} + +func (c *gRPCClient) GetGoogleServiceAccount(ctx context.Context, req *storagetransferpb.GetGoogleServiceAccountRequest, opts ...gax.CallOption) (*storagetransferpb.GoogleServiceAccount, error) { + if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { + cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) + defer cancel() + ctx = cctx + } + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "project_id", url.QueryEscape(req.GetProjectId()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append((*c.CallOptions).GetGoogleServiceAccount[0:len((*c.CallOptions).GetGoogleServiceAccount):len((*c.CallOptions).GetGoogleServiceAccount)], opts...) + var resp *storagetransferpb.GoogleServiceAccount + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.client.GetGoogleServiceAccount(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} + +func (c *gRPCClient) CreateTransferJob(ctx context.Context, req *storagetransferpb.CreateTransferJobRequest, opts ...gax.CallOption) (*storagetransferpb.TransferJob, error) { + if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { + cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) + defer cancel() + ctx = cctx + } + ctx = insertMetadata(ctx, c.xGoogMetadata) + opts = append((*c.CallOptions).CreateTransferJob[0:len((*c.CallOptions).CreateTransferJob):len((*c.CallOptions).CreateTransferJob)], opts...) + var resp *storagetransferpb.TransferJob + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.client.CreateTransferJob(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} + +func (c *gRPCClient) UpdateTransferJob(ctx context.Context, req *storagetransferpb.UpdateTransferJobRequest, opts ...gax.CallOption) (*storagetransferpb.TransferJob, error) { + if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { + cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) + defer cancel() + ctx = cctx + } + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "job_name", url.QueryEscape(req.GetJobName()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append((*c.CallOptions).UpdateTransferJob[0:len((*c.CallOptions).UpdateTransferJob):len((*c.CallOptions).UpdateTransferJob)], opts...) + var resp *storagetransferpb.TransferJob + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.client.UpdateTransferJob(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} + +func (c *gRPCClient) GetTransferJob(ctx context.Context, req *storagetransferpb.GetTransferJobRequest, opts ...gax.CallOption) (*storagetransferpb.TransferJob, error) { + if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { + cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) + defer cancel() + ctx = cctx + } + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "job_name", url.QueryEscape(req.GetJobName()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append((*c.CallOptions).GetTransferJob[0:len((*c.CallOptions).GetTransferJob):len((*c.CallOptions).GetTransferJob)], opts...) + var resp *storagetransferpb.TransferJob + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.client.GetTransferJob(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return resp, nil +} + +func (c *gRPCClient) ListTransferJobs(ctx context.Context, req *storagetransferpb.ListTransferJobsRequest, opts ...gax.CallOption) *TransferJobIterator { + ctx = insertMetadata(ctx, c.xGoogMetadata) + opts = append((*c.CallOptions).ListTransferJobs[0:len((*c.CallOptions).ListTransferJobs):len((*c.CallOptions).ListTransferJobs)], opts...) + it := &TransferJobIterator{} + req = proto.Clone(req).(*storagetransferpb.ListTransferJobsRequest) + it.InternalFetch = func(pageSize int, pageToken string) ([]*storagetransferpb.TransferJob, string, error) { + var resp *storagetransferpb.ListTransferJobsResponse + req.PageToken = pageToken + if pageSize > math.MaxInt32 { + req.PageSize = math.MaxInt32 + } else { + req.PageSize = int32(pageSize) + } + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.client.ListTransferJobs(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, "", err + } + + it.Response = resp + return resp.GetTransferJobs(), resp.GetNextPageToken(), nil + } + fetch := func(pageSize int, pageToken string) (string, error) { + items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) + if err != nil { + return "", err + } + it.items = append(it.items, items...) + return nextPageToken, nil + } + it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) + it.pageInfo.MaxSize = int(req.GetPageSize()) + it.pageInfo.Token = req.GetPageToken() + return it +} + +func (c *gRPCClient) PauseTransferOperation(ctx context.Context, req *storagetransferpb.PauseTransferOperationRequest, opts ...gax.CallOption) error { + if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { + cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) + defer cancel() + ctx = cctx + } + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append((*c.CallOptions).PauseTransferOperation[0:len((*c.CallOptions).PauseTransferOperation):len((*c.CallOptions).PauseTransferOperation)], opts...) + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + _, err = c.client.PauseTransferOperation(ctx, req, settings.GRPC...) + return err + }, opts...) + return err +} + +func (c *gRPCClient) ResumeTransferOperation(ctx context.Context, req *storagetransferpb.ResumeTransferOperationRequest, opts ...gax.CallOption) error { + if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { + cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) + defer cancel() + ctx = cctx + } + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append((*c.CallOptions).ResumeTransferOperation[0:len((*c.CallOptions).ResumeTransferOperation):len((*c.CallOptions).ResumeTransferOperation)], opts...) + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + _, err = c.client.ResumeTransferOperation(ctx, req, settings.GRPC...) + return err + }, opts...) + return err +} + +func (c *gRPCClient) RunTransferJob(ctx context.Context, req *storagetransferpb.RunTransferJobRequest, opts ...gax.CallOption) (*RunTransferJobOperation, error) { + if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { + cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond) + defer cancel() + ctx = cctx + } + md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "job_name", url.QueryEscape(req.GetJobName()))) + ctx = insertMetadata(ctx, c.xGoogMetadata, md) + opts = append((*c.CallOptions).RunTransferJob[0:len((*c.CallOptions).RunTransferJob):len((*c.CallOptions).RunTransferJob)], opts...) + var resp *longrunningpb.Operation + err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { + var err error + resp, err = c.client.RunTransferJob(ctx, req, settings.GRPC...) + return err + }, opts...) + if err != nil { + return nil, err + } + return &RunTransferJobOperation{ + lro: longrunning.InternalNewOperation(*c.LROClient, resp), + }, nil +} + +// RunTransferJobOperation manages a long-running operation from RunTransferJob. +type RunTransferJobOperation struct { + lro *longrunning.Operation +} + +// RunTransferJobOperation returns a new RunTransferJobOperation from a given name. +// The name must be that of a previously created RunTransferJobOperation, possibly from a different process. +func (c *gRPCClient) RunTransferJobOperation(name string) *RunTransferJobOperation { + return &RunTransferJobOperation{ + lro: longrunning.InternalNewOperation(*c.LROClient, &longrunningpb.Operation{Name: name}), + } +} + +// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. +// +// See documentation of Poll for error-handling information. +func (op *RunTransferJobOperation) Wait(ctx context.Context, opts ...gax.CallOption) error { + return op.lro.WaitWithInterval(ctx, nil, time.Minute, opts...) +} + +// Poll fetches the latest state of the long-running operation. +// +// Poll also fetches the latest metadata, which can be retrieved by Metadata. +// +// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and +// the operation has completed with failure, the error is returned and op.Done will return true. +// If Poll succeeds and the operation has completed successfully, +// op.Done will return true, and the response of the operation is returned. +// If Poll succeeds and the operation has not completed, the returned response and error are both nil. +func (op *RunTransferJobOperation) Poll(ctx context.Context, opts ...gax.CallOption) error { + return op.lro.Poll(ctx, nil, opts...) +} + +// Metadata returns metadata associated with the long-running operation. +// Metadata itself does not contact the server, but Poll does. +// To get the latest metadata, call this method after a successful call to Poll. +// If the metadata is not available, the returned metadata and error are both nil. +func (op *RunTransferJobOperation) Metadata() (*storagetransferpb.TransferOperation, error) { + var meta storagetransferpb.TransferOperation + if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { + return nil, nil + } else if err != nil { + return nil, err + } + return &meta, nil +} + +// Done reports whether the long-running operation has completed. +func (op *RunTransferJobOperation) Done() bool { + return op.lro.Done() +} + +// Name returns the name of the long-running operation. +// The name is assigned by the server and is unique within the service from which the operation is created. +func (op *RunTransferJobOperation) Name() string { + return op.lro.Name() +} + +// TransferJobIterator manages a stream of *storagetransferpb.TransferJob. +type TransferJobIterator struct { + items []*storagetransferpb.TransferJob + pageInfo *iterator.PageInfo + nextFunc func() error + + // Response is the raw response for the current page. + // It must be cast to the RPC response type. + // Calling Next() or InternalFetch() updates this value. + Response interface{} + + // InternalFetch is for use by the Google Cloud Libraries only. + // It is not part of the stable interface of this package. + // + // InternalFetch returns results from a single call to the underlying RPC. + // The number of results is no greater than pageSize. + // If there are no more results, nextPageToken is empty and err is nil. + InternalFetch func(pageSize int, pageToken string) (results []*storagetransferpb.TransferJob, nextPageToken string, err error) +} + +// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. +func (it *TransferJobIterator) PageInfo() *iterator.PageInfo { + return it.pageInfo +} + +// Next returns the next result. Its second return value is iterator.Done if there are no more +// results. Once Next returns Done, all subsequent calls will return Done. +func (it *TransferJobIterator) Next() (*storagetransferpb.TransferJob, error) { + var item *storagetransferpb.TransferJob + if err := it.nextFunc(); err != nil { + return item, err + } + item = it.items[0] + it.items = it.items[1:] + return item, nil +} + +func (it *TransferJobIterator) bufLen() int { + return len(it.items) +} + +func (it *TransferJobIterator) takeBuf() interface{} { + b := it.items + it.items = nil + return b +} diff --git a/storagetransfer/apiv1/storage_transfer_client_example_test.go b/storagetransfer/apiv1/storage_transfer_client_example_test.go new file mode 100644 index 00000000000..b2799ada738 --- /dev/null +++ b/storagetransfer/apiv1/storage_transfer_client_example_test.go @@ -0,0 +1,194 @@ +// Copyright 2021 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// Code generated by protoc-gen-go_gapic. DO NOT EDIT. + +package storagetransfer_test + +import ( + "context" + + storagetransfer "cloud.google.com/go/storagetransfer/apiv1" + "google.golang.org/api/iterator" + storagetransferpb "google.golang.org/genproto/googleapis/storagetransfer/v1" +) + +func ExampleNewClient() { + ctx := context.Background() + c, err := storagetransfer.NewClient(ctx) + if err != nil { + // TODO: Handle error. + } + defer c.Close() + + // TODO: Use client. + _ = c +} + +func ExampleClient_GetGoogleServiceAccount() { + ctx := context.Background() + c, err := storagetransfer.NewClient(ctx) + if err != nil { + // TODO: Handle error. + } + defer c.Close() + + req := &storagetransferpb.GetGoogleServiceAccountRequest{ + // TODO: Fill request struct fields. + } + resp, err := c.GetGoogleServiceAccount(ctx, req) + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp +} + +func ExampleClient_CreateTransferJob() { + ctx := context.Background() + c, err := storagetransfer.NewClient(ctx) + if err != nil { + // TODO: Handle error. + } + defer c.Close() + + req := &storagetransferpb.CreateTransferJobRequest{ + // TODO: Fill request struct fields. + } + resp, err := c.CreateTransferJob(ctx, req) + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp +} + +func ExampleClient_UpdateTransferJob() { + ctx := context.Background() + c, err := storagetransfer.NewClient(ctx) + if err != nil { + // TODO: Handle error. + } + defer c.Close() + + req := &storagetransferpb.UpdateTransferJobRequest{ + // TODO: Fill request struct fields. + } + resp, err := c.UpdateTransferJob(ctx, req) + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp +} + +func ExampleClient_GetTransferJob() { + ctx := context.Background() + c, err := storagetransfer.NewClient(ctx) + if err != nil { + // TODO: Handle error. + } + defer c.Close() + + req := &storagetransferpb.GetTransferJobRequest{ + // TODO: Fill request struct fields. + } + resp, err := c.GetTransferJob(ctx, req) + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp +} + +func ExampleClient_ListTransferJobs() { + ctx := context.Background() + c, err := storagetransfer.NewClient(ctx) + if err != nil { + // TODO: Handle error. + } + defer c.Close() + + req := &storagetransferpb.ListTransferJobsRequest{ + // TODO: Fill request struct fields. + } + it := c.ListTransferJobs(ctx, req) + for { + resp, err := it.Next() + if err == iterator.Done { + break + } + if err != nil { + // TODO: Handle error. + } + // TODO: Use resp. + _ = resp + } +} + +func ExampleClient_PauseTransferOperation() { + ctx := context.Background() + c, err := storagetransfer.NewClient(ctx) + if err != nil { + // TODO: Handle error. + } + defer c.Close() + + req := &storagetransferpb.PauseTransferOperationRequest{ + // TODO: Fill request struct fields. + } + err = c.PauseTransferOperation(ctx, req) + if err != nil { + // TODO: Handle error. + } +} + +func ExampleClient_ResumeTransferOperation() { + ctx := context.Background() + c, err := storagetransfer.NewClient(ctx) + if err != nil { + // TODO: Handle error. + } + defer c.Close() + + req := &storagetransferpb.ResumeTransferOperationRequest{ + // TODO: Fill request struct fields. + } + err = c.ResumeTransferOperation(ctx, req) + if err != nil { + // TODO: Handle error. + } +} + +func ExampleClient_RunTransferJob() { + ctx := context.Background() + c, err := storagetransfer.NewClient(ctx) + if err != nil { + // TODO: Handle error. + } + defer c.Close() + + req := &storagetransferpb.RunTransferJobRequest{ + // TODO: Fill request struct fields. + } + op, err := c.RunTransferJob(ctx, req) + if err != nil { + // TODO: Handle error. + } + + err = op.Wait(ctx) + if err != nil { + // TODO: Handle error. + } +}