gcloud 0.10.0 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +8 -8
  2. data/CHANGELOG.md +36 -0
  3. data/lib/gcloud/backoff.rb +5 -5
  4. data/lib/gcloud/bigquery.rb +24 -0
  5. data/lib/gcloud/bigquery/connection.rb +32 -25
  6. data/lib/gcloud/bigquery/data.rb +99 -1
  7. data/lib/gcloud/bigquery/dataset.rb +5 -13
  8. data/lib/gcloud/bigquery/dataset/list.rb +124 -2
  9. data/lib/gcloud/bigquery/job/list.rb +125 -2
  10. data/lib/gcloud/bigquery/project.rb +30 -27
  11. data/lib/gcloud/bigquery/query_data.rb +102 -1
  12. data/lib/gcloud/bigquery/table.rb +17 -2
  13. data/lib/gcloud/bigquery/table/list.rb +132 -3
  14. data/lib/gcloud/datastore.rb +30 -19
  15. data/lib/gcloud/datastore/dataset.rb +2 -22
  16. data/lib/gcloud/datastore/dataset/lookup_results.rb +160 -4
  17. data/lib/gcloud/datastore/dataset/query_results.rb +229 -23
  18. data/lib/gcloud/datastore/transaction.rb +2 -5
  19. data/lib/gcloud/dns.rb +20 -0
  20. data/lib/gcloud/dns/change/list.rb +109 -6
  21. data/lib/gcloud/dns/connection.rb +18 -9
  22. data/lib/gcloud/dns/project.rb +4 -8
  23. data/lib/gcloud/dns/record/list.rb +96 -13
  24. data/lib/gcloud/dns/zone.rb +9 -24
  25. data/lib/gcloud/dns/zone/list.rb +102 -5
  26. data/lib/gcloud/dns/zone/transaction.rb +1 -1
  27. data/lib/gcloud/logging.rb +19 -0
  28. data/lib/gcloud/logging/entry/list.rb +83 -14
  29. data/lib/gcloud/logging/metric/list.rb +89 -12
  30. data/lib/gcloud/logging/project.rb +18 -30
  31. data/lib/gcloud/logging/resource_descriptor/list.rb +105 -6
  32. data/lib/gcloud/logging/sink/list.rb +89 -12
  33. data/lib/gcloud/pubsub.rb +23 -0
  34. data/lib/gcloud/pubsub/project.rb +21 -29
  35. data/lib/gcloud/pubsub/service.rb +1 -3
  36. data/lib/gcloud/pubsub/subscription/list.rb +167 -13
  37. data/lib/gcloud/pubsub/topic.rb +15 -13
  38. data/lib/gcloud/pubsub/topic/batch.rb +10 -4
  39. data/lib/gcloud/pubsub/topic/list.rb +134 -8
  40. data/lib/gcloud/resource_manager.rb +24 -0
  41. data/lib/gcloud/resource_manager/connection.rb +18 -9
  42. data/lib/gcloud/resource_manager/manager.rb +7 -4
  43. data/lib/gcloud/resource_manager/project/list.rb +93 -14
  44. data/lib/gcloud/storage.rb +63 -0
  45. data/lib/gcloud/storage/bucket.rb +100 -61
  46. data/lib/gcloud/storage/bucket/list.rb +132 -8
  47. data/lib/gcloud/storage/connection.rb +68 -44
  48. data/lib/gcloud/storage/errors.rb +9 -3
  49. data/lib/gcloud/storage/file.rb +48 -4
  50. data/lib/gcloud/storage/file/list.rb +151 -15
  51. data/lib/gcloud/storage/file/verifier.rb +3 -3
  52. data/lib/gcloud/storage/project.rb +15 -30
  53. data/lib/gcloud/translate.rb +20 -0
  54. data/lib/gcloud/translate/connection.rb +12 -3
  55. data/lib/gcloud/version.rb +1 -1
  56. data/lib/gcloud/vision.rb +20 -0
  57. data/lib/gcloud/vision/connection.rb +10 -1
  58. data/lib/gcloud/vision/image.rb +15 -18
  59. metadata +16 -2
