gcloud 0.11.0 → 0.12.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.
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
@@ -14,9 +14,9 @@
14
14
 
15
15
 
16
16
  require "gcloud/gce"
17
- require "gcloud/bigquery/connection"
17
+ require "gcloud/errors"
18
+ require "gcloud/bigquery/service"
18
19
  require "gcloud/bigquery/credentials"
19
- require "gcloud/bigquery/errors"
20
20
  require "gcloud/bigquery/dataset"
21
21
  require "gcloud/bigquery/job"
22
22
  require "gcloud/bigquery/query_data"
@@ -46,17 +46,15 @@ module Gcloud
46
46
  #
47
47
  class Project
48
48
  ##
49
- # @private The Connection object.
50
- attr_accessor :connection
49
+ # @private The Service object.
50
+ attr_accessor :service
51
51
 
52
52
  ##
53
- # Creates a new Connection instance.
53
+ # Creates a new Service instance.
54
54
  #
55
55
  # See {Gcloud.bigquery}
56
- def initialize project, credentials
57
- project = project.to_s # Always cast to a string
58
- fail ArgumentError, "project is missing" if project.empty?
59
- @connection = Connection.new project, credentials
56
+ def initialize service
57
+ @service = service
60
58
  end
61
59
 
62
60
  ##
@@ -71,7 +69,7 @@ module Gcloud
71
69
  # bigquery.project #=> "my-todo-project"
72
70
  #
73
71
  def project
74
- connection.project
72
+ service.project
75
73
  end
76
74
 
77
75
  ##
@@ -148,16 +146,12 @@ module Gcloud
148
146
  def query_job query, priority: "INTERACTIVE", cache: true, table: nil,
149
147
  create: nil, write: nil, large_results: nil, flatten: nil,
150
148
  dataset: nil
151
- ensure_connection!
149
+ ensure_service!
152
150
  options = { priority: priority, cache: cache, table: table,
153
151
  create: create, write: write, large_results: large_results,
154
152
  flatten: flatten, dataset: dataset }
155
- resp = connection.query_job query, options
156
- if resp.success?
157
- Job.from_gapi resp.data, connection
158
- else
159
- fail ApiError.from_response(resp)
160
- end
153
+ gapi = service.query_job query, options
154
+ Job.from_gapi gapi, service
161
155
  end
162
156
 
163
157
  ##
@@ -223,15 +217,11 @@ module Gcloud
223
217
  #
224
218
  def query query, max: nil, timeout: 10000, dryrun: nil, cache: true,
225
219
  dataset: nil, project: nil
226
- ensure_connection!
220
+ ensure_service!
227
221
  options = { max: max, timeout: timeout, dryrun: dryrun, cache: cache,
228
222
  dataset: dataset, project: project }
229
- resp = connection.query query, options
230
- if resp.success?
231
- QueryData.from_gapi resp.data, connection
232
- else
233
- fail ApiError.from_response(resp)
234
- end
223
+ gapi = service.query query, options
224
+ QueryData.from_gapi gapi, service
235
225
  end
236
226
 
237
227
  ##
@@ -252,14 +242,11 @@ module Gcloud
252
242
  # puts dataset.name
253
243
  #
254
244
  def dataset dataset_id
255
- ensure_connection!
256
- resp = connection.get_dataset dataset_id
257
- if resp.success?
258
- Dataset.from_gapi resp.data, connection
259
- else
260
- return nil if resp.status == 404
261
- fail ApiError.from_response(resp)
262
- end
245
+ ensure_service!
246
+ gapi = service.get_dataset dataset_id
247
+ Dataset.from_gapi gapi, service
248
+ rescue Gcloud::NotFoundError
249
+ nil
263
250
  end
264
251
 
265
252
  ##
@@ -273,11 +260,6 @@ module Gcloud
273
260
  # @param [Integer] expiration The default lifetime of all tables in the
274
261
  # dataset, in milliseconds. The minimum value is 3600000 milliseconds
275
262
  # (one hour).
276
- # @param [Array<Hash>] access The access rules for a Dataset using the
277
- # Google Cloud Datastore API data structure of an array of hashes. See
278
- # [BigQuery Access
279
- # Control](https://cloud.google.com/bigquery/access-control) for more
280
- # information.
281
263
  # @param [String] location The geographic location where the dataset
