gcloud 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +8 -8
  2. data/CHANGELOG.md +15 -0
  3. data/OVERVIEW.md +38 -5
  4. data/lib/gcloud.rb +55 -4
  5. data/lib/gcloud/bigquery/data.rb +2 -0
  6. data/lib/gcloud/bigquery/dataset.rb +1 -1
  7. data/lib/gcloud/bigquery/dataset/list.rb +2 -0
  8. data/lib/gcloud/bigquery/job/list.rb +2 -0
  9. data/lib/gcloud/bigquery/project.rb +2 -9
  10. data/lib/gcloud/bigquery/table/list.rb +2 -0
  11. data/lib/gcloud/datastore.rb +23 -28
  12. data/lib/gcloud/datastore/connection.rb +3 -1
  13. data/lib/gcloud/datastore/dataset.rb +167 -22
  14. data/lib/gcloud/datastore/dataset/lookup_results.rb +2 -0
  15. data/lib/gcloud/datastore/dataset/query_results.rb +2 -0
  16. data/lib/gcloud/datastore/entity.rb +11 -11
  17. data/lib/gcloud/datastore/key.rb +33 -16
  18. data/lib/gcloud/dns/change/list.rb +2 -0
  19. data/lib/gcloud/dns/project.rb +1 -1
  20. data/lib/gcloud/dns/record/list.rb +2 -0
  21. data/lib/gcloud/dns/zone.rb +2 -2
  22. data/lib/gcloud/dns/zone/list.rb +2 -0
  23. data/lib/gcloud/gce.rb +0 -5
  24. data/lib/gcloud/pubsub.rb +65 -62
  25. data/lib/gcloud/pubsub/connection.rb +20 -2
  26. data/lib/gcloud/pubsub/project.rb +233 -72
  27. data/lib/gcloud/pubsub/subscription.rb +45 -13
  28. data/lib/gcloud/pubsub/subscription/list.rb +2 -0
  29. data/lib/gcloud/pubsub/topic.rb +66 -85
  30. data/lib/gcloud/pubsub/topic/list.rb +2 -0
  31. data/lib/gcloud/resource_manager.rb +244 -0
  32. data/lib/gcloud/resource_manager/connection.rb +124 -0
  33. data/lib/gcloud/resource_manager/credentials.rb +30 -0
  34. data/lib/gcloud/resource_manager/errors.rb +64 -0
  35. data/lib/gcloud/resource_manager/manager.rb +319 -0
  36. data/lib/gcloud/resource_manager/project.rb +529 -0
  37. data/lib/gcloud/resource_manager/project/list.rb +91 -0
  38. data/lib/gcloud/resource_manager/project/updater.rb +137 -0
  39. data/lib/gcloud/storage/bucket.rb +1 -1
  40. data/lib/gcloud/storage/bucket/cors.rb +2 -0
  41. data/lib/gcloud/storage/bucket/list.rb +2 -0
  42. data/lib/gcloud/storage/file/list.rb +2 -0
  43. data/lib/gcloud/storage/project.rb +1 -1
  44. data/lib/gcloud/version.rb +1 -1
  45. metadata +10 -2
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MGI1YjI0MjIxZDhhNmI1YjY0ZWM2MjY0YTg5ZDY3OWZlMmVlYjhhNw==
4
+ NGQ2OGZmZTRjOGY4ZGRiNjZmZTE0OGExZTZmODJkYjE4MmJlN2E4YQ==
5
5
  data.tar.gz: !binary |-
6
- MzdkOGRmYTY3MmQ0YzU0ZGM4YmIwZGQwNDdmY2EzNDNjYTY4NjRlYg==
6
+ YmYxMDQyOGNjNDBhZmMyNTgwMjBkYzhiM2Y4YTk0Y2JkYzQxN2QwNg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MTU4ZGJkZmM4YTg2YWY2YWNiYjQyYmQxNDM3YzBhNTczZDViMzgzMGQyMGIz
10
- ODVlY2JmNDJjNjljMmRlZTY5MmZhNzdiZTg3ZjUxMTI0MjM5ZTA5OWM4Njg4
11
- NzdiNmE4ZTVjOTk3MzIwODliYjM0NTcyNmFmNDVmYWFhZDU3OWM=
9
+ MGEwZmE1NzFkZGQ1MzE1YTliMmM5MDhmNWQ0MDliMTkxMDk4ZjUyNWI2MmQy
10
+ NGZkNWNkZTJhODY2OGJmMDg1Zjg2MWMwMzVlMzAyNDJkMmFmZmJlNWM4Y2Zk
11
+ NjJjNWRiZmU5YzM2ZjRmMjQ3MWE1MWE5ZmZhMDU5ZGI2NjczMWM=
12
12
  data.tar.gz: !binary |-
