google-cloud-pubsub 2.23.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -0
  3. data/OVERVIEW.md +188 -144
  4. data/lib/google/cloud/pubsub/admin_clients.rb +116 -0
  5. data/lib/google/cloud/pubsub/async_publisher.rb +9 -9
  6. data/lib/google/cloud/pubsub/batch_publisher.rb +4 -4
  7. data/lib/google/cloud/pubsub/errors.rb +3 -3
  8. data/lib/google/cloud/pubsub/message.rb +8 -8
  9. data/lib/google/cloud/pubsub/{subscriber → message_listener}/enumerator_queue.rb +1 -1
  10. data/lib/google/cloud/pubsub/{subscriber → message_listener}/inventory.rb +2 -4
  11. data/lib/google/cloud/pubsub/{subscriber → message_listener}/sequencer.rb +1 -1
  12. data/lib/google/cloud/pubsub/{subscriber → message_listener}/stream.rb +10 -10
  13. data/lib/google/cloud/pubsub/{subscriber → message_listener}/timed_unary_buffer.rb +1 -1
  14. data/lib/google/cloud/pubsub/message_listener.rb +413 -0
  15. data/lib/google/cloud/pubsub/project.rb +98 -531
  16. data/lib/google/cloud/pubsub/publisher.rb +373 -0
  17. data/lib/google/cloud/pubsub/received_message.rb +44 -39
  18. data/lib/google/cloud/pubsub/service.rb +24 -386
  19. data/lib/google/cloud/pubsub/subscriber.rb +442 -279
  20. data/lib/google/cloud/pubsub/version.rb +1 -1
  21. data/lib/google/cloud/pubsub.rb +8 -15
  22. data/lib/google-cloud-pubsub.rb +5 -4
  23. metadata +9 -17
  24. data/lib/google/cloud/pubsub/policy.rb +0 -188
  25. data/lib/google/cloud/pubsub/retry_policy.rb +0 -88
  26. data/lib/google/cloud/pubsub/schema/list.rb +0 -180
  27. data/lib/google/cloud/pubsub/schema.rb +0 -378
  28. data/lib/google/cloud/pubsub/snapshot/list.rb +0 -178
  29. data/lib/google/cloud/pubsub/snapshot.rb +0 -205
  30. data/lib/google/cloud/pubsub/subscription/list.rb +0 -205
  31. data/lib/google/cloud/pubsub/subscription/push_config.rb +0 -268
  32. data/lib/google/cloud/pubsub/subscription.rb +0 -1467
  33. data/lib/google/cloud/pubsub/topic/list.rb +0 -171
  34. data/lib/google/cloud/pubsub/topic.rb +0 -1100
@@ -18,6 +18,7 @@ require "google/cloud/pubsub/credentials"
18
18
  require "google/cloud/pubsub/convert"
19
19
  require "google/cloud/pubsub/version"
20
20
  require "google/cloud/pubsub/v1"
21
+ require "google/cloud/pubsub/admin_clients"
21
22
  require "securerandom"
22
23
 
23
24
  module Google
@@ -50,9 +51,9 @@ module Google
50
51
  @universe_domain = universe_domain || ENV["GOOGLE_CLOUD_UNIVERSE_DOMAIN"] || "googleapis.com"
51
52
  end
52
53
 
53
- def subscriber
54
- return mocked_subscriber if mocked_subscriber
55
- @subscriber ||= V1::Subscriber::Client.new do |config|
54
+ def subscription_admin
55
+ return mocked_subscription_admin if mocked_subscription_admin
56
+ @subscription_admin ||= SubscriptionAdmin::Client.new do |config|
56
57
  config.credentials = credentials if credentials
57
58
  override_client_config_timeouts config if timeout
58
59
  config.endpoint = host if host
@@ -62,11 +63,11 @@ module Google
62
63
  config.metadata = { "google-cloud-resource-prefix": "projects/#{@project}" }
63
64
  end
64
65
  end
65
- attr_accessor :mocked_subscriber
66
+ attr_accessor :mocked_subscription_admin
66
67
 
67
- def publisher
68
- return mocked_publisher if mocked_publisher
69
- @publisher ||= V1::Publisher::Client.new do |config|
68
+ def topic_admin
69
+ return mocked_topic_admin if mocked_topic_admin
70
+ @topic_admin ||= TopicAdmin::Client.new do |config|
70
71
  config.credentials = credentials if credentials
71
72
  override_client_config_timeouts config if timeout
72
73
  config.endpoint = host if host
