gcloud 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +8 -8
  2. data/AUTHENTICATION.md +3 -3
  3. data/CHANGELOG.md +92 -0
  4. data/OVERVIEW.md +3 -3
  5. data/lib/gcloud.rb +75 -25
  6. data/lib/gcloud/backoff.rb +5 -1
  7. data/lib/gcloud/bigquery.rb +25 -43
  8. data/lib/gcloud/bigquery/copy_job.rb +13 -13
  9. data/lib/gcloud/bigquery/data.rb +20 -16
  10. data/lib/gcloud/bigquery/dataset.rb +202 -177
  11. data/lib/gcloud/bigquery/dataset/access.rb +118 -104
  12. data/lib/gcloud/bigquery/dataset/list.rb +14 -18
  13. data/lib/gcloud/bigquery/extract_job.rb +12 -12
  14. data/lib/gcloud/bigquery/insert_response.rb +12 -14
  15. data/lib/gcloud/bigquery/job.rb +45 -57
  16. data/lib/gcloud/bigquery/job/list.rb +18 -24
  17. data/lib/gcloud/bigquery/load_job.rb +35 -27
  18. data/lib/gcloud/bigquery/project.rb +53 -73
  19. data/lib/gcloud/bigquery/query_data.rb +28 -35
  20. data/lib/gcloud/bigquery/query_job.rb +18 -18
  21. data/lib/gcloud/bigquery/schema.rb +359 -0
  22. data/lib/gcloud/bigquery/service.rb +506 -0
  23. data/lib/gcloud/bigquery/table.rb +185 -266
  24. data/lib/gcloud/bigquery/table/list.rb +15 -19
  25. data/lib/gcloud/bigquery/view.rb +126 -81
  26. data/lib/gcloud/datastore.rb +39 -27
  27. data/lib/gcloud/datastore/commit.rb +2 -2
  28. data/lib/gcloud/datastore/dataset.rb +8 -19
  29. data/lib/gcloud/datastore/dataset/lookup_results.rb +2 -4
  30. data/lib/gcloud/datastore/dataset/query_results.rb +0 -2
  31. data/lib/gcloud/datastore/entity.rb +7 -1
  32. data/lib/gcloud/datastore/errors.rb +5 -27
  33. data/lib/gcloud/datastore/grpc_utils.rb +4 -3
  34. data/lib/gcloud/datastore/key.rb +6 -0
  35. data/lib/gcloud/datastore/service.rb +18 -12
  36. data/lib/gcloud/datastore/transaction.rb +0 -10
  37. data/lib/gcloud/dns.rb +29 -19
  38. data/lib/gcloud/dns/change.rb +10 -15
  39. data/lib/gcloud/dns/change/list.rb +4 -4
  40. data/lib/gcloud/dns/importer.rb +1 -1
  41. data/lib/gcloud/dns/project.rb +32 -49
  42. data/lib/gcloud/dns/record.rb +8 -2
  43. data/lib/gcloud/dns/record/list.rb +4 -4
  44. data/lib/gcloud/dns/service.rb +167 -0
  45. data/lib/gcloud/dns/zone.rb +33 -52
  46. data/lib/gcloud/dns/zone/list.rb +12 -16
  47. data/lib/gcloud/errors.rb +31 -19
  48. data/lib/gcloud/logging.rb +50 -39
  49. data/lib/gcloud/logging/entry.rb +197 -24
  50. data/lib/gcloud/logging/entry/list.rb +0 -2
  51. data/lib/gcloud/logging/logger.rb +1 -1
  52. data/lib/gcloud/logging/metric.rb +3 -9
  53. data/lib/gcloud/logging/metric/list.rb +0 -2
  54. data/lib/gcloud/logging/project.rb +58 -54
  55. data/lib/gcloud/logging/resource_descriptor.rb +2 -2
  56. data/lib/gcloud/logging/resource_descriptor/list.rb +0 -2
  57. data/lib/gcloud/logging/service.rb +32 -23
  58. data/lib/gcloud/logging/sink.rb +8 -14
  59. data/lib/gcloud/logging/sink/list.rb +0 -2
  60. data/lib/gcloud/pubsub.rb +21 -16
  61. data/lib/gcloud/pubsub/policy.rb +204 -0
  62. data/lib/gcloud/pubsub/project.rb +26 -38
  63. data/lib/gcloud/pubsub/service.rb +39 -31
  64. data/lib/gcloud/pubsub/subscription.rb +56 -59
  65. data/lib/gcloud/pubsub/subscription/list.rb +4 -4
  66. data/lib/gcloud/pubsub/topic.rb +69 -66
  67. data/lib/gcloud/pubsub/topic/list.rb +0 -2
  68. data/lib/gcloud/pubsub/topic/{batch.rb → publisher.rb} +15 -2
  69. data/lib/gcloud/resource_manager.rb +27 -26
  70. data/lib/gcloud/resource_manager/manager.rb +19 -39
  71. data/lib/gcloud/resource_manager/policy.rb +211 -0
  72. data/lib/gcloud/resource_manager/project.rb +97 -121
  73. data/lib/gcloud/resource_manager/project/list.rb +7 -7
  74. data/lib/gcloud/resource_manager/project/updater.rb +4 -9
  75. data/lib/gcloud/resource_manager/service.rb +127 -0
  76. data/lib/gcloud/storage.rb +24 -42
  77. data/lib/gcloud/storage/bucket.rb +104 -192
  78. data/lib/gcloud/storage/bucket/acl.rb +47 -143
  79. data/lib/gcloud/storage/bucket/cors.rb +55 -11
  80. data/lib/gcloud/storage/bucket/list.rb +14 -14
  81. data/lib/gcloud/storage/errors.rb +3 -43
  82. data/lib/gcloud/storage/file.rb +114 -111
  83. data/lib/gcloud/storage/file/acl.rb +27 -113
  84. data/lib/gcloud/storage/file/list.rb +21 -21
  85. data/lib/gcloud/storage/project.rb +49 -59
  86. data/lib/gcloud/storage/service.rb +347 -0
  87. data/lib/gcloud/translate.rb +24 -14
  88. data/lib/gcloud/translate/api.rb +12 -21
  89. data/lib/gcloud/translate/detection.rb +5 -5
  90. data/lib/gcloud/translate/language.rb +1 -1
  91. data/lib/gcloud/translate/service.rb +80 -0
  92. data/lib/gcloud/translate/translation.rb +6 -6
  93. data/lib/gcloud/version.rb +1 -1
  94. data/lib/gcloud/vision.rb +24 -15
  95. data/lib/gcloud/vision/annotate.rb +24 -21
  96. data/lib/gcloud/vision/annotation.rb +9 -9
  97. data/lib/gcloud/vision/annotation/entity.rb +11 -11
  98. data/lib/gcloud/vision/annotation/face.rb +25 -25
  99. data/lib/gcloud/vision/annotation/properties.rb +8 -8
  100. data/lib/gcloud/vision/annotation/safe_search.rb +4 -4
  101. data/lib/gcloud/vision/annotation/text.rb +7 -7
  102. data/lib/gcloud/vision/annotation/vertex.rb +1 -1
  103. data/lib/gcloud/vision/image.rb +11 -11
  104. data/lib/gcloud/vision/location.rb +5 -2
  105. data/lib/gcloud/vision/project.rb +14 -16
  106. data/lib/gcloud/vision/service.rb +66 -0
  107. data/lib/google/api_client.rb +0 -0
  108. metadata +27 -24
  109. data/lib/gcloud/bigquery/connection.rb +0 -624
  110. data/lib/gcloud/bigquery/errors.rb +0 -68
  111. data/lib/gcloud/bigquery/table/schema.rb +0 -234
  112. data/lib/gcloud/dns/connection.rb +0 -173
  113. data/lib/gcloud/dns/errors.rb +0 -68
  114. data/lib/gcloud/resource_manager/connection.rb +0 -134
  115. data/lib/gcloud/resource_manager/errors.rb +0 -68
  116. data/lib/gcloud/storage/connection.rb +0 -444
  117. data/lib/gcloud/translate/connection.rb +0 -85
  118. data/lib/gcloud/translate/errors.rb +0 -68
  119. data/lib/gcloud/upload.rb +0 -95
  120. data/lib/gcloud/vision/connection.rb +0 -63
  121. data/lib/gcloud/vision/errors.rb +0 -69
