1// Copyright 2021 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 17package workflows 18 19import ( 20 "context" 21 "fmt" 22 "math" 23 "net/url" 24 "time" 25 26 "cloud.google.com/go/longrunning" 27 lroauto "cloud.google.com/go/longrunning/autogen" 28 "github.com/golang/protobuf/proto" 29 gax "github.com/googleapis/gax-go/v2" 30 "google.golang.org/api/iterator" 31 "google.golang.org/api/option" 32 "google.golang.org/api/option/internaloption" 33 gtransport "google.golang.org/api/transport/grpc" 34 workflowspb "google.golang.org/genproto/googleapis/cloud/workflows/v1beta" 35 longrunningpb "google.golang.org/genproto/googleapis/longrunning" 36 "google.golang.org/grpc" 37 "google.golang.org/grpc/metadata" 38) 39 40var newClientHook clientHook 41 42// CallOptions contains the retry settings for each method of Client. 43type CallOptions struct { 44 ListWorkflows []gax.CallOption 45 GetWorkflow []gax.CallOption 46 CreateWorkflow []gax.CallOption 47 DeleteWorkflow []gax.CallOption 48 UpdateWorkflow []gax.CallOption 49} 50 51func defaultClientOptions() []option.ClientOption { 52 return []option.ClientOption{ 53 internaloption.WithDefaultEndpoint("workflows.googleapis.com:443"), 54 internaloption.WithDefaultMTLSEndpoint("workflows.mtls.googleapis.com:443"), 55 internaloption.WithDefaultAudience("https://workflows.googleapis.com/"), 56 internaloption.WithDefaultScopes(DefaultAuthScopes()...), 57 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), 58 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 59 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 60 } 61} 62 63func defaultCallOptions() *CallOptions { 64 return &CallOptions{ 65 ListWorkflows: []gax.CallOption{}, 66 GetWorkflow: []gax.CallOption{}, 67 CreateWorkflow: []gax.CallOption{}, 68 DeleteWorkflow: []gax.CallOption{}, 69 UpdateWorkflow: []gax.CallOption{}, 70 } 71} 72 73// Client is a client for interacting with Workflows API. 74// 75// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 76type Client struct { 77 // Connection pool of gRPC connections to the service. 78 connPool gtransport.ConnPool 79 80 // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE 81 disableDeadlines bool 82 83 // The gRPC API client. 84 client workflowspb.WorkflowsClient 85 86 // LROClient is used internally to handle longrunning operations. 87 // It is exposed so that its CallOptions can be modified if required. 88 // Users should not Close this client. 89 LROClient *lroauto.OperationsClient 90 91 // The call options for this service. 92 CallOptions *CallOptions 93 94 // The x-goog-* metadata to be sent with each request. 95 xGoogMetadata metadata.MD 96} 97 98// NewClient creates a new workflows client. 99// 100// Workflows is used to deploy and execute workflow programs. 101// Workflows makes sure the program executes reliably, despite hardware and 102// networking interruptions. 103func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) { 104 clientOpts := defaultClientOptions() 105 106 if newClientHook != nil { 107 hookOpts, err := newClientHook(ctx, clientHookParams{}) 108 if err != nil { 109 return nil, err 110 } 111 clientOpts = append(clientOpts, hookOpts...) 112 } 113 114 disableDeadlines, err := checkDisableDeadlines() 115 if err != nil { 116 return nil, err 117 } 118 119 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 120 if err != nil { 121 return nil, err 122 } 123 c := &Client{ 124 connPool: connPool, 125 disableDeadlines: disableDeadlines, 126 CallOptions: defaultCallOptions(), 127 128 client: workflowspb.NewWorkflowsClient(connPool), 129 } 130 c.setGoogleClientInfo() 131 132 c.LROClient, err = lroauto.NewOperationsClient(ctx, gtransport.WithConnPool(connPool)) 133 if err != nil { 134 // This error "should not happen", since we are just reusing old connection pool 135 // and never actually need to dial. 136 // If this does happen, we could leak connp. However, we cannot close conn: 137 // If the user invoked the constructor with option.WithGRPCConn, 138 // we would close a connection that's still in use. 139 // TODO: investigate error conditions. 140 return nil, err 141 } 142 return c, nil 143} 144 145// Connection returns a connection to the API service. 146// 147// Deprecated. 148func (c *Client) Connection() *grpc.ClientConn { 149 return c.connPool.Conn() 150} 151 152// Close closes the connection to the API service. The user should invoke this when 153// the client is no longer required. 154func (c *Client) Close() error { 155 return c.connPool.Close() 156} 157 158// setGoogleClientInfo sets the name and version of the application in 159// the `x-goog-api-client` header passed on each request. Intended for 160// use by Google-written clients. 161func (c *Client) setGoogleClientInfo(keyval ...string) { 162 kv := append([]string{"gl-go", versionGo()}, keyval...) 163 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 164 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 165} 166 167// ListWorkflows lists Workflows in a given project and location. 168// The default order is not specified. 169func (c *Client) ListWorkflows(ctx context.Context, req *workflowspb.ListWorkflowsRequest, opts ...gax.CallOption) *WorkflowIterator { 170 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 171 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 172 opts = append(c.CallOptions.ListWorkflows[0:len(c.CallOptions.ListWorkflows):len(c.CallOptions.ListWorkflows)], opts...) 173 it := &WorkflowIterator{} 174 req = proto.Clone(req).(*workflowspb.ListWorkflowsRequest) 175 it.InternalFetch = func(pageSize int, pageToken string) ([]*workflowspb.Workflow, string, error) { 176 var resp *workflowspb.ListWorkflowsResponse 177 req.PageToken = pageToken 178 if pageSize > math.MaxInt32 { 179 req.PageSize = math.MaxInt32 180 } else { 181 req.PageSize = int32(pageSize) 182 } 183 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 184 var err error 185 resp, err = c.client.ListWorkflows(ctx, req, settings.GRPC...) 186 return err 187 }, opts...) 188 if err != nil { 189 return nil, "", err 190 } 191 192 it.Response = resp 193 return resp.GetWorkflows(), resp.GetNextPageToken(), nil 194 } 195 fetch := func(pageSize int, pageToken string) (string, error) { 196 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 197 if err != nil { 198 return "", err 199 } 200 it.items = append(it.items, items...) 201 return nextPageToken, nil 202 } 203 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 204 it.pageInfo.MaxSize = int(req.GetPageSize()) 205 it.pageInfo.Token = req.GetPageToken() 206 return it 207} 208 209// GetWorkflow gets details of a single Workflow. 210func (c *Client) GetWorkflow(ctx context.Context, req *workflowspb.GetWorkflowRequest, opts ...gax.CallOption) (*workflowspb.Workflow, error) { 211 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 212 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 213 opts = append(c.CallOptions.GetWorkflow[0:len(c.CallOptions.GetWorkflow):len(c.CallOptions.GetWorkflow)], opts...) 214 var resp *workflowspb.Workflow 215 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 216 var err error 217 resp, err = c.client.GetWorkflow(ctx, req, settings.GRPC...) 218 return err 219 }, opts...) 220 if err != nil { 221 return nil, err 222 } 223 return resp, nil 224} 225 226// CreateWorkflow creates a new workflow. If a workflow with the specified name already 227// exists in the specified project and location, the long running operation 228// will return ALREADY_EXISTS error. 229func (c *Client) CreateWorkflow(ctx context.Context, req *workflowspb.CreateWorkflowRequest, opts ...gax.CallOption) (*CreateWorkflowOperation, error) { 230 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 231 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 232 opts = append(c.CallOptions.CreateWorkflow[0:len(c.CallOptions.CreateWorkflow):len(c.CallOptions.CreateWorkflow)], opts...) 233 var resp *longrunningpb.Operation 234 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 235 var err error 236 resp, err = c.client.CreateWorkflow(ctx, req, settings.GRPC...) 237 return err 238 }, opts...) 239 if err != nil { 240 return nil, err 241 } 242 return &CreateWorkflowOperation{ 243 lro: longrunning.InternalNewOperation(c.LROClient, resp), 244 }, nil 245} 246 247// DeleteWorkflow deletes a workflow with the specified name. 248// This method also cancels and deletes all running executions of the 249// workflow. 250func (c *Client) DeleteWorkflow(ctx context.Context, req *workflowspb.DeleteWorkflowRequest, opts ...gax.CallOption) (*DeleteWorkflowOperation, error) { 251 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 252 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 253 opts = append(c.CallOptions.DeleteWorkflow[0:len(c.CallOptions.DeleteWorkflow):len(c.CallOptions.DeleteWorkflow)], opts...) 254 var resp *longrunningpb.Operation 255 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 256 var err error 257 resp, err = c.client.DeleteWorkflow(ctx, req, settings.GRPC...) 258 return err 259 }, opts...) 260 if err != nil { 261 return nil, err 262 } 263 return &DeleteWorkflowOperation{ 264 lro: longrunning.InternalNewOperation(c.LROClient, resp), 265 }, nil 266} 267 268// UpdateWorkflow updates an existing workflow. 269// Running this method has no impact on already running executions of the 270// workflow. A new revision of the workflow may be created as a result of a 271// successful update operation. In that case, such revision will be used 272// in new workflow executions. 273func (c *Client) UpdateWorkflow(ctx context.Context, req *workflowspb.UpdateWorkflowRequest, opts ...gax.CallOption) (*UpdateWorkflowOperation, error) { 274 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "workflow.name", url.QueryEscape(req.GetWorkflow().GetName()))) 275 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 276 opts = append(c.CallOptions.UpdateWorkflow[0:len(c.CallOptions.UpdateWorkflow):len(c.CallOptions.UpdateWorkflow)], opts...) 277 var resp *longrunningpb.Operation 278 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 279 var err error 280 resp, err = c.client.UpdateWorkflow(ctx, req, settings.GRPC...) 281 return err 282 }, opts...) 283 if err != nil { 284 return nil, err 285 } 286 return &UpdateWorkflowOperation{ 287 lro: longrunning.InternalNewOperation(c.LROClient, resp), 288 }, nil 289} 290 291// CreateWorkflowOperation manages a long-running operation from CreateWorkflow. 292type CreateWorkflowOperation struct { 293 lro *longrunning.Operation 294} 295 296// CreateWorkflowOperation returns a new CreateWorkflowOperation from a given name. 297// The name must be that of a previously created CreateWorkflowOperation, possibly from a different process. 298func (c *Client) CreateWorkflowOperation(name string) *CreateWorkflowOperation { 299 return &CreateWorkflowOperation{ 300 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 301 } 302} 303 304// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 305// 306// See documentation of Poll for error-handling information. 307func (op *CreateWorkflowOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*workflowspb.Workflow, error) { 308 var resp workflowspb.Workflow 309 if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { 310 return nil, err 311 } 312 return &resp, nil 313} 314 315// Poll fetches the latest state of the long-running operation. 316// 317// Poll also fetches the latest metadata, which can be retrieved by Metadata. 318// 319// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 320// the operation has completed with failure, the error is returned and op.Done will return true. 321// If Poll succeeds and the operation has completed successfully, 322// op.Done will return true, and the response of the operation is returned. 323// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 324func (op *CreateWorkflowOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*workflowspb.Workflow, error) { 325 var resp workflowspb.Workflow 326 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 327 return nil, err 328 } 329 if !op.Done() { 330 return nil, nil 331 } 332 return &resp, nil 333} 334 335// Metadata returns metadata associated with the long-running operation. 336// Metadata itself does not contact the server, but Poll does. 337// To get the latest metadata, call this method after a successful call to Poll. 338// If the metadata is not available, the returned metadata and error are both nil. 339func (op *CreateWorkflowOperation) Metadata() (*workflowspb.OperationMetadata, error) { 340 var meta workflowspb.OperationMetadata 341 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 342 return nil, nil 343 } else if err != nil { 344 return nil, err 345 } 346 return &meta, nil 347} 348 349// Done reports whether the long-running operation has completed. 350func (op *CreateWorkflowOperation) Done() bool { 351 return op.lro.Done() 352} 353 354// Name returns the name of the long-running operation. 355// The name is assigned by the server and is unique within the service from which the operation is created. 356func (op *CreateWorkflowOperation) Name() string { 357 return op.lro.Name() 358} 359 360// DeleteWorkflowOperation manages a long-running operation from DeleteWorkflow. 361type DeleteWorkflowOperation struct { 362 lro *longrunning.Operation 363} 364 365// DeleteWorkflowOperation returns a new DeleteWorkflowOperation from a given name. 366// The name must be that of a previously created DeleteWorkflowOperation, possibly from a different process. 367func (c *Client) DeleteWorkflowOperation(name string) *DeleteWorkflowOperation { 368 return &DeleteWorkflowOperation{ 369 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 370 } 371} 372 373// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 374// 375// See documentation of Poll for error-handling information. 376func (op *DeleteWorkflowOperation) Wait(ctx context.Context, opts ...gax.CallOption) error { 377 return op.lro.WaitWithInterval(ctx, nil, time.Minute, opts...) 378} 379 380// Poll fetches the latest state of the long-running operation. 381// 382// Poll also fetches the latest metadata, which can be retrieved by Metadata. 383// 384// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 385// the operation has completed with failure, the error is returned and op.Done will return true. 386// If Poll succeeds and the operation has completed successfully, 387// op.Done will return true, and the response of the operation is returned. 388// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 389func (op *DeleteWorkflowOperation) Poll(ctx context.Context, opts ...gax.CallOption) error { 390 return op.lro.Poll(ctx, nil, opts...) 391} 392 393// Metadata returns metadata associated with the long-running operation. 394// Metadata itself does not contact the server, but Poll does. 395// To get the latest metadata, call this method after a successful call to Poll. 396// If the metadata is not available, the returned metadata and error are both nil. 397func (op *DeleteWorkflowOperation) Metadata() (*workflowspb.OperationMetadata, error) { 398 var meta workflowspb.OperationMetadata 399 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 400 return nil, nil 401 } else if err != nil { 402 return nil, err 403 } 404 return &meta, nil 405} 406 407// Done reports whether the long-running operation has completed. 408func (op *DeleteWorkflowOperation) Done() bool { 409 return op.lro.Done() 410} 411 412// Name returns the name of the long-running operation. 413// The name is assigned by the server and is unique within the service from which the operation is created. 414func (op *DeleteWorkflowOperation) Name() string { 415 return op.lro.Name() 416} 417 418// UpdateWorkflowOperation manages a long-running operation from UpdateWorkflow. 419type UpdateWorkflowOperation struct { 420 lro *longrunning.Operation 421} 422 423// UpdateWorkflowOperation returns a new UpdateWorkflowOperation from a given name. 424// The name must be that of a previously created UpdateWorkflowOperation, possibly from a different process. 425func (c *Client) UpdateWorkflowOperation(name string) *UpdateWorkflowOperation { 426 return &UpdateWorkflowOperation{ 427 lro: longrunning.InternalNewOperation(c.LROClient, &longrunningpb.Operation{Name: name}), 428 } 429} 430 431// Wait blocks until the long-running operation is completed, returning the response and any errors encountered. 432// 433// See documentation of Poll for error-handling information. 434func (op *UpdateWorkflowOperation) Wait(ctx context.Context, opts ...gax.CallOption) (*workflowspb.Workflow, error) { 435 var resp workflowspb.Workflow 436 if err := op.lro.WaitWithInterval(ctx, &resp, time.Minute, opts...); err != nil { 437 return nil, err 438 } 439 return &resp, nil 440} 441 442// Poll fetches the latest state of the long-running operation. 443// 444// Poll also fetches the latest metadata, which can be retrieved by Metadata. 445// 446// If Poll fails, the error is returned and op is unmodified. If Poll succeeds and 447// the operation has completed with failure, the error is returned and op.Done will return true. 448// If Poll succeeds and the operation has completed successfully, 449// op.Done will return true, and the response of the operation is returned. 450// If Poll succeeds and the operation has not completed, the returned response and error are both nil. 451func (op *UpdateWorkflowOperation) Poll(ctx context.Context, opts ...gax.CallOption) (*workflowspb.Workflow, error) { 452 var resp workflowspb.Workflow 453 if err := op.lro.Poll(ctx, &resp, opts...); err != nil { 454 return nil, err 455 } 456 if !op.Done() { 457 return nil, nil 458 } 459 return &resp, nil 460} 461 462// Metadata returns metadata associated with the long-running operation. 463// Metadata itself does not contact the server, but Poll does. 464// To get the latest metadata, call this method after a successful call to Poll. 465// If the metadata is not available, the returned metadata and error are both nil. 466func (op *UpdateWorkflowOperation) Metadata() (*workflowspb.OperationMetadata, error) { 467 var meta workflowspb.OperationMetadata 468 if err := op.lro.Metadata(&meta); err == longrunning.ErrNoMetadata { 469 return nil, nil 470 } else if err != nil { 471 return nil, err 472 } 473 return &meta, nil 474} 475 476// Done reports whether the long-running operation has completed. 477func (op *UpdateWorkflowOperation) Done() bool { 478 return op.lro.Done() 479} 480 481// Name returns the name of the long-running operation. 482// The name is assigned by the server and is unique within the service from which the operation is created. 483func (op *UpdateWorkflowOperation) Name() string { 484 return op.lro.Name() 485} 486 487// WorkflowIterator manages a stream of *workflowspb.Workflow. 488type WorkflowIterator struct { 489 items []*workflowspb.Workflow 490 pageInfo *iterator.PageInfo 491 nextFunc func() error 492 493 // Response is the raw response for the current page. 494 // It must be cast to the RPC response type. 495 // Calling Next() or InternalFetch() updates this value. 496 Response interface{} 497 498 // InternalFetch is for use by the Google Cloud Libraries only. 499 // It is not part of the stable interface of this package. 500 // 501 // InternalFetch returns results from a single call to the underlying RPC. 502 // The number of results is no greater than pageSize. 503 // If there are no more results, nextPageToken is empty and err is nil. 504 InternalFetch func(pageSize int, pageToken string) (results []*workflowspb.Workflow, nextPageToken string, err error) 505} 506 507// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 508func (it *WorkflowIterator) PageInfo() *iterator.PageInfo { 509 return it.pageInfo 510} 511 512// Next returns the next result. Its second return value is iterator.Done if there are no more 513// results. Once Next returns Done, all subsequent calls will return Done. 514func (it *WorkflowIterator) Next() (*workflowspb.Workflow, error) { 515 var item *workflowspb.Workflow 516 if err := it.nextFunc(); err != nil { 517 return item, err 518 } 519 item = it.items[0] 520 it.items = it.items[1:] 521 return item, nil 522} 523 524func (it *WorkflowIterator) bufLen() int { 525 return len(it.items) 526} 527 528func (it *WorkflowIterator) takeBuf() interface{} { 529 b := it.items 530 it.items = nil 531 return b 532} 533