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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 31f7875bf6e782a6e02f78fc4c56059b6a7a33aad2aea8e5bb8ddb93932749e6
4
- data.tar.gz: 2d0babe313899246327cdc92fe537a94e5b2a8d58d2e300913bc79bfac4ced84
3
+ metadata.gz: f50e50b175e8946175ae34b8eae4e2ad6a9df8c2ab38ab71db0f4a7820b683f5
4
+ data.tar.gz: 3494e84da57d1d7f98fb96069435106d91a7c84d773e26722073ade94f2b47fe
5
5
  SHA512:
6
- metadata.gz: 73fd57fd66a11124e0e987f9dedde6bc1435a5ada931fab6eec310ecc219c977c977eef845eeb134f184dd41903a8f60a630573ba455f597ddbbcd768c00a593
7
- data.tar.gz: 39c0e508c9f06534fa6ea0ba9ecf91fff841b6d4709ccdfd2cbcafefa879b47a2c96d9678a96de109098962f4a31b462eea1893f2a00d0025a76f1c6fb2cb1b0
6
+ metadata.gz: bbcf972b4d1548ebfae47f13ac49f5f77472b6ce914172ac56acbb42b46f44c2a047868bee5dc8f1bf0a33bce4d937720be43a6e6bc349d661090c1e6e89fe70
7
+ data.tar.gz: 796d977752cd8f8cd699237fda28fcf964324e7542feb4ac1b9d9b6a6eeeb07111d4473292e318fb72b6d35ea5d4853b023770625ec225b6067ddbb0eba2102e
@@ -1,5 +1,11 @@
1
1
  # Release History
2
2
 
3
+ ### 1.16.0 / 2020-03-20
4
+
5
+ #### Features
6
+
7
+ * Added support for backing up and restoring databases ([#5053](https://www.github.com/googleapis/google-cloud-ruby/issues/5053))
8
+
3
9
  ### 1.15.0 / 2020-03-15
4
10
 
5
11
  #### Features
@@ -66,7 +66,7 @@
66
66
  "retry_params_name": "default"
67
67
  },
68
68
  "CreateBackup": {
69
- "timeout_millis": 30000,
69
+ "timeout_millis": 3600000,
70
70
  "retry_codes_name": "non_idempotent",
71
71
  "retry_params_name": "default"
72
72
  },
@@ -76,12 +76,12 @@
76
76
  "retry_params_name": "default"
77
77
  },
78
78
  "UpdateBackup": {
79
- "timeout_millis": 30000,
79
+ "timeout_millis": 3600000,
80
80
  "retry_codes_name": "non_idempotent",
81
81
  "retry_params_name": "default"
82
82
  },
83
83
  "DeleteBackup": {
84
- "timeout_millis": 30000,
84
+ "timeout_millis": 3600000,
85
85
  "retry_codes_name": "idempotent",
86
86
  "retry_params_name": "default"
87
87
  },
@@ -91,7 +91,7 @@
91
91
  "retry_params_name": "default"
92
92
  },
93
93
  "RestoreDatabase": {
94
- "timeout_millis": 30000,
94
+ "timeout_millis": 3600000,
95
95
  "retry_codes_name": "non_idempotent",
96
96
  "retry_params_name": "default"
97
97
  },
