google-cloud-spanner 1.15.0 → 1.16.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.
@@ -0,0 +1,63 @@
1
+ # Copyright 2020 Google LLC
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
+ # https://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/database/backup_info"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Spanner
21
+ class Database
22
+ class RestoreInfo
23
+ ##
24
+ # @private Creates a new Database::RestoreInfo instance.
25
+ def initialize grpc
26
+ @grpc = grpc
27
+ end
28
+
29
+ ##
30
+ # The database restored from source type `:BACKUP`.
31
+ # @return [Symbol]
32
+ def source_type
33
+ @grpc.source_type
34
+ end
35
+
36
+ ##
37
+ # Database restored from backup.
38
+ #
39
+ # @return [Boolean]
40
+ def source_backup?
41
+ @grpc.source_type == :BACKUP
42
+ end
43
+
44
+ # Information about the backup used to restore the database.
45
+ # The backup may no longer exist.
46
+ #
47
+ # @return [Google::Cloud::Spanner::Database::BackupInfo, nil]
48
+ def backup_info
49
+ return nil unless @grpc.backup_info
50
+ BackupInfo.from_grpc @grpc.backup_info
51
+ end
52
+
53
+ ##
54
+ # @private Creates a new Database::RestoreInfo instance from a
55
+ # Google::Spanner::Admin::Database::V1::RestoreInfo.
56
+ def self.from_grpc grpc
57
+ new grpc
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -334,6 +334,406 @@ module Google
334
334
  Database::Job.from_grpc grpc, service
335
335
  end
336
336
 
