google-cloud-pubsub 0.34.1 → 0.35.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f7e9076f21ca7cc0d91beb6d1a9c47af43dcb4016657ab58f174ee945a13d32e
4
- data.tar.gz: eebba9543b5870f7bad014fe56bfa814d4682be7931236b23d202695caece7bb
3
+ metadata.gz: 9eb65ead0bfa397fca5d4eab13a005d64dabb629da1b0b4638fbbd45fab65cf3
4
+ data.tar.gz: 01027a9e96bda8a16107309c06eba2f4cd971a56ca851b59463a1958c06a63ec
5
5
  SHA512:
6
- metadata.gz: 3695804541d32698fd556b9b954a50666ee5584c8b93eda85376801e51be0f286abd829963b78a0946a1effa52cb27b231f18736f10b7f5dcdbeeeb287326da1
7
- data.tar.gz: c955d2a764101590c29c8ee821e2cc56a48eaa4fe322fd27c3a238fc9b8cc614f5f11a7ea3fded7b1c8e994c0f35d3597f728a4ea76729b4a0088cfea82475a7
6
+ metadata.gz: 93dfbbcc2ea3e8cb444ed00bbf92a9d514cbf3868fc7cbe540be112358203c0d0becf16dd36e459accd65a1cb12db589f7e29a8942685418356b8a530ce374c9
7
+ data.tar.gz: 660f2887f98ddef1f1c5a83f928e4855008f6770c3bbe02ac6f9a1c567ad8bd3dfc160bdbb7abc397de8197ccd27e6d588443c352a8ff3a6b8cfd3e09b3231bb
@@ -2,20 +2,39 @@
2
2
 
