gcloud 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +13 -5
- data/AUTHENTICATION.md +71 -0
- data/CHANGELOG.md +5 -0
- data/README.md +10 -15
- data/lib/gcloud.rb +30 -0
- data/lib/gcloud/backoff.rb +3 -0
- data/lib/gcloud/credentials.rb +47 -30
- data/lib/gcloud/datastore.rb +246 -15
- data/lib/gcloud/datastore/connection.rb +4 -2
- data/lib/gcloud/datastore/credentials.rb +3 -1
- data/lib/gcloud/datastore/dataset.rb +130 -25
- data/lib/gcloud/datastore/dataset/lookup_results.rb +1 -0
- data/lib/gcloud/datastore/dataset/query_results.rb +7 -5
- data/lib/gcloud/datastore/entity.rb +99 -17
- data/lib/gcloud/datastore/errors.rb +13 -2
- data/lib/gcloud/datastore/key.rb +133 -2
- data/lib/gcloud/datastore/properties.rb +6 -1
- data/lib/gcloud/datastore/proto.rb +2 -1
- data/lib/gcloud/datastore/query.rb +4 -4
- data/lib/gcloud/datastore/transaction.rb +3 -0
- data/lib/gcloud/storage.rb +280 -13
- data/lib/gcloud/storage/bucket.rb +248 -11
- data/lib/gcloud/storage/bucket/acl.rb +631 -4
- data/lib/gcloud/storage/bucket/list.rb +1 -0
- data/lib/gcloud/storage/connection.rb +1 -0
- data/lib/gcloud/storage/credentials.rb +3 -1
- data/lib/gcloud/storage/errors.rb +9 -1
- data/lib/gcloud/storage/file.rb +231 -6
- data/lib/gcloud/storage/file/acl.rb +365 -2
- data/lib/gcloud/storage/file/list.rb +1 -0
- data/lib/gcloud/storage/file/verifier.rb +1 -0
- data/lib/gcloud/storage/project.rb +119 -10
- data/lib/gcloud/version.rb +18 -3
- metadata +33 -80
- data/.gemtest +0 -0
- data/.rubocop.yml +0 -17
- data/Manifest.txt +0 -66
- data/Rakefile +0 -35
- data/gcloud.gemspec +0 -63
- data/rakelib/console.rake +0 -28
- data/rakelib/manifest.rake +0 -24
- data/rakelib/proto.rake +0 -17
- data/rakelib/rubocop.rake +0 -17
- data/rakelib/test.rake +0 -144
- data/test/gcloud/datastore/proto/test_cursor.rb +0 -36
- data/test/gcloud/datastore/proto/test_direction.rb +0 -60
- data/test/gcloud/datastore/proto/test_operator.rb +0 -76
- data/test/gcloud/datastore/proto/test_value.rb +0 -231
- data/test/gcloud/datastore/test_connection.rb +0 -93
- data/test/gcloud/datastore/test_credentials.rb +0 -38
- data/test/gcloud/datastore/test_dataset.rb +0 -413
- data/test/gcloud/datastore/test_entity.rb +0 -161
- data/test/gcloud/datastore/test_entity_exclude.rb +0 -225
- data/test/gcloud/datastore/test_key.rb +0 -189
- data/test/gcloud/datastore/test_query.rb +0 -271
- data/test/gcloud/datastore/test_transaction.rb +0 -121
- data/test/gcloud/storage/test_backoff.rb +0 -127
- data/test/gcloud/storage/test_bucket.rb +0 -270
- data/test/gcloud/storage/test_bucket_acl.rb +0 -253
- data/test/gcloud/storage/test_default_acl.rb +0 -256
- data/test/gcloud/storage/test_file.rb +0 -221
- data/test/gcloud/storage/test_file_acl.rb +0 -367
- data/test/gcloud/storage/test_project.rb +0 -180
- data/test/gcloud/storage/test_storage.rb +0 -29
- data/test/gcloud/storage/test_verifier.rb +0 -62
- data/test/gcloud/test_version.rb +0 -8
- data/test/helper.rb +0 -91
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
YWUyNTJhOGZkZDNmY2E2NTFlOWJkNjRjZTEyNzhmYzkyMjVlY2RlMA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MGNhMjVhNjE5Njc5ZmM5NTU4MDlkYjJiYjA0OGQ1MmJjM2M0NDAyNA==
|
5
7
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
YzUzYTQ3NWRkMDI1N2U1YTVkMWJkZDZjM2VjYTAxZjYxZGMxMWRlMmRjY2Vi
|
10
|
+
MmU2MGFlNDI4YTE1NmIzZjg2ODYzOGU3NWUzMDE3YmNmMzU4Y2JiMjMwOGUz
|
11
|
+
NGU3ZTdmMzM4ZGU2YjAyZmFiOTg4OGZiOGNmMWEwM2Y3YzA4MjA=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZGEwM2NmNTQ0ZjExNjQ4NzhlZGFmODg5NjkwZjhkNjVmMjBmMzJlM2QzZTg5
|
14
|
+
YTFmOTc5NGVhMzdjZWNjMTY1OTY3OWE0OGE0MGQwM2UzNmY5N2IyZDI1MmQy
|
15
|
+
NDhjNmZkYzQ3YTUwYWIxZTA1MTk0NGIwZDNiMjMyMDAzZmNiMTY=
|
data/AUTHENTICATION.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Authentication
|
2
|
+
|
3
|
+
## Creating a Service Account
|
4
|
+
|
5
|
+
Gcloud aims to make authentication as simple as possible. Google Cloud requires a **Project ID** and **Service Account Credentials** to connect to the APIs. To create a service account:
|
6
|
+
|
7
|
+
1. Visit the [Google Developers Console](https://console.developers.google.com/project).
|
8
|
+
2. Create a new project or click on an existing project.
|
9
|
+
3. Navigate to **APIs & auth** > **APIs section** and turn on the following APIs (you may need to enable billing in order to use these services):
|
10
|
+
* Google Cloud Datastore API
|
11
|
+
* Google Cloud Storage
|
12
|
+
* Google Cloud Storage JSON API
|
13
|
+
4. Navigate to **APIs & auth** > **Credentials** and then:
|
14
|
+
* If you want to use a new service account, click on **Create new Client ID** and select **Service account**. After the account is created, you will be prompted to download the JSON key file that the library uses to authorize your requests.
|
15
|
+
* If you want to generate a new key for an existing service account, click on **Generate new JSON key** and download the JSON key file.
|
16
|
+
|
17
|
+
You will use the **Project ID** and **JSON file** to connect to services with gcloud.
|
18
|
+
|
19
|
+
## Project and Credential Lookup
|
20
|
+
|
21
|
+
Gcloud aims to make authentication as simple as possible, and provides several mechanisms to configure your system without providing **Project ID** and **Service Account Credentials** directly in code.
|
22
|
+
|
23
|
+
**Project ID** is discovered in the following order:
|
24
|
+
|
25
|
+
1. Specify project ID in code
|
26
|
+
2. Discover project ID in environment variables
|
27
|
+
3. Discover GCE project ID
|
28
|
+
|
29
|
+
**Credentials** are discovered in the following order:
|
30
|
+
|
31
|
+
1. Specify credentials in code
|
32
|
+
2. Discover credentials path in environment variables
|
33
|
+
3. Discover credentials JSON in environment variables
|
34
|
+
4. Discover credentials file in the Cloud SDK's path
|
35
|
+
5. Discover GCE credentials
|
36
|
+
|
37
|
+
### Compute Engine
|
38
|
+
|
39
|
+
While running on Google Compute Engine no extra work is needed. The **Project ID** and **Credentials** and are discovered automatically. Code should be written as if already authenticated.
|
40
|
+
|
41
|
+
### Environment Variables
|
42
|
+
|
43
|
+
The **Project ID** and **Credentials JSON** can be placed in environment variables instead of declaring them directly in code. Each service has its own environment variable, allowing for different service accounts to be used for different services. The path to the **Credentials JSON** file can be stored in the environment variable, or the **Credentials JSON** itself can be stored for environments such as Docker containers where writing files is difficult or not encouraged.
|
44
|
+
|
45
|
+
Here are the environment variables that Datastore checks for project ID:
|
46
|
+
|
47
|
+
1. DATASTORE_PROJECT
|
48
|
+
2. GOOGLE_CLOUD_PROJECT
|
49
|
+
|
50
|
+
Here are the environment variables that Datastore checks for credentials:
|
51
|
+
|
52
|
+
1. DATASTORE_KEYFILE - Path to JSON file
|
53
|
+
2. GOOGLE_CLOUD_KEYFILE - Path to JSON file
|
54
|
+
3. DATASTORE_KEYFILE_JSON - JSON contents
|
55
|
+
4. GOOGLE_CLOUD_KEYFILE_JSON - JSON contents
|
56
|
+
|
57
|
+
### Cloud SDK
|
58
|
+
|
59
|
+
This option allows for an easy way to authenticate during development. If credentials are not provided in code or in environment variables, then Cloud SDK credentials are discovered.
|
60
|
+
|
61
|
+
To configure your system for this, simply:
|
62
|
+
|
63
|
+
1. [Download and install the Cloud SDK](https://cloud.google.com/sdk)
|
64
|
+
2. Authenticate using OAuth2 `$ gcloud auth login`
|
65
|
+
3. Write code as if already authenticated.
|
66
|
+
|
67
|
+
**NOTE:** This is _not_ recommended for running in production. The Cloud SDK should only be used during development.
|
68
|
+
|
69
|
+
## Troubleshooting
|
70
|
+
|
71
|
+
If you're having trouble authenticating open a [Github Issue](https://github.com/GoogleCloudPlatform/gcloud-ruby/issues/new?title=Authentication+question) to get help. Also consider searching or asking [questions](http://stackoverflow.com/questions/tagged/gcloud-ruby) on [StackOverflow](http://stackoverflow.com).
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -4,6 +4,7 @@ Idiomatic Ruby client for [Google Cloud Platform](https://cloud.google.com/) ser
|
|
4
4
|
|
5
5
|
[![Travis Build Status](https://travis-ci.org/GoogleCloudPlatform/gcloud-ruby.svg)](https://travis-ci.org/GoogleCloudPlatform/gcloud-ruby/)
|
6
6
|
[![Coverage Status](https://img.shields.io/coveralls/GoogleCloudPlatform/gcloud-ruby.svg)](https://coveralls.io/r/GoogleCloudPlatform/gcloud-ruby?branch=master)
|
7
|
+
[![Gem Version](https://badge.fury.io/rb/gcloud.svg)](http://badge.fury.io/rb/gcloud)
|
7
8
|
|
8
9
|
* [Homepage](http://googlecloudplatform.github.io/gcloud-ruby/)
|
9
10
|
* [API Documentation](http://googlecloudplatform.github.io/gcloud-ruby/docs/master/)
|
@@ -13,7 +14,7 @@ Idiomatic Ruby client for [Google Cloud Platform](https://cloud.google.com/) ser
|
|
13
14
|
This client supports the following Google Cloud Platform services:
|
14
15
|
|
15
16
|
* [Google Cloud Datastore](https://cloud.google.com/datastore/) ([docs](https://cloud.google.com/datastore/docs))
|
16
|
-
* [Google Cloud Storage](https://cloud.google.com/storage/) ([docs](https://cloud.google.com/
|
17
|
+
* [Google Cloud Storage](https://cloud.google.com/storage/) ([docs](https://cloud.google.com/storage/docs/json_api/))
|
17
18
|
|
18
19
|
If you need support for other Google APIs, check out the [Google API Ruby Client library](https://github.com/google/google-api-ruby-client).
|
19
20
|
|
@@ -23,21 +24,11 @@ If you need support for other Google APIs, check out the [Google API Ruby Client
|
|
23
24
|
$ gem install gcloud
|
24
25
|
```
|
25
26
|
|
26
|
-
###
|
27
|
+
### Authentication
|
27
28
|
|
28
|
-
|
29
|
+
Gcloud uses Service Account credentials to connect to Google Cloud services. When running on Compute Engine the credentials will be discovered automatically. When running on other environments the Service Account credentials can be specified by providing the path to the JSON file, or the JSON itself, in environment variables. Additionally, Cloud SDK credentials can also be discovered automatically, but this is only recommended during development.
|
29
30
|
|
30
|
-
|
31
|
-
2. Create a new project or click on an existing project.
|
32
|
-
3. Navigate to **APIs & auth** > **APIs section** and turn on the following APIs (you may need to enable billing in order to use these services):
|
33
|
-
* Google Cloud Datastore API
|
34
|
-
* Google Cloud Storage
|
35
|
-
* Google Cloud Storage JSON API
|
36
|
-
4. Navigate to **APIs & auth** > **Credentials** and then:
|
37
|
-
* If you want to use a new service account, click on **Create new Client ID** and select **Service account**. After the account is created, you will be prompted to download the JSON key file that the library uses to authorize your requests.
|
38
|
-
* If you want to generate a new key for an existing service account, click on **Generate new JSON key** and download the JSON key file.
|
39
|
-
|
40
|
-
You will use the **Project ID** and **JSON file** to connect to services with gcloud.
|
31
|
+
Instructions and configuration options are covered in the [Authentication guide](AUTHENTICATION.md). The examples in Quick Start will demonstrate providing the **Project ID** and **Credentials JSON file path** in code.
|
41
32
|
|
42
33
|
### Datastore
|
43
34
|
|
@@ -48,6 +39,8 @@ Follow the [activation instructions](https://cloud.google.com/datastore/docs/act
|
|
48
39
|
See the [gcloud-ruby Datastore API documentation](http://googlecloudplatform.github.io/gcloud-ruby/docs/master/Gcloud/Storage.html) to learn how to interact with the Cloud Datastore using this library.
|
49
40
|
|
50
41
|
```ruby
|
42
|
+
require 'gcloud/datastore'
|
43
|
+
|
51
44
|
dataset = Gcloud.datastore "my-todo-project-id",
|
52
45
|
"/path/to/keyfile.json"
|
53
46
|
|
@@ -68,11 +61,13 @@ completed_tasks = dataset.run query
|
|
68
61
|
|
69
62
|
### Storage
|
70
63
|
|
71
|
-
[Google Cloud Storage](https://cloud.google.com/storage/) ([docs](https://cloud.google.com/
|
64
|
+
[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.
|
72
65
|
|
73
66
|
See the [gcloud-ruby Storage API documentation](http://googlecloudplatform.github.io/gcloud-ruby/docs/master/Gcloud/Storage.html) to learn how to connect to Cloud Storage using this library.
|
74
67
|
|
75
68
|
```ruby
|
69
|
+
require 'gcloud/storage'
|
70
|
+
|
76
71
|
storage = Gcloud.storage "my-todo-project-id",
|
77
72
|
"/path/to/keyfile.json"
|
78
73
|
|
data/lib/gcloud.rb
CHANGED
@@ -1,5 +1,35 @@
|
|
1
|
+
#--
|
2
|
+
# Copyright 2015 Google Inc. All rights reserved.
|
3
|
+
#
|
4
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
# you may not use this file except in compliance with the License.
|
6
|
+
# You may obtain a copy of the License at
|
7
|
+
#
|
8
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
#
|
10
|
+
# Unless required by applicable law or agreed to in writing, software
|
11
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
# See the License for the specific language governing permissions and
|
14
|
+
# limitations under the License.
|
15
|
+
|
1
16
|
require "gcloud/version"
|
2
17
|
|
18
|
+
##
|
19
|
+
# = Google Cloud
|
20
|
+
#
|
21
|
+
# Gcloud is the official library for interacting with the Google Cloud Platform.
|
22
|
+
# Google Cloud Platform is a set of modular cloud-based services that allow
|
23
|
+
# you to create anything from simple websites to complex applications.
|
24
|
+
#
|
25
|
+
# Gcloud's goal is to provide a API that is familiar and comfortable to
|
26
|
+
# Rubyists. Authentication is handled by providing project and credential
|
27
|
+
# information, or if you are running on Google Compute Engine this configuration
|
28
|
+
# is taken care of for you.
|
29
|
+
#
|
30
|
+
# You can learn more about various options for connection on the
|
31
|
+
# {Authentication Guide}[AUTHENTICATION.md].
|
32
|
+
#
|
3
33
|
module Gcloud
|
4
34
|
##
|
5
35
|
# Base Gcloud exception class.
|
data/lib/gcloud/backoff.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
#--
|
1
2
|
# Copyright 2014 Google Inc. All rights reserved.
|
2
3
|
#
|
3
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -12,6 +13,8 @@
|
|
12
13
|
# See the License for the specific language governing permissions and
|
13
14
|
# limitations under the License.
|
14
15
|
|
16
|
+
#--
|
17
|
+
# Google Cloud Backoff
|
15
18
|
module Gcloud
|
16
19
|
##
|
17
20
|
# Backoff allows users to control how Google API calls are retried.
|
data/lib/gcloud/credentials.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
#--
|
1
2
|
# Copyright 2014 Google Inc. All rights reserved.
|
2
3
|
#
|
3
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -17,6 +18,8 @@ require "signet/oauth_2/client"
|
|
17
18
|
require "forwardable"
|
18
19
|
require "googleauth"
|
19
20
|
|
21
|
+
#--
|
22
|
+
# Google Cloud Credentials
|
20
23
|
module Gcloud
|
21
24
|
##
|
22
25
|
# Represents the Oauth2 signing logic.
|
@@ -26,7 +29,9 @@ module Gcloud
|
|
26
29
|
TOKEN_CREDENTIAL_URI = "https://accounts.google.com/o/oauth2/token"
|
27
30
|
AUDIENCE = "https://accounts.google.com/o/oauth2/token"
|
28
31
|
SCOPE = []
|
29
|
-
|
32
|
+
PATH_ENV_VARS = ["GOOGLE_CLOUD_KEYFILE"]
|
33
|
+
JSON_ENV_VARS = ["GOOGLE_CLOUD_KEYFILE_JSON"]
|
34
|
+
DEFAULT_PATHS = ["~/.config/gcloud/application_default_credentials.json"]
|
30
35
|
|
31
36
|
attr_accessor :client
|
32
37
|
|
@@ -38,59 +43,71 @@ module Gcloud
|
|
38
43
|
:scope, :issuer, :signing_key
|
39
44
|
|
40
45
|
def initialize keyfile, options = {}
|
46
|
+
verify_keyfile_provided! keyfile
|
41
47
|
if keyfile.is_a? Signet::OAuth2::Client
|
42
48
|
@client = keyfile
|
43
|
-
|
49
|
+
elsif keyfile.is_a? Hash
|
44
50
|
@client = init_client keyfile, options
|
51
|
+
else
|
52
|
+
verify_keyfile_exists! keyfile
|
53
|
+
@client = init_client JSON.parse(::File.read(keyfile)), options
|
45
54
|
end
|
46
55
|
@client.fetch_access_token!
|
47
56
|
end
|
48
57
|
|
58
|
+
# rubocop:disable all
|
59
|
+
# Disabled rubocop because this is intentionally complex.
|
60
|
+
|
49
61
|
##
|
50
62
|
# Returns the default credentials.
|
51
63
|
#
|
52
|
-
def self.default
|
53
|
-
|
54
|
-
|
55
|
-
|
64
|
+
def self.default options = {}
|
65
|
+
env = ->(v) { ENV[v] }
|
66
|
+
json = ->(v) { JSON.parse ENV[v] rescue nil unless ENV[v].nil? }
|
67
|
+
path = ->(p) { ::File.file? p }
|
68
|
+
|
69
|
+
# First try to find keyfile file from environment variables.
|
70
|
+
self::PATH_ENV_VARS.map(&env).reject(&:nil?).select(&path).each do |file|
|
71
|
+
return new file, options
|
72
|
+
end
|
73
|
+
# Second try to find keyfile json from environment variables.
|
74
|
+
self::JSON_ENV_VARS.map(&json).reject(&:nil?).each do |hash|
|
75
|
+
return new hash, options
|
56
76
|
end
|
57
|
-
|
58
|
-
|
77
|
+
# Third try to find keyfile file from known file paths.
|
78
|
+
self::DEFAULT_PATHS.select(&path).each do |file|
|
79
|
+
return new file, options
|
80
|
+
end
|
81
|
+
# Finally get instantiated client from Google::Auth.
|
82
|
+
scope = options[:scope] || options["scope"] || self::SCOPE
|
83
|
+
client = Google::Auth.get_application_default scope
|
59
84
|
new client
|
60
85
|
end
|
61
86
|
|
87
|
+
# rubocop:enable all
|
88
|
+
|
89
|
+
protected
|
90
|
+
|
62
91
|
##
|
63
|
-
#
|
64
|
-
|
65
|
-
|
66
|
-
# This file is created when running <tt>gcloud auth login</tt>
|
67
|
-
def self.sdk_default_creds #:nodoc:
|
68
|
-
# This method will likely be moved once we gain better
|
69
|
-
# support for running in a GCE environment.
|
70
|
-
sdk_creds = "~/.config/gcloud/application_default_credentials.json"
|
71
|
-
File.expand_path sdk_creds
|
92
|
+
# Verify that the keyfile argument is provided.
|
93
|
+
def verify_keyfile_provided! keyfile
|
94
|
+
fail "You must provide a keyfile to connect with." if keyfile.nil?
|
72
95
|
end
|
73
96
|
|
74
|
-
|
97
|
+
##
|
98
|
+
# Verify that the keyfile argument is a file.
|
99
|
+
def verify_keyfile_exists! keyfile
|
100
|
+
exists = ::File.file? keyfile
|
101
|
+
fail "The keyfile '#{keyfile}' is not a valid file." unless exists
|
102
|
+
end
|
75
103
|
|
76
104
|
##
|
77
105
|
# Initializes the Signet client.
|
78
106
|
def init_client keyfile, options
|
79
|
-
verify_keyfile! keyfile
|
80
107
|
client_opts = client_options keyfile, options
|
81
108
|
Signet::OAuth2::Client.new client_opts
|
82
109
|
end
|
83
110
|
|
84
|
-
##
|
85
|
-
# Initializes the Signet client.
|
86
|
-
def verify_keyfile! keyfile
|
87
|
-
if keyfile.nil?
|
88
|
-
fail "You must provide a keyfile to connect with."
|
89
|
-
elsif !::File.file?(keyfile)
|
90
|
-
fail "The keyfile '#{keyfile}' is not a valid file."
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
111
|
##
|
95
112
|
# returns a new Hash with string keys instead of symbol keys.
|
96
113
|
def stringify_hash_keys hash
|
@@ -111,7 +128,7 @@ module Gcloud
|
|
111
128
|
# Constructor options override default options
|
112
129
|
options = default_options.merge options
|
113
130
|
# Keyfile options override everything
|
114
|
-
options = options.merge
|
131
|
+
options = options.merge keyfile
|
115
132
|
|
116
133
|
# client options for initializing signet client
|
117
134
|
{ token_credential_uri: options["token_credential_uri"],
|
data/lib/gcloud/datastore.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
#--
|
1
2
|
# Copyright 2014 Google Inc. All rights reserved.
|
2
3
|
#
|
3
4
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -18,30 +19,41 @@ require "gcloud/datastore/dataset"
|
|
18
19
|
require "gcloud/datastore/transaction"
|
19
20
|
require "gcloud/datastore/credentials"
|
20
21
|
|
21
|
-
|
22
|
+
#--
|
22
23
|
# Google Cloud Datastore
|
23
24
|
module Gcloud
|
24
25
|
##
|
25
|
-
#
|
26
|
+
# Creates a new object for connecting to the Datastore service.
|
27
|
+
# Each call creates a new connection.
|
28
|
+
#
|
29
|
+
# === Parameters
|
30
|
+
#
|
31
|
+
# +project+::
|
32
|
+
# Dataset identifier for the Datastore you are connecting to. (+String+)
|
33
|
+
# +keyfile+::
|
34
|
+
# Keyfile downloaded from Google Cloud. If file path the file must be
|
35
|
+
# readable. (+String+ or +Hash+)
|
36
|
+
#
|
37
|
+
# === Returns
|
38
|
+
#
|
39
|
+
# Gcloud::Datastore::Dataset
|
40
|
+
#
|
41
|
+
# === Example
|
42
|
+
#
|
43
|
+
# require "glcoud/datastore"
|
44
|
+
#
|
45
|
+
# dataset = Gcloud.datastore "my-todo-project",
|
46
|
+
# "/path/to/keyfile.json"
|
26
47
|
#
|
27
48
|
# entity = Gcloud::Datastore::Entity.new
|
28
49
|
# entity.key = Gcloud::Datastore::Key.new "Task"
|
29
50
|
# entity["description"] = "Get started with Google Cloud"
|
30
51
|
# entity["completed"] = false
|
31
52
|
#
|
32
|
-
# dataset = Gcloud.datastore "my-todo-project",
|
33
|
-
# "/path/to/keyfile.json"
|
34
53
|
# dataset.save entity
|
35
54
|
#
|
36
|
-
|
37
|
-
|
38
|
-
# @param keyfile [String] the path to the keyfile you downloaded from
|
39
|
-
# Google Cloud. The file must readable.
|
40
|
-
# @return [Gcloud::Datastore::Dataset] new dataset.
|
41
|
-
#
|
42
|
-
# See Gcloud::Datastore::Dataset
|
43
|
-
def self.datastore project = ENV["DATASTORE_PROJECT"],
|
44
|
-
keyfile = nil
|
55
|
+
def self.datastore project = nil, keyfile = nil
|
56
|
+
project ||= Gcloud::Datastore::Dataset.default_project
|
45
57
|
if keyfile.nil?
|
46
58
|
credentials = Gcloud::Datastore::Credentials.default
|
47
59
|
else
|
@@ -51,7 +63,20 @@ module Gcloud
|
|
51
63
|
end
|
52
64
|
|
53
65
|
##
|
54
|
-
# Google Cloud Datastore
|
66
|
+
# = Google Cloud Datastore
|
67
|
+
#
|
68
|
+
# Google Cloud Datastore is a fully managed, schemaless database for storing
|
69
|
+
# non-relational data. You should feel at home if you are familiar with
|
70
|
+
# relational databases, but there are some key differences to be aware of to
|
71
|
+
# make the most of using Datastore.
|
72
|
+
#
|
73
|
+
# Gcloud's goal is to provide a API that is familiar and comfortable to
|
74
|
+
# Rubyists. Authentication is handled by Gcloud.datastore. You can provide
|
75
|
+
# the project and credential information to connect to the Datastore service,
|
76
|
+
# or if you are running on Google Compute Engine this configuration is taken
|
77
|
+
# care of for you.
|
78
|
+
#
|
79
|
+
# require "glcoud/datastore"
|
55
80
|
#
|
56
81
|
# dataset = Gcloud.datastore "my-todo-project",
|
57
82
|
# "/path/to/keyfile.json"
|
@@ -59,8 +84,214 @@ module Gcloud
|
|
59
84
|
# entity["completed"] = true
|
60
85
|
# dataset.save entity
|
61
86
|
#
|
87
|
+
# You can learn more about various options for connection on the
|
88
|
+
# {Authentication Guide}[link:AUTHENTICATION.md].
|
89
|
+
#
|
90
|
+
# To learn more about Datastore, read the
|
91
|
+
# {Google Cloud Datastore Concepts Overview
|
92
|
+
# }[https://cloud.google.com/datastore/docs/concepts/overview].
|
93
|
+
#
|
94
|
+
# == Retrieving Records
|
95
|
+
#
|
96
|
+
# Records, called "entities" in Datastore, are retrieved by using a Key.
|
97
|
+
# The Key is more than a numeric identifier, it is a complex data structure
|
98
|
+
# that can be used to model relationships. The simplest Key has a string
|
99
|
+
# <tt>kind</tt> value, and either a numeric <tt>id</tt> value, or a string
|
100
|
+
# <tt>name</tt> value. A single record can be retrieved by calling
|
101
|
+
# Gcloud::Datastore::Dataset#find and passing the parts of the key:
|
102
|
+
#
|
103
|
+
# require "glcoud/datastore"
|
104
|
+
#
|
105
|
+
# dataset = Gcloud.datastore
|
106
|
+
# entity = dataset.find "Task", "start"
|
107
|
+
#
|
108
|
+
# Optionally, Gcloud::Datastore::Dataset#find can be given a Key object:
|
109
|
+
#
|
110
|
+
# require "glcoud/datastore"
|
111
|
+
#
|
112
|
+
# dataset = Gcloud.datastore
|
113
|
+
# key = Gcloud::Datastore::Key.new "Task", 12345
|
114
|
+
# entity = dataset.find key
|
115
|
+
#
|
116
|
+
# See Gcloud::Datastore::Dataset#find
|
117
|
+
#
|
118
|
+
# == Querying Records
|
119
|
+
#
|
120
|
+
# Multiple records can be found that match criteria.
|
121
|
+
# (See Gcloud::Datastore::Query#where)
|
122
|
+
#
|
123
|
+
# require "glcoud/datastore"
|
124
|
+
#
|
125
|
+
# dataset = Gcloud.datastore
|
126
|
+
# query = Gcloud::Datastore::Query.new
|
127
|
+
# query.kind("List").
|
128
|
+
# where("active", "=", true)
|
129
|
+
# active_lists = dataset.run query
|
130
|
+
#
|
131
|
+
# Records can also be ordered. (See Gcloud::Datastore::Query#order)
|
132
|
+
#
|
133
|
+
# require "glcoud/datastore"
|
134
|
+
#
|
135
|
+
# dataset = Gcloud.datastore
|
136
|
+
# query = Gcloud::Datastore::Query.new
|
137
|
+
# query.kind("List").
|
138
|
+
# where("active", "=", true).
|
139
|
+
# order("name")
|
140
|
+
# active_lists = dataset.run query
|
141
|
+
#
|
142
|
+
# The number of records returned can be specified.
|
143
|
+
# (See Gcloud::Datastore::Query#limit)
|
144
|
+
#
|
145
|
+
# require "glcoud/datastore"
|
146
|
+
#
|
147
|
+
# dataset = Gcloud.datastore
|
148
|
+
# query = Gcloud::Datastore::Query.new
|
149
|
+
# query.kind("List").
|
150
|
+
# where("active", "=", true).
|
151
|
+
# order("name").
|
152
|
+
# limit(5)
|
153
|
+
# active_lists = dataset.run query
|
154
|
+
#
|
155
|
+
# Records' Key structures can also be queried.
|
156
|
+
# (See Gcloud::Datastore::Query#ancestor)
|
157
|
+
#
|
158
|
+
# require "glcoud/datastore"
|
159
|
+
#
|
160
|
+
# dataset = Gcloud.datastore
|
161
|
+
#
|
162
|
+
# list = dataset.find "List", "todos"
|
163
|
+
# query = Gcloud::Datastore::Query.new
|
164
|
+
# query.kind("Task").
|
165
|
+
# ancestor(list.key)
|
166
|
+
# items = dataset.run query
|
167
|
+
#
|
168
|
+
# See Gcloud::Datastore::Query and Gcloud::Datastore::Dataset#run
|
169
|
+
#
|
170
|
+
# == Paginating Records
|
171
|
+
#
|
172
|
+
# All Records may not return at once, requiring multiple calls to Datastore
|
173
|
+
# to return them all. The returned records will have a <tt>cursor</tt> if
|
174
|
+
# there are more available.
|
175
|
+
#
|
176
|
+
# require "glcoud/datastore"
|
177
|
+
#
|
178
|
+
# dataset = Gcloud.datastore
|
179
|
+
#
|
180
|
+
# list = dataset.find "List", "todos"
|
181
|
+
# query = Gcloud::Datastore::Query.new
|
182
|
+
# query.kind("Task").
|
183
|
+
# ancestor(list.key)
|
184
|
+
# all_tasks = []
|
185
|
+
# tmp_tasks = dataset.run query
|
186
|
+
# while tmp_tasks.any? do
|
187
|
+
# tmp_tasks.each do |task|
|
188
|
+
# all_tasks << task
|
189
|
+
# end
|
190
|
+
# # break loop if no more tasks available
|
191
|
+
# break if tmp_tasks.cursor.nil?
|
192
|
+
# # set cursor on the query
|
193
|
+
# query = query.cursor tmp_tasks.cursor
|
194
|
+
# # query for more records
|
195
|
+
# tmp_tasks = dataset.run query
|
196
|
+
# end
|
197
|
+
#
|
198
|
+
# See Gcloud::Datastore::Dataset::LookupResults and
|
199
|
+
# Gcloud::Datastore::Dataset::QueryResults
|
200
|
+
#
|
201
|
+
# == Creating Records
|
202
|
+
#
|
203
|
+
# New entities can be created and persisted buy calling Dataset#save.
|
204
|
+
# The entity must have a Key to be saved. If the Key is incomplete then
|
205
|
+
# it will be completed when saved.
|
206
|
+
#
|
207
|
+
# require "glcoud/datastore"
|
208
|
+
#
|
209
|
+
# dataset = Gcloud.datastore
|
210
|
+
# entity = Gcloud::Datastore::Entity.new
|
211
|
+
# entity.key = Gcloud::Datastore::Key.new "User"
|
212
|
+
# entity["name"] = "Heidi Henderson"
|
213
|
+
# entity.key.id #=> nil
|
214
|
+
# dataset.save entity
|
215
|
+
# entity.key.id #=> 123456789
|
216
|
+
#
|
217
|
+
# == Updating Records
|
218
|
+
#
|
219
|
+
# Entities hold properties. A property has a name that is a string or symbol,
|
220
|
+
# and a value that is an object. Most value objects are supported, including
|
221
|
+
# String, Integer, Date, Time, and even other Entity or Key objects. Changes
|
222
|
+
# to the Entity's properties are persisted by calling Dataset#save.
|
223
|
+
#
|
224
|
+
# require "glcoud/datastore"
|
225
|
+
#
|
226
|
+
# dataset = Gcloud.datastore
|
227
|
+
# entity = datastore.find "User", "heidi"
|
228
|
+
# # Read the status property
|
229
|
+
# entity["status"] #=> "inactive"
|
230
|
+
# # Write the status property
|
231
|
+
# entity["status"] = "active"
|
232
|
+
# # Persist the changes
|
233
|
+
# dataset.save entity
|
234
|
+
#
|
235
|
+
# == Deleting Records
|
236
|
+
#
|
237
|
+
# Entities can be removed from Datastore by calling Dataset#delete and passing
|
238
|
+
# the Entity object or the entity's Key object.
|
239
|
+
#
|
240
|
+
# require "glcoud/datastore"
|
241
|
+
#
|
242
|
+
# dataset = Gcloud.datastore
|
243
|
+
# entity = datastore.find "User", "heidi"
|
244
|
+
# dataset.delete entity
|
245
|
+
#
|
246
|
+
# == Transactions
|
247
|
+
#
|
248
|
+
# Complex logic can be wrapped in a Transaction. All queries and updates
|
249
|
+
# within the Dataset#transaction block are run within the transaction scope,
|
250
|
+
# and will be automatically committed when the block completes.
|
251
|
+
#
|
252
|
+
# require "gcloud/datastore"
|
253
|
+
#
|
254
|
+
# dataset = Gcloud.datastore
|
255
|
+
#
|
256
|
+
# key = Gcloud::Datastore::Key.new "User", "heidi"
|
257
|
+
#
|
258
|
+
# user = Gcloud::Datastore::Entity.new
|
259
|
+
# user.key = key
|
260
|
+
# user["name"] = "Heidi Henderson"
|
261
|
+
# user["email"] = "heidi@example.net"
|
262
|
+
#
|
263
|
+
# dataset.transaction do |tx|
|
264
|
+
# if tx.find(user.key).nil?
|
265
|
+
# tx.save user
|
266
|
+
# end
|
267
|
+
# end
|
268
|
+
#
|
269
|
+
# Alternatively, if no block is given the transaction object is returned
|
270
|
+
# allowing you to commit or rollback manually.
|
271
|
+
#
|
272
|
+
# require "gcloud/datastore"
|
273
|
+
#
|
274
|
+
# dataset = Gcloud.datastore
|
275
|
+
#
|
276
|
+
# key = Gcloud::Datastore::Key.new "User", "heidi"
|
277
|
+
#
|
278
|
+
# user = Gcloud::Datastore::Entity.new
|
279
|
+
# user.key = key
|
280
|
+
# user["name"] = "Heidi Henderson"
|
281
|
+
# user["email"] = "heidi@example.net"
|
282
|
+
#
|
283
|
+
# tx = dataset.transaction
|
284
|
+
# begin
|
285
|
+
# if tx.find(user.key).nil?
|
286
|
+
# tx.save user
|
287
|
+
# end
|
288
|
+
# tx.commit
|
289
|
+
# rescue
|
290
|
+
# tx.rollback
|
291
|
+
# end
|
62
292
|
#
|
63
|
-
# See Gcloud::Datastore::
|
293
|
+
# See Gcloud::Datastore::Transaction and
|
294
|
+
# Gcloud::Datastore::Dataset#transaction
|
64
295
|
module Datastore
|
65
296
|
end
|
66
297
|
end
|