@@ -0,0 +1,315 @@
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/backup/job"
17
+ require "google/cloud/spanner/backup/list"
18
+ require "google/cloud/spanner/backup/restore/job"
19
+
20
+ module Google
21
+ module Cloud
22
+ module Spanner
23
+ ##
24
+ # # Backup
25
+ #
26
+ # A backup is a representation of Cloud Spanner database backup.
27
+ #
28
+ # See {Google::Cloud::Spanner::Instance#backups},
29
+ # {Google::Cloud::Spanner::Instance#backup}, and
30
+ # {Google::Cloud::Spanner::Database#create_backup}.
31
+ #
32
+ # @example
33
+ # require "google/cloud"
34
+ #
35
+ # spanner = Google::Cloud::Spanner.new
36
+ # database = spanner.database "my-instance", "my-database"
37
+ #
38
+ # expire_time = Time.now + 36000
39
+ # job = database.create_backup "my-backup", expire_time
40
+ #
41
+ # job.done? #=> false
42
+ # job.reload! # API call
43
+ # job.done? #=> true
44
+ #
45
+ # if job.error?
46
+ # status = job.error
47
+ # else
48
+ # backup = job.backup
49
+ # end
50
+ #
51
+ class Backup
52
+ ##
53
+ # @private The gRPC Service object.
54
+ attr_accessor :service
55
+
56
+ ##
57
+ # @private Creates a new Backup instance.
58
+ def initialize grpc, service
59
+ @grpc = grpc
60
+ @service = service
61
+ end
62
+
63
+ ##
64
+ # The unique identifier for the project.
65
+ # @return [String]
66
+ def project_id
67
+ @grpc.name.split("/")[1]
68
+ end
69
+
70
+ ##
71
+ # The unique identifier for the instance.
72
+ # @return [String]
73
+ def instance_id
74
+ @grpc.name.split("/")[3]
75
+ end
76
+
77
+ ##
78
+ # The unique identifier for the backup.
79
+ # @return [String]
80
+ def backup_id
81
+ @grpc.name.split("/")[5]
82
+ end
83
+
84
+ ##
85
+ # Name of the database from which this backup was created.
86
+ # @return [String]
87
+ def database_id
88
+ @grpc.database.split("/")[5]
89
+ end
90
+
91
+ ##
92
+ # The full path for the backup. Values are of the form
93
+ # `projects/<project>/instances/<instance>/backups/<backup_id>`.
94
+ # @return [String]
95
+ def path
96
+ @grpc.name
97
+ end
98
+
99
+ ##
100
+ # The current backup state. Possible values are `:CREATING` and
101
+ # `:READY`.
102
+ # @return [Symbol]
103
+ def state
104
+ @grpc.state
105
+ end
106
+
107
+ ##
108
+ # The backup is still being created. A backup is not yet available
109
+ # for the database restore operation.
110
+ # @return [Boolean]
111
+ def creating?
112
+ state == :CREATING
113
+ end
114
+
115
+ ##
116
+ # The backup is created and can be used to restore a database.
117
+ # @return [Boolean]
118
+ def ready?
119
+ state == :READY
120
+ end
121
+
122
+ ##
123
+ # The expiration time of the backup, with microseconds granularity.
124
+ # @return [Time]
125
+ def expire_time
126
+ Convert.timestamp_to_time @grpc.expire_time
127
+ end
128
+
129
+ ##
130
+ # Update backup expiration time.
131
+ #
132
+ # Set expiration time of the backup, with microseconds granularity
133
+ # that must be at least 6 hours and at most 366 days from the time the
134
+ # request is received. Once the `expire_time` has passed, Cloud Spanner
135
+ # will delete the backup and free the resources used by the backup.
136
+ #
137
+ # @param [Time] time Backup expiration time.
138
+ # @raise [Google::Cloud::Error] if expire time is in past or update
139
+ # call is aborted.
140
+ #
141
+ # @example
142
+ # require "google/cloud/spanner"
143
+ #
144
+ # spanner = Google::Cloud::Spanner.new
145
+ #
146
+ # instance = spanner.instance "my-instance"
147
+ # backup = instance.backup "my-backup"
148
+ # backup.expire_time = Time.now + 36000
149
+ # puts backup.expire_time
150
+ #
151
+ def expire_time= time
152
+ ensure_service!
153
+
154
+ expire_time_was = @grpc.expire_time
155
+ @grpc.expire_time = Convert.time_to_timestamp time
156
+ update_mask = Google::Protobuf::FieldMask.new paths: ["expire_time"]
157
+ @grpc = service.update_backup @grpc, update_mask
158
+ rescue Google::Cloud::Error => error
159
+ @grpc.expire_time = expire_time_was
160
+ raise error
161
+ end
162
+
163
+ ##
164
+ # Create time is approximately the time when the backup request was
165
+ # received.
166
+ # @return [Time]
167
+ def create_time
168
+ Convert.timestamp_to_time @grpc.create_time
169
+ end
170
+
171
+ ##
172
+ # Size of the backup in bytes.
173
+ # @return [Integer]
174
+ def size_in_bytes
175
+ @grpc.size_bytes
176
+ end
177
+
178
+ ##
179
+ # The instances of the restored databases that reference the backup.
180
+ # Referencing databases may exist in different instances.
181
+ # The existence of any referencing database prevents the backup from
182
+ # being deleted. When a restored database from the backup enters the
183
+ # `READY` state, the reference to the backup is removed.
184
+ #
185
+ # @return [Array<Google::Cloud::Spanner::Database>] Returns list of
186
+ # referencing database instances.
187
+ #
188
+ # @example
189
+ # spanner = Google::Cloud::Spanner.new
190
+ #
191
+ # instance = spanner.instance "my-instance"
192
+ # backup = instance.backup "my-backup"
193
+ #
194
+ # backup.referencing_databases.each do |database|
195
+ # puts database.database_id
196
+ # end
197
+ #
198
+ def referencing_databases
199
+ ensure_service!
200
+
201
+ @grpc.referencing_databases.map do |referencing_database|
202
+ segments = referencing_database.split "/"
203
+ database_grpc = service.get_database segments[3], segments[5]
204
+ Database.from_grpc database_grpc, service
205
+ end
206
+ end
207
+
208
+ ##
209
+ # Permanently deletes the backup.
210
+ #
211
+ # @return [Boolean] Returns `true` if the backup was deleted.
212
+ #
213
+ # @example
214
+ # require "google/cloud/spanner"
215
+ #
216
+ # spanner = Google::Cloud::Spanner.new
217
+ #
218
+ # instance = spanner.instance "my-instance"
219
+ # backup = instance.backup "my-backup"
220
+ # backup.delete # true
221
+ #
222
+ def delete
223
+ ensure_service!
224
+ service.delete_backup instance_id, backup_id
225
+ true
226
+ end
227
+
228
+ ##
229
+ # Restores deleted database from the backup.
230
+ #
231
+ # @param [String] database_id The unique identifier for the database,
232
+ # which cannot be changed after the database is created. Values are of
233
+ # the form `[a-z][a-z0-9_\-]*[a-z0-9]` and must be between 2 and 30
234
+ # characters in length. Required.
235
+ # @param [String] instance_id The name of the instance in which to
236
+ # create the restored database. This instance must be in the same
237
+ # project and have the same instance configuration as the instance
238
+ # containing the source backup. Optional. Default value is same as a
239
+ # backup instance.
240
+ # @return [Database] Restored database.
241
+ #
242
+ # @example
243
+ # require "google/cloud/spanner"
244
+ #
245
+ # spanner = Google::Cloud::Spanner.new
246
+ #
247
+ # instance = spanner.instance "my-instance"
248
+ # backup = instance.backup "my-backup"
249
+ # job = backup.restore "my-restored-database"
250
+ #
251
+ # job.done? #=> false
252
+ # job.reload! # API call
253
+ # job.done? #=> true
254
+ #
255
+ # if job.error?
256
+ # status = job.error
257
+ # else
258
+ # database = job.database
259
+ # end
260
+ #
261
+ # @example Restore database in provided instance id
262
+ # require "google/cloud/spanner"
263
+ #
264
+ # spanner = Google::Cloud::Spanner.new
265
+ #
266
+ # instance = spanner.instance "my-instance"
267
+ # backup = instance.backup "my-backup"
268
+ # job = backup.restore(
269
+ # "my-restored-database",
270
+ # instance_id: "other-instance"
271
+ # )
272
+ #
273
+ # job.done? #=> false
274
+ # job.reload! # API call
275
+ # job.done? #=> true
276
+ #
277
+ # if job.error?
278
+ # status = job.error
279
+ # else
280
+ # database = job.database
281
+ # end
282
+ #
283
+ def restore database_id, instance_id: nil
284
+ ensure_service!
285
+
286
+ instance_id ||= self.instance_id
287
+
288
+ grpc = service.restore_database \
289
+ self.instance_id,
290
+ backup_id,
291
+ instance_id,
292
+ database_id
293
+ Restore::Job.from_grpc grpc, service
294
+ end
295
+
296
+ ##
297
+ # @private
298
+ # Creates a new Backup instance from a
299
+ # {Google::Spanner::Admin::Database::V1::Backup}.
300
+ def self.from_grpc grpc, service
301
+ new grpc, service
302
+ end
303
+
304
+ protected
305
+
306
+ ##
307
+ # @private Raise an error unless an active connection to the service is
308
+ # available.
309
+ def ensure_service!
310
+ raise "Must have active connection to service" unless service
311
+ end
312
+ end
313
+ end
314
+ end
315
+ end
@@ -0,0 +1,274 @@
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/status"
17
+ require "google/cloud/spanner/backup/job/list"
18
+
19
+ module Google
20
+ module Cloud
21
+ module Spanner
22
+ class Backup
23
+ ##
24
+ # # Job
25
+ #
26
+ # A resource representing the long-running, asynchronous processing of
27
+ # backup creation. The job can be refreshed to retrieve the backup
28
+ # object once the operation has been completed.
29
+ #
30
+ # See {Google::Cloud::Spanner::Database#create_backup}
31
+ #
32
+ # @see https://cloud.google.com/spanner/reference/rpc/google.longrunning#google.longrunning.Operation
33
+ # Long-running Operation
34
+ #
35
+ # @example
36
+ # require "google/cloud/spanner"
37
+ #
38
+ # spanner = Google::Cloud::Spanner.new
39
+ #
40
+ # database = spanner.database "my-instance", "my-database"
41
+ # expire_time = Time.now + 36000
42
+ # job = database.create_backup "my-backup", expire_time: expire_time
43
+ #
44
+ # job.done? #=> false
45
+ # job.reload! # API call
46
+ # job.done? #=> true
47
+ #
48
+ # if job.error?
49
+ # status = job.error
50
+ # else
51
+ # backup = job.backup
52
+ # end
53
+ #
54
+ class Job
55
+ ##
56
+ # @private The Google::Gax::Operation gRPC object.
57
+ attr_accessor :grpc
58
+
59
+ ##
60
+ # @private The gRPC Service object.
61
+ attr_accessor :service
62
+
63
+ ##
64
+ # @private Creates a new Backup::Job instance.
65
+ def initialize
66
+ @grpc = nil
67
+ @service = nil
68
+ end
69
+
70
+ ##
71
+ # The backup is the object of the operation.
72
+ #
73
+ # @return [Backup, nil] The backup, or
74
+ # `nil` if the operation is not complete.
75
+ #
76
+ # @example
77
+ # require "google/cloud/spanner"
78
+ #
79
+ # spanner = Google::Cloud::Spanner.new
80
+ #
81
+ # database = spanner.database "my-instance", "my-database"
82
+ # expire_time = Time.now + 36000
83
+ # job = database.create_backup "my-backup", expire_time: expire_time
84
+ #
85
+ # job.done? #=> false
86
+ # job.reload!
87
+ # job.done? #=> true
88
+ # backup = job.backup
89
+ #
90
+ def backup
91
+ return nil unless done?
92
+ return nil unless @grpc.grpc_op.result == :response
93
+ Backup.from_grpc @grpc.results, service
94
+ end
95
+
96
+ ##
97
+ # Checks if the processing of the backup operation is complete.
98
+ #
99
+ # @return [boolean] `true` when complete, `false` otherwise.
100
+ #
101
+ # @example
102
+ # require "google/cloud/spanner"
103
+ #
104
+ # spanner = Google::Cloud::Spanner.new
105
+ #
106
+ # database = spanner.database "my-instance", "my-database"
107
+ # expire_time = Time.now + 36000
108
+ # job = database.create_backup "my-backup", expire_time: expire_time
109
+ #
110
+ # job.done? #=> false
111
+ #
112
+ def done?
113
+ @grpc.done?
114
+ end
115
+
116
+ ##
117
+ # Checks if the processing of the backup operation has errored.
118
+ #
119
+ # @return [boolean] `true` when errored, `false` otherwise.
120
+ #
121
+ # @example
122
+ # require "google/cloud/spanner"
123
+ #
124
+ # spanner = Google::Cloud::Spanner.new
125
+ #
126
+ # database = spanner.database "my-instance", "my-database"
127
+ # expire_time = Time.now + 36000
128
+ # job = database.create_backup "my-backup", expire_time: expire_time
129
+ #
130
+ # job.error? #=> false
131
+ #
132
+ def error?
133
+ @grpc.error?
134
+ end
135
+
136
+ ##
137
+ # The status if the operation associated with this job produced an
138
+ # error.
139
+ #
140
+ # @return [Google::Cloud::Spanner::Status, nil] A status object with
141
+ # the status code and message, or `nil` if no error occurred.
142
+ #
143
+ # @example
144
+ # require "google/cloud/spanner"
145
+ #
146
+ # spanner = Google::Cloud::Spanner.new
147
+ #
148
+ # database = spanner.database "my-instance", "my-database"
149
+ # expire_time = Time.now + 36000
150
+ # job = database.create_backup "my-backup", expire_time: expire_time
151
+ #
152
+ # job.error? # true
153
+ #
154
+ # error = job.error
155
+ #
156
+ def error
157
+ return nil unless error?
158
+ Google::Cloud::Spanner::Status.from_grpc @grpc.error
159
+ end
160
+
161
+ ##
162
+ # Reloads the job with current data from the long-running,
163
+ # asynchronous processing of a backup operation.
164
+ #
165
+ # @return [Google::Cloud::Spanner::Backup::Job] The same job
166
+ # instance.
167
+ #
168
+ # @example
169
+ # require "google/cloud/spanner"
170
+ #
171
+ # spanner = Google::Cloud::Spanner.new
172
+ #
173
+ # database = spanner.database "my-instance", "my-database"
174
+ # expire_time = Time.now + 36000
175
+ # job = database.create_backup "my-backup", expire_time: expire_time
176
+ #
177
+ # job.done? #=> false
178
+ # job.reload! # API call
179
+ # job.done? #=> true
180
+ #
181
+ def reload!
182
+ @grpc.reload!
183
+ self
184
+ end
185
+ alias refresh! reload!
186
+
187
+ ##
188
+ # Reloads the job until the operation is complete. The delay between
189
+ # reloads will incrementally increase.
190
+ #
191
+ # @example
192
+ # require "google/cloud/spanner"
193
+ #
194
+ # spanner = Google::Cloud::Spanner.new
195
+ #
196
+ # database = spanner.database "my-instance", "my-database"
197
+ # expire_time = Time.now + 36000
198
+ # job = database.create_backup "my-backup", expire_time: expire_time
199
+ #
200
+ # job.done? #=> false
201
+ # job.wait_until_done!
202
+ # job.done? #=> true
203
+ #
204
+ def wait_until_done!
205
+ @grpc.wait_until_done!
206
+ end
207
+
208
+ ##
209
+ # Cancel the backup job.
210
+ #
211
+ # @example
212
+ # require "google/cloud/spanner"
213
+ #
214
+ # spanner = Google::Cloud::Spanner.new
215
+ #
216
+ # database = spanner.database "my-instance", "my-database"
217
+ # expire_time = Time.now + 36000
218
+ # job = database.create_backup "my-backup", expire_time: expire_time
219
+ #
220
+ # job.done? #=> false
221
+ # job.cancel
222
+ #
223
+ def cancel
224
+ @grpc.cancel
225
+ end
226
+
227
+ ##
228
+ # The operation progress in percentage.
229
+ #
230
+ # @return [Integer]
231
+ def progress_percent
232
+ @grpc.metadata.progress.progress_percent
233
+ end
234
+
235
+ ##
236
+ # The operation start time.
237
+ #
238
+ # @return [Time, nil]
239
+ def start_time
240
+ return nil unless @grpc.metadata.progress.start_time
241
+ Convert.timestamp_to_time @grpc.metadata.progress.start_time
242
+ end
243
+
244
+ ##
245
+ # The operation end time.
246
+ #
247
+ # @return [Time, nil]
248
+ def end_time
249
+ return nil unless @grpc.metadata.progress.end_time
250
+ Convert.timestamp_to_time @grpc.metadata.progress.end_time
251
+ end
252
+
253
+ ##
254
+ # The operation canceled time.
255
+ #
256
+ # @return [Time, nil]
257
+ def cancel_time
258
+ return nil unless @grpc.metadata.cancel_time
259
+ Convert.timestamp_to_time @grpc.metadata.cancel_time
260
+ end
261
+
262
+ ##
263
+ # @private New Backup::Job from a Google::Gax::Operation object.
264
+ def self.from_grpc grpc, service
265
+ new.tap do |job|
266
+ job.instance_variable_set :@grpc, grpc
267
+ job.instance_variable_set :@service, service
268
+ end
269
+ end
270
+ end
271
+ end
272
+ end
273
+ end
274
+ end