@@ -20,8 +20,8 @@ module Gcloud
20
20
  ##
21
21
  # # Sink
22
22
  #
23
- # Used to export log entries outside Cloud Logging. When you create a sink,
24
- # new log entries are exported. Cloud Logging does not send
23
+ # Used to export log entries outside Stackdriver Logging. When you create a
24
+ # sink, new log entries are exported. Stackdriver Logging does not send
25
25
  # previously-ingested log entries to the sink's destination.
26
26
  #
27
27
  # Before creating the sink, ensure that you have granted
@@ -48,7 +48,7 @@ module Gcloud
48
48
  #
49
49
  # bucket = storage.create_bucket "my-logs-bucket"
50
50
  #
51
- # # Grant owner permission to Cloud Logging service
51
+ # # Grant owner permission to Stackdriver Logging service
52
52
  # email = "cloud-logs@google.com"
53
53
  # bucket.acl.add_owner "group-#{email}"
54
54
  #
@@ -99,7 +99,7 @@ module Gcloud
99
99
  # that defines the log entries to be exported. The filter must be
100
100
  # consistent with the log entry format designed by the `version`
101
101
  # parameter, regardless of the format of the log entry that was originally
102
- # written to Cloud Logging.
102
+ # written to Stackdriver Logging.
103
103
  def filter
