google-cloud-spanner 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -243,11 +243,11 @@ module Google
243
243
  def self.execute_query service, session_path, sql, params: nil,
244
244
  types: nil, transaction: nil,
245
245
  partition_token: nil, seqno: nil,
246
- query_options: nil
246
+ query_options: nil, call_options: nil
247
247
  execute_query_options = {
248
248
  transaction: transaction, params: params, types: types,
249
249
  partition_token: partition_token, seqno: seqno,
250
- query_options: query_options
250
+ query_options: query_options, call_options: call_options
251
251
  }
252
252
  enum = service.execute_streaming_sql session_path, sql,
253
253
  execute_query_options
@@ -262,10 +262,13 @@ module Google
262
262
  # @private
263
263
  def self.read service, session_path, table, columns, keys: nil,
264
264
  index: nil, limit: nil, transaction: nil,
265
- partition_token: nil
266
- read_options = { keys: keys, index: index, limit: limit,
267
- transaction: transaction,
268
- partition_token: partition_token }
265
+ partition_token: nil, call_options: nil
266
+ read_options = {
267
+ keys: keys, index: index, limit: limit,
268
+ transaction: transaction,
269
+ partition_token: partition_token,
270
+ call_options: call_options
271
+ }
269
272
  enum = service.streaming_read_table \
270
273
  session_path, table, columns, read_options
271
274
  from_enum(enum, service).tap do |results|
@@ -105,19 +105,26 @@ module Google
105
105
  credentials == :this_channel_is_insecure
106
106
  end
107
107
 
108
- def list_instances token: nil, max: nil
109
- paged_enum = instances.list_instances parent: project_path,
110
- page_size: max,
111
- page_token: token
108
+ def list_instances token: nil, max: nil, call_options: nil
109
+ opts = default_options call_options: call_options
110
+ request = {
111
+ parent: project_path,
112
+ page_size: max,
113
+ page_token: token
114
+ }
115
+ paged_enum = instances.list_instances request, opts
112
116
  paged_enum.response
113
117
  end
114
118
 
115
- def get_instance name
116
- instances.get_instance name: instance_path(name)
119
+ def get_instance name, call_options: nil
120
+ opts = default_options call_options: call_options
121
+ request = { name: instance_path(name) }
122
+ instances.get_instance request, opts
117
123
  end
118
124
 
119
125
  def create_instance instance_id, name: nil, config: nil, nodes: nil,
120
- labels: nil
126
+ labels: nil, call_options: nil
127
+ opts = default_options call_options: call_options
121
128
  labels = Hash[labels.map { |k, v| [String(k), String(v)] }] if labels
122
129
 
123
130
  create_obj = Admin::Instance::V1::Instance.new({
@@ -125,116 +132,163 @@ module Google
125
132
  node_count: nodes, labels: labels
126
133
  }.delete_if { |_, v| v.nil? })
127
134
 
128
- instances.create_instance parent: project_path,
129
- instance_id: instance_id,
130
- instance: create_obj
135
+ request = {
136
+ parent: project_path,
137
+ instance_id: instance_id,
138
+ instance: create_obj
139
+ }
140
+ instances.create_instance request, opts
131
141
  end
132
142
 
133
- def update_instance instance
143
+ def update_instance instance, call_options: nil
144
+ opts = default_options call_options: call_options
134
145
  mask = Google::Protobuf::FieldMask.new(
135
146
  paths: %w[display_name node_count labels]
136
147
  )
137
-
138
- instances.update_instance instance: instance, field_mask: mask
148
+ request = { instance: instance, field_mask: mask }
149
+ instances.update_instance request, opts
139
150
  end
140
151
 
141
- def delete_instance name
142
- instances.delete_instance name: instance_path(name)
152
+ def delete_instance name, call_options: nil
153
+ opts = default_options call_options: call_options
154
+ request = { name: instance_path(name) }
155
+ instances.delete_instance request, opts
143
156
  end
144
157
 
145
- def get_instance_policy name
146
- instances.get_iam_policy resource: instance_path(name)
158
+ def get_instance_policy name, call_options: nil
159
+ opts = default_options call_options: call_options
160
+ request = { resource: instance_path(name) }
161
+ instances.get_iam_policy request, opts
147
162
  end
148
163
 