@@ -103,7 +103,7 @@ module Gcloud
103
103
  # end
104
104
  #
105
105
  def remove name, type
106
- @deletions += @zone.records(name, type).all
106
+ @deletions += @zone.records(name, type).all.to_a
107
107
  end
108
108
 
109
109
  ##
@@ -302,6 +302,25 @@ module Gcloud
302
302
  # logger.info "Job started."
303
303
  # ```
304
304
  #
305
+ # ## Configuring Backoff
306
+ #
307
+ # The {Gcloud::Backoff} class allows users to globally configure how Cloud API
308
+ # requests are automatically retried in the case of some errors, such as a
309
+ # `500` or `503` status code, or a specific internal error code such as
310
+ # `rateLimitExceeded`.
311
+ #
312
+ # If an API call fails, the response will be inspected to see if the call
313
+ # should be retried. If the response matches the criteria, then the request
314
+ # will be retried after a delay. If another error occurs, the delay will be
315
+ # increased incrementally before a subsequent attempt. The first retry will be
316
+ # delayed one second, the second retry two seconds, and so on.
317
+ #
318
+ # ```ruby
319
+ # require "gcloud"
320
+ # require "gcloud/backoff"
321
+ #
322
+ # Gcloud::Backoff.retries = 5 # Raise the maximum number of retries from 3
323
+ # ```
305
324
  #
306
325
  module Logging
307
326
  end
@@ -34,12 +34,40 @@ module Gcloud
34
34
 
35
35
  ##
36
36
  # Whether there is a next page of entries.
37
+ #
38
+ # @return [Boolean]
39
+ #
40
+ # @example
41
+ # require "gcloud"
42
+ #
43
+ # gcloud = Gcloud.new
44
+ # logging = gcloud.logging
45
+ #
46
+ # entries = logging.entries
47
+ # if entries.next?
48
+ # next_entries = entries.next
49
+ # end
50
+ #
37
51
  def next?
38
52
  !token.nil?
39
53
  end
40
54
 
41
55
  ##
42
56
  # Retrieve the next page of entries.
57
+ #
58
+ # @return [Sink::List]
59
+ #
60
+ # @example
61
+ # require "gcloud"
62
+ #
63
+ # gcloud = Gcloud.new
64
+ # logging = gcloud.logging
65
+ #
66
+ # entries = dataset.entries
67
+ # if entries.next?
68
+ # next_entries = entries.next
69
+ # end
70
+ #
43
71
  def next
44
72
  return nil unless next?
45
73
  ensure_service!
@@ -51,30 +79,71 @@ module Gcloud
51
79
  end
52
80
 
53
81
  ##
54
- # Retrieves all log entries by repeatedly loading {#next} until
55
- # {#next?} returns `false`. Returns the list instance for method
56
- # chaining.
82
+ # Retrieves all log entries by repeatedly loading {#next} until {#next?}
83
+ # returns `false`. Calls the given block once for each log entry, which
84
+ # is passed as the parameter.
85
+ #
86
+ # An Enumerator is returned if no block is given.
57
87
  #
58
88
  # This method may make several API calls until all log entries are
59
89
  # retrieved. Be sure to use as narrow a search criteria as possible.
60
90
  # Please use with caution.
61
91
  #
62
- # @example
92
+ # @param [Integer] request_limit The upper limit of API requests to make
93
+ # to load all log entries. Default is no limit.
94
+ # @yield [entry] The block for accessing each log entry.
95
+ # @yieldparam [Entry] entry The log entry object.
96
+ #
97
+ # @return [Enumerator]
98
+ #
99
+ # @example Iterating each log entry by passing a block:
100
+ # require "gcloud"
101
+ #
102
+ # gcloud = Gcloud.new
103
+ # logging = gcloud.logging
104
+ # entries = logging.entries order: "timestamp desc"
105
+ #
106
+ # entries.all do |entry|
107
+ # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
108
+ # end
109
+ #
110
+ # @example Using the enumerator by not passing a block:
63
111
  # require "gcloud"