104
104
  @grpc.filter
105
105
  end
@@ -110,7 +110,7 @@ module Gcloud
110
110
  # that defines the log entries to be exported. The filter must be
111
111
  # consistent with the log entry format designed by the `version`
112
112
  # parameter, regardless of the format of the log entry that was originally
113
- # written to Cloud Logging.
113
+ # written to Stackdriver Logging.
114
114
  def filter= filter
115
115
  @grpc.filter = filter
116
116
  end
@@ -118,7 +118,7 @@ module Gcloud
118
118
  ##
119
119
  # The log entry version used when exporting log entries from this sink.
120
120
  # This version does not have to correspond to the version of the log entry
121
- # when it was written to Cloud Logging.
121
+ # when it was written to Stackdriver Logging.
122
122
  def version
123
123
  @grpc.output_version_format
124
124
  end
@@ -126,7 +126,7 @@ module Gcloud
126
126
  ##
127
127
  # Updates the log entry version used when exporting log entries from this
128
128
  # sink. This version does not have to correspond to the version of the log
129
- # entry when it was written to Cloud Logging. Accepted values are
129
+ # entry when it was written to Stackdriver Logging. Accepted values are
130
130
  # `:VERSION_FORMAT_UNSPECIFIED`, `:V2`, and `:V1`.
131
131
  def version= version
132
132
  @grpc.output_version_format = self.class.resolve_version(version)
@@ -166,8 +166,6 @@ module Gcloud
166
166
  def save
167
167
  ensure_service!
168
168
  @grpc = service.update_sink name, destination, filter, version
169
- rescue GRPC::BadStatus => e
170
- raise Gcloud::Error.from_error(e)
171
169
  end
172
170
 
173
171
  ##
@@ -176,8 +174,6 @@ module Gcloud
176
174
  def reload!
177
175
  ensure_service!
178
176
  @grpc = service.get_sink name
