gcloud 0.12.2 → 0.20.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. checksums.yaml +5 -13
  2. data/lib/gcloud.rb +27 -456
  3. data/lib/gcloud/bigquery.rb +2 -382
  4. data/lib/gcloud/datastore.rb +2 -576
  5. data/lib/gcloud/dns.rb +2 -321
  6. data/lib/gcloud/logging.rb +1 -322
  7. data/lib/gcloud/pubsub.rb +2 -476
  8. data/lib/gcloud/resource_manager.rb +2 -273
  9. data/lib/gcloud/storage.rb +2 -440
  10. data/lib/gcloud/translate.rb +1 -250
  11. data/lib/gcloud/version.rb +2 -2
  12. data/lib/gcloud/vision.rb +1 -501
  13. metadata +36 -332
  14. data/AUTHENTICATION.md +0 -75
  15. data/CHANGELOG.md +0 -382
  16. data/OVERVIEW.md +0 -259
  17. data/lib/gcloud/backoff.rb +0 -150
  18. data/lib/gcloud/bigquery/copy_job.rb +0 -97
  19. data/lib/gcloud/bigquery/credentials.rb +0 -29
  20. data/lib/gcloud/bigquery/data.rb +0 -239
  21. data/lib/gcloud/bigquery/dataset.rb +0 -753
  22. data/lib/gcloud/bigquery/dataset/access.rb +0 -507
  23. data/lib/gcloud/bigquery/dataset/list.rb +0 -169
  24. data/lib/gcloud/bigquery/extract_job.rb +0 -117
  25. data/lib/gcloud/bigquery/insert_response.rb +0 -81
  26. data/lib/gcloud/bigquery/job.rb +0 -299
  27. data/lib/gcloud/bigquery/job/list.rb +0 -172
  28. data/lib/gcloud/bigquery/load_job.rb +0 -202
  29. data/lib/gcloud/bigquery/project.rb +0 -475
  30. data/lib/gcloud/bigquery/query_data.rb +0 -234
  31. data/lib/gcloud/bigquery/query_job.rb +0 -137
  32. data/lib/gcloud/bigquery/schema.rb +0 -359
  33. data/lib/gcloud/bigquery/service.rb +0 -506
  34. data/lib/gcloud/bigquery/table.rb +0 -1141
  35. data/lib/gcloud/bigquery/table/list.rb +0 -180
  36. data/lib/gcloud/bigquery/view.rb +0 -475
  37. data/lib/gcloud/credentials.rb +0 -129
  38. data/lib/gcloud/datastore/commit.rb +0 -148
  39. data/lib/gcloud/datastore/credentials.rb +0 -35
  40. data/lib/gcloud/datastore/cursor.rb +0 -76
  41. data/lib/gcloud/datastore/dataset.rb +0 -660
  42. data/lib/gcloud/datastore/dataset/lookup_results.rb +0 -219
  43. data/lib/gcloud/datastore/dataset/query_results.rb +0 -386
  44. data/lib/gcloud/datastore/entity.rb +0 -449
  45. data/lib/gcloud/datastore/errors.rb +0 -41
  46. data/lib/gcloud/datastore/gql_query.rb +0 -211
  47. data/lib/gcloud/datastore/grpc_utils.rb +0 -132
  48. data/lib/gcloud/datastore/key.rb +0 -281
  49. data/lib/gcloud/datastore/properties.rb +0 -128
  50. data/lib/gcloud/datastore/query.rb +0 -348
  51. data/lib/gcloud/datastore/service.rb +0 -167
  52. data/lib/gcloud/datastore/transaction.rb +0 -362
  53. data/lib/gcloud/dns/change.rb +0 -158
  54. data/lib/gcloud/dns/change/list.rb +0 -173
  55. data/lib/gcloud/dns/credentials.rb +0 -29
  56. data/lib/gcloud/dns/importer.rb +0 -183
  57. data/lib/gcloud/dns/project.rb +0 -247
  58. data/lib/gcloud/dns/record.rb +0 -170
  59. data/lib/gcloud/dns/record/list.rb +0 -174
  60. data/lib/gcloud/dns/service.rb +0 -167
  61. data/lib/gcloud/dns/zone.rb +0 -759
  62. data/lib/gcloud/dns/zone/list.rb +0 -168
  63. data/lib/gcloud/dns/zone/transaction.rb +0 -176
  64. data/lib/gcloud/errors.rb +0 -206
  65. data/lib/gcloud/gce.rb +0 -56
  66. data/lib/gcloud/grpc_utils.rb +0 -87
  67. data/lib/gcloud/logging/credentials.rb +0 -29
  68. data/lib/gcloud/logging/entry.rb +0 -465
  69. data/lib/gcloud/logging/entry/http_request.rb +0 -141
  70. data/lib/gcloud/logging/entry/list.rb +0 -177
  71. data/lib/gcloud/logging/entry/operation.rb +0 -90
  72. data/lib/gcloud/logging/logger.rb +0 -307
  73. data/lib/gcloud/logging/metric.rb +0 -169
  74. data/lib/gcloud/logging/metric/list.rb +0 -172
  75. data/lib/gcloud/logging/project.rb +0 -642
  76. data/lib/gcloud/logging/resource.rb +0 -84
  77. data/lib/gcloud/logging/resource_descriptor.rb +0 -137
  78. data/lib/gcloud/logging/resource_descriptor/list.rb +0 -174
  79. data/lib/gcloud/logging/service.rb +0 -267
  80. data/lib/gcloud/logging/sink.rb +0 -227
  81. data/lib/gcloud/logging/sink/list.rb +0 -171
  82. data/lib/gcloud/pubsub/credentials.rb +0 -29
  83. data/lib/gcloud/pubsub/message.rb +0 -94
  84. data/lib/gcloud/pubsub/policy.rb +0 -204
  85. data/lib/gcloud/pubsub/project.rb +0 -482
  86. data/lib/gcloud/pubsub/received_message.rb +0 -160
  87. data/lib/gcloud/pubsub/service.rb +0 -334
  88. data/lib/gcloud/pubsub/subscription.rb +0 -565
  89. data/lib/gcloud/pubsub/subscription/list.rb +0 -208
  90. data/lib/gcloud/pubsub/topic.rb +0 -511
  91. data/lib/gcloud/pubsub/topic/list.rb +0 -174
  92. data/lib/gcloud/pubsub/topic/publisher.rb +0 -85
  93. data/lib/gcloud/resource_manager/credentials.rb +0 -30
  94. data/lib/gcloud/resource_manager/manager.rb +0 -266
  95. data/lib/gcloud/resource_manager/policy.rb +0 -211
  96. data/lib/gcloud/resource_manager/project.rb +0 -484
  97. data/lib/gcloud/resource_manager/project/list.rb +0 -167
  98. data/lib/gcloud/resource_manager/project/updater.rb +0 -130
  99. data/lib/gcloud/resource_manager/service.rb +0 -127
  100. data/lib/gcloud/storage/bucket.rb +0 -775
  101. data/lib/gcloud/storage/bucket/acl.rb +0 -810
  102. data/lib/gcloud/storage/bucket/cors.rb +0 -153
  103. data/lib/gcloud/storage/bucket/list.rb +0 -172
  104. data/lib/gcloud/storage/credentials.rb +0 -29
  105. data/lib/gcloud/storage/errors.rb +0 -65
  106. data/lib/gcloud/storage/file.rb +0 -842
  107. data/lib/gcloud/storage/file/acl.rb +0 -425
  108. data/lib/gcloud/storage/file/list.rb +0 -191
  109. data/lib/gcloud/storage/file/verifier.rb +0 -67
  110. data/lib/gcloud/storage/project.rb +0 -316
  111. data/lib/gcloud/storage/service.rb +0 -347
  112. data/lib/gcloud/translate/api.rb +0 -241
  113. data/lib/gcloud/translate/detection.rb +0 -137
  114. data/lib/gcloud/translate/language.rb +0 -69
  115. data/lib/gcloud/translate/service.rb +0 -80
  116. data/lib/gcloud/translate/translation.rb +0 -112
  117. data/lib/gcloud/vision/annotate.rb +0 -224
  118. data/lib/gcloud/vision/annotation.rb +0 -455
  119. data/lib/gcloud/vision/annotation/entity.rb +0 -234
  120. data/lib/gcloud/vision/annotation/face.rb +0 -1750
  121. data/lib/gcloud/vision/annotation/properties.rb +0 -245
  122. data/lib/gcloud/vision/annotation/safe_search.rb +0 -161
  123. data/lib/gcloud/vision/annotation/text.rb +0 -236
  124. data/lib/gcloud/vision/annotation/vertex.rb +0 -108
  125. data/lib/gcloud/vision/credentials.rb +0 -29
  126. data/lib/gcloud/vision/image.rb +0 -590
  127. data/lib/gcloud/vision/location.rb +0 -115
  128. data/lib/gcloud/vision/project.rb +0 -278
  129. data/lib/gcloud/vision/service.rb +0 -66
  130. data/lib/google/api/annotations.rb +0 -14
  131. data/lib/google/api/http.rb +0 -30
  132. data/lib/google/api/label.rb +0 -24
  133. data/lib/google/api/monitored_resource.rb +0 -25
  134. data/lib/google/datastore/v1beta3/datastore.rb +0 -115
  135. data/lib/google/datastore/v1beta3/datastore_services.rb +0 -33
  136. data/lib/google/datastore/v1beta3/entity.rb +0 -63
  137. data/lib/google/datastore/v1beta3/query.rb +0 -128
  138. data/lib/google/devtools/cloudtrace/v1/trace.rb +0 -78
  139. data/lib/google/devtools/cloudtrace/v1/trace_services.rb +0 -32
  140. data/lib/google/example/library/v1/library.rb +0 -91
  141. data/lib/google/example/library/v1/library_services.rb +0 -40
  142. data/lib/google/iam/v1/iam_policy.rb +0 -33
  143. data/lib/google/iam/v1/iam_policy_services.rb +0 -30
  144. data/lib/google/iam/v1/policy.rb +0 -25
  145. data/lib/google/logging/type/http_request.rb +0 -28
  146. data/lib/google/logging/type/log_severity.rb +0 -27
  147. data/lib/google/logging/v2/log_entry.rb +0 -44
  148. data/lib/google/logging/v2/logging.rb +0 -56
  149. data/lib/google/logging/v2/logging_config.rb +0 -59
  150. data/lib/google/logging/v2/logging_config_services.rb +0 -32
  151. data/lib/google/logging/v2/logging_metrics.rb +0 -51
  152. data/lib/google/logging/v2/logging_metrics_services.rb +0 -32
  153. data/lib/google/logging/v2/logging_services.rb +0 -31
  154. data/lib/google/longrunning/operations.rb +0 -50
  155. data/lib/google/longrunning/operations_services.rb +0 -29
  156. data/lib/google/protobuf/descriptor.rb +0 -0
  157. data/lib/google/pubsub/v1/pubsub.rb +0 -129
  158. data/lib/google/pubsub/v1/pubsub_services.rb +0 -56
  159. data/lib/google/pubsub/v1beta2/pubsub.rb +0 -126
  160. data/lib/google/pubsub/v1beta2/pubsub_services.rb +0 -56
  161. data/lib/google/rpc/code.rb +0 -32
  162. data/lib/google/rpc/error_details.rb +0 -61
  163. data/lib/google/rpc/status.rb +0 -19
  164. data/lib/google/type/color.rb +0 -20
  165. data/lib/google/type/date.rb +0 -18
  166. data/lib/google/type/dayofweek.rb +0 -23
  167. data/lib/google/type/latlng.rb +0 -17
  168. data/lib/google/type/money.rb +0 -18
  169. data/lib/google/type/timeofday.rb +0 -19