3
3
  In general, the google-cloud-pubsub library uses [Service
4
4
  Account](https://cloud.google.com/iam/docs/creating-managing-service-accounts)
5
- credentials to connect to Google Cloud services. When running on Compute Engine
5
+ credentials to connect to Google Cloud services. When running within [Google
6
+ Cloud Platform environments](#google-cloud-platform-environments)
6
7
  the credentials will be discovered automatically. When running on other
7
8
  environments, the Service Account credentials can be specified by providing the
8
9
  path to the [JSON
9
10
  keyfile](https://cloud.google.com/iam/docs/managing-service-account-keys) for
10
- the account (or the JSON itself) in environment variables. Additionally, Cloud
11
- SDK credentials can also be discovered automatically, but this is only
12
- recommended during development.
11
+ the account (or the JSON itself) in [environment
12
+ variables](#environment-variables). Additionally, Cloud SDK credentials can also
13
+ be discovered automatically, but this is only recommended during development.
14
+
15
+ ## Quickstart
16
+
17
+ 1. [Create a service account and credentials](#creating-a-service-account).
18
+ 2. Set the [environment variable](#environment-variables).
19
+
20
+ ```sh
21
+ export PUBSUB_CREDENTIALS=/path/to/json`
22
+ ```
23
+
24
+ 3. Initialize the client.
25
+
26
+ ```ruby
27
+ require "google/cloud/pubsub"
28
+
29
+ client = Google::Cloud::PubSub.new
30
+ ```
13
31
 
14
32
  ## Project and Credential Lookup
15
33
 
16
- The google-cloud-pubsub library aims to make authentication as simple as
17
- possible, and provides several mechanisms to configure your system without
18
- providing **Project ID** and **Service Account Credentials** directly in code.
34
+ The google-cloud-pubsub library aims to make authentication
35
+ as simple as possible, and provides several mechanisms to configure your system
36
+ without providing **Project ID** and **Service Account Credentials** directly in
37
+ code.
19
38
 
20
39
  **Project ID** is discovered in the following order:
21
40
 
@@ -23,6 +42,7 @@ providing **Project ID** and **Service Account Credentials** directly in code.
23
42
  2. Specify project ID in configuration
24
43
  3. Discover project ID in environment variables
25
44
  4. Discover GCE project ID
45
+ 5. Discover project ID in credentials JSON
26
46
 
27
47
  **Credentials** are discovered in the following order:
28
48
 
@@ -73,12 +93,12 @@ environment variable, or the **Credentials JSON** itself can be stored for
73
93
  environments such as Docker containers where writing files is difficult or not
74
94
  encouraged.
75
95
 
76
- The environment variables that Pub/Sub checks for project ID are:
96
+ The environment variables that google-cloud-pubsub checks for project ID are:
77
97
 
78
98
  1. `PUBSUB_PROJECT`
79
99
  2. `GOOGLE_CLOUD_PROJECT`
80
100
 
81
- The environment variables that Pub/Sub checks for credentials are configured on {Google::Cloud::PubSub::V1::Credentials}:
101
+ The environment variables that google-cloud-pubsub checks for credentials are configured on {Google::Cloud::PubSub::V1::Credentials}:
82
102
 
83
103
  1. `PUBSUB_CREDENTIALS` - Path to JSON file, or JSON contents
84
104
  2. `PUBSUB_KEYFILE` - Path to JSON file, or JSON contents
@@ -92,7 +112,7 @@ require "google/cloud/pubsub"
92
112
  ENV["PUBSUB_PROJECT"] = "my-project-id"
93
113
  ENV["PUBSUB_CREDENTIALS"] = "path/to/keyfile.json"
94
114
 
95
- pubsub = Google::Cloud::PubSub.new
115
+ client = Google::Cloud::PubSub.new
96
116
  ```
97
117
 
98
118
  ### Configuration
@@ -107,7 +127,7 @@ Google::Cloud::PubSub.configure do |config|
107
127
  config.credentials = "path/to/keyfile.json"
108
128
  end
109
129
 
110
- pubsub = Google::Cloud::PubSub.new
130
+ client = Google::Cloud::PubSub.new
111
131
  ```
112
132
 
113
133
  ### Cloud SDK
@@ -140,7 +160,8 @@ Google Cloud requires a **Project ID** and **Service Account Credentials** to
140
160
  connect to the APIs. You will use the **Project ID** and **JSON key file** to
141
161
  connect to most services with google-cloud-pubsub.
142
162
 
143
- If you are not running this client on Google Compute Engine, you need a Google
163
+ If you are not running this client within [Google Cloud Platform
164
+ environments](#google-cloud-platform-environments), you need a Google
144
165
  Developers service account.
145
166
 
146
167
  1. Visit the [Google Developers Console][dev-console].
@@ -1,5 +1,18 @@
1
1
  # Release History
2
2
 
3
+ ### 0.35.0 / 2019-04-25
4
+
5
+ * Add Subscription#push_config and Subscription::PushConfig
6
+ * Add Subscription#expires_in
7
+ * Add Topic#reload!
8
+ * Add Subscription#reload!
9
+ * Update low-level generated files
10
+ * Add PushConfig#oidc_token
11
+ * Add ordering_key to PubsubMessage.
12
+ * Add enable_message_ordering to Subscription.
13
+ * Extract gRPC header values from request.
14
+ * Update documentation.
15
+
3
16
  ### 0.34.1 / 2019-02-13
4
17
 
5
18
  * Fix bug (typo) in retrieving default on_error proc.
@@ -217,7 +217,8 @@ module Google
217
217
  @cond.wait
218
218
  else
219
219
  # still waiting for the interval to publish the batch...
220
- @cond.wait(@interval - time_since_first_publish)
220
+ timeout = @interval - time_since_first_publish
221
+ @cond.wait timeout
221
222
  end
222
223
  end
223
224
  end
@@ -234,21 +235,21 @@ module Google
234
235
  def publish_batch_async topic_name, batch
235
236
  return unless @publish_thread_pool.running?
236
237
 
237
- Concurrent::Future.new(executor: @publish_thread_pool) do
238
+ Concurrent::Future.new executor: @publish_thread_pool do
238
239
  begin
239
240
  grpc = @service.publish topic_name, batch.messages
240
- batch.items.zip(Array(grpc.message_ids)) do |item, id|
241
+ batch.items.zip Array(grpc.message_ids) do |item, id|
241
242
  next unless item.callback
242
243
 
243
244
  item.msg.message_id = id
244
- publish_result = PublishResult.from_grpc(item.msg)
245
+ publish_result = PublishResult.from_grpc item.msg
245
246
  execute_callback_async item.callback, publish_result
246
247
  end
247
248
  rescue StandardError => e
248
249
  batch.items.each do |item|
249
250
  next unless item.callback
250
251
 
251
- publish_result = PublishResult.from_error(item.msg, e)
252
+ publish_result = PublishResult.from_error item.msg, e
252
253
  execute_callback_async item.callback, publish_result
253
254
  end
254
255
  end
@@ -258,7 +259,7 @@ module Google
258
259
  def execute_callback_async callback, publish_result
259
260
  return unless @callback_thread_pool.running?
260
261
 
261
- Concurrent::Future.new(executor: @callback_thread_pool) do
262
+ Concurrent::Future.new executor: @callback_thread_pool do
262
263
  callback.call publish_result
263
264
  end.execute
264
265
  end
@@ -150,7 +150,7 @@ module Google
150
150
  def topic topic_name, project: nil, skip_lookup: nil, async: nil
151
151
  ensure_service!
152
152
  options = { project: project }
153
- return Topic.from_name(topic_name, service, options) if skip_lookup
153
+ return Topic.from_name topic_name, service, options if skip_lookup
154
154
  grpc = service.get_topic topic_name
155
155
  Topic.from_grpc grpc, service, async: async
156
156
  rescue Google::Cloud::NotFoundError
@@ -168,7 +168,7 @@ module Google
168
168
  ##
169
169
  # Gets the details of a subscription.
170
170
  def get_subscription subscription_name, options = {}
171
- subscription = subscription_path(subscription_name, options)
171
+ subscription = subscription_path subscription_name, options
172
172
  execute do
173
173
  subscriber.get_subscription subscription, options: default_options
174
174
  end
@@ -218,8 +218,8 @@ module Google
218
218
  ##
219
219
  # Creates a subscription on a given topic for a given subscriber.
220
220
  def create_subscription topic, subscription_name, options = {}
221
- name = subscription_path(subscription_name, options)
222
- topic = topic_path(topic)
221
+ name = subscription_path subscription_name, options
222
+ topic = topic_path topic
223
223
  push_config = if options[:endpoint]
224
224
  Google::Cloud::PubSub::V1::PushConfig.new \
225
225
  push_endpoint: options[:endpoint],
@@ -263,7 +263,7 @@ module Google
263
263
  ##
264
264
  # Pulls a single message from the server.
265
265
  def pull subscription, options = {}
266
- subscription = subscription_path(subscription, options)
266
+ subscription = subscription_path subscription, options
267
267
  max_messages = options.fetch(:max, 100).to_i
268
268
  return_immediately = !(!options.fetch(:immediate, true))
269
269
 
@@ -293,7 +293,7 @@ module Google
293
293
  ##
294
294
  # Modifies the PushConfig for a specified subscription.
295
295
  def modify_push_config subscription, endpoint, attributes
296
- subscription = subscription_path(subscription)
296
+ subscription = subscription_path subscription
297
297
  # Convert attributes to strings to match the protobuf definition
298
298
  attributes = Hash[attributes.map { |k, v| [String(k), String(v)] }]
299
299
  push_config = Google::Cloud::PubSub::V1::PushConfig.new(
@@ -370,7 +370,7 @@ module Google
370
370
  ##
371
371
  # Adjusts the given subscription to a time or snapshot.
372
372
  def seek subscription, time_or_snapshot
373
- subscription = subscription_path(subscription)
373
+ subscription = subscription_path subscription
374
374
  execute do
375
375
  if a_time? time_or_snapshot
376
376
  time = Convert.time_to_timestamp time_or_snapshot
@@ -394,7 +394,7 @@ module Google
394
394
  end
395
395
 
396
396
  def set_topic_policy topic_name, new_policy, options = {}
397
- resource = topic_path(topic_name, options)
397
+ resource = topic_path topic_name, options
398
398
 
399
399
  execute do
400
400
  publisher.set_iam_policy resource, new_policy,
@@ -403,7 +403,7 @@ module Google
403
403
  end
404
404
 
405
405
  def test_topic_permissions topic_name, permissions, options = {}
406
- resource = topic_path(topic_name, options)
406
+ resource = topic_path topic_name, options
407
407
 
408
408
  execute do
409
409
  publisher.test_iam_permissions resource, permissions,
@@ -412,7 +412,7 @@ module Google
412
412
  end
413
413
 
414
414
  def get_subscription_policy subscription_name, options = {}
415
- resource = subscription_path(subscription_name, options)
415
+ resource = subscription_path subscription_name, options
416
416
 
417
417
  execute do
418
418
  subscriber.get_iam_policy resource, options: default_options
@@ -420,7 +420,7 @@ module Google
420
420
  end
421
421
 
422
422
  def set_subscription_policy subscription_name, new_policy, options = {}
423
- resource = subscription_path(subscription_name, options)
423
+ resource = subscription_path subscription_name, options
424
424
 
425
425
  execute do
426
426
  subscriber.set_iam_policy resource, new_policy,
@@ -430,7 +430,7 @@ module Google
430
430
 
431
431
  def test_subscription_permissions subscription_name,
432
432
  permissions, options = {}
433
- resource = subscription_path(subscription_name, options)
433
+ resource = subscription_path subscription_name, options
434
434
 
435
435
  execute do
436
436
  subscriber.test_iam_permissions resource, permissions,
@@ -445,19 +445,19 @@ module Google
445
445
 
446
446
  def topic_path topic_name, options = {}
447
447
  return topic_name if topic_name.to_s.include? "/"
448
- "#{project_path(options)}/topics/#{topic_name}"
448
+ "#{project_path options}/topics/#{topic_name}"
449
449
  end
450
450
 
451
451
  def subscription_path subscription_name, options = {}
452
452
  return subscription_name if subscription_name.to_s.include? "/"
453
- "#{project_path(options)}/subscriptions/#{subscription_name}"
453
+ "#{project_path options}/subscriptions/#{subscription_name}"
454
454
  end
455
455
 
456
456
  def snapshot_path snapshot_name, options = {}
457
457
  if snapshot_name.nil? || snapshot_name.to_s.include?("/")
458
458
  return snapshot_name
459
459
  end
460
- "#{project_path(options)}/snapshots/#{snapshot_name}"
460
+ "#{project_path options}/snapshots/#{snapshot_name}"
461
461
  end
462
462
 
463
463
  def inspect
@@ -138,7 +138,7 @@ module Google
138
138
  #
139
139
  def labels= new_labels
140
140
  raise ArgumentError, "Value must be a Hash" if new_labels.nil?
141
- labels_map = Google::Protobuf::Map.new(:string, :string)
141
+ labels_map = Google::Protobuf::Map.new :string, :string
142
142
  Hash(new_labels).each { |k, v| labels_map[String(k)] = String(v) }
143
143
  update_grpc = @grpc.dup
144
144
  update_grpc.labels = labels_map
@@ -128,7 +128,7 @@ module Google
128
128
  def all request_limit: nil
129
129
  request_limit = request_limit.to_i if request_limit
130
130
  unless block_given?
131
- return enum_for(:all, request_limit: request_limit)
131
+ return enum_for :all, request_limit: request_limit
132
132
  end
133
133
  results = self
134
134
  loop do
@@ -91,7 +91,7 @@ module Google
91
91
  @started = nil
92
92
  @stopped = nil
93
93
 
94
- stream_pool = Array.new(@streams) do
94
+ stream_pool = Array.new @streams do
95
95
  Thread.new { Stream.new self }
96
96
  end
97
97
  @stream_pool = stream_pool.map(&:value)
@@ -37,7 +37,7 @@ module Google
37
37
  end
38
38
 
39
39
  def each
40
- return enum_for(:each) unless block_given?
40
+ return enum_for :each unless block_given?
41
41
 
42
42
  loop do
43
43
  obj = @queue.pop
@@ -266,7 +266,7 @@ module Google
266
266
  def perform_callback_async rec_msg
267
267
  return unless callback_thread_pool.running?
268
268
 
269
- Concurrent::Future.new(executor: callback_thread_pool) do
269
+ Concurrent::Future.new executor: callback_thread_pool do
270
270
  begin
271
271
  @subscriber.callback.call rec_msg
272
272
  rescue StandardError => callback_error
@@ -34,7 +34,7 @@ module Google
34
34
  # entry.
35
35
  @register = {}
36
36
 
37
- @task = Concurrent::TimerTask.new(execution_interval: interval) do
37
+ @task = Concurrent::TimerTask.new execution_interval: interval do
38
38
  flush!
39
39
  end
40
40
  end
@@ -204,7 +204,7 @@ module Google
204
204
  end
205
205
 
206
206
  def add_future pool
207
- Concurrent::Future.new(executor: pool) do
207
+ Concurrent::Future.new executor: pool do
208
208
  begin
209
209
  yield
210
210
  rescue StandardError => error
@@ -16,6 +16,7 @@
16
16
  require "google/cloud/pubsub/convert"
17
17
  require "google/cloud/errors"
18
18
  require "google/cloud/pubsub/subscription/list"
19
+ require "google/cloud/pubsub/subscription/push_config"
19
20
  require "google/cloud/pubsub/received_message"
20
21
  require "google/cloud/pubsub/snapshot"
21
22
  require "google/cloud/pubsub/subscriber"
@@ -189,7 +190,8 @@ module Google
189
190
 
190
191
  ##
191
192
  # Returns the URL locating the endpoint to which messages should be
192
- # pushed.
193
+ # pushed. For example, a Webhook endpoint might use
194
+ # "https://example.com/push".
193
195
  #
194
196
  # Makes an API call to retrieve the endpoint value when called on a
195
197
  # reference object. See {#reference?}.
@@ -203,6 +205,7 @@ module Google
203
205
 
204
206
  ##
205
207
  # Sets the URL locating the endpoint to which messages should be pushed.
208
+ # For example, a Webhook endpoint might use "https://example.com/push".
206
209
  #
207
210
  # @param [String] new_endpoint The new endpoint value.
208
211
  #
@@ -218,6 +221,65 @@ module Google
218
221
  )
219
222
  end
220
223
 
224
+ ##
225
+ # Inspect the Subscription's push configuration settings. The
226
+ # configuration can be changed by modifying the values in the method's
227
+ # block.
228
+ #
229
+ # Subscription objects that are reference only will return an empty
230
+ # {Subscription::PushConfig} object, which can be configured and saved
231
+ # using the method's block. Unlike {#endpoint}, which will retrieve the
232
+ # full resource from the API before returning. To get the actual values
233
+ # for a reference object, call {#reload!} before calling {#push_config}.
234
+ #
235
+ # @yield [push_config] a block for modifying the push configuration
236
+ # @yieldparam [Subscription::PushConfig] push_config the push
237
+ # configuration
238
+ #
239
+ # @return [Subscription::PushConfig]
240
+ #
241
+ # @example
242
+ # require "google/cloud/pubsub"
243
+ #
244
+ # pubsub = Google::Cloud::PubSub.new
245
+ #
246
+ # sub = pubsub.subscription "my-topic-sub"
247
+ # sub.push_config.endpoint #=> "http://example.com/callback"
248
+ # sub.push_config.authentication.email #=> "user@example.com"
249
+ # sub.push_config.authentication.audience #=> "client-12345"
250
+ #
251
+ # @example Update the push configuration by passing a block:
252
+ # require "google/cloud/pubsub"
253
+ #
254
+ # pubsub = Google::Cloud::PubSub.new
255
+ # sub = pubsub.subscription "my-subscription"
256
+ #
257
+ # sub.push_config do |pc|
258
+ # pc.endpoint = "http://example.net/callback"
259
+ # pc.set_oidc_token "user@example.net", "client-67890"
260
+ # end
261
+ #
262
+ def push_config
263
+ ensure_service!
264
+
265
+ orig_config = reference? ? nil : @grpc.push_config
266
+ config = PushConfig.from_grpc orig_config
267
+
268
+ if block_given?
269
+ old_config = config.to_grpc.dup
270
+ yield config
271
+ new_config = config.to_grpc
272
+
273
+ if old_config != new_config # has the object been changed?
274
+ update_grpc = Google::Cloud::PubSub::V1::Subscription.new \
275
+ name: name, push_config: new_config
276
+ @grpc = service.update_subscription update_grpc, :push_config
277
+ end
278
+ end
279
+
280
+ config.freeze
281
+ end
282
+
221
283
  ##
222
284
  # A hash of user-provided labels associated with this subscription.
223
285
  # Labels can be used to organize and group subscriptions.See [Creating
@@ -256,6 +318,48 @@ module Google
256
318
  @resource_name = nil
257
319
  end
258
320
 
321
+ ##
322
+ # The duration (in seconds) for when a subscription expires after the
323
+ # subscription goes inactive. A subscription is considered active as
324
+ # long as any connected subscriber is successfully consuming messages
325
+ # from the subscription or is issuing operations on the subscription.
326
+ #
327
+ # If {#expires_in=} is not set, a *default* value of of 31 days will be
328
+ # used. The minimum allowed value is 1 day.
329
+ #
330
+ # Makes an API call to retrieve the labels value when called on a
331
+ # reference object. See {#reference?}.
332
+ #
333
+ # @return [Numeric, nil] The expiration duration, or `nil` if unset.
334
+ #
335
+ def expires_in
336
+ ensure_grpc!
337
+
338
+ return nil if @grpc.expiration_policy.nil?
339
+
340
+ Convert.duration_to_number @grpc.expiration_policy.ttl
341
+ end
342
+
343
+ ##
344
+ # Sets the duration (in seconds) for when a subscription expires after
345
+ # the subscription goes inactive.
346
+ #
347
+ # See also {#expires_in}.
348
+ #
349
+ # @param [Numeric, nil] ttl The expiration duration in seconds, or `nil`
350
+ # to unset.
351
+ #
352
+ def expires_in= ttl
353
+ new_expiration_policy = Google::Pubsub::V1::ExpirationPolicy.new(
354
+ ttl: Convert.number_to_duration(ttl)
355
+ )
356
+
357
+ update_grpc = Google::Cloud::PubSub::V1::Subscription.new \
358
+ name: name, expiration_policy: new_expiration_policy
359
+ @grpc = service.update_subscription update_grpc, :expiration_policy
360
+ @resource_name = nil
361
+ end
362
+
259
363
  ##
260
364
  # Determines whether the subscription exists in the Pub/Sub service.
261
365
  #
@@ -682,6 +786,28 @@ module Google
682
786
  !@grpc.nil?
683
787
  end
684
788
 
789
+ ##
790
+ # Reloads the subscription with current data from the Pub/Sub service.
791
+ #
792
+ # @return [Google::Cloud::PubSub::Subscription] Returns the reloaded
793
+ # subscription
794
+ #
795
+ # @example
796
+ # require "google/cloud/pubsub"
797
+ #
798
+ # pubsub = Google::Cloud::PubSub.new
799
+ #
800
+ # sub = pubsub.get_subscription "my-topic-sub"
801
+ # sub.reload!
802
+ #
803
+ def reload!
804
+ ensure_service!
805
+ @grpc = service.get_subscription name
806
+ @resource_name = nil
807
+ self
808
+ end
809
+ alias refresh! reload!
810
+
685
811
  ##
686
812
  # Gets the [Cloud IAM](https://cloud.google.com/iam/) access control
687
813
  # policy for this subscription.
@@ -834,8 +960,7 @@ module Google
834
960
  # Ensures a Google::Cloud::PubSub::V1::Subscription object exists.
835
961
  def ensure_grpc!
836
962
  ensure_service!
837
- @grpc = service.get_subscription name if reference?
838
- @resource_name = nil
963
+ reload! if reference?
839
964
  end
840
965
 
841
966
  ##