gcloud 0.6.3 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (175) hide show
  1. checksums.yaml +8 -8
  2. data/AUTHENTICATION.md +13 -9
  3. data/CHANGELOG.md +8 -3
  4. data/OVERVIEW.md +46 -8
  5. data/lib/gcloud.rb +123 -117
  6. data/lib/gcloud/backoff.rb +43 -15
  7. data/lib/gcloud/bigquery.rb +211 -195
  8. data/lib/gcloud/bigquery/connection.rb +9 -9
  9. data/lib/gcloud/bigquery/copy_job.rb +15 -16
  10. data/lib/gcloud/bigquery/credentials.rb +3 -3
  11. data/lib/gcloud/bigquery/data.rb +12 -11
  12. data/lib/gcloud/bigquery/dataset.rb +162 -216
  13. data/lib/gcloud/bigquery/dataset/access.rb +59 -43
  14. data/lib/gcloud/bigquery/dataset/list.rb +3 -3
  15. data/lib/gcloud/bigquery/errors.rb +9 -5
  16. data/lib/gcloud/bigquery/extract_job.rb +18 -18
  17. data/lib/gcloud/bigquery/insert_response.rb +7 -4
  18. data/lib/gcloud/bigquery/job.rb +48 -44
  19. data/lib/gcloud/bigquery/job/list.rb +3 -3
  20. data/lib/gcloud/bigquery/load_job.rb +24 -25
  21. data/lib/gcloud/bigquery/project.rb +145 -204
  22. data/lib/gcloud/bigquery/query_data.rb +10 -9
  23. data/lib/gcloud/bigquery/query_job.rb +23 -32
  24. data/lib/gcloud/bigquery/table.rb +238 -280
  25. data/lib/gcloud/bigquery/table/list.rb +3 -3
  26. data/lib/gcloud/bigquery/table/schema.rb +79 -87
  27. data/lib/gcloud/bigquery/view.rb +69 -82
  28. data/lib/gcloud/credentials.rb +3 -9
  29. data/lib/gcloud/datastore.rb +194 -170
  30. data/lib/gcloud/datastore/connection.rb +12 -8
  31. data/lib/gcloud/datastore/credentials.rb +6 -4
  32. data/lib/gcloud/datastore/dataset.rb +74 -141
  33. data/lib/gcloud/datastore/dataset/lookup_results.rb +6 -4
  34. data/lib/gcloud/datastore/dataset/query_results.rb +6 -4
  35. data/lib/gcloud/datastore/entity.rb +81 -76
  36. data/lib/gcloud/datastore/errors.rb +10 -8
  37. data/lib/gcloud/datastore/key.rb +41 -77
  38. data/lib/gcloud/datastore/properties.rb +3 -3
  39. data/lib/gcloud/datastore/proto.rb +7 -4
  40. data/lib/gcloud/datastore/query.rb +26 -3
  41. data/lib/gcloud/datastore/transaction.rb +12 -8
  42. data/lib/gcloud/dns.rb +180 -152
  43. data/lib/gcloud/dns/change.rb +16 -16
  44. data/lib/gcloud/dns/change/list.rb +3 -3
  45. data/lib/gcloud/dns/connection.rb +9 -10
  46. data/lib/gcloud/dns/credentials.rb +3 -3
  47. data/lib/gcloud/dns/errors.rb +9 -5
  48. data/lib/gcloud/dns/importer.rb +17 -23
  49. data/lib/gcloud/dns/project.rb +42 -64
  50. data/lib/gcloud/dns/record.rb +58 -46
  51. data/lib/gcloud/dns/record/list.rb +6 -7
  52. data/lib/gcloud/dns/zone.rb +198 -289
  53. data/lib/gcloud/dns/zone/list.rb +3 -3
  54. data/lib/gcloud/dns/zone/transaction.rb +56 -72
  55. data/lib/gcloud/errors.rb +174 -3
  56. data/lib/gcloud/gce.rb +3 -4
  57. data/lib/gcloud/grpc_utils.rb +76 -0
  58. data/lib/gcloud/logging.rb +308 -0
  59. data/lib/gcloud/logging/credentials.rb +29 -0
  60. data/lib/gcloud/logging/entry.rb +303 -0
  61. data/lib/gcloud/logging/entry/http_request.rb +141 -0
  62. data/lib/gcloud/logging/entry/list.rb +111 -0
  63. data/lib/gcloud/logging/entry/operation.rb +90 -0
  64. data/lib/gcloud/logging/logger.rb +307 -0
  65. data/lib/gcloud/logging/metric.rb +175 -0
  66. data/lib/gcloud/logging/metric/list.rb +98 -0
  67. data/lib/gcloud/logging/project.rb +650 -0
  68. data/lib/gcloud/logging/resource.rb +95 -0
  69. data/lib/gcloud/logging/resource_descriptor.rb +140 -0
  70. data/lib/gcloud/logging/resource_descriptor/list.rb +78 -0
  71. data/lib/gcloud/logging/service.rb +258 -0
  72. data/lib/gcloud/logging/sink.rb +233 -0
  73. data/lib/gcloud/logging/sink/list.rb +97 -0
  74. data/lib/gcloud/pubsub.rb +241 -199
  75. data/lib/gcloud/pubsub/credentials.rb +3 -3
  76. data/lib/gcloud/pubsub/message.rb +26 -20
  77. data/lib/gcloud/pubsub/project.rb +166 -233
  78. data/lib/gcloud/pubsub/received_message.rb +28 -38
  79. data/lib/gcloud/pubsub/service.rb +323 -0
  80. data/lib/gcloud/pubsub/subscription.rb +172 -242
  81. data/lib/gcloud/pubsub/subscription/list.rb +11 -9
  82. data/lib/gcloud/pubsub/topic.rb +152 -271
  83. data/lib/gcloud/pubsub/topic/batch.rb +66 -0
  84. data/lib/gcloud/pubsub/topic/list.rb +9 -7
  85. data/lib/gcloud/resource_manager.rb +158 -138
  86. data/lib/gcloud/resource_manager/connection.rb +6 -5
  87. data/lib/gcloud/resource_manager/credentials.rb +3 -3
  88. data/lib/gcloud/resource_manager/errors.rb +9 -5
  89. data/lib/gcloud/resource_manager/manager.rb +54 -86
  90. data/lib/gcloud/resource_manager/project.rb +69 -88
  91. data/lib/gcloud/resource_manager/project/list.rb +4 -5
  92. data/lib/gcloud/resource_manager/project/updater.rb +12 -14
  93. data/lib/gcloud/search.rb +158 -135
  94. data/lib/gcloud/search/api_client.rb +7 -7
  95. data/lib/gcloud/search/connection.rb +8 -8
  96. data/lib/gcloud/search/credentials.rb +3 -3
  97. data/lib/gcloud/search/document.rb +64 -87
  98. data/lib/gcloud/search/document/list.rb +5 -5
  99. data/lib/gcloud/search/errors.rb +9 -5
  100. data/lib/gcloud/search/field_value.rb +32 -38
  101. data/lib/gcloud/search/field_values.rb +50 -80
  102. data/lib/gcloud/search/fields.rb +44 -65
  103. data/lib/gcloud/search/index.rb +163 -204
  104. data/lib/gcloud/search/index/list.rb +5 -5
  105. data/lib/gcloud/search/project.rb +31 -47
  106. data/lib/gcloud/search/result.rb +27 -31
  107. data/lib/gcloud/search/result/list.rb +6 -6
  108. data/lib/gcloud/storage.rb +224 -190
  109. data/lib/gcloud/storage/bucket.rb +202 -227
  110. data/lib/gcloud/storage/bucket/acl.rb +83 -170
  111. data/lib/gcloud/storage/bucket/cors.rb +31 -34
  112. data/lib/gcloud/storage/bucket/list.rb +3 -3
  113. data/lib/gcloud/storage/connection.rb +11 -7
  114. data/lib/gcloud/storage/credentials.rb +3 -3
  115. data/lib/gcloud/storage/errors.rb +11 -8
  116. data/lib/gcloud/storage/file.rb +129 -171
  117. data/lib/gcloud/storage/file/acl.rb +51 -99
  118. data/lib/gcloud/storage/file/list.rb +3 -3
  119. data/lib/gcloud/storage/file/verifier.rb +3 -2
  120. data/lib/gcloud/storage/project.rb +111 -132
  121. data/lib/gcloud/upload.rb +4 -7
  122. data/lib/gcloud/version.rb +2 -4
  123. data/lib/google/api/annotations.rb +14 -0
  124. data/lib/google/api/http.rb +30 -0
  125. data/lib/google/api/label.rb +24 -0
  126. data/lib/google/api/monitored_resource.rb +25 -0
  127. data/lib/google/datastore/v1beta3/datastore.rb +115 -0
  128. data/lib/google/datastore/v1beta3/datastore_services.rb +33 -0
  129. data/lib/google/datastore/v1beta3/entity.rb +63 -0
  130. data/lib/google/datastore/v1beta3/query.rb +128 -0
  131. data/lib/google/devtools/cloudtrace/v1/trace.rb +78 -0
  132. data/lib/google/devtools/cloudtrace/v1/trace_services.rb +32 -0
  133. data/lib/google/example/library/v1/library.rb +91 -0
  134. data/lib/google/example/library/v1/library_services.rb +40 -0
  135. data/lib/google/iam/v1/iam_policy.rb +33 -0
  136. data/lib/google/iam/v1/iam_policy_services.rb +30 -0
  137. data/lib/google/iam/v1/policy.rb +25 -0
  138. data/lib/google/logging/type/http_request.rb +28 -0
  139. data/lib/google/logging/type/log_severity.rb +27 -0
  140. data/lib/google/logging/v2/log_entry.rb +44 -0
  141. data/lib/google/logging/v2/logging.rb +56 -0
  142. data/lib/google/logging/v2/logging_config.rb +59 -0
  143. data/lib/google/logging/v2/logging_config_services.rb +32 -0
  144. data/lib/google/logging/v2/logging_metrics.rb +51 -0
  145. data/lib/google/logging/v2/logging_metrics_services.rb +32 -0
  146. data/lib/google/logging/v2/logging_services.rb +31 -0
  147. data/lib/google/longrunning/operations.rb +50 -0
  148. data/lib/google/longrunning/operations_services.rb +29 -0
  149. data/lib/google/protobuf/any.rb +17 -0
  150. data/lib/google/protobuf/api.rb +31 -0
  151. data/lib/google/protobuf/descriptor.rb +0 -0
  152. data/lib/google/protobuf/duration.rb +17 -0
  153. data/lib/google/protobuf/empty.rb +15 -0
  154. data/lib/google/protobuf/field_mask.rb +16 -0
  155. data/lib/google/protobuf/source_context.rb +16 -0
  156. data/lib/google/protobuf/struct.rb +35 -0
  157. data/lib/google/protobuf/timestamp.rb +17 -0
  158. data/lib/google/protobuf/type.rb +79 -0
  159. data/lib/google/protobuf/wrappers.rb +48 -0
  160. data/lib/google/pubsub/v1/pubsub.rb +129 -0
  161. data/lib/google/pubsub/v1/pubsub_services.rb +56 -0
  162. data/lib/google/pubsub/v1beta2/pubsub.rb +126 -0
  163. data/lib/google/pubsub/v1beta2/pubsub_services.rb +56 -0
  164. data/lib/google/rpc/code.rb +32 -0
  165. data/lib/google/rpc/error_details.rb +61 -0
  166. data/lib/google/rpc/status.rb +19 -0
  167. data/lib/google/type/color.rb +20 -0
  168. data/lib/google/type/date.rb +18 -0
  169. data/lib/google/type/dayofweek.rb +23 -0
  170. data/lib/google/type/latlng.rb +17 -0
  171. data/lib/google/type/money.rb +18 -0
  172. data/lib/google/type/timeofday.rb +19 -0
  173. metadata +101 -4
  174. data/lib/gcloud/pubsub/connection.rb +0 -295
  175. data/lib/gcloud/pubsub/errors.rb +0 -93