179
- rescue GRPC::BadStatus => e
180
- raise Gcloud::Error.from_error(e)
181
177
  end
182
178
  alias_method :refresh!, :reload!
183
179
 
@@ -197,9 +193,7 @@ module Gcloud
197
193
  def delete
198
194
  ensure_service!
199
195
  service.delete_sink name
200
- return true
201
- rescue GRPC::BadStatus => e
202
- raise Gcloud::Error.from_error(e)
196
+ true
203
197
  end
204
198
 
205
199
  ##
@@ -73,8 +73,6 @@ module Gcloud
73
73
  ensure_service!
74
74
  list_grpc = @service.list_sinks token: token, max: @max
75
75
  self.class.from_grpc list_grpc, @service
76
- rescue GRPC::BadStatus => e
77
- raise Gcloud::Error.from_error(e)
78
76
  end
79
77
 
80
78
  ##
data/lib/gcloud/pubsub.rb CHANGED
@@ -36,6 +36,9 @@ module Gcloud
36
36
  # The default scope is:
37
37
  #
38
38
  # * `https://www.googleapis.com/auth/pubsub`
39
+ # @param [Integer] retries Number of times to retry requests on server error.
40
+ # The default value is `3`. Optional.
41
+ # @param [Integer] timeout Default timeout to use in requests. Optional.
39
42
  #
40
43
  # @return [Gcloud::Pubsub::Project]
41
44
  #
@@ -47,7 +50,8 @@ module Gcloud
47
50
  # topic = pubsub.topic "my-topic"
48
51
  # topic.publish "task completed"
49
52
  #
50
- def self.pubsub project = nil, keyfile = nil, scope: nil
53
+ def self.pubsub project = nil, keyfile = nil, scope: nil, retries: nil,
54
+ timeout: nil
51
55
  project ||= Gcloud::Pubsub::Project.default_project
52
56
  if ENV["PUBSUB_EMULATOR_HOST"]
53
57
  ps = Gcloud::Pubsub::Project.new project, :this_channel_is_insecure
@@ -59,7 +63,9 @@ module Gcloud
59
63
  else
60
64
  credentials = Gcloud::Pubsub::Credentials.new keyfile, scope: scope
61
65
  end
62
- Gcloud::Pubsub::Project.new project, credentials
66
+ Gcloud::Pubsub::Project.new(
67
+ Gcloud::Pubsub::Service.new(
68
+ project, credentials, retries: retries, timeout: timeout))
63
69
  end
64
70
 
65
71
  ##
@@ -72,7 +78,7 @@ module Gcloud
72
78
  # Cloud Pub/Sub allows developers to communicate between independently written
73
79
  # applications.
74
80
  #
75
- # Gcloud's goal is to provide a API that is familiar and comfortable to
81
+ # The goal of gcloud-ruby is to provide a API that is comfortable to
76
82
  # Rubyists. Authentication is handled by {Gcloud#pubsub}. You can provide the
77
83
  # project and credential information to connect to the Pub/Sub service, or if
78
84
  # you are running on Google Compute Engine this configuration is taken care
@@ -389,24 +395,23 @@ module Gcloud
389
395
  # end
390
396
  # ```
391
397
  #
392
- # ## Configuring Backoff
398
+ # ## Configuring retries and timeout
393
399
  #
394
- # The {Gcloud::Backoff} class allows users to globally configure how Cloud API
395
- # requests are automatically retried in the case of some errors, such as a
396
- # `500` or `503` status code, or a specific internal error code such as
397
- # `rateLimitExceeded`.
400
+ # You can configure how many times API requests may be automatically retried.
401
+ # When an API request fails, the response will be inspected to see if the
402
+ # request meets criteria indicating that it may succeed on retry, such as
403
+ # `500` and `503` status codes or a specific internal error code such as
404
+ # `rateLimitExceeded`. If it meets the criteria, the request will be retried
405
+ # after a delay. If another error occurs, the delay will be increased before a
406
+ # subsequent attempt, until the `retries` limit is reached.
398
407
  #
