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