@@ -1,4 +1,3 @@
1
- #--
2
1
  # Copyright 2015 Google Inc. All rights reserved.
3
2
  #
4
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,6 +12,7 @@
13
12
  # See the License for the specific language governing permissions and
14
13
  # limitations under the License.
15
14
 
15
+
16
16
  require "delegate"
17
17
 
18
18
  module Gcloud
@@ -24,7 +24,7 @@ module Gcloud
24
24
  ##
25
25
  # If not empty, indicates that there are more subscriptions
26
26
  # that match the request and this value should be passed to
27
- # the next Gcloud::PubSub::Topic#subscriptions to continue.
27
+ # the next {Gcloud::Pubsub::Topic#subscriptions} to continue.
28
28
  attr_accessor :token
29
29
 
30
30
  ##
@@ -32,19 +32,21 @@ module Gcloud
32
32
  def initialize arr = [], token = nil
33
33
  super arr
34
34
  @token = token
35
+ @token = nil if @token == ""
35
36
  end
36
37
 
37
38
  ##
38
- # New Subscription::List from a response object.
39
- def self.from_response resp, conn #:nodoc:
40
- subs = Array(resp.data["subscriptions"]).map do |gapi_object|
41
- if gapi_object.is_a? String
42
- Subscription.new_lazy gapi_object, conn
39
+ # @private New Subscriptions::List from a
40
+ # Google::Pubsub::V1::ListSubscriptionsRequest object.
41
+ def self.from_grpc grpc_list, service
42
+ subs = Array(grpc_list.subscriptions).map do |grpc|
43
+ if grpc.is_a? String
44
+ Subscription.new_lazy grpc, service
43
45
  else