399
- # If an API call fails, the response will be inspected to see if the call
400
- # should be retried. If the response matches the criteria, then the request
401
- # will be retried after a delay. If another error occurs, the delay will be
402
- # increased incrementally before a subsequent attempt. The first retry will be
403
- # delayed one second, the second retry two seconds, and so on.
408
+ # You can also set the request `timeout` value in seconds.
404
409
  #
405
410
  # ```ruby
406
411
  # require "gcloud"
407
- # require "gcloud/backoff"
408
412
  #
409
- # Gcloud::Backoff.retries = 5 # Raise the maximum number of retries from 3
413
+ # gcloud = Gcloud.new
414
+ # pubsub = gcloud.pubsub retries: 10, timeout: 120
410
415
  # ```
411
416
  #
412
417
  # See the [Pub/Sub error codes](https://cloud.google.com/pubsub/error-codes)
@@ -452,7 +457,7 @@ module Gcloud
452
457
  # sub.topic.name #=> "projects/other-project-id/topics/other-topic"
453
458
  # ```
454
459
  #
455
- # ## Using the Gcloud SDK Emulator
460
+ # ## Using the Google Cloud Pub/Sub Emulator
456
461
  #
457
462
  # To develop and test your application locally, you can use the [Google Cloud
458
463
  # Pub/Sub Emulator](https://cloud.google.com/pubsub/emulator), which provides
