google-cloud-bigtable 1.2.2 → 1.3.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,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+
18
+ module Google
19
+ module Cloud
20
+ module Bigtable
21
+ class Backup
22
+ ##
23
+ # # Job
24
+ #
25
+ # A resource representing the long-running, asynchronous processing of an backup create operation. The job can
26
+ # be refreshed to retrieve the backup object once the operation has been completed.
27
+ #
28
+ # See {Cluster#create_backup}.
29
+ #
30
+ # @see https://cloud.google.com/bigtable/docs/reference/admin/rpc/google.longrunning#google.longrunning.Operation
31
+ # Long-running Operation
32
+ #
33
+ # @example
34
+ # require "google/cloud/bigtable"
35
+ #
36
+ # bigtable = Google::Cloud::Bigtable.new
37
+ # instance = bigtable.instance("my-instance")
38
+ # cluster = instance.cluster("my-cluster")
39
+ # table = instance.table("my-table")
40
+ #
41
+ # expire_time = Time.now + 60 * 60 * 7
42
+ # job = cluster.create_backup(table, "my-backup", expire_time)
43
+ #
44
+ # job.wait_until_done!
45
+ # job.done? #=> true
46
+ #
47
+ # if job.error?
48
+ # status = job.error
49
+ # else
50
+ # backup = job.backup
51
+ # end
52
+ #
53
+ class Job < LongrunningJob
54
+ ##
55
+ # Get the backup object from operation results.
56
+ #
57
+ # @return [Google::Cloud::Bigtable::Backup, nil] The backup instance, or `nil` if the operation is not
58
+ # complete.
59
+ #
60
+ # @example
61
+ # require "google/cloud/bigtable"
62
+ #
63
+ # bigtable = Google::Cloud::Bigtable.new
64
+ # instance = bigtable.instance("my-instance")
65
+ # cluster = instance.cluster("my-cluster")
66
+ # table = instance.table("my-table")
67
+ #
68
+ # expire_time = Time.now + 60 * 60 * 7
69
+ # job = cluster.create_backup(table, "my-backup", expire_time)
70
+ #
71
+ # job.wait_until_done!
72
+ # job.done? #=> true
73
+ #
74
+ # if job.error?
75
+ # status = job.error
76
+ # else
77
+ # backup = job.backup
78
+ # end
79
+ #
80
+ def backup
81
+ Backup.from_grpc results, service if results
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2020 Google LLC
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+
18
+ module Google
19
+ module Cloud
20
+ module Bigtable
21
+ class Backup
22
+ ##
23
+ # Backup::List is a special-case array with additional values.
24
+ class List < DelegateClass(::Array)
25
+ # @private
26
+ # The gRPC Service object.
27
+ attr_accessor :service
28
+
29
+ # @private
30
+ # The gRPC page enumerable object.
31
+ attr_accessor :grpc
32
+
33
+ # @private
34
+ # Creates a new Backup::List with an array of backups.
35
+ def initialize arr = []
36
+ super arr
37
+ end
38
+
39
+ ##
40
+ # Whether there is a next page of backups.
41
+ #
42
+ # @return [Boolean]
43
+ #
44
+ # @example
45
+ # require "google/cloud/bigtable"
46
+ #
47
+ # bigtable = Google::Cloud::Bigtable.new
48
+ #
49
+ # instance = bigtable.instance("my-instance")
50
+ # cluster = instance.cluster("my-cluster")
51
+ #
52
+ # backups = cluster.backups
53
+ #
54
+ # if backups.next?
55
+ # next_backups = backups.next
56
+ # end
57
+ #
58
+ def next?
59
+ grpc.next_page?
60
+ end
61
+
62
+ ##
63
+ # Retrieves the next page of backups.
64
+ #
65
+ # @return [Backup::List] The list of backups.
66
+ #
67
+ # @example
68
+ # require "google/cloud/bigtable"
69
+ #
70
+ # bigtable = Google::Cloud::Bigtable.new
71
+ #
72
+ # instance = bigtable.instance("my-instance")
73
+ # cluster = instance.cluster("my-cluster")
74
+ #
75
+ # backups = cluster.backups
76
+ #
77
+ # if backups.next?
78
+ # next_backups = backups.next
79
+ # end
80
+ #
81
+ def next
82
+ ensure_grpc!
83
+
84
+ return nil unless next?
85
+ grpc.next_page
86
+ self.class.from_grpc grpc, service
87
+ end
88
+
89
+ ##
90
+ # Retrieves remaining results by repeatedly invoking {#next} until {#next?} returns `false`. Calls the given
91
+ # block once for each result, which is passed as the argument to the block.
92
+ #
93
+ # An enumerator is returned if no block is given.
94
+ #
95
+ # This method will make repeated API calls until all remaining results are retrieved (unlike `#each`, for
96
+ # example, which merely iterates over the results returned by a single API call). Use with caution.
97
+ #
98
+ # @yield [backup] The block for accessing each backup.
99
+ # @yieldparam [Backup] backup The backup object.
100
+ #
101
+ # @return [Enumerator,nil] An enumerator is returned if no block is given, otherwise `nil`.
102
+ #
103
+ # @example Iterating each backup by passing a block:
104
+ # require "google/cloud/bigtable"
105
+ #
106
+ # bigtable = Google::Cloud::Bigtable.new
107
+ #
108
+ # instance = bigtable.instance("my-instance")
109
+ # cluster = instance.cluster("my-cluster")
110
+ #
111
+ # cluster.backups.all do |backup|
112
+ # puts backup.backup_id
113
+ # end
114
+ #
115
+ # @example Using the enumerator by not passing a block:
116
+ # require "google/cloud/bigtable"
117
+ #
118
+ # bigtable = Google::Cloud::Bigtable.new
119
+ #
120
+ # instance = bigtable.instance("my-instance")
121
+ # cluster = instance.cluster("my-cluster")
122
+ #
123
+ # all_backup_ids = cluster.backups.all.map do |backup|
124
+ # backup.backup_id
125
+ # end
126
+ #
127
+ def all
128
+ return enum_for :all unless block_given?
129
+
130
+ results = self
131
+ loop do
132
+ results.each { |r| yield r }
133
+ break unless next?
134
+ grpc.next_page
135
+ results = self.class.from_grpc grpc, service
136
+ end
137
+ end
138
+
139
+ # @private
140
+ # New Snapshot::List from a Google::Gax::PagedEnumerable<Google::Bigtable::Admin::V2::Backup> object.
141
+ # @param grpc [Google::Gax::PagedEnumerable<Google::Bigtable::Admin::V2::Backup> ]
142
+ # @param service [Google::Cloud::Bigtable::Service]
143
+ # @return [Array<Google::Cloud::Bigtable::Backup>]
144
+ def self.from_grpc grpc, service
145
+ backups = List.new(
146
+ Array(grpc.response.backups).map do |backup|
147
+ Backup.from_grpc backup, service
148
+ end
149
+ )
150
+ backups.grpc = grpc
151
+ backups.service = service
152
+ backups
153
+ end
154
+
155
+ protected
156
+
157
+ # @private
158
+ #
159
+ # Raises an error if an active gRPC call is not available.
160
+ def ensure_grpc!
161
+ raise "Must have active gRPC call" unless grpc
162
+ end
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
@@ -15,6 +15,7 @@
15
15
  # limitations under the License.
