1// Copyright 2019 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 gapic-generator. DO NOT EDIT.
16
17package dataproc
18
19import (
20	"context"
21	"fmt"
22	"math"
23	"time"
24
25	"github.com/golang/protobuf/proto"
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/transport"
30	dataprocpb "google.golang.org/genproto/googleapis/cloud/dataproc/v1beta2"
31	"google.golang.org/grpc"
32	"google.golang.org/grpc/codes"
33	"google.golang.org/grpc/metadata"
34)
35
36// AutoscalingPolicyCallOptions contains the retry settings for each method of AutoscalingPolicyClient.
37type AutoscalingPolicyCallOptions struct {
38	CreateAutoscalingPolicy []gax.CallOption
39	UpdateAutoscalingPolicy []gax.CallOption
40	GetAutoscalingPolicy    []gax.CallOption
41	ListAutoscalingPolicies []gax.CallOption
42	DeleteAutoscalingPolicy []gax.CallOption
43}
44
45func defaultAutoscalingPolicyClientOptions() []option.ClientOption {
46	return []option.ClientOption{
47		option.WithEndpoint("dataproc.googleapis.com:443"),
48		option.WithScopes(DefaultAuthScopes()...),
49	}
50}
51
52func defaultAutoscalingPolicyCallOptions() *AutoscalingPolicyCallOptions {
53	retry := map[[2]string][]gax.CallOption{
54		{"default", "idempotent"}: {
55			gax.WithRetry(func() gax.Retryer {
56				return gax.OnCodes([]codes.Code{
57					codes.DeadlineExceeded,
58					codes.Unavailable,
59				}, gax.Backoff{
60					Initial:    100 * time.Millisecond,
61					Max:        60000 * time.Millisecond,
62					Multiplier: 1.3,
63				})
64			}),
65		},
66	}
67	return &AutoscalingPolicyCallOptions{
68		CreateAutoscalingPolicy: retry[[2]string{"default", "non_idempotent"}],
69		UpdateAutoscalingPolicy: retry[[2]string{"default", "idempotent"}],
70		GetAutoscalingPolicy:    retry[[2]string{"default", "idempotent"}],
71		ListAutoscalingPolicies: retry[[2]string{"default", "idempotent"}],
72		DeleteAutoscalingPolicy: retry[[2]string{"default", "non_idempotent"}],
73	}
74}
75
76// AutoscalingPolicyClient is a client for interacting with Google Cloud Dataproc API.
77//
78// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
79type AutoscalingPolicyClient struct {
80	// The connection to the service.
81	conn *grpc.ClientConn
82
83	// The gRPC API client.
84	autoscalingPolicyClient dataprocpb.AutoscalingPolicyServiceClient
85
86	// The call options for this service.
87	CallOptions *AutoscalingPolicyCallOptions
88
89	// The x-goog-* metadata to be sent with each request.
90	xGoogMetadata metadata.MD
91}
92
93// NewAutoscalingPolicyClient creates a new autoscaling policy service client.
94//
95// The API interface for managing autoscaling policies in the
96// Google Cloud Dataproc API.
97func NewAutoscalingPolicyClient(ctx context.Context, opts ...option.ClientOption) (*AutoscalingPolicyClient, error) {
98	conn, err := transport.DialGRPC(ctx, append(defaultAutoscalingPolicyClientOptions(), opts...)...)
99	if err != nil {
100		return nil, err
101	}
102	c := &AutoscalingPolicyClient{
103		conn:        conn,
104		CallOptions: defaultAutoscalingPolicyCallOptions(),
105
106		autoscalingPolicyClient: dataprocpb.NewAutoscalingPolicyServiceClient(conn),
107	}
108	c.setGoogleClientInfo()
109	return c, nil
110}
111
112// Connection returns the client's connection to the API service.
113func (c *AutoscalingPolicyClient) Connection() *grpc.ClientConn {
114	return c.conn
115}
116
117// Close closes the connection to the API service. The user should invoke this when
118// the client is no longer required.
119func (c *AutoscalingPolicyClient) Close() error {
120	return c.conn.Close()
121}
122
123// setGoogleClientInfo sets the name and version of the application in
124// the `x-goog-api-client` header passed on each request. Intended for
125// use by Google-written clients.
126func (c *AutoscalingPolicyClient) setGoogleClientInfo(keyval ...string) {
127	kv := append([]string{"gl-go", versionGo()}, keyval...)
128	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
129	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
130}
131
132// CreateAutoscalingPolicy creates new autoscaling policy.
133func (c *AutoscalingPolicyClient) CreateAutoscalingPolicy(ctx context.Context, req *dataprocpb.CreateAutoscalingPolicyRequest, opts ...gax.CallOption) (*dataprocpb.AutoscalingPolicy, error) {
134	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", req.GetParent()))
135	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
136	opts = append(c.CallOptions.CreateAutoscalingPolicy[0:len(c.CallOptions.CreateAutoscalingPolicy):len(c.CallOptions.CreateAutoscalingPolicy)], opts...)
137	var resp *dataprocpb.AutoscalingPolicy
138	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
139		var err error
140		resp, err = c.autoscalingPolicyClient.CreateAutoscalingPolicy(ctx, req, settings.GRPC...)
141		return err
142	}, opts...)
143	if err != nil {
144		return nil, err
145	}
146	return resp, nil
147}
148
149// UpdateAutoscalingPolicy updates (replaces) autoscaling policy.
150//
151// Disabled check for update_mask, because all updates will be full
152// replacements.
153func (c *AutoscalingPolicyClient) UpdateAutoscalingPolicy(ctx context.Context, req *dataprocpb.UpdateAutoscalingPolicyRequest, opts ...gax.CallOption) (*dataprocpb.AutoscalingPolicy, error) {
154	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "policy.name", req.GetPolicy().GetName()))
155	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
156	opts = append(c.CallOptions.UpdateAutoscalingPolicy[0:len(c.CallOptions.UpdateAutoscalingPolicy):len(c.CallOptions.UpdateAutoscalingPolicy)], opts...)
157	var resp *dataprocpb.AutoscalingPolicy
158	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
159		var err error
160		resp, err = c.autoscalingPolicyClient.UpdateAutoscalingPolicy(ctx, req, settings.GRPC...)
161		return err
162	}, opts...)
163	if err != nil {
164		return nil, err
165	}
166	return resp, nil
167}
168
169// GetAutoscalingPolicy retrieves autoscaling policy.
170func (c *AutoscalingPolicyClient) GetAutoscalingPolicy(ctx context.Context, req *dataprocpb.GetAutoscalingPolicyRequest, opts ...gax.CallOption) (*dataprocpb.AutoscalingPolicy, error) {
171	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", req.GetName()))
172	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
173	opts = append(c.CallOptions.GetAutoscalingPolicy[0:len(c.CallOptions.GetAutoscalingPolicy):len(c.CallOptions.GetAutoscalingPolicy)], opts...)
174	var resp *dataprocpb.AutoscalingPolicy
175	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
176		var err error
177		resp, err = c.autoscalingPolicyClient.GetAutoscalingPolicy(ctx, req, settings.GRPC...)
178		return err
179	}, opts...)
180	if err != nil {
181		return nil, err
182	}
183	return resp, nil
184}
185
186// ListAutoscalingPolicies lists autoscaling policies in the project.
187func (c *AutoscalingPolicyClient) ListAutoscalingPolicies(ctx context.Context, req *dataprocpb.ListAutoscalingPoliciesRequest, opts ...gax.CallOption) *AutoscalingPolicyIterator {
188	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", req.GetParent()))
189	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
190	opts = append(c.CallOptions.ListAutoscalingPolicies[0:len(c.CallOptions.ListAutoscalingPolicies):len(c.CallOptions.ListAutoscalingPolicies)], opts...)
191	it := &AutoscalingPolicyIterator{}
192	req = proto.Clone(req).(*dataprocpb.ListAutoscalingPoliciesRequest)
193	it.InternalFetch = func(pageSize int, pageToken string) ([]*dataprocpb.AutoscalingPolicy, string, error) {
194		var resp *dataprocpb.ListAutoscalingPoliciesResponse
195		req.PageToken = pageToken
196		if pageSize > math.MaxInt32 {
197			req.PageSize = math.MaxInt32
198		} else {
199			req.PageSize = int32(pageSize)
200		}
201		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
202			var err error
203			resp, err = c.autoscalingPolicyClient.ListAutoscalingPolicies(ctx, req, settings.GRPC...)
204			return err
205		}, opts...)
206		if err != nil {
207			return nil, "", err
208		}
209		return resp.Policies, resp.NextPageToken, nil
210	}
211	fetch := func(pageSize int, pageToken string) (string, error) {
212		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
213		if err != nil {
214			return "", err
215		}
216		it.items = append(it.items, items...)
217		return nextPageToken, nil
218	}
219	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
220	it.pageInfo.MaxSize = int(req.PageSize)
221	it.pageInfo.Token = req.PageToken
222	return it
223}
224
225// DeleteAutoscalingPolicy deletes an autoscaling policy. It is an error to delete an autoscaling
226// policy that is in use by one or more clusters.
227func (c *AutoscalingPolicyClient) DeleteAutoscalingPolicy(ctx context.Context, req *dataprocpb.DeleteAutoscalingPolicyRequest, opts ...gax.CallOption) error {
228	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", req.GetName()))
229	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
230	opts = append(c.CallOptions.DeleteAutoscalingPolicy[0:len(c.CallOptions.DeleteAutoscalingPolicy):len(c.CallOptions.DeleteAutoscalingPolicy)], opts...)
231	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
232		var err error
233		_, err = c.autoscalingPolicyClient.DeleteAutoscalingPolicy(ctx, req, settings.GRPC...)
234		return err
235	}, opts...)
236	return err
237}
238
239// AutoscalingPolicyIterator manages a stream of *dataprocpb.AutoscalingPolicy.
240type AutoscalingPolicyIterator struct {
241	items    []*dataprocpb.AutoscalingPolicy
242	pageInfo *iterator.PageInfo
243	nextFunc func() error
244
245	// InternalFetch is for use by the Google Cloud Libraries only.
246	// It is not part of the stable interface of this package.
247	//
248	// InternalFetch returns results from a single call to the underlying RPC.
249	// The number of results is no greater than pageSize.
250	// If there are no more results, nextPageToken is empty and err is nil.
251	InternalFetch func(pageSize int, pageToken string) (results []*dataprocpb.AutoscalingPolicy, nextPageToken string, err error)
252}
253
254// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
255func (it *AutoscalingPolicyIterator) PageInfo() *iterator.PageInfo {
256	return it.pageInfo
257}
258
259// Next returns the next result. Its second return value is iterator.Done if there are no more
260// results. Once Next returns Done, all subsequent calls will return Done.
261func (it *AutoscalingPolicyIterator) Next() (*dataprocpb.AutoscalingPolicy, error) {
262	var item *dataprocpb.AutoscalingPolicy
263	if err := it.nextFunc(); err != nil {
264		return item, err
265	}
266	item = it.items[0]
267	it.items = it.items[1:]
268	return item, nil
269}
270
271func (it *AutoscalingPolicyIterator) bufLen() int {
272	return len(it.items)
273}
274
275func (it *AutoscalingPolicyIterator) takeBuf() interface{} {
276	b := it.items
277	it.items = nil
278	return b
279}
280