44
- Subscription.from_gapi gapi_object, conn
46
+ Subscription.from_grpc grpc, service
45
47
  end
46
48
  end
47
- new subs, resp.data["nextPageToken"]
49
+ new subs, grpc_list.next_page_token
48
50
  end
49
51
  end
50
52
  end
@@ -1,4 +1,3 @@
1
- #--
2
1
  # Copyright 2015 Google Inc. All rights reserved.
3
2
  #
4
3
  # Licensed under the Apache License, Version 2.0 (the "License");
@@ -13,18 +12,20 @@
13
12
  # See the License for the specific language governing permissions and
14
13
  # limitations under the License.
15
14
 
16
- require "json"
17
- require "gcloud/pubsub/errors"
15
+
16
+ require "gcloud/errors"
17
+ require "gcloud/pubsub/topic/batch"
18
18
  require "gcloud/pubsub/topic/list"
19
19
  require "gcloud/pubsub/subscription"
20
20
 
21
21
  module Gcloud
22
22
  module Pubsub
23
23
  ##
24
- # = Topic
24
+ # # Topic
25
25
  #
26
26
  # A named resource to which messages are published.
27
27
  #
28
+ # @example
28
29
  # require "gcloud"
29
30
  #
30
31
  # gcloud = Gcloud.new