149
- def set_instance_policy name, new_policy
150
- instances.set_iam_policy resource: instance_path(name),
151
- policy: new_policy
164
+ def set_instance_policy name, new_policy, call_options: nil
165
+ opts = default_options call_options: call_options
166
+ request = {
167
+ resource: instance_path(name),
168
+ policy: new_policy
169
+ }
170
+ instances.set_iam_policy request, opts
152
171
  end
153
172
 
154
- def test_instance_permissions name, permissions
155
- instances.test_iam_permissions resource: instance_path(name),
156
- permissions: permissions
173
+ def test_instance_permissions name, permissions, call_options: nil
174
+ opts = default_options call_options: call_options
175
+ request = {
176
+ resource: instance_path(name),
177
+ permissions: permissions
178
+ }
179
+ instances.test_iam_permissions request, opts
157
180
  end
158
181
 
159
- def list_instance_configs token: nil, max: nil
160
- paged_enum = instances.list_instance_configs \
161
- parent: project_path, page_size: max, page_token: token
182
+ def list_instance_configs token: nil, max: nil, call_options: nil
183
+ opts = default_options call_options: call_options
184
+ request = { parent: project_path, page_size: max, page_token: token }
185
+ paged_enum = instances.list_instance_configs request, opts
162
186
  paged_enum.response
163
187
  end
164
188
 
165
- def get_instance_config name
166
- instances.get_instance_config name: instance_config_path(name)
189
+ def get_instance_config name, call_options: nil
190
+ opts = default_options call_options: call_options
191
+ request = { name: instance_config_path(name) }
192
+ instances.get_instance_config request, opts
167
193
  end
168
194
 
169
- def list_databases instance_id, token: nil, max: nil
170
- paged_enum = databases.list_databases \
195
+ def list_databases instance_id, token: nil, max: nil, call_options: nil
196
+ opts = default_options call_options: call_options
197
+ request = {
171
198
  parent: instance_path(instance_id),
172
199
  page_size: max,
173
200
  page_token: token
201
+ }
202
+ paged_enum = databases.list_databases request, opts
174
203
  paged_enum.response
175
204
  end
176
205
 
177
- def get_database instance_id, database_id
178
- databases.get_database name: database_path(instance_id, database_id)
206
+ def get_database instance_id, database_id, call_options: nil
207
+ opts = default_options call_options: call_options
208
+ request = { name: database_path(instance_id, database_id) }
209
+ databases.get_database request, opts
179
210
  end
180
211
 
181
- def create_database instance_id, database_id, statements: []
182
- databases.create_database \
212
+ def create_database instance_id, database_id, statements: [],
213
+ call_options: nil
214
+ opts = default_options call_options: call_options
215
+ request = {
183
216
  parent: instance_path(instance_id),
184
217
  create_statement: "CREATE DATABASE `#{database_id}`",
185
218
  extra_statements: Array(statements)
219
+ }
220
+ databases.create_database request, opts
186
221
  end
187
222
 
188
- def drop_database instance_id, database_id
189
- databases.drop_database \
190
- database: database_path(instance_id, database_id)
223
+ def drop_database instance_id, database_id, call_options: nil
224
+ opts = default_options call_options: call_options
225
+ request = { database: database_path(instance_id, database_id) }
226
+ databases.drop_database request, opts
191
227
  end
192
228
 
193
- def get_database_ddl instance_id, database_id
194
- databases.get_database_ddl \
195
- database: database_path(instance_id, database_id)
229
+ def get_database_ddl instance_id, database_id, call_options: nil
230
+ opts = default_options call_options: call_options
231
+ request = { database: database_path(instance_id, database_id) }
232
+ databases.get_database_ddl request, opts
196
233
  end
197
234
 
198
235
  def update_database_ddl instance_id, database_id, statements: [],
199
- operation_id: nil
200
- databases.update_database_ddl \
236
+ operation_id: nil, call_options: nil
237
+ opts = default_options call_options: call_options
238
+ request = {
201
239
  database: database_path(instance_id, database_id),
202
240
  statements: Array(statements),
203
241
  operation_id: operation_id
242
+ }
243
+ databases.update_database_ddl request, opts
204
244
  end
205
245
 
206
- def get_database_policy instance_id, database_id
207
- databases.get_iam_policy \
208
- resource: database_path(instance_id, database_id)
246
+ def get_database_policy instance_id, database_id, call_options: nil
247
+ opts = default_options call_options: call_options
248
+ request = { resource: database_path(instance_id, database_id) }
249
+ databases.get_iam_policy request, opts
209
250
  end
210
251
 