282
264
  # should reside. Possible values include `EU` and `US`. The default
283
265
  # value is `US`.
@@ -324,20 +306,29 @@ module Gcloud
324
306
  # end
325
307
  #
326
308
  def create_dataset dataset_id, name: nil, description: nil,
327
- expiration: nil, access: nil, location: nil
309
+ expiration: nil, location: nil
310
+ ensure_service!
311
+
312
+ new_ds = Google::Apis::BigqueryV2::Dataset.new(
313
+ dataset_reference: Google::Apis::BigqueryV2::DatasetReference.new(
314
+ project_id: project, dataset_id: dataset_id))
315
+
316
+ # Can set location only on creation, no Dataset#location method
317
+ new_ds.update! location: location unless location.nil?
318
+
319
+ updater = Dataset::Updater.new(new_ds).tap do |b|
320
+ b.name = name unless name.nil?
321
+ b.description = description unless description.nil?
322
+ b.default_expiration = expiration unless expiration.nil?
323
+ end
324
+
328
325
  if block_given?
329
- access_builder = Dataset::Access.new connection.default_access_rules,
330
- "projectId" => project
331
- yield access_builder
332
- access = access_builder.access if access_builder.changed?
326
+ yield updater
327
+ updater.check_for_mutated_access!
333
328
  end
334
329
 
335
- ensure_connection!
336
- options = { name: name, description: description,
337
- expiration: expiration, access: access, location: location }
338
- resp = connection.insert_dataset dataset_id, options
339
- return Dataset.from_gapi(resp.data, connection) if resp.success?
340
- fail ApiError.from_response(resp)
330
+ gapi = service.insert_dataset new_ds
331
+ Dataset.from_gapi gapi, service
341
332
  end
342
333
 
343
334
  ##
@@ -383,14 +374,10 @@ module Gcloud
383
374
  # end
384
375
  #
385
376
  def datasets all: nil, token: nil, max: nil
386
- ensure_connection!
377
+ ensure_service!
387
378
  options = { all: all, token: token, max: max }
388
- resp = connection.list_datasets options
389
- if resp.success?
390
- Dataset::List.from_response resp, connection, all, max
391
- else
392
- fail ApiError.from_response(resp)
393
- end
379
+ gapi = service.list_datasets options
380
+ Dataset::List.from_gapi gapi, service, all, max
394
381
  end
395
382
 
396
383
  ##
@@ -410,14 +397,11 @@ module Gcloud
410
397
  # job = bigquery.job "my_job"
411
398
  #
412
399
  def job job_id
413
- ensure_connection!
414
- resp = connection.get_job job_id
415
- if resp.success?
416
- Job.from_gapi resp.data, connection
417
- else
418
- return nil if resp.status == 404
419
- fail ApiError.from_response(resp)
420
- end
400
+ ensure_service!
401
+ gapi = service.get_job job_id
402
+ Job.from_gapi gapi, service
403
+ rescue Gcloud::NotFoundError
404
+ nil
421
405
  end
422
406
 
423
407
  ##
@@ -473,22 +457,18 @@ module Gcloud
473
457
  # end
474
458
  #
475
459
  def jobs all: nil, token: nil, max: nil, filter: nil
476
- ensure_connection!
460
+ ensure_service!
477
461
  options = { all: all, token: token, max: max, filter: filter }
478
- resp = connection.list_jobs options
479
- if resp.success?
480
- Job::List.from_response resp, connection, all, max, filter
481
- else
482
- fail ApiError.from_response(resp)
483
- end
462
+ gapi = service.list_jobs options
463
+ Job::List.from_gapi gapi, service, all, max, filter
484
464
  end
485
465
 
486
466
  protected
487
467
 
488
468
  ##
489
- # Raise an error unless an active connection is available.
490
- def ensure_connection!
491
- fail "Must have active connection" unless connection
469
+ # Raise an error unless an active service is available.
470
+ def ensure_service!
471
+ fail "Must have active connection" unless service
492
472
  end
493
473
  end
494
474
  end
@@ -13,6 +13,7 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
+ require "gcloud/bigquery/service"
16
17
  require "gcloud/bigquery/data"