@@ -35,30 +36,30 @@ module Gcloud
35
36
  #
36
37
  class Topic
37
38
  ##
38
- # The Connection object.
39
- attr_accessor :connection #:nodoc:
39
+ # @private The gRPC Service object.
40
+ attr_accessor :service
40
41
 
41
42
  ##
42
- # The Google API Client object.
43
- attr_accessor :gapi #:nodoc:
43
+ # @private The gRPC Google::Pubsub::V1::Topic object.
44
+ attr_accessor :grpc
44
45
 
45
46
  ##
46
- # Create an empty Topic object.
47
- def initialize #:nodoc:
48
- @connection = nil
49
- @gapi = {}
47
+ # @private Create an empty {Topic} object.
48
+ def initialize
49
+ @service = nil
50
+ @grpc = Google::Pubsub::V1::Topic.new
50
51
  @name = nil
51
52
  @exists = nil
52
53
  end
53
54
 
54
55
  ##
55
- # New lazy Topic object without making an HTTP request.
56
- def self.new_lazy name, conn, options = {} #:nodoc:
56
+ # @private New lazy {Topic} object without making an HTTP request.
57
+ def self.new_lazy name, service, options = {}
57
58
  new.tap do |t|
58
- t.gapi = nil
59
- t.connection = conn
59
+ t.grpc = nil
60
+ t.service = service
60
61
  t.instance_eval do
61
- @name = conn.topic_path(name, options)
62
+ @name = service.topic_path(name, options)
62
63
  end
63
64
  end
64
65
  end
@@ -67,18 +68,15 @@ module Gcloud
67
68
  # The name of the topic in the form of
68
69
  # "/projects/project-identifier/topics/topic-name".
69
70
  def name
70
- @gapi ? @gapi["name"] : @name
71
+ @grpc ? @grpc.name : @name
71
72
  end
72
73
 
73
74
  ##
74
75
  # Permanently deletes the topic.
75
76
  #
76
- # === Returns
77
- #
78
- # +true+ if the topic was deleted.
79
- #
80
- # === Example
77
+ # @return [Boolean] Returns `true` if the topic was deleted.
81
78
  #
79
+ # @example
82
80
  # require "gcloud"
83
81
  #
84
82
  # gcloud = Gcloud.new
@@ -88,39 +86,30 @@ module Gcloud
88
86
  # topic.delete
89
87
  #
90
88
  def delete
91
- ensure_connection!
92
- resp = connection.delete_topic name
93
- if resp.success?
94
- true
95
- else
96
- fail ApiError.from_response(resp)
97
- end
89
+ ensure_service!
90
+ service.delete_topic name
91
+ return true
92
+ rescue GRPC::BadStatus => e
93
+ raise Error.from_error(e)
98
94
  end
99
95
 
100
96
  ##
101
- # Creates a new Subscription object on the current Topic.
102
- #
103
- # === Parameters
104
- #
105
- # +subscription_name+::
106
- # Name of the new subscription. Must start with a letter, and contain
107
- # only letters ([A-Za-z]), numbers ([0-9], dashes (-), underscores (_),
108
- # periods (.), tildes (~), plus (+) or percent signs (%). It must be
109
- # between 3 and 255 characters in length, and it must not start with
110
- # "goog". (+String+)
111
- # +deadline+::
112
- # The maximum number of seconds after a subscriber receives a message
113
- # before the subscriber should acknowledge the message. (+Integer+)
114
- # +endpoint+::
115
- # A URL locating the endpoint to which messages should be pushed.
116
- # e.g. "https://example.com/push" (+String+)
117
- #
118
- # === Returns
119
- #
120
- # Gcloud::Pubsub::Subscription
121
- #
122
- # === Examples
123
- #
97
+ # Creates a new {Subscription} object on the current Topic.
98
+ #
99
+ # @param [String] subscription_name Name of the new subscription. Must
100
+ # start with a letter, and contain only letters ([A-Za-z]), numbers
101
+ # ([0-9], dashes (-), underscores (_), periods (.), tildes (~), plus (+)
102
+ # or percent signs (%). It must be between 3 and 255 characters in
103
+ # length, and it must not start with "goog".
104
+ # @param [Integer] deadline The maximum number of seconds after a
105
+ # subscriber receives a message before the subscriber should acknowledge
106
+ # the message.
107
+ # @param [String] endpoint A URL locating the endpoint to which messages
108
+ # should be pushed.
109
+ #
110
+ # @return [Gcloud::Pubsub::Subscription]
111
+ #
112
+ # @example
124
113
  # require "gcloud"
125
114
  #
126
115
  # gcloud = Gcloud.new
@@ -130,8 +119,7 @@ module Gcloud
130
119
  # sub = topic.subscribe "my-topic-sub"
131
120
  # puts sub.name # => "my-topic-sub"