64
112
  #
65
113
  # gcloud = Gcloud.new
66
114
  # logging = gcloud.logging
67
- # hour_ago = (Time.now - 60*60).utc.strftime('%FT%TZ')
68
- # recent_errors = "timestamp >= \"#{hour_ago}\" severity >= ERROR"
69
- # entries = logging.entries(filter: recent_errors).all
70
- #
71
- def all
72
- while next?
73
- next_records = self.next
74
- push(*next_records)
75
- self.token = next_records.token
115
+ # entries = logging.entries order: "timestamp desc"
116
+ #
117
+ # all_payloads = entries.all.map do |entry|
118
+ # entry.payload
119
+ # end
120
+ #
121
+ # @example Limit the number of API calls made:
122
+ # require "gcloud"
123
+ #
124
+ # gcloud = Gcloud.new
125
+ # logging = gcloud.logging
126
+ # entries = logging.entries order: "timestamp desc"
127
+ #
128
+ # entries.all(request_limit: 10) do |entry|
129
+ # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
130
+ # end
131
+ #
132
+ def all request_limit: nil
133
+ request_limit = request_limit.to_i if request_limit
134
+ unless block_given?
135
+ return enum_for(:all, request_limit: request_limit)
136
+ end
137
+ results = self
138
+ loop do
139
+ results.each { |r| yield r }
140
+ if request_limit
141
+ request_limit -= 1
142
+ break if request_limit < 0
143
+ end
144
+ break unless results.next?
145
+ results = results.next
76
146
  end
77
- self
78
147
  end
79
148
 
80
149
  ##
@@ -34,45 +34,121 @@ module Gcloud
34
34
 
35
35
  ##
36
36
  # Whether there is a next page of metrics.
37
+ #
38
+ # @return [Boolean]
39
+ #
40
+ # @example
41
+ # require "gcloud"
42
+ #
43
+ # gcloud = Gcloud.new
44
+ # logging = gcloud.logging
45
+ #
46
+ # metrics = logging.metrics
47
+ # if metrics.next?
48
+ # next_metrics = metrics.next
49
+ # end
50
+ #
37
51
  def next?
38
52
  !token.nil?
39
53
  end
40
54
 
41
55
  ##
42
56
  # Retrieve the next page of metrics.
57
+ #
58
+ # @return [Sink::List]
59
+ #
60
+ # @example
61
+ # require "gcloud"
62
+ #
63
+ # gcloud = Gcloud.new
64
+ # logging = gcloud.logging
65
+ #
66
+ # metrics = dataset.metrics
67
+ # if metrics.next?
68
+ # next_metrics = metrics.next
69
+ # end
70
+ #
43
71
  def next
44
72
  return nil unless next?
45
73
  ensure_service!
46
- grpc = @service.list_metrics token: token
74
+ grpc = @service.list_metrics token: token, max: @max
47
75
  self.class.from_grpc grpc, @service
48
76
  rescue GRPC::BadStatus => e
49
77
  raise Gcloud::Error.from_error(e)
50
78
  end
51
79
 
52
80
  ##
53
- # Retrieves all metrics by repeatedly loading {#next?} until {#next?}
54
- # returns `false`. Returns the list instance for method chaining.
81
+ # Retrieves all metrics by repeatedly loading {#next} until {#next?}
82
+ # returns `false`. Calls the given block once for each metric, which is
83
+ # passed as the parameter.
55
84
  #
56
- # @example
85
+ # An Enumerator is returned if no block is given.
86
+ #
87
+ # This method may make several API calls until all metrics are
88
+ # retrieved. Be sure to use as narrow a search criteria as possible.
89
+ # Please use with caution.
90
+ #
91
+ # @param [Integer] request_limit The upper limit of API requests to make
92
+ # to load all metrics. Default is no limit.
93
+ # @yield [metric] The block for accessing each metric.
94
+ # @yieldparam [Metric] metric The metric object.
95
+ #
96
+ # @return [Enumerator]
97
+ #
98
+ # @example Iterating each metric by passing a block:
99
+ # require "gcloud"
100
+ #
101
+ # gcloud = Gcloud.new
102
+ # logging = gcloud.logging
103
+ # metrics = logging.metrics
104
+ #
105
+ # metrics.all do |metric|
106
+ # puts "#{metric.name}: #{metric.filter}"
107
+ # end
108
+ #
109
+ # @example Using the enumerator by not passing a block:
57
110
  # require "gcloud"
