gcloud 0.12.2 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. checksums.yaml +5 -13
  2. data/lib/gcloud.rb +27 -456
  3. data/lib/gcloud/bigquery.rb +2 -382
  4. data/lib/gcloud/datastore.rb +2 -576
  5. data/lib/gcloud/dns.rb +2 -321
  6. data/lib/gcloud/logging.rb +1 -322
  7. data/lib/gcloud/pubsub.rb +2 -476
  8. data/lib/gcloud/resource_manager.rb +2 -273
  9. data/lib/gcloud/storage.rb +2 -440
  10. data/lib/gcloud/translate.rb +1 -250
  11. data/lib/gcloud/version.rb +2 -2
  12. data/lib/gcloud/vision.rb +1 -501
  13. metadata +36 -332
  14. data/AUTHENTICATION.md +0 -75
  15. data/CHANGELOG.md +0 -382
  16. data/OVERVIEW.md +0 -259
  17. data/lib/gcloud/backoff.rb +0 -150
  18. data/lib/gcloud/bigquery/copy_job.rb +0 -97
  19. data/lib/gcloud/bigquery/credentials.rb +0 -29
  20. data/lib/gcloud/bigquery/data.rb +0 -239
  21. data/lib/gcloud/bigquery/dataset.rb +0 -753
  22. data/lib/gcloud/bigquery/dataset/access.rb +0 -507
  23. data/lib/gcloud/bigquery/dataset/list.rb +0 -169
  24. data/lib/gcloud/bigquery/extract_job.rb +0 -117
  25. data/lib/gcloud/bigquery/insert_response.rb +0 -81
  26. data/lib/gcloud/bigquery/job.rb +0 -299
  27. data/lib/gcloud/bigquery/job/list.rb +0 -172
  28. data/lib/gcloud/bigquery/load_job.rb +0 -202
  29. data/lib/gcloud/bigquery/project.rb +0 -475
  30. data/lib/gcloud/bigquery/query_data.rb +0 -234
  31. data/lib/gcloud/bigquery/query_job.rb +0 -137
  32. data/lib/gcloud/bigquery/schema.rb +0 -359
  33. data/lib/gcloud/bigquery/service.rb +0 -506
  34. data/lib/gcloud/bigquery/table.rb +0 -1141
  35. data/lib/gcloud/bigquery/table/list.rb +0 -180
  36. data/lib/gcloud/bigquery/view.rb +0 -475
  37. data/lib/gcloud/credentials.rb +0 -129
  38. data/lib/gcloud/datastore/commit.rb +0 -148
  39. data/lib/gcloud/datastore/credentials.rb +0 -35
  40. data/lib/gcloud/datastore/cursor.rb +0 -76
  41. data/lib/gcloud/datastore/dataset.rb +0 -660
  42. data/lib/gcloud/datastore/dataset/lookup_results.rb +0 -219
  43. data/lib/gcloud/datastore/dataset/query_results.rb +0 -386
  44. data/lib/gcloud/datastore/entity.rb +0 -449
  45. data/lib/gcloud/datastore/errors.rb +0 -41
  46. data/lib/gcloud/datastore/gql_query.rb +0 -211
  47. data/lib/gcloud/datastore/grpc_utils.rb +0 -132
  48. data/lib/gcloud/datastore/key.rb +0 -281
  49. data/lib/gcloud/datastore/properties.rb +0 -128
  50. data/lib/gcloud/datastore/query.rb +0 -348
  51. data/lib/gcloud/datastore/service.rb +0 -167
  52. data/lib/gcloud/datastore/transaction.rb +0 -362
  53. data/lib/gcloud/dns/change.rb +0 -158
  54. data/lib/gcloud/dns/change/list.rb +0 -173
  55. data/lib/gcloud/dns/credentials.rb +0 -29
  56. data/lib/gcloud/dns/importer.rb +0 -183
  57. data/lib/gcloud/dns/project.rb +0 -247
  58. data/lib/gcloud/dns/record.rb +0 -170
  59. data/lib/gcloud/dns/record/list.rb +0 -174
  60. data/lib/gcloud/dns/service.rb +0 -167
  61. data/lib/gcloud/dns/zone.rb +0 -759
  62. data/lib/gcloud/dns/zone/list.rb +0 -168
  63. data/lib/gcloud/dns/zone/transaction.rb +0 -176
  64. data/lib/gcloud/errors.rb +0 -206
  65. data/lib/gcloud/gce.rb +0 -56
  66. data/lib/gcloud/grpc_utils.rb +0 -87
  67. data/lib/gcloud/logging/credentials.rb +0 -29
  68. data/lib/gcloud/logging/entry.rb +0 -465
  69. data/lib/gcloud/logging/entry/http_request.rb +0 -141
  70. data/lib/gcloud/logging/entry/list.rb +0 -177
  71. data/lib/gcloud/logging/entry/operation.rb +0 -90
  72. data/lib/gcloud/logging/logger.rb +0 -307
  73. data/lib/gcloud/logging/metric.rb +0 -169
  74. data/lib/gcloud/logging/metric/list.rb +0 -172
  75. data/lib/gcloud/logging/project.rb +0 -642
  76. data/lib/gcloud/logging/resource.rb +0 -84
  77. data/lib/gcloud/logging/resource_descriptor.rb +0 -137
  78. data/lib/gcloud/logging/resource_descriptor/list.rb +0 -174
  79. data/lib/gcloud/logging/service.rb +0 -267
  80. data/lib/gcloud/logging/sink.rb +0 -227
  81. data/lib/gcloud/logging/sink/list.rb +0 -171
  82. data/lib/gcloud/pubsub/credentials.rb +0 -29
  83. data/lib/gcloud/pubsub/message.rb +0 -94
  84. data/lib/gcloud/pubsub/policy.rb +0 -204
  85. data/lib/gcloud/pubsub/project.rb +0 -482
  86. data/lib/gcloud/pubsub/received_message.rb +0 -160
  87. data/lib/gcloud/pubsub/service.rb +0 -334
  88. data/lib/gcloud/pubsub/subscription.rb +0 -565
  89. data/lib/gcloud/pubsub/subscription/list.rb +0 -208
  90. data/lib/gcloud/pubsub/topic.rb +0 -511
  91. data/lib/gcloud/pubsub/topic/list.rb +0 -174
  92. data/lib/gcloud/pubsub/topic/publisher.rb +0 -85
  93. data/lib/gcloud/resource_manager/credentials.rb +0 -30
  94. data/lib/gcloud/resource_manager/manager.rb +0 -266
  95. data/lib/gcloud/resource_manager/policy.rb +0 -211
  96. data/lib/gcloud/resource_manager/project.rb +0 -484
  97. data/lib/gcloud/resource_manager/project/list.rb +0 -167
  98. data/lib/gcloud/resource_manager/project/updater.rb +0 -130
  99. data/lib/gcloud/resource_manager/service.rb +0 -127
  100. data/lib/gcloud/storage/bucket.rb +0 -775
  101. data/lib/gcloud/storage/bucket/acl.rb +0 -810
  102. data/lib/gcloud/storage/bucket/cors.rb +0 -153
  103. data/lib/gcloud/storage/bucket/list.rb +0 -172
  104. data/lib/gcloud/storage/credentials.rb +0 -29
  105. data/lib/gcloud/storage/errors.rb +0 -65
  106. data/lib/gcloud/storage/file.rb +0 -842
  107. data/lib/gcloud/storage/file/acl.rb +0 -425
  108. data/lib/gcloud/storage/file/list.rb +0 -191
  109. data/lib/gcloud/storage/file/verifier.rb +0 -67
  110. data/lib/gcloud/storage/project.rb +0 -316
  111. data/lib/gcloud/storage/service.rb +0 -347
  112. data/lib/gcloud/translate/api.rb +0 -241
  113. data/lib/gcloud/translate/detection.rb +0 -137
  114. data/lib/gcloud/translate/language.rb +0 -69
  115. data/lib/gcloud/translate/service.rb +0 -80
  116. data/lib/gcloud/translate/translation.rb +0 -112
  117. data/lib/gcloud/vision/annotate.rb +0 -224
  118. data/lib/gcloud/vision/annotation.rb +0 -455
  119. data/lib/gcloud/vision/annotation/entity.rb +0 -234
  120. data/lib/gcloud/vision/annotation/face.rb +0 -1750
  121. data/lib/gcloud/vision/annotation/properties.rb +0 -245
  122. data/lib/gcloud/vision/annotation/safe_search.rb +0 -161
  123. data/lib/gcloud/vision/annotation/text.rb +0 -236
  124. data/lib/gcloud/vision/annotation/vertex.rb +0 -108
  125. data/lib/gcloud/vision/credentials.rb +0 -29
  126. data/lib/gcloud/vision/image.rb +0 -590
  127. data/lib/gcloud/vision/location.rb +0 -115
  128. data/lib/gcloud/vision/project.rb +0 -278
  129. data/lib/gcloud/vision/service.rb +0 -66
  130. data/lib/google/api/annotations.rb +0 -14
  131. data/lib/google/api/http.rb +0 -30
  132. data/lib/google/api/label.rb +0 -24
  133. data/lib/google/api/monitored_resource.rb +0 -25
  134. data/lib/google/datastore/v1beta3/datastore.rb +0 -115
  135. data/lib/google/datastore/v1beta3/datastore_services.rb +0 -33
  136. data/lib/google/datastore/v1beta3/entity.rb +0 -63
  137. data/lib/google/datastore/v1beta3/query.rb +0 -128
  138. data/lib/google/devtools/cloudtrace/v1/trace.rb +0 -78
  139. data/lib/google/devtools/cloudtrace/v1/trace_services.rb +0 -32
  140. data/lib/google/example/library/v1/library.rb +0 -91
  141. data/lib/google/example/library/v1/library_services.rb +0 -40
  142. data/lib/google/iam/v1/iam_policy.rb +0 -33
  143. data/lib/google/iam/v1/iam_policy_services.rb +0 -30
  144. data/lib/google/iam/v1/policy.rb +0 -25
  145. data/lib/google/logging/type/http_request.rb +0 -28
  146. data/lib/google/logging/type/log_severity.rb +0 -27
  147. data/lib/google/logging/v2/log_entry.rb +0 -44
  148. data/lib/google/logging/v2/logging.rb +0 -56
  149. data/lib/google/logging/v2/logging_config.rb +0 -59
  150. data/lib/google/logging/v2/logging_config_services.rb +0 -32
  151. data/lib/google/logging/v2/logging_metrics.rb +0 -51
  152. data/lib/google/logging/v2/logging_metrics_services.rb +0 -32
  153. data/lib/google/logging/v2/logging_services.rb +0 -31
  154. data/lib/google/longrunning/operations.rb +0 -50
  155. data/lib/google/longrunning/operations_services.rb +0 -29
  156. data/lib/google/protobuf/descriptor.rb +0 -0
  157. data/lib/google/pubsub/v1/pubsub.rb +0 -129
  158. data/lib/google/pubsub/v1/pubsub_services.rb +0 -56
  159. data/lib/google/pubsub/v1beta2/pubsub.rb +0 -126
  160. data/lib/google/pubsub/v1beta2/pubsub_services.rb +0 -56
  161. data/lib/google/rpc/code.rb +0 -32
  162. data/lib/google/rpc/error_details.rb +0 -61
  163. data/lib/google/rpc/status.rb +0 -19
  164. data/lib/google/type/color.rb +0 -20
  165. data/lib/google/type/date.rb +0 -18
  166. data/lib/google/type/dayofweek.rb +0 -23
  167. data/lib/google/type/latlng.rb +0 -17
  168. data/lib/google/type/money.rb +0 -18
  169. data/lib/google/type/timeofday.rb +0 -19
