google-cloud-spanner 1.15.0 → 1.16.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,177 @@
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 "delegate"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Spanner
21
+ class Backup
22
+ class Job
23
+ ##
24
+ # # List
25
+ #
26
+ # List is a special case Array with additional values for backup
27
+ # operations.
28
+ #
29
+ class List < DelegateClass(::Array)
30
+ # @private
31
+ # The gRPC Service object.
32
+ attr_accessor :service
33
+
34
+ # @private
35
+ # The gRPC page enumerable object.
36
+ attr_accessor :grpc
37
+
38
+ ##
39
+ # @private Create a new Backup::Job::List with an array of
40
+ # Google::Lognrunning::Operation instances.
41
+ def initialize arr = []
42
+ super arr
43
+ end
44
+
45
+ ##
46
+ # Whether there is a next page of backup jobs.
47
+ #
48
+ # @return [Boolean]
49
+ #
50
+ # @example
51
+ # require "google/cloud/spanner"
52
+ #
53
+ # spanner = Google::Cloud::Spanner.new
54
+ #
55
+ # instance = spanner.instance "my-instance"
56
+ #
57
+ # jobs = instance.backup_operations
58
+ # if jobs.next?
59
+ # next_jobs = jobs.next
60
+ # end
61
+ #
62
+ def next?
63
+ grpc.next_page?
64
+ end
65
+
66
+ ##
67
+ # Retrieve the next page of backup jobs.
68
+ #
69
+ # @return [Backup::Job::List]
70
+ #
71
+ # @example
72
+ # require "google/cloud/spanner"
73
+ #
74
+ # spanner = Google::Cloud::Spanner.new
75
+ #
76
+ # instance = spanner.instance "my-instance"
77
+ #
78
+ # jobs = instance.backup_operations
79
+ # if jobs.next?
80
+ # next_jobs = jobs.next
81
+ # end
82
+ #
83
+ def next
84
+ ensure_service!
85
+
86
+ return nil unless next?
87
+ grpc.next_page
88
+ self.class.from_grpc grpc, service
89
+ end
90
+
91
+ ##
92
+ # Retrieves remaining results by repeatedly invoking {#next} until
93
+ # {#next?} returns `false`. Calls the given block once for each
94
+ # result, which is passed as the argument to the block.
95
+ #
96
+ # An Enumerator is returned if no block is given.
97
+ #
98
+ # This method will make repeated API calls until all remaining
99
+ # results are retrieved. (Unlike `#each`, for example, which merely
100
+ # iterates over the results returned by a single API call.) Use with
101
+ # caution.
102
+ #
103
+ # @yield [job] The block for accessing each backup job.
104
+ # @yieldparam [Google::Cloud::Spanner::Backup::Job] job The backup
105
+ # job object.
106
+ #
107
+ # @return [Enumerator]
108
+ #
109
+ # @example Iterating each backup job by passing a block:
110
+ # require "google/cloud/spanner"
111
+ #
112
+ # spanner = Google::Cloud::Spanner.new
113
+ #
114
+ # instance = spanner.instance "my-instance"
115
+ #
116
+ # jobs = instance.backup_operations
117
+ # jobs.all do |job|
118
+ # puts job.backup.backup_id
119
+ # end
120
+ #
121
+ # @example Using the enumerator by not passing a block:
122
+ # require "google/cloud/spanner"
123
+ #
124
+ # spanner = Google::Cloud::Spanner.new
125
+ #
126
+ # instance = spanner.instance "my-instance"
127
+ #
128
+ # jobs = instance.backup_operations
129
+ # all_backup_ids = jobs.all.map do |job|
130
+ # job.backup.backup_id
131
+ # end
132
+ #
133
+ def all
134
+ return enum_for :all unless block_given?
135
+
136
+ results = self
137
+ loop do
138
+ results.each { |r| yield r }
139
+ break unless next?
140
+ grpc.next_page
141
+ results = self.class.from_grpc grpc, service
142
+ end
143
+ end
144
+
145
+ ##
146
+ # @private
147
+ #
148
+ # New Backup::Job::List from a
149
+ # Google::Gax::PagedEnumerable<Google::Longrunning::Operation>
150
+ # object. Operation object is a backup operation.
151
+ #
152
+ def self.from_grpc grpc, service
153
+ operations_client = \
154
+ service.databases.instance_variable_get "@operations_client"
155
+ jobs = new(Array(grpc.response.operations).map do |job_grpc|
156
+ Job.from_grpc \
157
+ Google::Gax::Operation.new(job_grpc, operations_client),
158
+ service
159
+ end)
160
+ jobs.grpc = grpc
161
+ jobs.service = service
162
+ jobs
163
+ end
164
+
165
+ protected
166
+
167
+ ##
168
+ # Raise an error unless an active service is available.
169
+ def ensure_service!
170
+ raise "Must have active connection" unless @service
171
+ end
172
+ end
173
+ end
174
+ end
175
+ end
176
+ end
177
+ end
@@ -0,0 +1,170 @@
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 "delegate"
17
+
18
+ module Google
19
+ module Cloud
20
+ module Spanner
21
+ class Backup
22
+ ##
23
+ # # List
24
+ #
25
+ # Google::Cloud::Spanner::Backup::List is a special case Array with
26
+ # additional values.
27
+ #
28
+ class List < DelegateClass(::Array)
29
+ # @private
30
+ # The gRPC Service object.
31
+ attr_accessor :service
32
+
33
+ # @private
34
+ # The gRPC page enumerable object.
35
+ attr_accessor :grpc
36
+
37
+ ##
38
+ # @private Create a new Backup::List with an array of
39
+ # Backup instances.
40
+ def initialize arr = []
41
+ super arr
42
+ end
43
+
44
+ ##
45
+ # Whether there is a next page of backups.
46
+ #
47
+ # @return [Boolean]
48
+ #
49
+ # @example
50
+ # require "google/cloud/spanner"
51
+ #
52
+ # spanner = Google::Cloud::Spanner.new
53
+ #
54
+ # instance = spanner.instance "my-instance"
55
+ # backups = instance.backups
56
+ #
57
+ # if backups.next?
58
+ # next_backups = backups.next
59
+ # end
60
+ #
61
+ def next?
62
+ grpc.next_page?
63
+ end
64
+
65
+ ##
66
+ # Retrieve the next page of backups.
67
+ #
68
+ # @return [Google::Cloud::Spanner::Backup::List]
69
+ #
70
+ # @example
71
+ # require "google/cloud/spanner"
72
+ #
73
+ # spanner = Google::Cloud::Spanner.new
74
+ #
75
+ # instance = spanner.instance "my-instance"
76
+ # backups = instance.backups
77
+
78
+ # if backups.next?
79
+ # next_backups = backups.next
80
+ # end
81
+ #
82
+ def next
83
+ ensure_service!
84
+
85
+ return nil unless next?
86
+ grpc.next_page
87
+ self.class.from_grpc grpc, service
88
+ end
89
+
90
+ ##
91
+ # Retrieves remaining results by repeatedly invoking {#next} until
92
+ # {#next?} returns `false`. Calls the given block once for each
93
+ # result, which is passed as the argument to the block.
94
+ #
95
+ # An Enumerator is returned if no block is given.
96
+ #
97
+ # This method will make repeated API calls until all remaining results
98
+ # are retrieved. (Unlike `#each`, for example, which merely iterates
99
+ # over the results returned by a single API call.) Use with caution.
100
+ #
101
+ # @yield [backup] The block for accessing each backup.
102
+ # @yieldparam [Google::Cloud::Spanner::Backup] backup The backup
103
+ # object.
104
+ #
105
+ # @return [Enumerator]
106
+ #
107
+ # @example Iterating each backup by passing a block:
108
+ # require "google/cloud/spanner"
109
+ #
110
+ # spanner = Google::Cloud::Spanner.new
111
+ #
112
+ # instance = spanner.instance "my-instance"
113
+ # backups = instance.backups
114
+ #
115
+ # backups.all do |backup|
116
+ # puts backup.backup_id
117
+ # end
118
+ #
119
+ # @example Using the enumerator by not passing a block:
120
+ # require "google/cloud/spanner"
121
+ #
122
+ # spanner = Google::Cloud::Spanner.new
123
+ #
124
+ # instance = spanner.instance "my-instance"
125
+ # backups = instance.backups
126
+ #
127
+ # all_backup_ids = backups.all.map do |backup|
128
+ # backup.backup_id
129
+ # end
130
+ #
131
+ def all
132
+ return enum_for :all unless block_given?
133
+
134
+ results = self
135
+ loop do
136
+ results.each { |r| yield r }
137
+ break unless next?
138
+ grpc.next_page
139
+ results = self.class.from_grpc grpc, service
140
+ end
141
+ end
142
+
143
+ ##
144
+ # @private
145
+ # New Backup::List from a
146
+ # Google::Gax::PagedEnumerable<Google::Spanner::Admin::Database::\
147
+ # V1::Backup>
148
+ # object.
149
+ def self.from_grpc grpc, service
150
+ backups = List.new(Array(grpc.response.backups).map do |backup|
151
+ Backup.from_grpc backup, service
152
+ end)
153
+
154
+ backups.grpc = grpc
155
+ backups.service = service
156
+ backups
157
+ end
158
+
159
+ protected
160
+
161
+ ##
162
+ # Raise an error unless an active service is available.
163
+ def ensure_service!
164
+ raise "Must have active connection" unless @service
165
+ end
166
+ end
167
+ end
168
+ end
169
+ end
170
+ end
@@ -0,0 +1,246 @@
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
+
18
+ module Google
19
+ module Cloud
20
+ module Spanner
21
+ class Backup
22
+ class Restore
23
+ ##
24
+ # # Job
25
+ #
26
+ # A resource representing the long-running, asynchronous processing of
27
+ # a backup restore. The job can be refreshed to retrieve the restored
28
+ # database object once the operation has been completed.
29
+ #
30
+ # See {Backup#restore}
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
+ # instance = spanner.instance "my-instance"
41
+ # backup = instance.backup "my-backup"
42
+ # job = backup.restore "my-restored-database"
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
+ # database = job.database
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 Restore::Job instance.
65
+ def initialize
66
+ @grpc = nil
67
+ @service = nil
68
+ end
69
+
70
+ ##
71
+ # The database is the object of the operation.
72
+ #
73
+ # @return [Database, nil] The database instance, 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
+ # instance = spanner.instance "my-instance"
82
+ # backup = instance.backup "my-backup"
83
+ # job = backup.restore "my-restored-database"
84
+ #
85
+ # job.done? #=> false
86
+ # job.reload!
87
+ # job.done? #=> true
88
+ # database = job.database
89
+ #
90
+ def database
91
+ return nil unless done?
92
+ return nil unless @grpc.grpc_op.result == :response
93
+ Database.from_grpc @grpc.results, service
94
+ end
95
+
96
+ ##
97
+ # Checks if the processing of the restore 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
+ # instance = spanner.instance "my-instance"
107
+ # backup = instance.backup "my-backup"
108
+ # job = backup.restore "my-restored-database"
109
+ #
110
+ # job.done? #=> false
111
+ #
112
+ def done?
113
+ @grpc.done?
114
+ end
115
+
116
+ ##
117
+ # Checks if the processing of the restore 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
+ # instance = spanner.instance "my-instance"
127
+ # backup = instance.backup "my-backup"
128
+ # job = backup.restore "my-restored-database"
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
+ # instance = spanner.instance "my-instance"
149
+ # backup = instance.backup "my-backup"
150
+ # job = backup.restore "my-restored-database"
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 restore operation.
164
+ #
165
+ # @return [Backup::Job] The same job instance.
166
+ #
167
+ # @example
168
+ # require "google/cloud/spanner"
169
+ #
170
+ # spanner = Google::Cloud::Spanner.new
171
+ #
172
+ # instance = spanner.instance "my-instance"
173
+ # backup = instance.backup "my-backup"
174
+ # job = backup.restore "my-restored-database"
175
+ #
176
+ # job.done? #=> false
177
+ # job.reload! # API call
178
+ # job.done? #=> true
179
+ #
180
+ def reload!
181
+ @grpc.reload!
182
+ self
183
+ end
184
+ alias refresh! reload!
185
+
186
+ ##
187
+ # Reloads the job until the operation is complete. The delay between
188
+ # reloads will incrementally increase.
189
+ #
190
+ # @example
191
+ # require "google/cloud/spanner"
192
+ #
193
+ # spanner = Google::Cloud::Spanner.new
194
+ #
195
+ # instance = spanner.instance "my-instance"
196
+ # backup = instance.backup "my-backup"
197
+ # job = backup.restore "my-restored-database"
198
+ #
199
+ # job.done? #=> false
200
+ # job.wait_until_done!
201
+ # job.done? #=> true
202
+ #
203
+ def wait_until_done!
204
+ @grpc.wait_until_done!
205
+ end
206
+
207
+ ##
208
+ # The operation progress in percentage.
209
+ #
210
+ # @return [Integer]
211
+ def progress_percent
212
+ @grpc.metadata.progress.progress_percent
213
+ end
214
+
215
+ ##
216
+ # The operation start time.
217
+ #
218
+ # @return [Time, nil]
219
+ def start_time
220
+ return nil unless @grpc.metadata.progress.start_time
221
+ Convert.timestamp_to_time @grpc.metadata.progress.start_time
222
+ end
223
+
224
+ ##
225
+ # The operation end time.
226
+ #
227
+ # @return [Time, nil]
228
+ def end_time
229
+ return nil unless @grpc.metadata.progress.end_time
230
+ Convert.timestamp_to_time @grpc.metadata.progress.end_time
231
+ end
232
+
233
+ ##
234
+ # @private New Restore::Job from a Google::Gax::Operation object.
235
+ def self.from_grpc grpc, service
236
+ new.tap do |job|
237
+ job.instance_variable_set :@grpc, grpc
238
+ job.instance_variable_set :@service, service
239
+ end
240
+ end
241
+ end
242
+ end
243
+ end
244
+ end
245
+ end
246
+ end