@@ -76,12 +77,12 @@ module Google
76
77
  config.metadata = { "google-cloud-resource-prefix": "projects/#{@project}" }
77
78
  end
78
79
  end
79
- attr_accessor :mocked_publisher
80
+ attr_accessor :mocked_topic_admin
80
81
 
81
82
  def iam
82
83
  return mocked_iam if mocked_iam
83
84
  @iam ||= begin
84
- iam = (@publisher || @subscriber || @schemas || subscriber).iam_policy_client
85
+ iam = (@topic_admin || @subscription_admin || @schemas).iam_policy_client
85
86
  iam.configure do |config|
86
87
  override_client_config_timeouts config if timeout
87
88
  config.lib_name = "gccl"
@@ -107,77 +108,6 @@ module Google
107
108
  end
108
109
  attr_accessor :mocked_schemas
109
110
 
110
- ##
111
- # Gets the configuration of a topic.
112
- # Since the topic only has the name attribute,
113
- # this method is only useful to check the existence of a topic.
114
- # If other attributes are added in the future,
115
- # they will be returned here.
116
- def get_topic topic_name, options = {}
117
- publisher.get_topic topic: topic_path(topic_name, options)
118
- end
119
-
120
- ##
121
- # Lists matching topics.
122
- def list_topics options = {}
123
- paged_enum = publisher.list_topics project: project_path(options),
124
- page_size: options[:max],
125
- page_token: options[:token]
126
-
127
- paged_enum.response
128
- end
129
-
130
- ##
131
- # Creates the given topic with the given name.
132
- def create_topic topic_name,
133
- labels: nil,
134
- kms_key_name: nil,
135
- persistence_regions: nil,
136
- schema_name: nil,
137
- message_encoding: nil,
138
- retention: nil,
139
- ingestion_data_source_settings: nil,
140
- options: {}
141
- if persistence_regions
142
- message_storage_policy = Google::Cloud::PubSub::V1::MessageStoragePolicy.new(
143
- allowed_persistence_regions: Array(persistence_regions)
144
- )
145
- end
146
-
147
- if schema_name || message_encoding
148
- unless schema_name && message_encoding
149
- raise ArgumentError, "Schema settings must include both schema_name and message_encoding."
150
- end
151
- schema_settings = Google::Cloud::PubSub::V1::SchemaSettings.new(
152
- schema: schema_path(schema_name),
153
- encoding: message_encoding.to_s.upcase
154
- )
155
- end
156
-
157
- publisher.create_topic \
158
- name: topic_path(topic_name, options),
159
- labels: labels,
160
- kms_key_name: kms_key_name,
161
- message_storage_policy: message_storage_policy,
162
- schema_settings: schema_settings,
163
- message_retention_duration: Convert.number_to_duration(retention),
164
- ingestion_data_source_settings: ingestion_data_source_settings
165
- end
166
-
167
- def update_topic topic_obj, *fields
168
- mask = Google::Protobuf::FieldMask.new paths: fields.map(&:to_s)
169
- publisher.update_topic topic: topic_obj, update_mask: mask
170
- end
171
-
172
- ##
173
- # Deletes the topic with the given name. All subscriptions to this topic
174
- # are also deleted. Raises GRPC status code 5 if the topic does not
175
- # exist. After a topic is deleted, a new topic may be created with the
176
- # same name.
177
- def delete_topic topic_name
178
- publisher.delete_topic topic: topic_path(topic_name)
179
- end
180
-
181
111
  ##
182
112
  # Adds one or more messages to the topic.
183
113
  # Raises GRPC status code 5 if the topic does not exist.
@@ -186,57 +116,11 @@ module Google
186
116
  def publish topic, messages, compress: false
187
117
  request = { topic: topic_path(topic), messages: messages }
188
118
  compress_options = ::Gapic::CallOptions.new metadata: { "grpc-internal-encoding-request": "gzip" }
