google-cloud-spanner 0.21.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/lib/google-cloud-spanner.rb +106 -0
  3. data/lib/google/cloud/spanner.rb +382 -0
  4. data/lib/google/cloud/spanner/admin/database/v1.rb +17 -0
  5. data/lib/google/cloud/spanner/admin/database/v1/database_admin_client.rb +703 -0
  6. data/lib/google/cloud/spanner/admin/database/v1/database_admin_client_config.json +73 -0
  7. data/lib/google/cloud/spanner/admin/database/v1/doc/google/iam/v1/policy.rb +139 -0
  8. data/lib/google/cloud/spanner/admin/database/v1/doc/google/protobuf/any.rb +114 -0
  9. data/lib/google/cloud/spanner/admin/database/v1/doc/google/rpc/status.rb +83 -0
  10. data/lib/google/cloud/spanner/admin/database/v1/doc/google/spanner/admin/database/v1/spanner_database_admin.rb +188 -0
  11. data/lib/google/cloud/spanner/admin/instance/v1.rb +17 -0
  12. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/iam/v1/policy.rb +139 -0
  13. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/any.rb +114 -0
  14. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/protobuf/field_mask.rb +223 -0
  15. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/rpc/status.rb +83 -0
  16. data/lib/google/cloud/spanner/admin/instance/v1/doc/google/spanner/admin/instance/v1/spanner_instance_admin.rb +268 -0
  17. data/lib/google/cloud/spanner/admin/instance/v1/instance_admin_client.rb +868 -0
  18. data/lib/google/cloud/spanner/admin/instance/v1/instance_admin_client_config.json +78 -0
  19. data/lib/google/cloud/spanner/client.rb +1034 -0
  20. data/lib/google/cloud/spanner/commit.rb +351 -0
  21. data/lib/google/cloud/spanner/convert.rb +311 -0
  22. data/lib/google/cloud/spanner/credentials.rb +32 -0
  23. data/lib/google/cloud/spanner/data.rb +199 -0
  24. data/lib/google/cloud/spanner/database.rb +377 -0
  25. data/lib/google/cloud/spanner/database/job.rb +179 -0
  26. data/lib/google/cloud/spanner/database/list.rb +171 -0
  27. data/lib/google/cloud/spanner/errors.rb +73 -0
  28. data/lib/google/cloud/spanner/fields.rb +252 -0
  29. data/lib/google/cloud/spanner/instance.rb +472 -0
  30. data/lib/google/cloud/spanner/instance/config.rb +99 -0
  31. data/lib/google/cloud/spanner/instance/config/list.rb +171 -0
  32. data/lib/google/cloud/spanner/instance/job.rb +197 -0
  33. data/lib/google/cloud/spanner/instance/list.rb +167 -0
  34. data/lib/google/cloud/spanner/policy.rb +201 -0
  35. data/lib/google/cloud/spanner/pool.rb +279 -0
  36. data/lib/google/cloud/spanner/project.rb +480 -0
  37. data/lib/google/cloud/spanner/range.rb +99 -0
  38. data/lib/google/cloud/spanner/results.rb +280 -0
  39. data/lib/google/cloud/spanner/service.rb +458 -0
  40. data/lib/google/cloud/spanner/session.rb +565 -0
  41. data/lib/google/cloud/spanner/snapshot.rb +260 -0
  42. data/lib/google/cloud/spanner/transaction.rb +533 -0
  43. data/lib/google/cloud/spanner/v1.rb +17 -0
  44. data/lib/google/cloud/spanner/v1/doc/google/protobuf/duration.rb +77 -0
  45. data/lib/google/cloud/spanner/v1/doc/google/protobuf/struct.rb +73 -0
  46. data/lib/google/cloud/spanner/v1/doc/google/protobuf/timestamp.rb +81 -0
  47. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/keys.rb +148 -0
  48. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/mutation.rb +80 -0
  49. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/query_plan.rb +120 -0
  50. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/result_set.rb +175 -0
  51. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/spanner.rb +206 -0
  52. data/lib/google/cloud/spanner/v1/doc/google/spanner/v1/transaction.rb +351 -0
  53. data/lib/google/cloud/spanner/v1/spanner_client.rb +850 -0
  54. data/lib/google/cloud/spanner/v1/spanner_client_config.json +78 -0
  55. data/lib/google/cloud/spanner/version.rb +22 -0
  56. data/lib/google/spanner/admin/database/v1/spanner_database_admin_pb.rb +85 -0
  57. data/lib/google/spanner/admin/database/v1/spanner_database_admin_services_pb.rb +95 -0
  58. data/lib/google/spanner/admin/instance/v1/spanner_instance_admin_pb.rb +106 -0
  59. data/lib/google/spanner/admin/instance/v1/spanner_instance_admin_services_pb.rb +180 -0
  60. data/lib/google/spanner/v1/keys_pb.rb +33 -0
  61. data/lib/google/spanner/v1/mutation_pb.rb +38 -0
  62. data/lib/google/spanner/v1/query_plan_pb.rb +47 -0
  63. data/lib/google/spanner/v1/result_set_pb.rb +43 -0
  64. data/lib/google/spanner/v1/spanner_pb.rb +90 -0
  65. data/lib/google/spanner/v1/spanner_services_pb.rb +131 -0
  66. data/lib/google/spanner/v1/transaction_pb.rb +51 -0
  67. data/lib/google/spanner/v1/type_pb.rb +43 -0
  68. metadata +309 -0
