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,172 +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
-
18
- module Gcloud
19
- module Bigquery
20
- class Job
21
- ##
22
- # Job::List is a special case Array with additional values.
23
- class List < DelegateClass(::Array)
24
- ##
25
- # If not empty, indicates that there are more records that match
26
- # the request and this value should be passed to continue.
27
- attr_accessor :token
28
-
29
- # A hash of this page of results.
30
- attr_accessor :etag
31
-
32
- ##
33
- # @private Create a new Job::List with an array of jobs.
34
- def initialize arr = []
35
- super arr
36
- end
37
-
38
- ##
39
- # Whether there is a next page of jobs.
40
- #
41
- # @return [Boolean]
42
- #
43
- # @example
44
- # require "gcloud"
45
- #
46
- # gcloud = Gcloud.new
47
- # bigquery = gcloud.bigquery
48
- #
49
- # jobs = bigquery.jobs
50
- # if jobs.next?
51
- # next_jobs = jobs.next
52
- # end
53
- def next?
54
- !token.nil?
55
- end
56
-
57
- ##
58
- # Retrieve the next page of jobs.
59
- #
60
- # @return [Job::List]
61
- #
62
- # @example
63
- # require "gcloud"
64
- #
65
- # gcloud = Gcloud.new
66
- # bigquery = gcloud.bigquery
67
- #
68
- # jobs = bigquery.jobs
69
- # if jobs.next?
70
- # next_jobs = jobs.next
71
- # end
72
- def next
73
- return nil unless next?
74
- ensure_service!
75
- options = { all: @hidden, token: token, max: @max, filter: @filter }
76
- gapi = @service.list_jobs options
77
- self.class.from_gapi gapi, @service, @hidden, @max, @filter
78
- end
79
-
80
- ##
81
- # Retrieves all jobs by repeatedly loading {#next} until {#next?}
82
- # returns `false`. Calls the given block once for each job, which is
83
- # passed as the parameter.
84
- #
85
- # An Enumerator is returned if no block is given.
86
- #
87
- # This method may make several API calls until all jobs are retrieved.
88
- # Be sure to use as narrow a search criteria as possible. Please use
89
- # with caution.
90
- #
91
- # @param [Integer] request_limit The upper limit of API requests to make
92
- # to load all jobs. Default is no limit.
93
- # @yield [job] The block for accessing each job.
94
- # @yieldparam [Job] job The job object.
95
- #
96
- # @return [Enumerator]
97
- #
98
- # @example Iterating each job by passing a block:
99
- # require "gcloud"
100
- #
101
- # gcloud = Gcloud.new
102
- # bigquery = gcloud.bigquery
103
- #
104
- # bigquery.jobs.all do |job|
105
- # puts job.state
106
- # end
107
- #
108
- # @example Using the enumerator by not passing a block:
109
- # require "gcloud"
110
- #
111
- # gcloud = Gcloud.new
112
- # bigquery = gcloud.bigquery
113
- #
114
- # all_states = bigquery.jobs.all.map do |job|
115
- # job.state
116
- # end
117
- #
118
- # @example Limit the number of API calls made:
119
- # require "gcloud"
120
- #
121
- # gcloud = Gcloud.new
122
- # bigquery = gcloud.bigquery
123
- #
124
- # bigquery.jobs.all(request_limit: 10) do |job|
125
- # puts job.state
126
- # end
127
- #
128
- def all request_limit: nil
129
- request_limit = request_limit.to_i if request_limit
130
- unless block_given?
131
- return enum_for(:all, request_limit: request_limit)
132
- end
133
- results = self
134
- loop do
135
- results.each { |r| yield r }
136
- if request_limit
137
- request_limit -= 1
138
- break if request_limit < 0
139
- end
140
- break unless results.next?
141
- results = results.next
142
- end
143
- end
144
-
145
- ##
146
- # @private New Job::List from a Google API Client
147
- # Google::Apis::BigqueryV2::JobList object.
148
- def self.from_gapi gapi_list, service, hidden = nil, max = nil,
149
- filter = nil
150
- jobs = List.new(Array(gapi_list.jobs).map do |gapi_object|
151
- Job.from_gapi gapi_object, service
152
- end)
153
- jobs.instance_variable_set :@token, gapi_list.next_page_token
154
- jobs.instance_variable_set :@etag, gapi_list.etag
155
- jobs.instance_variable_set :@service, service
156
- jobs.instance_variable_set :@hidden, hidden
157
- jobs.instance_variable_set :@max, max
158
- jobs.instance_variable_set :@filter, filter
159
- jobs
160
- end
161
-
162
- protected
163
-
164
- ##
165
- # Raise an error unless an active service is available.
166
- def ensure_service!
167
- fail "Must have active connection" unless @service
168
- end
169
- end
170
- end
171
- end
172
- end
@@ -1,202 +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/bigquery/service"
17
-
18
- module Gcloud
19
- module Bigquery
20
- ##
21
- # # LoadJob
22
- #
23
- # A {Job} subclass representing a load operation that may be performed
24
- # on a {Table}. A LoadJob instance is created when you call {Table#load}.
25
- #
26
- # @see https://cloud.google.com/bigquery/loading-data-into-bigquery Loading
27
- # Data Into BigQuery
28
- # @see https://cloud.google.com/bigquery/docs/reference/v2/jobs Jobs API
29
- # reference
30
- #
31
- class LoadJob < Job
32
- ##
33
- # The URI or URIs representing the Google Cloud Storage files from which
34
- # the operation loads data.
35
- def sources
36
- Array @gapi.configuration.load.source_uris
37
- end
38
-
39
- ##
40
- # The table into which the operation loads data. This is the table on
41
- # which {Table#load} was invoked. Returns a {Table} instance.
42
- def destination
43
- table = @gapi.configuration.load.destination_table
44
- return nil unless table
45
- retrieve_table table.project_id,
46
- table.dataset_id,
47
- table.table_id
48
- end
49
-
50
- ##
51
- # The delimiter used between fields in the source data. The default is a
52
- # comma (,).
53
- def delimiter
54
- @gapi.configuration.load.field_delimiter || ","
55
- end
56
-
57
- ##
58
- # The number of header rows at the top of a CSV file to skip. The default
59
- # value is `0`.
60
- def skip_leading_rows
61
- @gapi.configuration.load.skip_leading_rows || 0
62
- end
63
-
64
- ##
65
- # Checks if the character encoding of the data is UTF-8. This is the
66
- # default.
67
- def utf8?
68
- val = @gapi.configuration.load.encoding
69
- return true if val.nil?
70
- val == "UTF-8"
71
- end
72
-
73
- ##
74
- # Checks if the character encoding of the data is ISO-8859-1.
75
- def iso8859_1?
76
- val = @gapi.configuration.load.encoding
77
- val == "ISO-8859-1"
78
- end
79
-
80
- ##
81
- # The value that is used to quote data sections in a CSV file.
82
- # The default value is a double-quote (`"`). If your data does not contain
83
- # quoted sections, the value should be an empty string. If your data
84
- # contains quoted newline characters, {#quoted_newlines?} should return
85
- # `true`.
86
- def quote
87
- val = @gapi.configuration.load.quote
88
- val = "\"" if val.nil?
89
- val
90
- end
91
-
92
- ##
93
- # The maximum number of bad records that the load operation can ignore. If
94
- # the number of bad records exceeds this value, an error is
95
- # returned. The default value is `0`, which requires that all records be
96
- # valid.
97
- def max_bad_records
98
- val = @gapi.configuration.load.max_bad_records
99
- val = 0 if val.nil?
100
- val
101
- end
102
-
103
- ##
104
- # Checks if quoted data sections may contain newline characters in a CSV
105
- # file. The default is `false`.
106
- def quoted_newlines?
107
- val = @gapi.configuration.load.allow_quoted_newlines
108
- val = true if val.nil?
109
- val
110
- end
111
-
112
- ##
113
- # Checks if the format of the source data is
114
- # [newline-delimited JSON](http://jsonlines.org/). The default is `false`.
115
- def json?
116
- val = @gapi.configuration.load.source_format
117
- val == "NEWLINE_DELIMITED_JSON"
118
- end
119
-
120
- ##
121
- # Checks if the format of the source data is CSV. The default is `true`.
122
- def csv?
123
- val = @gapi.configuration.load.source_format
124
- return true if val.nil?
125
- val == "CSV"
126
- end
127
-
128
- ##
129
- # Checks if the source data is a Google Cloud Datastore backup.
130
- def backup?
131
- val = @gapi.configuration.load.source_format
132
- val == "DATASTORE_BACKUP"
133
- end
134
-
135
- ##
136
- # Checks if the load operation accepts rows that are missing trailing
137
- # optional columns. The missing values are treated as nulls. If `false`,
138
- # records with missing trailing columns are treated as bad records, and
139
- # if there are too many bad records, an error is returned. The default
140
- # value is `false`. Only applicable to CSV, ignored for other formats.
141
- def allow_jagged_rows?
142
- val = @gapi.configuration.load.allow_jagged_rows
143
- val = false if val.nil?
144
- val
145
- end
146
-
147
- ##
148
- # Checks if the load operation allows extra values that are not
149
- # represented in the table schema. If `true`, the extra values are
150
- # ignored. If `false`, records with extra columns are treated as bad
151
- # records, and if there are too many bad records, an invalid error is
152
- # returned. The default is `false`.
153
- def ignore_unknown_values?
154
- val = @gapi.configuration.load.ignore_unknown_values
155
- val = false if val.nil?
156
- val
157
- end
158
-
159
- ##
160
- # The schema for the data. Returns a hash. Can be empty if the table has
161
- # already has the correct schema (see {Table#schema}), or if the schema
162
- # can be inferred from the loaded data.
163
- def schema
164
- Schema.from_gapi(@gapi.configuration.load.schema).freeze
165
- end
166
-
167
- ##
168
- # The number of source files.
169
- def input_files
170
- Integer @gapi.statistics.load.input_files
171
- rescue
172
- nil
173
- end
174
-
175
- ##
176
- # The number of bytes of source data.
177
- def input_file_bytes
178
- Integer @gapi.statistics.load.input_file_bytes
179
- rescue
180
- nil
181
- end
182
-
183
- ##
184
- # The number of rows that have been loaded into the table. While an
185
- # import job is in the running state, this value may change.
186
- def output_rows
187
- Integer @gapi.statistics.load.output_rows
188
- rescue
189
- nil
190
- end
191
-
192
- ##
193
- # The number of bytes that have been loaded into the table. While an
194
- # import job is in the running state, this value may change.
195
- def output_bytes
196
- Integer @gapi.statistics.load.output_bytes
197
- rescue
198
- nil
199
- end
200
- end
201
- end
202
- end
@@ -1,475 +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/gce"
17
- require "gcloud/errors"
18
- require "gcloud/bigquery/service"
19
- require "gcloud/bigquery/credentials"
20
- require "gcloud/bigquery/dataset"
21
- require "gcloud/bigquery/job"
22
- require "gcloud/bigquery/query_data"
23
-
24
- module Gcloud
25
- module Bigquery
26
- ##
27
- # # Project
28
- #
29
- # Projects are top-level containers in Google Cloud Platform. They store
30
- # information about billing and authorized users, and they contain BigQuery
31
- # data. Each project has a friendly name and a unique ID.
32
- #
33
- # Gcloud::Bigquery::Project is the main object for interacting with
34
- # Google BigQuery. {Gcloud::Bigquery::Dataset} objects are created,
35
- # accessed, and deleted by Gcloud::Bigquery::Project.
36
- #
37
- # See {Gcloud#bigquery}
38
- #
39
- # @example
40
- # require "gcloud"
41
- #
42
- # gcloud = Gcloud.new
43
- # bigquery = gcloud.bigquery
44
- # dataset = bigquery.dataset "my_dataset"
45
- # table = dataset.table "my_table"
46
- #
47
- class Project
48
- ##
49
- # @private The Service object.
50
- attr_accessor :service
51
-
52
- ##
53
- # Creates a new Service instance.
54
- #
55
- # See {Gcloud.bigquery}
56
- def initialize service
57
- @service = service
58
- end
59
-
60
- ##
61
- # The BigQuery project connected to.
62
- #
63
- # @example
64
- # require "gcloud"
65
- #
66
- # gcloud = Gcloud.new "my-todo-project", "/path/to/keyfile.json"
67
- # bigquery = gcloud.bigquery
68
- #
69
- # bigquery.project #=> "my-todo-project"
70
- #
71
- def project
72
- service.project
73
- end
74
-
75
- ##
76
- # @private Default project.
77
- def self.default_project
78
- ENV["BIGQUERY_PROJECT"] ||
79
- ENV["GCLOUD_PROJECT"] ||
80
- ENV["GOOGLE_CLOUD_PROJECT"] ||
81
- Gcloud::GCE.project_id
82
- end
83
-
84
- ##
85
- # Queries data using the [asynchronous
86
- # method](https://cloud.google.com/bigquery/querying-data).
87
- #
88
- # @param [String] query A query string, following the BigQuery [query
89
- # syntax](https://cloud.google.com/bigquery/query-reference), of the
90
- # query to execute. Example: "SELECT count(f1) FROM
91
- # [myProjectId:myDatasetId.myTableId]".
92
- # @param [String] priority Specifies a priority for the query. Possible
93
- # values include `INTERACTIVE` and `BATCH`. The default value is
94
- # `INTERACTIVE`.
95
- # @param [Boolean] cache Whether to look for the result in the query
96
- # cache. The query cache is a best-effort cache that will be flushed
97
- # whenever tables in the query are modified. The default value is true.
98
- # For more information, see [query
99
- # caching](https://developers.google.com/bigquery/querying-data).
100
- # @param [Table] table The destination table where the query results
101
- # should be stored. If not present, a new table will be created to store
102
- # the results.
103
- # @param [String] create Specifies whether the job is allowed to create
104
- # new tables.
105
- #
106
- # The following values are supported:
107
- #
108
- # * `needed` - Create the table if it does not exist.
109
- # * `never` - The table must already exist. A 'notFound' error is
110
- # raised if the table does not exist.
111
- # @param [String] write Specifies the action that occurs if the
112
- # destination table already exists.
113
- #
114
- # The following values are supported:
115
- #
116
- # * `truncate` - BigQuery overwrites the table data.
117
- # * `append` - BigQuery appends the data to the table.
118
- # * `empty` - A 'duplicate' error is returned in the job result if the
119
- # table exists and contains data.
120
- # @param [Boolean] large_results If `true`, allows the query to produce
121
- # arbitrarily large result tables at a slight cost in performance.
122
- # Requires `table` parameter to be set.
123
- # @param [Boolean] flatten Flattens all nested and repeated fields in the
124
- # query results. The default value is `true`. `large_results` parameter
125
- # must be `true` if this is set to `false`.
126
- # @param [Dataset, String] dataset Specifies the default dataset to use
127
- # for unqualified table names in the query.
128
- #
129
- # @return [Gcloud::Bigquery::QueryJob]
130
- #
131
- # @example
132
- # require "gcloud"
133
- #
134
- # gcloud = Gcloud.new
135
- # bigquery = gcloud.bigquery
136
- #
137
- # job = bigquery.query_job "SELECT name FROM [my_proj:my_data.my_table]"
138
- #
139
- # job.wait_until_done!
140
- # if !job.failed?
141
- # job.query_results.each do |row|
142
- # puts row["name"]
143
- # end
144
- # end
145
- #
146
- def query_job query, priority: "INTERACTIVE", cache: true, table: nil,
147
- create: nil, write: nil, large_results: nil, flatten: nil,
148
- dataset: nil
149
- ensure_service!
150
- options = { priority: priority, cache: cache, table: table,
151
- create: create, write: write, large_results: large_results,
152
- flatten: flatten, dataset: dataset }
153
- gapi = service.query_job query, options
154
- Job.from_gapi gapi, service
155
- end
156
-
157
- ##
158
- # Queries data using the [synchronous
159
- # method](https://cloud.google.com/bigquery/querying-data).
160
- #
161
- # @param [String] query A query string, following the BigQuery [query
162
- # syntax](https://cloud.google.com/bigquery/query-reference), of the
163
- # query to execute. Example: "SELECT count(f1) FROM
164
- # [myProjectId:myDatasetId.myTableId]".
165
- # @param [Integer] max The maximum number of rows of data to return per
166
- # page of results. Setting this flag to a small value such as 1000 and
167
- # then paging through results might improve reliability when the query
168
- # result set is large. In addition to this limit, responses are also
169
- # limited to 10 MB. By default, there is no maximum row count, and only
170
- # the byte limit applies.
171
- # @param [Integer] timeout How long to wait for the query to complete, in
172
- # milliseconds, before the request times out and returns. Note that this
173
- # is only a timeout for the request, not the query. If the query takes
174
- # longer to run than the timeout value, the call returns without any
175
- # results and with QueryData#complete? set to false. The default value
176
- # is 10000 milliseconds (10 seconds).
177
- # @param [Boolean] dryrun If set to `true`, BigQuery doesn't run the job.
178
- # Instead, if the query is valid, BigQuery returns statistics about the
179
- # job such as how many bytes would be processed. If the query is
180
- # invalid, an error returns. The default value is `false`.
181
- # @param [Boolean] cache Whether to look for the result in the query
182
- # cache. The query cache is a best-effort cache that will be flushed
183
- # whenever tables in the query are modified. The default value is true.
184
- # For more information, see [query
185
- # caching](https://developers.google.com/bigquery/querying-data).
186
- # @param [String] dataset Specifies the default datasetId and projectId to
187
- # assume for any unqualified table names in the query. If not set, all
188
- # table names in the query string must be qualified in the format
189
- # 'datasetId.tableId'.
190
- # @param [String] project Specifies the default projectId to assume for
191
- # any unqualified table names in the query. Only used if `dataset`
192
- # option is set.
193
- #
194
- # @return [Gcloud::Bigquery::QueryData]
195
- #
196
- # @example
197
- # require "gcloud"
198
- #
199
- # gcloud = Gcloud.new
200
- # bigquery = gcloud.bigquery
201
- #
202
- # data = bigquery.query "SELECT name FROM [my_proj:my_data.my_table]"
203
- # data.each do |row|
204
- # puts row["name"]
205
- # end
206
- #
207
- # @example Retrieve all rows: (See {QueryData#all})
208
- # require "gcloud"
209
- #
210
- # gcloud = Gcloud.new
211
- # bigquery = gcloud.bigquery
212
- #
213
- # data = bigquery.query "SELECT name FROM [my_proj:my_data.my_table]"
214
- # data.all do |row|
215
- # puts row["name"]
216
- # end
217
- #
218
- def query query, max: nil, timeout: 10000, dryrun: nil, cache: true,
219
- dataset: nil, project: nil
220
- ensure_service!
221
- options = { max: max, timeout: timeout, dryrun: dryrun, cache: cache,
222
- dataset: dataset, project: project }
223
- gapi = service.query query, options
224
- QueryData.from_gapi gapi, service
225
- end
226
-
227
- ##
228
- # Retrieves an existing dataset by ID.
229
- #
230
- # @param [String] dataset_id The ID of a dataset.
231
- #
232
- # @return [Gcloud::Bigquery::Dataset, nil] Returns `nil` if the dataset
233
- # does not exist.
234
- #
235
- # @example
236
- # require "gcloud"
237
- #
238
- # gcloud = Gcloud.new
239
- # bigquery = gcloud.bigquery
240
- #
241
- # dataset = bigquery.dataset "my_dataset"
242
- # puts dataset.name
243
- #
244
- def dataset dataset_id
245
- ensure_service!
246
- gapi = service.get_dataset dataset_id
247
- Dataset.from_gapi gapi, service
248
- rescue Gcloud::NotFoundError
249
- nil
250
- end
251
-
252
- ##
253
- # Creates a new dataset.
254
- #
255
- # @param [String] dataset_id A unique ID for this dataset, without the
256
- # project name. The ID must contain only letters (a-z, A-Z), numbers
257
- # (0-9), or underscores (_). The maximum length is 1,024 characters.
258
- # @param [String] name A descriptive name for the dataset.
259
- # @param [String] description A user-friendly description of the dataset.
260
- # @param [Integer] expiration The default lifetime of all tables in the
261
- # dataset, in milliseconds. The minimum value is 3600000 milliseconds
262
- # (one hour).
263
- # @param [String] location The geographic location where the dataset
264
- # should reside. Possible values include `EU` and `US`. The default
265
- # value is `US`.
266
- # @yield [access] a block for setting rules
267
- # @yieldparam [Dataset::Access] access the object accepting rules
268
- #
269
- # @return [Gcloud::Bigquery::Dataset]
270
- #
271
- # @example
272
- # require "gcloud"
273
- #
274
- # gcloud = Gcloud.new
275
- # bigquery = gcloud.bigquery
276
- #
277
- # dataset = bigquery.create_dataset "my_dataset"
278
- #
279
- # @example A name and description can be provided:
280
- # require "gcloud"
281
- #
282
- # gcloud = Gcloud.new
283
- # bigquery = gcloud.bigquery
284
- #
285
- # dataset = bigquery.create_dataset "my_dataset",
286
- # name: "My Dataset",
287
- # description: "This is my Dataset"
288
- #
289
- # @example Access rules can be provided with the `access` option:
290
- # require "gcloud"
291
- #
292
- # gcloud = Gcloud.new
293
- # bigquery = gcloud.bigquery
294
- #
295
- # dataset = bigquery.create_dataset "my_dataset",
296
- # access: [{"role"=>"WRITER", "userByEmail"=>"writers@example.com"}]
297
- #
298
- # @example Or, configure access with a block: (See {Dataset::Access})
299
- # require "gcloud"
300
- #
301
- # gcloud = Gcloud.new
302
- # bigquery = gcloud.bigquery
303
- #
304
- # dataset = bigquery.create_dataset "my_dataset" do |access|
305
- # access.add_writer_user "writers@example.com"
306
- # end
307
- #
308
- def create_dataset dataset_id, name: nil, description: nil,
309
- expiration: nil, location: nil
310
- ensure_service!
311
-
312
- new_ds = Google::Apis::BigqueryV2::Dataset.new(
313
- dataset_reference: Google::Apis::BigqueryV2::DatasetReference.new(
314
- project_id: project, dataset_id: dataset_id))
315
-
316
- # Can set location only on creation, no Dataset#location method
317
- new_ds.update! location: location unless location.nil?
318
-
319
- updater = Dataset::Updater.new(new_ds).tap do |b|
320
- b.name = name unless name.nil?
321
- b.description = description unless description.nil?
322
- b.default_expiration = expiration unless expiration.nil?
323
- end
324
-
325
- if block_given?
326
- yield updater
327
- updater.check_for_mutated_access!
328
- end
329
-
330
- gapi = service.insert_dataset new_ds
331
- Dataset.from_gapi gapi, service
332
- end
333
-
334
- ##
335
- # Retrieves the list of datasets belonging to the project.
336
- #
337
- # @param [Boolean] all Whether to list all datasets, including hidden
338
- # ones. The default is `false`.
339
- # @param [String] token A previously-returned page token representing part
340
- # of the larger set of results to view.
341
- # @param [Integer] max Maximum number of datasets to return.
342
- #
343
- # @return [Array<Gcloud::Bigquery::Dataset>] (See
344
- # {Gcloud::Bigquery::Dataset::List})
345
- #
346
- # @example
347
- # require "gcloud"
348
- #
349
- # gcloud = Gcloud.new
350
- # bigquery = gcloud.bigquery
351
- #
352
- # datasets = bigquery.datasets
353
- # datasets.each do |dataset|
354
- # puts dataset.name
355
- # end
356
- #
357
- # @example Retrieve hidden datasets with the `all` optional arg:
358
- # require "gcloud"
359
- #
360
- # gcloud = Gcloud.new
361
- # bigquery = gcloud.bigquery
362
- #
363
- # all_datasets = bigquery.datasets all: true
364
- #
365
- # @example Retrieve all datasets: (See {Dataset::List#all})
366
- # require "gcloud"
367
- #
368
- # gcloud = Gcloud.new
369
- # bigquery = gcloud.bigquery
370
- #
371
- # datasets = bigquery.datasets
372
- # datasets.all do |dataset|
373
- # puts dataset.name
374
- # end
375
- #
376
- def datasets all: nil, token: nil, max: nil
377
- ensure_service!
378
- options = { all: all, token: token, max: max }
379
- gapi = service.list_datasets options
380
- Dataset::List.from_gapi gapi, service, all, max
381
- end
382
-
383
- ##
384
- # Retrieves an existing job by ID.
385
- #
386
- # @param [String] job_id The ID of a job.
387
- #
388
- # @return [Gcloud::Bigquery::Job, nil] Returns `nil` if the job does not
389
- # exist.
390
- #
391
- # @example
392
- # require "gcloud"
393
- #
394
- # gcloud = Gcloud.new
395
- # bigquery = gcloud.bigquery
396
- #
397
- # job = bigquery.job "my_job"
398
- #
399
- def job job_id
400
- ensure_service!
401
- gapi = service.get_job job_id
402
- Job.from_gapi gapi, service
403
- rescue Gcloud::NotFoundError
404
- nil
405
- end
406
-
407
- ##
408
- # Retrieves the list of jobs belonging to the project.
409
- #
410
- # @param [Boolean] all Whether to display jobs owned by all users in the
411
- # project. The default is `false`.
412
- # @param [String] token A previously-returned page token representing part
413
- # of the larger set of results to view.
414
- # @param [Integer] max Maximum number of jobs to return.
415
- # @param [String] filter A filter for job state.
416
- #
417
- # Acceptable values are:
418
- #
419
- # * `done` - Finished jobs
420
- # * `pending` - Pending jobs
421
- # * `running` - Running jobs
422
- #
423
- # @return [Array<Gcloud::Bigquery::Job>] (See
424
- # {Gcloud::Bigquery::Job::List})
425
- #
426
- # @example
427
- # require "gcloud"
428
- #
429
- # gcloud = Gcloud.new
430
- # bigquery = gcloud.bigquery
431
- #
432
- # jobs = bigquery.jobs
433
- # jobs.each do |job|
434
- # # process job
435
- # end
436
- #
437
- # @example Retrieve only running jobs using the `filter` optional arg:
438
- # require "gcloud"
439
- #
440
- # gcloud = Gcloud.new
441
- # bigquery = gcloud.bigquery
442
- #
443
- # running_jobs = bigquery.jobs filter: "running"
444
- # running_jobs.each do |job|
445
- # # process job
446
- # end
447
- #
448
- # @example Retrieve all jobs: (See {Job::List#all})
449
- # require "gcloud"
450
- #
451
- # gcloud = Gcloud.new
452
- # bigquery = gcloud.bigquery
453
- #
454
- # jobs = bigquery.jobs
455
- # jobs.all do |job|
456
- # # process job
457
- # end
458
- #
459
- def jobs all: nil, token: nil, max: nil, filter: nil
460
- ensure_service!
461
- options = { all: all, token: token, max: max, filter: filter }
462
- gapi = service.list_jobs options
463
- Job::List.from_gapi gapi, service, all, max, filter
464
- end
465
-
466
- protected
467
-
468
- ##
469
- # Raise an error unless an active service is available.
470
- def ensure_service!
471
- fail "Must have active connection" unless service
472
- end
473
- end
474
- end
475
- end