13
- NjNkMTAzNDI0YWE1ZWJjOTVjZGMzZjZlMGNhOTQ2NWNmYWZkZGE2NzFkMDY5
14
- MGU3OTkxMGRlZWIzNjQzOGM4OTJhNWExY2MyM2E1NzQ1MDNhZTNjMjhkMjAz
15
- ZjZkZGI2OGJkMjliYzQyNTkyMmFkNzU2MDRkNmNiYWQxYWE4ZmE=
13
+ NDI4OGMxMmVkMGVjMDBiMTExM2E5MGNmMGJhMTc0ODFlYjIwMTRlNTc1MDli
14
+ OTg1NjJjMDdkYTJiNDZjMGU1YmYyNTFjZWU4MzgzNzdlZDYyZDYwNGM1MjYy
15
+ YmRmMjJkZDNjMTAyMjBlMmZjY2FhMGRiZDc3ODkxYTA0NDczODI=
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  # Release History
2
2
 
3
+ ### 0.5.0 / 2015-10-29
4
+
5
+ #### Changes
6
+
7
+ * Add support for Release Manager service
8
+ * Pub/Sub Additions
9
+ * Fix issue getting and setting policies (jeffmendoza)
10
+ * Modified `autocreate` default value and behavior
11
+ * Add `skip_lookup` option on resource lookup methods
12
+ * Add `Project#publish` method
13
+ * Add `Project#subscribe` method
14
+ * Datastore Additions
15
+ * Add `namespace` option on running queries (jondot)
16
+ * Add `query`, `entity`, and `key` helpers to Dataset
17
+
3
18
  ### 0.4.1 / 2015-10-20
4
19
 
5
20
  #### Changes
data/OVERVIEW.md CHANGED
@@ -53,16 +53,16 @@ gcloud = Gcloud.new
53
53
  dataset = gcloud.datastore
54
54
 
55
55
  # Create a new task to demo datastore
56
- demo_task = Gcloud::Datastore::Entity.new
57
- demo_task.key = Gcloud::Datastore::Key.new "Task", "datastore-demo"
58
- demo_task[:description] = "Demonstrate Datastore functionality"
59
- demo_task[:completed] = false
56
+ demo_task = dataset.entity "Task", "datastore-demo" do |t|
57
+ t["description"] = "Demonstrate Datastore functionality"
58
+ t["completed"] = false
59
+ end
60
60
 
61
61
  # Save the new task
62
62
  dataset.save demo_task
63
63
 
64
64
  # Run a query for all completed tasks
65
- query = Gcloud::Datastore::Query.new.kind("Task").
65
+ query = dataset.query("Task").
66
66
  where("completed", "=", true)
67
67
  completed_tasks = dataset.run query
68
68
  ```
@@ -120,6 +120,39 @@ sub = pubsub.subscription "my-topic-sub"
120
120
  msgs = sub.pull
121
121
  ```
122
122
 
