gcloud 0.12.2 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. checksums.yaml +5 -13
  2. data/lib/gcloud.rb +27 -456
  3. data/lib/gcloud/bigquery.rb +2 -382
  4. data/lib/gcloud/datastore.rb +2 -576
  5. data/lib/gcloud/dns.rb +2 -321
  6. data/lib/gcloud/logging.rb +1 -322
  7. data/lib/gcloud/pubsub.rb +2 -476
  8. data/lib/gcloud/resource_manager.rb +2 -273
  9. data/lib/gcloud/storage.rb +2 -440
  10. data/lib/gcloud/translate.rb +1 -250
  11. data/lib/gcloud/version.rb +2 -2
  12. data/lib/gcloud/vision.rb +1 -501
  13. metadata +36 -332
  14. data/AUTHENTICATION.md +0 -75
  15. data/CHANGELOG.md +0 -382
  16. data/OVERVIEW.md +0 -259
  17. data/lib/gcloud/backoff.rb +0 -150
  18. data/lib/gcloud/bigquery/copy_job.rb +0 -97
  19. data/lib/gcloud/bigquery/credentials.rb +0 -29
  20. data/lib/gcloud/bigquery/data.rb +0 -239
  21. data/lib/gcloud/bigquery/dataset.rb +0 -753
  22. data/lib/gcloud/bigquery/dataset/access.rb +0 -507
  23. data/lib/gcloud/bigquery/dataset/list.rb +0 -169
  24. data/lib/gcloud/bigquery/extract_job.rb +0 -117
  25. data/lib/gcloud/bigquery/insert_response.rb +0 -81
  26. data/lib/gcloud/bigquery/job.rb +0 -299
  27. data/lib/gcloud/bigquery/job/list.rb +0 -172
  28. data/lib/gcloud/bigquery/load_job.rb +0 -202
  29. data/lib/gcloud/bigquery/project.rb +0 -475
  30. data/lib/gcloud/bigquery/query_data.rb +0 -234
  31. data/lib/gcloud/bigquery/query_job.rb +0 -137
  32. data/lib/gcloud/bigquery/schema.rb +0 -359
  33. data/lib/gcloud/bigquery/service.rb +0 -506
  34. data/lib/gcloud/bigquery/table.rb +0 -1141
  35. data/lib/gcloud/bigquery/table/list.rb +0 -180
  36. data/lib/gcloud/bigquery/view.rb +0 -475
  37. data/lib/gcloud/credentials.rb +0 -129
  38. data/lib/gcloud/datastore/commit.rb +0 -148
  39. data/lib/gcloud/datastore/credentials.rb +0 -35
  40. data/lib/gcloud/datastore/cursor.rb +0 -76
  41. data/lib/gcloud/datastore/dataset.rb +0 -660
  42. data/lib/gcloud/datastore/dataset/lookup_results.rb +0 -219
  43. data/lib/gcloud/datastore/dataset/query_results.rb +0 -386
  44. data/lib/gcloud/datastore/entity.rb +0 -449
  45. data/lib/gcloud/datastore/errors.rb +0 -41
  46. data/lib/gcloud/datastore/gql_query.rb +0 -211
  47. data/lib/gcloud/datastore/grpc_utils.rb +0 -132
  48. data/lib/gcloud/datastore/key.rb +0 -281
  49. data/lib/gcloud/datastore/properties.rb +0 -128
  50. data/lib/gcloud/datastore/query.rb +0 -348
  51. data/lib/gcloud/datastore/service.rb +0 -167
  52. data/lib/gcloud/datastore/transaction.rb +0 -362
  53. data/lib/gcloud/dns/change.rb +0 -158
  54. data/lib/gcloud/dns/change/list.rb +0 -173
  55. data/lib/gcloud/dns/credentials.rb +0 -29
  56. data/lib/gcloud/dns/importer.rb +0 -183
  57. data/lib/gcloud/dns/project.rb +0 -247
  58. data/lib/gcloud/dns/record.rb +0 -170
  59. data/lib/gcloud/dns/record/list.rb +0 -174
  60. data/lib/gcloud/dns/service.rb +0 -167
  61. data/lib/gcloud/dns/zone.rb +0 -759
  62. data/lib/gcloud/dns/zone/list.rb +0 -168
  63. data/lib/gcloud/dns/zone/transaction.rb +0 -176
  64. data/lib/gcloud/errors.rb +0 -206
  65. data/lib/gcloud/gce.rb +0 -56
  66. data/lib/gcloud/grpc_utils.rb +0 -87
  67. data/lib/gcloud/logging/credentials.rb +0 -29
  68. data/lib/gcloud/logging/entry.rb +0 -465
  69. data/lib/gcloud/logging/entry/http_request.rb +0 -141
  70. data/lib/gcloud/logging/entry/list.rb +0 -177
  71. data/lib/gcloud/logging/entry/operation.rb +0 -90
  72. data/lib/gcloud/logging/logger.rb +0 -307
  73. data/lib/gcloud/logging/metric.rb +0 -169
  74. data/lib/gcloud/logging/metric/list.rb +0 -172
  75. data/lib/gcloud/logging/project.rb +0 -642
  76. data/lib/gcloud/logging/resource.rb +0 -84
  77. data/lib/gcloud/logging/resource_descriptor.rb +0 -137
  78. data/lib/gcloud/logging/resource_descriptor/list.rb +0 -174
  79. data/lib/gcloud/logging/service.rb +0 -267
  80. data/lib/gcloud/logging/sink.rb +0 -227
  81. data/lib/gcloud/logging/sink/list.rb +0 -171
  82. data/lib/gcloud/pubsub/credentials.rb +0 -29
  83. data/lib/gcloud/pubsub/message.rb +0 -94
  84. data/lib/gcloud/pubsub/policy.rb +0 -204
  85. data/lib/gcloud/pubsub/project.rb +0 -482
  86. data/lib/gcloud/pubsub/received_message.rb +0 -160
  87. data/lib/gcloud/pubsub/service.rb +0 -334
  88. data/lib/gcloud/pubsub/subscription.rb +0 -565
  89. data/lib/gcloud/pubsub/subscription/list.rb +0 -208
  90. data/lib/gcloud/pubsub/topic.rb +0 -511
  91. data/lib/gcloud/pubsub/topic/list.rb +0 -174
  92. data/lib/gcloud/pubsub/topic/publisher.rb +0 -85
  93. data/lib/gcloud/resource_manager/credentials.rb +0 -30
  94. data/lib/gcloud/resource_manager/manager.rb +0 -266
  95. data/lib/gcloud/resource_manager/policy.rb +0 -211
  96. data/lib/gcloud/resource_manager/project.rb +0 -484
  97. data/lib/gcloud/resource_manager/project/list.rb +0 -167
  98. data/lib/gcloud/resource_manager/project/updater.rb +0 -130
  99. data/lib/gcloud/resource_manager/service.rb +0 -127
  100. data/lib/gcloud/storage/bucket.rb +0 -775
  101. data/lib/gcloud/storage/bucket/acl.rb +0 -810
  102. data/lib/gcloud/storage/bucket/cors.rb +0 -153
  103. data/lib/gcloud/storage/bucket/list.rb +0 -172
  104. data/lib/gcloud/storage/credentials.rb +0 -29
  105. data/lib/gcloud/storage/errors.rb +0 -65
  106. data/lib/gcloud/storage/file.rb +0 -842
  107. data/lib/gcloud/storage/file/acl.rb +0 -425
  108. data/lib/gcloud/storage/file/list.rb +0 -191
  109. data/lib/gcloud/storage/file/verifier.rb +0 -67
  110. data/lib/gcloud/storage/project.rb +0 -316
  111. data/lib/gcloud/storage/service.rb +0 -347
  112. data/lib/gcloud/translate/api.rb +0 -241
  113. data/lib/gcloud/translate/detection.rb +0 -137
  114. data/lib/gcloud/translate/language.rb +0 -69
  115. data/lib/gcloud/translate/service.rb +0 -80
  116. data/lib/gcloud/translate/translation.rb +0 -112
  117. data/lib/gcloud/vision/annotate.rb +0 -224
  118. data/lib/gcloud/vision/annotation.rb +0 -455
  119. data/lib/gcloud/vision/annotation/entity.rb +0 -234
  120. data/lib/gcloud/vision/annotation/face.rb +0 -1750
  121. data/lib/gcloud/vision/annotation/properties.rb +0 -245
  122. data/lib/gcloud/vision/annotation/safe_search.rb +0 -161
  123. data/lib/gcloud/vision/annotation/text.rb +0 -236
  124. data/lib/gcloud/vision/annotation/vertex.rb +0 -108
  125. data/lib/gcloud/vision/credentials.rb +0 -29
  126. data/lib/gcloud/vision/image.rb +0 -590
  127. data/lib/gcloud/vision/location.rb +0 -115
  128. data/lib/gcloud/vision/project.rb +0 -278
  129. data/lib/gcloud/vision/service.rb +0 -66
  130. data/lib/google/api/annotations.rb +0 -14
  131. data/lib/google/api/http.rb +0 -30
  132. data/lib/google/api/label.rb +0 -24
  133. data/lib/google/api/monitored_resource.rb +0 -25
  134. data/lib/google/datastore/v1beta3/datastore.rb +0 -115
  135. data/lib/google/datastore/v1beta3/datastore_services.rb +0 -33
  136. data/lib/google/datastore/v1beta3/entity.rb +0 -63
  137. data/lib/google/datastore/v1beta3/query.rb +0 -128
  138. data/lib/google/devtools/cloudtrace/v1/trace.rb +0 -78
  139. data/lib/google/devtools/cloudtrace/v1/trace_services.rb +0 -32
  140. data/lib/google/example/library/v1/library.rb +0 -91
  141. data/lib/google/example/library/v1/library_services.rb +0 -40
  142. data/lib/google/iam/v1/iam_policy.rb +0 -33
  143. data/lib/google/iam/v1/iam_policy_services.rb +0 -30
  144. data/lib/google/iam/v1/policy.rb +0 -25
  145. data/lib/google/logging/type/http_request.rb +0 -28
  146. data/lib/google/logging/type/log_severity.rb +0 -27
  147. data/lib/google/logging/v2/log_entry.rb +0 -44
  148. data/lib/google/logging/v2/logging.rb +0 -56
  149. data/lib/google/logging/v2/logging_config.rb +0 -59
  150. data/lib/google/logging/v2/logging_config_services.rb +0 -32
  151. data/lib/google/logging/v2/logging_metrics.rb +0 -51
  152. data/lib/google/logging/v2/logging_metrics_services.rb +0 -32
  153. data/lib/google/logging/v2/logging_services.rb +0 -31
  154. data/lib/google/longrunning/operations.rb +0 -50
  155. data/lib/google/longrunning/operations_services.rb +0 -29
  156. data/lib/google/protobuf/descriptor.rb +0 -0
  157. data/lib/google/pubsub/v1/pubsub.rb +0 -129
  158. data/lib/google/pubsub/v1/pubsub_services.rb +0 -56
  159. data/lib/google/pubsub/v1beta2/pubsub.rb +0 -126
  160. data/lib/google/pubsub/v1beta2/pubsub_services.rb +0 -56
  161. data/lib/google/rpc/code.rb +0 -32
  162. data/lib/google/rpc/error_details.rb +0 -61
  163. data/lib/google/rpc/status.rb +0 -19
  164. data/lib/google/type/color.rb +0 -20
  165. data/lib/google/type/date.rb +0 -18
  166. data/lib/google/type/dayofweek.rb +0 -23
  167. data/lib/google/type/latlng.rb +0 -17
  168. data/lib/google/type/money.rb +0 -18
  169. data/lib/google/type/timeofday.rb +0 -19