189
- compress ? (publisher.publish request, compress_options) : (publisher.publish request)
190
- end
191
-
192
- ##
193
- # Gets the details of a subscription.
194
- def get_subscription subscription_name, options = {}
195
- subscriber.get_subscription subscription: subscription_path(subscription_name, options)
196
- end
197
-
198
- ##
199
- # Lists matching subscriptions by project and topic.
200
- def list_topics_subscriptions topic, options = {}
201
- publisher.list_topic_subscriptions topic: topic_path(topic, options),
202
- page_size: options[:max],
203
- page_token: options[:token]
204
- end
205
-
206
- ##
207
- # Lists matching subscriptions by project.
208
- def list_subscriptions options = {}
209
- paged_enum = subscriber.list_subscriptions project: project_path(options),
210
- page_size: options[:max],
211
- page_token: options[:token]
212
-
213
- paged_enum.response
214
- end
215
-
216
- ##
217
- # Creates a subscription on a given topic for a given subscriber.
218
- def create_subscription topic, subscription_name, options = {}
219
- updated_option = construct_create_subscription_options topic, subscription_name, options
220
- subscriber.create_subscription(**updated_option)
221
- end
222
-
223
- def update_subscription subscription_obj, *fields
224
- mask = Google::Protobuf::FieldMask.new paths: fields.map(&:to_s)
225
- subscriber.update_subscription subscription: subscription_obj, update_mask: mask
226
- end
227
-
228
- ##
229
- # Deletes an existing subscription. All pending messages in the subscription are immediately dropped.
230
- def delete_subscription subscription
231
- subscriber.delete_subscription subscription: subscription_path(subscription)
232
- end
233
-
234
- ##
235
- # Detaches a subscription from its topic. All messages retained in the subscription are dropped. Subsequent
236
- # `Pull` and `StreamingPull` requests will raise `FAILED_PRECONDITION`. If the subscription is a push
237
- # subscription, pushes to the endpoint will stop.
238
- def detach_subscription subscription
239
- publisher.detach_subscription subscription: subscription_path(subscription)
119
+ if compress
120
+ (topic_admin.publish_internal request, compress_options)
121
+ else
122
+ (topic_admin.publish_internal request)
123
+ end
240
124
  end
241
125
 
242
126
  ##
@@ -245,233 +129,32 @@ module Google
245
129
  max_messages = options.fetch(:max, 100).to_i
246
130
  return_immediately = !(!options.fetch(:immediate, true))
247
131
 
248
- subscriber.pull subscription: subscription_path(subscription, options),
249
- max_messages: max_messages,
250
- return_immediately: return_immediately
132
+ subscription_admin.pull_internal subscription: subscription_path(subscription, options),
133
+ max_messages: max_messages,
134
+ return_immediately: return_immediately
251
135
  end
252
136
 
253
137
  def streaming_pull request_enum, options = {}
254
- subscriber.streaming_pull request_enum, options
138
+ subscription_admin.streaming_pull_internal request_enum, options
255
139
  end
256
140
 
257
141
  ##
258
142
  # Acknowledges receipt of a message.
259
143
  def acknowledge subscription, *ack_ids
260
- subscriber.acknowledge subscription: subscription_path(subscription), ack_ids: ack_ids
261
- end
262
-
263
- ##
264
- # Modifies the PushConfig for a specified subscription.
265
- def modify_push_config subscription, endpoint, attributes
266
- # Convert attributes to strings to match the protobuf definition
267
- attributes = attributes.to_h { |k, v| [String(k), String(v)] }
268
- push_config = Google::Cloud::PubSub::V1::PushConfig.new(
269
- push_endpoint: endpoint,
270
- attributes: attributes
271
- )
272
-
273
- subscriber.modify_push_config subscription: subscription_path(subscription),
274
- push_config: push_config
144
+ subscription_admin.acknowledge_internal subscription: subscription_path(subscription),
145
+ ack_ids: ack_ids
275
146
  end
276
147
 
277
148
  ##
278
149
  # Modifies the ack deadline for a specific message.
279
150
  def modify_ack_deadline subscription, ids, deadline
