gcloud 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +8 -8
  2. data/AUTHENTICATION.md +3 -3
  3. data/CHANGELOG.md +92 -0
  4. data/OVERVIEW.md +3 -3
  5. data/lib/gcloud.rb +75 -25
  6. data/lib/gcloud/backoff.rb +5 -1
  7. data/lib/gcloud/bigquery.rb +25 -43
  8. data/lib/gcloud/bigquery/copy_job.rb +13 -13
  9. data/lib/gcloud/bigquery/data.rb +20 -16
  10. data/lib/gcloud/bigquery/dataset.rb +202 -177
  11. data/lib/gcloud/bigquery/dataset/access.rb +118 -104
  12. data/lib/gcloud/bigquery/dataset/list.rb +14 -18
  13. data/lib/gcloud/bigquery/extract_job.rb +12 -12
  14. data/lib/gcloud/bigquery/insert_response.rb +12 -14
  15. data/lib/gcloud/bigquery/job.rb +45 -57
  16. data/lib/gcloud/bigquery/job/list.rb +18 -24
  17. data/lib/gcloud/bigquery/load_job.rb +35 -27
  18. data/lib/gcloud/bigquery/project.rb +53 -73
  19. data/lib/gcloud/bigquery/query_data.rb +28 -35
  20. data/lib/gcloud/bigquery/query_job.rb +18 -18
  21. data/lib/gcloud/bigquery/schema.rb +359 -0
  22. data/lib/gcloud/bigquery/service.rb +506 -0
  23. data/lib/gcloud/bigquery/table.rb +185 -266
  24. data/lib/gcloud/bigquery/table/list.rb +15 -19
  25. data/lib/gcloud/bigquery/view.rb +126 -81
  26. data/lib/gcloud/datastore.rb +39 -27
  27. data/lib/gcloud/datastore/commit.rb +2 -2
  28. data/lib/gcloud/datastore/dataset.rb +8 -19
  29. data/lib/gcloud/datastore/dataset/lookup_results.rb +2 -4
  30. data/lib/gcloud/datastore/dataset/query_results.rb +0 -2
  31. data/lib/gcloud/datastore/entity.rb +7 -1
  32. data/lib/gcloud/datastore/errors.rb +5 -27
  33. data/lib/gcloud/datastore/grpc_utils.rb +4 -3
  34. data/lib/gcloud/datastore/key.rb +6 -0
  35. data/lib/gcloud/datastore/service.rb +18 -12
  36. data/lib/gcloud/datastore/transaction.rb +0 -10
  37. data/lib/gcloud/dns.rb +29 -19
  38. data/lib/gcloud/dns/change.rb +10 -15
  39. data/lib/gcloud/dns/change/list.rb +4 -4
  40. data/lib/gcloud/dns/importer.rb +1 -1
  41. data/lib/gcloud/dns/project.rb +32 -49
  42. data/lib/gcloud/dns/record.rb +8 -2
  43. data/lib/gcloud/dns/record/list.rb +4 -4
  44. data/lib/gcloud/dns/service.rb +167 -0
  45. data/lib/gcloud/dns/zone.rb +33 -52
  46. data/lib/gcloud/dns/zone/list.rb +12 -16
  47. data/lib/gcloud/errors.rb +31 -19
  48. data/lib/gcloud/logging.rb +50 -39
  49. data/lib/gcloud/logging/entry.rb +197 -24
  50. data/lib/gcloud/logging/entry/list.rb +0 -2
  51. data/lib/gcloud/logging/logger.rb +1 -1
  52. data/lib/gcloud/logging/metric.rb +3 -9
  53. data/lib/gcloud/logging/metric/list.rb +0 -2
  54. data/lib/gcloud/logging/project.rb +58 -54
  55. data/lib/gcloud/logging/resource_descriptor.rb +2 -2
  56. data/lib/gcloud/logging/resource_descriptor/list.rb +0 -2
  57. data/lib/gcloud/logging/service.rb +32 -23
  58. data/lib/gcloud/logging/sink.rb +8 -14
  59. data/lib/gcloud/logging/sink/list.rb +0 -2
  60. data/lib/gcloud/pubsub.rb +21 -16
  61. data/lib/gcloud/pubsub/policy.rb +204 -0
  62. data/lib/gcloud/pubsub/project.rb +26 -38
  63. data/lib/gcloud/pubsub/service.rb +39 -31
  64. data/lib/gcloud/pubsub/subscription.rb +56 -59
  65. data/lib/gcloud/pubsub/subscription/list.rb +4 -4
  66. data/lib/gcloud/pubsub/topic.rb +69 -66
  67. data/lib/gcloud/pubsub/topic/list.rb +0 -2
  68. data/lib/gcloud/pubsub/topic/{batch.rb → publisher.rb} +15 -2
  69. data/lib/gcloud/resource_manager.rb +27 -26
  70. data/lib/gcloud/resource_manager/manager.rb +19 -39
  71. data/lib/gcloud/resource_manager/policy.rb +211 -0
  72. data/lib/gcloud/resource_manager/project.rb +97 -121
  73. data/lib/gcloud/resource_manager/project/list.rb +7 -7
  74. data/lib/gcloud/resource_manager/project/updater.rb +4 -9
  75. data/lib/gcloud/resource_manager/service.rb +127 -0
  76. data/lib/gcloud/storage.rb +24 -42
  77. data/lib/gcloud/storage/bucket.rb +104 -192
  78. data/lib/gcloud/storage/bucket/acl.rb +47 -143
  79. data/lib/gcloud/storage/bucket/cors.rb +55 -11
  80. data/lib/gcloud/storage/bucket/list.rb +14 -14
  81. data/lib/gcloud/storage/errors.rb +3 -43
  82. data/lib/gcloud/storage/file.rb +114 -111
  83. data/lib/gcloud/storage/file/acl.rb +27 -113
  84. data/lib/gcloud/storage/file/list.rb +21 -21
  85. data/lib/gcloud/storage/project.rb +49 -59
  86. data/lib/gcloud/storage/service.rb +347 -0
  87. data/lib/gcloud/translate.rb +24 -14
  88. data/lib/gcloud/translate/api.rb +12 -21
  89. data/lib/gcloud/translate/detection.rb +5 -5
  90. data/lib/gcloud/translate/language.rb +1 -1
  91. data/lib/gcloud/translate/service.rb +80 -0
  92. data/lib/gcloud/translate/translation.rb +6 -6
  93. data/lib/gcloud/version.rb +1 -1
  94. data/lib/gcloud/vision.rb +24 -15
  95. data/lib/gcloud/vision/annotate.rb +24 -21
  96. data/lib/gcloud/vision/annotation.rb +9 -9
  97. data/lib/gcloud/vision/annotation/entity.rb +11 -11
  98. data/lib/gcloud/vision/annotation/face.rb +25 -25
  99. data/lib/gcloud/vision/annotation/properties.rb +8 -8
  100. data/lib/gcloud/vision/annotation/safe_search.rb +4 -4
  101. data/lib/gcloud/vision/annotation/text.rb +7 -7
  102. data/lib/gcloud/vision/annotation/vertex.rb +1 -1
  103. data/lib/gcloud/vision/image.rb +11 -11
  104. data/lib/gcloud/vision/location.rb +5 -2
  105. data/lib/gcloud/vision/project.rb +14 -16
  106. data/lib/gcloud/vision/service.rb +66 -0
  107. data/lib/google/api_client.rb +0 -0
  108. metadata +27 -24
  109. data/lib/gcloud/bigquery/connection.rb +0 -624
  110. data/lib/gcloud/bigquery/errors.rb +0 -68
  111. data/lib/gcloud/bigquery/table/schema.rb +0 -234
  112. data/lib/gcloud/dns/connection.rb +0 -173
  113. data/lib/gcloud/dns/errors.rb +0 -68
  114. data/lib/gcloud/resource_manager/connection.rb +0 -134
  115. data/lib/gcloud/resource_manager/errors.rb +0 -68
  116. data/lib/gcloud/storage/connection.rb +0 -444
  117. data/lib/gcloud/translate/connection.rb +0 -85
  118. data/lib/gcloud/translate/errors.rb +0 -68
  119. data/lib/gcloud/upload.rb +0 -95
  120. data/lib/gcloud/vision/connection.rb +0 -63
  121. data/lib/gcloud/vision/errors.rb +0 -69