58
111
  #
59
112
  # gcloud = Gcloud.new
60
113
  # logging = gcloud.logging
61
- # all_metrics = logging.metrics.all # Load all pages of metrics
114
+ # metrics = logging.metrics
115
+ #
116
+ # all_names = metrics.all.map do |metric|
117
+ # metric.name
118
+ # end
62
119
  #
63
- def all
64
- while next?
65
- next_records = self.next
66
- push(*next_records)
67
- self.token = next_records.token
120
+ # @example Limit the number of API calls made:
121
+ # require "gcloud"
122
+ #
123
+ # gcloud = Gcloud.new
124
+ # logging = gcloud.logging
125
+ # metrics = logging.metrics
126
+ #
127
+ # metrics.all(request_limit: 10) do |metric|
128
+ # puts "#{metric.name}: #{metric.filter}"
129
+ # end
130
+ #
131
+ def all request_limit: nil
132
+ request_limit = request_limit.to_i if request_limit
133
+ unless block_given?
134
+ return enum_for(:all, request_limit: request_limit)
135
+ end
136
+ results = self
137
+ loop do
138
+ results.each { |r| yield r }
139
+ if request_limit
140
+ request_limit -= 1
141
+ break if request_limit < 0
142
+ end
143
+ break unless results.next?
144
+ results = results.next
68
145
  end
69
- self
70
146
  end
71
147
 
72
148
  ##
73
149
  # @private New Metric::List from a
74
150
  # Google::Logging::V2::ListLogMetricsResponse object.
75
- def self.from_grpc grpc_list, service
151
+ def self.from_grpc grpc_list, service, max = nil
76
152
  metrics = new(Array(grpc_list.metrics).map do |grpc_metric|
77
153
  Metric.from_grpc grpc_metric, service
78
154
  end)
@@ -80,6 +156,7 @@ module Gcloud
80
156
  token = nil if token == ""
81
157
  metrics.instance_variable_set "@token", token
82
158
  metrics.instance_variable_set "@service", service
159
+ metrics.instance_variable_set "@max", max
83
160
  metrics
84
161
  end
85
162
 
@@ -132,18 +132,15 @@ module Gcloud
132
132
  # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
133
133
  # end
134
134
  #
135
- # @example With pagination: (See {Gcloud::Logging::Entry::List})
135
+ # @example Retrieve all log entries: (See {Entry::List#all})
136
136
  # require "gcloud"
137
137
  #
138
138
  # gcloud = Gcloud.new
139
139
  # logging = gcloud.logging
140
140
  # entries = logging.entries
141
- # loop do
142
- # entries.each do |e|
143
- # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
144
- # end
145
- # break unless entries.next?
146
- # entries = entries.next
141
+ #
142
+ # entries.all do |e|
143
+ # puts "[#{e.timestamp}] #{e.log_name} #{e.payload.inspect}"
147
144
  # end
148
145
  #
149
146
  def entries projects: nil, filter: nil, order: nil, token: nil, max: nil
@@ -341,18 +338,15 @@ module Gcloud
341
338
  # gcloud = Gcloud.new
342
339
  # logging = gcloud.logging
343
340
  # resource_descriptors = logging.resource_descriptors
344
- # loop do
345
- # resource_descriptors.each do |rd|
346
- # puts rd.type
347
- # end
348
- # break unless resource_descriptors.next?
349
- # resource_descriptors = resource_descriptors.next
341
+ #
342
+ # resource_descriptors.all do |rd|
343
+ # puts rd.type
350
344
  # end
351
345
  #