@@ -1,204 +0,0 @@
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
@@ -1,482 +0,0 @@
1
- # Copyright 2015 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/gce"
17
- require "gcloud/errors"
18
- require "gcloud/pubsub/service"
19
- require "gcloud/pubsub/credentials"
20
- require "gcloud/pubsub/topic"
21
-
22
- module Gcloud
23
- module Pubsub
24
- ##
25
- # # Project
26
- #
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
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}
35
- #
36
- # @example
37
- # require "gcloud"
38
- #
39
- # gcloud = Gcloud.new
40
- # pubsub = gcloud.pubsub
41
- #
42
- # topic = pubsub.topic "my-topic"
43
- # topic.publish "task completed"
44
- #
45
- class Project
46
- ##
47
- # @private The gRPC Service object.
48
- attr_accessor :service
49
-
50
- ##
51
- # @private Creates a new Pub/Sub Project instance.
52
- def initialize service
53
- @service = service
54
- end
55
-
56
- # The Pub/Sub project connected to.
57
- #
58
- # @example
59
- # require "gcloud"
60
- #
61
- # gcloud = Gcloud.new "my-todo-project",
62
- # "/path/to/keyfile.json"
63
- # pubsub = gcloud.pubsub
64
- #
65
- # pubsub.project #=> "my-todo-project"
66
- #
67
- def project
68
- service.project
69
- end
70
-
71
- ##
72
- # @private Default project.
73
- def self.default_project
74
- ENV["PUBSUB_PROJECT"] ||
75
- ENV["GCLOUD_PROJECT"] ||
76
- ENV["GOOGLE_CLOUD_PROJECT"] ||
77
- Gcloud::GCE.project_id
78
- end
79
-
80
- ##
81
- # Retrieves topic by name.
82
- #
83
- # The topic will be created if the topic does not exist and the
84
- # `autocreate` option is set to true.
85
- #
86
- # @param [String] topic_name Name of a topic.
87
- # @param [Boolean] autocreate Flag to control whether the requested topic
88
- # will be created if it does not exist. Ignored if `skip_lookup` is
89
- # `true`. The default value is `false`.
90
- # @param [String] project If the topic belongs to a project other than the
91
- # one currently connected to, the alternate project ID can be specified
92
- # here.
93
- # @param [Boolean] skip_lookup Optionally create a {Topic} object without
94
- # verifying the topic resource exists on the Pub/Sub service. Calls made
95
- # on this object will raise errors if the topic resource does not exist.
96
- # Default is `false`.
97
- #
98
- # @return [Gcloud::Pubsub::Topic, nil] Returns `nil` if topic does not
99
- # exist. Will return a newly created{ Gcloud::Pubsub::Topic} if the
100
- # topic does not exist and `autocreate` is set to `true`.
101
- #
102
- # @example
103
- # require "gcloud"
104
- #
105
- # gcloud = Gcloud.new
106
- # pubsub = gcloud.pubsub
107
- # topic = pubsub.topic "existing-topic"
108
- #
109
- # @example By default `nil` will be returned if the topic does not exist.
110
- # require "gcloud"
111
- #
112
- # gcloud = Gcloud.new
113
- # pubsub = gcloud.pubsub
114
- # topic = pubsub.topic "non-existing-topic" #=> nil
115
- #
116
- # @example With the `autocreate` option set to `true`.
117
- # require "gcloud"
118
- #
119
- # gcloud = Gcloud.new
120
- # pubsub = gcloud.pubsub
121
- # topic = pubsub.topic "non-existing-topic", autocreate: true
122
- #
123
- # @example Create a topic in a different project with the `project` flag.
124
- # require "gcloud"
125
- #
126
- # gcloud = Gcloud.new
127
- # pubsub = gcloud.pubsub
128
- # topic = pubsub.topic "another-topic", project: "another-project"
129
- #
130
- # @example Skip the lookup against the service with `skip_lookup`:
131
- # require "gcloud"
132
- #
133
- # gcloud = Gcloud.new
134
- # pubsub = gcloud.pubsub
135
- # topic = pubsub.topic "another-topic", skip_lookup: true
136
- #
137
- def topic topic_name, autocreate: nil, project: nil, skip_lookup: nil
138
- ensure_service!
139
- options = { project: project }
140
- return Topic.new_lazy(topic_name, service, options) if skip_lookup
141
- grpc = service.get_topic topic_name
142
- Topic.from_grpc grpc, service
143
- rescue Gcloud::NotFoundError
144
- return create_topic(topic_name) if autocreate
145
- nil
146
- end
147
- alias_method :get_topic, :topic
148
- alias_method :find_topic, :topic
149
-
150
- ##
151
- # Creates a new topic.
152
- #
153
- # @param [String] topic_name Name of a topic.
154
- #
155
- # @return [Gcloud::Pubsub::Topic]
156
- #
157
- # @example
158
- # require "gcloud"
159
- #
160
- # gcloud = Gcloud.new
161
- # pubsub = gcloud.pubsub
162
- # topic = pubsub.create_topic "my-topic"
163
- #
164
- def create_topic topic_name
165
- ensure_service!
166
- grpc = service.create_topic topic_name
167
- Topic.from_grpc grpc, service
168
- end
169
- alias_method :new_topic, :create_topic
170
-
171
- ##
172
- # Retrieves a list of topics for the given project.
173
- #
174
- # @param [String] token The `token` value returned by the last call to
175
- # `topics`; indicates that this is a continuation of a call, and that
176
- # the system should return the next page of data.
177
- # @param [Integer] max Maximum number of topics to return.
178
- #
179
- # @return [Array<Gcloud::Pubsub::Topic>] (See
180
- # {Gcloud::Pubsub::Topic::List})
181
- #
182
- # @example
183
- # require "gcloud"
184
- #
185
- # gcloud = Gcloud.new
186
- # pubsub = gcloud.pubsub
187
- #
188
- # topics = pubsub.topics
189
- # topics.each do |topic|
190
- # puts topic.name
191
- # end
192
- #
193
- # @example Retrieve all topics: (See {Topic::List#all})
194
- # require "gcloud"
195
- #
196
- # gcloud = Gcloud.new
197
- # pubsub = gcloud.pubsub
198
- #
199
- # topics = pubsub.topics
200
- # topics.all do |topic|
201
- # puts topic.name
202
- # end
203
- #
204
- def topics token: nil, max: nil
205
- ensure_service!
206
- options = { token: token, max: max }
207
- grpc = service.list_topics options
208
- Topic::List.from_grpc grpc, service, max
209
- end
210
- alias_method :find_topics, :topics
211
- alias_method :list_topics, :topics
212
-
213
- ##
214
- # Publishes one or more messages to the given topic. The topic will be
215
- # created if the topic does previously not exist and the `autocreate`
216
- # option is provided.
217
- #
218
- # A note about auto-creating the topic: Any message published to a topic
219
- # without a subscription will be lost.
220
- #
221
- # @param [String] topic_name Name of a topic.
222
- # @param [String, File] data The message data.
223
- # @param [Hash] attributes Optional attributes for the message.
224
- # @option attributes [Boolean] :autocreate Flag to control whether the
225
- # provided topic will be created if it does not exist.
226
- # @yield [publisher] a block for publishing multiple messages in one
227
- # request
228
- # @yieldparam [Topic::Publisher] publisher the topic publisher object
229
- #
230
- # @return [Message, Array<Message>] Returns the published message when
231
- # called without a block, or an array of messages when called with a
232
- # block.
233
- #
234
- # @example
235
- # require "gcloud"
236
- #
237
- # gcloud = Gcloud.new
238
- # pubsub = gcloud.pubsub
239
- #
240
- # msg = pubsub.publish "my-topic", "new-message"
241
- #
242
- # @example A message can be published using a File object:
243
- # require "gcloud"
244
- #
245
- # gcloud = Gcloud.new
246
- # pubsub = gcloud.pubsub
247
- #
248
- # msg = pubsub.publish "my-topic", File.open("message.txt")
249
- #
250
- # @example Additionally, a message can be published with attributes:
251
- # require "gcloud"
252
- #
253
- # gcloud = Gcloud.new
254
- # pubsub = gcloud.pubsub
255
- #
256
- # msg = pubsub.publish "my-topic", "new-message", foo: :bar,
257
- # this: :that
258
- #
259
- # @example Multiple messages can be sent at the same time using a block:
260
- # require "gcloud"
261
- #
262
- # gcloud = Gcloud.new
263
- # pubsub = gcloud.pubsub
264
- #
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
269
- # end
270
- #
271
- # @example With `autocreate`:
272
- # require "gcloud"
273
- #
274
- # gcloud = Gcloud.new
275
- # pubsub = gcloud.pubsub
276
- #
277
- # msg = pubsub.publish "new-topic", "new-message", autocreate: true
278
- #
279
- def publish topic_name, data = nil, attributes = {}
280
- # Fix parameters
281
- if data.is_a?(::Hash) && attributes.empty?
282
- attributes = data
283
- data = nil
284
- end
285
- # extract autocreate option
286
- autocreate = attributes.delete :autocreate
287
- ensure_service!
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
292
- end
293
-
294
- ##
295
- # Creates a new {Subscription} object for the provided topic. The topic
296
- # will be created if the topic does previously not exist and the
297
- # `autocreate` option is provided.
298
- #
299
- # @param [String] topic_name Name of a topic.
300
- # @param [String] subscription_name Name of the new subscription. Must
301
- # start with a letter, and contain only letters ([A-Za-z]), numbers
302
- # ([0-9], dashes (-), underscores (_), periods (.), tildes (~), plus (+)
303
- # or percent signs (%). It must be between 3 and 255 characters in
304
- # length, and it must not start with "goog".
305
- # @param [Integer] deadline The maximum number of seconds after a
306
- # subscriber receives a message before the subscriber should acknowledge
307
- # the message.
308
- # @param [String] endpoint A URL locating the endpoint to which messages
309
- # should be pushed.
310
- # @param [String] autocreate Flag to control whether the topic will be
311
- # created if it does not exist.
312
- #
313
- # @return [Gcloud::Pubsub::Subscription]
314
- #
315
- # @example
316
- # require "gcloud"
317
- #
318
- # gcloud = Gcloud.new
319
- # pubsub = gcloud.pubsub
320
- #
321
- # sub = pubsub.subscribe "my-topic", "my-topic-sub"
322
- # puts sub.name # => "my-topic-sub"
323
- #
324
- # @example The name is optional, and will be generated if not given.
325
- # require "gcloud"
326
- #
327
- # gcloud = Gcloud.new
328
- # pubsub = gcloud.pubsub
329
- #
330
- # sub = pubsub.subscribe "my-topic"
331
- # puts sub.name # => "generated-sub-name"
332
- #
333
- # @example Wait 2 minutes for acknowledgement and push all to an endpoint:
334
- # require "gcloud"
335
- #
336
- # gcloud = Gcloud.new
337
- # pubsub = gcloud.pubsub
338
- #
339
- # sub = pubsub.subscribe "my-topic", "my-topic-sub",
340
- # deadline: 120,
341
- # endpoint: "https://example.com/push"
342
- #
343
- # @example With `autocreate`:
344
- # require "gcloud"
345
- #
346
- # gcloud = Gcloud.new
347
- # pubsub = gcloud.pubsub
348
- #
349
- # sub = pubsub.subscribe "new-topic", "new-topic-sub", autocreate: true
350
- #
351
- def subscribe topic_name, subscription_name, deadline: nil, endpoint: nil,
352
- autocreate: nil
353
- ensure_service!
354
- options = { deadline: deadline, endpoint: endpoint }
355
- grpc = service.create_subscription topic_name,
356
- subscription_name, options
357
- Subscription.from_grpc grpc, service
358
- rescue Gcloud::NotFoundError => e
359
- if autocreate
360
- create_topic topic_name
361
- return subscribe(topic_name, subscription_name,
362
- deadline: deadline, endpoint: endpoint,
363
- autocreate: false)
364
- end
365
- raise e
366
- end
367
- alias_method :create_subscription, :subscribe
368
- alias_method :new_subscription, :subscribe
369
-
370
- ##
371
- # Retrieves subscription by name.
372
- #
373
- # @param [String] subscription_name Name of a subscription.
374
- # @param [String] project If the subscription belongs to a project other
375
- # than the one currently connected to, the alternate project ID can be
376
- # specified here.
377
- # @param [Boolean] skip_lookup Optionally create a {Subscription} object
378
- # without verifying the subscription resource exists on the Pub/Sub
379
- # service. Calls made on this object will raise errors if the service
380
- # resource does not exist. Default is `false`.
381
- #
382
- # @return [Gcloud::Pubsub::Subscription, nil] Returns `nil` if the
383
- # subscription does not exist
384
- #
385
- # @example
386
- # require "gcloud"
387
- #
388
- # gcloud = Gcloud.new
389
- # pubsub = gcloud.pubsub
390
- #
391
- # subscription = pubsub.subscription "my-sub"
392
- # puts subscription.name
393
- #
394
- # @example Skip the lookup against the service with `skip_lookup`:
395
- # require "gcloud"
396
- #
397
- # gcloud = Gcloud.new
398
- # pubsub = gcloud.pubsub
399
- #
400
- # # No API call is made to retrieve the subscription information.
401
- # subscription = pubsub.subscription "my-sub", skip_lookup: true
402
- # puts subscription.name
403
- #
404
- def subscription subscription_name, project: nil, skip_lookup: nil
405
- ensure_service!
406
- options = { project: project }
407
- if skip_lookup
408
- return Subscription.new_lazy subscription_name, service, options
409
- end
410
- grpc = service.get_subscription subscription_name
411
- Subscription.from_grpc grpc, service
412
- rescue Gcloud::NotFoundError
413
- nil
414
- end
415
- alias_method :get_subscription, :subscription
416
- alias_method :find_subscription, :subscription
417
-
418
- ##
419
- # Retrieves a list of subscriptions for the given project.
420
- #
421
- # @param [String] token A previously-returned page token representing part
422
- # of the larger set of results to view.
423
- # @param [Integer] max Maximum number of subscriptions to return.
424
- #
425
- # @return [Array<Gcloud::Pubsub::Subscription>] (See
426
- # {Gcloud::Pubsub::Subscription::List})
427
- #
428
- # @example
429
- # require "gcloud"
430
- #
431
- # gcloud = Gcloud.new
432
- # pubsub = gcloud.pubsub
433
- #
434
- # subscriptions = pubsub.subscriptions
435
- # subscriptions.each do |subscription|
436
- # puts subscription.name
437
- # end
438
- #
439
- # @example Retrieve all subscriptions: (See {Subscription::List#all})
440
- # require "gcloud"
441
- #
442
- # gcloud = Gcloud.new
443
- # pubsub = gcloud.pubsub
444
- #
445
- # subscriptions = pubsub.subscriptions
446
- # subscriptions.all do |subscription|
447
- # puts subscription.name
448
- # end
449
- #
450
- def subscriptions token: nil, max: nil
451
- ensure_service!
452
- options = { token: token, max: max }
453
- grpc = service.list_subscriptions options
454
- Subscription::List.from_grpc grpc, service, max
455
- end
456
- alias_method :find_subscriptions, :subscriptions
457
- alias_method :list_subscriptions, :subscriptions
458
-
459
- protected
460
-
461
- ##
462
- # @private Raise an error unless an active connection to the service is
463
- # available.
464
- def ensure_service!
465
- fail "Must have active connection to service" unless service
466
- end
467
-
468
- ##
469
- # Call the publish API with arrays of data data and attrs.
470
- def publish_batch_messages topic_name, batch, autocreate = false
471
- grpc = service.publish topic_name, batch.messages
472
- batch.to_gcloud_messages Array(grpc.message_ids)
473
- rescue Gcloud::NotFoundError => e
474
- if autocreate
475
- create_topic topic_name
476
- return publish_batch_messages topic_name, batch, false
477
- end
478
- raise e
479
- end
480
- end
481
- end
482
- end