17
18
 
18
19
  module Gcloud
@@ -23,8 +24,8 @@ module Gcloud
23
24
  # Represents Data returned from a query a a list of name/value pairs.
24
25
  class QueryData < Data
25
26
  ##
26
- # @private The Connection object.
27
- attr_accessor :connection
27
+ # @private The Service object.
28
+ attr_accessor :service
28
29
 
29
30
  # @private
30
31
  def initialize arr = []
@@ -34,33 +35,32 @@ module Gcloud
34
35
 
35
36
  # The total number of bytes processed for this query.
36
37
  def total_bytes
37
- @gapi["totalBytesProcessed"]
38
+ Integer @gapi.total_bytes_processed
39
+ rescue
40
+ nil
38
41
  end
39
42
 
40
43
  # Whether the query has completed or not. When data is present this will
41
44
  # always be `true`. When `false`, `total` will not be available.
42
45
  def complete?
43
- @gapi["jobComplete"]
46
+ @gapi.job_complete
44
47
  end
45
48
 
46
49
  # Whether the query result was fetched from the query cache.
47
50
  def cache_hit?
48
- @gapi["cacheHit"]
51
+ @gapi.cache_hit
49
52
  end
50
53
 
51
54
  ##
52
55
  # The schema of the data.
53
56
  def schema
54
- s = @gapi["schema"]
55
- s = s.to_hash if s.respond_to? :to_hash
56
- s = {} if s.nil?
57
- s
57
+ Schema.from_gapi(@gapi.schema).freeze
58
58
  end
59
59
 
60
60
  ##
61
61
  # The fields of the data.
62
62
  def fields
63
- f = schema["fields"]
63
+ f = schema.fields
64
64
  f = f.to_hash if f.respond_to? :to_hash
65
65
  f = [] if f.nil?
66
66
  f
@@ -69,7 +69,7 @@ module Gcloud
69
69
  ##
70
70
  # The name of the columns in the data.
71
71
  def headers
72
- fields.map { |f| f["name"] }
72
+ fields.map(&:name)
73
73
  end
74
74
 
75
75
  ##
@@ -112,13 +112,9 @@ module Gcloud
112
112
  #
113
113
  def next
114
114
  return nil unless next?
115
- ensure_connection!
116
- resp = connection.job_query_results job_id, token: token
117
- if resp.success?
118
- QueryData.from_gapi resp.data, connection
119
- else
120
- fail ApiError.from_response(resp)
121
- end
115
+ ensure_service!
116
+ gapi = service.job_query_results job_id, token: token
117
+ QueryData.from_gapi gapi, service
122
118
  end
123
119
 
124
120
  ##
@@ -195,29 +191,26 @@ module Gcloud
195
191
  def job
196
192
  return @job if @job
197
193
  return nil unless job?
198
- ensure_connection!
199
- resp = connection.get_job job_id
200
- if resp.success?
201
- @job = Job.from_gapi resp.data, connection
202
- else
203
- return nil if resp.status == 404
204
- fail ApiError.from_response(resp)
205
- end
194
+ ensure_service!
195
+ gapi = service.get_job job_id
196
+ @job = Job.from_gapi gapi, service
197
+ rescue Gcloud::NotFoundError
198
+ nil
206
199
  end
207
200
 
208
201
  ##
209
202
  # @private New Data from a response object.
210
- def self.from_gapi gapi, connection
211
- if gapi["schema"].nil?
203
+ def self.from_gapi gapi, service
204
+ if gapi.schema.nil?
212
205
  formatted_rows = []
213
206
  else
214
- formatted_rows = format_rows gapi["rows"],
215
- gapi["schema"]["fields"]
207
+ formatted_rows = format_rows gapi.rows,
208
+ gapi.schema.fields
216
209
  end
217
210
 
218
211
  data = new formatted_rows
219
212
  data.gapi = gapi
220
- data.connection = connection
213
+ data.service = service
221
214
  data
222
215
  end
223
216
 
@@ -225,16 +218,16 @@ module Gcloud
225
218
 
226
219
  ##
227
220
  # Raise an error unless an active connection is available.
228
- def ensure_connection!
229
- fail "Must have active connection" unless connection
221
+ def ensure_service!
222
+ fail "Must have active connection" unless service
230
223
  end