16
16
 
17
17
 
18
+ require "google/cloud/bigtable/backup"
18
19
  require "google/cloud/bigtable/cluster/list"
19
20
  require "google/cloud/bigtable/cluster/job"
20
21
 
@@ -196,6 +197,104 @@ module Google
196
197
  @grpc.location
197
198
  end
198
199
 
200
+ ##
201
+ # Creates a new Cloud Bigtable Backup.
202
+ #
203
+ # @param source_table [Table, String] The table object, or the name of the table,
204
+ # from which the backup is to be created. The table needs to be in the same
205
+ # instance as the backup. Required.
206
+ # @param backup_id [String] The id of the backup to be created. This string must
207
+ # be between 1 and 50 characters in length and match the regex
208
+ # `[_a-zA-Z0-9][-_.a-zA-Z0-9]*`. Required.
209
+ # @param expire_time [Time] The expiration time of the backup, with microseconds
210
+ # granularity that must be at least 6 hours and at most 30 days
211
+ # from the time the request is received. Once the `expire_time`
212
+ # has passed, Cloud Bigtable will delete the backup and free the
213
+ # resources used by the backup. Required.
214
+ # @return [Google::Cloud::Bigtable::Backup::Job]
215
+ # The job representing the long-running, asynchronous processing of
216
+ # a backup create operation.
217
+ #
218
+ # @example
219
+ # require "google/cloud/bigtable"
220
+ #
221
+ # bigtable = Google::Cloud::Bigtable.new
222
+ # instance = bigtable.instance("my-instance")
223
+ # cluster = instance.cluster("my-cluster")
224
+ # table = instance.table("my-table")
225
+ #
226
+ # expire_time = Time.now + 60 * 60 * 7
227
+ # job = cluster.create_backup(table, "my-backup", expire_time)
228
+ #
229
+ # job.wait_until_done!
230
+ # job.done? #=> true
231
+ #
232
+ # if job.error?
233
+ # status = job.error
234
+ # else
235
+ # backup = job.backup
236
+ # end
237
+ #
238
+ def create_backup source_table, backup_id, expire_time
239
+ source_table_id = source_table.respond_to?(:name) ? source_table.name : source_table
240
+ grpc = service.create_backup instance_id: instance_id,
241
+ cluster_id: cluster_id,
242
+ backup_id: backup_id,
243
+ source_table_id: source_table_id,
244
+ expire_time: expire_time
245
+ Backup::Job.from_grpc grpc, service
246
+ end
247
+
248
+ ##
249
+ # Gets a backup in the cluster.
250
+ #
251
+ # @param backup_id [String] The unique ID of the requested backup.
252
+ #
253
+ # @return [Google::Cloud::Bigtable::Backup, nil] The backup object, or `nil` if not found in the service.
254
+ #
255
+ # @example
256
+ # require "google/cloud/bigtable"
257
+ #
258
+ # bigtable = Google::Cloud::Bigtable.new
259
+ #
260
+ # instance = bigtable.instance("my-instance")
261
+ # cluster = instance.cluster("my-cluster")
262
+ #
263
+ # backup = cluster.backup("my-backup")
264
+ #
265
+ # if backup
266
+ # puts backup.backup_id
267
+ # end
268
+ #
269
+ def backup backup_id
270
+ grpc = service.get_backup instance_id, cluster_id, backup_id
271
+ Backup.from_grpc grpc, service
272
+ rescue Google::Cloud::NotFoundError
273
+ nil
274
+ end
275
+
276
+ ##
277
+ # Lists all backups in the cluster.
278
+ #
279
+ # @return [Array<Google::Cloud::Bigtable::Backup>] (See {Google::Cloud::Bigtable::Backup::List})
280
+ #
281
+ # @example
282
+ # require "google/cloud/bigtable"
283
+ #
284
+ # bigtable = Google::Cloud::Bigtable.new
285
+ #
286
+ # instance = bigtable.instance("my-instance")
287
+ # cluster = instance.cluster("my-cluster")
288
+ #
289
+ # cluster.backups.all do |backup|
290
+ # puts backup.backup_id
291
+ # end
292
+ #
293
+ def backups
294
+ grpc = service.list_backups instance_id, cluster_id
295
+ Backup::List.from_grpc grpc, service
296
+ end
297
+
199
298
  ##