280
- subscriber.modify_ack_deadline subscription: subscription_path(subscription),
281
- ack_ids: Array(ids),
282
- ack_deadline_seconds: deadline
283
- end
284
-
285
- ##
286
- # Lists snapshots by project.
287
- def list_snapshots options = {}
288
- paged_enum = subscriber.list_snapshots project: project_path(options),
289
- page_size: options[:max],
290
- page_token: options[:token]
291
-
292
- paged_enum.response
293
- end
294
-
295
- ##
296
- # Creates a snapshot on a given subscription.
297
- def create_snapshot subscription, snapshot_name, labels: nil
298
- subscriber.create_snapshot name: snapshot_path(snapshot_name),
299
- subscription: subscription_path(subscription),
300
- labels: labels
301
- end
302
-
303
- def update_snapshot snapshot_obj, *fields
304
- mask = Google::Protobuf::FieldMask.new paths: fields.map(&:to_s)
305
- subscriber.update_snapshot snapshot: snapshot_obj, update_mask: mask
306
- end
307
-
308
- ##
309
- # Deletes an existing snapshot.
310
- # All pending messages in the snapshot are immediately dropped.
311
- def delete_snapshot snapshot
312
- subscriber.delete_snapshot snapshot: snapshot_path(snapshot)
313
- end
314
-
315
- ##
316
- # Adjusts the given subscription to a time or snapshot.
317
- def seek subscription, time_or_snapshot
318
- if a_time? time_or_snapshot
319
- time = Convert.time_to_timestamp time_or_snapshot
320
- subscriber.seek subscription: subscription, time: time
321
- else
322
- time_or_snapshot = time_or_snapshot.name if time_or_snapshot.is_a? Snapshot
323
- subscriber.seek subscription: subscription_path(subscription),
324
- snapshot: snapshot_path(time_or_snapshot)
325
- end
326
- end
327
-
328
- ##
329
- # Lists schemas in the current (or given) project.
330
- # @param view [String, Symbol, nil] Possible values:
331
- # * `BASIC` - Include the name and type of the schema, but not the definition.
332
- # * `FULL` - Include all Schema object fields.
333
- #
334
- def list_schemas view, options = {}
335
- schema_view = Google::Cloud::PubSub::V1::SchemaView.const_get view.to_s.upcase
336
- paged_enum = schemas.list_schemas parent: project_path(options),
337
- view: schema_view,
338
- page_size: options[:max],
339
- page_token: options[:token]
340
-
341
- paged_enum.response
342
- end
343
-
344
- ##
345
- # Lists all schema revisions for the named schema.
346
- # @param name [String] The name of the schema to list revisions for.
347
- # @param view [String, Symbol, nil] Possible values:
348
- # * `BASIC` - Include the name and type of the schema, but not the definition.
349
- # * `FULL` - Include all Schema object fields.
350
- #
351
- def list_schema_revisions name, view, page_size, page_token
352
- schema_view = Google::Cloud::PubSub::V1::SchemaView.const_get view.to_s.upcase
353
- schemas.list_schema_revisions name: name,
354
- view: schema_view,
355
- page_size: page_size,
356
- page_token: page_token
357
- end
358
-
359
- ##
360
- # Creates a schema in the current (or given) project.
361
- def create_schema schema_id, type, definition, options = {}
362
- schema = Google::Cloud::PubSub::V1::Schema.new(
363
- type: type,
364
- definition: definition
365
- )
366
- schemas.create_schema parent: project_path(options),
367
- schema: schema,
368
- schema_id: schema_id
369
- end
370
-
371
- ##
372
- # Gets the details of a schema.
373
- # @param view [String, Symbol, nil] The set of fields to return in the response. Possible values:
374
- # * `BASIC` - Include the name and type of the schema, but not the definition.
375
- # * `FULL` - Include all Schema object fields.
376
- #
377
- def get_schema schema_name, view, options = {}
378
- schema_view = Google::Cloud::PubSub::V1::SchemaView.const_get view.to_s.upcase
379
- schemas.get_schema name: schema_path(schema_name, options),
380
- view: schema_view
381
- end
382
-
383
- ##
384
- # Delete a schema.
385
- def delete_schema schema_name
386
- schemas.delete_schema name: schema_path(schema_name)
387
- end
388
-
389
- ##
390
- # Commits a new schema revision to an existing schema.
391
- #
392
- # @param name [String] The name of the schema to revision.
393
- # @param definition [String] The definition of the schema. This should
394
- # contain a string representing the full definition of the schema that
395
- # is a valid schema definition of the type specified in `type`. See
396
- # https://cloud.google.com/pubsub/docs/schemas for details.
397
- # @param type [String, Symbol] The type of the schema. Required. Possible
398
- # values are case-insensitive and include:
399
- #
400
- # * `PROTOCOL_BUFFER` - A Protocol Buffer schema definition.
401
- # * `AVRO` - An Avro schema definition.
402
- #
403
- # @return [Google::Cloud::PubSub::V1::Schema]
404
- #
405
- def commit_schema name, definition, type
406
- schema = Google::Cloud::PubSub::V1::Schema.new(
407
- name: name,
408
- definition: definition,
409
- type: type
410
- )
411
- schemas.commit_schema name: name, schema: schema
412
- end
413
-
414
- ##
415
- # Validate the definition string intended for a schema.
416
- def validate_schema type, definition, options = {}
417
- schema = Google::Cloud::PubSub::V1::Schema.new(
418
- type: type,
419
- definition: definition
420
- )
421
- schemas.validate_schema parent: project_path(options),
422
- schema: schema
423
- end
424
-
425
- ##
426
- # Validates a message against a schema.
427
- #
428
- # @param message_data [String] Message to validate against the provided `schema_spec`.
429
- # @param message_encoding [Google::Cloud::PubSub::V1::Encoding] The encoding expected for messages.
430
- # @param schema_name [String] Name of the schema against which to validate.
431
- # @param project [String] Name of the project if not the default project.
432
- # @param type [String] Ad-hoc schema type against which to validate.
433
- # @param definition [String] Ad-hoc schema definition against which to validate.
434
- #
435
- def validate_message message_data, message_encoding, schema_name: nil, project: nil, type: nil, definition: nil
436
- if type && definition
437
- schema = Google::Cloud::PubSub::V1::Schema.new(
438
- type: type,
439
- definition: definition
440
- )
441
- end
442
- schemas.validate_message parent: project_path(project: project),
443
- name: schema_path(schema_name),
444
- schema: schema,
445
- message: message_data,
446
- encoding: message_encoding
151
+ subscription_admin.modify_ack_deadline_internal subscription: subscription_path(subscription),
152
+ ack_ids: Array(ids),
153
+ ack_deadline_seconds: deadline
447
154
  end