132
121
  #
133
- # The name is optional, and will be generated if not given.
134
- #
122
+ # @example The name is optional, and will be generated if not given:
135
123
  # require "gcloud"
136
124
  #
137
125
  # gcloud = Gcloud.new
@@ -141,9 +129,7 @@ module Gcloud
141
129
  # sub = topic.subscribe "my-topic-sub"
142
130
  # puts sub.name # => "generated-sub-name"
143
131
  #
144
- # The subscription can be created that waits two minutes for
145
- # acknowledgement and pushed all messages to an endpoint
146
- #
132
+ # @example Wait 2 minutes for acknowledgement and push all to an endpoint:
147
133
  # require "gcloud"
148
134
  #
149
135
  # gcloud = Gcloud.new
@@ -155,14 +141,12 @@ module Gcloud
155
141
  # endpoint: "https://example.com/push"
156
142
  #
157
143
  def subscribe subscription_name, deadline: nil, endpoint: nil
158
- ensure_connection!
144
+ ensure_service!
159
145
  options = { deadline: deadline, endpoint: endpoint }
160
- resp = connection.create_subscription name, subscription_name, options
161
- if resp.success?
162
- Subscription.from_gapi resp.data, connection
163
- else
164
- fail ApiError.from_response(resp)
165
- end
146
+ grpc = service.create_subscription name, subscription_name, options
147
+ Subscription.from_grpc grpc, service
148
+ rescue GRPC::BadStatus => e
149
+ raise Error.from_error(e)
166
150
  end
167
151
  alias_method :create_subscription, :subscribe
168
152
  alias_method :new_subscription, :subscribe
@@ -170,22 +154,16 @@ module Gcloud
170
154
  ##
171
155
  # Retrieves subscription by name.
172
156
  #
173
- # === Parameters
174
- #
175
- # +subscription_name+::
176
- # Name of a subscription. (+String+)
177
- # +skip_lookup+::
178
- # Optionally create a Subscription object without verifying the
179
- # subscription resource exists on the Pub/Sub service. Calls made on
180
- # this object will raise errors if the service resource does not exist.
181
- # Default is +false+. (+Boolean+)
182
- #
183
- # === Returns
157
+ # @param [String] subscription_name Name of a subscription.
158
+ # @param [Boolean] skip_lookup Optionally create a {Subscription} object
159
+ # without verifying the subscription resource exists on the Pub/Sub
160
+ # service. Calls made on this object will raise errors if the service
161
+ # resource does not exist. Default is `false`.
184
162
  #
185
- # Gcloud::Pubsub::Subscription or nil if subscription does not exist
186
- #
187
- # === Example
163
+ # @return [Gcloud::Pubsub::Subscription, nil] Returns `nil` if
164
+ # the subscription does not exist.
188
165
  #
166
+ # @example
189
167
  # require "gcloud"
190
168
  #
191
169
  # gcloud = Gcloud.new
@@ -195,9 +173,7 @@ module Gcloud
195
173
  # subscription = topic.subscription "my-topic-subscription"
196
174
  # puts subscription.name
197
175
  #
198
- # The lookup against the Pub/Sub service can be skipped using the
199
- # +skip_lookup+ option:
200
- #
176
+ # @example Skip the lookup against the service with `skip_lookup`:
201
177
  # require "gcloud"
202
178
  #
203
179
  # gcloud = Gcloud.new
@@ -208,14 +184,13 @@ module Gcloud
208
184
  # puts subscription.name
209
185
  #
210
186
  def subscription subscription_name, skip_lookup: nil
211
- ensure_connection!
212
- if skip_lookup
213
- return Subscription.new_lazy(subscription_name, connection)
214
- end
215
- resp = connection.get_subscription subscription_name
216
- return Subscription.from_gapi(resp.data, connection) if resp.success?
217
- return nil if resp.status == 404
218
- fail ApiError.from_response(resp)
187
+ ensure_service!
188
+ return Subscription.new_lazy subscription_name, service if skip_lookup
189
+ grpc = service.get_subscription subscription_name
190
+ Subscription.from_grpc grpc, service
191
+ rescue GRPC::BadStatus => e
192
+ return nil if e.code == 5
193
+ raise Error.from_error(e)
219
194
  end
220
195
  alias_method :get_subscription, :subscription
221
196
  alias_method :find_subscription, :subscription
@@ -223,21 +198,14 @@ module Gcloud
223
198
  ##
224
199
  # Retrieves a list of subscription names for the given project.
225
200
  #
226
- # === Parameters
227
- #
228
- # +token+::
229
- # The +token+ value returned by the last call to +subscriptions+;
230
- # indicates that this is a continuation of a call, and that the system
231
- # should return the next page of data. (+String+)
232
- # +max+::
233
- # Maximum number of subscriptions to return. (+Integer+)
201
+ # @param [String] token The `token` value returned by the last call to
202
+ # `subscriptions`; indicates that this is a continuation of a call, and
203
+ # that the system should return the next page of data.
204
+ # @param [Integer] max Maximum number of subscriptions to return.
234
205
  #