@@ -0,0 +1,472 @@
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 "google/cloud/spanner/instance/job"
17
+ require "google/cloud/spanner/instance/list"
18
+ require "google/cloud/spanner/instance/config"
19
+ require "google/cloud/spanner/database"
20
+ require "google/cloud/spanner/policy"
21
+
22
+ module Google
23
+ module Cloud
24
+ module Spanner
25
+ ##
26
+ # # Instance
27
+ #
28
+ # Represents a Cloud Spanner instance. Instances are dedicated Cloud
29
+ # Spanner serving and storage resources to be used by Cloud Spanner
30
+ # databases. Instances offer isolation: problems with databases in one
31
+ # instance will not affect other instances. However, within an instance
32
+ # databases can affect each other. For example, if one database in an
33
+ # instance receives a lot of requests and consumes most of the instance
34
+ # resources, fewer resources are available for other databases in that
35
+ # instance, and their performance may suffer.
36
+ #
37
+ # See {Google::Cloud::Spanner::Project#instances},
38
+ # {Google::Cloud::Spanner::Project#instance}, and
39
+ # {Google::Cloud::Spanner::Project#create_instance}.
40
+ #
41
+ # @example
42
+ # require "google/cloud/spanner"
43
+ #
44
+ # spanner = Google::Cloud::Spanner.new
45
+ #
46
+ # job = spanner.create_instance "my-new-instance",
47
+ # name: "My New Instance",
48
+ # config: "regional-us-central1",
49
+ # nodes: 5,
50
+ # labels: { production: :env }
51
+ #
52
+ # job.done? #=> false
53
+ # job.reload! # API call
54
+ # job.done? #=> true
55
+ #
56
+ # instance = spanner.instance "my-new-instance"
57
+ #
58
+ class Instance
59
+ ##
60
+ # @private The gRPC Service object.
61
+ attr_accessor :service
62
+
63
+ # @private Creates a new Instance instance.
64
+ def initialize grpc, service
65
+ @grpc = grpc
66
+ @service = service
67
+ end
68
+
69
+ # The unique identifier for the project.
70
+ # @return [String]
71
+ def project_id
72
+ Admin::Instance::V1::InstanceAdminClient
73
+ .match_project_from_instance_name @grpc.name
74
+ end
75
+
76
+ # The unique identifier for the instance.
77
+ # @return [String]
78
+ def instance_id
79
+ Admin::Instance::V1::InstanceAdminClient
80
+ .match_instance_from_instance_name @grpc.name
81
+ end
82
+
83
+ ##
84
+ # The full path for the instance resource. Values are of the form
85
+ # `projects/<project_id>/instances/<instance_id>`.
86
+ # @return [String]
87
+ def path
88
+ @grpc.name
89
+ end
90
+
91
+ ##
92
+ # The descriptive name for this instance as it appears in UIs. Must be
93
+ # unique per project and between 4 and 30 characters in length.
94
+ # @return [String]
95
+ def name
96
+ @grpc.display_name
97
+ end
98
+ alias_method :display_name, :name
99
+
100
+ ##
101
+ # The instance configuration resource.
102
+ # @return [Instance::Config]
103
+ def config
104
+ ensure_service!
105
+ config_grpc = service.get_instance_config @grpc.config
106
+ Instance::Config.from_grpc config_grpc
107
+ rescue Google::Cloud::NotFoundError
108
+ @grpc.config
109
+ end
110
+
111
+ ##
112
+ # Updates the descriptive name for this instance as it appears in UIs.
113
+ # @param display_name [String] The descriptive name for this instance.
114
+ def name= display_name
115
+ @grpc.display_name = display_name
116
+ end
117
+ alias_method :display_name=, :name=
118
+
119
+ ##
120
+ # The number of nodes allocated to this instance.
121
+ # @return [Integer]
122
+ def nodes
123
+ @grpc.node_count
124
+ end
125
+ alias_method :node_count, :nodes
126
+
127
+ ##
128
+ # Updates the number of nodes allocated to this instance.
129
+ # @param nodes [Integer] The number of nodes allocated to this instance.
130
+ def nodes= nodes
131
+ @grpc.node_count = nodes
132
+ end
133
+ alias_method :node_count=, :nodes=
134
+
135
+ ##
136
+ # The current instance state. Possible values are `:CREATING` and
137
+ # `:READY`.
138
+ # @return [Symbol]
139
+ def state
140
+ @grpc.state
141
+ end
142
+
143
+ ##
144
+ # The instance is still being created. Resources may not be available
145
+ # yet, and operations such as database creation may not work.
146
+ # @return [Boolean]
147
+ def creating?
148
+ state == :CREATING
149
+ end
150
+
151
+ ##
152
+ # The instance is fully created and ready to do work such as creating
153
+ # databases.
154
+ # @return [Boolean]
155
+ def ready?
156
+ state == :READY
157
+ end
158
+
159
+ ##
160
+ # Cloud Labels are a flexible and lightweight mechanism for organizing
161
+ # cloud resources into groups that reflect a customer's organizational
162
+ # needs and deployment strategies. Cloud Labels can be used to filter
163
+ # collections of resources. They can be used to control how resource
164
+ # metrics are aggregated. And they can be used as arguments to policy
165
+ # management rules (e.g. route, firewall, load balancing, etc.).
166
+ #
167
+ # * Label keys must be between 1 and 63 characters long and must conform
168
+ # to the following regular expression: `[a-z]([-a-z0-9]*[a-z0-9])?`.
169
+ # * Label values must be between 0 and 63 characters long and must
170
+ # conform to the regular expression `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
171
+ # * No more than 64 labels can be associated with a given resource.
172
+ #
173
+ # @return [Hash{String=>String}] The label keys and values in a hash.
174
+ #
175
+ def labels
176
+ @grpc.labels
177
+ end
178
+
179
+ ##
180
+ # Updates the Cloud Labels.
181
+ # @param labels [Hash{String=>String}] The Cloud Labels.
182
+ def labels= labels
183
+ @grpc.labels = Google::Protobuf::Map.new(
184
+ :string, :string,
185
+ Hash[labels.map { |k, v| [String(k), String(v)] }])
186
+ end
187
+
188
+ def save
189
+ job_grpc = service.update_instance @grpc
190
+ Instance::Job.from_grpc job_grpc, service
191
+ end
192
+ alias_method :update, :save
193
+
194
+ ##
195
+ # Permanently deletes the instance.
196
+ #
197
+ # Immediately upon completion of the request:
198
+ #
199
+ # * Billing ceases for all of the instance's reserved resources.
200
+ #
201
+ # Soon afterward:
202
+ #
203
+ # * The instance and all of its databases immediately and irrevocably
204
+ # disappear from the API. All data in the databases is permanently
205
+ # deleted.
206
+ #
207
+ # @return [Boolean] Returns `true` if the instance was deleted.
208
+ #
209
+ # @example
210
+ # require "google/cloud/spanner"
211
+ #
212
+ # spanner = Google::Cloud::Spanner.new
213
+ #
214
+ # instance = spanner.instance "my-instance"
215
+ # instance.delete
216
+ #
217
+ def delete
218
+ ensure_service!
219
+ service.delete_instance path
220
+ true
221
+ end
222
+
223
+ ##
224
+ # Retrieves the list of databases for the given instance.
225
+ #
226
+ # @param [String] token The `token` value returned by the last call to
227
+ # `databases`; indicates that this is a continuation of a call,
228
+ # and that the system should return the next page of data.
229
+ # @param [Integer] max Maximum number of databases to return.
230
+ #
231
+ # @return [Array<Google::Cloud::Spanner::Database>] (See
232
+ # {Google::Cloud::Spanner::Database::List})
233
+ #
234
+ # @example
235
+ # require "google/cloud/spanner"
236
+ #
237
+ # spanner = Google::Cloud::Spanner.new
238
+ #
239
+ # instance = spanner.instance "my-instance"
240
+ # databases = instance.databases
241
+ # databases.each do |database|
242
+ # puts database.database_id
243
+ # end
244
+ #
245
+ # @example Retrieve all: (See {Instance::Config::List::List#all})
246
+ # require "google/cloud/spanner"
247
+ #
248
+ # spanner = Google::Cloud::Spanner.new
249
+ #
250
+ # instance = spanner.instance "my-instance"
251
+ # databases = instance.databases
252
+ # databases.all do |database|
253
+ # puts database.database_id
254
+ # end
255
+ #
256
+ def databases token: nil, max: nil
257
+ ensure_service!
258
+ grpc = service.list_databases instance_id, token: token, max: max
259
+ Database::List.from_grpc grpc, service, instance_id, max
260
+ end
261
+
262
+ ##
263
+ # Retrieves a database belonging to the instance by identifier.
264
+ #
265
+ # @param [String] database_id The unique identifier for the database.
266
+ #
267
+ # @return [Google::Cloud::Spanner::Database, nil] Returns `nil`
268
+ # if database does not exist.
269
+ #
270
+ # @example
271
+ # require "google/cloud/spanner"
272
+ #
273
+ # spanner = Google::Cloud::Spanner.new
274
+ # instance = spanner.instance "my-instance"
275
+ # database = instance.database "my-database"
276
+ #
277
+ # @example Will return `nil` if instance does not exist.
278
+ # require "google/cloud/spanner"
279
+ #
280
+ # spanner = Google::Cloud::Spanner.new
281
+ # instance = spanner.instance "my-instance"
282
+ # database = instance.database "my-database" #=> nil
283
+ #
284
+ def database database_id
285
+ ensure_service!
286
+ grpc = service.get_database instance_id, database_id
287
+ Database.from_grpc grpc, service
288
+ rescue Google::Cloud::NotFoundError
289
+ nil
290
+ end
291
+
292
+ ##
293
+ # Creates a database and starts preparing it to begin serving.
294
+ #
295
+ # See {Database::Job}.
296
+ #
297
+ # @param [String] database_id The unique identifier for the database,
298
+ # which cannot be changed after the database is created. Values are of
299
+ # the form `[a-z][a-z0-9_\-]*[a-z0-9]` and must be between 2 and 30
300
+ # characters in length. Required.
301
+ # @param [Array<String>] statements DDL statements to run inside the
302
+ # newly created database. Statements can create tables, indexes, etc.
303
+ # These statements execute atomically with the creation of the
304
+ # database: if there is an error in any statement, the database is not
305
+ # created. Optional.
306
+ #
307
+ # @return [Database::Job] The job representing the long-running,
308
+ # asynchronous processing of a database create operation.
309
+ #
310
+ # @example
311
+ # require "google/cloud/spanner"
312
+ #
313
+ # spanner = Google::Cloud::Spanner.new
314
+ #
315
+ # instance = spanner.instance "my-instance"
316
+ # job = instance.create_database "my-new-database"
317
+ #
318
+ # job.done? #=> false
319
+ # job.reload! # API call
320
+ # job.done? #=> true
321
+ # database = job.database
322
+ #
323
+ def create_database database_id, statements: []
324
+ grpc = service.create_database instance_id, database_id,
325
+ statements: statements
326
+ Database::Job.from_grpc grpc, service
327
+ end
328
+
329
+ ##
330
+ # Gets the [Cloud IAM](https://cloud.google.com/iam/) access control
331
+ # policy for this instance.
332
+ #
333
+ # @see https://cloud.google.com/spanner/reference/rpc/google.iam.v1#google.iam.v1.Policy
334
+ # google.iam.v1.IAMPolicy
335
+ #
336
+ # @yield [policy] A block for updating the policy. The latest policy
337
+ # will be read from the Spanner service and passed to the block. After
338
+ # the block completes, the modified policy will be written to the
339
+ # service.
340
+ # @yieldparam [Policy] policy the current Cloud IAM Policy for this
341
+ # instance
342
+ #
343
+ # @return [Policy] The current Cloud IAM Policy for this instance.
344
+ #
345
+ # @example
346
+ # require "google/cloud/spanner"
347
+ #
348
+ # spanner = Google::Cloud::Spanner.new
349
+ # instance = spanner.instance "my-instance"
350
+ #
351
+ # policy = instance.policy
352
+ #
353
+ # @example Update the policy by passing a block:
354
+ # require "google/cloud/spanner"
355
+ #
356
+ # spanner = Google::Cloud::Spanner.new
357
+ # instance = spanner.instance "my-instance"
358
+ #
359
+ # instance.policy do |p|
360
+ # p.add "roles/owner", "user:owner@example.com"
361
+ # end # 2 API calls
362
+ #
363
+ def policy
364
+ ensure_service!
365
+ grpc = service.get_instance_policy path
366
+ policy = Policy.from_grpc grpc
367
+ return policy unless block_given?
368
+ yield policy
369
+ self.policy = policy
370
+ end
371
+
372
+ ##
373
+ # Updates the [Cloud IAM](https://cloud.google.com/iam/) access control
374
+ # policy for this instance. The policy should be read from {#policy}.
375
+ # See {Google::Cloud::Spanner::Policy} for an explanation of the policy
376
+ # `etag` property and how to modify policies.
377
+ #
378
+ # You can also update the policy by passing a block to {#policy}, which
379
+ # will call this method internally after the block completes.
380
+ #
381
+ # @see https://cloud.google.com/spanner/reference/rpc/google.iam.v1#google.iam.v1.Policy
382
+ # google.iam.v1.IAMPolicy
383
+ #
384
+ # @param [Policy] new_policy a new or modified Cloud IAM Policy for this
385
+ # instance
386
+ #
387
+ # @return [Policy] The policy returned by the API update operation.
388
+ #
389
+ # @example
390
+ # require "google/cloud/spanner"
391
+ #
392
+ # spanner = Google::Cloud::Spanner.new
393
+ # instance = spanner.instance "my-instance"
394
+ #
395
+ # policy = instance.policy # API call
396
+ #
397
+ # policy.add "roles/owner", "user:owner@example.com"
398
+ #
399
+ # instance.policy = policy # API call
400
+ #
401
+ def policy= new_policy
402
+ ensure_service!
403
+ grpc = service.set_instance_policy path, new_policy.to_grpc
404
+ Policy.from_grpc grpc
405
+ end
406
+
407
+ ##
408
+ # Tests the specified permissions against the [Cloud
409
+ # IAM](https://cloud.google.com/iam/) access control policy.
410
+ #
411
+ # @see https://cloud.google.com/iam/docs/managing-policies Managing
412
+ # Policies
413
+ #
414
+ # @param [String, Array<String>] permissions The set of permissions to
415
+ # check access for. Permissions with wildcards (such as `*` or
416
+ # `storage.*`) are not allowed.
417
+ #
418
+ # The permissions that can be checked on a instance are:
419
+ #
420
+ # * pubsub.instances.create
421
+ # * pubsub.instances.list
422
+ # * pubsub.instances.get
423
+ # * pubsub.instances.getIamPolicy
424
+ # * pubsub.instances.update
425
+ # * pubsub.instances.setIamPolicy
426
+ # * pubsub.instances.delete
427
+ #
428
+ # @return [Array<Strings>] The permissions that have access.
429
+ #
430
+ # @example
431
+ # require "google/cloud/spanner"
432
+ #
433
+ # spanner = Google::Cloud::Spanner.new
434
+ # instance = spanner.instance "my-instance"
435
+ # perms = instance.test_permissions "spanner.instances.get",
436
+ # "spanner.instances.update"
437
+ # perms.include? "spanner.instances.get" #=> true
438
+ # perms.include? "spanner.instances.update" #=> false
439
+ #
440
+ def test_permissions *permissions
441
+ permissions = Array(permissions).flatten
442
+ permissions = Array(permissions).flatten
443
+ ensure_service!
444
+ grpc = service.test_instance_permissions path, permissions
445
+ grpc.permissions
446
+ end
447
+
448
+ ##
449
+ # @private Creates a new Instance instance from a
450
+ # Google::Spanner::Admin::Instance::V1::Instance.
451
+ def self.from_grpc grpc, service
452
+ new grpc, service
453
+ end
454
+
455
+ protected
456
+
457
+ ##
458
+ # @private Raise an error unless an active connection to the service is
459
+ # available.
460
+ def ensure_service!
461
+ fail "Must have active connection to service" unless service
462
+ end
463
+
464
+ def instance_config_path name
465
+ return name if name.to_s.include? "/"
466
+ Admin::Instance::V1::InstanceAdminClient.instance_config_path(
467
+ project, name.to_s)
468
+ end
469
+ end
470
+ end
471
+ end
472
+ end