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 dataproc 18 19import ( 20 "context" 21 "fmt" 22 "math" 23 "net/url" 24 "time" 25 26 gax "github.com/googleapis/gax-go/v2" 27 "google.golang.org/api/iterator" 28 "google.golang.org/api/option" 29 "google.golang.org/api/option/internaloption" 30 gtransport "google.golang.org/api/transport/grpc" 31 dataprocpb "google.golang.org/genproto/googleapis/cloud/dataproc/v1" 32 "google.golang.org/grpc" 33 "google.golang.org/grpc/codes" 34 "google.golang.org/grpc/metadata" 35 "google.golang.org/protobuf/proto" 36) 37 38var newAutoscalingPolicyClientHook clientHook 39 40// AutoscalingPolicyCallOptions contains the retry settings for each method of AutoscalingPolicyClient. 41type AutoscalingPolicyCallOptions struct { 42 CreateAutoscalingPolicy []gax.CallOption 43 UpdateAutoscalingPolicy []gax.CallOption 44 GetAutoscalingPolicy []gax.CallOption 45 ListAutoscalingPolicies []gax.CallOption 46 DeleteAutoscalingPolicy []gax.CallOption 47} 48 49func defaultAutoscalingPolicyGRPCClientOptions() []option.ClientOption { 50 return []option.ClientOption{ 51 internaloption.WithDefaultEndpoint("dataproc.googleapis.com:443"), 52 internaloption.WithDefaultMTLSEndpoint("dataproc.mtls.googleapis.com:443"), 53 internaloption.WithDefaultAudience("https://dataproc.googleapis.com/"), 54 internaloption.WithDefaultScopes(DefaultAuthScopes()...), 55 internaloption.EnableJwtWithScope(), 56 option.WithGRPCDialOption(grpc.WithDisableServiceConfig()), 57 option.WithGRPCDialOption(grpc.WithDefaultCallOptions( 58 grpc.MaxCallRecvMsgSize(math.MaxInt32))), 59 } 60} 61 62func defaultAutoscalingPolicyCallOptions() *AutoscalingPolicyCallOptions { 63 return &AutoscalingPolicyCallOptions{ 64 CreateAutoscalingPolicy: []gax.CallOption{}, 65 UpdateAutoscalingPolicy: []gax.CallOption{ 66 gax.WithRetry(func() gax.Retryer { 67 return gax.OnCodes([]codes.Code{ 68 codes.DeadlineExceeded, 69 codes.Unavailable, 70 }, gax.Backoff{ 71 Initial: 100 * time.Millisecond, 72 Max: 60000 * time.Millisecond, 73 Multiplier: 1.30, 74 }) 75 }), 76 }, 77 GetAutoscalingPolicy: []gax.CallOption{ 78 gax.WithRetry(func() gax.Retryer { 79 return gax.OnCodes([]codes.Code{ 80 codes.DeadlineExceeded, 81 codes.Unavailable, 82 }, gax.Backoff{ 83 Initial: 100 * time.Millisecond, 84 Max: 60000 * time.Millisecond, 85 Multiplier: 1.30, 86 }) 87 }), 88 }, 89 ListAutoscalingPolicies: []gax.CallOption{ 90 gax.WithRetry(func() gax.Retryer { 91 return gax.OnCodes([]codes.Code{ 92 codes.DeadlineExceeded, 93 codes.Unavailable, 94 }, gax.Backoff{ 95 Initial: 100 * time.Millisecond, 96 Max: 60000 * time.Millisecond, 97 Multiplier: 1.30, 98 }) 99 }), 100 }, 101 DeleteAutoscalingPolicy: []gax.CallOption{}, 102 } 103} 104 105// internalAutoscalingPolicyClient is an interface that defines the methods availaible from Cloud Dataproc API. 106type internalAutoscalingPolicyClient interface { 107 Close() error 108 setGoogleClientInfo(...string) 109 Connection() *grpc.ClientConn 110 CreateAutoscalingPolicy(context.Context, *dataprocpb.CreateAutoscalingPolicyRequest, ...gax.CallOption) (*dataprocpb.AutoscalingPolicy, error) 111 UpdateAutoscalingPolicy(context.Context, *dataprocpb.UpdateAutoscalingPolicyRequest, ...gax.CallOption) (*dataprocpb.AutoscalingPolicy, error) 112 GetAutoscalingPolicy(context.Context, *dataprocpb.GetAutoscalingPolicyRequest, ...gax.CallOption) (*dataprocpb.AutoscalingPolicy, error) 113 ListAutoscalingPolicies(context.Context, *dataprocpb.ListAutoscalingPoliciesRequest, ...gax.CallOption) *AutoscalingPolicyIterator 114 DeleteAutoscalingPolicy(context.Context, *dataprocpb.DeleteAutoscalingPolicyRequest, ...gax.CallOption) error 115} 116 117// AutoscalingPolicyClient is a client for interacting with Cloud Dataproc API. 118// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 119// 120// The API interface for managing autoscaling policies in the 121// Dataproc API. 122type AutoscalingPolicyClient struct { 123 // The internal transport-dependent client. 124 internalClient internalAutoscalingPolicyClient 125 126 // The call options for this service. 127 CallOptions *AutoscalingPolicyCallOptions 128} 129 130// Wrapper methods routed to the internal client. 131 132// Close closes the connection to the API service. The user should invoke this when 133// the client is no longer required. 134func (c *AutoscalingPolicyClient) Close() error { 135 return c.internalClient.Close() 136} 137 138// setGoogleClientInfo sets the name and version of the application in 139// the `x-goog-api-client` header passed on each request. Intended for 140// use by Google-written clients. 141func (c *AutoscalingPolicyClient) setGoogleClientInfo(keyval ...string) { 142 c.internalClient.setGoogleClientInfo(keyval...) 143} 144 145// Connection returns a connection to the API service. 146// 147// Deprecated. 148func (c *AutoscalingPolicyClient) Connection() *grpc.ClientConn { 149 return c.internalClient.Connection() 150} 151 152// CreateAutoscalingPolicy creates new autoscaling policy. 153func (c *AutoscalingPolicyClient) CreateAutoscalingPolicy(ctx context.Context, req *dataprocpb.CreateAutoscalingPolicyRequest, opts ...gax.CallOption) (*dataprocpb.AutoscalingPolicy, error) { 154 return c.internalClient.CreateAutoscalingPolicy(ctx, req, opts...) 155} 156 157// UpdateAutoscalingPolicy updates (replaces) autoscaling policy. 158// 159// Disabled check for update_mask, because all updates will be full 160// replacements. 161func (c *AutoscalingPolicyClient) UpdateAutoscalingPolicy(ctx context.Context, req *dataprocpb.UpdateAutoscalingPolicyRequest, opts ...gax.CallOption) (*dataprocpb.AutoscalingPolicy, error) { 162 return c.internalClient.UpdateAutoscalingPolicy(ctx, req, opts...) 163} 164 165// GetAutoscalingPolicy retrieves autoscaling policy. 166func (c *AutoscalingPolicyClient) GetAutoscalingPolicy(ctx context.Context, req *dataprocpb.GetAutoscalingPolicyRequest, opts ...gax.CallOption) (*dataprocpb.AutoscalingPolicy, error) { 167 return c.internalClient.GetAutoscalingPolicy(ctx, req, opts...) 168} 169 170// ListAutoscalingPolicies lists autoscaling policies in the project. 171func (c *AutoscalingPolicyClient) ListAutoscalingPolicies(ctx context.Context, req *dataprocpb.ListAutoscalingPoliciesRequest, opts ...gax.CallOption) *AutoscalingPolicyIterator { 172 return c.internalClient.ListAutoscalingPolicies(ctx, req, opts...) 173} 174 175// DeleteAutoscalingPolicy deletes an autoscaling policy. It is an error to delete an autoscaling 176// policy that is in use by one or more clusters. 177func (c *AutoscalingPolicyClient) DeleteAutoscalingPolicy(ctx context.Context, req *dataprocpb.DeleteAutoscalingPolicyRequest, opts ...gax.CallOption) error { 178 return c.internalClient.DeleteAutoscalingPolicy(ctx, req, opts...) 179} 180 181// autoscalingPolicyGRPCClient is a client for interacting with Cloud Dataproc API over gRPC transport. 182// 183// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls. 184type autoscalingPolicyGRPCClient struct { 185 // Connection pool of gRPC connections to the service. 186 connPool gtransport.ConnPool 187 188 // flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE 189 disableDeadlines bool 190 191 // Points back to the CallOptions field of the containing AutoscalingPolicyClient 192 CallOptions **AutoscalingPolicyCallOptions 193 194 // The gRPC API client. 195 autoscalingPolicyClient dataprocpb.AutoscalingPolicyServiceClient 196 197 // The x-goog-* metadata to be sent with each request. 198 xGoogMetadata metadata.MD 199} 200 201// NewAutoscalingPolicyClient creates a new autoscaling policy service client based on gRPC. 202// The returned client must be Closed when it is done being used to clean up its underlying connections. 203// 204// The API interface for managing autoscaling policies in the 205// Dataproc API. 206func NewAutoscalingPolicyClient(ctx context.Context, opts ...option.ClientOption) (*AutoscalingPolicyClient, error) { 207 clientOpts := defaultAutoscalingPolicyGRPCClientOptions() 208 if newAutoscalingPolicyClientHook != nil { 209 hookOpts, err := newAutoscalingPolicyClientHook(ctx, clientHookParams{}) 210 if err != nil { 211 return nil, err 212 } 213 clientOpts = append(clientOpts, hookOpts...) 214 } 215 216 disableDeadlines, err := checkDisableDeadlines() 217 if err != nil { 218 return nil, err 219 } 220 221 connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...) 222 if err != nil { 223 return nil, err 224 } 225 client := AutoscalingPolicyClient{CallOptions: defaultAutoscalingPolicyCallOptions()} 226 227 c := &autoscalingPolicyGRPCClient{ 228 connPool: connPool, 229 disableDeadlines: disableDeadlines, 230 autoscalingPolicyClient: dataprocpb.NewAutoscalingPolicyServiceClient(connPool), 231 CallOptions: &client.CallOptions, 232 } 233 c.setGoogleClientInfo() 234 235 client.internalClient = c 236 237 return &client, nil 238} 239 240// Connection returns a connection to the API service. 241// 242// Deprecated. 243func (c *autoscalingPolicyGRPCClient) Connection() *grpc.ClientConn { 244 return c.connPool.Conn() 245} 246 247// setGoogleClientInfo sets the name and version of the application in 248// the `x-goog-api-client` header passed on each request. Intended for 249// use by Google-written clients. 250func (c *autoscalingPolicyGRPCClient) setGoogleClientInfo(keyval ...string) { 251 kv := append([]string{"gl-go", versionGo()}, keyval...) 252 kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version) 253 c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...)) 254} 255 256// Close closes the connection to the API service. The user should invoke this when 257// the client is no longer required. 258func (c *autoscalingPolicyGRPCClient) Close() error { 259 return c.connPool.Close() 260} 261 262func (c *autoscalingPolicyGRPCClient) CreateAutoscalingPolicy(ctx context.Context, req *dataprocpb.CreateAutoscalingPolicyRequest, opts ...gax.CallOption) (*dataprocpb.AutoscalingPolicy, error) { 263 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 264 cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond) 265 defer cancel() 266 ctx = cctx 267 } 268 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 269 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 270 opts = append((*c.CallOptions).CreateAutoscalingPolicy[0:len((*c.CallOptions).CreateAutoscalingPolicy):len((*c.CallOptions).CreateAutoscalingPolicy)], opts...) 271 var resp *dataprocpb.AutoscalingPolicy 272 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 273 var err error 274 resp, err = c.autoscalingPolicyClient.CreateAutoscalingPolicy(ctx, req, settings.GRPC...) 275 return err 276 }, opts...) 277 if err != nil { 278 return nil, err 279 } 280 return resp, nil 281} 282 283func (c *autoscalingPolicyGRPCClient) UpdateAutoscalingPolicy(ctx context.Context, req *dataprocpb.UpdateAutoscalingPolicyRequest, opts ...gax.CallOption) (*dataprocpb.AutoscalingPolicy, error) { 284 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 285 cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond) 286 defer cancel() 287 ctx = cctx 288 } 289 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "policy.name", url.QueryEscape(req.GetPolicy().GetName()))) 290 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 291 opts = append((*c.CallOptions).UpdateAutoscalingPolicy[0:len((*c.CallOptions).UpdateAutoscalingPolicy):len((*c.CallOptions).UpdateAutoscalingPolicy)], opts...) 292 var resp *dataprocpb.AutoscalingPolicy 293 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 294 var err error 295 resp, err = c.autoscalingPolicyClient.UpdateAutoscalingPolicy(ctx, req, settings.GRPC...) 296 return err 297 }, opts...) 298 if err != nil { 299 return nil, err 300 } 301 return resp, nil 302} 303 304func (c *autoscalingPolicyGRPCClient) GetAutoscalingPolicy(ctx context.Context, req *dataprocpb.GetAutoscalingPolicyRequest, opts ...gax.CallOption) (*dataprocpb.AutoscalingPolicy, error) { 305 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 306 cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond) 307 defer cancel() 308 ctx = cctx 309 } 310 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 311 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 312 opts = append((*c.CallOptions).GetAutoscalingPolicy[0:len((*c.CallOptions).GetAutoscalingPolicy):len((*c.CallOptions).GetAutoscalingPolicy)], opts...) 313 var resp *dataprocpb.AutoscalingPolicy 314 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 315 var err error 316 resp, err = c.autoscalingPolicyClient.GetAutoscalingPolicy(ctx, req, settings.GRPC...) 317 return err 318 }, opts...) 319 if err != nil { 320 return nil, err 321 } 322 return resp, nil 323} 324 325func (c *autoscalingPolicyGRPCClient) ListAutoscalingPolicies(ctx context.Context, req *dataprocpb.ListAutoscalingPoliciesRequest, opts ...gax.CallOption) *AutoscalingPolicyIterator { 326 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent()))) 327 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 328 opts = append((*c.CallOptions).ListAutoscalingPolicies[0:len((*c.CallOptions).ListAutoscalingPolicies):len((*c.CallOptions).ListAutoscalingPolicies)], opts...) 329 it := &AutoscalingPolicyIterator{} 330 req = proto.Clone(req).(*dataprocpb.ListAutoscalingPoliciesRequest) 331 it.InternalFetch = func(pageSize int, pageToken string) ([]*dataprocpb.AutoscalingPolicy, string, error) { 332 resp := &dataprocpb.ListAutoscalingPoliciesResponse{} 333 if pageToken != "" { 334 req.PageToken = pageToken 335 } 336 if pageSize > math.MaxInt32 { 337 req.PageSize = math.MaxInt32 338 } else if pageSize != 0 { 339 req.PageSize = int32(pageSize) 340 } 341 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 342 var err error 343 resp, err = c.autoscalingPolicyClient.ListAutoscalingPolicies(ctx, req, settings.GRPC...) 344 return err 345 }, opts...) 346 if err != nil { 347 return nil, "", err 348 } 349 350 it.Response = resp 351 return resp.GetPolicies(), resp.GetNextPageToken(), nil 352 } 353 fetch := func(pageSize int, pageToken string) (string, error) { 354 items, nextPageToken, err := it.InternalFetch(pageSize, pageToken) 355 if err != nil { 356 return "", err 357 } 358 it.items = append(it.items, items...) 359 return nextPageToken, nil 360 } 361 362 it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf) 363 it.pageInfo.MaxSize = int(req.GetPageSize()) 364 it.pageInfo.Token = req.GetPageToken() 365 366 return it 367} 368 369func (c *autoscalingPolicyGRPCClient) DeleteAutoscalingPolicy(ctx context.Context, req *dataprocpb.DeleteAutoscalingPolicyRequest, opts ...gax.CallOption) error { 370 if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines { 371 cctx, cancel := context.WithTimeout(ctx, 600000*time.Millisecond) 372 defer cancel() 373 ctx = cctx 374 } 375 md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))) 376 ctx = insertMetadata(ctx, c.xGoogMetadata, md) 377 opts = append((*c.CallOptions).DeleteAutoscalingPolicy[0:len((*c.CallOptions).DeleteAutoscalingPolicy):len((*c.CallOptions).DeleteAutoscalingPolicy)], opts...) 378 err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error { 379 var err error 380 _, err = c.autoscalingPolicyClient.DeleteAutoscalingPolicy(ctx, req, settings.GRPC...) 381 return err 382 }, opts...) 383 return err 384} 385 386// AutoscalingPolicyIterator manages a stream of *dataprocpb.AutoscalingPolicy. 387type AutoscalingPolicyIterator struct { 388 items []*dataprocpb.AutoscalingPolicy 389 pageInfo *iterator.PageInfo 390 nextFunc func() error 391 392 // Response is the raw response for the current page. 393 // It must be cast to the RPC response type. 394 // Calling Next() or InternalFetch() updates this value. 395 Response interface{} 396 397 // InternalFetch is for use by the Google Cloud Libraries only. 398 // It is not part of the stable interface of this package. 399 // 400 // InternalFetch returns results from a single call to the underlying RPC. 401 // The number of results is no greater than pageSize. 402 // If there are no more results, nextPageToken is empty and err is nil. 403 InternalFetch func(pageSize int, pageToken string) (results []*dataprocpb.AutoscalingPolicy, nextPageToken string, err error) 404} 405 406// PageInfo supports pagination. See the google.golang.org/api/iterator package for details. 407func (it *AutoscalingPolicyIterator) PageInfo() *iterator.PageInfo { 408 return it.pageInfo 409} 410 411// Next returns the next result. Its second return value is iterator.Done if there are no more 412// results. Once Next returns Done, all subsequent calls will return Done. 413func (it *AutoscalingPolicyIterator) Next() (*dataprocpb.AutoscalingPolicy, error) { 414 var item *dataprocpb.AutoscalingPolicy 415 if err := it.nextFunc(); err != nil { 416 return item, err 417 } 418 item = it.items[0] 419 it.items = it.items[1:] 420 return item, nil 421} 422 423func (it *AutoscalingPolicyIterator) bufLen() int { 424 return len(it.items) 425} 426 427func (it *AutoscalingPolicyIterator) takeBuf() interface{} { 428 b := it.items 429 it.items = nil 430 return b 431} 432