@@ -0,0 +1,204 @@
1
+ # Copyright 2016 Google Inc. All rights reserved.
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
+ # http://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
+
16
+ require "gcloud/errors"
17
+
18
+ module Gcloud
19
+ module Pubsub
20
+ ##
21
+ # # Policy
22
+ #
23
+ # Represents a Cloud IAM Policy for the Pub/Sub service.
24
+ #
25
+ # A common pattern for updating a resource's metadata, such as its Policy,
26
+ # is to read the current data from the service, update the data locally, and
27
+ # then send the modified data for writing. This pattern may result in a
28
+ # conflict if two or more processes attempt the sequence simultaneously. IAM
29
+ # solves this problem with the {Gcloud::Pubsub::Policy#etag} property, which
30
+ # is used to verify whether the policy has changed since the last request.
31
+ # When you make a request to with an `etag` value, Cloud IAM compares the
32
+ # `etag` value in the request with the existing `etag` value associated with
33
+ # the policy. It writes the policy only if the `etag` values match.
34
+ #
35
+ # When you update a policy, first read the policy (and its current `etag`)
36
+ # from the service, then modify the policy locally, and then write the
37
+ # modified policy to the service. See {Gcloud::Pubsub::Topic#policy} and
38
+ # {Gcloud::Pubsub::Topic#policy=}.
39
+ #
40
+ # @see https://cloud.google.com/iam/docs/managing-policies Managing policies
41
+ # @see https://cloud.google.com/pubsub/reference/rpc/google.iam.v1#iampolicy
42
+ # google.iam.v1.IAMPolicy
43
+ #
44
+ # @attr [String] etag Used to verify whether the policy has changed since
45
+ # the last request. The policy will be written only if the `etag` values
46
+ # match.
47
+ # @attr [Hash{String => Array<String>}] roles The bindings that associate
48
+ # roles with an array of members. See [Understanding
49
+ # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
50
+ # listing of primitive and curated roles.
51
+ # See [Binding](https://cloud.google.com/pubsub/reference/rpc/google.iam.v1#binding)
52
+ # for a listing of values and patterns for members.
53
+ #
54
+ # @example
55
+ # require "gcloud"
56
+ #
57
+ # gcloud = Gcloud.new
58
+ # pubsub = gcloud.pubsub
59
+ # topic = pubsub.topic "my-topic"
60
+ #
61
+ # policy = topic.policy # API call
62
+ #
63
+ # policy.remove "roles/owner", "user:owner@example.com" # Local call
64
+ # policy.add "roles/owner", "user:newowner@example.com" # Local call
65
+ # policy.roles["roles/viewer"] = ["allUsers"] # Local call
66
+ #
67
+ # topic.policy = policy # API call
68
+ #
69
+ class Policy
70
+ attr_reader :etag, :roles
71
+
72
+ ##
73
+ # @private Creates a Policy object.
74
+ def initialize etag, roles
75
+ @etag = etag
76
+ @roles = roles
77
+ end
78
+
79
+ ##
80
+ # Convenience method for adding a member to a binding on this policy.
81
+ # See [Understanding
82
+ # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
83
+ # listing of primitive and curated roles.
84
+ # See [Binding](https://cloud.google.com/pubsub/reference/rpc/google.iam.v1#binding)
85
+ # for a listing of values and patterns for members.
86
+ #
87
+ # @param [String] role_name A Cloud IAM role, such as
88
+ # `"roles/pubsub.admin"`.
89
+ # @param [String] member A Cloud IAM identity, such as
90
+ # `"user:owner@example.com"`.
91
+ #
92
+ # @example
93
+ # require "gcloud"
94
+ #
95
+ # gcloud = Gcloud.new
96
+ # pubsub = gcloud.pubsub
97
+ # topic = pubsub.topic "my-topic"
98
+ #
99
+ # policy = topic.policy # API call
100
+ #
101
+ # policy.add "roles/owner", "user:newowner@example.com" # Local call
102
+ #
103
+ # topic.policy = policy # API call
104
+ #
105
+ def add role_name, member
106
+ role(role_name) << member
107
+ end
108
+
109
+ ##
110
+ # Convenience method for removing a member from a binding on this policy.
111
+ # See [Understanding
112
+ # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
113
+ # listing of primitive and curated roles.
114
+ # See [Binding](https://cloud.google.com/pubsub/reference/rpc/google.iam.v1#binding)
115
+ # for a listing of values and patterns for members.
116
+ #
117
+ # @param [String] role_name A Cloud IAM role, such as
118
+ # `"roles/pubsub.admin"`.
119
+ # @param [String] member A Cloud IAM identity, such as
120
+ # `"user:owner@example.com"`.
121
+ #
122
+ # @example
123
+ # require "gcloud"
124
+ #
125
+ # gcloud = Gcloud.new
126
+ # pubsub = gcloud.pubsub
127
+ # topic = pubsub.topic "my-topic"
128
+ #
129
+ # policy = topic.policy # API call
130
+ #
131
+ # policy.remove "roles/owner", "user:owner@example.com" # Local call
132
+ #
133
+ # topic.policy = policy # API call
134
+ #
135
+ def remove role_name, member
136
+ role(role_name).delete member
137
+ end
138
+
139
+ ##
140
+ # Convenience method returning the array of members bound to a role in
141
+ # this policy, or an empty array if no value is present for the role in
142
+ # {#roles}. See [Understanding
143
+ # Roles](https://cloud.google.com/iam/docs/understanding-roles) for a
144
+ # listing of primitive and curated roles. See
145
+ # [Binding](https://cloud.google.com/pubsub/reference/rpc/google.iam.v1#binding)
146
+ # for a listing of values and patterns for members.
147
+ #
148
+ # @return [Array<String>] The members strings, or an empty array.
149
+ #
150
+ # @example
151
+ # require "gcloud"
152
+ #
153
+ # gcloud = Gcloud.new
154
+ # pubsub = gcloud.pubsub
155
+ # topic = pubsub.topic "my-topic"
156
+ #
157
+ # policy = topic.policy
158
+ #
159
+ # policy.role("roles/viewer") << "user:viewer@example.com"
160
+ #
161
+ def role role_name
162
+ roles[role_name] ||= []
163
+ end
164
+
165
+ ##
166
+ # Returns a deep copy of the policy.
167
+ #
168
+ # @return [Policy]
169
+ #
170
+ def deep_dup
171
+ dup.tap do |p|
172
+ roles_dup = p.roles.each_with_object({}) do |(k, v), memo|
173
+ memo[k] = v.dup rescue value
174
+ end
175
+ p.instance_variable_set "@roles", roles_dup
176
+ end
177
+ end
178
+
179
+ ##
180
+ # @private Convert the Policy to a Google::Iam::V1::Policy object.
181
+ def to_grpc
182
+ Google::Iam::V1::Policy.new(
183
+ etag: etag,
184
+ bindings: roles.keys.map do |role_name|
185
+ next if roles[role_name].empty?
186
+ Google::Iam::V1::Binding.new(
187
+ role: role_name,
188
+ members: roles[role_name]
189
+ )
190
+ end
191
+ )
192
+ end
193
+
194
+ ##
195
+ # @private New Policy from a Google::Iam::V1::Policy object.
196
+ def self.from_grpc grpc
197
+ roles = grpc.bindings.each_with_object({}) do |binding, memo|
198
+ memo[binding.role] = binding.members.to_a
199
+ end
200
+ new grpc.etag, roles
201
+ end
202
+ end
203
+ end
204
+ end
@@ -48,11 +48,9 @@ module Gcloud
48
48
  attr_accessor :service
