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 cx
18
19import (
20	"context"
21	"fmt"
22	"math"
23	"net/url"
24	"time"
25
26	"github.com/golang/protobuf/proto"
27	gax "github.com/googleapis/gax-go/v2"
28	"google.golang.org/api/iterator"
29	"google.golang.org/api/option"
30	"google.golang.org/api/option/internaloption"
31	gtransport "google.golang.org/api/transport/grpc"
32	cxpb "google.golang.org/genproto/googleapis/cloud/dialogflow/cx/v3"
33	"google.golang.org/grpc"
34	"google.golang.org/grpc/codes"
35	"google.golang.org/grpc/metadata"
36)
37
38var newSecuritySettingsClientHook clientHook
39
40// SecuritySettingsCallOptions contains the retry settings for each method of SecuritySettingsClient.
41type SecuritySettingsCallOptions struct {
42	CreateSecuritySettings []gax.CallOption
43	GetSecuritySettings    []gax.CallOption
44	UpdateSecuritySettings []gax.CallOption
45	ListSecuritySettings   []gax.CallOption
46	DeleteSecuritySettings []gax.CallOption
47}
48
49func defaultSecuritySettingsClientOptions() []option.ClientOption {
50	return []option.ClientOption{
51		internaloption.WithDefaultEndpoint("dialogflow.googleapis.com:443"),
52		internaloption.WithDefaultMTLSEndpoint("dialogflow.mtls.googleapis.com:443"),
53		internaloption.WithDefaultAudience("https://dialogflow.googleapis.com/"),
54		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
55		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
56		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
57			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
58	}
59}
60
61func defaultSecuritySettingsCallOptions() *SecuritySettingsCallOptions {
62	return &SecuritySettingsCallOptions{
63		CreateSecuritySettings: []gax.CallOption{
64			gax.WithRetry(func() gax.Retryer {
65				return gax.OnCodes([]codes.Code{
66					codes.Unavailable,
67				}, gax.Backoff{
68					Initial:    100 * time.Millisecond,
69					Max:        60000 * time.Millisecond,
70					Multiplier: 1.30,
71				})
72			}),
73		},
74		GetSecuritySettings: []gax.CallOption{
75			gax.WithRetry(func() gax.Retryer {
76				return gax.OnCodes([]codes.Code{
77					codes.Unavailable,
78				}, gax.Backoff{
79					Initial:    100 * time.Millisecond,
80					Max:        60000 * time.Millisecond,
81					Multiplier: 1.30,
82				})
83			}),
84		},
85		UpdateSecuritySettings: []gax.CallOption{
86			gax.WithRetry(func() gax.Retryer {
87				return gax.OnCodes([]codes.Code{
88					codes.Unavailable,
89				}, gax.Backoff{
90					Initial:    100 * time.Millisecond,
91					Max:        60000 * time.Millisecond,
92					Multiplier: 1.30,
93				})
94			}),
95		},
96		ListSecuritySettings: []gax.CallOption{
97			gax.WithRetry(func() gax.Retryer {
98				return gax.OnCodes([]codes.Code{
99					codes.Unavailable,
100				}, gax.Backoff{
101					Initial:    100 * time.Millisecond,
102					Max:        60000 * time.Millisecond,
103					Multiplier: 1.30,
104				})
105			}),
106		},
107		DeleteSecuritySettings: []gax.CallOption{
108			gax.WithRetry(func() gax.Retryer {
109				return gax.OnCodes([]codes.Code{
110					codes.Unavailable,
111				}, gax.Backoff{
112					Initial:    100 * time.Millisecond,
113					Max:        60000 * time.Millisecond,
114					Multiplier: 1.30,
115				})
116			}),
117		},
118	}
119}
120
121// SecuritySettingsClient is a client for interacting with Dialogflow API.
122//
123// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
124type SecuritySettingsClient struct {
125	// Connection pool of gRPC connections to the service.
126	connPool gtransport.ConnPool
127
128	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
129	disableDeadlines bool
130
131	// The gRPC API client.
132	securitySettingsClient cxpb.SecuritySettingsServiceClient
133
134	// The call options for this service.
135	CallOptions *SecuritySettingsCallOptions
136
137	// The x-goog-* metadata to be sent with each request.
138	xGoogMetadata metadata.MD
139}
140
141// NewSecuritySettingsClient creates a new security settings service client.
142//
143// Service for managing security settings for Dialogflow.
144func NewSecuritySettingsClient(ctx context.Context, opts ...option.ClientOption) (*SecuritySettingsClient, error) {
145	clientOpts := defaultSecuritySettingsClientOptions()
146
147	if newSecuritySettingsClientHook != nil {
148		hookOpts, err := newSecuritySettingsClientHook(ctx, clientHookParams{})
149		if err != nil {
150			return nil, err
151		}
152		clientOpts = append(clientOpts, hookOpts...)
153	}
154
155	disableDeadlines, err := checkDisableDeadlines()
156	if err != nil {
157		return nil, err
158	}
159
160	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
161	if err != nil {
162		return nil, err
163	}
164	c := &SecuritySettingsClient{
165		connPool:         connPool,
166		disableDeadlines: disableDeadlines,
167		CallOptions:      defaultSecuritySettingsCallOptions(),
168
169		securitySettingsClient: cxpb.NewSecuritySettingsServiceClient(connPool),
170	}
171	c.setGoogleClientInfo()
172
173	return c, nil
174}
175
176// Connection returns a connection to the API service.
177//
178// Deprecated.
179func (c *SecuritySettingsClient) Connection() *grpc.ClientConn {
180	return c.connPool.Conn()
181}
182
183// Close closes the connection to the API service. The user should invoke this when
184// the client is no longer required.
185func (c *SecuritySettingsClient) Close() error {
186	return c.connPool.Close()
187}
188
189// setGoogleClientInfo sets the name and version of the application in
190// the `x-goog-api-client` header passed on each request. Intended for
191// use by Google-written clients.
192func (c *SecuritySettingsClient) setGoogleClientInfo(keyval ...string) {
193	kv := append([]string{"gl-go", versionGo()}, keyval...)
194	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
195	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
196}
197
198// CreateSecuritySettings create security settings in the specified location.
199func (c *SecuritySettingsClient) CreateSecuritySettings(ctx context.Context, req *cxpb.CreateSecuritySettingsRequest, opts ...gax.CallOption) (*cxpb.SecuritySettings, error) {
200	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
201		cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond)
202		defer cancel()
203		ctx = cctx
204	}
205	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
206	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
207	opts = append(c.CallOptions.CreateSecuritySettings[0:len(c.CallOptions.CreateSecuritySettings):len(c.CallOptions.CreateSecuritySettings)], opts...)
208	var resp *cxpb.SecuritySettings
209	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
210		var err error
211		resp, err = c.securitySettingsClient.CreateSecuritySettings(ctx, req, settings.GRPC...)
212		return err
213	}, opts...)
214	if err != nil {
215		return nil, err
216	}
217	return resp, nil
218}
219
220// GetSecuritySettings retrieves the specified SecuritySettings.
221// The returned settings may be stale by up to 1 minute.
222func (c *SecuritySettingsClient) GetSecuritySettings(ctx context.Context, req *cxpb.GetSecuritySettingsRequest, opts ...gax.CallOption) (*cxpb.SecuritySettings, error) {
223	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
224		cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond)
225		defer cancel()
226		ctx = cctx
227	}
228	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
229	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
230	opts = append(c.CallOptions.GetSecuritySettings[0:len(c.CallOptions.GetSecuritySettings):len(c.CallOptions.GetSecuritySettings)], opts...)
231	var resp *cxpb.SecuritySettings
232	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
233		var err error
234		resp, err = c.securitySettingsClient.GetSecuritySettings(ctx, req, settings.GRPC...)
235		return err
236	}, opts...)
237	if err != nil {
238		return nil, err
239	}
240	return resp, nil
241}
242
243// UpdateSecuritySettings updates the specified SecuritySettings.
244func (c *SecuritySettingsClient) UpdateSecuritySettings(ctx context.Context, req *cxpb.UpdateSecuritySettingsRequest, opts ...gax.CallOption) (*cxpb.SecuritySettings, error) {
245	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
246		cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond)
247		defer cancel()
248		ctx = cctx
249	}
250	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "security_settings.name", url.QueryEscape(req.GetSecuritySettings().GetName())))
251	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
252	opts = append(c.CallOptions.UpdateSecuritySettings[0:len(c.CallOptions.UpdateSecuritySettings):len(c.CallOptions.UpdateSecuritySettings)], opts...)
253	var resp *cxpb.SecuritySettings
254	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
255		var err error
256		resp, err = c.securitySettingsClient.UpdateSecuritySettings(ctx, req, settings.GRPC...)
257		return err
258	}, opts...)
259	if err != nil {
260		return nil, err
261	}
262	return resp, nil
263}
264
265// ListSecuritySettings returns the list of all security settings in the specified location.
266func (c *SecuritySettingsClient) ListSecuritySettings(ctx context.Context, req *cxpb.ListSecuritySettingsRequest, opts ...gax.CallOption) *SecuritySettingsIterator {
267	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
268	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
269	opts = append(c.CallOptions.ListSecuritySettings[0:len(c.CallOptions.ListSecuritySettings):len(c.CallOptions.ListSecuritySettings)], opts...)
270	it := &SecuritySettingsIterator{}
271	req = proto.Clone(req).(*cxpb.ListSecuritySettingsRequest)
272	it.InternalFetch = func(pageSize int, pageToken string) ([]*cxpb.SecuritySettings, string, error) {
273		var resp *cxpb.ListSecuritySettingsResponse
274		req.PageToken = pageToken
275		if pageSize > math.MaxInt32 {
276			req.PageSize = math.MaxInt32
277		} else {
278			req.PageSize = int32(pageSize)
279		}
280		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
281			var err error
282			resp, err = c.securitySettingsClient.ListSecuritySettings(ctx, req, settings.GRPC...)
283			return err
284		}, opts...)
285		if err != nil {
286			return nil, "", err
287		}
288
289		it.Response = resp
290		return resp.GetSecuritySettings(), resp.GetNextPageToken(), nil
291	}
292	fetch := func(pageSize int, pageToken string) (string, error) {
293		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
294		if err != nil {
295			return "", err
296		}
297		it.items = append(it.items, items...)
298		return nextPageToken, nil
299	}
300	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
301	it.pageInfo.MaxSize = int(req.GetPageSize())
302	it.pageInfo.Token = req.GetPageToken()
303	return it
304}
305
306// DeleteSecuritySettings deletes the specified SecuritySettings.
307func (c *SecuritySettingsClient) DeleteSecuritySettings(ctx context.Context, req *cxpb.DeleteSecuritySettingsRequest, opts ...gax.CallOption) error {
308	if _, ok := ctx.Deadline(); !ok && !c.disableDeadlines {
309		cctx, cancel := context.WithTimeout(ctx, 60000*time.Millisecond)
310		defer cancel()
311		ctx = cctx
312	}
313	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
314	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
315	opts = append(c.CallOptions.DeleteSecuritySettings[0:len(c.CallOptions.DeleteSecuritySettings):len(c.CallOptions.DeleteSecuritySettings)], opts...)
316	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
317		var err error
318		_, err = c.securitySettingsClient.DeleteSecuritySettings(ctx, req, settings.GRPC...)
319		return err
320	}, opts...)
321	return err
322}
323
324// SecuritySettingsIterator manages a stream of *cxpb.SecuritySettings.
325type SecuritySettingsIterator struct {
326	items    []*cxpb.SecuritySettings
327	pageInfo *iterator.PageInfo
328	nextFunc func() error
329
330	// Response is the raw response for the current page.
331	// It must be cast to the RPC response type.
332	// Calling Next() or InternalFetch() updates this value.
333	Response interface{}
334
335	// InternalFetch is for use by the Google Cloud Libraries only.
336	// It is not part of the stable interface of this package.
337	//
338	// InternalFetch returns results from a single call to the underlying RPC.
339	// The number of results is no greater than pageSize.
340	// If there are no more results, nextPageToken is empty and err is nil.
341	InternalFetch func(pageSize int, pageToken string) (results []*cxpb.SecuritySettings, nextPageToken string, err error)
342}
343
344// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
345func (it *SecuritySettingsIterator) PageInfo() *iterator.PageInfo {
346	return it.pageInfo
347}
348
349// Next returns the next result. Its second return value is iterator.Done if there are no more
350// results. Once Next returns Done, all subsequent calls will return Done.
351func (it *SecuritySettingsIterator) Next() (*cxpb.SecuritySettings, error) {
352	var item *cxpb.SecuritySettings
353	if err := it.nextFunc(); err != nil {
354		return item, err
355	}
356	item = it.items[0]
357	it.items = it.items[1:]
358	return item, nil
359}
360
361func (it *SecuritySettingsIterator) bufLen() int {
362	return len(it.items)
363}
364
365func (it *SecuritySettingsIterator) takeBuf() interface{} {
366	b := it.items
367	it.items = nil
368	return b
369}
370