211
- def set_database_policy instance_id, database_id, new_policy
212
- databases.set_iam_policy \
252
+ def set_database_policy instance_id, database_id, new_policy,
253
+ call_options: nil
254
+ opts = default_options call_options: call_options
255
+ request = {
213
256
  resource: database_path(instance_id, database_id),
214
257
  policy: new_policy
258
+ }
259
+ databases.set_iam_policy request, opts
215
260
  end
216
261
 
217
- def test_database_permissions instance_id, database_id, permissions
218
- databases.test_iam_permissions \
262
+ def test_database_permissions instance_id, database_id, permissions,
263
+ call_options: nil
264
+ opts = default_options call_options: call_options
265
+ request = {
219
266
  resource: database_path(instance_id, database_id),
220
267
  permissions: permissions
268
+ }
269
+ databases.test_iam_permissions request, opts
221
270
  end
222
271
 
223
- def get_session session_name
224
- opts = default_options_from_session session_name
272
+ def get_session session_name, call_options: nil
273
+ opts = default_options session_name: session_name,
274
+ call_options: call_options
225
275
  service.get_session({ name: session_name }, opts)
226
276
  end
227
277
 
228
- def create_session database_name, labels: nil
229
- opts = default_options_from_session database_name
278
+ def create_session database_name, labels: nil,
279
+ call_options: nil
280
+ opts = default_options session_name: database_name,
281
+ call_options: call_options
230
282
  session = V1::Session.new labels: labels if labels
231
283
  service.create_session(
232
284
  { database: database_name, session: session }, opts
233
285
  )
234
286
  end
235
287
 
236
- def batch_create_sessions database_name, session_count, labels: nil
237
- opts = default_options_from_session database_name
288
+ def batch_create_sessions database_name, session_count, labels: nil,
289
+ call_options: nil
290
+ opts = default_options session_name: database_name,
291
+ call_options: call_options
238
292
  session = V1::Session.new labels: labels if labels
239
293
  # The response may have fewer sessions than requested in the RPC.