49
49
 
50
50
  ##
51
- # @private Creates a new Connection instance.
52
- def initialize project, credentials
53
- project = project.to_s # Always cast to a string
54
- fail ArgumentError, "project is missing" if project.empty?
55
- @service = Service.new project, credentials
51
+ # @private Creates a new Pub/Sub Project instance.
52
+ def initialize service
53
+ @service = service
56
54
  end
57
55
 
58
56
  # The Pub/Sub project connected to.
@@ -142,12 +140,9 @@ module Gcloud
142
140
  return Topic.new_lazy(topic_name, service, options) if skip_lookup
143
141
  grpc = service.get_topic topic_name
144
142
  Topic.from_grpc grpc, service
145
- rescue GRPC::BadStatus => e
146
- if e.code == 5
147
- return create_topic(topic_name) if autocreate
148
- return nil
149
- end
150
- raise Error.from_error(e)
143
+ rescue Gcloud::NotFoundError
144
+ return create_topic(topic_name) if autocreate
145
+ nil
151
146
  end
152
147
  alias_method :get_topic, :topic
153
148
  alias_method :find_topic, :topic
@@ -170,8 +165,6 @@ module Gcloud
170
165
  ensure_service!
171
166
  grpc = service.create_topic topic_name
172
167
  Topic.from_grpc grpc, service
173
- rescue GRPC::BadStatus => e
174
- raise Error.from_error(e)
175
168
  end
176
169
  alias_method :new_topic, :create_topic
177
170
 
@@ -213,8 +206,6 @@ module Gcloud
213
206
  options = { token: token, max: max }
214
207
  grpc = service.list_topics options
215
208
  Topic::List.from_grpc grpc, service, max
216
- rescue GRPC::BadStatus => e
217
- raise Error.from_error(e)
218
209
  end
219
210
  alias_method :find_topics, :topics
220
211
  alias_method :list_topics, :topics
@@ -232,8 +223,9 @@ module Gcloud
232
223
  # @param [Hash] attributes Optional attributes for the message.
233
224
  # @option attributes [Boolean] :autocreate Flag to control whether the
234
225
  # provided topic will be created if it does not exist.
235
- # @yield [batch] a block for publishing multiple messages in one request
236
- # @yieldparam [Topic::Batch] batch the batch object
226
+ # @yield [publisher] a block for publishing multiple messages in one
227
+ # request
228
+ # @yieldparam [Topic::Publisher] publisher the topic publisher object
237
229
  #
