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
@@ -14,6 +14,7 @@
14
14
 
15
15
 
16
16
  require "gcloud/version"
17
+ require "gcloud/backoff"
17
18
  require "google/api_client"
18
19
 
19
20
  module Gcloud
@@ -38,14 +39,14 @@ module Gcloud
38
39
  end
39
40
 
40
41
  def get_project project_id = @project
41
- @client.execute(
42
+ execute(
42
43
  api_method: @dns.projects.get,
43
44
  parameters: { project: project_id }
44
45
  )
45
46
  end
46
47
 
47
48
  def get_zone zone_id
48
- @client.execute(
49
+ execute(
49
50
  api_method: @dns.managed_zones.get,
50
51
  parameters: { project: @project, managedZone: zone_id }
51
52
  )
@@ -57,7 +58,7 @@ module Gcloud
57
58
  maxResults: max
58
59
  }.delete_if { |_, v| v.nil? }
59
60
 
60
- @client.execute(
61
+ execute(
61
62
  api_method: @dns.managed_zones.list,
62
63
  parameters: params
63
64
  )
@@ -71,7 +72,7 @@ module Gcloud
71
72
  nameServerSet: name_server_set
72
73
  }.delete_if { |_, v| v.nil? }
73
74
 
74
- @client.execute(
75
+ execute(
75
76
  api_method: @dns.managed_zones.create,
76
77
  parameters: { project: @project },
77
78
  body_object: body
@@ -79,14 +80,14 @@ module Gcloud
79
80
  end
80
81
 
81
82
  def delete_zone zone_id
82
- @client.execute(
83
+ execute(
83
84
  api_method: @dns.managed_zones.delete,
84
85
  parameters: { project: @project, managedZone: zone_id }
85
86
  )
86
87
  end
87
88
 
88
89
  def get_change zone_id, change_id
89
- @client.execute(
90
+ execute(
90
91
  api_method: @dns.changes.get,
91
92
  parameters: { project: @project, managedZone: zone_id,
92
93
  changeId: change_id }
@@ -101,7 +102,7 @@ module Gcloud
101
102
  sortOrder: order
102
103
  }.delete_if { |_, v| v.nil? }
103
104
 
104
- @client.execute(
105
+ execute(
105
106
  api_method: @dns.changes.list,
106
107
  parameters: params
107
108
  )
@@ -112,7 +113,7 @@ module Gcloud
112
113
  "additions" => Array(additions),
113
114
  "deletions" => Array(deletions) }
114
115
 
115
- @client.execute(
116
+ execute(
116
117
  api_method: @dns.changes.create,
117
118
  parameters: { project: @project, managedZone: zone_id },
118
119
  body_object: change
@@ -127,7 +128,7 @@ module Gcloud
127
128
  type: type
128
129
  }.delete_if { |_, v| v.nil? }
129
130
 
130
- @client.execute(
131
+ execute(
131
132
  api_method: @dns.resource_record_sets.list,
132
133
  parameters: params
133
134
  )
@@ -159,6 +160,14 @@ module Gcloud
159
160
  def inspect
160
161
  "#{self.class}(#{@project})"
161
162
  end
163
+
164
+ protected
165
+
166
+ def execute options
167
+ Gcloud::Backoff.new.execute_gapi do
168
+ @client.execute options
169
+ end
170
+ end
162
171
  end
163
172
  end
164
173
  end
@@ -180,25 +180,21 @@ module Gcloud
180
180
  # puts zone.name
181
181
  # end
182
182
  #
183
- # @example With pagination: (See {Gcloud::Dns::Zone::List})
183
+ # @example Retrieve all zones: (See {Zone::List#all})
184
184
  # require "gcloud"
185
185
  #
186
186
  # gcloud = Gcloud.new
187
187
  # dns = gcloud.dns
188
188
  # zones = dns.zones
189
- # loop do
190
- # zones.each do |zone|
191
- # puts zone.name
192
- # end
193
- # break unless zones.next?
194
- # zones = zones.next
189
+ # zones.all do |zone|
190
+ # puts zone.name
195
191
  # end
196
192
  #
197
193
  def zones token: nil, max: nil
198
194
  ensure_connection!
199
195
  resp = connection.list_zones token: token, max: max
200
196
  if resp.success?
201
- Zone::List.from_response resp, connection
197
+ Zone::List.from_response resp, connection, max
202
198
  else
203
199
  fail ApiError.from_response(resp)
204
200
  end
@@ -27,54 +27,137 @@ module Gcloud
27
27
  attr_accessor :token
28
28
 
29
29
  ##
30
- # Create a new Record::List with an array of Record instances.
30
+ # @private Create a new Record::List with an array of Record instances.
31
31
  def initialize arr = []
32
32
  super arr
33
33
  end
34
34
 
35
35
  ##
36
36
  # Whether there a next page of records.
37
+ #
38
+ # @return [Boolean]
39
+ #
40
+ # @example
41
+ # require "gcloud"
42
+ #
43
+ # gcloud = Gcloud.new
44
+ # dns = gcloud.dns
45
+ # zone = dns.zone "example-com"
46
+ #
47
+ # records = zone.records "example.com."
48
+ # if records.next?
49
+ # next_records = records.next
50
+ # end
51
+ #
37
52
  def next?
38
53
  !token.nil?
39
54
  end
40
55
 
41
56
  ##
42
57
  # Retrieve the next page of records.
58
+ #
59
+ # @return [Record::List]
60
+ #
61
+ # @example
62
+ # require "gcloud"
63
+ #
64
+ # gcloud = Gcloud.new
65
+ # dns = gcloud.dns
66
+ # zone = dns.zone "example-com"
67
+ #
68
+ # records = zone.records "example.com."
69
+ # if records.next?
70
+ # next_records = records.next
71
+ # end
72
+ #
43
73
  def next
44
74
  return nil unless next?
45
75
  ensure_zone!
46
- @zone.records token: token
76
+ @zone.records @name, @type, token: token, max: @max
47
77
  end
48
78
 
49
79
  ##
50
- # Retrieves all records by repeatedly loading pages until {#next?}
51
- # returns false. Returns the list instance for method chaining.
80
+ # Retrieves all records by repeatedly loading {#next} until {#next?}
81
+ # returns `false`. Calls the given block once for each record, which is
82
+ # passed as the parameter.
52
83
  #
53
- # @example
84
+ # An Enumerator is returned if no block is given.
85
+ #
86
+ # This method may make several API calls until all records are
87
+ # retrieved. Be sure to use as narrow a search criteria as possible.
88
+ # Please use with caution.
89
+ #
90
+ # @param [Integer] request_limit The upper limit of API requests to make
91
+ # to load all records. Default is no limit.
92
+ # @yield [record] The block for accessing each record.
93
+ # @yieldparam [Record] record The record object.
94
+ #
95
+ # @return [Enumerator]
96
+ #
97
+ # @example Iterating each record by passing a block:
98
+ # require "gcloud"
99
+ #
100
+ # gcloud = Gcloud.new
101
+ # dns = gcloud.dns
102
+ # zone = dns.zone "example-com"
103
+ # records = zone.records "example.com."
104
+ #
105
+ # records.all do |record|
106
+ # puts record.name
107
+ # end
108
+ #
109
+ # @example Using the enumerator by not passing a block:
110
+ # require "gcloud"
111
+ #
112
+ # gcloud = Gcloud.new
113
+ # dns = gcloud.dns
114
+ # zone = dns.zone "example-com"
115
+ # records = zone.records "example.com."
116
+ #
117
+ # all_names = records.all.map do |record|
118
+ # record.name
119
+ # end
120
+ #
121
+ # @example Limit the number of API calls made:
54
122
  # require "gcloud"
55
123
  #
56
124
  # gcloud = Gcloud.new
57
125
  # dns = gcloud.dns
58
126
  # zone = dns.zone "example-com"
59
- # records = zone.records.all # Load all pages of records
127
+ # records = zone.records "example.com."
60
128
  #
61
- def all
62
- while next?
63
- next_records = self.next
64
- push(*next_records)
65
- self.token = next_records.token
129
+ # records.all(request_limit: 10) do |record|
130
+ # puts record.name
131
+ # end
132
+ #
133
+ def all request_limit: nil
134
+ request_limit = request_limit.to_i if request_limit
135
+ unless block_given?
136
+ return enum_for(:all, request_limit: request_limit)
137
+ end
138
+ results = self
139
+ loop do
140
+ results.each { |r| yield r }
141
+ if request_limit
142
+ request_limit -= 1
143
+ break if request_limit < 0
144
+ end
145
+ break unless results.next?
146
+ results = results.next
66
147
  end
67
- self
68
148
  end
69
149
 
70
150
  ##
71
151
  # @private New Records::List from a response object.
72
- def self.from_response resp, zone
152
+ def self.from_response resp, zone, name = nil, type = nil, max = nil
73
153
  records = new(Array(resp.data["rrsets"]).map do |gapi_object|
74
154
  Record.from_gapi gapi_object
75
155
  end)
76
156
  records.instance_variable_set "@token", resp.data["nextPageToken"]
77
157
  records.instance_variable_set "@zone", zone
158
+ records.instance_variable_set "@name", name
159
+ records.instance_variable_set "@type", type
160
+ records.instance_variable_set "@max", max
78
161
  records
79
162
  end
80
163
 
@@ -236,19 +236,15 @@ module Gcloud
236
236
  # zone = dns.zone "example-com"
237
237
  # changes = zone.changes order: :desc
238
238
  #
239
- # @example With pagination: (See {Gcloud::Dns::Change::List})
239
+ # @example Retrieve all changes: (See {Change::List#all})
240
240
  # require "gcloud"
241
241
  #
242
242
  # gcloud = Gcloud.new
243
243
  # dns = gcloud.dns
244
244
  # zone = dns.zone "example-com"
245
245
  # changes = zone.changes
246
- # loop do
247
- # changes.each do |change|
248
- # puts "#{change.name} - #{change.status}"
249
- # end
250
- # break unless changes.next?
251
- # changes = changes.next
246
+ # changes.all do |change|
247
+ # puts "#{change.name} - #{change.status}"
252
248
  # end
253
249
  #
254
250
  def changes token: nil, max: nil, order: nil
@@ -260,7 +256,7 @@ module Gcloud
260
256
  resp = connection.list_changes id, token: token, max: max,
261
257
  order: order, sort: sort
262
258
  if resp.success?
263
- Change::List.from_response resp, self
259
+ Change::List.from_response resp, self, max, order
264
260
  else
265
261
  fail ApiError.from_response(resp)
266
262
  end
@@ -304,28 +300,17 @@ module Gcloud
304
300
  # records = zone.records "www", "A"
305
301
  # records.first.name #=> "www.example.com."
306
302
  #
307
- # @example With pagination: (See {Gcloud::Dns::Record::List})
303
+ # @example Retrieve all records: (See {Gcloud::Dns::Record::List#all})
308
304
  # require "gcloud"
309
305
  #
310
306
  # gcloud = Gcloud.new
311
307
  # dns = gcloud.dns
312
308
  # zone = dns.zone "example-com"
313
309
  # records = zone.records "example.com."
314
- # loop do
315
- # records.each do |record|
316
- # puts record.name
317
- # end
318
- # break unless records.next?
319
- # records = records.next
320
- # end
321
- #
322
- # @example Retrieve all pages: (See {Gcloud::Dns::Record::List#all})
323
- # require "gcloud"
324
310
  #
325
- # gcloud = Gcloud.new
326
- # dns = gcloud.dns
327
- # zone = dns.zone "example-com"
328
- # records = zone.records.all
311
+ # records.all do |record|
312
+ # puts record.name
313
+ # end
329
314
  #
330
315
  def records name = nil, type = nil, token: nil, max: nil
331
316
  ensure_connection!
@@ -334,7 +319,7 @@ module Gcloud
334
319
 
335
320
  resp = connection.list_records id, name, type, token: token, max: max
336
321
  if resp.success?
337
- Record::List.from_response resp, self
322
+ Record::List.from_response resp, self, name, type, max
338
323
  else
339
324
  fail ApiError.from_response(resp)
340
325
  end
@@ -22,43 +22,140 @@ module Gcloud
22
22
  # Zone::List is a special case Array with additional values.
23
23
  class List < DelegateClass(::Array)
24
24
  ##
25
- # If not empty, indicates that there are more records that match
25
+ # If not empty, indicates that there are more zones that match
26
26
  # the request and this value should be passed to continue.
27
27
  attr_accessor :token
28
28
 
29
29
  ##
30
- # Create a new Zone::List with an array of Zone instances.
30
+ # @private Create a new Zone::List with an array of Zone instances.
31
31
  def initialize arr = []
32
32
  super arr
33
33
  end
34
34
 
35
35
  ##
36
36
  # Whether there a next page of zones.
37
+ #
38
+ # @return [Boolean]
39
+ #
40
+ # @example
41
+ # require "gcloud"
42
+ #
43
+ # gcloud = Gcloud.new
44
+ # dns = gcloud.dns
45
+ #
46
+ # zones = dns.zones
47
+ # if zones.next?
48
+ # next_zones = zones.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 zones.
57
+ #
58
+ # @return [Zone::List]
59
+ #
60
+ # @example
61
+ # require "gcloud"
62
+ #
63
+ # gcloud = Gcloud.new
64
+ # dns = gcloud.dns
65
+ #
66
+ # zones = dns.zones
67
+ # if zones.next?
68
+ # next_zones = zones.next
69
+ # end
70
+ #
43
71
  def next
44
72
  return nil unless next?
45
73
  ensure_connection!
46
- resp = @connection.list_zones token: token
74
+ resp = @connection.list_zones token: token, max: @max
47
75
  if resp.success?
48
- Zone::List.from_response resp, @connection
76
+ Zone::List.from_response resp, @connection, @max
49
77
  else
50
78
  fail ApiError.from_response(resp)
51
79
  end
52
80
  end
53
81
 
82
+ ##
83
+ # Retrieves all zones by repeatedly loading {#next} until {#next?}
84
+ # returns `false`. Calls the given block once for each zone, which is
85
+ # passed as the parameter.
86
+ #
87
+ # An Enumerator is returned if no block is given.
88
+ #
89
+ # This method may make several API calls until all zones are retrieved.
90
+ # Be sure to use as narrow a search criteria as possible. Please use
91
+ # with caution.
92
+ #
93
+ # @param [Integer] request_limit The upper limit of API requests to make
94
+ # to load all zones. Default is no limit.
95
+ # @yield [zone] The block for accessing each zone.
96
+ # @yieldparam [Zone] zone The zone object.
97
+ #
98
+ # @return [Enumerator]
99
+ #
100
+ # @example Iterating each zone by passing a block:
101
+ # require "gcloud"
102
+ #
103
+ # gcloud = Gcloud.new
104
+ # dns = gcloud.dns
105
+ # zones = dns.zones
106
+ #
107
+ # zones.all do |zone|
108
+ # puts zone.name
109
+ # end
110
+ #
111
+ # @example Using the enumerator by not passing a block:
112
+ # require "gcloud"
113
+ #
114
+ # gcloud = Gcloud.new
115
+ # dns = gcloud.dns
116
+ # zones = dns.zones
117
+ #
118
+ # all_names = zones.all.map do |zone|
119
+ # zone.name
120
+ # end
121
+ #
122
+ # @example Limit the number of API calls made:
123
+ # require "gcloud"
124
+ #
125
+ # gcloud = Gcloud.new
126
+ # dns = gcloud.dns
127
+ # zones = dns.zones
128
+ #
129
+ # zones.all(request_limit: 10) do |zone|
130
+ # puts zone.name
131
+ # end
132
+ #
133
+ def all request_limit: nil
134
+ request_limit = request_limit.to_i if request_limit
135
+ unless block_given?
136
+ return enum_for(:all, request_limit: request_limit)
137
+ end
138
+ results = self
139
+ loop do
140
+ results.each { |r| yield r }
141
+ if request_limit
142
+ request_limit -= 1
143
+ break if request_limit < 0
144
+ end
145
+ break unless results.next?
146
+ results = results.next
147
+ end
148
+ end
149
+
54
150
  ##
55
151
  # @private New Zones::List from a response object.
56
- def self.from_response resp, conn
152
+ def self.from_response resp, conn, max = nil
57
153
  zones = new(Array(resp.data["managedZones"]).map do |gapi_object|
58
154
  Zone.from_gapi gapi_object, conn
59
155
  end)
60
156
  zones.instance_variable_set "@token", resp.data["nextPageToken"]
61
157
  zones.instance_variable_set "@connection", conn
158
+ zones.instance_variable_set "@max", max
62
159
  zones
63
160
  end
64
161