gcloud 0.12.2 → 0.20.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 (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,167 +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/datastore/credentials"
17
- require "google/datastore/v1beta3/datastore_services"
18
- require "gcloud/backoff"
19
-
20
- module Gcloud
21
- module Datastore
22
- ##
23
- # @private Represents the gRPC Datastore service, including all the API
24
- # methods.
25
- class Service
26
- attr_accessor :project, :credentials, :host, :retries, :timeout
27
-
28
- ##
29
- # Creates a new Service instance.
30
- def initialize project, credentials, host: nil, retries: nil, timeout: nil
31
- @project = project
32
- @credentials = credentials
33
- @host = host || "datastore.googleapis.com"
34
- @retries = retries
35
- @timeout = timeout
36
- end
37
-
38
- def creds
39
- return credentials if insecure?
40
- GRPC::Core::ChannelCredentials.new.compose \
41
- GRPC::Core::CallCredentials.new credentials.client.updater_proc
42
- end
43
-
44
- def datastore
45
- return mocked_datastore if mocked_datastore
46
- @datastore ||= Google::Datastore::V1beta3::Datastore::Stub.new(
47
- host, creds, timeout: timeout)
48
- end
49
- attr_accessor :mocked_datastore
50
-
51
- def insecure?
52
- credentials == :this_channel_is_insecure
53
- end
54
-
55
- ##
56
- # Allocate IDs for incomplete keys.
57
- # (This is useful for referencing an entity before it is inserted.)
58
- def allocate_ids *incomplete_keys
59
- allocate_req = Google::Datastore::V1beta3::AllocateIdsRequest.new(
60
- project_id: project,
61
- keys: incomplete_keys
62
- )
63
-
64
- execute { datastore.allocate_ids allocate_req }
65
- end
66
-
67
- ##
68
- # Look up entities by keys.
69
- def lookup *keys, consistency: nil, transaction: nil
70
- lookup_req = Google::Datastore::V1beta3::LookupRequest.new(
71
- project_id: project,
72
- keys: keys
73
- )
74
- lookup_req.read_options = generate_read_options consistency, transaction
75
-
76
- execute { datastore.lookup lookup_req }
77
- end
78
-
79
- # Query for entities.
80
- def run_query query, namespace = nil, consistency: nil, transaction: nil
81
- run_req = Google::Datastore::V1beta3::RunQueryRequest.new(
82
- project_id: project)
83
- if query.is_a? Google::Datastore::V1beta3::Query
84
- run_req["query"] = query
85
- elsif query.is_a? Google::Datastore::V1beta3::GqlQuery
86
- run_req["gql_query"] = query
87
- else
88
- fail ArgumentError, "Unable to query with a #{query.class} object."
89
- end
90
- run_req.read_options = generate_read_options consistency, transaction
91
-
92
- run_req.partition_id = Google::Datastore::V1beta3::PartitionId.new(
93
- namespace_id: namespace) if namespace
94
-
95
- execute { datastore.run_query run_req }
96
- end
97
-
98
- ##
99
- # Begin a new transaction.
100
- def begin_transaction
101
- tx_req = Google::Datastore::V1beta3::BeginTransactionRequest.new(
102
- project_id: project
103
- )
104
-
105
- execute { datastore.begin_transaction tx_req }
106
- end
107
-
108
- ##
109
- # Commit a transaction, optionally creating, deleting or modifying
110
- # some entities.
111
- def commit mutations, transaction: nil
112
- commit_req = Google::Datastore::V1beta3::CommitRequest.new(
113
- project_id: project,
114
- mode: :NON_TRANSACTIONAL,
115
- mutations: mutations
116
- )
117
- if transaction
118
- commit_req.mode = :TRANSACTIONAL
119
- commit_req.transaction = transaction
120
- end
121
-
122
- execute { datastore.commit commit_req }
123
- end
124
-
125
- ##
126
- # Roll back a transaction.
127
- def rollback transaction
128
- rb_req = Google::Datastore::V1beta3::RollbackRequest.new(
129
- project_id: project,
130
- transaction: transaction
131
- )
132
-
133
- execute { datastore.rollback rb_req }
134
- end
135
-
136
- def inspect
137
- "#{self.class}(#{@project})"
138
- end
139
-
140
- ##
141
- # Performs backoff and error handling
142
- def execute
143
- Gcloud::Backoff.new(retries: retries).execute_grpc do
144
- yield
145
- end
146
- rescue GRPC::BadStatus => e
147
- raise Gcloud::Error.from_error(e)
148
- end
149
-
150
- protected
151
-
152
- def generate_read_options consistency, transaction
153
- if consistency == :eventual
154
- return Google::Datastore::V1beta3::ReadOptions.new(
155
- read_consistency: :EVENTUAL)
156
- elsif consistency == :strong
157
- return Google::Datastore::V1beta3::ReadOptions.new(
158
- read_consistency: :STRONG)
159
- elsif transaction
160
- return Google::Datastore::V1beta3::ReadOptions.new(
161
- transaction: transaction)
162
- end
163
- nil
164
- end
165
- end
166
- end
167
- end
@@ -1,362 +0,0 @@
1
- # Copyright 2014 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
- module Gcloud
17
- module Datastore
18
- ##
19
- # # Transaction
20
- #
21
- # Special Connection instance for running transactions.
22
- #
23
- # See {Gcloud::Datastore::Dataset#transaction}
24
- #
25
- # @see https://cloud.google.com/datastore/docs/concepts/transactions
26
- # Transactions
27
- #
28
- # @example Transactional update:
29
- # def transfer_funds from_key, to_key, amount
30
- # datastore.transaction do |tx|
31
- # from = tx.find from_key
32
- # from["balance"] -= amount
33
- # to = tx.find to_key
34
- # to["balance"] += amount
35
- # tx.save from, to
36
- # end
37
- # end
38
- #
39
- # @example Retry logic using the transactional update example above:
40
- # (1..5).each do |i|
41
- # begin
42
- # return transfer_funds from_key, to_key, amount
43
- # rescue Gcloud::Error => e
44
- # raise e if i == 5
45
- # end
46
- # end
47
- #
48
- # @example Transactional read:
49
- # task_list_key = datastore.key "TaskList", "default"
50
- # datastore.transaction do |tx|
51
- # task_list = tx.find task_list_key
52
- # query = tx.query("Task").ancestor(task_list)
53
- # tasks_in_list = tx.run query
54
- # end
55
- #
56
- class Transaction < Dataset
57
- attr_reader :id
58
-
59
- ##
60
- # @private Creates a new Transaction instance.
61
- # Takes a Connection and Service instead of project and Credentials.
62
- def initialize service
63
- @service = service
64
- reset!
65
- start
66
- end
67
-
68
- ##
69
- # Persist entities in a transaction.
70
- #
71
- # @example Transactional get or create:
72
- # task_key = datastore.key "Task", "sampleTask"
73
- #
74
- # task = nil
75
- # datastore.transaction do |tx|
76
- # task = tx.find task_key
77
- # if task.nil?
78
- # task = datastore.entity task_key do |t|
79
- # t["type"] = "Personal"
80
- # t["done"] = false
81
- # t["priority"] = 4
82
- # t["description"] = "Learn Cloud Datastore"
83
- # end
84
- # tx.save task
85
- # end
86
- # end
87
- #
88
- def save *entities
89
- @commit.save(*entities)
90
- # Do not save yet
91
- entities
92
- end
93
- alias_method :upsert, :save
94
-
95
- ##
96
- # Insert entities in a transaction. An InvalidArgumentError will raised if
97
- # the entities cannot be inserted.
98
- #
99
- # @example Transactional insert:
100
- # task_key = datastore.key "Task", "sampleTask"
101
- #
102
- # task = nil
103
- # datastore.transaction do |tx|
104
- # task = tx.find task_key
105
- # if task.nil?
106
- # task = datastore.entity task_key do |t|
107
- # t["type"] = "Personal"
108
- # t["done"] = false
109
- # t["priority"] = 4
110
- # t["description"] = "Learn Cloud Datastore"
111
- # end
112
- # tx.insert task
113
- # end
114
- # end
115
- #
116
- def insert *entities
117
- @commit.insert(*entities)
118
- # Do not insert yet
119
- entities
120
- end
121
-
122
- ##
123
- # Update entities in a transaction. An InvalidArgumentError will raised if
124
- # the entities cannot be updated.
125
- #
126
- # @example Transactional update:
127
- # task_key = datastore.key "Task", "sampleTask"
128
- #
129
- # task = nil
130
- # datastore.transaction do |tx|
131
- # task = tx.find task_key
132
- # if task
133
- # task["done"] = true
134
- # tx.update task
135
- # end
136
- # end
137
- #
138
- def update *entities
139
- @commit.update(*entities)
140
- # Do not update yet
141
- entities
142
- end
143
-
144
- ##
145
- # Remove entities in a transaction.
146
- #
147
- # @example
148
- # datastore.transaction do |tx|
149
- # if tx.find(task_list.key).nil?
150
- # tx.delete task1, task2
151
- # end
152
- # end
153
- #
154
- def delete *entities_or_keys
155
- @commit.delete(*entities_or_keys)
156
- # Do not delete yet
157
- true
158
- end
159
-
160
- ##
161
- # Retrieve an entity by providing key information. The lookup is run
162
- # within the transaction.
163
- #
164
- # @param [Key, String] key_or_kind A Key object or `kind` string value.
165
- #
166
- # @return [Gcloud::Datastore::Entity, nil]
167
- #
168
- # @example Finding an entity with a key:
169
- # task_key = datastore.key "Task", "sampleTask"
170
- # task = datastore.find task_key
171
- #
172
- # @example Finding an entity with a `kind` and `id`/`name`:
173
- # task = datastore.find "Task", "sampleTask"
174
- #
175
- def find key_or_kind, id_or_name = nil
176
- key = key_or_kind
177
- unless key.is_a? Gcloud::Datastore::Key
178
- key = Key.new key_or_kind, id_or_name
179
- end
180
- find_all(key).first
181
- end
182
- alias_method :get, :find
183
-
184
- ##
185
- # Retrieve the entities for the provided keys. The lookup is run within
186
- # the transaction.
187
- #
188
- # @param [Key] keys One or more Key objects to find records for.
189
- #
190
- # @return [Gcloud::Datastore::Dataset::LookupResults]
191
- #
192
- # @example
193
- # gcloud = Gcloud.new
194
- # datastore = gcloud.datastore
195
- # task_key1 = datastore.key "Task", 123456
196
- # task_key2 = datastore.key "Task", 987654
197
- # tasks = datastore.find_all task_key1, task_key2
198
- #
199
- def find_all *keys
200
- ensure_service!
201
- lookup_res = service.lookup(*Array(keys).flatten.map(&:to_grpc),
202
- transaction: @id)
203
- LookupResults.from_grpc lookup_res, service, nil, @id
204
- end
205
- alias_method :lookup, :find_all
206
-
207
- ##
208
- # Retrieve entities specified by a Query. The query is run within the
209
- # transaction.
210
- #
211
- # @param [Query] query The Query object with the search criteria.
212
- # @param [String] namespace The namespace the query is to run within.
213
- #
214
- # @return [Gcloud::Datastore::Dataset::QueryResults]
215
- #
216
- # @example
217
- # query = datastore.query("Task").
218
- # where("done", "=", false)
219
- # datastore.transaction do |tx|
220
- # tasks = tx.run query
221
- # end
222
- #
223
- # @example Run the query within a namespace with the `namespace` option:
224
- # query = Gcloud::Datastore::Query.new.kind("Task").
225
- # where("done", "=", false)
226
- # datastore.transaction do |tx|
227
- # tasks = tx.run query, namespace: "ns~todo-project"
228
- # end
229
- #
230
- def run query, namespace: nil
231
- ensure_service!
232
- unless query.is_a?(Query) || query.is_a?(GqlQuery)
233
- fail ArgumentError, "Cannot run a #{query.class} object."
234
- end
235
- query_res = service.run_query query.to_grpc, namespace,
236
- transaction: @id
237
- QueryResults.from_grpc query_res, service, namespace, query.to_grpc.dup
238
- end
239
- alias_method :run_query, :run
240
-
241
- ##
242
- # Begins a transaction.
243
- # This method is run when a new Transaction is created.
244
- def start
245
- fail TransactionError, "Transaction already opened." unless @id.nil?
246
-
247
- ensure_service!
248
- tx_res = service.begin_transaction
249
- @id = tx_res.transaction
250
- end
251
- alias_method :begin_transaction, :start
252
-
253
- ##
254
- # Commits a transaction.
255
- #
256
- # @yield [commit] an optional block for making changes
257
- # @yieldparam [Commit] commit The object that changes are made on
258
- #
259
- # @example
260
- # require "gcloud"
261
- #
262
- # gcloud = Gcloud.new
263
- # datastore = gcloud.datastore
264
- #
265
- # task = datastore.entity "Task" do |t|
266
- # t["type"] = "Personal"
267
- # t["done"] = false
268
- # t["priority"] = 4
269
- # t["description"] = "Learn Cloud Datastore"
270
- # end
271
- #
272
- # tx = datastore.transaction
273
- # begin
274
- # if tx.find(task.key).nil?
275
- # tx.save task
276
- # end
277
- # tx.commit
278
- # rescue
279
- # tx.rollback
280
- # end
281
- #
282
- # @example Commit can be passed a block, same as {Dataset#commit}:
283
- # require "gcloud"
284
- #
285
- # gcloud = Gcloud.new
286
- # datastore = gcloud.datastore
287
- #
288
- # tx = datastore.transaction
289
- # begin
290
- # tx.commit do |c|
291
- # c.save task1, task2
292
- # c.delete entity1, entity2
293
- # end
294
- # rescue
295
- # tx.rollback
296
- # end
297
- #
298
- def commit
299
- fail TransactionError,
300
- "Cannot commit when not in a transaction." if @id.nil?
301
-
302
- yield @commit if block_given?
303
-
304
- ensure_service!
305
-
306
- commit_res = service.commit @commit.mutations, transaction: @id
307
- entities = @commit.entities
308
- returned_keys = commit_res.mutation_results.map(&:key)
309
- returned_keys.each_with_index do |key, index|
310
- next if entities[index].nil?
311
- entities[index].key = Key.from_grpc(key) unless key.nil?
312
- end
313
- # Make sure all entity keys are frozen so all show as persisted
314
- entities.each { |e| e.key.freeze unless e.persisted? }
315
- true
316
- end
317
-
318
- ##
319
- # Rolls a transaction back.
320
- #
321
- # @example
322
- # require "gcloud"
323
- #
324
- # gcloud = Gcloud.new
325
- # datastore = gcloud.datastore
326
- #
327
- # task = datastore.entity "Task" do |t|
328
- # t["type"] = "Personal"
329
- # t["done"] = false
330
- # t["priority"] = 4
331
- # t["description"] = "Learn Cloud Datastore"
332
- # end
333
- #
334
- # tx = datastore.transaction
335
- # begin
336
- # if tx.find(task.key).nil?
337
- # tx.save task
338
- # end
339
- # tx.commit
340
- # rescue
341
- # tx.rollback
342
- # end
343
- def rollback
344
- if @id.nil?
345
- fail TransactionError, "Cannot rollback when not in a transaction."
346
- end
347
-
348
- ensure_service!
349
- service.rollback @id
350
- true
351
- end
352
-
353
- ##
354
- # Reset the transaction.
355
- # {Transaction#start} must be called afterwards.
356
- def reset!
357
- @id = nil
358
- @commit = Commit.new
359
- end
360
- end
361
- end
362
- end