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 pubsub
18
19import (
20	"context"
21	"fmt"
22	"math"
23	"net/url"
24
25	gax "github.com/googleapis/gax-go/v2"
26	"google.golang.org/api/iterator"
27	"google.golang.org/api/option"
28	"google.golang.org/api/option/internaloption"
29	gtransport "google.golang.org/api/transport/grpc"
30	iampb "google.golang.org/genproto/googleapis/iam/v1"
31	pubsubpb "google.golang.org/genproto/googleapis/pubsub/v1"
32	"google.golang.org/grpc"
33	"google.golang.org/grpc/metadata"
34	"google.golang.org/protobuf/proto"
35)
36
37var newSchemaClientHook clientHook
38
39// SchemaCallOptions contains the retry settings for each method of SchemaClient.
40type SchemaCallOptions struct {
41	CreateSchema       []gax.CallOption
42	GetSchema          []gax.CallOption
43	ListSchemas        []gax.CallOption
44	DeleteSchema       []gax.CallOption
45	ValidateSchema     []gax.CallOption
46	ValidateMessage    []gax.CallOption
47	GetIamPolicy       []gax.CallOption
48	SetIamPolicy       []gax.CallOption
49	TestIamPermissions []gax.CallOption
50}
51
52func defaultSchemaGRPCClientOptions() []option.ClientOption {
53	return []option.ClientOption{
54		internaloption.WithDefaultEndpoint("pubsub.googleapis.com:443"),
55		internaloption.WithDefaultMTLSEndpoint("pubsub.mtls.googleapis.com:443"),
56		internaloption.WithDefaultAudience("https://pubsub.googleapis.com/"),
57		internaloption.WithDefaultScopes(DefaultAuthScopes()...),
58		internaloption.EnableJwtWithScope(),
59		option.WithGRPCDialOption(grpc.WithDisableServiceConfig()),
60		option.WithGRPCDialOption(grpc.WithDefaultCallOptions(
61			grpc.MaxCallRecvMsgSize(math.MaxInt32))),
62	}
63}
64
65func defaultSchemaCallOptions() *SchemaCallOptions {
66	return &SchemaCallOptions{
67		CreateSchema:       []gax.CallOption{},
68		GetSchema:          []gax.CallOption{},
69		ListSchemas:        []gax.CallOption{},
70		DeleteSchema:       []gax.CallOption{},
71		ValidateSchema:     []gax.CallOption{},
72		ValidateMessage:    []gax.CallOption{},
73		GetIamPolicy:       []gax.CallOption{},
74		SetIamPolicy:       []gax.CallOption{},
75		TestIamPermissions: []gax.CallOption{},
76	}
77}
78
79// internalSchemaClient is an interface that defines the methods availaible from Cloud Pub/Sub API.
80type internalSchemaClient interface {
81	Close() error
82	setGoogleClientInfo(...string)
83	Connection() *grpc.ClientConn
84	CreateSchema(context.Context, *pubsubpb.CreateSchemaRequest, ...gax.CallOption) (*pubsubpb.Schema, error)
85	GetSchema(context.Context, *pubsubpb.GetSchemaRequest, ...gax.CallOption) (*pubsubpb.Schema, error)
86	ListSchemas(context.Context, *pubsubpb.ListSchemasRequest, ...gax.CallOption) *SchemaIterator
87	DeleteSchema(context.Context, *pubsubpb.DeleteSchemaRequest, ...gax.CallOption) error
88	ValidateSchema(context.Context, *pubsubpb.ValidateSchemaRequest, ...gax.CallOption) (*pubsubpb.ValidateSchemaResponse, error)
89	ValidateMessage(context.Context, *pubsubpb.ValidateMessageRequest, ...gax.CallOption) (*pubsubpb.ValidateMessageResponse, error)
90	GetIamPolicy(context.Context, *iampb.GetIamPolicyRequest, ...gax.CallOption) (*iampb.Policy, error)
91	SetIamPolicy(context.Context, *iampb.SetIamPolicyRequest, ...gax.CallOption) (*iampb.Policy, error)
92	TestIamPermissions(context.Context, *iampb.TestIamPermissionsRequest, ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error)
93}
94
95// SchemaClient is a client for interacting with Cloud Pub/Sub API.
96// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
97//
98// Service for doing schema-related operations.
99type SchemaClient struct {
100	// The internal transport-dependent client.
101	internalClient internalSchemaClient
102
103	// The call options for this service.
104	CallOptions *SchemaCallOptions
105}
106
107// Wrapper methods routed to the internal client.
108
109// Close closes the connection to the API service. The user should invoke this when
110// the client is no longer required.
111func (c *SchemaClient) Close() error {
112	return c.internalClient.Close()
113}
114
115// setGoogleClientInfo sets the name and version of the application in
116// the `x-goog-api-client` header passed on each request. Intended for
117// use by Google-written clients.
118func (c *SchemaClient) setGoogleClientInfo(keyval ...string) {
119	c.internalClient.setGoogleClientInfo(keyval...)
120}
121
122// Connection returns a connection to the API service.
123//
124// Deprecated.
125func (c *SchemaClient) Connection() *grpc.ClientConn {
126	return c.internalClient.Connection()
127}
128
129// CreateSchema creates a schema.
130func (c *SchemaClient) CreateSchema(ctx context.Context, req *pubsubpb.CreateSchemaRequest, opts ...gax.CallOption) (*pubsubpb.Schema, error) {
131	return c.internalClient.CreateSchema(ctx, req, opts...)
132}
133
134// GetSchema gets a schema.
135func (c *SchemaClient) GetSchema(ctx context.Context, req *pubsubpb.GetSchemaRequest, opts ...gax.CallOption) (*pubsubpb.Schema, error) {
136	return c.internalClient.GetSchema(ctx, req, opts...)
137}
138
139// ListSchemas lists schemas in a project.
140func (c *SchemaClient) ListSchemas(ctx context.Context, req *pubsubpb.ListSchemasRequest, opts ...gax.CallOption) *SchemaIterator {
141	return c.internalClient.ListSchemas(ctx, req, opts...)
142}
143
144// DeleteSchema deletes a schema.
145func (c *SchemaClient) DeleteSchema(ctx context.Context, req *pubsubpb.DeleteSchemaRequest, opts ...gax.CallOption) error {
146	return c.internalClient.DeleteSchema(ctx, req, opts...)
147}
148
149// ValidateSchema validates a schema.
150func (c *SchemaClient) ValidateSchema(ctx context.Context, req *pubsubpb.ValidateSchemaRequest, opts ...gax.CallOption) (*pubsubpb.ValidateSchemaResponse, error) {
151	return c.internalClient.ValidateSchema(ctx, req, opts...)
152}
153
154// ValidateMessage validates a message against a schema.
155func (c *SchemaClient) ValidateMessage(ctx context.Context, req *pubsubpb.ValidateMessageRequest, opts ...gax.CallOption) (*pubsubpb.ValidateMessageResponse, error) {
156	return c.internalClient.ValidateMessage(ctx, req, opts...)
157}
158
159// GetIamPolicy gets the access control policy for a resource. Returns an empty policy
160// if the resource exists and does not have a policy set.
161func (c *SchemaClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
162	return c.internalClient.GetIamPolicy(ctx, req, opts...)
163}
164
165// SetIamPolicy sets the access control policy on the specified resource. Replaces
166// any existing policy.
167//
168// Can return NOT_FOUND, INVALID_ARGUMENT, and PERMISSION_DENIED
169// errors.
170func (c *SchemaClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
171	return c.internalClient.SetIamPolicy(ctx, req, opts...)
172}
173
174// TestIamPermissions returns permissions that a caller has on the specified resource. If the
175// resource does not exist, this will return an empty set of
176// permissions, not a NOT_FOUND error.
177//
178// Note: This operation is designed to be used for building
179// permission-aware UIs and command-line tools, not for authorization
180// checking. This operation may “fail open” without warning.
181func (c *SchemaClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
182	return c.internalClient.TestIamPermissions(ctx, req, opts...)
183}
184
185// schemaGRPCClient is a client for interacting with Cloud Pub/Sub API over gRPC transport.
186//
187// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
188type schemaGRPCClient struct {
189	// Connection pool of gRPC connections to the service.
190	connPool gtransport.ConnPool
191
192	// flag to opt out of default deadlines via GOOGLE_API_GO_EXPERIMENTAL_DISABLE_DEFAULT_DEADLINE
193	disableDeadlines bool
194
195	// Points back to the CallOptions field of the containing SchemaClient
196	CallOptions **SchemaCallOptions
197
198	// The gRPC API client.
199	schemaClient pubsubpb.SchemaServiceClient
200
201	iamPolicyClient iampb.IAMPolicyClient
202
203	// The x-goog-* metadata to be sent with each request.
204	xGoogMetadata metadata.MD
205}
206
207// NewSchemaClient creates a new schema service client based on gRPC.
208// The returned client must be Closed when it is done being used to clean up its underlying connections.
209//
210// Service for doing schema-related operations.
211func NewSchemaClient(ctx context.Context, opts ...option.ClientOption) (*SchemaClient, error) {
212	clientOpts := defaultSchemaGRPCClientOptions()
213	if newSchemaClientHook != nil {
214		hookOpts, err := newSchemaClientHook(ctx, clientHookParams{})
215		if err != nil {
216			return nil, err
217		}
218		clientOpts = append(clientOpts, hookOpts...)
219	}
220
221	disableDeadlines, err := checkDisableDeadlines()
222	if err != nil {
223		return nil, err
224	}
225
226	connPool, err := gtransport.DialPool(ctx, append(clientOpts, opts...)...)
227	if err != nil {
228		return nil, err
229	}
230	client := SchemaClient{CallOptions: defaultSchemaCallOptions()}
231
232	c := &schemaGRPCClient{
233		connPool:         connPool,
234		disableDeadlines: disableDeadlines,
235		schemaClient:     pubsubpb.NewSchemaServiceClient(connPool),
236		CallOptions:      &client.CallOptions,
237		iamPolicyClient:  iampb.NewIAMPolicyClient(connPool),
238	}
239	c.setGoogleClientInfo()
240
241	client.internalClient = c
242
243	return &client, nil
244}
245
246// Connection returns a connection to the API service.
247//
248// Deprecated.
249func (c *schemaGRPCClient) Connection() *grpc.ClientConn {
250	return c.connPool.Conn()
251}
252
253// setGoogleClientInfo sets the name and version of the application in
254// the `x-goog-api-client` header passed on each request. Intended for
255// use by Google-written clients.
256func (c *schemaGRPCClient) setGoogleClientInfo(keyval ...string) {
257	kv := append([]string{"gl-go", versionGo()}, keyval...)
258	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
259	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
260}
261
262// Close closes the connection to the API service. The user should invoke this when
263// the client is no longer required.
264func (c *schemaGRPCClient) Close() error {
265	return c.connPool.Close()
266}
267
268func (c *schemaGRPCClient) CreateSchema(ctx context.Context, req *pubsubpb.CreateSchemaRequest, opts ...gax.CallOption) (*pubsubpb.Schema, error) {
269	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
270	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
271	opts = append((*c.CallOptions).CreateSchema[0:len((*c.CallOptions).CreateSchema):len((*c.CallOptions).CreateSchema)], opts...)
272	var resp *pubsubpb.Schema
273	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
274		var err error
275		resp, err = c.schemaClient.CreateSchema(ctx, req, settings.GRPC...)
276		return err
277	}, opts...)
278	if err != nil {
279		return nil, err
280	}
281	return resp, nil
282}
283
284func (c *schemaGRPCClient) GetSchema(ctx context.Context, req *pubsubpb.GetSchemaRequest, opts ...gax.CallOption) (*pubsubpb.Schema, error) {
285	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
286	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
287	opts = append((*c.CallOptions).GetSchema[0:len((*c.CallOptions).GetSchema):len((*c.CallOptions).GetSchema)], opts...)
288	var resp *pubsubpb.Schema
289	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
290		var err error
291		resp, err = c.schemaClient.GetSchema(ctx, req, settings.GRPC...)
292		return err
293	}, opts...)
294	if err != nil {
295		return nil, err
296	}
297	return resp, nil
298}
299
300func (c *schemaGRPCClient) ListSchemas(ctx context.Context, req *pubsubpb.ListSchemasRequest, opts ...gax.CallOption) *SchemaIterator {
301	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
302	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
303	opts = append((*c.CallOptions).ListSchemas[0:len((*c.CallOptions).ListSchemas):len((*c.CallOptions).ListSchemas)], opts...)
304	it := &SchemaIterator{}
305	req = proto.Clone(req).(*pubsubpb.ListSchemasRequest)
306	it.InternalFetch = func(pageSize int, pageToken string) ([]*pubsubpb.Schema, string, error) {
307		resp := &pubsubpb.ListSchemasResponse{}
308		if pageToken != "" {
309			req.PageToken = pageToken
310		}
311		if pageSize > math.MaxInt32 {
312			req.PageSize = math.MaxInt32
313		} else if pageSize != 0 {
314			req.PageSize = int32(pageSize)
315		}
316		err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
317			var err error
318			resp, err = c.schemaClient.ListSchemas(ctx, req, settings.GRPC...)
319			return err
320		}, opts...)
321		if err != nil {
322			return nil, "", err
323		}
324
325		it.Response = resp
326		return resp.GetSchemas(), resp.GetNextPageToken(), nil
327	}
328	fetch := func(pageSize int, pageToken string) (string, error) {
329		items, nextPageToken, err := it.InternalFetch(pageSize, pageToken)
330		if err != nil {
331			return "", err
332		}
333		it.items = append(it.items, items...)
334		return nextPageToken, nil
335	}
336
337	it.pageInfo, it.nextFunc = iterator.NewPageInfo(fetch, it.bufLen, it.takeBuf)
338	it.pageInfo.MaxSize = int(req.GetPageSize())
339	it.pageInfo.Token = req.GetPageToken()
340
341	return it
342}
343
344func (c *schemaGRPCClient) DeleteSchema(ctx context.Context, req *pubsubpb.DeleteSchemaRequest, opts ...gax.CallOption) error {
345	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName())))
346	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
347	opts = append((*c.CallOptions).DeleteSchema[0:len((*c.CallOptions).DeleteSchema):len((*c.CallOptions).DeleteSchema)], opts...)
348	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
349		var err error
350		_, err = c.schemaClient.DeleteSchema(ctx, req, settings.GRPC...)
351		return err
352	}, opts...)
353	return err
354}
355
356func (c *schemaGRPCClient) ValidateSchema(ctx context.Context, req *pubsubpb.ValidateSchemaRequest, opts ...gax.CallOption) (*pubsubpb.ValidateSchemaResponse, error) {
357	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
358	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
359	opts = append((*c.CallOptions).ValidateSchema[0:len((*c.CallOptions).ValidateSchema):len((*c.CallOptions).ValidateSchema)], opts...)
360	var resp *pubsubpb.ValidateSchemaResponse
361	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
362		var err error
363		resp, err = c.schemaClient.ValidateSchema(ctx, req, settings.GRPC...)
364		return err
365	}, opts...)
366	if err != nil {
367		return nil, err
368	}
369	return resp, nil
370}
371
372func (c *schemaGRPCClient) ValidateMessage(ctx context.Context, req *pubsubpb.ValidateMessageRequest, opts ...gax.CallOption) (*pubsubpb.ValidateMessageResponse, error) {
373	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "parent", url.QueryEscape(req.GetParent())))
374	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
375	opts = append((*c.CallOptions).ValidateMessage[0:len((*c.CallOptions).ValidateMessage):len((*c.CallOptions).ValidateMessage)], opts...)
376	var resp *pubsubpb.ValidateMessageResponse
377	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
378		var err error
379		resp, err = c.schemaClient.ValidateMessage(ctx, req, settings.GRPC...)
380		return err
381	}, opts...)
382	if err != nil {
383		return nil, err
384	}
385	return resp, nil
386}
387
388func (c *schemaGRPCClient) GetIamPolicy(ctx context.Context, req *iampb.GetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
389	ctx = insertMetadata(ctx, c.xGoogMetadata)
390	opts = append((*c.CallOptions).GetIamPolicy[0:len((*c.CallOptions).GetIamPolicy):len((*c.CallOptions).GetIamPolicy)], opts...)
391	var resp *iampb.Policy
392	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
393		var err error
394		resp, err = c.iamPolicyClient.GetIamPolicy(ctx, req, settings.GRPC...)
395		return err
396	}, opts...)
397	if err != nil {
398		return nil, err
399	}
400	return resp, nil
401}
402
403func (c *schemaGRPCClient) SetIamPolicy(ctx context.Context, req *iampb.SetIamPolicyRequest, opts ...gax.CallOption) (*iampb.Policy, error) {
404	ctx = insertMetadata(ctx, c.xGoogMetadata)
405	opts = append((*c.CallOptions).SetIamPolicy[0:len((*c.CallOptions).SetIamPolicy):len((*c.CallOptions).SetIamPolicy)], opts...)
406	var resp *iampb.Policy
407	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
408		var err error
409		resp, err = c.iamPolicyClient.SetIamPolicy(ctx, req, settings.GRPC...)
410		return err
411	}, opts...)
412	if err != nil {
413		return nil, err
414	}
415	return resp, nil
416}
417
418func (c *schemaGRPCClient) TestIamPermissions(ctx context.Context, req *iampb.TestIamPermissionsRequest, opts ...gax.CallOption) (*iampb.TestIamPermissionsResponse, error) {
419	ctx = insertMetadata(ctx, c.xGoogMetadata)
420	opts = append((*c.CallOptions).TestIamPermissions[0:len((*c.CallOptions).TestIamPermissions):len((*c.CallOptions).TestIamPermissions)], opts...)
421	var resp *iampb.TestIamPermissionsResponse
422	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
423		var err error
424		resp, err = c.iamPolicyClient.TestIamPermissions(ctx, req, settings.GRPC...)
425		return err
426	}, opts...)
427	if err != nil {
428		return nil, err
429	}
430	return resp, nil
431}
432
433// SchemaIterator manages a stream of *pubsubpb.Schema.
434type SchemaIterator struct {
435	items    []*pubsubpb.Schema
436	pageInfo *iterator.PageInfo
437	nextFunc func() error
438
439	// Response is the raw response for the current page.
440	// It must be cast to the RPC response type.
441	// Calling Next() or InternalFetch() updates this value.
442	Response interface{}
443
444	// InternalFetch is for use by the Google Cloud Libraries only.
445	// It is not part of the stable interface of this package.
446	//
447	// InternalFetch returns results from a single call to the underlying RPC.
448	// The number of results is no greater than pageSize.
449	// If there are no more results, nextPageToken is empty and err is nil.
450	InternalFetch func(pageSize int, pageToken string) (results []*pubsubpb.Schema, nextPageToken string, err error)
451}
452
453// PageInfo supports pagination. See the google.golang.org/api/iterator package for details.
454func (it *SchemaIterator) PageInfo() *iterator.PageInfo {
455	return it.pageInfo
456}
457
458// Next returns the next result. Its second return value is iterator.Done if there are no more
459// results. Once Next returns Done, all subsequent calls will return Done.
460func (it *SchemaIterator) Next() (*pubsubpb.Schema, error) {
461	var item *pubsubpb.Schema
462	if err := it.nextFunc(); err != nil {
463		return item, err
464	}
465	item = it.items[0]
466	it.items = it.items[1:]
467	return item, nil
468}
469
470func (it *SchemaIterator) bufLen() int {
471	return len(it.items)
472}
473
474func (it *SchemaIterator) takeBuf() interface{} {
475	b := it.items
476	it.items = nil
477	return b
478}
479