240
294
  request = {
@@ -245,16 +299,18 @@ module Google
245
299
  service.batch_create_sessions request, opts
246
300
  end
247
301
 
248
- def delete_session session_name
249
- opts = default_options_from_session session_name
302
+ def delete_session session_name, call_options: nil
303
+ opts = default_options session_name: session_name,
304
+ call_options: call_options
250
305
  service.delete_session({ name: session_name }, opts)
251
306
  end
252
307
 
253
308
  def execute_streaming_sql session_name, sql, transaction: nil,
254
309
  params: nil, types: nil, resume_token: nil,
255
310
  partition_token: nil, seqno: nil,
256
- query_options: nil
257
- opts = default_options_from_session session_name
311
+ query_options: nil, call_options: nil
312
+ opts = default_options session_name: session_name,
313
+ call_options: call_options
258
314
  request = {
259
315
  session: session_name,
260
316
  sql: sql,
@@ -269,8 +325,10 @@ module Google
269
325
  service.execute_streaming_sql request, opts
270
326
  end
271
327
 
272
- def execute_batch_dml session_name, transaction, statements, seqno
273
- opts = default_options_from_session session_name
328
+ def execute_batch_dml session_name, transaction, statements, seqno,
329
+ call_options: nil
330
+ opts = default_options session_name: session_name,
331
+ call_options: call_options
274
332
  statements = statements.map(&:to_grpc)
275
333
  request = {
276
334
  session: session_name,
@@ -293,8 +351,10 @@ module Google
293
351
 
294
352
  def streaming_read_table session_name, table_name, columns, keys: nil,
295
353
  index: nil, transaction: nil, limit: nil,
296
- resume_token: nil, partition_token: nil
297
- opts = default_options_from_session session_name
354
+ resume_token: nil, partition_token: nil,
355
+ call_options: nil
356
+ opts = default_options session_name: session_name,
357
+ call_options: call_options
298
358
  request = {
299
359
  session: session_name, table: table_name, columns: columns,
300
360
  key_set: keys, transaction: transaction, index: index,
@@ -306,11 +366,12 @@ module Google
306
366
 
307
367
  def partition_read session_name, table_name, columns, transaction,
308
368
  keys: nil, index: nil, partition_size_bytes: nil,
309
- max_partitions: nil
369
+ max_partitions: nil, call_options: nil
310
370
  partition_opts = partition_options partition_size_bytes,
311
371
  max_partitions
312
372
 
313
- opts = default_options_from_session session_name
373
+ opts = default_options session_name: session_name,
374
+ call_options: call_options
314
375
  request = {
315
376
  session: session_name, table: table_name, key_set: keys,
316
377
  transaction: transaction, index: index, columns: columns,
@@ -321,11 +382,12 @@ module Google
321
382
 
322
383
  def partition_query session_name, sql, transaction, params: nil,
323
384
  types: nil, partition_size_bytes: nil,
324
- max_partitions: nil
385
+ max_partitions: nil, call_options: nil
325
386
  partition_opts = partition_options partition_size_bytes,
326
387
  max_partitions
327
388
 
328
- opts = default_options_from_session session_name
389
+ opts = default_options session_name: session_name,
390
+ call_options: call_options
329
391
  request = {
330
392
  session: session_name, sql: sql, transaction: transaction,
331
393
  params: params, param_types: types,
@@ -334,14 +396,16 @@ module Google
334
396
  service.partition_query request, opts
335
397
  end
336
398
 
337
- def commit session_name, mutations = [], transaction_id: nil
399
+ def commit session_name, mutations = [], transaction_id: nil,
400
+ call_options: nil
338
401
  tx_opts = nil
339
402
  if transaction_id.nil?
340
403
  tx_opts = V1::TransactionOptions.new(
341
404
  read_write: V1::TransactionOptions::ReadWrite.new
342
405
  )
343
406
  end
344
- opts = default_options_from_session session_name
407
+ opts = default_options session_name: session_name,
408
+ call_options: call_options
345
409
  request = {
346
410
  session: session_name, transaction_id: transaction_id,
347
411
  single_use_transaction: tx_opts, mutations: mutations
@@ -349,23 +413,25 @@ module Google
349
413
  service.commit request, opts
350
414
  end
351
415
 
352
- def rollback session_name, transaction_id
353
- opts = default_options_from_session session_name
416
+ def rollback session_name, transaction_id, call_options: nil
417
+ opts = default_options session_name: session_name,
418
+ call_options: call_options
354
419
  request = { session: session_name, transaction_id: transaction_id }
355
420
  service.rollback request, opts
356
421
  end
357
422
 
358
- def begin_transaction session_name
423
+ def begin_transaction session_name, call_options: nil
359
424
  tx_opts = V1::TransactionOptions.new(
360
425
  read_write: V1::TransactionOptions::ReadWrite.new
361
426
  )
362
- opts = default_options_from_session session_name
427
+ opts = default_options session_name: session_name,
428
+ call_options: call_options
363
429
  request = { session: session_name, options: tx_opts }
364
430
  service.begin_transaction request, opts
365
431
  end
366
432
 
367
433
  def create_snapshot session_name, strong: nil, timestamp: nil,
368
- staleness: nil
434
+ staleness: nil, call_options: nil
369
435
  tx_opts = V1::TransactionOptions.new(
370
436
  read_only: V1::TransactionOptions::ReadOnly.new(
371
437
  {
@@ -376,80 +442,107 @@ module Google
376
442
  }.delete_if { |_, v| v.nil? }
377
443
  )
378
444
  )
379
- opts = default_options_from_session session_name
445
+ opts = default_options session_name: session_name,
446
+ call_options: call_options
380
447
  request = { session: session_name, options: tx_opts }
381
448
  service.begin_transaction request, opts
382
449
  end
383
450
 
384
- def create_pdml session_name
451
+ def create_pdml session_name, call_options: nil
385
452
  tx_opts = V1::TransactionOptions.new(
386
453
  partitioned_dml: V1::TransactionOptions::PartitionedDml.new
387
454
  )
388
- opts = default_options_from_session session_name
455
+ opts = default_options session_name: session_name,
456
+ call_options: call_options
389
457
  request = { session: session_name, options: tx_opts }
390
458
  service.begin_transaction request, opts
391
459
  end
392
460
 
393
- def create_backup instance_id, database_id, backup_id, expire_time
461
+ def create_backup instance_id, database_id, backup_id, expire_time,
462
+ call_options: nil
463
+ opts = default_options call_options: call_options
394
464
  backup = {
395
465
  database: database_path(instance_id, database_id),
396
466
  expire_time: expire_time
397
467
  }
398
- databases.create_backup parent: instance_path(instance_id),
399
- backup_id: backup_id,
400
- backup: backup
468
+ request = {
469
+ parent: instance_path(instance_id),
470
+ backup_id: backup_id,
471
+ backup: backup
472
+ }
473
+ databases.create_backup request, opts
401
474
  end
402
475
 
403
- def get_backup instance_id, backup_id
404
- databases.get_backup name: backup_path(instance_id, backup_id)
476
+ def get_backup instance_id, backup_id, call_options: nil
477
+ opts = default_options call_options: call_options
478
+ request = { name: backup_path(instance_id, backup_id) }
479
+ databases.get_backup request, opts
405
480
  end
406
481
 
407
- def update_backup backup, update_mask
408
- databases.update_backup backup: backup, update_mask: update_mask
482
+ def update_backup backup, update_mask, call_options: nil
483
+ opts = default_options call_options: call_options
484
+ request = { backup: backup, update_mask: update_mask }
485
+ databases.update_backup request, opts
409
486
  end
410
487
 
411
- def delete_backup instance_id, backup_id
412
- databases.delete_backup name: backup_path(instance_id, backup_id)
488
+ def delete_backup instance_id, backup_id, call_options: nil
489
+ opts = default_options call_options: call_options
490
+ request = { name: backup_path(instance_id, backup_id) }
491
+ databases.delete_backup request, opts
413
492
  end
414
493
 
415
494
  def list_backups instance_id,
416
- filter: nil, page_size: nil, page_token: nil
417
- databases.list_backups parent: instance_path(instance_id),
418
- filter: filter,
419
- page_size: page_size,
420
- page_token: page_token
495
+ filter: nil, page_size: nil, page_token: nil,
496
+ call_options: nil
497
+ opts = default_options call_options: call_options
498
+ request = {
499
+ parent: instance_path(instance_id),
500
+ filter: filter,
501
+ page_size: page_size,
502
+ page_token: page_token
503
+ }
504
+ databases.list_backups request, opts
421
505
  end
422
506
 
423
507
  def list_database_operations instance_id,
424
508
  filter: nil,
425
509
  page_size: nil,
426
- page_token: nil
427
- databases.list_database_operations(
510
+ page_token: nil,
511
+ call_options: nil
512
+ opts = default_options call_options: call_options
513
+ request = {
428
514
  parent: instance_path(instance_id),
429
515
  filter: filter,
430
516
  page_size: page_size,
431
517
  page_token: page_token
432
- )
518
+ }
519
+ databases.list_database_operations request, opts
433
520
  end
434
521
 
435
522
  def list_backup_operations instance_id,
436
523
  filter: nil, page_size: nil,
437
- page_token: nil
438
- databases.list_backup_operations(
524
+ page_token: nil,
525
+ call_options: nil
526
+ opts = default_options call_options: call_options
527
+ request = {
439
528
  parent: instance_path(instance_id),
440
529
  filter: filter,
441
530
  page_size: page_size,
442
531
  page_token: page_token
443
- )
532
+ }
533
+ databases.list_backup_operations request, opts
444
534
  end
445
535
 
446
536
  def restore_database backup_instance_id, backup_id,
447
- database_instance_id, database_id
448
- databases.restore_database(
537
+ database_instance_id, database_id,
538
+ call_options: nil
539
+ opts = default_options call_options: call_options
540
+ request = {
449
541
  parent: instance_path(database_instance_id),
450
542
  database_id: database_id,
451
543
  backup: backup_path(backup_instance_id, backup_id)
452
- )
544
+ }
545
+ databases.restore_database request, opts
453
546
  end
454
547
 
455
548
  def inspect
@@ -466,9 +559,17 @@ module Google
466
559
  value << " gccl"
467
560
  end
468
561
 
469
- def default_options_from_session session_name
470
- default_prefix = session_name.split("/sessions/").first
471
- { metadata: { "google-cloud-resource-prefix" => default_prefix } }
562
+ def default_options session_name: nil, call_options: nil
563
+ opts = {}
564
+ if session_name
565
+ default_prefix = session_name.split("/sessions/").first
566
+ opts[:metadata] = { "google-cloud-resource-prefix" => default_prefix }
567
+ end
568
+ if call_options
569
+ opts[:timeout] = call_options[:timeout] if call_options[:timeout]
570
+ opts[:retry_policy] = call_options[:retry_policy] if call_options[:retry_policy]
571
+ end
572
+ return opts unless opts.empty?
472
573
  end
473
574
 
474
575
  def partition_options partition_size_bytes, max_partitions