231
224
 
232
225
  def job?
233
- @gapi["jobReference"] && @gapi["jobReference"]["jobId"]
226
+ @gapi.job_reference && @gapi.job_reference.job_id
234
227
  end
235
228
 
236
229
  def job_id
237
- @gapi["jobReference"]["jobId"]
230
+ @gapi.job_reference.job_id
238
231
  end
239
232
  end
240
233
  end
@@ -13,6 +13,8 @@
13
13
  # limitations under the License.
14
14
 
15
15
 
16
+ require "gcloud/bigquery/service"
17
+
16
18
  module Gcloud
17
19
  module Bigquery
18
20
  ##
@@ -30,14 +32,14 @@ module Gcloud
30
32
  ##
31
33
  # Checks if the priority for the query is `BATCH`.
32
34
  def batch?
33
- val = config["query"]["priority"]
35
+ val = @gapi.configuration.query.priority
34
36
  val == "BATCH"
35
37
  end
36
38
 
37
39
  ##
38
40
  # Checks if the priority for the query is `INTERACTIVE`.
39
41
  def interactive?
40
- val = config["query"]["priority"]
42
+ val = @gapi.configuration.query.priority
41
43
  return true if val.nil?
42
44
  val == "INTERACTIVE"
43
45
  end
@@ -46,7 +48,7 @@ module Gcloud
46
48
  # Checks if the the query job allows arbitrarily large results at a slight
47
49
  # cost to performance.
48
50
  def large_results?
49
- val = config["query"]["allowLargeResults"]
51
+ val = @gapi.configuration.query.allow_large_results
50
52
  return false if val.nil?
51
53
  val
52
54
  end
@@ -56,7 +58,7 @@ module Gcloud
56
58
  # For more information, see [Query
57
59
  # Caching](https://cloud.google.com/bigquery/querying-data#querycaching).
58
60
  def cache?
59
- val = config["query"]["useQueryCache"]
61
+ val = @gapi.configuration.query.use_query_cache
60
62
  return false if val.nil?
61
63
  val
62
64
  end
@@ -66,7 +68,7 @@ module Gcloud
66
68
  # results. The default is `true`. If the value is `false`, #large_results?
67
69
  # should return `true`.
68
70
  def flatten?
69
- val = config["query"]["flattenResults"]
71
+ val = @gapi.configuration.query.flatten_results
70
72
  return true if val.nil?
71
73
  val
72
74
  end
@@ -74,23 +76,25 @@ module Gcloud
74
76
  ##
75
77
  # Checks if the query results are from the query cache.
76
78
  def cache_hit?
77
- stats["query"]["cacheHit"]
79
+ @gapi.statistics.query.cache_hit
78
80
  end
79
81
 
80
82
  ##
81
83
  # The number of bytes processed by the query.
82
84
  def bytes_processed
83
- stats["query"]["totalBytesProcessed"]
85
+ Integer @gapi.statistics.query.total_bytes_processed
86
+ rescue
87
+ nil
84
88
  end
85
89
 
86
90
  ##
87
91
  # The table in which the query results are stored.
88
92
  def destination
89
- table = config["query"]["destinationTable"]
93
+ table = @gapi.configuration.query.destination_table
90
94
  return nil unless table
91
- retrieve_table table["projectId"],
92
- table["datasetId"],
93
- table["tableId"]
95
+ retrieve_table table.project_id,
96
+ table.dataset_id,
97
+ table.table_id
94
98
  end
95
99
 
96
100
  ##
@@ -123,14 +127,10 @@ module Gcloud
123
127
  # data = data.next if data.next?
124
128
  #
125
129
  def query_results token: nil, max: nil, start: nil, timeout: nil
126
- ensure_connection!
130
+ ensure_service!
127
131
  options = { token: token, max: max, start: start, timeout: timeout }
128
- resp = connection.job_query_results job_id, options
129
- if resp.success?
130
- QueryData.from_gapi resp.data, connection
131
- else
132
- fail ApiError.from_response(resp)
133
- end
132
+ gapi = service.job_query_results job_id, options
133
+ QueryData.from_gapi gapi, service
134
134
  end
135
135
  end
136
136
  end