gcloud 0.6.3 → 0.7.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 (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,13 +12,14 @@
13
12
  # See the License for the specific language governing permissions and
14
13
  # limitations under the License.
15
14
 
15
+
16
16
  require "gcloud/credentials"
17
17
 
18
18
  module Gcloud
19
19
  module Pubsub
20
20
  ##
21
- # Represents the OAuth 2.0 signing logic for Pub/Sub.
22
- class Credentials < Gcloud::Credentials #:nodoc:
21
+ # @private Represents the OAuth 2.0 signing logic for Pub/Sub.
22
+ class Credentials < Gcloud::Credentials
23
23
  SCOPE = ["https://www.googleapis.com/auth/pubsub"]
24
24
  PATH_ENV_VARS = %w(PUBSUB_KEYFILE GCLOUD_KEYFILE GOOGLE_CLOUD_KEYFILE)
25
25
  JSON_ENV_VARS = %w(PUBSUB_KEYFILE_JSON GCLOUD_KEYFILE_JSON
@@ -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,20 +12,22 @@
13
12
  # See the License for the specific language governing permissions and
14
13
  # limitations under the License.
15
14
 
16
- require "gcloud/pubsub/errors"
15
+
16
+ require "gcloud/errors"
17
17
 
18
18
  module Gcloud
19
19
  module Pubsub
20
20
  ##
21
- # = Message
21
+ # # Message
22
22
  #
23
23
  # Represents a Pub/Sub Message.
24
24
  #
25
- # Message objects are created by Topic#publish.
26
- # Subscription#pull returns an array of ReceivedMessage objects, each of
27
- # which contains a Message object. Each ReceivedMessage object can be
25
+ # Message objects are created by {Topic#publish}.
26
+ # {Subscription#pull} returns an array of {ReceivedMessage} objects, each of
27
+ # which contains a Message object. Each {ReceivedMessage} object can be
28
28
  # acknowledged and/or delayed.
29
29
  #
30
+ # @example
30
31
  # require "gcloud"
31
32
  #
32
33
  # gcloud = Gcloud.new
@@ -44,45 +45,50 @@ module Gcloud
44
45
  #
45
46
  class Message
46
47
  ##
47
- # The Google API Client object.
48
- attr_accessor :gapi #:nodoc:
48
+ # @private The gRPC Google::Pubsub::V1::PubsubMessage object.
49
+ attr_accessor :grpc
49
50
 
50
51
  ##
51
52
  # Create an empty Message object.
52
53
  # This can be used to publish several messages in bulk.
53
54
  def initialize data = nil, attributes = {}
54
- @gapi = {}
55
- @gapi["data"] = data
56
- @gapi["attributes"] = attributes
55
+ # Convert attributes to strings to match the protobuf definition
56
+ attributes = Hash[attributes.map { |k, v| [String(k), String(v)] }]
57
+
58
+ @grpc = Google::Pubsub::V1::PubsubMessage.new(
59
+ data: String(data).encode("ASCII-8BIT"),
60
+ attributes: attributes)
57
61
  end
58
62
 
59
63
  ##
60
64
  # The received data.
61
65
  def data
62
- @gapi["data"]
66
+ @grpc.data
63
67
  end
64
68
 
65
69
  ##
66
70
  # The received attributes.
67
71
  def attributes
68
- attrs = @gapi["attributes"]
69
- attrs = attrs.to_hash if attrs.respond_to? :to_hash
70
- attrs
72
+ return @grpc.attributes.to_h if @grpc.attributes.respond_to? :to_h
73
+ # Enumerable doesn't have to_h on Ruby 2.0, so fallback to this
74
+ Hash[@grpc.attributes.to_a]
71
75
  end
72
76
 
73
77
  ##
74
78
  # The ID of this message, assigned by the server at publication time.
75
79
  # Guaranteed to be unique within the topic.
76
80
  def message_id
77
- @gapi["messageId"]
81
+ @grpc.message_id
78
82
  end
79
83
  alias_method :msg_id, :message_id
80
84
 
81
85
  ##
82
- # New Topic from a Google API Client object.
83
- def self.from_gapi gapi #:nodoc:
84
- new.tap do |f|
85
- f.gapi = gapi
86
+ # @private New Message from a Google::Pubsub::V1::PubsubMessage object.
87
+ def self.from_grpc grpc
88
+ new.tap do |m|
89
+ m.instance_eval do
90
+ @grpc = grpc
91
+ end
86
92
  end
87
93
  end
88
94
  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,24 +12,28 @@
13
12
  # See the License for the specific language governing permissions and
14
13
  # limitations under the License.
15
14
 
15
+
16
16
  require "gcloud/gce"
17
- require "gcloud/pubsub/connection"
17
+ require "gcloud/errors"
18
+ require "gcloud/pubsub/service"
18
19
  require "gcloud/pubsub/credentials"
19
- require "gcloud/pubsub/errors"
20
20
  require "gcloud/pubsub/topic"
21
21
 
22
22
  module Gcloud
23
23
  module Pubsub
24
24
  ##
25
- # = Project
25
+ # # Project
26
26
  #
27
27
  # Represents the project that pubsub messages are pushed to and pulled from.
28
- # Topic is a named resource to which messages are sent by publishers.
29
- # Subscription is a named resource representing the stream of messages from
30
- # a single, specific topic, to be delivered to the subscribing application.
31
- # Message is a combination of data and attributes that a publisher sends to
32
- # a topic and is eventually delivered to subscribers.
28
+ # {Topic} is a named resource to which messages are sent by publishers.
29
+ # {Subscription} is a named resource representing the stream of messages
30
+ # from a single, specific topic, to be delivered to the subscribing
31
+ # application. {Message} is a combination of data and attributes that a
32
+ # publisher sends to a topic and is eventually delivered to subscribers.
33
+ #
34
+ # See {Gcloud#pubsub}
33
35
  #
36
+ # @example
34
37
  # require "gcloud"
35
38
  #
36
39
  # gcloud = Gcloud.new
@@ -39,24 +42,22 @@ module Gcloud
39
42
  # topic = pubsub.topic "my-topic"
40
43
  # topic.publish "task completed"
41
44
  #
42
- # See Gcloud#pubsub
43
45
  class Project
44
46
  ##
45
- # The Connection object.
46
- attr_accessor :connection #:nodoc:
47
+ # @private The gRPC Service object.
48
+ attr_accessor :service
47
49
 
48
50
  ##
49
- # Creates a new Connection instance.
50
- def initialize project, credentials #:nodoc:
51
+ # @private Creates a new Connection instance.
52
+ def initialize project, credentials
51
53
  project = project.to_s # Always cast to a string
52
54
  fail ArgumentError, "project is missing" if project.empty?
53
- @connection = Connection.new project, credentials
55
+ @service = Service.new project, credentials
54
56
  end
55
57
 
56
58
  # The Pub/Sub project connected to.
57
59
  #
58
- # === Example
59
- #
60
+ # @example
60
61
  # require "gcloud"
61
62
  #
62
63
  # gcloud = Gcloud.new "my-todo-project",
@@ -66,12 +67,12 @@ module Gcloud
66
67
  # pubsub.project #=> "my-todo-project"
67
68
  #
68
69
  def project
69
- connection.project
70
+ service.project
70
71
  end
71
72
 
72
73
  ##
73
- # Default project.
74
- def self.default_project #:nodoc:
74
+ # @private Default project.
75
+ def self.default_project
75
76
  ENV["PUBSUB_PROJECT"] ||
76
77
  ENV["GCLOUD_PROJECT"] ||
77
78
  ENV["GOOGLE_CLOUD_PROJECT"] ||
@@ -81,67 +82,54 @@ module Gcloud
81
82
  ##
82
83
  # Retrieves topic by name.
83
84
  #
84
- # === Parameters
85
- #
86
- # +topic_name+::
87
- # Name of a topic. (+String+)
88
- # +autocreate+::
89
- # Flag to control whether the requested topic will be created if it does
90
- # not exist. Ignored if +skip_lookup+ is +true+. The default value is
91
- # +false+. (+Boolean+)
92
- # +project+::
93
- # If the topic belongs to a project other than the one currently
94
- # connected to, the alternate project ID can be specified here.
95
- # (+String+)
96
- # +skip_lookup+::
97
- # Optionally create a Topic object without verifying the topic resource
98
- # exists on the Pub/Sub service. Calls made on this object will raise
99
- # errors if the topic resource does not exist. Default is +false+.
100
- # (+Boolean+)
101
- #
102
- # === Returns
103
- #
104
- # Gcloud::Pubsub::Topic or nil if topic does not exist. Will return a
105
- # newly created Gcloud::Pubsub::Topic if the topic does not exist and
106
- # +autocreate+ is set to +true+.
107
- #
108
- # === Examples
109
- #
85
+ # The topic will be created if the topic does not exist and the
86
+ # `autocreate` option is set to true.
87
+ #
88
+ # @param [String] topic_name Name of a topic.
89
+ # @param [Boolean] autocreate Flag to control whether the requested topic
90
+ # will be created if it does not exist. Ignored if `skip_lookup` is
91
+ # `true`. The default value is `false`.
92
+ # @param [String] project If the topic belongs to a project other than the
93
+ # one currently connected to, the alternate project ID can be specified
94
+ # here.
95
+ # @param [Boolean] skip_lookup Optionally create a {Topic} object without
96
+ # verifying the topic resource exists on the Pub/Sub service. Calls made
97
+ # on this object will raise errors if the topic resource does not exist.
98
+ # Default is `false`.
99
+ #
100
+ # @return [Gcloud::Pubsub::Topic, nil] Returns `nil` if topic does not
101
+ # exist. Will return a newly created{ Gcloud::Pubsub::Topic} if the
102
+ # topic does not exist and `autocreate` is set to `true`.
103
+ #
104
+ # @example
110
105
  # require "gcloud"
111
106
  #
112
107
  # gcloud = Gcloud.new
113
108
  # pubsub = gcloud.pubsub
114
109
  # topic = pubsub.topic "existing-topic"
115
110
  #
116
- # By default +nil+ will be returned if the topic does not exist.
117
- # the topic will be created in Pub/Sub when needed.
118
- #
111
+ # @example By default `nil` will be returned if the topic does not exist.
119
112
  # require "gcloud"
120
113
  #
121
114
  # gcloud = Gcloud.new
122
115
  # pubsub = gcloud.pubsub
123
116
  # topic = pubsub.topic "non-existing-topic" #=> nil
124
117
  #
125
- # The topic will be created if the topic does not exist and the
126
- # +autocreate+ option is set to true.
127
- #
118
+ # @example With the `autocreate` option set to `true`.
128
119
  # require "gcloud"
129
120
  #
130
121
  # gcloud = Gcloud.new
131
122
  # pubsub = gcloud.pubsub
132
123
  # topic = pubsub.topic "non-existing-topic", autocreate: true
133
124
  #
134
- # A topic in a different project can be created using the +project+ flag.
135
- #
125
+ # @example Create a topic in a different project with the `project` flag.
136
126
  # require "gcloud"
137
127
  #
138
128
  # gcloud = Gcloud.new
139
129
  # pubsub = gcloud.pubsub
140
130
  # topic = pubsub.topic "another-topic", project: "another-project"
141
131
  #
142
- # The lookup against the Pub/Sub service can be skipped using the
143
- # +skip_lookup+ option:
144
- #
132
+ # @example Skip the lookup against the service with `skip_lookup`:
145
133
  # require "gcloud"
146
134
  #
147
135
  # gcloud = Gcloud.new
@@ -149,16 +137,17 @@ module Gcloud
149
137
  # topic = pubsub.topic "another-topic", skip_lookup: true
150
138
  #
151
139
  def topic topic_name, autocreate: nil, project: nil, skip_lookup: nil
152
- ensure_connection!
140
+ ensure_service!
153
141
  options = { project: project }
154
- return Topic.new_lazy(topic_name, connection, options) if skip_lookup
155
- resp = connection.get_topic topic_name
156
- return Topic.from_gapi(resp.data, connection) if resp.success?
157
- if resp.status == 404
142
+ return Topic.new_lazy(topic_name, service, options) if skip_lookup
143
+ grpc = service.get_topic topic_name
144
+ Topic.from_grpc grpc, service
145
+ rescue GRPC::BadStatus => e
146
+ if e.code == 5
158
147
  return create_topic(topic_name) if autocreate
159
148
  return nil
160
149
  end
161
- fail ApiError.from_response(resp)
150
+ raise Error.from_error(e)
162
151
  end
163
152
  alias_method :get_topic, :topic
164
153
  alias_method :find_topic, :topic
@@ -166,17 +155,11 @@ module Gcloud
166
155
  ##
167
156
  # Creates a new topic.
168
157
  #
169
- # === Parameters
170
- #
171
- # +topic_name+::
172
- # Name of a topic. (+String+)
173
- #
174
- # === Returns
158
+ # @param [String] topic_name Name of a topic.
175
159
  #
176
- # Gcloud::Pubsub::Topic
177
- #
178
- # === Examples
160
+ # @return [Gcloud::Pubsub::Topic]
179
161
  #
162
+ # @example
180
163
  # require "gcloud"
181
164
  #
182
165
  # gcloud = Gcloud.new
@@ -184,34 +167,26 @@ module Gcloud
184
167
  # topic = pubsub.create_topic "my-topic"
185
168
  #
186
169
  def create_topic topic_name
187
- ensure_connection!
188
- resp = connection.create_topic topic_name
189
- if resp.success?
190
- Topic.from_gapi resp.data, connection
191
- else
192
- fail ApiError.from_response(resp)
193
- end
170
+ ensure_service!
171
+ grpc = service.create_topic topic_name
172
+ Topic.from_grpc grpc, service
173
+ rescue GRPC::BadStatus => e
174
+ raise Error.from_error(e)
194
175
  end
195
176
  alias_method :new_topic, :create_topic
196
177
 
197
178
  ##
198
179
  # Retrieves a list of topics for the given project.
199
180
  #
200
- # === Parameters
181
+ # @param [String] token The `token` value returned by the last call to
182
+ # `topics`; indicates that this is a continuation of a call, and that
183
+ # the system should return the next page of data.
184
+ # @param [Integer] max Maximum number of topics to return.
201
185
  #
202
- # +token+::
203
- # The +token+ value returned by the last call to +topics+; indicates
204
- # that this is a continuation of a call, and that the system should
205
- # return the next page of data. (+String+)
206
- # +max+::
207
- # Maximum number of topics to return. (+Integer+)
208
- #
209
- # === Returns
210
- #
211
- # Array of Gcloud::Pubsub::Topic (See Gcloud::Pubsub::Topic::List)
212
- #
213
- # === Examples
186
+ # @return [Array<Gcloud::Pubsub::Topic>] (See
187
+ # {Gcloud::Pubsub::Topic::List})
214
188
  #
189
+ # @example
215
190
  # require "gcloud"
216
191
  #
217
192
  # gcloud = Gcloud.new
@@ -222,9 +197,7 @@ module Gcloud
222
197
  # puts topic.name
223
198
  # end
224
199
  #
225
- # If you have a significant number of topics, you may need to paginate
226
- # through them: (See Topic::List#token)
227
- #
200
+ # @example With pagination: (See {Topic::List#token})
228
201
  # require "gcloud"
229
202
  #
230
203
  # gcloud = Gcloud.new
@@ -243,40 +216,37 @@ module Gcloud
243
216
  # end
244
217
  #
245
218
  def topics token: nil, max: nil
246
- ensure_connection!
219
+ ensure_service!
247
220
  options = { token: token, max: max }
248
- resp = connection.list_topics options
249
- if resp.success?
250
- Topic::List.from_response resp, connection
251
- else
252
- fail ApiError.from_response(resp)
253
- end
221
+ grpc = service.list_topics options
222
+ Topic::List.from_grpc grpc, service
223
+ rescue GRPC::BadStatus => e
224
+ raise Error.from_error(e)
254
225
  end
255
226
  alias_method :find_topics, :topics
256
227
  alias_method :list_topics, :topics
257
228
 
258
229
  ##
259
- # Publishes one or more messages to the given topic.
230
+ # Publishes one or more messages to the given topic. The topic will be
231
+ # created if the topic does previously not exist and the `autocreate`
232
+ # option is provided.
260
233
  #
261
- # === Parameters
262
- #
263
- # +topic_name+::
264
- # Name of a topic. (+String+)
265
- # +data+::
266
- # The message data. (+String+)
267
- # +attributes+::
268
- # Optional attributes for the message. (+Hash+)
269
- # <code>attributes[:autocreate]</code>::
270
- # Flag to control whether the provided topic will be created if it does
271
- # not exist.
272
- #
273
- # === Returns
234
+ # A note about auto-creating the topic: Any message published to a topic
235
+ # without a subscription will be lost.
274
236
  #
275
- # Message object when called without a block,
276
- # Array of Message objects when called with a block
237
+ # @param [String] topic_name Name of a topic.
238
+ # @param [String] data The message data.
239
+ # @param [Hash] attributes Optional attributes for the message.
240
+ # @option attributes [Boolean] :autocreate Flag to control whether the
241
+ # provided topic will be created if it does not exist.
242
+ # @yield [batch] a block for publishing multiple messages in one request
243
+ # @yieldparam [Topic::Batch] batch the batch object
277
244
  #
278
- # === Examples
245
+ # @return [Message, Array<Message>] Returns the published message when
246
+ # called without a block, or an array of messages when called with a
247
+ # block.
279
248
  #
249
+ # @example
280
250
  # require "gcloud"
281
251
  #
282
252
  # gcloud = Gcloud.new
@@ -284,8 +254,7 @@ module Gcloud
284
254
  #
285
255
  # msg = pubsub.publish "my-topic", "new-message"
286
256
  #
287
- # Additionally, a message can be published with attributes:
288
- #
257
+ # @example Additionally, a message can be published with attributes:
289
258
  # require "gcloud"
290
259
  #
291
260
  # gcloud = Gcloud.new
@@ -294,8 +263,7 @@ module Gcloud
294
263
  # msg = pubsub.publish "my-topic", "new-message", foo: :bar,
295
264
  # this: :that
296
265
  #
297
- # Multiple messages can be published at the same time by passing a block:
298
- #
266
+ # @example Multiple messages can be sent at the same time using a block:
299
267
  # require "gcloud"
300
268
  #
301
269
  # gcloud = Gcloud.new
@@ -307,9 +275,7 @@ module Gcloud
307
275
  # batch.publish "new-message-3", foo: :bif
308
276
  # end
309
277
  #
310
- # Additionally, the topic will be created if the topic does previously not
311
- # exist and the +autocreate+ option is provided.
312
- #
278
+ # @example With `autocreate`:
313
279
  # require "gcloud"
314
280
  #
315
281
  # gcloud = Gcloud.new
@@ -317,9 +283,6 @@ module Gcloud
317
283
  #
318
284
  # msg = pubsub.publish "new-topic", "new-message", autocreate: true
319
285
  #
320
- # A note about auto-creating the topic: Any message published to a topic
321
- # without a subscription will be lost.
322
- #
323
286
  def publish topic_name, data = nil, attributes = {}
324
287
  # Fix parameters
325
288
  if data.is_a?(::Hash) && attributes.empty?
@@ -328,7 +291,7 @@ module Gcloud
328
291
  end
329
292
  # extract autocreate option
330
293
  autocreate = attributes.delete :autocreate
331
- ensure_connection!
294
+ ensure_service!
332
295
  batch = Topic::Batch.new data, attributes
333
296
  yield batch if block_given?
334
297
  return nil if batch.messages.count.zero?
@@ -336,34 +299,27 @@ module Gcloud
336
299
  end
337
300
 
338
301
  ##
339
- # Creates a new Subscription object for the provided topic.
340
- #
341
- # === Parameters
342
- #
343
- # +topic_name+::
344
- # Name of a topic. (+String+)
345
- # +subscription_name+::
346
- # Name of the new subscription. Must start with a letter, and contain
347
- # only letters ([A-Za-z]), numbers ([0-9], dashes (-), underscores (_),
348
- # periods (.), tildes (~), plus (+) or percent signs (%). It must be
349
- # between 3 and 255 characters in length, and it must not start with
350
- # "goog". (+String+)
351
- # +deadline+::
352
- # The maximum number of seconds after a subscriber receives a message
353
- # before the subscriber should acknowledge the message. (+Integer+)
354
- # +endpoint+::
355
- # A URL locating the endpoint to which messages should be pushed.
356
- # e.g. "https://example.com/push" (+String+)
357
- # +autocreate+::
358
- # Flag to control whether the topic will be created if it does not
359
- # exist.
360
- #
361
- # === Returns
362
- #
363
- # Gcloud::Pubsub::Subscription
364
- #
365
- # === Examples
366
- #
302
+ # Creates a new {Subscription} object for the provided topic. The topic
303
+ # will be created if the topic does previously not exist and the
304
+ # `autocreate` option is provided.
305
+ #
306
+ # @param [String] topic_name Name of a topic.
307
+ # @param [String] subscription_name Name of the new subscription. Must
308
+ # start with a letter, and contain only letters ([A-Za-z]), numbers
309
+ # ([0-9], dashes (-), underscores (_), periods (.), tildes (~), plus (+)
310
+ # or percent signs (%). It must be between 3 and 255 characters in
311
+ # length, and it must not start with "goog".
312
+ # @param [Integer] deadline The maximum number of seconds after a
313
+ # subscriber receives a message before the subscriber should acknowledge
314
+ # the message.
315
+ # @param [String] endpoint A URL locating the endpoint to which messages
316
+ # should be pushed.
317
+ # @param [String] autocreate Flag to control whether the topic will be
318
+ # created if it does not exist.
319
+ #
320
+ # @return [Gcloud::Pubsub::Subscription]
321
+ #
322
+ # @example
367
323
  # require "gcloud"
368
324
  #
369
325
  # gcloud = Gcloud.new
@@ -372,8 +328,7 @@ module Gcloud
372
328
  # sub = pubsub.subscribe "my-topic", "my-topic-sub"
373
329
  # puts sub.name # => "my-topic-sub"
374
330
  #
375
- # The name is optional, and will be generated if not given.
376
- #
331
+ # @example The name is optional, and will be generated if not given.
377
332
  # require "gcloud"
378
333
  #
379
334
  # gcloud = Gcloud.new
@@ -382,9 +337,7 @@ module Gcloud
382
337
  # sub = pubsub.subscribe "my-topic"
383
338
  # puts sub.name # => "generated-sub-name"
384
339
  #
385
- # The subscription can be created that waits two minutes for
386
- # acknowledgement and pushed all messages to an endpoint
387
- #
340
+ # @example Wait 2 minutes for acknowledgement and push all to an endpoint:
388
341
  # require "gcloud"
389
342
  #
390
343
  # gcloud = Gcloud.new
@@ -394,9 +347,7 @@ module Gcloud
394
347
  # deadline: 120,
395
348
  # endpoint: "https://example.com/push"
396
349
  #
397
- # Additionally, the topic will be created if the topic does previously not
398
- # exist and the +autocreate+ option is provided.
399
- #
350
+ # @example With `autocreate`:
400
351
  # require "gcloud"
401
352
  #
402
353
  # gcloud = Gcloud.new
@@ -406,18 +357,19 @@ module Gcloud
406
357
  #
407
358
  def subscribe topic_name, subscription_name, deadline: nil, endpoint: nil,
408
359
  autocreate: nil
409
- ensure_connection!
360
+ ensure_service!
410
361
  options = { deadline: deadline, endpoint: endpoint }
411
- resp = connection.create_subscription topic_name,
412
- subscription_name, options
413
- return Subscription.from_gapi(resp.data, connection) if resp.success?
414
- if autocreate && resp.status == 404
362
+ grpc = service.create_subscription topic_name,
363
+ subscription_name, options
364
+ Subscription.from_grpc grpc, service
365
+ rescue GRPC::BadStatus => e
366
+ if autocreate && e.code == 5
415
367
  create_topic topic_name
416
368
  return subscribe(topic_name, subscription_name,
417
369
  deadline: deadline, endpoint: endpoint,
418
370
  autocreate: false)
419
371
  end
420
- fail ApiError.from_response(resp)
372
+ raise Error.from_error(e)
421
373
  end
422
374
  alias_method :create_subscription, :subscribe
423
375
  alias_method :new_subscription, :subscribe
@@ -425,26 +377,19 @@ module Gcloud
425
377
  ##
426
378
  # Retrieves subscription by name.
427
379
  #
428
- # === Parameters
429
- #
430
- # +subscription_name+::
431
- # Name of a subscription. (+String+)
432
- # +project+::
433
- # If the subscription belongs to a project other than the one currently
434
- # connected to, the alternate project ID can be specified here.
435
- # (+String+)
436
- # +skip_lookup+::
437
- # Optionally create a Subscription object without verifying the
438
- # subscription resource exists on the Pub/Sub service. Calls made on
439
- # this object will raise errors if the service resource does not exist.
440
- # Default is +false+. (+Boolean+)
380
+ # @param [String] subscription_name Name of a subscription.
381
+ # @param [String] project If the subscription belongs to a project other
382
+ # than the one currently connected to, the alternate project ID can be
383
+ # specified here.
384
+ # @param [Boolean] skip_lookup Optionally create a {Subscription} object
385
+ # without verifying the subscription resource exists on the Pub/Sub
386
+ # service. Calls made on this object will raise errors if the service
387
+ # resource does not exist. Default is `false`.
441
388
  #
442
- # === Returns
443
- #
444
- # Gcloud::Pubsub::Subscription or +nil+ if the subscription does not exist
445
- #
446
- # === Example
389
+ # @return [Gcloud::Pubsub::Subscription, nil] Returns `nil` if the
390
+ # subscription does not exist
447
391
  #
392
+ # @example
448
393
  # require "gcloud"
449
394
  #
450
395
  # gcloud = Gcloud.new
@@ -453,9 +398,7 @@ module Gcloud
453
398
  # subscription = pubsub.subscription "my-sub"
454
399
  # puts subscription.name
455
400
  #
456
- # The lookup against the Pub/Sub service can be skipped using the
457
- # +skip_lookup+ option:
458
- #
401
+ # @example Skip the lookup against the service with `skip_lookup`:
459
402
  # require "gcloud"
460
403
  #
461
404
  # gcloud = Gcloud.new
@@ -466,15 +409,16 @@ module Gcloud
466
409
  # puts subscription.name
467
410
  #
468
411
  def subscription subscription_name, project: nil, skip_lookup: nil
469
- ensure_connection!
412
+ ensure_service!
470
413
  options = { project: project }
471
414
  if skip_lookup
472
- return Subscription.new_lazy(subscription_name, connection, options)
415
+ return Subscription.new_lazy subscription_name, service, options
473
416
  end
474
- resp = connection.get_subscription subscription_name
475
- return Subscription.from_gapi(resp.data, connection) if resp.success?
476
- return nil if resp.status == 404
477
- fail ApiError.from_response(resp)
417
+ grpc = service.get_subscription subscription_name
418
+ Subscription.from_grpc grpc, service
419
+ rescue GRPC::BadStatus => e
420
+ return nil if e.code == 5
421
+ raise Error.from_error(e)
478
422
  end
479
423
  alias_method :get_subscription, :subscription
480
424
  alias_method :find_subscription, :subscription
@@ -482,24 +426,16 @@ module Gcloud
482
426
  ##
483
427
  # Retrieves a list of subscriptions for the given project.
484
428
  #
485
- # === Parameters
429
+ # @param [String] prefix Filter results to subscriptions whose names begin
430
+ # with this prefix.
431
+ # @param [String] token A previously-returned page token representing part
432
+ # of the larger set of results to view.
433
+ # @param [Integer] max Maximum number of subscriptions to return.
486
434
  #
487
- # +prefix+::
488
- # Filter results to subscriptions whose names begin with this prefix.
489
- # (+String+)
490
- # +token+::
491
- # A previously-returned page token representing part of the larger set
492
- # of results to view. (+String+)
493
- # +max+::
494
- # Maximum number of subscriptions to return. (+Integer+)
495
- #
496
- # === Returns
497
- #
498
- # Array of Gcloud::Pubsub::Subscription
499
- # (See Gcloud::Pubsub::Subscription::List)
500
- #
501
- # === Examples
435
+ # @return [Array<Gcloud::Pubsub::Subscription>] (See
436
+ # {Gcloud::Pubsub::Subscription::List})
502
437
  #
438
+ # @example
503
439
  # require "gcloud"
504
440
  #
505
441
  # gcloud = Gcloud.new
@@ -510,9 +446,7 @@ module Gcloud
510
446
  # puts subscription.name
511
447
  # end
512
448
  #
513
- # If you have a significant number of subscriptions, you may need to
514
- # paginate through them: (See Subscription::List#token)
515
- #
449
+ # @example With pagination: (See {Subscription::List#token})
516
450
  # require "gcloud"
517
451
  #
518
452
  # gcloud = Gcloud.new
@@ -531,14 +465,12 @@ module Gcloud
531
465
  # end
532
466
  #
533
467
  def subscriptions prefix: nil, token: nil, max: nil
534
- ensure_connection!
468
+ ensure_service!
535
469
  options = { prefix: prefix, token: token, max: max }
536
- resp = connection.list_subscriptions options
537
- if resp.success?
538
- Subscription::List.from_response resp, connection
539
- else
540
- fail ApiError.from_response(resp)
541
- end
470
+ grpc = service.list_subscriptions options
471
+ Subscription::List.from_grpc grpc, service
472
+ rescue GRPC::BadStatus => e
473
+ raise Error.from_error(e)
542
474
  end
543
475
  alias_method :find_subscriptions, :subscriptions
544
476
  alias_method :list_subscriptions, :subscriptions
@@ -546,22 +478,23 @@ module Gcloud
546
478
  protected
547
479
 
548
480
  ##
549
- # Raise an error unless an active connection is available.
550
- def ensure_connection!
551
- 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
552
485
  end
553
486
 
554
487
  ##
555
488
  # Call the publish API with arrays of data data and attrs.
556
489
  def publish_batch_messages topic_name, batch, autocreate = false
557
- resp = connection.publish topic_name, batch.messages
558
- if resp.success?
559
- batch.to_gcloud_messages resp.data["messageIds"]
560
- elsif autocreate && resp.status == 404
490
+ grpc = service.publish topic_name, batch.messages
491
+ batch.to_gcloud_messages Array(grpc.message_ids)
492
+ rescue GRPC::BadStatus => e
493
+ if autocreate && e.code == 5
561
494
  create_topic topic_name
562
495
  publish_batch_messages topic_name, batch, false
563
496
  else
564
- fail ApiError.from_response(resp)
497
+ raise Error.from_error(e)
565
498
  end
566
499
  end
567
500
  end