235
- # === Returns
236
- #
237
- # Array of Subscription objects (See Subscription::List)
238
- #
239
- # === Examples
206
+ # @return [Array<Subscription>] (See {Subscription::List})
240
207
  #
208
+ # @example
241
209
  # require "gcloud"
242
210
  #
243
211
  # gcloud = Gcloud.new
@@ -249,9 +217,7 @@ module Gcloud
249
217
  # puts subscription.name
250
218
  # end
251
219
  #
252
- # If you have a significant number of subscriptions, you may need to
253
- # paginate through them: (See Subscription::List#token)
254
- #
220
+ # @example With pagination: (See {Subscription::List#token})
255
221
  # require "gcloud"
256
222
  #
257
223
  # gcloud = Gcloud.new
@@ -271,14 +237,12 @@ module Gcloud
271
237
  # end
272
238
  #
273
239
  def subscriptions token: nil, max: nil
274
- ensure_connection!
240
+ ensure_service!
275
241
  options = { token: token, max: max }
276
- resp = connection.list_topics_subscriptions name, options
277
- if resp.success?
278
- Subscription::List.from_response resp, connection
279
- else
280
- fail ApiError.from_response(resp)
281
- end
242
+ grpc = service.list_topics_subscriptions name, options
243
+ Subscription::List.from_grpc grpc, service
244
+ rescue GRPC::BadStatus => e
245
+ raise Error.from_error(e)
282
246
  end
283
247
  alias_method :find_subscriptions, :subscriptions
284
248
  alias_method :list_subscriptions, :subscriptions
@@ -286,20 +250,16 @@ module Gcloud
286
250
  ##
287
251
  # Publishes one or more messages to the topic.
288
252
  #
289
- # === Parameters
290
- #
291
- # +data+::
292
- # The message data. (+String+)
293
- # +attributes+::
294
- # Optional attributes for the message. (+Hash+)
295
- #
296
- # === Returns
297
- #
298
- # Message object when called without a block,
299
- # Array of Message objects when called with a block
253
+ # @param [String] data The message data.
254
+ # @param [Hash] attributes Optional attributes for the message.
255
+ # @yield [batch] a block for publishing multiple messages in one request
256
+ # @yieldparam [Topic::Batch] batch the batch object
300
257
  #
301
- # === Examples
258
+ # @return [Message, Array<Message>] Returns the published message when
259
+ # called without a block, or an array of messages when called with a
260
+ # block.
302
261
  #
262
+ # @example
303
263
  # require "gcloud"
304
264
  #
305
265
  # gcloud = Gcloud.new
@@ -308,8 +268,7 @@ module Gcloud
308
268
  # topic = pubsub.topic "my-topic"
309
269
  # msg = topic.publish "new-message"
310
270
  #
311
- # Additionally, a message can be published with attributes:
312
- #
271
+ # @example Additionally, a message can be published with attributes:
313
272
  # require "gcloud"
314
273
  #
315
274
  # gcloud = Gcloud.new
@@ -320,8 +279,7 @@ module Gcloud
320
279
  # foo: :bar,
321
280
  # this: :that
322
281
  #
323
- # Multiple messages can be published at the same time by passing a block:
324
- #
282
+ # @example Multiple messages can be sent at the same time using a block:
325
283
  # require "gcloud"
326
284
  #
327
285
  # gcloud = Gcloud.new
@@ -335,7 +293,7 @@ module Gcloud
335
293
  # end
336
294
  #
337
295
  def publish data = nil, attributes = {}
338
- ensure_connection!
296
+ ensure_service!
339
297
  batch = Batch.new data, attributes
340
298
  yield batch if block_given?
341
299
  return nil if batch.messages.count.zero?
@@ -345,17 +303,12 @@ module Gcloud
345
303
  ##
346
304
  # Gets the access control policy.
347
305
  #
348
- # === Parameters
349
- #
350
- # +force+::
351
- # Force the latest policy to be retrieved from the Pub/Sub service when
352
- # +true. Otherwise the policy will be memoized to reduce the number of
353
- # API calls made to the Pub/Sub service. The default is +false+.
354
- # (+Boolean+)
306
+ # @param [Boolean] force Force the latest policy to be retrieved from the
307
+ # Pub/Sub service when `true`. Otherwise the policy will be memoized to
308
+ # reduce the number of API calls made to the Pub/Sub service. The
309
+ # default is `false`.
355
310
  #
356
- # === Returns
357
- #
358
- # A hash that conforms to the following structure:
311
+ # @return [Hash] Returns a hash that conforms to the following structure:
359
312
  #
360
313
  # {
361
314
  # "etag"=>"CAE=",
@@ -365,11 +318,7 @@ module Gcloud
365
318
  # }]
366
319
  # }
367
320
  #
368
- # === Examples
369
- #
370
- # By default, the policy values are memoized to reduce the number of API
371
- # calls to the Pub/Sub service.
372
- #
321
+ # @example Policy values are memoized to reduce the number of API calls:
373
322
  # require "gcloud"
