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,211 +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
- require "gcloud/datastore/entity"
17
- require "gcloud/datastore/key"
18
-
19
- module Gcloud
20
- module Datastore
21
- ##
22
- # # GqlQuery
23
- #
24
- # Represents a GQL query.
25
- #
26
- # GQL is a SQL-like language for retrieving entities or keys from Datastore.
27
- #
28
- # @see https://cloud.google.com/datastore/docs/apis/gql/gql_reference GQL
29
- # Reference
30
- #
31
- # @example
32
- # gql_query = Gcloud::Datastore::GqlQuery.new
33
- # gql_query.query_string = "SELECT * FROM Task ORDER BY created ASC"
34
- # tasks = datastore.run gql_query
35
- #
36
- class GqlQuery
37
- ##
38
- # Returns a new GqlQuery instance.
39
- #
40
- # @example
41
- # gql_query = Gcloud::Datastore::GqlQuery.new
42
- #
43
- def initialize
44
- @grpc = Google::Datastore::V1beta3::GqlQuery.new
45
- end
46
-
47
- ##
48
- # The GQL query string for the query. The string may contain named
49
- # or positional argument binding sites that start with `@`. Corresponding
50
- # binding values should be set with {#named_bindings=} or
51
- # {#positional_bindings=}.
52
- #
53
- # @return [String] a GQL statement
54
- #
55
- def query_string
56
- gql = @grpc.query_string.dup
57
- gql.freeze
58
- gql
59
- end
60
-
61
- ##
62
- # Sets the GQL query string for the query. The string may contain named
63
- # or positional argument binding sites that start with `@`. Corresponding
64
- # binding values should be set with {#named_bindings=} or
65
- # {#positional_bindings=}.
66
- #
67
- # See the [GQL
68
- # Reference](https://cloud.google.com/datastore/docs/apis/gql/gql_reference).
69
- #
70
- # @param [String] new_query_string a valid GQL statement
71
- #
72
- # @example
73
- # gql_query = Gcloud::Datastore::GqlQuery.new
74
- # gql_query.query_string = "SELECT * FROM Task " \
75
- # "WHERE done = @done AND priority = @priority"
76
- # gql_query.named_bindings = {done: false, priority: 4}
77
- #
78
- def query_string= new_query_string
79
- @grpc.query_string = new_query_string.to_s
80
- end
81
-
82
- ##
83
- # Whether the query may contain literal values. When false, the query
84
- # string must not contain any literals and instead must bind all values
85
- # using {#named_bindings=} or {#positional_bindings=}.
86
- #
87
- # @return [Boolean] `true` if the query may contain literal values
88
- #
89
- def allow_literals
90
- @grpc.allow_literals
91
- end
92
-
93
- ##
94
- # Sets whether the query may contain literal values. When false, the query
95
- # string must not contain any literals and instead must bind all values
96
- # using {#named_bindings=} or {#positional_bindings=}.
97
- #
98
- # @param [Boolean] new_allow_literals `true` if the query may contain
99
- # literal values
100
- #
101
- # @example
102
- # gql_query = Gcloud::Datastore::GqlQuery.new
103
- # gql_query.query_string = "SELECT * FROM Task " \
104
- # "WHERE completed = false AND priority = 4"
105
- # gql_query.allow_literals = true
106
- #
107
- def allow_literals= new_allow_literals
108
- @grpc.allow_literals = new_allow_literals
109
- end
110
-
111
- ##
112
- # The named binding values for a query that contains named argument
113
- # binding sites that start with `@`.
114
- #
115
- # @return [Hash] a frozen hash that maps the binding site names in the
116
- # query string to valid GQL arguments
117
- #
118
- def named_bindings
119
- bindings = Hash[@grpc.named_bindings.map do |name, gql_query_param|
120
- if gql_query_param.cursor
121
- [name, Cursor.from_grpc(gql_query_param.cursor)]
122
- else
123
- [name, GRPCUtils.from_value(gql_query_param.value)]
124
- end
125
- end]
126
- bindings.freeze
127
- bindings
128
- end
129
-
130
- ##
131
- # Sets named binding values for a query that contains named argument
132
- # binding sites that start with `@`.
133
- #
134
- # @param [Hash] new_named_bindings a hash that maps the binding site names
135
- # in the query string to valid GQL arguments
136
- #
137
- # @example
138
- # gql_query = Gcloud::Datastore::GqlQuery.new
139
- # gql_query.query_string = "SELECT * FROM Task " \
140
- # "WHERE done = @done AND priority = @priority"
141
- # gql_query.named_bindings = {done: false, priority: 4}
142
- #
143
- def named_bindings= new_named_bindings
144
- @grpc.named_bindings.clear
145
- new_named_bindings.map do |name, value|
146
- if value.is_a? Gcloud::Datastore::Cursor
147
- @grpc.named_bindings[name.to_s] = \
148
- Google::Datastore::V1beta3::GqlQueryParameter.new(
149
- cursor: value.to_grpc)
150
- else
151
- @grpc.named_bindings[name.to_s] = \
152
- Google::Datastore::V1beta3::GqlQueryParameter.new(
153
- value: GRPCUtils.to_value(value))
154
- end
155
- end
156
- end
157
-
158
- ##
159
- # The binding values for a query that contains numbered argument binding
160
- # sites that start with `@`.
161
- #
162
- # @return [Array] a frozen array containing the query arguments in the
163
- # order of the numbered binding sites in the query string
164
- #
165
- def positional_bindings
166
- bindings = @grpc.positional_bindings.map do |gql_query_param|
167
- if gql_query_param.cursor
168
- Cursor.from_grpc gql_query_param.cursor
169
- else
170
- GRPCUtils.from_value gql_query_param.value
171
- end
172
- end
173
- bindings.freeze
174
- bindings
175
- end
176
-
177
- ##
178
- # Sets the binding values for a query that contains numbered argument
179
- # binding sites that start with `@`.
180
- #
181
- # @param [Array] new_positional_bindings query arguments in the order
182
- # of the numbered binding sites in the query string
183
- #
184
- # @example
185
- # gql_query = Gcloud::Datastore::GqlQuery.new
186
- # gql_query.query_string = "SELECT * FROM Task" \
187
- # "WHERE completed = @1 AND priority = @2"
188
- # gql_query.positional_bindings = [false, 4]
189
- #
190
- def positional_bindings= new_positional_bindings
191
- @grpc.positional_bindings.clear
192
- new_positional_bindings.map do |value|
193
- if value.is_a? Gcloud::Datastore::Cursor
194
- @grpc.positional_bindings << \
195
- Google::Datastore::V1beta3::GqlQueryParameter.new(
196
- cursor: value.to_grpc)
197
- else
198
- @grpc.positional_bindings << \
199
- Google::Datastore::V1beta3::GqlQueryParameter.new(
200
- value: GRPCUtils.to_value(value))
201
- end
202
- end
203
- end
204
-
205
- # @private
206
- def to_grpc
207
- @grpc
208
- end
209
- end
210
- end
211
- end
@@ -1,132 +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
- require "gcloud/grpc_utils"
17
- require "gcloud/datastore/errors"
18
- require "stringio"
19
- require "base64"
20
-
21
- module Gcloud
22
- ##
23
- # @private Conversion to/from Datastore GRPC objects.
24
- # This file adds Datastore methods to GRPCUtils.
25
- module GRPCUtils
26
- # rubocop:disable all
27
-
28
- PROP_FILTER_OPS = { "<" => :LESS_THAN,
29
- "lt" => :LESS_THAN,
30
- "<=" => :LESS_THAN_OR_EQUAL,
31
- "lte" => :LESS_THAN_OR_EQUAL,
32
- ">" => :GREATER_THAN,
33
- "gt" => :GREATER_THAN,
34
- ">=" => :GREATER_THAN_OR_EQUAL,
35
- "gte" => :GREATER_THAN_OR_EQUAL,
36
- "=" => :EQUAL,
37
- "eq" => :EQUAL,
38
- "eql" => :EQUAL,
39
- "~" => :HAS_ANCESTOR,
40
- "~>" => :HAS_ANCESTOR,
41
- "ancestor" => :HAS_ANCESTOR,
42
- "has_ancestor" => :HAS_ANCESTOR,
43
- "has ancestor" => :HAS_ANCESTOR }
44
-
45
- ##
46
- # Get a property filter operator from op
47
- def self.to_prop_filter_op op
48
- PROP_FILTER_OPS[op.to_s.downcase] || :EQUAL
49
- end
50
-
51
- ##
52
- # Gets an object from a Google::Datastore::V1beta3::Value.
53
- def self.from_value grpc_value
54
- if grpc_value.value_type == :null_value
55
- return nil
56
- elsif grpc_value.value_type == :key_value
57
- return Gcloud::Datastore::Key.from_grpc(grpc_value.key_value)
58
- elsif grpc_value.value_type == :entity_value
59
- return Gcloud::Datastore::Entity.from_grpc(grpc_value.entity_value)
60
- elsif grpc_value.value_type == :boolean_value
61
- return grpc_value.boolean_value
62
- elsif grpc_value.value_type == :double_value
63
- return grpc_value.double_value
64
- elsif grpc_value.value_type == :integer_value
65
- return grpc_value.integer_value
66
- elsif grpc_value.value_type == :string_value
67
- return grpc_value.string_value
68
- elsif grpc_value.value_type == :array_value
69
- return Array(grpc_value.array_value.values).map { |v| from_value v }
70
- elsif grpc_value.value_type == :timestamp_value
71
- return Time.at grpc_value.timestamp_value.seconds,
72
- grpc_value.timestamp_value.nanos/1000.0
73
- elsif grpc_value.value_type == :geo_point_value
74
- return grpc_value.geo_point_value.to_hash
75
- elsif grpc_value.value_type == :blob_value
76
- return StringIO.new(grpc_value.blob_value.force_encoding("ASCII-8BIT"))
77
- else
78
- nil
79
- end
80
- end
81
-
82
- ##
83
- # Stores an object into a Google::Datastore::V1beta3::Value.
84
- def self.to_value value
85
- v = Google::Datastore::V1beta3::Value.new
86
- if NilClass === value
87
- v.null_value = :NULL_VALUE
88
- elsif TrueClass === value
89
- v.boolean_value = true
90
- elsif FalseClass === value
91
- v.boolean_value = false
92
- elsif Integer === value
93
- v.integer_value = value
94
- elsif Float === value
95
- v.double_value = value
96
- elsif defined?(BigDecimal) && BigDecimal === value
97
- v.double_value = value
98
- elsif Gcloud::Datastore::Key === value
99
- v.key_value = value.to_grpc
100
- elsif Gcloud::Datastore::Entity === value
101
- v.entity_value = value.to_grpc
102
- elsif String === value
103
- v.string_value = value
104
- elsif Array === value
105
- v.array_value = Google::Datastore::V1beta3::ArrayValue.new(
106
- values: value.map { |val| to_value val }
107
- )
108
- elsif value.respond_to? :to_time
109
- v.timestamp_value = Google::Protobuf::Timestamp.new(
110
- seconds: value.to_time.to_i, nanos: value.to_time.nsec)
111
- elsif value.respond_to?(:to_hash) && value.keys.sort == [:latitude, :longitude]
112
- v.geo_point_value = Google::Type::LatLng.new(value)
113
- elsif value.respond_to?(:read) && value.respond_to?(:rewind)
114
- value.rewind
115
- v.blob_value = value.read.force_encoding("ASCII-8BIT")
116
- else
117
- fail Gcloud::Datastore::PropertyError,
118
- "A property of type #{value.class} is not supported."
119
- end
120
- v
121
- end
122
-
123
- def self.encode_bytes bytes
124
- Base64.strict_encode64(bytes.to_s).encode("ASCII-8BIT")
125
- end
126
-
127
- def self.decode_bytes bytes
128
- Base64.decode64(bytes.to_s).force_encoding Encoding::ASCII_8BIT
129
- end
130
- # rubocop:enable all
131
- end
132
- end
@@ -1,281 +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
- module Gcloud
17
- module Datastore
18
- ##
19
- # # Key
20
- #
21
- # Every Datastore record has an identifying key, which includes the record's
22
- # entity kind and a unique identifier. The identifier may be either a key
23
- # name string, assigned explicitly by the application, or an integer numeric
24
- # ID, assigned automatically by Datastore.
25
- #
26
- # @example
27
- # task_key = Gcloud::Datastore::Key.new "Task", "sampleTask"
28
- #
29
- class Key
30
- ##
31
- # The kind of the Key.
32
- #
33
- # @return [String]
34
- #
35
- # @example
36
- # key = Gcloud::Datastore::Key.new "TaskList"
37
- # key.kind #=> "TaskList"
38
- # key.kind = "Task"
39
- #
40
- attr_accessor :kind
41
-
42
- ##
43
- # The project of the Key.
44
- #
45
- # @return [String]
46
- #
47
- # @example
48
- # require "gcloud"
49
- #
50
- # gcloud = Gcloud.new "my-todo-project",
51
- # "/path/to/keyfile.json"
52
- #
53
- # datastore = gcloud.datastore
54
- # task = datastore.find "Task", "sampleTask"
55
- # task.key.project #=> "my-todo-project"
56
- #
57
- attr_accessor :project
58
- alias_method :dataset_id, :project
59
- alias_method :dataset_id=, :project=
60
-
61
- ##
62
- # The namespace of the Key.
63
- #
64
- # @return [String, nil]
65
- #
66
- # @example
67
- # require "gcloud"
68
- #
69
- # gcloud = Gcloud.new "my-todo-project",
70
- # "/path/to/keyfile.json"
71
- #
72
- # datastore = gcloud.datastore
73
- # task = datastore.find "Task", "sampleTask"
74
- # task.key.namespace #=> "ns~todo-project"
75
- #
76
- attr_accessor :namespace
77
-
78
- ##
79
- # Create a new Key instance.
80
- #
81
- # @param [String] kind The kind of the Key. This is optional.
82
- # @param [Integer, String] id_or_name The id or name of the Key. This is
83
- # optional.
84
- #
85
- # @return [Gcloud::Datastore::Dataset::Key]
86
- #
87
- # @example
88
- # task_key = Gcloud::Datastore::Key.new "Task", "sampleTask"
89
- #
90
- def initialize kind = nil, id_or_name = nil
91
- @kind = kind
92
- if id_or_name.is_a? Integer
93
- @id = id_or_name
94
- else
95
- @name = id_or_name
96
- end
97
- end
98
-
99
- ##
100
- # @private Set the id of the Key.
101
- # If a name is already present it will be removed.
102
- #
103
- # @return [Integer, nil]
104
- #
105
- # @example
106
- # task_key = Gcloud::Datastore::Key.new "Task", "sampleTask"
107
- # task_key.id #=> nil
108
- # task_key.name #=> "sampleTask"
109
- # task_key.id = 654321
110
- # task_key.id #=> 654321
111
- # task_key.name #=> nil
112
- #
113
- def id= new_id
114
- @name = nil if new_id
115
- @id = new_id
116
- end
117
-
118
- ##
119
- # The id of the Key.
120
- #
121
- # @return [Integer, nil]
122
- #
123
- # @example
124
- # task_key = Gcloud::Datastore::Key.new "Task", 123456
125
- # task_key.id #=> 123456
126
- #
127
- attr_reader :id
128
-
129
- ##
130
- # @private Set the name of the Key.
131
- # If an id is already present it will be removed.
132
- #
133
- # @return [String, nil]
134
- #
135
- # @example
136
- # task_key = Gcloud::Datastore::Key.new "Task", 123456
137
- # task_key.id #=> 123456
138
- # task_key.name #=> nil
139
- # task_key.name = "sampleTask"
140
- # task_key.id #=> nil
141
- # task_key.name #=> "sampleTask"
142
- #
143
- def name= new_name
144
- @id = nil if new_name
145
- @name = new_name
146
- end
147
-
148
- ##
149
- # The name of the Key.
150
- #
151
- # @return [String, nil]
152
- #
153
- # @example
154
- # task_key = Gcloud::Datastore::Key.new "Task", "sampleTask"
155
- # task_key.name #=> "sampleTask"
156
- #
157
- attr_reader :name
158
-
159
- ##
160
- # Set the parent of the Key.
161
- #
162
- # @return [Key, nil]
163
- #
164
- # @example
165
- # task_key = Gcloud::Datastore::Key.new "Task", "sampleTask"
166
- # task_key.parent = Gcloud::Datastore::Key.new "TaskList", "default"
167
- #
168
- # @example With multiple levels:
169
- # user_key = Gcloud::Datastore::Key.new "User", "alice"
170
- # task_list_key = Gcloud::Datastore::Key.new "TaskList", "default"
171
- # task_key = Gcloud::Datastore::Key.new "Task", "sampleTask"
172
- # task_list_key.parent = user_key
173
- # task_key.parent = task_list_key
174
- #
175
- def parent= new_parent
176
- # store key if given an entity
177
- new_parent = new_parent.key if new_parent.respond_to? :key
178
- @parent = new_parent
179
- end
180
-
181
- ##
182
- # The parent of the Key.
183
- #
184
- # @return [Key, nil]
185
- #
186
- # @example
187
- # require "gcloud"
188
- #
189
- # gcloud = Gcloud.new
190
- # datastore = gcloud.datastore
191
- #
192
- # task_list = datastore.find "TaskList", "default"
193
- # query = datastore.query("Task").
194
- # ancestor(task_list)
195
- # lists = datastore.run query
196
- # lists.first.key.parent #=> Key("TaskList", "default")
197
- #
198
- attr_reader :parent
199
-
200
- ##
201
- # Represent the Key's path (including parent) as an array of arrays.
202
- # Each inner array contains two values, the kind and the id or name.
203
- # If neither an id or name exist then nil will be returned.
204
- #
205
- # @return [Array<Array<(String, String)>>]
206
- #
207
- # @example
208
- # task_key = Gcloud::Datastore::Key.new "Task", "sampleTask"
209
- # task_key.parent = Gcloud::Datastore::Key.new "TaskList", "default"
210
- # task_key.path #=> [["TaskList", "default"], ["Task", "sampleTask"]]
211
- #
212
- def path
213
- new_path = parent ? parent.path : []
214
- new_path << [kind, (id || name)]
215
- end
216
-
217
- ##
218
- # Determine if the key is complete.
219
- # A complete key has either an id or a name.
220
- #
221
- # Inverse of {#incomplete?}
222
- def complete?
223
- !incomplete?
224
- end
225
-
226
- ##
227
- # Determine if the key is incomplete.
228
- # An incomplete key has neither an id nor a name.
229
- #
230
- # Inverse of {#complete?}
231
- def incomplete?
232
- kind.nil? || (id.nil? && (name.nil? || name.empty?))
233
- end
234
-
235
- ##
236
- # The number of bytes the Key will take to serialize during API calls.
237
- def serialized_size
238
- to_grpc.to_proto.length
239
- end
240
-
241
- ##
242
- # @private Convert the Key to a Google::Datastore::V1beta3::Key object.
243
- def to_grpc
244
- grpc_path = path.map do |pe_kind, pe_id_or_name|
245
- path_args = { kind: pe_kind }
246
- if pe_id_or_name.is_a? Integer
247
- path_args[:id] = pe_id_or_name
248
- elsif pe_id_or_name.is_a? String
249
- path_args[:name] = pe_id_or_name unless pe_id_or_name.empty?
250
- end
251
- Google::Datastore::V1beta3::Key::PathElement.new(path_args)
252
- end
253
- grpc = Google::Datastore::V1beta3::Key.new(path: grpc_path)
254
- if project || namespace
255
- grpc.partition_id = Google::Datastore::V1beta3::PartitionId.new(
256
- project_id: project.to_s, namespace_id: namespace.to_s)
257
- end
258
- grpc
259
- end
260
-
261
- ##
262
- # @private Create a new Key from a Google::Datastore::V1beta3::Key object.
263
- def self.from_grpc grpc
264
- key_grpc = grpc.dup
265
- key = Key.new
266
- path_grpc = key_grpc.path.pop
267
- if path_grpc
268
- key = Key.new path_grpc.kind, (path_grpc.id || path_grpc.name)
269
- end
270
- if key_grpc.partition_id
271
- key.project = key_grpc.partition_id.project_id
272
- key.namespace = key_grpc.partition_id.namespace_id
273
- end
274
- key.parent = Key.from_grpc(key_grpc) if key_grpc.path.count > 0
275
- # Freeze the key to make it immutable.
276
- key.freeze
277
- key
278
- end
279
- end
280
- end
281
- end