200
299
  # Updates the cluster.
201
300
  #
@@ -109,7 +109,7 @@ module Google
109
109
  # @yield [cluster] The block for accessing each cluster.
110
110
  # @yieldparam [Cluster] cluster The cluster object.
111
111
  #
112
- # @return [Enumerator]
112
+ # @return [Enumerator,nil] An enumerator is returned if no block is given, otherwise `nil`.
113
113
  #
114
114
  # @example Iterating each cluster by passing a block:
115
115
  # require "google/cloud/bigtable"
@@ -117,7 +117,7 @@ module Google
117
117
  # @yieldparam [String] name the column family name.
118
118
  # @yieldparam [ColumnFamily] column_family the column family object.
119
119
  #
120
- # @return [Enumerator]
120
+ # @return [Enumerator,nil] An enumerator is returned if no block is given, otherwise `nil`.
121
121
  #
122
122
  def each
123
123
  return enum_for :each unless block_given?
@@ -106,7 +106,7 @@ module Google
106
106
  # @yield [instance] The block for accessing each instance.
107
107
  # @yieldparam [Instance] instance The instance object.
108
108
  #
109
- # @return [Enumerator]
109
+ # @return [Enumerator,nil] An enumerator is returned if no block is given, otherwise `nil`.
110
110
  #
111
111
  # @example Iterating each instance by passing a block:
112
112
  # require "google/cloud/bigtable"
@@ -97,6 +97,17 @@ module Google
97
97
  @grpc.wait_until_done!
98
98
  end
99
99
 