374
323
  #
375
324
  # gcloud = Gcloud.new
@@ -379,9 +328,7 @@ module Gcloud
379
328
  # puts topic.policy["bindings"]
380
329
  # puts topic.policy["rules"]
381
330
  #
382
- # To retrieve the latest policy from the Pub/Sub service, use the +force+
383
- # flag.
384
- #
331
+ # @example Use `force` to retrieve the latest policy from the service:
385
332
  # require "gcloud"
386
333
  #
387
334
  # gcloud = Gcloud.new
@@ -393,22 +340,19 @@ module Gcloud
393
340
  def policy force: nil
394
341
  @policy = nil if force
395
342
  @policy ||= begin
396
- ensure_connection!
397
- resp = connection.get_topic_policy name
398
- fail ApiError.from_response(resp) unless resp.success?
399
- policy = resp.data
400
- policy = policy.to_hash if policy.respond_to? :to_hash
401
- policy
343
+ ensure_service!
344
+ grpc = service.get_topic_policy name
345
+ JSON.parse(Google::Iam::V1::Policy.encode_json(grpc))
346
+ rescue GRPC::BadStatus => e
347
+ raise Error.from_error(e)
402
348
  end
403
349
  end
404
350
 
405
351
  ##
406
352
  # Sets the access control policy.
407
353
  #
408
- # === Parameters
409
- #
410
- # +new_policy+::
411
- # A hash that conforms to the following structure:
354
+ # @param [String] new_policy A hash that conforms to the following
355
+ # structure:
412
356
  #
413
357
  # {
414
358
  # "bindings" => [{
@@ -417,8 +361,7 @@ module Gcloud
417
361
  # }]
418
362
  # }
419
363
  #
420
- # === Example
421
- #
364
+ # @example
422
365
  # require "gcloud"
423
366
  #
424
367
  # gcloud = Gcloud.new
@@ -434,28 +377,23 @@ module Gcloud
434
377
  # topic.policy = viewer_policy
435
378
  #
436
379
  def policy= new_policy
437
- ensure_connection!
438
- resp = connection.set_topic_policy name, new_policy
439
- if resp.success?
440
- @policy = resp.data["policy"]
441
- @policy = @policy.to_hash if @policy.respond_to? :to_hash
442
- else
443
- fail ApiError.from_response(resp)
444
- end
380
+ ensure_service!
381
+ grpc = service.set_topic_policy name, new_policy
382
+ @policy = JSON.parse(Google::Iam::V1::Policy.encode_json(grpc))
383
+ rescue GRPC::BadStatus => e
384
+ raise Error.from_error(e)
445
385
  end
446
386
 
447
387
  ##
448
- # Tests the specified permissions against the {Cloud
449
- # IAM}[https://cloud.google.com/iam/] access control policy. See
450
- # {Managing Policies}[https://cloud.google.com/iam/docs/managing-policies]
451
- # for more information.
388
+ # Tests the specified permissions against the [Cloud
389
+ # IAM](https://cloud.google.com/iam/) access control policy.
452
390
  #
453
- # === Parameters
391
+ # @see https://cloud.google.com/iam/docs/managing-policies Managing
392
+ # Policies
454
393
  #
455
- # +permissions+::
456
- # The set of permissions to check access for. Permissions with wildcards
457
- # (such as +*+ or +storage.*+) are not allowed.
458
- # (String or Array of Strings)
394
+ # @param [String, Array<String>] permissions The set of permissions to
395
+ # check access for. Permissions with wildcards (such as `*` or
396
+ # `storage.*`) are not allowed.
459
397
  #
460
398
  # The permissions that can be checked on a topic are:
461
399
  #
@@ -467,12 +405,9 @@ module Gcloud
467
405
  # * pubsub.topics.getIamPolicy
468
406
  # * pubsub.topics.setIamPolicy
469
407
  #
470
- # === Returns
471
- #
472
- # The permissions that have access. (Array of Strings)
473
- #
474
- # === Example
408
+ # @return [Array<Strings>] The permissions that have access.
475
409
  #
410
+ # @example
476
411
  # require "gcloud"
477
412
  #
478
413
  # gcloud = Gcloud.new
@@ -485,20 +420,18 @@ module Gcloud
485
420
  #
486
421
  def test_permissions *permissions
487
422
  permissions = Array(permissions).flatten
488
- ensure_connection!
489
- resp = connection.test_topic_permissions name, permissions
490
- if resp.success?
491
- Array(resp.data["permissions"])
492
- else
493
- fail ApiError.from_response(resp)
494
- end
423
+ permissions = Array(permissions).flatten
424
+ ensure_service!
425
+ grpc = service.test_topic_permissions name, permissions
426
+ grpc.permissions
427
+ rescue GRPC::BadStatus => e
428
+ raise Error.from_error(e)
495
429
  end
496
430
 
497
431
  ##
498
432
  # Determines whether the topic exists in the Pub/Sub service.