@@ -1,129 +0,0 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- require "json"
17
- require "signet/oauth_2/client"
18
- require "forwardable"
19
- require "googleauth"
20
-
21
- module Gcloud
22
- ##
23
- # @private
24
- # Represents the OAuth 2.0 signing logic.
25
- # This class is intended to be inherited by API-specific classes
26
- # which overrides the SCOPE constant.
27
- class Credentials
28
- TOKEN_CREDENTIAL_URI = "https://accounts.google.com/o/oauth2/token"
29
- AUDIENCE = "https://accounts.google.com/o/oauth2/token"
30
- SCOPE = []
31
- PATH_ENV_VARS = %w(GCLOUD_KEYFILE GOOGLE_CLOUD_KEYFILE)
32
- JSON_ENV_VARS = %w(GCLOUD_KEYFILE_JSON GOOGLE_CLOUD_KEYFILE_JSON)
33
- DEFAULT_PATHS = ["~/.config/gcloud/application_default_credentials.json"]
34
-
35
- attr_accessor :client
36
-
37
- ##
38
- # Delegate client methods to the client object.
39
- extend Forwardable
40
- def_delegators :@client,
41
- :token_credential_uri, :audience,
42
- :scope, :issuer, :signing_key
43
-
44
- def initialize keyfile, scope: nil
45
- verify_keyfile_provided! keyfile
46
- if keyfile.is_a? Signet::OAuth2::Client
47
- @client = keyfile
48
- elsif keyfile.is_a? Hash
49
- hash = stringify_hash_keys keyfile
50
- hash["scope"] ||= scope
51
- @client = init_client hash
52
- else
53
- verify_keyfile_exists! keyfile
54
- json = JSON.parse ::File.read(keyfile)
55
- json["scope"] ||= scope
56
- @client = init_client json
57
- end
58
- @client.fetch_access_token!
59
- end
60
-
61
- ##
62
- # Returns the default credentials.
63
- #
64
- def self.default scope: nil
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, scope: scope
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, scope: scope
76
- end
77
- # Third try to find keyfile file from known file paths.
78
- self::DEFAULT_PATHS.select(&path).each do |file|
79
- return new file, scope: scope
80
- end
81
- # Finally get instantiated client from Google::Auth.
82
- scope ||= self::SCOPE
83
- client = Google::Auth.get_application_default scope
84
- new client
85
- end
86
-
87
- protected
88
-
89
- ##
90
- # Verify that the keyfile argument is provided.
91
- def verify_keyfile_provided! keyfile
92
- fail "You must provide a keyfile to connect with." if keyfile.nil?
93
- end
94
-
95
- ##
96
- # Verify that the keyfile argument is a file.
97
- def verify_keyfile_exists! keyfile
98
- exists = ::File.file? keyfile
99
- fail "The keyfile '#{keyfile}' is not a valid file." unless exists
100
- end
101
-
102
- ##
103
- # Initializes the Signet client.
104
- def init_client keyfile
105
- client_opts = client_options keyfile
106
- Signet::OAuth2::Client.new client_opts
107
- end
108
-
109
- ##
110
- # returns a new Hash with string keys instead of symbol keys.
111
- def stringify_hash_keys hash
112
- Hash[hash.map { |(k, v)| [k.to_s, v] }]
113
- end
114
-
115
- def client_options options
116
- # Keyfile options have higher priority over constructor defaults
117
- options["token_credential_uri"] ||= self.class::TOKEN_CREDENTIAL_URI
118
- options["audience"] ||= self.class::AUDIENCE
119
- options["scope"] ||= self.class::SCOPE
120
-
121
- # client options for initializing signet client
122
- { token_credential_uri: options["token_credential_uri"],
123
- audience: options["audience"],
124
- scope: Array(options["scope"]),
125
- issuer: options["client_email"],
126
- signing_key: OpenSSL::PKey::RSA.new(options["private_key"]) }
127
- end
128
- end
129
- end
@@ -1,148 +0,0 @@
1
- # Copyright 2016 Google Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- module Gcloud
17
- module Datastore
18
- ##
19
- # # Commit
20
- #
21
- # Object yielded from `commit` methods to allow multiple changes to be made
22
- # in a single commit.
23
- #
24
- # @example
25
- # gcloud = Gcloud.new
26
- # datastore = gcloud.datastore
27
- # datastore.commit do |c|
28
- # c.save task1, task2
29
- # c.delete entity1, entity2
30
- # end
31
- #
32
- # See {Gcloud::Datastore::Dataset#commit} and
33
- # {Gcloud::Datastore::Transaction#commit}.
34
- class Commit
35
- ##
36
- # @private Create a new Commit object.
37
- def initialize
38
- @shared_upserts = []
39
- @shared_inserts = []
40
- @shared_updates = []
41
- @shared_deletes = []
42
- end
43
-
44
- ##
45
- # Saves entities to the Datastore.
46
- #
47
- # @param [Entity] entities One or more Entity objects to save.
48
- #
49
- # @example
50
- # gcloud = Gcloud.new
51
- # datastore = gcloud.datastore
52
- # datastore.commit do |c|
53
- # c.save task1, task2
54
- # end
55
- #
56
- def save *entities
57
- entities = Array(entities).flatten
58
- @shared_upserts += entities unless entities.empty?
59
- # Do not save yet
60
- entities
61
- end
62
- alias_method :upsert, :save
63
-
64
- ##
65
- # Inserts entities to the Datastore.
66
- #
67
- # @param [Entity] entities One or more Entity objects to insert.
68
- #
69
- # @example
70
- # gcloud = Gcloud.new
71
- # datastore = gcloud.datastore
72
- # datastore.commit do |c|
73
- # c.insert task1, task2
74
- # end
75
- #
76
- def insert *entities
77
- entities = Array(entities).flatten
78
- @shared_inserts += entities unless entities.empty?
79
- # Do not insert yet
80
- entities
81
- end
82
-
83
- ##
84
- # Updates entities to the Datastore.
85
- #
86
- # @param [Entity] entities One or more Entity objects to update.
87
- #
88
- # @example
89
- # gcloud = Gcloud.new
90
- # datastore = gcloud.datastore
91
- # datastore.commit do |c|
92
- # c.update task1, task2
93
- # end
94
- #
95
- def update *entities
96
- entities = Array(entities).flatten
97
- @shared_updates += entities unless entities.empty?
98
- # Do not update yet
99
- entities
100
- end
101
-
102
- ##
103
- # Remove entities from the Datastore.
104
- #
105
- # @param [Entity, Key] entities_or_keys One or more Entity or Key
106
- # objects to remove.
107
- #
108
- # @example
109
- # gcloud = Gcloud.new
110
- # datastore = gcloud.datastore
111
- # datastore.commit do |c|
112
- # c.delete task1, task2
113
- # end
114
- #
115
- def delete *entities_or_keys
116
- keys = Array(entities_or_keys).flatten.map do |e_or_k|
117
- e_or_k.respond_to?(:key) ? e_or_k.key : e_or_k
118
- end
119
- @shared_deletes += keys unless keys.empty?
120
- # Do not delete yet
121
- true
122
- end
123
-
124
- # @private Mutations object to be committed.
125
- def mutations
126
- mutations = []
127
- mutations += @shared_upserts.map do |entity|
128
- Google::Datastore::V1beta3::Mutation.new upsert: entity.to_grpc
129
- end
130
- mutations += @shared_inserts.map do |entity|
131
- Google::Datastore::V1beta3::Mutation.new insert: entity.to_grpc
132
- end
133
- mutations += @shared_updates.map do |entity|
134
- Google::Datastore::V1beta3::Mutation.new update: entity.to_grpc
135
- end
136
- mutations += @shared_deletes.map do |key|
137
- Google::Datastore::V1beta3::Mutation.new delete: key.to_grpc
138
- end
139
- mutations
140
- end
141
-
142
- # @private All entities saved in the commit.
143
- def entities
144
- @shared_upserts + @shared_inserts + @shared_updates
145
- end
146
- end
147
- end
148
- end
@@ -1,35 +0,0 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- require "gcloud/credentials"
17
-
18
- module Gcloud
19
- module Datastore
20
- ##
21
- # @private
22
- #
23
- # Authentication credentials to Google Cloud.
24
- # The most common way to create this object is to provide the path
25
- # to the JSON keyfile downloaded from Google Cloud.
26
- #
27
- # @see https://developers.google.com/accounts/docs/application-default-credentials
28
- class Credentials < Gcloud::Credentials
29
- SCOPE = ["https://www.googleapis.com/auth/datastore"]
30
- PATH_ENV_VARS = %w(DATASTORE_KEYFILE GCLOUD_KEYFILE GOOGLE_CLOUD_KEYFILE)
31
- JSON_ENV_VARS = %w(DATASTORE_KEYFILE_JSON GCLOUD_KEYFILE_JSON
32
- GOOGLE_CLOUD_KEYFILE_JSON)
33
- end
34
- end
35
- end
@@ -1,76 +0,0 @@
1
- # Copyright 2016 Google Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- module Gcloud
17
- module Datastore
18
- ##
19
- # # Cursor
20
- #
21
- # Cursor is a point in query results. Cursors are returned in QueryResults.
22
- #
23
- # @example
24
- # require "gcloud"
25
- #
26
- # gcloud = Gcloud.new
27
- # datastore = gcloud.datastore
28
- #
29
- # query = datastore.query("Task").
30
- # where("done", "=", false)
31
- #
32
- # tasks = datastore.run query
33
- # tasks.cursor #=> Cursor
34
- #
35
- class Cursor
36
- # Base64 encoded array of bytes
37
- def initialize cursor
38
- @cursor = cursor
39
- end
40
-
41
- # Base64 encoded array of bytes
42
- def to_s
43
- @cursor
44
- end
45
-
46
- # @private
47
- def inspect
48
- "#{self.class}(#{@cursor})"
49
- end
50
-
51
- # @private
52
- def == other
53
- return false unless other.is_a? Cursor
54
- @cursor == other.to_s
55
- end
56
-
57
- # @private
58
- def <=> other
59
- return -1 unless other.is_a? Cursor
60
- @cursor <=> other.to_s
61
- end
62
-
63
- # @private byte array as a string
64
- def to_grpc
65
- GRPCUtils.decode_bytes(@cursor)
66
- end
67
-
68
- # @private byte array as a string
69
- def self.from_grpc grpc
70
- grpc = String grpc
71
- return nil if grpc.empty?
72
- new GRPCUtils.encode_bytes(grpc)
73
- end
74
- end
75
- end
76
- end
@@ -1,660 +0,0 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- require "gcloud/gce"
17
- require "gcloud/datastore/grpc_utils"
18
- require "gcloud/datastore/credentials"
19
- require "gcloud/datastore/service"
20
- require "gcloud/datastore/commit"
21
- require "gcloud/datastore/entity"
22
- require "gcloud/datastore/key"
23
- require "gcloud/datastore/query"
24
- require "gcloud/datastore/gql_query"
25
- require "gcloud/datastore/cursor"
26
- require "gcloud/datastore/dataset/lookup_results"
27
- require "gcloud/datastore/dataset/query_results"
28
-
29
- module Gcloud
30
- module Datastore
31
- ##
32
- # # Dataset
33
- #
34
- # Dataset is the data saved in a project's Datastore.
35
- # Dataset is analogous to a database in relational database world.
36
- #
37
- # Gcloud::Datastore::Dataset is the main object for interacting with
38
- # Google Datastore. {Gcloud::Datastore::Entity} objects are created,
39
- # read, updated, and deleted by Gcloud::Datastore::Dataset.
40
- #
41
- # See {Gcloud#datastore}
42
- #
43
- # @example
44
- # require "gcloud"
45
- #
46
- # gcloud = Gcloud.new
47
- # datastore = gcloud.datastore
48
- #
49
- # query = datastore.query("Task").
50
- # where("done", "=", false)
51
- #
52
- # tasks = datastore.run query
53
- #
54
- class Dataset
55
- ##
56
- # @private The gRPC Service object.
57
- attr_accessor :service
58
-
59
- ##
60
- # @private Creates a new Dataset instance.
61
- #
62
- # See {Gcloud#datastore}
63
- def initialize service
64
- @service = service
65
- end
66
-
67
- ##
68
- # The Datastore project connected to.
69
- #
70
- # @example
71
- # require "gcloud"
72
- #
73
- # gcloud = Gcloud.new "my-todo-project",
74
- # "/path/to/keyfile.json"
75
- #
76
- # datastore = gcloud.datastore
77
- # datastore.project #=> "my-todo-project"
78
- #
79
- def project
80
- service.project
81
- end
82
-
83
- ##
84
- # @private Default project.
85
- def self.default_project
86
- ENV["DATASTORE_DATASET"] ||
87
- ENV["DATASTORE_PROJECT"] ||
88
- ENV["GCLOUD_PROJECT"] ||
89
- ENV["GOOGLE_CLOUD_PROJECT"] ||
90
- Gcloud::GCE.project_id
91
- end
92
-
93
- ##
94
- # Generate IDs for a Key before creating an entity.
95
- #
96
- # @param [Key] incomplete_key A Key without `id` or `name` set.
97
- # @param [String] count The number of new key IDs to create.
98
- #
99
- # @return [Array<Gcloud::Datastore::Key>]
100
- #
101
- # @example
102
- # task_key = datastore.key "Task"
103
- # task_keys = datastore.allocate_ids task_key, 5
104
- #
105
- def allocate_ids incomplete_key, count = 1
106
- if incomplete_key.complete?
107
- fail Gcloud::Datastore::KeyError,
108
- "An incomplete key must be provided."
109
- end
110
-
111
- ensure_service!
112
- incomplete_keys = count.times.map { incomplete_key.to_grpc }
113
- allocate_res = service.allocate_ids(*incomplete_keys)
114
- allocate_res.keys.map { |key| Key.from_grpc key }
115
- end
116
-
117
- ##
118
- # Persist one or more entities to the Datastore.
119
- #
120
- # @param [Entity] entities One or more entity objects to be saved.
121
- #
122
- # @return [Array<Gcloud::Datastore::Entity>]
123
- #
124
- # @example Insert a new entity:
125
- # task = datastore.entity "Task" do |t|
126
- # t["type"] = "Personal"
127
- # t["done"] = false
128
- # t["priority"] = 4
129
- # t["description"] = "Learn Cloud Datastore"
130
- # end
131
- # task.key.id #=> nil
132
- # datastore.save task
133
- # task.key.id #=> 123456
134
- #
135
- # @example Insert multiple new entities in a batch:
136
- # task1 = datastore.entity "Task" do |t|
137
- # t["type"] = "Personal"
138
- # t["done"] = false
139
- # t["priority"] = 4
140
- # t["description"] = "Learn Cloud Datastore"
141
- # end
142
- #
143
- # task2 = datastore.entity "Task" do |t|
144
- # t["type"] = "Personal"
145
- # t["done"] = false
146
- # t["priority"] = 5
147
- # t["description"] = "Integrate Cloud Datastore"
148
- # end
149
- #
150
- # task_key1, task_key2 = datastore.save(task1, task2).map(&:key)
151
- #
152
- # @example Update an existing entity:
153
- # task = datastore.find "Task", "sampleTask"
154
- # task["priority"] = 5
155
- # datastore.save task
156
- #
157
- def save *entities
158
- commit { |c| c.save(*entities) }
159
- end
160
- alias_method :upsert, :save
161
-
162
- ##
163
- # Insert one or more entities to the Datastore. An InvalidArgumentError
164
- # will raised if the entities cannot be inserted.
165
- #
166
- # @param [Entity] entities One or more entity objects to be inserted.
167
- #
168
- # @return [Array<Gcloud::Datastore::Entity>]
169
- #
170
- # @example Insert a new entity:
171
- # task = datastore.entity "Task" do |t|
172
- # t["type"] = "Personal"
173
- # t["done"] = false
174
- # t["priority"] = 4
175
- # t["description"] = "Learn Cloud Datastore"
176
- # end
177
- # task.key.id #=> nil
178
- # datastore.insert task
179
- # task.key.id #=> 123456
180
- #
181
- # @example Insert multiple new entities in a batch:
182
- # task1 = datastore.entity "Task" do |t|
183
- # t["type"] = "Personal"
184
- # t["done"] = false
185
- # t["priority"] = 4
186
- # t["description"] = "Learn Cloud Datastore"
187
- # end
188
- #
189
- # task2 = datastore.entity "Task" do |t|
190
- # t["type"] = "Personal"
191
- # t["done"] = false
192
- # t["priority"] = 5
193
- # t["description"] = "Integrate Cloud Datastore"
194
- # end
195
- #
196
- # task_key1, task_key2 = datastore.insert(task1, task2).map(&:key)
197
- #
198
- def insert *entities
199
- commit { |c| c.insert(*entities) }
200
- end
201
-
202
- ##
203
- # Update one or more entities to the Datastore. An InvalidArgumentError
204
- # will raised if the entities cannot be updated.
205
- #
206
- # @param [Entity] entities One or more entity objects to be updated.
207
- #
208
- # @return [Array<Gcloud::Datastore::Entity>]
209
- #
210
- # @example Update an existing entity:
211
- # task = datastore.find "Task", "sampleTask"
212
- # task["done"] = true
213
- # datastore.save task
214
- #
215
- # @example update multiple new entities in a batch:
216
- # query = datastore.query("Task").where("done", "=", false)
217
- # tasks = datastore.run query
218
- # tasks.each { |t| t["done"] = true }
219
- # datastore.update tasks
220
- #
221
- def update *entities
222
- commit { |c| c.update(*entities) }
223
- end
224
-
225
- ##
226
- # Remove entities from the Datastore.
227
- #
228
- # @param [Entity, Key] entities_or_keys One or more Entity or Key objects
229
- # to remove.
230
- #
231
- # @return [Boolean] Returns `true` if successful
232
- #
233
- # @example
234
- # gcloud = Gcloud.new
235
- # datastore = gcloud.datastore
236
- # datastore.delete task1, task2
237
- #
238
- def delete *entities_or_keys
239
- commit { |c| c.delete(*entities_or_keys) }
240
- true
241
- end
242
-
243
- ##
244
- # Make multiple changes in a single commit.
245
- #
246
- # @yield [commit] a block for making changes
247
- # @yieldparam [Commit] commit The object that changes are made on
248
- #
249
- # @return [Array<Gcloud::Datastore::Entity>] The entities that were
250
- # persisted.
251
- #
252
- # @example
253
- # gcloud = Gcloud.new
254
- # datastore = gcloud.datastore
255
- # datastore.commit do |c|
256
- # c.save task3, task4
257
- # c.delete task1, task2
258
- # end
259
- #
260
- def commit
261
- return unless block_given?
262
- c = Commit.new
263
- yield c
264
-
265
- ensure_service!
266
- commit_res = service.commit c.mutations
267
- entities = c.entities
268
- returned_keys = commit_res.mutation_results.map(&:key)
269
- returned_keys.each_with_index do |key, index|
270
- next if entities[index].nil?
271
- entities[index].key = Key.from_grpc(key) unless key.nil?
272
- end
273
- entities.each { |e| e.key.freeze unless e.persisted? }
274
- entities
275
- end
276
-
277
- ##
278
- # Retrieve an entity by key.
279
- #
280
- # @param [Key, String] key_or_kind A Key object or `kind` string value.
281
- # @param [Integer, String, nil] id_or_name The Key's `id` or `name` value
282
- # if a `kind` was provided in the first parameter.
283
- # @param [Symbol] consistency The non-transactional read consistency to
284
- # use. Cannot be set to `:strong` for global queries. Accepted values
285
- # are `:eventual` and `:strong`.
286
- #
287
- # The default consistency depends on the type of lookup used. See
288
- # [Eventual Consistency in Google Cloud
289
- # Datastore](https://cloud.google.com/datastore/docs/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore/#h.tf76fya5nqk8)
290
- # for more information.
291
- #
292
- # @return [Gcloud::Datastore::Entity, nil]
293
- #
294
- # @example Finding an entity with a key:
295
- # task_key = datastore.key "Task", "sampleTask"
296
- # task = datastore.find task_key
297
- #
298
- # @example Finding an entity with a `kind` and `id`/`name`:
299
- # task = datastore.find "Task", "sampleTask"
300
- #
301
- def find key_or_kind, id_or_name = nil, consistency: nil
302
- key = key_or_kind
303
- unless key.is_a? Gcloud::Datastore::Key
304
- key = Key.new key_or_kind, id_or_name
305
- end
306
- find_all(key, consistency: consistency).first
307
- end
308
- alias_method :get, :find
309
-
310
- ##
311
- # Retrieve the entities for the provided keys. The order of results is
312
- # undefined and has no relation to the order of `keys` arguments.
313
- #
314
- # @param [Key] keys One or more Key objects to find records for.
315
- # @param [Symbol] consistency The non-transactional read consistency to
316
- # use. Cannot be set to `:strong` for global queries. Accepted values
317
- # are `:eventual` and `:strong`.
318
- #
319
- # The default consistency depends on the type of lookup used. See
320
- # [Eventual Consistency in Google Cloud
321
- # Datastore](https://cloud.google.com/datastore/docs/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore/#h.tf76fya5nqk8)
322
- # for more information.
323
- #
324
- # @return [Gcloud::Datastore::Dataset::LookupResults]
325
- #
326
- # @example
327
- # gcloud = Gcloud.new
328
- # datastore = gcloud.datastore
329
- #
330
- # task_key1 = datastore.key "Task", "sampleTask1"
331
- # task_key2 = datastore.key "Task", "sampleTask2"
332
- # tasks = datastore.find_all task_key1, task_key2
333
- #
334
- def find_all *keys, consistency: nil
335
- ensure_service!
336
- check_consistency! consistency
337
- lookup_res = service.lookup(*Array(keys).flatten.map(&:to_grpc),
338
- consistency: consistency)
339
- LookupResults.from_grpc lookup_res, service, consistency
340
- end
341
- alias_method :lookup, :find_all
342
-
343
- ##
344
- # Retrieve entities specified by a Query.
345
- #
346
- # @param [Query, GqlQuery] query The object with the search criteria.
347
- # @param [String] namespace The namespace the query is to run within.
348
- # @param [Symbol] consistency The non-transactional read consistency to
349
- # use. Cannot be set to `:strong` for global queries. Accepted values
350
- # are `:eventual` and `:strong`.
351
- #
352
- # The default consistency depends on the type of query used. See
353
- # [Eventual Consistency in Google Cloud
354
- # Datastore](https://cloud.google.com/datastore/docs/articles/balancing-strong-and-eventual-consistency-with-google-cloud-datastore/#h.tf76fya5nqk8)
355
- # for more information.
356
- #
357
- # @return [Gcloud::Datastore::Dataset::QueryResults]
358
- #
359
- # @example
360
- # query = datastore.query("Task").
361
- # where("done", "=", false)
362
- # tasks = datastore.run query
363
- #
364
- # @example Run an ancestor query with eventual consistency:
365
- # task_list_key = datastore.key "TaskList", "default"
366
- # query.kind("Task").
367
- # ancestor(task_list_key)
368
- #
369
- # tasks = datastore.run query, consistency: :eventual
370
- #
371
- # @example Run the query within a namespace with the `namespace` option:
372
- # query = datastore.query("Task").
373
- # where("done", "=", false)
374
- # tasks = datastore.run query, namespace: "ns~todo-project"
375
- #
376
- # @example Run the query with a GQL string.
377
- # gql_query = datastore.gql "SELECT * FROM Task WHERE done = @done",
378
- # done: false
379
- # tasks = datastore.run gql_query
380
- #
381
- # @example Run the GQL query within a namespace with `namespace` option:
382
- # gql_query = datastore.gql "SELECT * FROM Task WHERE done = @done",
383
- # done: false
384
- # tasks = datastore.run gql_query, namespace: "ns~todo-project"
385
- #
386
- def run query, namespace: nil, consistency: nil
387
- ensure_service!
388
- unless query.is_a?(Query) || query.is_a?(GqlQuery)
389
- fail ArgumentError, "Cannot run a #{query.class} object."
390
- end
391
- check_consistency! consistency
392
- query_res = service.run_query query.to_grpc, namespace,
393
- consistency: consistency
394
- QueryResults.from_grpc query_res, service, namespace, query.to_grpc.dup
395
- end
396
- alias_method :run_query, :run
397
-
398
- ##
399
- # Creates a Datastore Transaction.
400
- #
401
- # @yield [tx] a block yielding a new transaction
402
- # @yieldparam [Transaction] tx the transaction object
403
- #
404
- # @example Runs the given block in a database transaction:
405
- # require "gcloud"
406
- #
407
- # gcloud = Gcloud.new
408
- # datastore = gcloud.datastore
409
- #
410
- # task = datastore.entity "Task", "sampleTask" do |t|
411
- # t["type"] = "Personal"
412
- # t["done"] = false
413
- # t["priority"] = 4
414
- # t["description"] = "Learn Cloud Datastore"
415
- # end
416
- #
417
- # datastore.transaction do |tx|
418
- # if tx.find(task.key).nil?
419
- # tx.save task
420
- # end
421
- # end
422
- #
423
- # @example If no block is given, a Transaction object is returned:
424
- # require "gcloud"
425
- #
426
- # gcloud = Gcloud.new
427
- # datastore = gcloud.datastore
428
- #
429
- # task = datastore.entity "Task", "sampleTask" do |t|
430
- # t["type"] = "Personal"
431
- # t["done"] = false
432
- # t["priority"] = 4
433
- # t["description"] = "Learn Cloud Datastore"
434
- # end
435
- #
436
- # tx = datastore.transaction
437
- # begin
438
- # if tx.find(task.key).nil?
439
- # tx.save task
440
- # end
441
- # tx.commit
442
- # rescue
443
- # tx.rollback
444
- # end
445
- #
446
- def transaction
447
- tx = Transaction.new service
448
- return tx unless block_given?
449
-
450
- begin
451
- yield tx
452
- tx.commit
453
- rescue
454
- begin
455
- tx.rollback
456
- rescue
457
- raise TransactionError, "Transaction failed to commit and rollback."
458
- end
459
- raise TransactionError, "Transaction failed to commit."
460
- end
461
- end
462
-
463
- ##
464
- # Create a new Query instance. This is a convenience method to make the
465
- # creation of Query objects easier.
466
- #
467
- # @param [String] kinds The kind of entities to query. This is optional.
468
- #
469
- # @return [Gcloud::Datastore::Query]
470
- #
471
- # @example
472
- # query = datastore.query("Task").
473
- # where("done", "=", false)
474
- # tasks = datastore.run query
475
- #
476
- # @example The previous example is equivalent to:
477
- # query = Gcloud::Datastore::Query.new.
478
- # kind("Task").
479
- # where("done", "=", false)
480
- # tasks = datastore.run query
481
- #
482
- def query *kinds
483
- query = Query.new
484
- query.kind(*kinds) unless kinds.empty?
485
- query
486
- end
487
-
488
- ##
489
- # Create a new GqlQuery instance. This is a convenience method to make the
490
- # creation of GqlQuery objects easier.
491
- #
492
- # @param [String] query The GQL query string.
493
- # @param [Hash] bindings Named bindings for the GQL query string, each
494
- # key must match regex `[A-Za-z_$][A-Za-z_$0-9]*`, must not match regex
495
- # `__.*__`, and must not be `""`. The value must be an `Object` that can
496
- # be stored as an Entity property value, or a `Cursor`.
497
- #
498
- # @return [Gcloud::Datastore::GqlQuery]
499
- #
500
- # @example
501
- # gql_query = datastore.gql "SELECT * FROM Task WHERE done = @done",
502
- # done: false
503
- # tasks = datastore.run gql_query
504
- #
505
- # @example The previous example is equivalent to:
506
- # gql_query = Gcloud::Datastore::GqlQuery.new
507
- # gql_query.query_string = "SELECT * FROM Task WHERE done = @done"
508
- # gql_query.named_bindings = {done: false}
509
- # tasks = datastore.run gql_query
510
- #
511
- def gql query, bindings = {}
512
- gql = GqlQuery.new
513
- gql.query_string = query
514
- gql.named_bindings = bindings unless bindings.empty?
515
- gql
516
- end
517
-
518
- ##
519
- # Create a new Key instance. This is a convenience method to make the
520
- # creation of Key objects easier.
521
- #
522
- # @param [Array<Array(String,(String|Integer|nil))>] path An optional list
523
- # of pairs for the key's path. Each pair may include the key's kind
524
- # (String) and an id (Integer) or name (String). This is optional.
525
- # @param [String] project The project of the Key. This is optional.
526
- # @param [String] namespace namespace kind of the Key. This is optional.
527
- #
528
- # @return [Gcloud::Datastore::Key]
529
- #
530
- # @example
531
- # task_key = datastore.key "Task", "sampleTask"
532
- #
533
- # @example The previous example is equivalent to:
534
- # task_key = Gcloud::Datastore::Key.new "Task", "sampleTask"
535
- #
536
- # @example Create an empty key:
537
- # key = datastore.key
538
- #
539
- # @example Create an incomplete key:
540
- # key = datastore.key "User"
541
- #
542
- # @example Create a key with a parent:
543
- # key = datastore.key [["TaskList", "default"], ["Task", "sampleTask"]]
544
- # key.path #=> [["TaskList", "default"], ["Task", "sampleTask"]]
545
- #
546
- # @example Create a key with multi-level ancestry:
547
- # key = datastore.key([
548
- # ["User", "alice"],
549
- # ["TaskList", "default"],
550
- # ["Task", "sampleTask"]
551
- # ])
552
- # key.path #=> [["User", "alice"], ["TaskList", "default"], [ ... ]]
553
- #
554
- # @example Create an incomplete key with a parent:
555
- # key = datastore.key "TaskList", "default", "Task"
556
- # key.path #=> [["TaskList", "default"], ["Task", nil]]
557
- #
558
- # @example Create a key with a project and namespace:
559
- # key = datastore.key ["TaskList", "default"], ["Task", "sampleTask"],
560
- # project: "my-todo-project",
561
- # namespace: "ns~todo-project"
562
- # key.path #=> [["TaskList", "default"], ["Task", "sampleTask"]]
563
- # key.project #=> "my-todo-project",
564
- # key.namespace #=> "ns~todo-project"
565
- #
566
- def key *path, project: nil, namespace: nil
567
- path = path.flatten.each_slice(2).to_a # group in pairs
568
- kind, id_or_name = path.pop
569
- Key.new(kind, id_or_name).tap do |k|
570
- k.project = project
571
- k.namespace = namespace
572
- unless path.empty?
573
- k.parent = key path, project: project, namespace: namespace
574
- end
575
- end
576
- end
577
-
578
- ##
579
- # Create a new empty Entity instance. This is a convenience method to make
580
- # the creation of Entity objects easier.
581
- #
582
- # @param [Key, Array<Array(String,(String|Integer|nil))>] key_or_path An
583
- # optional list of pairs for the key's path. Each pair may include the #
584
- # key's kind (String) and an id (Integer) or name (String). This is #
585
- # optional.
586
- # @param [String] project The project of the Key. This is optional.
587
- # @param [String] namespace namespace kind of the Key. This is optional.
588
- # @yield [entity] a block yielding a new entity
589
- # @yieldparam [Entity] entity the newly created entity object
590
- #
591
- # @return [Gcloud::Datastore::Entity]
592
- #
593
- # @example
594
- # task = datastore.entity
595
- #
596
- # @example The previous example is equivalent to:
597
- # task = Gcloud::Datastore::Entity.new
598
- #
599
- # @example The key can also be passed in as an object:
600
- # task_key = datastore.key "Task", "sampleTask"
601
- # task = datastore.entity task_key
602
- #
603
- # @example Or the key values can be passed in as parameters:
604
- # task = datastore.entity "Task", "sampleTask"
605
- #
606
- # @example The previous example is equivalent to:
607
- # task_key = Gcloud::Datastore::Key.new "Task", "sampleTask"
608
- # task = Gcloud::Datastore::Entity.new
609
- # task.key = task_key
610
- #
611
- # @example The newly created entity can also be configured using a block:
612
- # task = datastore.entity "Task", "sampleTask" do |t|
613
- # t["type"] = "Personal"
614
- # t["done"] = false
615
- # t["priority"] = 4
616
- # t["description"] = "Learn Cloud Datastore"
617
- # end
618
- #
619
- # @example The previous example is equivalent to:
620
- # task_key = Gcloud::Datastore::Key.new "Task", "sampleTask"
621
- # task = Gcloud::Datastore::Entity.new
622
- # task.key = task_key
623
- # task["type"] = "Personal"
624
- # task["done"] = false
625
- # task["priority"] = 4
626
- # task["description"] = "Learn Cloud Datastore"
627
- #
628
- def entity *key_or_path, project: nil, namespace: nil
629
- entity = Entity.new
630
-
631
- # Set the key
632
- if key_or_path.flatten.first.is_a? Gcloud::Datastore::Key
633
- entity.key = key_or_path.flatten.first
634
- else
635
- entity.key = key key_or_path, project: project, namespace: namespace
636
- end
637
-
638
- yield entity if block_given?
639
-
640
- entity
641
- end
642
-
643
- protected
644
-
645
- ##
646
- # @private Raise an error unless an active connection to the service is
647
- # available.
648
- def ensure_service!
649
- fail "Must have active connection to service" unless service
650
- end
651
-
652
- def check_consistency! consistency
653
- fail(ArgumentError,
654
- format("Consistency must be :eventual or :strong, not %s.",
655
- consistency.inspect)
656
- ) unless [:eventual, :strong, nil].include? consistency
657
- end
658
- end
659
- end
660
- end