448
155
 
449
156
  # Helper methods
450
157
 
451
- def get_topic_policy topic_name, options = {}
452
- iam.get_iam_policy resource: topic_path(topic_name, options)
453
- end
454
-
455
- def set_topic_policy topic_name, new_policy, options = {}
456
- iam.set_iam_policy resource: topic_path(topic_name, options), policy: new_policy
457
- end
458
-
459
- def test_topic_permissions topic_name, permissions, options = {}
460
- iam.test_iam_permissions resource: topic_path(topic_name, options), permissions: permissions
461
- end
462
-
463
- def get_subscription_policy subscription_name, options = {}
464
- iam.get_iam_policy resource: subscription_path(subscription_name, options)
465
- end
466
-
467
- def set_subscription_policy subscription_name, new_policy, options = {}
468
- iam.set_iam_policy resource: subscription_path(subscription_name, options), policy: new_policy
469
- end
470
-
471
- def test_subscription_permissions subscription_name, permissions, options = {}
472
- iam.test_iam_permissions resource: subscription_path(subscription_name, options), permissions: permissions
473
- end
474
-
475
158
  def project_path options = {}
476
159
  project_name = options[:project] || project
477
160
  "projects/#{project_name}"
@@ -497,10 +180,6 @@ module Google
497
180
  "#{project_path options}/schemas/#{schema_name}"
498
181
  end
499
182
 
500
- def inspect
501
- "#<#{self.class.name} (#{@project})>"
502
- end
503
-
504
183
  protected
505
184
 
506
185
  # Set the timeout in the client config.
@@ -514,49 +193,8 @@ module Google
514
193
  rpc.timeout = timeout if rpc.respond_to? :timeout=
515
194
  end
516
195
  end
517
-
518
- def a_time? obj
519
- return false unless obj.respond_to? :to_time
520
- # Rails' String#to_time returns nil if the string doesn't parse.
521
- return false if obj.to_time.nil?
522
- true
523
- end
524
-
525
- def dead_letter_policy options
526
- return nil unless options[:dead_letter_topic_name]
527
- policy = Google::Cloud::PubSub::V1::DeadLetterPolicy.new dead_letter_topic: options[:dead_letter_topic_name]
528
- if options[:dead_letter_max_delivery_attempts]
529
- policy.max_delivery_attempts = options[:dead_letter_max_delivery_attempts]
530
- end
531
- policy
532
- end
533
-
534
- private
535
-
536
- def construct_create_subscription_options topic, subscription_name, options
537
- excess_options = [:deadline,
538
- :retention,
539
- :retain_acked,
540
- :message_ordering,
541
- :endpoint,
542
- :dead_letter_topic_name,
543
- :dead_letter_max_delivery_attempts,
544
- :dead_letter_topic]
545
-
546
- new_options = options.filter { |k, v| !v.nil? && !excess_options.include?(k) }
547
- new_options[:name] = subscription_path subscription_name, options
548
- new_options[:topic] = topic_path topic
549
- new_options[:message_retention_duration] = Convert.number_to_duration options[:retention]
550
- new_options[:dead_letter_policy] = dead_letter_policy options
551
- new_options[:ack_deadline_seconds] = options[:deadline]
552
- new_options[:retain_acked_messages] = options[:retain_acked]
553
- new_options[:enable_message_ordering] = options[:message_ordering]
554
-
555
- new_options.compact
556
- end
557
196
  end
558
197
  end
559
-
560
198
  Pubsub = PubSub unless const_defined? :Pubsub
561
199
  end
562
200
  end