238
230
  # @return [Message, Array<Message>] Returns the published message when
239
231
  # called without a block, or an array of messages when called with a
@@ -270,10 +262,10 @@ module Gcloud
270
262
  # gcloud = Gcloud.new
271
263
  # pubsub = gcloud.pubsub
272
264
  #
273
- # msgs = pubsub.publish "my-topic" do |batch|
274
- # batch.publish "new-message-1", foo: :bar
275
- # batch.publish "new-message-2", foo: :baz
276
- # batch.publish "new-message-3", foo: :bif
265
+ # msgs = pubsub.publish "my-topic" do |p|
266
+ # p.publish "new-message-1", foo: :bar
267
+ # p.publish "new-message-2", foo: :baz
268
+ # p.publish "new-message-3", foo: :bif
277
269
  # end
278
270
  #
279
271
  # @example With `autocreate`:
@@ -293,10 +285,10 @@ module Gcloud
293
285
  # extract autocreate option
294
286
  autocreate = attributes.delete :autocreate
295
287
  ensure_service!
296
- batch = Topic::Batch.new data, attributes
297
- yield batch if block_given?
298
- return nil if batch.messages.count.zero?
299
- publish_batch_messages topic_name, batch, autocreate
288
+ publisher = Topic::Publisher.new data, attributes
289
+ yield publisher if block_given?
290
+ return nil if publisher.messages.count.zero?
291
+ publish_batch_messages topic_name, publisher, autocreate
300
292
  end
301
293
 
302
294
  ##
@@ -363,14 +355,14 @@ module Gcloud
363
355
  grpc = service.create_subscription topic_name,
364
356
  subscription_name, options
365
357
  Subscription.from_grpc grpc, service
366
- rescue GRPC::BadStatus => e
367
- if autocreate && e.code == 5
358
+ rescue Gcloud::NotFoundError => e
359
+ if autocreate
368
360
  create_topic topic_name
369
361
  return subscribe(topic_name, subscription_name,
370
362
  deadline: deadline, endpoint: endpoint,
371
363
  autocreate: false)
372
364
  end
373
- raise Error.from_error(e)
365
+ raise e
374
366
  end
375
367
  alias_method :create_subscription, :subscribe
376
368
  alias_method :new_subscription, :subscribe
@@ -417,9 +409,8 @@ module Gcloud
417
409
  end
418
410
  grpc = service.get_subscription subscription_name
419
411
  Subscription.from_grpc grpc, service
420
- rescue GRPC::BadStatus => e
421
- return nil if e.code == 5
422
- raise Error.from_error(e)
412
+ rescue Gcloud::NotFoundError
413
+ nil
423
414
  end
424
415
  alias_method :get_subscription, :subscription
425
416
  alias_method :find_subscription, :subscription
@@ -461,8 +452,6 @@ module Gcloud
461
452
  options = { token: token, max: max }
462
453
  grpc = service.list_subscriptions options
463
454
  Subscription::List.from_grpc grpc, service, max
464
- rescue GRPC::BadStatus => e
465
- raise Error.from_error(e)
466
455
  end
467
456
  alias_method :find_subscriptions, :subscriptions
468
457
  alias_method :list_subscriptions, :subscriptions
@@ -481,13 +470,12 @@ module Gcloud
481
470
  def publish_batch_messages topic_name, batch, autocreate = false
482
471
  grpc = service.publish topic_name, batch.messages
483
472
  batch.to_gcloud_messages Array(grpc.message_ids)
484
- rescue GRPC::BadStatus => e
485
- if autocreate && e.code == 5
473
+ rescue Gcloud::NotFoundError => e
474
+ if autocreate
486
475
  create_topic topic_name
487
- publish_batch_messages topic_name, batch, false
488
- else
489
- raise Error.from_error(e)
476
+ return publish_batch_messages topic_name, batch, false
490
477
  end
478
+ raise e
491
479
  end
492
480
  end
493
481
  end