@@ -1,506 +0,0 @@
1
- # Copyright 2015 Google Inc. All rights reserved.
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
- # http://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 "gcloud/version"
17
- require "gcloud/errors"
18
- require "google/apis/bigquery_v2"
19
- require "pathname"
20
- require "digest/md5"
21
- require "mime/types"
22
-
23
- module Gcloud
24
- module Bigquery
25
- ##
26
- # @private Represents the Bigquery service and API calls.
27
- class Service
28
- ##
29
- # Alias to the Google Client API module
30
- API = Google::Apis::BigqueryV2
31
-
32
- # @private
33
- attr_accessor :project
34
-
35
- # @private
36
- attr_accessor :credentials
37
-
38
- ##
39
- # Creates a new Service instance.
40
- def initialize project, credentials, retries: nil, timeout: nil
41
- @project = project
42
- @credentials = credentials
43
- @credentials = credentials
44
- @service = API::BigqueryService.new
45
- @service.client_options.application_name = "gcloud-ruby"
46
- @service.client_options.application_version = Gcloud::VERSION
47
- @service.request_options.retries = retries || 3
48
- @service.request_options.timeout_sec = timeout if timeout
49
- @service.authorization = @credentials.client
50
- end
51
-
52
- def service
53
- return mocked_service if mocked_service
54
- @service
55
- end
56
- attr_accessor :mocked_service
57
-
58
- ##
59
- # Lists all datasets in the specified project to which you have
60
- # been granted the READER dataset role.
61
- def list_datasets options = {}
62
- service.list_datasets \
63
- @project, all: options[:all], max_results: options[:max],
64
- page_token: options[:token]
65
- rescue Google::Apis::Error => e
66
- raise Gcloud::Error.from_error(e)
67
- end
68
-
69
- ##
70
- # Returns the dataset specified by datasetID.
71
- def get_dataset dataset_id
72
- service.get_dataset @project, dataset_id
73
- rescue Google::Apis::Error => e
74
- raise Gcloud::Error.from_error(e)
75
- end
76
-
77
- ##
78
- # Creates a new empty dataset.
79
- def insert_dataset new_dataset_gapi
80
- service.insert_dataset @project, new_dataset_gapi
81
- rescue Google::Apis::Error => e
82
- raise Gcloud::Error.from_error(e)
83
- end
84
-
85
- ##
86
- # Updates information in an existing dataset, only replacing
87
- # fields that are provided in the submitted dataset resource.
88
- def patch_dataset dataset_id, patched_dataset_gapi
89
- service.patch_dataset @project, dataset_id, patched_dataset_gapi
90
- rescue Google::Apis::Error => e
91
- raise Gcloud::Error.from_error(e)
92
- end
93
-
94
- ##
95
- # Deletes the dataset specified by the datasetId value.
96
- # Before you can delete a dataset, you must delete all its tables,
97
- # either manually or by specifying force: true in options.
98
- # Immediately after deletion, you can create another dataset with
99
- # the same name.
100
- def delete_dataset dataset_id, force = nil
101
- service.delete_dataset @project, dataset_id, delete_contents: force
102
- rescue Google::Apis::Error => e
103
- raise Gcloud::Error.from_error(e)
104
- end
105
-
106
- ##
107
- # Lists all tables in the specified dataset.
108
- # Requires the READER dataset role.
109
- def list_tables dataset_id, options = {}
110
- service.list_tables @project, dataset_id, max_results: options[:max],
111
- page_token: options[:token]
112
- rescue Google::Apis::Error => e
113
- raise Gcloud::Error.from_error(e)
114
- end
115
-
116
- def get_project_table project_id, dataset_id, table_id
117
- service.get_table project_id, dataset_id, table_id
118
- rescue Google::Apis::Error => e
119
- raise Gcloud::Error.from_error(e)
120
- end
121
-
122
- ##
123
- # Gets the specified table resource by table ID.
124
- # This method does not return the data in the table,
125
- # it only returns the table resource,
126
- # which describes the structure of this table.
127
- def get_table dataset_id, table_id
128
- get_project_table @project, dataset_id, table_id
129
- rescue Google::Apis::Error => e
130
- raise Gcloud::Error.from_error(e)
131
- end
132
-
133
- ##
134
- # Creates a new, empty table in the dataset.
135
- def insert_table dataset_id, new_table_gapi
136
- service.insert_table @project, dataset_id, new_table_gapi
137
- rescue Google::Apis::Error => e
138
- raise Gcloud::Error.from_error(e)
139
- end
140
-
141
- ##
142
- # Updates information in an existing table, replacing fields that
143
- # are provided in the submitted table resource.
144
- def patch_table dataset_id, table_id, patched_table_gapi
145
- service.patch_table @project, dataset_id, table_id, patched_table_gapi
146
- rescue Google::Apis::Error => e
147
- raise Gcloud::Error.from_error(e)
148
- end
149
-
150
- ##
151
- # Deletes the table specified by tableId from the dataset.
152
- # If the table contains data, all the data will be deleted.
153
- def delete_table dataset_id, table_id
154
- service.delete_table @project, dataset_id, table_id
155
- rescue Google::Apis::Error => e
156
- raise Gcloud::Error.from_error(e)
157
- end
158
-
159
- ##
160
- # Retrieves data from the table.
161
- def list_tabledata dataset_id, table_id, options = {}
162
- service.list_table_data @project, dataset_id, table_id,
163
- max_results: options.delete(:max),
164
- page_token: options.delete(:token),
165
- start_index: options.delete(:start)
166
- rescue Google::Apis::Error => e
167
- raise Gcloud::Error.from_error(e)
168
- end
169
-
170
- def insert_tabledata dataset_id, table_id, rows, options = {}
171
- insert_rows = Array(rows).map do |row|
172
- Google::Apis::BigqueryV2::InsertAllTableDataRequest::Row.new(
173
- insert_id: Digest::MD5.base64digest(row.inspect),
174
- # Hash[row.map{|(k,v)| [k.to_s,v]}] for Hash<String,Object>
175
- json: row
176
- )
177
- end
178
- insert_req = Google::Apis::BigqueryV2::InsertAllTableDataRequest.new(
179
- rows: insert_rows,
180
- ignore_unknown_values: options[:ignore_unknown],
181
- skip_invalid_rows: options[:skip_invalid]
182
- )
183
-
184
- service.insert_all_table_data @project, dataset_id, table_id, insert_req
185
- rescue Google::Apis::Error => e
186
- raise Gcloud::Error.from_error(e)
187
- end
188
-
189
- ##
190
- # Lists all jobs in the specified project to which you have
191
- # been granted the READER job role.
192
- def list_jobs options = {}
193
- service.list_jobs \
194
- @project, all_users: options[:all], max_results: options[:max],
195
- page_token: options[:token], projection: "full",
196
- state_filter: options[:filter]
197
- rescue Google::Apis::Error => e
198
- raise Gcloud::Error.from_error(e)
199
- end
200
-
201
- ##
202
- # Returns the job specified by jobID.
203
- def get_job job_id
204
- service.get_job @project, job_id
205
- rescue Google::Apis::Error => e
206
- raise Gcloud::Error.from_error(e)
207
- end
208
-
209
- def insert_job config
210
- job_object = API::Job.new(
211
- configuration: config
212
- )
213
- service.insert_job @project, job_object
214
- rescue Google::Apis::Error => e
215
- raise Gcloud::Error.from_error(e)
216
- end
217
-
218
- def query_job query, options = {}
219
- config = query_table_config(query, options)
220
- service.insert_job @project, config
221
- rescue Google::Apis::Error => e
222
- raise Gcloud::Error.from_error(e)
223
- end
224
-
225
- def query query, options = {}
226
- service.query_job @project, query_config(query, options)
227
- rescue Google::Apis::Error => e
228
- raise Gcloud::Error.from_error(e)
229
- end
230
-
231
- ##
232
- # Returns the query data for the job
233
- def job_query_results job_id, options = {}
234
- service.get_job_query_results @project,
235
- job_id,
236
- max_results: options.delete(:max),
237
- page_token: options.delete(:token),
238
- start_index: options.delete(:start),
239
- timeout_ms: options.delete(:timeout)
240
- rescue Google::Apis::Error => e
241
- raise Gcloud::Error.from_error(e)
242
- end
243
-
244
- def copy_table source, target, options = {}
245
- service.insert_job @project, copy_table_config(source, target, options)
246
- rescue Google::Apis::Error => e
247
- raise Gcloud::Error.from_error(e)
248
- end
249
-
250
- def extract_table table, storage_files, options = {}
251
- service.insert_job \
252
- @project, extract_table_config(table, storage_files, options)
253
- rescue Google::Apis::Error => e
254
- raise Gcloud::Error.from_error(e)
255
- end
256
-
257
- def load_table_gs_url dataset_id, table_id, url, options = {}
258
- service.insert_job \
259
- @project, load_table_url_config(dataset_id, table_id, url, options)
260
- rescue Google::Apis::Error => e
261
- raise Gcloud::Error.from_error(e)
262
- end
263
-
264
- def load_table_file dataset_id, table_id, file, options = {}
265
- service.insert_job \
266
- @project, load_table_file_config(dataset_id, table_id, file, options),
267
- upload_source: file, content_type: mime_type_for(file)
268
- rescue Google::Apis::Error => e
269
- raise Gcloud::Error.from_error(e)
270
- end
271
-
272
- ##
273
- # Extracts at least `tbl` group, and possibly `dts` and `prj` groups,
274
- # from strings in the formats: "my_table", "my_dataset.my_table", or
275
- # "my-project:my_dataset.my_table". Then merges project_id and
276
- # dataset_id from the default table if they are missing.
277
- def self.table_ref_from_s str, default_table_ref
278
- str = str.to_s
279
- m = /\A(((?<prj>\S*):)?(?<dts>\S*)\.)?(?<tbl>\S*)\z/.match str
280
- unless m
281
- fail ArgumentError, "unable to identify table from #{str.inspect}"
282
- end
283
- str_table_ref_hash = {
284
- project_id: m["prj"],
285
- dataset_id: m["dts"],
286
- table_id: m["tbl"]
287
- }.delete_if { |_, v| v.nil? }
288
- new_table_ref_hash = default_table_ref.to_h.merge str_table_ref_hash
289
- Google::Apis::BigqueryV2::TableReference.new new_table_ref_hash
290
- end
291
-
292
- def inspect
293
- "#{self.class}(#{@project})"
294
- end
295
-
296
- protected
297
-
298
- def table_ref_from tbl
299
- return nil if tbl.nil?
300
- API::TableReference.new(
301
- project_id: tbl.project_id,
302
- dataset_id: tbl.dataset_id,
303
- table_id: tbl.table_id
304
- )
305
- end
306
-
307
- def dataset_ref_from dts, pjt = nil
308
- return nil if dts.nil?
309
- if dts.respond_to? :dataset_id
310
- API::DatasetReference.new(
311
- project_id: (pjt || dts.project_id || @project),
312
- dataset_id: dts.dataset_id
313
- )
314
- else
315
- API::DatasetReference.new(
316
- project_id: (pjt || @project),
317
- dataset_id: dts
318
- )
319
- end
320
- end
321
-
322
- def load_table_file_opts dataset_id, table_id, file, options = {}
323
- path = Pathname(file).to_path
324
- {
325
- destination_table: Google::Apis::BigqueryV2::TableReference.new(
326
- project_id: @project, dataset_id: dataset_id, table_id: table_id),
327
- create_disposition: create_disposition(options[:create]),
328
- write_disposition: write_disposition(options[:write]),
329
- source_format: source_format(path, options[:format]),
330
- projection_fields: projection_fields(options[:projection_fields]),
331
- allow_jagged_rows: options[:jagged_rows],
332
- allow_quoted_newlines: options[:quoted_newlines],
333
- encoding: options[:encoding], field_delimiter: options[:delimiter],
334
- ignore_unknown_values: options[:ignore_unknown],
335
- max_bad_records: options[:max_bad_records], quote: options[:quote],
336
- schema: options[:schema], skip_leading_rows: options[:skip_leading]
337
- }.delete_if { |_, v| v.nil? }
338
- end
339
-
340
- def load_table_file_config dataset_id, table_id, file, options = {}
341
- load_opts = load_table_file_opts dataset_id, table_id, file, options
342
- API::Job.new(
343
- configuration: API::JobConfiguration.new(
344
- load: API::JobConfigurationLoad.new(load_opts),
345
- dry_run: options[:dryrun]
346
- )
347
- )
348
- end
349
-
350
- def load_table_url_opts dataset_id, table_id, url, options = {}
351
- {
352
- destination_table: Google::Apis::BigqueryV2::TableReference.new(
353
- project_id: @project, dataset_id: dataset_id, table_id: table_id),
354
- source_uris: Array(url),
355
- create_disposition: create_disposition(options[:create]),
356
- write_disposition: write_disposition(options[:write]),
357
- source_format: source_format(url, options[:format]),
358
- projection_fields: projection_fields(options[:projection_fields]),
359
- allow_jagged_rows: options[:jagged_rows],
360
- allow_quoted_newlines: options[:quoted_newlines],
361
- encoding: options[:encoding], field_delimiter: options[:delimiter],
362
- ignore_unknown_values: options[:ignore_unknown],
363
- max_bad_records: options[:max_bad_records], quote: options[:quote],
364
- schema: options[:schema], skip_leading_rows: options[:skip_leading]
365
- }.delete_if { |_, v| v.nil? }
366
- end
367
-
368
- def load_table_url_config dataset_id, table_id, url, options = {}
369
- load_opts = load_table_url_opts dataset_id, table_id, url, options
370
- API::Job.new(
371
- configuration: API::JobConfiguration.new(
372
- load: API::JobConfigurationLoad.new(load_opts),
373
- dry_run: options[:dryrun]
374
- )
375
- )
376
- end
377
-
378
- ##
379
- # Job description for query job
380
- def query_table_config query, options
381
- dest_table = table_ref_from options[:table]
382
- default_dataset = dataset_ref_from options[:dataset]
383
- API::Job.new(
384
- configuration: API::JobConfiguration.new(
385
- query: API::JobConfigurationQuery.new(
386
- query: query,
387
- # tableDefinitions: { ... },
388
- priority: priority_value(options[:priority]),
389
- use_query_cache: options[:cache],
390
- destination_table: dest_table,
391
- create_disposition: create_disposition(options[:create]),
392
- write_disposition: write_disposition(options[:write]),
393
- allow_large_results: options[:large_results],
394
- flatten_results: options[:flatten],
395
- default_dataset: default_dataset
396
- )
397
- )
398
- )
399
- end
400
-
401
- def query_config query, options = {}
402
- dataset_config = dataset_ref_from options[:dataset], options[:project]
403
-
404
- API::QueryRequest.new(
405
- query: query,
406
- max_results: options[:max],
407
- default_dataset: dataset_config,
408
- timeout_ms: options[:timeout],
409
- dry_run: options[:dryrun],
410
- use_query_cache: options[:cache]
411
- )
412
- end
413
-
414
- ##
415
- # Job description for copy job
416
- def copy_table_config source, target, options = {}
417
- API::Job.new(
418
- configuration: API::JobConfiguration.new(
419
- copy: API::JobConfigurationTableCopy.new(
420
- source_table: source,
421
- destination_table: target,
422
- create_disposition: create_disposition(options[:create]),
423
- write_disposition: write_disposition(options[:write])
424
- ),
425
- dry_run: options[:dryrun]
426
- )
427
- )
428
- end
429
-
430
- def extract_table_config table, storage_files, options = {}
431
- storage_urls = Array(storage_files).map do |url|
432
- url.respond_to?(:to_gs_url) ? url.to_gs_url : url
433
- end
434
- dest_format = source_format storage_urls.first, options[:format]
435
- API::Job.new(
436
- configuration: API::JobConfiguration.new(
437
- extract: API::JobConfigurationExtract.new(
438
- destination_uris: Array(storage_urls),
439
- source_table: table,
440
- destination_format: dest_format,
441
- compression: options[:compression],
442
- field_delimiter: options[:delimiter],
443
- print_header: options[:header]
444
- ),
445
- dry_run: options[:dryrun]
446
- )
447
- )
448
- end
449
-
450
- def create_disposition str
451
- { "create_if_needed" => "CREATE_IF_NEEDED",
452
- "createifneeded" => "CREATE_IF_NEEDED",
453
- "if_needed" => "CREATE_IF_NEEDED",
454
- "needed" => "CREATE_IF_NEEDED",
455
- "create_never" => "CREATE_NEVER",
456
- "createnever" => "CREATE_NEVER",
457
- "never" => "CREATE_NEVER" }[str.to_s.downcase]
458
- end
459
-
460
- def write_disposition str
461
- { "write_truncate" => "WRITE_TRUNCATE",
462
- "writetruncate" => "WRITE_TRUNCATE",
463
- "truncate" => "WRITE_TRUNCATE",
464
- "write_append" => "WRITE_APPEND",
465
- "writeappend" => "WRITE_APPEND",
466
- "append" => "WRITE_APPEND",
467
- "write_empty" => "WRITE_EMPTY",
468
- "writeempty" => "WRITE_EMPTY",
469
- "empty" => "WRITE_EMPTY" }[str.to_s.downcase]
470
- end
471
-
472
- def priority_value str
473
- { "batch" => "BATCH",
474
- "interactive" => "INTERACTIVE" }[str.to_s.downcase]
475
- end
476
-
477
- def source_format path, format
478
- val = { "csv" => "CSV",
479
- "json" => "NEWLINE_DELIMITED_JSON",
480
- "newline_delimited_json" => "NEWLINE_DELIMITED_JSON",
481
- "avro" => "AVRO",
482
- "datastore" => "DATASTORE_BACKUP",
483
- "datastore_backup" => "DATASTORE_BACKUP" }[format.to_s.downcase]
484
- return val unless val.nil?
485
- return nil if path.nil?
486
- return "CSV" if path.end_with? ".csv"
487
- return "NEWLINE_DELIMITED_JSON" if path.end_with? ".json"
488
- return "AVRO" if path.end_with? ".avro"
489
- return "DATASTORE_BACKUP" if path.end_with? ".backup_info"
490
- nil
491
- end
492
-
493
- def projection_fields array_or_str
494
- Array(array_or_str) unless array_or_str.nil?
495
- end
496
-
497
- def mime_type_for file
498
- mime_type = MIME::Types.of(Pathname(file).to_path).first.to_s
499
- return nil if mime_type.empty?
500
- mime_type
501
- rescue
502
- nil
503
- end
504
- end
505
- end
506
- end