100
+ ##
101
+ # @private Gets the metadata object of the operation.
102
+ #
103
+ # @return [Object, nil] `nil` if the operation is not complete.
104
+ #
105
+ def metadata
106
+ return nil unless done?
107
+ return nil unless @grpc.grpc_op.result == :response
108
+ @grpc.metadata
109
+ end
110
+
100
111
  # @private
101
112
  # New BasicJob from a Google::Gax::Operation object.
102
113
  def self.from_grpc grpc, service
@@ -734,6 +734,75 @@ module Google
734
734
  end
735
735
  end
736
736
 
737
+ ##
738
+ # Starts creating a new backup. The underlying Google::Longrunning::Operation tracks creation of the backup.
739
+ #
740
+ # @return [Google::Gax::Operation]
741
+ #
742
+ def create_backup instance_id:, cluster_id:, backup_id:, source_table_id:, expire_time:
743
+ backup = Google::Bigtable::Admin::V2::Backup.new source_table: table_path(instance_id, source_table_id),
744
+ expire_time: expire_time
745
+ execute do
746
+ tables.create_backup cluster_path(instance_id, cluster_id), backup_id, backup
747
+ end
748
+ end
749
+
750
+ ##
751
+ # @return [Google::Bigtable::Admin::V2::Backup]
752
+ #
753
+ def get_backup instance_id, cluster_id, backup_id
754
+ execute do
755
+ tables.get_backup backup_path(instance_id, cluster_id, backup_id)
756
+ end
757
+ end
758
+
759
+ ##
760
+ # @return [Google::Gax::PagedEnumerable<Google::Bigtable::Admin::V2::Backup>]
761
+ #
762
+ def list_backups instance_id, cluster_id
763
+ execute do
764
+ tables.list_backups cluster_path(instance_id, cluster_id)
765
+ end
766
+ end
767
+
768
+ ##
769
+ # @param backup [Google::Bigtable::Admin::V2::Backup | Hash]
770
+ # @param fields [Array(String|Symbol)] the paths of fields to be updated
771
+ #
772
+ def update_backup backup, fields
773
+ mask = Google::Protobuf::FieldMask.new paths: fields.map(&:to_s)
774
+ execute do
775
+ tables.update_backup backup, mask
776
+ end
777
+ end
778
+
779
+ def delete_backup instance_id, cluster_id, backup_id
780
+ execute do
781
+ tables.delete_backup backup_path(instance_id, cluster_id, backup_id)
782
+ end
783
+ end
784
+
785
+ ##
786
+ # Create a new table by restoring from a completed backup.
787
+ #
788
+ # @param table_id [String] The table ID for the new table. This table must not yet exist.
789
+ # @param instance_id [String] The instance ID for the source backup. The table will be created in this instance.
790
+ # @param cluster_id [String] The cluster ID for the source backup.
791
+ # @param backup_id [String] The backup ID for the source backup.
792
+ #
793
+ # @return [Google::Gax::Operation] The {Google::Longrunning::Operation#metadata metadata} field type is
794
+ # {Google::Bigtable::Admin::RestoreTableMetadata RestoreTableMetadata}. The
795
+ # {Google::Longrunning::Operation#response response} type is {Google::Bigtable::Admin::V2::Table Table}, if
796
+ # successful.
797
+ #
798
+ def restore_table table_id, instance_id, cluster_id, backup_id
799
+ execute do
800
+ tables.restore_table instance_path(instance_id),
801
+ table_id: table_id,
802
+ backup: backup_path(instance_id, cluster_id, backup_id)
803
+ end
804
+ end
805
+
737
806
  ##
738
807
  # Executes the API call and wrap errors to {Google::Cloud::Error}.
739
808
  #
@@ -821,6 +890,16 @@ module Google
821
890
  Admin::V2::BigtableInstanceAdminClient.app_profile_path project_id, instance_id, app_profile_id
822
891
  end
823
892
 
893
+ ##
894
+ # Creates a formatted backup path.
895
+ #
896
+ # @return [String] Formatted backup path
897
+ # `projects/<project>/instances/<instance>/clusters/<cluster>/backups/<backup>`
898
+ #
899
+ def backup_path instance_id, cluster_id, backup_id
900
+ Admin::V2::BigtableTableAdminClient.backup_path project_id, instance_id, cluster_id, backup_id
901
+ end
902
+
824
903
  ##
825
904
  # Inspects the service object.
826
905
  # @return [String]