352
346
  def resource_descriptors token: nil, max: nil
353
347
  ensure_service!
354
348
  list_grpc = service.list_resource_descriptors token: token, max: max
355
- ResourceDescriptor::List.from_grpc list_grpc, service
349
+ ResourceDescriptor::List.from_grpc list_grpc, service, max
356
350
  rescue GRPC::BadStatus => e
357
351
  raise Gcloud::Error.from_error(e)
358
352
  end
@@ -401,24 +395,21 @@ module Gcloud
401
395
  # puts "#{s.name}: #{s.filter} -> #{s.destination}"
402
396
  # end
403
397
  #
404
- # @example With pagination: (See {Gcloud::Logging::Sink::List})
398
+ # @example Retrieve all sinks: (See {Sink::List#all})
405
399
  # require "gcloud"
406
400
  #
407
401
  # gcloud = Gcloud.new
408
402
  # logging = gcloud.logging
409
403
  # sinks = logging.sinks
410
- # loop do
411
- # sinks.each do |s|
412
- # puts "#{s.name}: #{s.filter} -> #{s.destination}"
413
- # end
414
- # break unless sinks.next?
415
- # sinks = sinks.next
404
+ #
405
+ # sinks.all do |s|
406
+ # puts "#{s.name}: #{s.filter} -> #{s.destination}"
416
407
  # end
417
408
  #
418
409
  def sinks token: nil, max: nil
419
410
  ensure_service!
420
411
  list_grpc = service.list_sinks token: token, max: max
421
- Sink::List.from_grpc list_grpc, service
412
+ Sink::List.from_grpc list_grpc, service, max
422
413
  rescue GRPC::BadStatus => e
423
414
  raise Gcloud::Error.from_error(e)
424
415
  end
@@ -545,24 +536,21 @@ module Gcloud
545
536
  # puts "#{m.name}: #{m.filter}"
546
537
  # end
547
538
  #
548
- # @example With pagination: (See {Gcloud::Logging::Metric::List})
539
+ # @example Retrieve all metrics: (See {Metric::List#all})
549
540
  # require "gcloud"
550
541
  #
551
542
  # gcloud = Gcloud.new
552
543
  # logging = gcloud.logging
553
544
  # metrics = logging.metrics
554
- # loop do
555
- # metrics.each do |m|
556
- # puts "#{m.name}: #{m.filter}"
557
- # end
558
- # break unless metrics.next?
559
- # metrics = metrics.next
545
+ #
546
+ # metrics.all do |m|
547
+ # puts "#{m.name}: #{m.filter}"
560
548
  # end
561
549
  #
562
550
  def metrics token: nil, max: nil
563
551
  ensure_service!
564
552
  grpc = service.list_metrics token: token, max: max
565
- Metric::List.from_grpc grpc, service
553
+ Metric::List.from_grpc grpc, service, max
566
554
  rescue GRPC::BadStatus => e
567
555
  raise Gcloud::Error.from_error(e)
568
556
  end
@@ -35,33 +35,132 @@ module Gcloud
35
35
 
36
36
  ##
37
37
  # Whether there is a next page of resource descriptors.
38
+ #
39
+ # @return [Boolean]
40
+ #
41
+ # @example
42
+ # require "gcloud"
43
+ #
44
+ # gcloud = Gcloud.new
45
+ # logging = gcloud.logging
46
+ #
47
+ # resource_descriptors = logging.resource_descriptors
48
+ # if resource_descriptors.next?
49
+ # next_resource_descriptors = resource_descriptors.next
50
+ # end
51
+ #
38
52
  def next?
39
53
  !token.nil?
40
54
  end
41
55
 
42
56
  ##
43
57
  # Retrieve the next page of resource descriptors.
58
+ #
59
+ # @return [Sink::List]
60
+ #
61
+ # @example
62
+ # require "gcloud"
63
+ #
64
+ # gcloud = Gcloud.new
65
+ # logging = gcloud.logging
66
+ #
67
+ # resource_descriptors = logging.resource_descriptors
68
+ # if resource_descriptors.next?
69
+ # next_resource_descriptors = resource_descriptors.next
70
+ # end
71
+ #
44
72
  def next