337
+ ##
338
+ # Retrieves the list of database operations for the given instance.
339
+ #
340
+ # @param filter [String]
341
+ # A filter expression that filters what operations are returned in the
342
+ # response.
343
+ #
344
+ # The response returns a list of
345
+ # {Google::Longrunning::Operation long-running operations} whose names
346
+ # are prefixed by a database name within the specified instance.
347
+ # The long-running operation
348
+ # {Google::Longrunning::Operation#metadata metadata} field type
349
+ # `metadata.type_url` describes the type of the metadata.
350
+ #
351
+ # The filter expression must specify the field name,
352
+ # a comparison operator, and the value that you want to use for
353
+ # filtering. The value must be a string, a number, or a boolean.
354
+ # The comparison operator must be
355
+ # <, >, <=, >=, !=, =, or :. Colon ':' represents a HAS operator
356
+ # which is roughly synonymous with equality. Filter rules are case
357
+ # insensitive.
358
+ #
359
+ # The long-running operation fields eligible for filtering are:
360
+ # * `name` --> The name of the long-running operation
361
+ # * `done` --> False if the operation is in progress, else true.
362
+ # * `metadata.type_url` (using filter string `metadata.@type`) and
363
+ # fields in `metadata.value` (using filter string
364
+ # `metadata.<field_name>`, where <field_name> is a field in
365
+ # metadata.value) are eligible for filtering.
366
+ # * `error` --> Error associated with the long-running operation.
367
+ # * `response.type_url` (using filter string `response.@type`) and
368
+ # fields in `response.value` (using filter string
369
+ # `response.<field_name>`, where <field_name> is a field in
370
+ # response.value)are eligible for filtering.
371
+ #
372
+ # To filter on multiple expressions, provide each separate
373
+ # expression within parentheses. By default, each expression
374
+ # is an AND expression. However, you can include AND, OR, and NOT
375
+ # expressions explicitly.
376
+ #
377
+ # Some examples of using filters are:
378
+ #
379
+ # * `done:true` --> The operation is complete.
380
+ # * `(metadata.@type:type.googleapis.com/google.spanner.admin.\
381
+ # database.v1.RestoreDatabaseMetadata)
382
+ # AND (metadata.source_type:BACKUP)
383
+ # AND (metadata.backup_info.backup:backup_howl)
384
+ # AND (metadata.name:restored_howl)
385
+ # AND (metadata.progress.start_time < \"2018-03-28T14:50:00Z\")
386
+ # AND (error:*)`
387
+ # --> Return RestoreDatabase operations from backups whose name
388
+ # contains "backup_howl", where the created database name
389
+ # contains the string "restored_howl", the start_time of the
390
+ # restore operation is before 2018-03-28T14:50:00Z,
391
+ # and the operation returned an error.
392
+ # @param page_size [Integer]
393
+ # The maximum number of resources contained in the underlying API
394
+ # response. If page streaming is performed per-resource, this
395
+ # parameter does not affect the return value. If page streaming is
396
+ # performed per-page, this determines the maximum number of
397
+ # resources in a page.
398
+ #
399
+ # @return [Array<Google::Cloud::Spanner::Database::Job>] List
400
+ # representing the long-running, asynchronous processing
401
+ # of a database operations.
402
+ # (See {Google::Cloud::Spanner::Database::Job::List})
403
+ #
404
+ # @example
405
+ # require "google/cloud/spanner"
406
+ #
407
+ # spanner = Google::Cloud::Spanner.new
408
+ #
409
+ # instance = spanner.instance "my-instance"
410
+ #
411
+ # jobs = instance.database_operations
412
+ # jobs.each do |job|
413
+ # if job.error?
414
+ # p job.error
415
+ # else
416
+ # p job.database.database_id
417
+ # end
418
+ # end
419
+ #
420
+ # @example Retrieve all
421
+ # require "google/cloud/spanner"
422
+ #
423
+ # spanner = Google::Cloud::Spanner.new
424
+ #
425
+ # instance = spanner.instance "my-instance"
426
+ #
427
+ # jobs = instance.database_operations
428
+ # jobs.all do |job|
429
+ # if job.error?
430
+ # p job.error
431
+ # else
432
+ # puts job.database.database_id
433
+ # end
434
+ # end
435
+ #
436
+ # @example List by page size
437
+ # require "google/cloud/spanner"
438
+ #
439
+ # spanner = Google::Cloud::Spanner.new
440
+ # instance = spanner.instance "my-instance"
441
+ #
442
+ # jobs = instance.database_operations page_size: 10
443
+ # jobs.each do |job|
444
+ # if job.error?
445
+ # p job.error
446
+ # else
447
+ # puts job.database.database_id
448
+ # end
449
+ # end
450
+ #
451
+ # @example Filter and list
452
+ # require "google/cloud/spanner"
453
+ #
454
+ # spanner = Google::Cloud::Spanner.new
455
+ #
456
+ # instance = spanner.instance "my-instance"
457
+ #
458
+ # filter = "metadata.@type:CreateDatabaseMetadata"
459
+ # jobs = instance.database_operations filter: filter
460
+ # jobs.each do |job|
461
+ # if job.error?
462
+ # p job.error
463
+ # else
464
+ # puts job.database.database_id
465
+ # end
466
+ # end
467
+ #
468
+ def database_operations filter: nil, page_size: nil
469
+ grpc = service.list_database_operations \
470
+ instance_id,
471
+ filter: filter,
472
+ page_size: page_size
473
+ Database::Job::List.from_grpc grpc, service
474
+ end
475
+
476
+ ##
477
+ # Retrieves backups belonging to the instance.
478
+ #
479
+ # @param [String] filter Optional. A filter expression that filters
480
+ # backups listed in the response. The expression must specify the
481
+ # field name, a comparison operator, and the value that you want to
482
+ # use for filtering. The value must be a string, a number, or a
483
+ # boolean. The comparison operator must be
484
+ # <, >, <=, >=, !=, =, or :. Colon ':' represents a HAS operator
485
+ # which is roughly synonymous with equality.
486
+ # Filter rules are case insensitive.
487
+ #
488
+ # The fields eligible for filtering are:
489
+ # * `name`
490
+ # * `database`
491
+ # * `state`
492
+ # * `create_time`(and values are of the format YYYY-MM-DDTHH:MM:SSZ)
493
+ # * `expire_time`(and values are of the format YYYY-MM-DDTHH:MM:SSZ)
494
+ # * `size_bytes`
495
+ #
496
+ # To filter on multiple expressions, provide each separate expression
497
+ # within parentheses. By default, each expression is an AND
498
+ # expression. However, you can include AND, OR, and NOT expressions
499
+ # explicitly.
500
+ #
501
+ # Some examples of using filters are:
502
+ #
503
+ # * `name:Howl` --> The backup's name contains the string "howl".
504
+ # * `database:prod`
505
+ # --> The database's name contains the string "prod".
506
+ # * `state:CREATING` --> The backup is pending creation.
507
+ # * `state:READY` --> The backup is fully created and ready for use.
508
+ # * `(name:howl) AND (create_time < \"2018-03-28T14:50:00Z\")`
509
+ # --> The backup name contains the string "howl" and
510
+ # `create_time` of the backup is before
511
+ # 2018-03-28T14:50:00Z.
512
+ # * `expire_time < \"2018-03-28T14:50:00Z\"`
513
+ # --> The backup `expire_time` is before 2018-03-28T14:50:00Z.
514
+ # * `size_bytes > 10000000000` -->
515
+ # The backup's size is greater than 10GB
516
+ # @param [Integer] page_size Optional. Number of backups to be returned
517
+ # in the response. If 0 or less, defaults to the server's maximum
518
+ # allowed page size.
519
+ # @return [Array<Google::Cloud::Spanner::Backup>] Enumerable list of
520
+ # backups. (See {Google::Cloud::Spanner::Backup::List})
521
+ #
522
+ # @example
523
+ # require "google/cloud/spanner"
524
+ #
525
+ # spanner = Google::Cloud::Spanner.new
526
+ # instance = spanner.instance "my-instance"
527
+ #
528
+ # instance.backups.all.each do |backup|
529
+ # puts backup.backup_id
530
+ # end
531
+ #
532
+ # @example List backups by page size
533
+ # require "google/cloud/spanner"
534
+ #
535
+ # spanner = Google::Cloud::Spanner.new
536
+ # instance = spanner.instance "my-instance"
537
+ #
538
+ # instance.backups(page_size: 5).all.each do |backup|
539
+ # puts backup.backup_id
540
+ # end
541
+ #
542
+ # @example Filter and list backups
543
+ # require "google/cloud/spanner"
544
+ #
545
+ # spanner = Google::Cloud::Spanner.new
546
+ # instance = spanner.instance "my-instance"
547
+ #
548
+ # # filter backups by name.
549
+ # instance.backups(filter: "name:my-backup").all.each do |backup|
550
+ # puts backup.backup_id
551
+ # end
552
+ #
553
+ # # filter backups by database name.
554
+ # instance.backups(filter: "database:prod-db").all.each do |backup|
555
+ # puts backup.backup_id
556
+ # end
557
+ #
558
+ def backups filter: nil, page_size: nil
559
+ ensure_service!
560
+ grpc = service.list_backups \
561
+ instance_id,
562
+ filter: filter,
563
+ page_size: page_size
564
+ Backup::List.from_grpc grpc, service
565
+ end
566
+
567
+ ##
568
+ # Retrieves a backup belonging to the instance by identifier.
569
+ #
570
+ # @param [String] backup_id The unique identifier for the backup.
571
+ #
572
+ # @return [Google::Cloud::Spanner::Backup, nil] Returns `nil`
573
+ # if database does not exist.
574
+ #
575
+ # @example
576
+ # require "google/cloud/spanner"
577
+ #
578
+ # spanner = Google::Cloud::Spanner.new
579
+ # instance = spanner.instance "my-instance"
580
+ # backup = instance.backup "my-backup"
581
+ #
582
+ # @example Will return `nil` if backup does not exist.
583
+ # require "google/cloud/spanner"
584
+ #
585
+ # spanner = Google::Cloud::Spanner.new
586
+ # instance = spanner.instance "my-instance"
587
+ # backup = instance.backup "non-existing-backup" # nil
588
+ #
589
+ def backup backup_id
590
+ ensure_service!
591
+ grpc = service.get_backup instance_id, backup_id
592
+ Backup.from_grpc grpc, service
593
+ rescue Google::Cloud::NotFoundError
594
+ nil
595
+ end
596
+
597
+ ##
598
+ # Retrieves the list of database backup operations for the given
599
+ # instance.
600
+ #
601
+ # @param filter [String]
602
+ # A filter expression that filters what operations are returned in the
603
+ # response.
604
+ #
605
+ # The response returns a list of
606
+ # {Google::Longrunning::Operation long-running operations} whose names
607
+ # are prefixed by a backup name within the specified instance.
608
+ # The long-running operation
609
+ # {Google::Longrunning::Operation#metadata metadata} field type
610
+ # `metadata.type_url` describes the type of the metadata.
611
+ #
612
+ # The filter expression must specify the field name of an operation, a
613
+ # comparison operator, and the value that you want to use for
614
+ # filtering.
615
+ # The value must be a string, a number, or a boolean. The comparison
616
+ # operator must be
617
+ # <, >, <=, >=, !=, =, or :. Colon ':'' represents a HAS operator
618
+ # which is roughly synonymous with equality. Filter rules are case
619
+ # insensitive.
620
+ #
621
+ # The long-running operation fields eligible for filtering are:
622
+ # * `name` --> The name of the long-running operation
623
+ # * `done` --> False if the operation is in progress, else true.
624
+ # * `metadata.type_url` (using filter string `metadata.@type`) and
625
+ # fields in `metadata.value` (using filter string
626
+ # `metadata.<field_name>`, where <field_name> is a field in
627
+ # metadata.value) are eligible for filtering.
628
+ # * `error` --> Error associated with the long-running operation.
629
+ # * `response.type_url` (using filter string `response.@type`) and
630
+ # fields in `response.value` (using filter string
631
+ # `response.<field_name>`, where <field_name> is a field in
632
+ # response.value) are eligible for filtering.
633
+ #
634
+ # To filter on multiple expressions, provide each separate
635
+ # expression within parentheses. By default, each expression is an
636
+ # AND expression. However, you can include AND, OR, and NOT
637
+ # expressions explicitly.
638
+ #
639
+ # Some examples of using filters are:
640
+ #
641
+ # * `done:true` --> The operation is complete.
642
+ # * `metadata.database:prod`
643
+ # --> The database the backup was taken from has a name containing
644
+ # the string "prod".
645
+ # * `(metadata.@type:type.googleapis.com/google.spanner.admin.\
646
+ # database.v1.CreateBackupMetadata)
647
+ # AND (metadata.name:howl)
648
+ # AND (metadata.progress.start_time < \"2018-03-28T14:50:00Z\")
649
+ # AND (error:*)`
650
+ # --> Return CreateBackup operations where the created backup name
651
+ # contains the string "howl", the progress.start_time of the
652
+ # backup operation is before 2018-03-28T14:50:00Z, and the
653
+ # operation returned an error.
654
+ # @param page_size [Integer]
655
+ # The maximum number of resources contained in the underlying API
656
+ # response. If page streaming is performed per-resource, this
657
+ # parameter does not affect the return value. If page streaming is
658
+ # performed per-page, this determines the maximum number of
659
+ # resources in a page.
660
+ #
661
+ # @return [Array<Google::Cloud::Spanner::Backup::Job>] List representing
662
+ # the long-running, asynchronous processing of a backup operations.
663
+ # (See {Google::Cloud::Spanner::Backup::Job::List})
664
+ #
665
+ # @example
666
+ # require "google/cloud/spanner"
667
+ #
668
+ # spanner = Google::Cloud::Spanner.new
669
+ #
670
+ # instance = spanner.instance "my-instance"
671
+ #
672
+ # jobs = instance.backup_operations
673
+ # jobs.each do |job|
674
+ # if job.error?
675
+ # p job.error
676
+ # else
677
+ # p job.backup.backup_id
678
+ # end
679
+ # end
680
+ #
681
+ # @example Retrieve all
682
+ # require "google/cloud/spanner"
683
+ #
684
+ # spanner = Google::Cloud::Spanner.new
685
+ #
686
+ # instance = spanner.instance "my-instance"
687
+ #
688
+ # jobs = instance.backup_operations
689
+ # jobs.all do |job|
690
+ # if job.error?
691
+ # p job.error
692
+ # else
693
+ # p job.backup.backup_id
694
+ # end
695
+ # end
696
+ #
697
+ # @example List by page size
698
+ # require "google/cloud/spanner"
699
+ #
700
+ # spanner = Google::Cloud::Spanner.new
701
+ # instance = spanner.instance "my-instance"
702
+ #
703
+ # jobs = instance.backup_operations page_size: 10
704
+ # jobs.each do |job|
705
+ # if job.error?
706
+ # p job.error
707
+ # else
708
+ # puts job.backup.backup_id
709
+ # end
710
+ # end
711
+ #
712
+ # @example Filter and list
713
+ # require "google/cloud/spanner"
714
+ #
715
+ # spanner = Google::Cloud::Spanner.new
716
+ #
717
+ # instance = spanner.instance "my-instance"
718
+ #
719
+ # filter = "metadata.@type:CreateBackupMetadata"
720
+ # jobs = instance.backup_operations filter: filter
721
+ # jobs.each do |job|
722
+ # if job.error?
723
+ # p job.error
724
+ # else
725
+ # puts job.backup.backup_id
726
+ # end
727
+ # end
728
+ #
729
+ def backup_operations filter: nil, page_size: nil
730
+ grpc = service.list_backup_operations \
731
+ instance_id,
732
+ filter: filter,
733
+ page_size: page_size
734
+ Backup::Job::List.from_grpc grpc, service
735
+ end
736
+
337
737
  ##
338
738
  # Gets the [Cloud IAM](https://cloud.google.com/iam/) access control
339
739
  # policy for this instance.