gcloud 0.12.2 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,29 +0,0 @@
1
- # Copyright 2015 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 Bigquery
20
- ##
21
- # @private Represents the Oauth2 signing logic for Bigquery.
22
- class Credentials < Gcloud::Credentials
23
- SCOPE = ["https://www.googleapis.com/auth/bigquery"]
24
- PATH_ENV_VARS = %w(BIGQUERY_KEYFILE GCLOUD_KEYFILE GOOGLE_CLOUD_KEYFILE)
25
- JSON_ENV_VARS = %w(BIGQUERY_KEYFILE_JSON GCLOUD_KEYFILE_JSON
26
- GOOGLE_CLOUD_KEYFILE_JSON)
27
- end
28
- end
29
- end
@@ -1,239 +0,0 @@
1
- # Copyright 2015 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 "delegate"
17
- require "gcloud/bigquery/service"
18
-
19
- module Gcloud
20
- module Bigquery
21
- ##
22
- # # Data
23
- #
24
- # Represents {Table} Data as a list of name/value pairs.
25
- # Also contains metadata such as `etag` and `total`.
26
- class Data < DelegateClass(::Array)
27
- ##
28
- # @private The {Table} object the data belongs to.
29
- attr_accessor :table
30
-
31
- ##
32
- # @private The Google API Client object.
33
- attr_accessor :gapi
34
-
35
- # @private
36
- def initialize arr = []
37
- @table = nil
38
- @gapi = {}
39
- super arr
40
- end
41
-
42
- ##
43
- # The resource type of the API response.
44
- def kind
45
- @gapi.kind
46
- end
47
-
48
- ##
49
- # The etag.
50
- def etag
51
- @gapi.etag
52
- end
53
-
54
- ##
55
- # A token used for paging results.
56
- def token
57
- @gapi.page_token
58
- end
59
-
60
- # The total number of rows in the complete table.
61
- def total
62
- Integer @gapi.total_rows
63
- rescue
64
- nil
65
- end
66
-
67
- ##
68
- # Whether there is a next page of data.
69
- #
70
- # @return [Boolean]
71
- #
72
- # @example
73
- # require "gcloud"
74
- #
75
- # gcloud = Gcloud.new
76
- # bigquery = gcloud.bigquery
77
- # table = dataset.table "my_table"
78
- #
79
- # data = table.data
80
- # if data.next?
81
- # next_data = data.next
82
- # end
83
- #
84
- def next?
85
- !token.nil?
86
- end
87
-
88
- ##
89
- # Retrieve the next page of data.
90
- #
91
- # @return [Data]
92
- #
93
- # @example
94
- # require "gcloud"
95
- #
96
- # gcloud = Gcloud.new
97
- # bigquery = gcloud.bigquery
98
- # table = dataset.table "my_table"
99
- #
100
- # data = table.data
101
- # if data.next?
102
- # next_data = data.next
103
- # end
104
- #
105
- def next
106
- return nil unless next?
107
- ensure_table!
108
- table.data token: token
109
- end
110
-
111
- ##
112
- # Retrieves all rows by repeatedly loading {#next} until {#next?} returns
113
- # `false`. Calls the given block once for each row, which is passed as the
114
- # parameter.
115
- #
116
- # An Enumerator is returned if no block is given.
117
- #
118
- # This method may make several API calls until all rows are retrieved. Be
119
- # sure to use as narrow a search criteria as possible. Please use with
120
- # caution.
121
- #
122
- # @param [Integer] request_limit The upper limit of API requests to make
123
- # to load all data. Default is no limit.
124
- # @yield [row] The block for accessing each row of data.
125
- # @yieldparam [Hash] row The row object.
126
- #
127
- # @return [Enumerator]
128
- #
129
- # @example Iterating each rows by passing a block:
130
- # require "gcloud"
131
- #
132
- # gcloud = Gcloud.new
133
- # bigquery = gcloud.bigquery
134
- # table = dataset.table "my_table"
135
- #
136
- # table.data.all do |row|
137
- # puts row["word"]
138
- # end
139
- #
140
- # @example Using the enumerator by not passing a block:
141
- # require "gcloud"
142
- #
143
- # gcloud = Gcloud.new
144
- # bigquery = gcloud.bigquery
145
- # table = dataset.table "my_table"
146
- #
147
- # words = table.data.all.map do |row|
148
- # row["word"]
149
- # end
150
- #
151
- # @example Limit the number of API calls made:
152
- # require "gcloud"
153
- #
154
- # gcloud = Gcloud.new
155
- # bigquery = gcloud.bigquery
156
- # table = dataset.table "my_table"
157
- #
158
- # table.data.all(request_limit: 10) do |row|
159
- # puts row["word"]
160
- # end
161
- #
162
- def all request_limit: nil
163
- request_limit = request_limit.to_i if request_limit
164
- return enum_for(:all, request_limit: request_limit) unless block_given?
165
- results = self
166
- loop do
167
- results.each { |r| yield r }
168
- if request_limit
169
- request_limit -= 1
170
- break if request_limit < 0
171
- end
172
- break unless results.next?
173
- results = results.next
174
- end
175
- end
176
-
177
- ##
178
- # Represents Table Data as a list of positional values (array of arrays).
179
- # No type conversion is made, e.g. numbers are formatted as strings.
180
- def raw
181
- Array(gapi.rows).map { |row| row.f.map(&:v) }
182
- end
183
-
184
- ##
185
- # @private New Data from a response object.
186
- def self.from_gapi gapi, table
187
- formatted_rows = format_rows gapi.rows, table.fields
188
-
189
- data = new formatted_rows
190
- data.table = table
191
- data.gapi = gapi
192
- data
193
- end
194
-
195
- # rubocop:disable all
196
- # Disabled rubocop because this implementation will not last.
197
-
198
- def self.format_rows rows, fields
199
- headers = Array(fields).map { |f| f.name }
200
- field_types = Array(fields).map { |f| f.type }
201
-
202
- Array(rows).map do |row|
203
- values = row.f.map { |f| f.v }
204
- formatted_values = format_values field_types, values
205
- Hash[headers.zip formatted_values]
206
- end
207
- end
208
-
209
- def self.format_values field_types, values
210
- field_types.zip(values).map do |type, value|
211
- begin
212
- if value.nil?
213
- nil
214
- elsif type == "INTEGER"
215
- Integer value
216
- elsif type == "FLOAT"
217
- Float value
218
- elsif type == "BOOLEAN"
219
- (value == "true" ? true : (value == "false" ? false : nil))
220
- else
221
- value
222
- end
223
- rescue
224
- value
225
- end
226
- end
227
- end
228
- # rubocop:enable all
229
-
230
- protected
231
-
232
- ##
233
- # Raise an error unless an active service is available.
234
- def ensure_table!
235
- fail "Must have active connection" unless table
236
- end
237
- end
238
- end
239
- end
@@ -1,753 +0,0 @@
1
- # Copyright 2015 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 "gcloud/errors"
18
- require "gcloud/bigquery/service"
19
- require "gcloud/bigquery/table"
20
- require "gcloud/bigquery/dataset/list"
21
- require "gcloud/bigquery/dataset/access"
22
- require "google/apis/bigquery_v2"
23
-
24
- module Gcloud
25
- module Bigquery
26
- ##
27
- # # Dataset
28
- #
29
- # Represents a Dataset. A dataset is a grouping mechanism that holds zero or
30
- # more tables. Datasets are the lowest level unit of access control; you
31
- # cannot control access at the table level. A dataset is contained within a
32
- # specific project.
33
- #
34
- # @example
35
- # require "gcloud"
36
- #
37
- # gcloud = Gcloud.new
38
- # bigquery = gcloud.bigquery
39
- #
40
- # dataset = bigquery.create_dataset "my_dataset",
41
- # name: "My Dataset",
42
- # description: "This is my Dataset"
43
- #
44
- class Dataset
45
- ##
46
- # @private The Connection object.
47
- attr_accessor :service
48
-
49
- ##
50
- # @private The Google API Client object.
51
- attr_accessor :gapi
52
-
53
- ##
54
- # @private Create an empty Dataset object.
55
- def initialize
56
- @service = nil
57
- @gapi = {}
58
- end
59
-
60
- ##
61
- # A unique ID for this dataset, without the project name.
62
- # The ID must contain only letters (a-z, A-Z), numbers (0-9),
63
- # or underscores (_). The maximum length is 1,024 characters.
64
- #
65
- # @!group Attributes
66
- #
67
- def dataset_id
68
- @gapi.dataset_reference.dataset_id
69
- end
70
-
71
- ##
72
- # The ID of the project containing this dataset.
73
- #
74
- # @!group Attributes
75
- #
76
- def project_id
77
- @gapi.dataset_reference.project_id
78
- end
79
-
80
- ##
81
- # @private
82
- # The gapi fragment containing the Project ID and Dataset ID as a
83
- # camel-cased hash.
84
- def dataset_ref
85
- dataset_ref = @gapi.dataset_reference
86
- dataset_ref = dataset_ref.to_h if dataset_ref.respond_to? :to_h
87
- dataset_ref
88
- end
89
-
90
- ##
91
- # A descriptive name for the dataset.
92
- #
93
- # @!group Attributes
94
- #
95
- def name
96
- @gapi.friendly_name
97
- end
98
-
99
- ##
100
- # Updates the descriptive name for the dataset.
101
- #
102
- # @!group Attributes
103
- #
104
- def name= new_name
105
- @gapi.update! friendly_name: new_name
106
- patch_gapi! :friendly_name
107
- end
108
-
109
- ##
110
- # A string hash of the dataset.
111
- #
112
- # @!group Attributes
113
- #
114
- def etag
115
- ensure_full_data!
116
- @gapi.etag
117
- end
118
-
119
- ##
120
- # A URL that can be used to access the dataset using the REST API.
121
- #
122
- # @!group Attributes
123
- #
124
- def api_url
125
- ensure_full_data!
126
- @gapi.self_link
127
- end
128
-
129
- ##
130
- # A user-friendly description of the dataset.
131
- #
132
- # @!group Attributes
133
- #
134
- def description
135
- ensure_full_data!
136
- @gapi.description
137
- end
138
-
139
- ##
140
- # Updates the user-friendly description of the dataset.
141
- #
142
- # @!group Attributes
143
- #
144
- def description= new_description
145
- @gapi.update! description: new_description
146
- patch_gapi! :description
147
- end
148
-
149
- ##
150
- # The default lifetime of all tables in the dataset, in milliseconds.
151
- #
152
- # @!group Attributes
153
- #
154
- def default_expiration
155
- ensure_full_data!
156
- begin
157
- Integer @gapi.default_table_expiration_ms
158
- rescue
159
- nil
160
- end
161
- end
162
-
163
- ##
164
- # Updates the default lifetime of all tables in the dataset, in
165
- # milliseconds.
166
- #
167
- # @!group Attributes
168
- #
169
- def default_expiration= new_default_expiration
170
- @gapi.update! default_table_expiration_ms: new_default_expiration
171
- patch_gapi! :default_table_expiration_ms
172
- end
173
-
174
- ##
175
- # The time when this dataset was created.
176
- #
177
- # @!group Attributes
178
- #
179
- def created_at
180
- ensure_full_data!
181
- begin
182
- Time.at(Integer(@gapi.creation_time) / 1000.0)
183
- rescue
184
- nil
185
- end
186
- end
187
-
188
- ##
189
- # The date when this dataset or any of its tables was last modified.
190
- #
191
- # @!group Attributes
192
- #
193
- def modified_at
194
- ensure_full_data!
195
- begin
196
- Time.at(Integer(@gapi.last_modified_time) / 1000.0)
197
- rescue
198
- nil
199
- end
200
- end
201
-
202
- ##
203
- # The geographic location where the dataset should reside. Possible
204
- # values include EU and US. The default value is US.
205
- #
206
- # @!group Attributes
207
- #
208
- def location
209
- ensure_full_data!
210
- @gapi.location
211
- end
212
-
213
- ##
214
- # Retrieves the access rules for a Dataset. The rules can be
215
- # updated when passing a block, see {Dataset::Access} for all the methods
216
- # available.
217
- #
218
- # @see https://cloud.google.com/bigquery/access-control BigQuery Access
219
- # Control
220
- #
221
- # @yield [access] a block for setting rules
222
- # @yieldparam [Dataset::Access] access the object accepting rules
223
- #
224
- # @return [Gcloud::Bigquery::Dataset::Access]
225
- #
226
- # @example
227
- # require "gcloud"
228
- #
229
- # gcloud = Gcloud.new
230
- # bigquery = gcloud.bigquery
231
- # dataset = bigquery.dataset "my_dataset"
232
- #
233
- # dataset.access #=> [{"role"=>"OWNER",
234
- # # "specialGroup"=>"projectOwners"},
235
- # # {"role"=>"WRITER",
236
- # # "specialGroup"=>"projectWriters"},
237
- # # {"role"=>"READER",
238
- # # "specialGroup"=>"projectReaders"},
239
- # # {"role"=>"OWNER",
240
- # # "userByEmail"=>"123456789-...com"}]
241
- #
242
- # @example Manage the access rules by passing a block:
243
- # require "gcloud"
244
- #
245
- # gcloud = Gcloud.new
246
- # bigquery = gcloud.bigquery
247
- # dataset = bigquery.dataset "my_dataset"
248
- #
249
- # dataset.access do |access|
250
- # access.add_owner_group "owners@example.com"
251
- # access.add_writer_user "writer@example.com"
252
- # access.remove_writer_user "readers@example.com"
253
- # access.add_reader_special :all
254
- # access.add_reader_view other_dataset_view_object
255
- # end
256
- #
257
- def access
258
- ensure_full_data!
259
- access_builder = Access.from_gapi @gapi
260
- if block_given?
261
- yield access_builder
262
- if access_builder.changed?
263
- @gapi.update! access: access_builder.to_gapi
264
- patch_gapi! :access
265
- end
266
- end
267
- access_builder.freeze
268
- end
269
-
270
- ##
271
- # Permanently deletes the dataset. The dataset must be empty before it can
272
- # be deleted unless the `force` option is set to `true`.
273
- #
274
- # @param [Boolean] force If `true`, delete all the tables in the dataset.
275
- # If `false` and the dataset contains tables, the request will fail.
276
- # Default is `false`.
277
- #
278
- # @return [Boolean] Returns `true` if the dataset was deleted.
279
- #
280
- # @example
281
- # require "gcloud"
282
- #
283
- # gcloud = Gcloud.new
284
- # bigquery = gcloud.bigquery
285
- #
286
- # dataset = bigquery.dataset "my_dataset"
287
- # dataset.delete
288
- #
289
- # @!group Lifecycle
290
- #
291
- def delete force: nil
292
- ensure_service!
293
- service.delete_dataset dataset_id, force
294
- true
295
- end
296
-
297
- ##
298
- # Creates a new table. If you are adapting existing code that was written
299
- # for the [Rest API
300
- # ](https://cloud.google.com/bigquery/docs/reference/v2/tables#resource),
301
- # you can pass the table's schema as a hash (see example.)
302
- #
303
- # @param [String] table_id The ID of the table. The ID must contain only
304
- # letters (a-z, A-Z), numbers (0-9), or underscores (_). The maximum
305
- # length is 1,024 characters.
306
- # @param [String] name A descriptive name for the table.
307
- # @param [String] description A user-friendly description of the table.
308
- # @param [Array<Schema::Field>] fields An array of Schema::Field objects
309
- # specifying the schema's data types for the table. The schema may also
310
- # be configured when passing a block.
311
- # @yield [table] a block for setting the table
312
- # @yieldparam [Table] table the table object to be updated
313
- #
314
- # @return [Gcloud::Bigquery::Table]
315
- #
316
- # @example
317
- # require "gcloud"
318
- #
319
- # gcloud = Gcloud.new
320
- # bigquery = gcloud.bigquery
321
- # dataset = bigquery.dataset "my_dataset"
322
- # table = dataset.create_table "my_table"
323
- #
324
- # @example You can also pass name and description options.
325
- # require "gcloud"
326
- #
327
- # gcloud = Gcloud.new
328
- # bigquery = gcloud.bigquery
329
- # dataset = bigquery.dataset "my_dataset"
330
- # table = dataset.create_table "my_table"
331
- # name: "My Table",
332
- # description: "A description of my table."
333
- #
334
- # @example The table's schema fields can be passed as an argument.
335
- # require "gcloud"
336
- #
337
- # gcloud = Gcloud.new
338
- # bigquery = gcloud.bigquery
339
- # dataset = bigquery.dataset "my_dataset"
340
- #
341
- # schema_fields = [
342
- # Gcloud::Bigquery::Schema::Field.new(
343
- # "first_name", :string, mode: :required),
344
- # Gcloud::Bigquery::Schema::Field.new(
345
- # "cities_lived", :record, mode: :repeated
346
- # fields: [
347
- # Gcloud::Bigquery::Schema::Field.new(
348
- # "place", :string, mode: :required),
349
- # Gcloud::Bigquery::Schema::Field.new(
350
- # "number_of_years", :integer, mode: :required),
351
- # ])
352
- # ]
353
- # table = dataset.create_table "my_table", fields: schema_fields
354
- #
355
- # @example Or the table's schema can be configured with the block.
356
- # require "gcloud"
357
- #
358
- # gcloud = Gcloud.new
359
- # bigquery = gcloud.bigquery
360
- # dataset = bigquery.dataset "my_dataset"
361
- #
362
- # table = dataset.create_table "my_table" do |t|
363
- # t.schema.string "first_name", mode: :required
364
- # t.schema.record "cities_lived", mode: :required do |s|
365
- # s.string "place", mode: :required
366
- # s.integer "number_of_years", mode: :required
367
- # end
368
- # end
369
- #
370
- # @example You can define the schema using a nested block.
371
- # require "gcloud"
372
- #
373
- # gcloud = Gcloud.new
374
- # bigquery = gcloud.bigquery
375
- # dataset = bigquery.dataset "my_dataset"
376
- # table = dataset.create_table "my_table" do |t|
377
- # t.name = "My Table",
378
- # t.description = "A description of my table."
379
- # t.schema do |s|
380
- # s.string "first_name", mode: :required
381
- # s.record "cities_lived", mode: :repeated do |r|
382
- # r.string "place", mode: :required
383
- # r.integer "number_of_years", mode: :required
384
- # end
385
- # end
386
- # end
387
- #
388
- # @!group Table
389
- #
390
- def create_table table_id, name: nil, description: nil, fields: nil
391
- ensure_service!
392
- new_tb = Google::Apis::BigqueryV2::Table.new(
393
- table_reference: Google::Apis::BigqueryV2::TableReference.new(
394
- project_id: project_id, dataset_id: dataset_id, table_id: table_id))
395
- updater = Table::Updater.new(new_tb).tap do |tb|
396
- tb.name = name unless name.nil?
397
- tb.description = description unless description.nil?
398
- tb.schema.fields = fields unless fields.nil?
399
- end
400
-
401
- yield updater if block_given?
402
-
403
- gapi = service.insert_table dataset_id, updater.to_gapi
404
- Table.from_gapi gapi, service
405
- end
406
-
407
- ##
408
- # Creates a new view table from the given query.
409
- #
410
- # @param [String] table_id The ID of the view table. The ID must contain
411
- # only letters (a-z, A-Z), numbers (0-9), or underscores (_). The
412
- # maximum length is 1,024 characters.
413
- # @param [String] query The query that BigQuery executes when the view is
414
- # referenced.
415
- # @param [String] name A descriptive name for the table.
416
- # @param [String] description A user-friendly description of the table.
417
- #
418
- # @return [Gcloud::Bigquery::View]
419
- #
420
- # @example
421
- # require "gcloud"
422
- #
423
- # gcloud = Gcloud.new
424
- # bigquery = gcloud.bigquery
425
- # dataset = bigquery.dataset "my_dataset"
426
- # view = dataset.create_view "my_view",
427
- # "SELECT name, age FROM [proj:dataset.users]"
428
- #
429
- # @example A name and description can be provided:
430
- # require "gcloud"
431
- #
432
- # gcloud = Gcloud.new
433
- # bigquery = gcloud.bigquery
434
- # dataset = bigquery.dataset "my_dataset"
435
- # view = dataset.create_view "my_view",
436
- # "SELECT name, age FROM [proj:dataset.users]",
437
- # name: "My View", description: "This is my view"
438
- #
439
- # @!group Table
440
- #
441
- def create_view table_id, query, name: nil, description: nil
442
- new_view_opts = {
443
- table_reference: Google::Apis::BigqueryV2::TableReference.new(
444
- project_id: project_id, dataset_id: dataset_id, table_id: table_id
445
- ),
446
- friendly_name: name,
447
- description: description,
448
- view: Google::Apis::BigqueryV2::ViewDefinition.new(
449
- query: query
450
- )
451
- }.delete_if { |_, v| v.nil? }
452
- new_view = Google::Apis::BigqueryV2::Table.new new_view_opts
453
-
454
- gapi = service.insert_table dataset_id, new_view
455
- Table.from_gapi gapi, service
456
- end
457
-
458
- ##
459
- # Retrieves an existing table by ID.
460
- #
461
- # @param [String] table_id The ID of a table.
462
- #
463
- # @return [Gcloud::Bigquery::Table, Gcloud::Bigquery::View, nil] Returns
464
- # `nil` if the table does not exist
465
- #
466
- # @example
467
- # require "gcloud"
468
- #
469
- # gcloud = Gcloud.new
470
- # bigquery = gcloud.bigquery
471
- # dataset = bigquery.dataset "my_dataset"
472
- # table = dataset.table "my_table"
473
- # puts table.name
474
- #
475
- # @!group Table
476
- #
477
- def table table_id
478
- ensure_service!
479
- gapi = service.get_table dataset_id, table_id
480
- Table.from_gapi gapi, service
481
- rescue Gcloud::NotFoundError
482
- nil
483
- end
484
-
485
- ##
486
- # Retrieves the list of tables belonging to the dataset.
487
- #
488
- # @param [String] token A previously-returned page token representing part
489
- # of the larger set of results to view.
490
- # @param [Integer] max Maximum number of tables to return.
491
- #
492
- # @return [Array<Gcloud::Bigquery::Table>, Array<Gcloud::Bigquery::View>]
493
- # (See {Gcloud::Bigquery::Table::List})
494
- #
495
- # @example
496
- # require "gcloud"
497
- #
498
- # gcloud = Gcloud.new
499
- # bigquery = gcloud.bigquery
500
- # dataset = bigquery.dataset "my_dataset"
501
- # tables = dataset.tables
502
- # tables.each do |table|
503
- # puts table.name
504
- # end
505
- #
506
- # @example Retrieve all tables: (See {Table::List#all})
507
- # require "gcloud"
508
- #
509
- # gcloud = Gcloud.new
510
- # bigquery = gcloud.bigquery
511
- # dataset = bigquery.dataset "my_dataset"
512
- # tables = dataset.tables
513
- # tables.all do |table|
514
- # puts table.name
515
- # end
516
- #
517
- # @!group Table
518
- #
519
- def tables token: nil, max: nil
520
- ensure_service!
521
- options = { token: token, max: max }
522
- gapi = service.list_tables dataset_id, options
523
- Table::List.from_gapi gapi, service, dataset_id, max
524
- end
525
-
526
- ##
527
- # Queries data using the [asynchronous
528
- # method](https://cloud.google.com/bigquery/querying-data).
529
- #
530
- # Sets the current dataset as the default dataset in the query. Useful for
531
- # using unqualified table names.
532
- #
533
- # @param [String] query A query string, following the BigQuery [query
534
- # syntax](https://cloud.google.com/bigquery/query-reference), of the
535
- # query to execute. Example: "SELECT count(f1) FROM
536
- # [myProjectId:myDatasetId.myTableId]".
537
- # @param [String] priority Specifies a priority for the query. Possible
538
- # values include `INTERACTIVE` and `BATCH`. The default value is
539
- # `INTERACTIVE`.
540
- # @param [Boolean] cache Whether to look for the result in the query
541
- # cache. The query cache is a best-effort cache that will be flushed
542
- # whenever tables in the query are modified. The default value is true.
543
- # For more information, see [query
544
- # caching](https://developers.google.com/bigquery/querying-data).
545
- # @param [Table] table The destination table where the query results
546
- # should be stored. If not present, a new table will be created to store
547
- # the results.
548
- # @param [String] create Specifies whether the job is allowed to create
549
- # new tables.
550
- #
551
- # The following values are supported:
552
- #
553
- # * `needed` - Create the table if it does not exist.
554
- # * `never` - The table must already exist. A 'notFound' error is
555
- # raised if the table does not exist.
556
- # @param [String] write Specifies the action that occurs if the
557
- # destination table already exists.
558
- #
559
- # The following values are supported:
560
- #
561
- # * `truncate` - BigQuery overwrites the table data.
562
- # * `append` - BigQuery appends the data to the table.
563
- # * `empty` - A 'duplicate' error is returned in the job result if the
564
- # table exists and contains data.
565
- # @param [Boolean] large_results If `true`, allows the query to produce
566
- # arbitrarily large result tables at a slight cost in performance.
567
- # Requires `table` parameter to be set.
568
- # @param [Boolean] flatten Flattens all nested and repeated fields in the
569
- # query results. The default value is `true`. `large_results` parameter
570
- # must be `true` if this is set to `false`.
571
- #
572
- # @return [Gcloud::Bigquery::QueryJob]
573
- #
574
- # @example
575
- # require "gcloud"
576
- #
577
- # gcloud = Gcloud.new
578
- # bigquery = gcloud.bigquery
579
- #
580
- # job = bigquery.query_job "SELECT name FROM my_table"
581
- #
582
- # job.wait_until_done!
583
- # if !job.failed?
584
- # job.query_results.each do |row|
585
- # puts row["name"]
586
- # end
587
- # end
588
- #
589
- # @!group Data
590
- #
591
- def query_job query, priority: "INTERACTIVE", cache: true, table: nil,
592
- create: nil, write: nil, large_results: nil, flatten: nil
593
- options = { priority: priority, cache: cache, table: table,
594
- create: create, write: write, large_results: large_results,
595
- flatten: flatten }
596
- options[:dataset] ||= self
597
- ensure_service!
598
- gapi = service.query_job query, options
599
- Job.from_gapi gapi, service
600
- end
601
-
602
- ##
603
- # Queries data using the [synchronous
604
- # method](https://cloud.google.com/bigquery/querying-data).
605
- #
606
- # Sets the current dataset as the default dataset in the query. Useful for
607
- # using unqualified table names.
608
- #
609
- # @param [String] query A query string, following the BigQuery [query
610
- # syntax](https://cloud.google.com/bigquery/query-reference), of the
611
- # query to execute. Example: "SELECT count(f1) FROM
612
- # [myProjectId:myDatasetId.myTableId]".
613
- # @param [Integer] max The maximum number of rows of data to return per
614
- # page of results. Setting this flag to a small value such as 1000 and
615
- # then paging through results might improve reliability when the query
616
- # result set is large. In addition to this limit, responses are also
617
- # limited to 10 MB. By default, there is no maximum row count, and only
618
- # the byte limit applies.
619
- # @param [Integer] timeout How long to wait for the query to complete, in
620
- # milliseconds, before the request times out and returns. Note that this
621
- # is only a timeout for the request, not the query. If the query takes
622
- # longer to run than the timeout value, the call returns without any
623
- # results and with QueryData#complete? set to false. The default value
624
- # is 10000 milliseconds (10 seconds).
625
- # @param [Boolean] dryrun If set to `true`, BigQuery doesn't run the job.
626
- # Instead, if the query is valid, BigQuery returns statistics about the
627
- # job such as how many bytes would be processed. If the query is
628
- # invalid, an error returns. The default value is `false`.
629
- # @param [Boolean] cache Whether to look for the result in the query
630
- # cache. The query cache is a best-effort cache that will be flushed
631
- # whenever tables in the query are modified. The default value is true.
632
- # For more information, see [query
633
- # caching](https://developers.google.com/bigquery/querying-data).
634
- #
635
- # @return [Gcloud::Bigquery::QueryData]
636
- #
637
- # @example
638
- # require "gcloud"
639
- #
640
- # gcloud = Gcloud.new
641
- # bigquery = gcloud.bigquery
642
- #
643
- # data = bigquery.query "SELECT name FROM my_table"
644
- # data.each do |row|
645
- # puts row["name"]
646
- # end
647
- #
648
- # @!group Data
649
- #
650
- def query query, max: nil, timeout: 10000, dryrun: nil, cache: true
651
- options = { max: max, timeout: timeout, dryrun: dryrun, cache: cache }
652
- options[:dataset] ||= dataset_id
653
- options[:project] ||= project_id
654
- ensure_service!
655
- gapi = service.query query, options
656
- QueryData.from_gapi gapi, service
657
- end
658
-
659
- ##
660
- # @private New Dataset from a Google API Client object.
661
- def self.from_gapi gapi, conn
662
- new.tap do |f|
663
- f.gapi = gapi
664
- f.service = conn
665
- end
666
- end
667
-
668
- protected
669
-
670
- ##
671
- # Raise an error unless an active service is available.
672
- def ensure_service!
673
- fail "Must have active connection" unless service
674
- end
675
-
676
- def patch_gapi! *attributes
677
- return if attributes.empty?
678
- ensure_service!
679
- patch_args = Hash[attributes.map do |attr|
680
- [attr, @gapi.send(attr)]
681
- end]
682
- patch_gapi = Google::Apis::BigqueryV2::Dataset.new patch_args
683
- @gapi = service.patch_dataset dataset_id, patch_gapi
684
- end
685
-
686
- ##
687
- # Load the complete representation of the dataset if it has been
688
- # only partially loaded by a request to the API list method.
689
- def ensure_full_data!
690
- reload_gapi! unless data_complete?
691
- end
692
-
693
- def reload_gapi!
694
- ensure_service!
695
- gapi = service.get_dataset dataset_id
696
- @gapi = gapi
697
- end
698
-
699
- def data_complete?
700
- @gapi.is_a? Google::Apis::BigqueryV2::Dataset
701
- end
702
-
703
- ##
704
- # Yielded to a block to accumulate changes for a patch request.
705
- class Updater < Dataset
706
- ##
707
- # A list of attributes that were updated.
708
- attr_reader :updates
709
-
710
- ##
711
- # Create an Updater object.
712
- def initialize gapi
713
- @updates = []
714
- @gapi = gapi
715
- end
716
-
717
- def access
718
- # TODO: make sure to call ensure_full_data! on Dataset#update
719
- @access ||= Access.from_gapi @gapi
720
- if block_given?
721
- yield @access
722
- check_for_mutated_access!
723
- end
724
- # Same as Dataset#access, but not frozen
725
- @access
726
- end
727
-
728
- ##
729
- # Make sure any access changes are saved
730
- def check_for_mutated_access!
731
- return if @access.nil?
732
- return unless @access.changed?
733
- @gapi.update! access: @access.to_gapi
734
- patch_gapi! :access
735
- end
736
-
737
- def to_gapi
738
- check_for_mutated_access!
739
- @gapi
740
- end
741
-
742
- protected
743
-
744
- ##
745
- # Queue up all the updates instead of making them.
746
- def patch_gapi! attribute
747
- @updates << attribute
748
- @updates.uniq!
749
- end
750
- end
751
- end
752
- end
753
- end