45
73
  return nil unless next?
46
74
  ensure_service!
47
- list_grpc = @service.list_resource_descriptors token: token
75
+ list_grpc = @service.list_resource_descriptors token: token, max: @max
48
76
  self.class.from_grpc list_grpc, @service
49
77
  rescue GRPC::BadStatus => e
50
78
  raise Gcloud::Error.from_error(e)
51
79
  end
52
80
 
81
+ ##
82
+ # Retrieves all resource descriptors by repeatedly loading {#next} until
83
+ # {#next?} returns `false`. Calls the given block once for each resource
84
+ # descriptor, which is passed as the parameter.
85
+ #
86
+ # An Enumerator is returned if no block is given.
87
+ #
88
+ # This method may make several API calls until all resource descriptors
89
+ # are retrieved. Be sure to use as narrow a search criteria as possible.
90
+ # Please use with caution.
91
+ #
92
+ # @param [Integer] request_limit The upper limit of API requests to make
93
+ # to load all resource descriptors. Default is no limit.
94
+ # @yield [resource_descriptor] The block for accessing each resource
95
+ # descriptor.
96
+ # @yieldparam [ResourceDescriptor] resource_descriptor The resource
97
+ # descriptor object.
98
+ #
99
+ # @return [Enumerator]
100
+ #
101
+ # @example Iterating each resource descriptor by passing a block:
102
+ # require "gcloud"
103
+ #
104
+ # gcloud = Gcloud.new
105
+ # logging = gcloud.logging
106
+ # resource_descriptors = logging.resource_descriptors
107
+ #
108
+ # resource_descriptors.all do |rd|
109
+ # puts rd.type
110
+ # end
111
+ #
112
+ # @example Using the enumerator by not passing a block:
113
+ # require "gcloud"
114
+ #
115
+ # gcloud = Gcloud.new
116
+ # logging = gcloud.logging
117
+ # resource_descriptors = logging.resource_descriptors
118
+ #
119
+ # all_types = resource_descriptors.all.map do |rd|
120
+ # rd.type
121
+ # end
122
+ #
123
+ # @example Limit the number of API calls made:
124
+ # require "gcloud"
125
+ #
126
+ # gcloud = Gcloud.new
127
+ # logging = gcloud.logging
128
+ # resource_descriptors = logging.resource_descriptors
129
+ #
130
+ # resource_descriptors.all(request_limit: 10) do |rd|
131
+ # puts rd.type
132
+ # end
133
+ #
134
+ def all request_limit: nil
135
+ request_limit = request_limit.to_i if request_limit
136
+ unless block_given?
137
+ return enum_for(:all, request_limit: request_limit)
138
+ end
139
+ results = self
140
+ loop do
141
+ results.each { |r| yield r }
142
+ if request_limit
143
+ request_limit -= 1
144
+ break if request_limit < 0
145
+ end
146
+ break unless results.next?
147
+ results = results.next
148
+ end
149
+ end
150
+
53
151
  ##
54
152
  # @private New ResourceDescriptor::List from a
55
153
  # Google::Logging::V2::ListMonitoredResourceDescriptorsResponse object.
56
- def self.from_grpc grpc_list, service
57
- sinks = new(Array(grpc_list.resource_descriptors).map do |grpc|
154
+ def self.from_grpc grpc_list, service, max = nil
155
+ rds = new(Array(grpc_list.resource_descriptors).map do |grpc|
58
156
  ResourceDescriptor.from_grpc grpc
59
157
  end)
60
158
  token = grpc_list.next_page_token
61
159
  token = nil if token == ""
62
- sinks.instance_variable_set "@token", token
63
- sinks.instance_variable_set "@service", service
64
- sinks
160
+ rds.instance_variable_set "@token", token
161
+ rds.instance_variable_set "@service", service
162
+ rds.instance_variable_set "@max", max
163
+ rds
65
164
  end
66
165
 
67
166
  protected