123
+ # Resource Manager
124
+
125
+ [Google Cloud Resource Manager](https://cloud.google.com/resource-manager/) ([docs](https://cloud.google.com/resource-manager/reference/rest/)) provides methods that you can use to programmatically manage your projects in the Google Cloud Platform. You may be familiar with managing projects in the [Developers Console](https://developers.google.com/console/help/new/). With this API you can do the following:
126
+
127
+ * Get a list of all projects associated with an account
128
+ * Create new projects
129
+ * Update existing projects
130
+ * Delete projects
131
+ * Undelete, or recover, projects that you don't want to delete
132
+
133
+ See the [gcloud-ruby Resource Manager API documentation](rdoc-ref:Gcloud::ResourceManager) to learn how to connect to Cloud Resource Manager using this library.
134
+
135
+ ```ruby
136
+ require "gcloud"
137
+
138
+ gcloud = Gcloud.new
139
+ resource_manager = gcloud.resource_manager
140
+
141
+ # List all projects
142
+ resource_manager.projects.each do |project|
143
+ puts projects.project_id
144
+ end
145
+
146
+ # Label a project as production
147
+ project = resource_manager.project "tokyo-rain-123"
148
+ project.update do |p|
149
+ p.labels["env"] = "production"
150
+ end
151
+
152
+ # List only projects with the "production" label
153
+ projects = resource_manager.projects filter: "labels.env:production"
154
+ ```
155
+
123
156
  # Storage
124
157
 
125
158
  [Google Cloud Storage](https://cloud.google.com/storage/) ([docs](https://cloud.google.com/storage/docs/json_api/)) allows you to store data on Google infrastructure with very high reliability, performance and availability, and can be used to distribute large data objects to users via direct download.
data/lib/gcloud.rb CHANGED
@@ -96,10 +96,10 @@ module Gcloud
96
96
  # gcloud = Gcloud.new
97
97
  # dataset = gcloud.datastore
98
98
  #
99
- # entity = Gcloud::Datastore::Entity.new
100
- # entity.key = Gcloud::Datastore::Key.new "Task"
101
- # entity["description"] = "Get started with Google Cloud"
102
- # entity["completed"] = false
99
+ # entity = dataset.entity "Task" do |t|
100
+ # t["description"] = "Get started with Google Cloud"
101
+ # t["completed"] = false
102
+ # end
103
103
  #
104
104
  # dataset.save entity
105
105
  #
@@ -299,4 +299,55 @@ module Gcloud
299
299
  require "gcloud/dns"
300
300
  Gcloud.dns @project, @keyfile, options
301
301
  end
302
+
303
+ # rubocop:disable Metrics/LineLength
304
+ # Disabled because the readonly scope in the example code is long and we can't
305
+ # shorten it.
306
+
307
+ ##
308
+ # Creates a new object for connecting to the Resource Manager service.
309
+ # Each call creates a new connection.
310
+ #
311
+ # === Parameters
312
+ #
313
+ # +options+::
314
+ # An optional Hash for controlling additional behavior. (+Hash+)
315
+ # <code>options[:scope]</code>::
316
+ # The OAuth 2.0 scopes controlling the set of resources and operations that
317
+ # the connection can access. See {Using OAuth 2.0 to Access Google
318
+ # APIs}[https://developers.google.com/identity/protocols/OAuth2]. (+String+
319
+ # or +Array+)
320
+ #
321
+ # The default scope is:
322
+ #
323
+ # * +https://www.googleapis.com/auth/cloud-platform+
324
+ #
325
+ # === Returns
326
+ #
327
+ # Gcloud::ResourceManager::Manager
328
+ #
329
+ # === Examples
330
+ #
331
+ # require "gcloud"
332
+ #
333
+ # gcloud = Gcloud.new
334
+ # resource_manager = gcloud.resource_manager
335
+ # resource_manager.projects.each do |project|
336
+ # puts projects.project_id
337
+ # end
338
+ #
339
+ # The default scope can be overridden with the +scope+ option:
340
+ #
341
+ # require "gcloud"
342
+ #
343
+ # gcloud = Gcloud.new
344
+ # readonly_scope = "https://www.googleapis.com/auth/cloudresourcemanager.readonly"
345
+ # resource_manager = gcloud.resource_manager scope: readonly_scope
346
+ #
347
+ def resource_manager options = {}
348
+ require "gcloud/resource_manager"
349
+ Gcloud.resource_manager @keyfile, options
350
+ end
351
+
352
+ # rubocop:enable Metrics/LineLength
302
353
  end
@@ -13,6 +13,8 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
 
16
+ require "delegate"
17
+
16
18
  module Gcloud
17
19
  module Bigquery
18
20
  ##
@@ -530,7 +530,7 @@ module Gcloud
530
530
  # === Returns
531
531
  #
532
532
  # Array of Gcloud::Bigquery::Table or Gcloud::Bigquery::View
533
- # (Gcloud::Bigquery::Table::List)
533
+ # (See Gcloud::Bigquery::Table::List)
534
534
  #
535
535
  # === Examples
536
536
  #
@@ -13,6 +13,8 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
 
16
+ require "delegate"
17
+
16
18
  module Gcloud
17
19
  module Bigquery
18
20
  class Dataset
@@ -13,6 +13,8 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
 
16
+ require "delegate"
17
+
16
18
  module Gcloud
17
19
  module Bigquery
18
20
  class Job
@@ -266,10 +266,6 @@ module Gcloud
266
266
  end
267
267
  end
268
268
 
269
- # rubocop:disable Metrics/AbcSize
270
- # rubocop:disable Metrics/MethodLength
271
- # Disabled rubocop because the level of abstraction is not violated here
272
-
273
269
  ##
274
270
  # Creates a new dataset.
275
271
  #
@@ -357,9 +353,6 @@ module Gcloud
357
353
  end
358
354
  end
359
355
 
360
- # rubocop:enable Metrics/AbcSize
361
- # rubocop:enable Metrics/MethodLength
362
-
363
356
  ##
364
357
  # Retrieves the list of datasets belonging to the project.
365
358
  #
@@ -378,7 +371,7 @@ module Gcloud
378
371
  #
379
372
  # === Returns
380
373
  #
381
- # Array of Gcloud::Bigquery::Dataset (Gcloud::Bigquery::Dataset::List)
374
+ # Array of Gcloud::Bigquery::Dataset (See Gcloud::Bigquery::Dataset::List)
382
375
  #
383
376
  # === Examples
384
377
  #
@@ -489,7 +482,7 @@ module Gcloud
489
482
  #
490
483
  # === Returns
491
484
  #
492
- # Array of Gcloud::Bigquery::Job (Gcloud::Bigquery::Job::List)
485
+ # Array of Gcloud::Bigquery::Job (See Gcloud::Bigquery::Job::List)
493
486
  #
494
487
  # === Examples
495
488
  #
@@ -13,6 +13,8 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
 
16
+ require "delegate"
17
+
16
18
  module Gcloud
17
19
  module Bigquery
18
20
  class Table
@@ -57,10 +57,10 @@ module Gcloud
57
57
  # dataset = Gcloud.datastore "my-todo-project",
58
58
  # "/path/to/keyfile.json"
59
59
  #
60
- # entity = Gcloud::Datastore::Entity.new
61
- # entity.key = Gcloud::Datastore::Key.new "Task"
62
- # entity["description"] = "Get started with Google Cloud"
63
- # entity["completed"] = false
60
+ # entity = dataset.entity "Task" do |t|
61
+ # t["description"] = "Get started with Google Cloud"
62
+ # t["completed"] = false
63
+ # end
64
64
  #
65
65
  # dataset.save entity
66
66
  #
@@ -125,7 +125,7 @@ module Gcloud
125
125
  #
126
126
  # gcloud = Gcloud.new
127
127
  # dataset = gcloud.datastore
128
- # key = Gcloud::Datastore::Key.new "Task", 12345
128
+ # key = dataset.key "Task", 12345
129
129
  # entity = dataset.find key
130
130
  #
131
131
  # See Gcloud::Datastore::Dataset#find
@@ -139,8 +139,7 @@ module Gcloud
139
139
  #
140
140
  # gcloud = Gcloud.new
141
141
  # dataset = gcloud.datastore
142
- # query = Gcloud::Datastore::Query.new
143
- # query.kind("List").
142
+ # query = dataset.query("List").
144
143
  # where("active", "=", true)
145
144
  # active_lists = dataset.run query
146
145
  #
@@ -150,8 +149,7 @@ module Gcloud
150
149
  #
151
150
  # gcloud = Gcloud.new
152
151
  # dataset = gcloud.datastore
153
- # query = Gcloud::Datastore::Query.new
154
- # query.kind("List").
152
+ # query = dataset.query("List").
155
153
  # where("active", "=", true).
156
154
  # order("name")
157
155
  # active_lists = dataset.run query
@@ -163,8 +161,7 @@ module Gcloud
163
161
  #
164
162
  # gcloud = Gcloud.new
165
163
  # dataset = gcloud.datastore
166
- # query = Gcloud::Datastore::Query.new
167
- # query.kind("List").
164
+ # query = dataset.query("List").
168
165
  # where("active", "=", true).
169
166
  # order("name").
170
167
  # limit(5)
@@ -179,8 +176,7 @@ module Gcloud
179
176
  # dataset = gcloud.datastore
180
177
  #
181
178
  # list = dataset.find "List", "todos"
182
- # query = Gcloud::Datastore::Query.new
183
- # query.kind("Task").
179
+ # query = dataset.query("Task").
184
180
  # ancestor(list.key)
185
181
  # items = dataset.run query
186
182
  #
@@ -198,8 +194,7 @@ module Gcloud
198
194
  # dataset = gcloud.datastore
199
195
  #
200
196
  # list = dataset.find "List", "todos"
201
- # query = Gcloud::Datastore::Query.new
202
- # query.kind("Task").
197
+ # query = dataset.query("Task").
203
198
  # ancestor(list.key)
204
199
  # all_tasks = []
205
200
  # tmp_tasks = dataset.run query
@@ -228,9 +223,9 @@ module Gcloud
228
223
  #
229
224
  # gcloud = Gcloud.new
230
225
  # dataset = gcloud.datastore
231
- # entity = Gcloud::Datastore::Entity.new
232
- # entity.key = Gcloud::Datastore::Key.new "User"
233
- # entity["name"] = "Heidi Henderson"
226
+ # entity = dataset.entity "User" do |e|
227
+ # e["name"] = "Heidi Henderson"
228
+ # end
234
229
  # entity.key.id #=> nil
235
230
  # dataset.save entity
236
231
  # entity.key.id #=> 123456789
@@ -277,12 +272,12 @@ module Gcloud
277
272
  # gcloud = Gcloud.new
278
273
  # dataset = gcloud.datastore
279
274
  #
280
- # key = Gcloud::Datastore::Key.new "User", "heidi"
275
+ # key = dataset.key "User", "heidi"
281
276
  #
282
- # user = Gcloud::Datastore::Entity.new
283
- # user.key = key
284
- # user["name"] = "Heidi Henderson"
285
- # user["email"] = "heidi@example.net"
277
+ # user = dataset.entity key do |u|
278
+ # u["name"] = "Heidi Henderson"
279
+ # u["email"] = "heidi@example.net"
280
+ # end
286
281
  #
287
282
  # dataset.transaction do |tx|
288
283
  # if tx.find(user.key).nil?
@@ -298,12 +293,12 @@ module Gcloud
298
293
  # gcloud = Gcloud.new
299
294
  # dataset = gcloud.datastore
300
295
  #
301
- # key = Gcloud::Datastore::Key.new "User", "heidi"
296
+ # key = dataset.key "User", "heidi"
302
297
  #
303
- # user = Gcloud::Datastore::Entity.new
304
- # user.key = key
305
- # user["name"] = "Heidi Henderson"
306
- # user["email"] = "heidi@example.net"
298
+ # user = dataset.entity key do |u|
299
+ # u["name"] = "Heidi Henderson"
300
+ # u["email"] = "heidi@example.net"
301
+ # end
307
302
  #
308
303
  # tx = dataset.transaction
309
304
  # begin
@@ -67,9 +67,11 @@ module Gcloud
67
67
  end
68
68
 
69
69
  # Query for entities.
70
- def run_query query
70
+ def run_query query, partition = nil
71
71
  run_query = Proto::RunQueryRequest.new.tap do |rq|
72
72
  rq.query = query
73
+ rq.partition_id = partition if partition
74
+ rq
73
75
  end
74
76
 
75
77
  Proto::RunQueryResponse.decode rpc("runQuery", run_query)
@@ -39,7 +39,7 @@ module Gcloud
39
39
  # gcloud = Gcloud.new
40
40
  # dataset = gcloud.datastore
41
41
  #
42
- # query = Gcloud::Datastore::Query.new.kind("Task").
42
+ # query = dataset.query("Task").
43
43
  # where("completed", "=", true)
44
44
  #
45
45
  # tasks = dataset.run query
@@ -101,7 +101,7 @@ module Gcloud
101
101
  #
102
102
  # === Example
103
103
  #
104
- # empty_key = Gcloud::Datastore::Key.new "Task"
104
+ # empty_key = dataset.key "Task"
105
105
  # task_keys = dataset.allocate_ids empty_key, 5
106
106
  #
107
107
  def allocate_ids incomplete_key, count = 1
@@ -160,7 +160,7 @@ module Gcloud
160
160
  #
161
161
  # Finding an entity with a key:
162
162
  #
163
- # key = Gcloud::Datastore::Key.new "Task", 123456
163
+ # key = dataset.key "Task", 123456
164
164
  # task = dataset.find key
165
165
  #
166
166
  # Finding an entity with a +kind+ and +id+/+name+:
@@ -169,7 +169,9 @@ module Gcloud
169
169
  #
170
170
  def find key_or_kind, id_or_name = nil
171
171
  key = key_or_kind
172
- key = Key.new key_or_kind, id_or_name unless key_or_kind.is_a? Key
172
+ unless key.is_a? Gcloud::Datastore::Key
173
+ key = Key.new key_or_kind, id_or_name
174
+ end
173
175
  find_all(key).first
174
176
  end
175
177
  alias_method :get, :find
@@ -190,8 +192,8 @@ module Gcloud
190
192
  #
191
193
  # gcloud = Gcloud.new
192
194
  # dataset = gcloud.datastore
193
- # key1 = Gcloud::Datastore::Key.new "Task", 123456
194
- # key2 = Gcloud::Datastore::Key.new "Task", 987654
195
+ # key1 = dataset.key "Task", 123456
196
+ # key2 = dataset.key "Task", 987654
195
197
  # tasks = dataset.find_all key1, key2
196
198
  #
197
199
  def find_all *keys
@@ -239,19 +241,31 @@ module Gcloud
239
241
  #
240
242
  # +query+::
241
243
  # The Query object with the search criteria. (+Query+)
244
+ # +options+::
245
+ # An optional Hash for controlling additional behavior. (+Hash+)
246
+ # <code>options[:namespace]</code>::
247
+ # The namespace the query is to run within. (+String+)
242
248
  #
243
249
  # === Returns
244
250
  #
245
251
  # Gcloud::Datastore::Dataset::QueryResults
246
252
  #
247
- # === Example
253
+ # === Examples
248
254
  #
249
- # query = Gcloud::Datastore::Query.new.kind("Task").
255
+ # query = dataset.query("Task").
250
256
  # where("completed", "=", true)
251
257
  # tasks = dataset.run query
252
258
  #
253
- def run query
254
- response = connection.run_query query.to_proto
259
+ # The query can optionally run within namespace when the +namespace+
260
+ # option is provided:
261
+ #
262
+ # query = Gcloud::Datastore::Query.new.kind("Task").
263
+ # where("completed", "=", true)
264
+ # tasks = dataset.run query, namespace: "ns~todo-project"
265
+ #
266
+ def run query, options = {}
267
+ partition = optional_partition_id options[:namespace]
268
+ response = connection.run_query query.to_proto, partition
255
269
  entities = to_gcloud_entities response.batch.entity_result
256
270
  cursor = Proto.encode_cursor response.batch.end_cursor
257
271
  more_results = Proto.to_more_results_string response.batch.more_results
@@ -271,12 +285,10 @@ module Gcloud
271
285
  # gcloud = Gcloud.new
272
286
  # dataset = gcloud.datastore
273
287
  #
274
- # key = Gcloud::Datastore::Key.new "User", "heidi"
275
- #
276
- # user = Gcloud::Datastore::Entity.new
277
- # user.key = key
278
- # user["name"] = "Heidi Henderson"
279
- # user["email"] = "heidi@example.net"
288
+ # user = dataset.entity "User", "heidi" do |u|
289
+ # u["name"] = "Heidi Henderson"
290
+ # u["email"] = "heidi@example.net"
291
+ # end
280
292
  #
281
293
  # dataset.transaction do |tx|
282
294
  # if tx.find(user.key).nil?
@@ -291,12 +303,10 @@ module Gcloud
291
303
  # gcloud = Gcloud.new
292
304
  # dataset = gcloud.datastore
293
305
  #
294
- # key = Gcloud::Datastore::Key.new "User", "heidi"
295
- #
296
- # user = Gcloud::Datastore::Entity.new
297
- # user.key = key
298
- # user["name"] = "Heidi Henderson"
299
- # user["email"] = "heidi@example.net"
306
+ # user = dataset.entity "User", "heidi" do |u|
307
+ # u["name"] = "Heidi Henderson"
308
+ # u["email"] = "heidi@example.net"
309
+ # end
300
310
  #
301
311
  # tx = dataset.transaction
302
312
  # begin
@@ -321,6 +331,133 @@ module Gcloud
321
331
  end
322
332
  end
323
333
 
334
+ ##
335
+ # Create a new Query instance. This is a convenience method to make the
336
+ # creation of Query objects easier.
337
+ #
338
+ # === Parameters
339
+ #
340
+ # +kinds+::
341
+ # The kind of entities to query. This is optional. (+String+)
342
+ #
343
+ # === Returns
344
+ #
345
+ # Gcloud::Datastore::Query
346
+ #
347
+ # === Example
348
+ #
349
+ # query = dataset.query("Task").
350
+ # where("completed", "=", true)
351
+ # tasks = dataset.run query
352
+ #
353
+ # This code is equivalent to the following:
354
+ #
355
+ # query = Gcloud::Datastore::Query.new.
356
+ # kind("Task").
357
+ # where("completed", "=", true)
358
+ # tasks = dataset.run query
359
+ #
360
+ def query *kinds
361
+ query = Query.new
362
+ query.kind(*kinds) unless kinds.empty?
363
+ query
364
+ end
365
+
366
+ ##
367
+ # Create a new Key instance. This is a convenience method to make the
368
+ # creation of Key objects easier.
369
+ #
370
+ # === Parameters
371
+ #
372
+ # +kind+::
373
+ # The kind of the Key. This is optional. (+String+)
374
+ # +id_or_name+::
375
+ # The id or name of the Key. This is optional. (+Integer+ or +String+)
376
+ #
377
+ # === Returns
378
+ #
379
+ # Gcloud::Datastore::Key
380
+ #
381
+ # === Example
382
+ #
383
+ # key = dataset.key "User", "heidi@example.com"
384
+ #
385
+ # This code is equivalent to the following:
386
+ #
387
+ # key = Gcloud::Datastore::Key.new "User", "heidi@example.com"
388
+ #
389
+ def key kind = nil, id_or_name = nil
390
+ Key.new kind, id_or_name
391
+ end
392
+
393
+ ##
394
+ # Create a new empty Entity instance. This is a convenience method to make
395
+ # the creation of Entity objects easier.
396
+ #
397
+ # === Parameters
398
+ #
399
+ # +key_or_kind+::
400
+ # A Key object or +kind+ string value. This is optional. (+Key+ or
401
+ # +String+ or +nil+)
402
+ # +id_or_name+::
403
+ # The Key's +id+ or +name+ value if a +kind+ was provided in the first
404
+ # parameter. (+Integer+ or +String+ or +nil+)
405
+ #
406
+ # === Returns
407
+ #
408
+ # Gcloud::Datastore::Entity
409
+ #
410
+ # === Examples
411
+ #
412
+ # entity = dataset.entity
413
+ #
414
+ # This code is equivalent to the following:
415
+ #
416
+ # entity = Gcloud::Datastore::Entity.new
417
+ #
418
+ # The key can also be passed in as an object:
419
+ #
420
+ # key = dataset.key "User", "heidi@example.com"
421
+ # entity = dataset.entity key
422
+ #
423
+ # Or the key values can be passed in as parameters:
424
+ #
425
+ # entity = dataset.entity "User", "heidi@example.com"
426
+ #
427
+ # This code is equivalent to the following:
428
+ #
429
+ # key = Gcloud::Datastore::Key.new "User", "heidi@example.com"
430
+ # entity = Gcloud::Datastore::Entity.new
431
+ # entity.key = key
432
+ #
433
+ # The newly created entity object can also be configured using a block:
434
+ #
435
+ # user = dataset.entity "User", "heidi@example.com" do |u|
436
+ # u["name"] = "Heidi Henderson"
437
+ # end
438
+ #
439
+ # This code is equivalent to the following:
440
+ #
441
+ # key = Gcloud::Datastore::Key.new "User", "heidi@example.com"
442
+ # entity = Gcloud::Datastore::Entity.new
443
+ # entity.key = key
444
+ # entity["name"] = "Heidi Henderson"
445
+ #
446
+ def entity key_or_kind = nil, id_or_name = nil
447
+ entity = Entity.new
448
+
449
+ # Set the key
450
+ key = key_or_kind
451
+ unless key.is_a? Gcloud::Datastore::Key
452
+ key = Key.new key_or_kind, id_or_name
453
+ end
454
+ entity.key = key
455
+
456
+ yield entity if block_given?
457
+
458
+ entity
459
+ end
460
+
324
461
  protected
325
462
 
326
463
  ##
@@ -372,6 +509,14 @@ module Gcloud
372
509
  end
373
510
  end
374
511
  end
512
+
513
+ def optional_partition_id namespace = nil
514
+ return nil if namespace.nil?
515
+ Proto::PartitionId.new.tap do |p|
516
+ p.namespace = namespace
517
+ p.dataset_id = project
518
+ end
519
+ end
375
520
  end
376
521
  end
377
522
  end