@@ -78,14 +78,10 @@ module Gcloud
78
78
  #
79
79
  def next
80
80
  return nil unless next?
81
- ensure_connection!
81
+ ensure_service!
82
82
  options = { token: token, max: @max }
83
- resp = @connection.list_tables @dataset_id, options
84
- if resp.success?
85
- self.class.from_response resp, @connection, @dataset_id, @max
86
- else
87
- fail ApiError.from_response(resp)
88
- end
83
+ gapi = @service.list_tables @dataset_id, options
84
+ self.class.from_gapi gapi, @service, @dataset_id, @max
89
85
  end
90
86
 
91
87
  ##
@@ -158,25 +154,25 @@ module Gcloud
158
154
 
159
155
  ##
160
156
  # @private New Table::List from a response object.
161
- def self.from_response resp, conn, dataset_id = nil, max = nil
162
- tables = List.new(Array(resp.data["tables"]).map do |gapi_object|
163
- Table.from_gapi gapi_object, conn
157
+ def self.from_gapi gapi_list, service, dataset_id = nil, max = nil
158
+ tables = List.new(Array(gapi_list.tables).map do |gapi_object|
159
+ Table.from_gapi gapi_object, service
164
160
  end)
165
- tables.instance_variable_set "@token", resp.data["nextPageToken"]
166
- tables.instance_variable_set "@etag", resp.data["etag"]
167
- tables.instance_variable_set "@total", resp.data["totalItems"]
168
- tables.instance_variable_set "@connection", conn
169
- tables.instance_variable_set "@dataset_id", dataset_id
170
- tables.instance_variable_set "@max", max
161
+ tables.instance_variable_set :@token, gapi_list.next_page_token
162
+ tables.instance_variable_set :@etag, gapi_list.etag
163
+ tables.instance_variable_set :@total, gapi_list.total_items
164
+ tables.instance_variable_set :@service, service
165
+ tables.instance_variable_set :@dataset_id, dataset_id
166
+ tables.instance_variable_set :@max, max
171
167
  tables
172
168
  end
173
169
 
174
170
  protected
175
171
 
176
172
  ##
177
- # Raise an error unless an active connection is available.
178
- def ensure_connection!
179
- fail "Must have active connection" unless @connection
173
+ # Raise an error unless an active service is available.
174
+ def ensure_service!
175
+ fail "Must have active connection" unless @service
180
176
  end
181
177
  end
182
178
  end
@@ -13,9 +13,11 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
+ require "gcloud/errors"
17
+ require "gcloud/bigquery/service"
16
18
  require "gcloud/bigquery/data"
17
19
  require "gcloud/bigquery/table/list"
18
- require "gcloud/bigquery/errors"
20
+ require "google/apis/bigquery_v2"
19
21
 
20
22
  module Gcloud
21
23
  module Bigquery
@@ -41,8 +43,8 @@ module Gcloud
41
43
  #
42
44
  class View
43
45
  ##
44
- # @private The Connection object.
45
- attr_accessor :connection
46
+ # @private The Service object.
47
+ attr_accessor :service
46
48
 
47
49
  ##
48
50
  # @private The Google API Client object.
@@ -51,8 +53,8 @@ module Gcloud
51
53
  ##
52
54
  # @private Create an empty Table object.
53
55
  def initialize
54
- @connection = nil
55
- @gapi = {}
56
+ @service = nil
57
+ @gapi = Google::Apis::BigqueryV2::Table.new
56
58
  end
57
59
 
58
60
  ##
@@ -63,7 +65,7 @@ module Gcloud
63
65
  # @!group Attributes
64
66
  #
65
67
  def table_id
66
- @gapi["tableReference"]["tableId"]
68
+ @gapi.table_reference.table_id
67
69
  end
68
70
 
69
71
  ##
@@ -72,7 +74,7 @@ module Gcloud
72
74
  # @!group Attributes
73
75
  #
74
76
  def dataset_id
75
- @gapi["tableReference"]["datasetId"]
77
+ @gapi.table_reference.dataset_id
76
78
  end
77
79
 
78
80
  ##
@@ -81,7 +83,7 @@ module Gcloud
81
83
  # @!group Attributes
82
84
  #
83
85
  def project_id
84
- @gapi["tableReference"]["projectId"]
86
+ @gapi.table_reference.project_id
85
87
  end
86
88
 
87
89
  ##
@@ -89,27 +91,63 @@ module Gcloud
89
91
  # The gapi fragment containing the Project ID, Dataset ID, and Table ID as
90
92
  # a camel-cased hash.
91
93
  def table_ref
92
- table_ref = @gapi["tableReference"]
94
+ table_ref = @gapi.table_reference
93
95
  table_ref = table_ref.to_hash if table_ref.respond_to? :to_hash
94
96
  table_ref
95
97
  end
96
98
 
99
+ ##
100
+ # The combined Project ID, Dataset ID, and Table ID for this table, in the
101
+ # format specified by the [Query
102
+ # Reference](https://cloud.google.com/bigquery/query-reference#from):
103
+ # `project_name:datasetId.tableId`. To use this value in queries see
104
+ # {#query_id}.
105
+ #
106
+ # @!group Attributes
107
+ #
108
+ def id
109
+ @gapi.id
110
+ end
111
+
112
+ ##
113
+ # The value returned by {#id}, wrapped in square brackets if the Project
114
+ # ID contains dashes, as specified by the [Query
115
+ # Reference](https://cloud.google.com/bigquery/query-reference#from).
116
+ # Useful in queries.
117
+ #
118
+ # @example
119
+ # require "gcloud"
120
+ #
121
+ # gcloud = Gcloud.new
122
+ # bigquery = gcloud.bigquery
123
+ # dataset = bigquery.dataset "my_dataset"
124
+ # table = dataset.table "my_table"
125
+ #
126
+ # data = bigquery.query "SELECT name FROM #{table.query_id}"
127
+ #
128
+ # @!group Attributes
129
+ #
130
+ def query_id
131
+ project_id["-"] ? "[#{id}]" : id
132
+ end
133
+
97
134
  ##
98
135
  # The name of the table.
99
136
  #
100
137
  # @!group Attributes
101
138
  #
102
139
  def name
103
- @gapi["friendlyName"]
140
+ @gapi.friendly_name
104
141
  end
105
142
 
106
143
  ##
107
144
  # Updates the name of the table.
108
145
  #
109
- # @!group Lifecycle
146
+ # @!group Attributes
110
147
  #
111
148
  def name= new_name
112
- patch_gapi! name: new_name
149
+ @gapi.update! friendly_name: new_name
150
+ patch_gapi! :friendly_name
113
151
  end
114
152
 
115
153
  ##
@@ -119,7 +157,7 @@ module Gcloud
119
157
  #
120
158
  def etag
121
159
  ensure_full_data!
122
- @gapi["etag"]
160
+ @gapi.etag
123
161
  end
124
162
 
125
163
  ##
@@ -129,7 +167,7 @@ module Gcloud
129
167
  #
130
168
  def api_url
131
169
  ensure_full_data!
132
- @gapi["selfLink"]
170
+ @gapi.self_link
133
171
  end
134
172
 
135
173
  ##
@@ -139,16 +177,17 @@ module Gcloud
139
177
  #
140
178
  def description
141
179
  ensure_full_data!
142
- @gapi["description"]
180
+ @gapi.description
143
181
  end
144
182
 
145
183
  ##
146
184
  # Updates the description of the table.
147
185
  #
148
- # @!group Lifecycle
186
+ # @!group Attributes
149
187
  #
150
188
  def description= new_description
151
- patch_gapi! description: new_description
189
+ @gapi.update! description: new_description
190
+ patch_gapi! :description
152
191
  end
153
192
 
154
193
  ##
@@ -158,7 +197,11 @@ module Gcloud
158
197
  #
159
198
  def created_at
160
199
  ensure_full_data!
161
- Time.at(@gapi["creationTime"] / 1000.0)
200
+ begin
201
+ Time.at(Integer(@gapi.creation_time) / 1000.0)
202
+ rescue
203
+ nil
204
+ end
162
205
  end
163
206
 
164
207
  ##
@@ -170,8 +213,11 @@ module Gcloud
170
213
  #
171
214
  def expires_at
172
215
  ensure_full_data!
173
- return nil if @gapi["expirationTime"].nil?
174
- Time.at(@gapi["expirationTime"] / 1000.0)
216
+ begin
217
+ Time.at(Integer(@gapi.expiration_time) / 1000.0)
218
+ rescue
219
+ nil
220
+ end
175
221
  end
176
222
 
177
223
  ##
@@ -181,7 +227,11 @@ module Gcloud
181
227
  #
182
228
  def modified_at
183
229
  ensure_full_data!
184
- Time.at(@gapi["lastModifiedTime"] / 1000.0)
230
+ begin
231
+ Time.at(Integer(@gapi.last_modified_time) / 1000.0)
232
+ rescue
233
+ nil
234
+ end
185
235
  end
186
236
 
187
237
  ##
@@ -190,7 +240,7 @@ module Gcloud
190
240
  # @!group Attributes
191
241
  #
192
242
  def table?
193
- @gapi["type"] == "TABLE"
243
+ @gapi.type == "TABLE"
194
244
  end
195
245
 
196
246
  ##
@@ -199,7 +249,7 @@ module Gcloud
199
249
  # @!group Attributes
200
250
  #
201
251
  def view?
202
- @gapi["type"] == "VIEW"
252
+ @gapi.type == "VIEW"
203
253
  end
204
254
 
205
255
  ##
@@ -210,41 +260,35 @@ module Gcloud
210
260
  #
211
261
  def location
212
262
  ensure_full_data!
213
- @gapi["location"]
263
+ @gapi.location
214
264
  end
215
265
 
216
266
  ##
217
- # The schema of the table.
267
+ # The schema of the view.
218
268
  #
219
269
  # @!group Attributes
220
270
  #
221
271
  def schema
222
272
  ensure_full_data!
223
- s = @gapi["schema"]
224
- s = s.to_hash if s.respond_to? :to_hash
225
- s = {} if s.nil?
226
- s
273
+ Schema.from_gapi(@gapi.schema).freeze
227
274
  end
228
275
 
229
276
  ##
230
- # The fields of the table.
277
+ # The fields of the view.
231
278
  #
232
279
  # @!group Attributes
233
280
  #
234
281
  def fields
235
- f = schema["fields"]
236
- f = f.to_hash if f.respond_to? :to_hash
237
- f = [] if f.nil?
238
- f
282
+ schema.fields
239
283
  end
240
284
 
241
285
  ##
242
- # The names of the columns in the table.
286
+ # The names of the columns in the view.
243
287
  #
244
288
  # @!group Attributes
245
289
  #
246
290
  def headers
247
- fields.map { |f| f["name"] }
291
+ fields.map(&:name)
248
292
  end
249
293
 
250
294
  ##
@@ -253,7 +297,7 @@ module Gcloud
253
297
  # @!group Attributes
254
298
  #
255
299
  def query
256
- @gapi["view"]["query"] if @gapi["view"]
300
+ @gapi.view.query if @gapi.view
257
301
  end
258
302
 
259
303
  ##
@@ -277,7 +321,9 @@ module Gcloud
277
321
  # @!group Lifecycle
278
322
  #
279
323
  def query= new_query
280
- patch_gapi! query: new_query
324
+ @gapi.view ||= Google::Apis::BigqueryV2::ViewDefinition.new
325
+ @gapi.view.update! query: new_query
326
+ patch_view_gapi! :query
281
327
  end
282
328
 
283
329
  ##
@@ -323,16 +369,12 @@ module Gcloud
323
369
  #
324
370
  # @!group Data
325
371
  #
326
- def data max: nil, timeout: nil, cache: nil, dryrun: nil
327
- sql = "SELECT * FROM #{@gapi['id']}"
328
- ensure_connection!
372
+ def data max: nil, timeout: 10000, cache: true, dryrun: nil
373
+ sql = "SELECT * FROM #{query_id}"
374
+ ensure_service!
329
375
  options = { max: max, timeout: timeout, cache: cache, dryrun: dryrun }
330
- resp = connection.query sql, options
331
- if resp.success?
332
- QueryData.from_gapi resp.data, connection
333
- else
334
- fail ApiError.from_response(resp)
335
- end
376
+ gapi = service.query sql, options
377
+ QueryData.from_gapi gapi, service
336
378
  end
337
379
 
338
380
  ##
@@ -353,13 +395,9 @@ module Gcloud
353
395
  # @!group Lifecycle
354
396
  #
355
397
  def delete
356
- ensure_connection!
357
- resp = connection.delete_table dataset_id, table_id
358
- if resp.success?
359
- true
360
- else
361
- fail ApiError.from_response(resp)
362
- end
398
+ ensure_service!
399
+ service.delete_table dataset_id, table_id
400
+ true
363
401
  end
364
402
 
365
403
  ##
@@ -368,13 +406,9 @@ module Gcloud
368
406
  # @!group Lifecycle
369
407
  #
370
408
  def reload!
371
- ensure_connection!
372
- resp = connection.get_table dataset_id, table_id
373
- if resp.success?
374
- @gapi = resp.data
375
- else
376
- fail ApiError.from_response(resp)
377
- end
409
+ ensure_service!
410
+ gapi = service.get_table dataset_id, table_id
411
+ @gapi = gapi
378
412
  end
379
413
  alias_method :refresh!, :reload!
380
414
 
@@ -383,26 +417,41 @@ module Gcloud
383
417
  def self.from_gapi gapi, conn
384
418
  new.tap do |f|
385
419
  f.gapi = gapi
386
- f.connection = conn
420
+ f.service = conn
387
421
  end
388
422
  end
389
423
 
390
424
  protected
391
425
 
392
426
  ##
393
- # Raise an error unless an active connection is available.
394
- def ensure_connection!
395
- fail "Must have active connection" unless connection
427
+ # Raise an error unless an active service is available.
428
+ def ensure_service!
429
+ fail "Must have active connection" unless service
396
430
  end
397
431
 
398
- def patch_gapi! options = {}
399
- ensure_connection!
400
- resp = connection.patch_table dataset_id, table_id, options
401
- if resp.success?
402
- @gapi = resp.data
403
- else
404
- fail ApiError.from_response(resp)
405
- end
432
+ def patch_gapi! *attributes
433
+ return if attributes.empty?
434
+ patch_args = Hash[attributes.map do |attr|
435
+ [attr, @gapi.send(attr)]
436
+ end]
437
+ patch_table_gapi patch_args
438
+ end
439
+
440
+ def patch_view_gapi! *attributes
441
+ return if attributes.empty?
442
+ patch_args = Hash[attributes.map do |attr|
443
+ [attr, @gapi.view.send(attr)]
444
+ end]
445
+ patch_view_args = Google::Apis::BigqueryV2::ViewDefinition.new(
446
+ patch_args
447
+ )
448
+ patch_table_gapi view: patch_view_args
449
+ end
450
+
451
+ def patch_table_gapi patch_args
452
+ ensure_service!
453
+ patch_gapi = Google::Apis::BigqueryV2::Table.new patch_args
454
+ @gapi = service.patch_table dataset_id, table_id, patch_gapi
406
455
  end
407
456
 
408
457
  ##
@@ -413,17 +462,13 @@ module Gcloud
413
462
  end
414
463
 
415
464
  def reload_gapi!
416
- ensure_connection!
417
- resp = connection.get_table dataset_id, table_id
418
- if resp.success?
419
- @gapi = resp.data
420
- else
421
- fail ApiError.from_response(resp)
422
- end
465
+ ensure_service!
466
+ gapi = service.get_table dataset_id, table_id
467
+ @gapi = gapi
423
468
  end
424
469
 
425
470
  def data_complete?
426
- !@gapi["creationTime"].nil?
471
+ @gapi.is_a? Google::Apis::BigqueryV2::Table
427
472
  end
428
473
  end
429
474
  end