499
433
  #
500
- # === Example
501
- #
434
+ # @example
502
435
  # require "gcloud"
503
436
  #
504
437
  # gcloud = Gcloud.new
@@ -508,19 +441,19 @@ module Gcloud
508
441
  # topic.exists? #=> true
509
442
  #
510
443
  def exists?
511
- # Always true if we have a gapi object
512
- return true unless @gapi.nil?
444
+ # Always true if we have a grpc object
445
+ return true unless @grpc.nil?
513
446
  # If we have a value, return it
514
447
  return @exists unless @exists.nil?
515
- ensure_gapi!
516
- @exists = !@gapi.nil?
448
+ ensure_grpc!
449
+ @exists = !@grpc.nil?
517
450
  end
518
451
 
519
452
  ##
453
+ # @private
520
454
  # Determines whether the topic object was created with an HTTP call.
521
455
  #
522
- # === Example
523
- #
456
+ # @example
524
457
  # require "gcloud"
525
458
  #
526
459
  # gcloud = Gcloud.new
@@ -529,98 +462,46 @@ module Gcloud
529
462
  # topic = pubsub.topic "my-topic"
530
463
  # topic.lazy? #=> false
531
464
  #
532
- def lazy? #:nodoc:
533
- @gapi.nil?
465
+ def lazy?
466
+ @grpc.nil?
534
467
  end
535
468
 
536
469
  ##
537
- # New Topic from a Google API Client object.
538
- def self.from_gapi gapi, conn #:nodoc:
470
+ # @private New Topic from a Google::Pubsub::V1::Topic object.
471
+ def self.from_grpc grpc, service
539
472
  new.tap do |f|
540
- f.gapi = gapi
541
- f.connection = conn
473
+ f.grpc = grpc
474
+ f.service = service
542
475
  end
543
476
  end
544
477
 
545
478
  protected
546
479
 
547
480
  ##
548
- # Raise an error unless an active connection is available.
549
- def ensure_connection!
550
- fail "Must have active connection" unless connection
481
+ # @private Raise an error unless an active connection to the service is
482
+ # available.
483
+ def ensure_service!
484
+ fail "Must have active connection to service" unless service
551
485
  end
552
486
 
553
487
  ##
554
- # Ensures a Google API object exists.
555
- def ensure_gapi!
556
- ensure_connection!
557
- return @gapi if @gapi
558
- resp = connection.get_topic @name
559
- @gapi = resp.data if resp.success?
488
+ # Ensures a Google::Pubsub::V1::Topic object exists.
489
+ def ensure_grpc!
490
+ ensure_service!
491
+ return @grpc if @grpc
492
+ @grpc = service.get_topic @name
493
+ rescue GRPC::BadStatus => e
494
+ return nil if e.code == 5
495
+ raise Error.from_error(e)
560
496
  end
561
497
 
562
498
  ##
563
499
  # Call the publish API with arrays of data data and attrs.
564
500
  def publish_batch_messages batch
565
- resp = connection.publish name, batch.messages
566
- if resp.success?
567
- batch.to_gcloud_messages resp.data["messageIds"]
568
- else
569
- fail ApiError.from_response(resp)
570
- end
571
- end
572
-
573
- ##
574
- # Batch object used to publish multiple messages at once.
575
- class Batch
576
- ##
577
- # The messages to publish
578
- attr_reader :messages #:nodoc:
579
-
580
- ##
581
- # Create a new instance of the object.
582
- def initialize data = nil, attributes = {} #:nodoc:
583
- @messages = []
584
- @mode = :batch
585
- return if data.nil?
586
- @mode = :single
587
- publish data, attributes
588
- end
589
-
590
- ##
591
- # Add multiple messages to the topic.
592
- # All messages added will be published at once.
593
- # See Gcloud::Pubsub::Topic#publish
594
- def publish data, attributes = {}
595
- @messages << [data, attributes]
596
- end
597
-
598
- ##
599
- # Create Message objects with message ids.
600
- def to_gcloud_messages message_ids #:nodoc:
601
- msgs = @messages.zip(Array(message_ids)).map do |arr, id|
602
- Message.from_gapi "data" => arr[0],
603
- "attributes" => jsonify_hash(arr[1]),
604
- "messageId" => id
605
- end
606
- # Return just one Message if a single publish,
607
- # otherwise return the array of Messages.
608
- if @mode == :single && msgs.count <= 1
609
- msgs.first
610
- else
611
- msgs
612
- end
613
- end
614
-
615
- protected
616
-
617
- ##
618
- # Make the hash look like it was returned from the Cloud API.
619
- def jsonify_hash hash
620
- hash = (hash || {}).to_h
621
- return hash if hash.empty?
622
- JSON.parse(JSON.dump(hash))
623
- end
501
+ grpc = service.publish name, batch.messages
502
+ batch.to_gcloud_messages Array(grpc.message_ids)
503
+ rescue GRPC::BadStatus => e
504
+ raise Error.from_error(e)
624
505
  end
625
506
  end
626
507
  end