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,169 +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 Dataset
21
- ##
22
- # Dataset::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 Dataset::List with an array of datasets.
34
- def initialize arr = []
35
- super arr
36
- end
37
-
38
- ##
39
- # Whether there is a next page of datasets.
40
- #
41
- # @return [Boolean]
42
- #
43
- # @example
44
- # require "gcloud"
45
- #
46
- # gcloud = Gcloud.new
47
- # bigquery = gcloud.bigquery
48
- #
49
- # datasets = bigquery.datasets
50
- # if datasets.next?
51
- # next_datasets = datasets.next
52
- # end
53
- def next?
54
- !token.nil?
55
- end
56
-
57
- ##
58
- # Retrieve the next page of datasets.
59
- #
60
- # @return [Dataset::List]
61
- #
62
- # @example
63
- # require "gcloud"
64
- #
65
- # gcloud = Gcloud.new
66
- # bigquery = gcloud.bigquery
67
- #
68
- # datasets = bigquery.datasets
69
- # if datasets.next?
70
- # next_datasets = datasets.next
71
- # end
72
- def next
73
- return nil unless next?
74
- ensure_service!
75
- options = { all: @hidden, token: token, max: @max }
76
- gapi = @service.list_datasets options
77
- self.class.from_gapi gapi, @service, @hidden, @max
78
- end
79
-
80
- ##
81
- # Retrieves all datasets by repeatedly loading {#next} until {#next?}
82
- # returns `false`. Calls the given block once for each dataset, 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 datasets are
88
- # retrieved. Be sure to use as narrow a search criteria as possible.
89
- # Please use with caution.
90
- #
91
- # @param [Integer] request_limit The upper limit of API requests to make
92
- # to load all datasets. Default is no limit.
93
- # @yield [dataset] The block for accessing each dataset.
94
- # @yieldparam [Dataset] dataset The dataset object.
95
- #
96
- # @return [Enumerator]
97
- #
98
- # @example Iterating each result by passing a block:
99
- # require "gcloud"
100
- #
101
- # gcloud = Gcloud.new
102
- # bigquery = gcloud.bigquery
103
- #
104
- # bigquery.datasets.all do |dataset|
105
- # puts dataset.name
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_names = bigquery.datasets.all.map do |dataset|
115
- # dataset.name
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.datasets.all(request_limit: 10) do |dataset|
125
- # puts dataset.name
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 Dataset::List from a response object.
147
- def self.from_gapi gapi_list, service, hidden = nil, max = nil
148
- datasets = List.new(Array(gapi_list.datasets).map do |gapi_object|
149
- Dataset.from_gapi gapi_object, service
150
- end)
151
- datasets.instance_variable_set :@token, gapi_list.next_page_token
152
- datasets.instance_variable_set :@etag, gapi_list.etag
153
- datasets.instance_variable_set :@service, service
154
- datasets.instance_variable_set :@hidden, hidden
155
- datasets.instance_variable_set :@max, max
156
- datasets
157
- end
158
-
159
- protected
160
-
161
- ##
162
- # Raise an error unless an active service is available.
163
- def ensure_service!
164
- fail "Must have active connection" unless @service
165
- end
166
- end
167
- end
168
- end
169
- end
@@ -1,117 +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
- module Gcloud
17
- module Bigquery
18
- ##
19
- # # ExtractJob
20
- #
21
- # A {Job} subclass representing an export operation that may be performed
22
- # on a {Table}. A ExtractJob instance is created when you call
23
- # {Table#extract}.
24
- #
25
- # @see https://cloud.google.com/bigquery/exporting-data-from-bigquery
26
- # Exporting Data From BigQuery
27
- # @see https://cloud.google.com/bigquery/docs/reference/v2/jobs Jobs API
28
- # reference
29
- #
30
- class ExtractJob < Job
31
- ##
32
- # The URI or URIs representing the Google Cloud Storage files to which
33
- # the data is exported.
34
- def destinations
35
- Array @gapi.configuration.extract.destination_uris
36
- end
37
-
38
- ##
39
- # The table from which the data is exported. This is the table upon
40
- # which {Table#extract} was called. Returns a {Table} instance.
41
- def source
42
- table = @gapi.configuration.extract.source_table
43
- return nil unless table
44
- retrieve_table table.project_id,
45
- table.dataset_id,
46
- table.table_id
47
- end
48
-
49
- ##
50
- # Checks if the export operation compresses the data using gzip. The
51
- # default is `false`.
52
- def compression?
53
- val = @gapi.configuration.extract.compression
54
- val == "GZIP"
55
- end
56
-
57
- ##
58
- # Checks if the destination format for the data is [newline-delimited
59
- # JSON](http://jsonlines.org/). The default is `false`.
60
- def json?
61
- val = @gapi.configuration.extract.destination_format
62
- val == "NEWLINE_DELIMITED_JSON"
63
- end
64
-
65
- ##
66
- # Checks if the destination format for the data is CSV. Tables with nested
67
- # or repeated fields cannot be exported as CSV. The default is `true`.
68
- def csv?
69
- val = @gapi.configuration.extract.destination_format
70
- return true if val.nil?
71
- val == "CSV"
72
- end
73
-
74
- ##
75
- # Checks if the destination format for the data is
76
- # [Avro](http://avro.apache.org/). The default is `false`.
77
- def avro?
78
- val = @gapi.configuration.extract.destination_format
79
- val == "AVRO"
80
- end
81
-
82
- ##
83
- # The symbol the operation uses to delimit fields in the exported data.
84
- # The default is a comma (,).
85
- def delimiter
86
- val = @gapi.configuration.extract.field_delimiter
87
- val = "," if val.nil?
88
- val
89
- end
90
-
91
- ##
92
- # Checks if the exported data contains a header row. The default is
93
- # `true`.
94
- def print_header?
95
- val = @gapi.configuration.extract.print_header
96
- val = true if val.nil?
97
- val
98
- end
99
-
100
- ##
101
- # The count of files per destination URI or URI pattern specified in
102
- # {#destinations}. Returns an Array of values in the same order as the URI
103
- # patterns.
104
- def destinations_file_counts
105
- Array @gapi.statistics.extract.destination_uri_file_counts
106
- end
107
-
108
- ##
109
- # The count of files per destination URI or URI pattern specified in
110
- # {#destinations}. Returns a Hash with the URI patterns as keys and the
111
- # counts as values.
112
- def destinations_counts
113
- Hash[destinations.zip destinations_file_counts]
114
- end
115
- end
116
- end
117
- end
@@ -1,81 +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
-
18
- module Gcloud
19
- module Bigquery
20
- ##
21
- # InsertResponse
22
- class InsertResponse
23
- # @private
24
- def initialize rows, gapi
25
- @rows = rows
26
- @gapi = gapi
27
- end
28
-
29
- def success?
30
- error_count.zero?
31
- end
32
-
33
- def insert_count
34
- @rows.count - error_count
35
- end
36
-
37
- def error_count
38
- Array(@gapi.insert_errors).count
39
- end
40
-
41
- def insert_errors
42
- Array(@gapi.insert_errors).map do |ie|
43
- row = @rows[ie.index]
44
- errors = ie.errors.map { |e| JSON.parse e.to_json }
45
- InsertError.new row, errors
46
- end
47
- end
48
-
49
- def error_rows
50
- Array(@gapi.insert_errors).map do |ie|
51
- @rows[ie.index]
52
- end
53
- end
54
-
55
- def errors_for row
56
- ie = insert_errors.detect { |e| e.row == row }
57
- return ie.errors if ie
58
- []
59
- end
60
-
61
- # @private New InsertResponse from the inserted rows and a
62
- # Google::Apis::BigqueryV2::InsertAllTableDataResponse object.
63
- def self.from_gapi rows, gapi
64
- new rows, gapi
65
- end
66
-
67
- ##
68
- # InsertError
69
- class InsertError
70
- attr_reader :row
71
- attr_reader :errors
72
-
73
- # @private
74
- def initialize row, errors
75
- @row = row
76
- @errors = errors
77
- end
78
- end
79
- end
80
- end
81
- end
@@ -1,299 +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/errors"
17
- require "gcloud/bigquery/service"
18
- require "gcloud/bigquery/query_data"
19
- require "gcloud/bigquery/job/list"
20
- require "json"
21
-
22
- module Gcloud
23
- module Bigquery
24
- ##
25
- # # Job
26
- #
27
- # Represents a generic Job that may be performed on a {Table}.
28
- #
29
- # The subclasses of Job represent the specific BigQuery job types:
30
- # {CopyJob}, {ExtractJob}, {LoadJob}, and {QueryJob}.
31
- #
32
- # A job instance is created when you call {Project#query_job},
33
- # {Dataset#query_job}, {Table#copy}, {Table#extract}, {Table#load}, or
34
- # {View#data}.
35
- #
36
- # @see https://cloud.google.com/bigquery/docs/managing_jobs_datasets_projects
37
- # Managing Jobs, Datasets, and Projects
38
- # @see https://cloud.google.com/bigquery/docs/reference/v2/jobs Jobs API
39
- # reference
40
- #
41
- # @example
42
- # require "gcloud"
43
- #
44
- # gcloud = Gcloud.new
45
- # bigquery = gcloud.bigquery
46
- #
47
- # q = "SELECT COUNT(word) as count FROM publicdata:samples.shakespeare"
48
- # job = bigquery.query_job q
49
- #
50
- # job.wait_until_done!
51
- #
52
- # if job.failed?
53
- # puts job.error
54
- # else
55
- # puts job.query_results.first
56
- # end
57
- #
58
- class Job
59
- ##
60
- # @private The Service object.
61
- attr_accessor :service
62
-
63
- ##
64
- # @private The Google API Client object.
65
- attr_accessor :gapi
66
-
67
- ##
68
- # @private Create an empty Job object.
69
- def initialize
70
- @service = nil
71
- @gapi = {}
72
- end
73
-
74
- ##
75
- # The ID of the job.
76
- def job_id
77
- @gapi.job_reference.job_id
78
- end
79
-
80
- ##
81
- # The ID of the project containing the job.
82
- def project_id
83
- @gapi.job_reference.project_id
84
- end
85
-
86
- ##
87
- # The current state of the job. The possible values are `PENDING`,
88
- # `RUNNING`, and `DONE`. A `DONE` state does not mean that the job
89
- # completed successfully. Use {#failed?} to discover if an error occurred
90
- # or if the job was successful.
91
- def state
92
- return nil if @gapi.status.nil?
93
- @gapi.status.state
94
- end
95
-
96
- ##
97
- # Checks if the job's state is `RUNNING`.
98
- def running?
99
- return false if state.nil?
100
- "running".casecmp(state).zero?
101
- end
102
-
103
- ##
104
- # Checks if the job's state is `PENDING`.
105
- def pending?
106
- return false if state.nil?
107
- "pending".casecmp(state).zero?
108
- end
109
-
110
- ##
111
- # Checks if the job's state is `DONE`. When `true`, the job has stopped
112
- # running. However, a `DONE` state does not mean that the job completed
113
- # successfully. Use {#failed?} to detect if an error occurred or if the
114
- # job was successful.
115
- def done?
116
- return false if state.nil?
117
- "done".casecmp(state).zero?
118
- end
119
-
120
- ##
121
- # Checks if an error is present.
122
- def failed?
123
- !error.nil?
124
- end
125
-
126
- ##
127
- # The time when the job was created.
128
- def created_at
129
- Time.at(Integer(@gapi.statistics.creation_time) / 1000.0)
130
- rescue
131
- nil
132
- end
133
-
134
- ##
135
- # The time when the job was started.
136
- # This field is present after the job's state changes from `PENDING`
137
- # to either `RUNNING` or `DONE`.
138
- def started_at
139
- Time.at(Integer(@gapi.statistics.start_time) / 1000.0)
140
- rescue
141
- nil
142
- end
143
-
144
- ##
145
- # The time when the job ended.
146
- # This field is present when the job's state is `DONE`.
147
- def ended_at
148
- Time.at(Integer(@gapi.statistics.end_time) / 1000.0)
149
- rescue
150
- nil
151
- end
152
-
153
- ##
154
- # The configuration for the job. Returns a hash.
155
- #
156
- # @see https://cloud.google.com/bigquery/docs/reference/v2/jobs Jobs API
157
- # reference
158
- def configuration
159
- JSON.parse @gapi.configuration.to_json
160
- end
161
- alias_method :config, :configuration
162
-
163
- ##
164
- # The statistics for the job. Returns a hash.
165
- #
166
- # @see https://cloud.google.com/bigquery/docs/reference/v2/jobs Jobs API
167
- # reference
168
- def statistics
169
- JSON.parse @gapi.statistics.to_json
170
- end
171
- alias_method :stats, :statistics
172
-
173
- ##
174
- # The job's status. Returns a hash. The values contained in the hash are
175
- # also exposed by {#state}, {#error}, and {#errors}.
176
- def status
177
- JSON.parse @gapi.status.to_json
178
- end
179
-
180
- ##
181
- # The last error for the job, if any errors have occurred. Returns a
182
- # hash.
183
- #
184
- # @see https://cloud.google.com/bigquery/docs/reference/v2/jobs Jobs API
185
- # reference
186
- #
187
- # @return [Hash] Returns a hash containing `reason` and `message` keys:
188
- #
189
- # {
190
- # "reason"=>"notFound",
191
- # "message"=>"Not found: Table publicdata:samples.BAD_ID"
192
- # }
193
- #
194
- def error
195
- return nil if @gapi.status.nil?
196
- return nil if @gapi.status.error_result.nil?
197
- JSON.parse @gapi.status.error_result.to_json
198
- end
199
-
200
- ##
201
- # The errors for the job, if any errors have occurred. Returns an array
202
- # of hash objects. See {#error}.
203
- def errors
204
- return [] if @gapi.status.nil?
205
- Array(@gapi.status.errors).map { |e| JSON.parse e.to_json }
206
- end
207
-
208
- ##
209
- # Created a new job with the current configuration.
210
- def rerun!
211
- ensure_service!
212
- gapi = service.insert_job @gapi.configuration
213
- Job.from_gapi gapi, service
214
- end
215
-
216
- ##
217
- # Reloads the job with current data from the BigQuery service.
218
- def reload!
219
- ensure_service!
220
- gapi = service.get_job job_id
221
- @gapi = gapi
222
- end
223
- alias_method :refresh!, :reload!
224
-
225
- ##
226
- # Refreshes the job until the job is `DONE`.
227
- # The delay between refreshes will incrementally increase.
228
- #
229
- # @example
230
- # require "gcloud"
231
- #
232
- # gcloud = Gcloud.new
233
- # bigquery = gcloud.bigquery
234
- # dataset = bigquery.dataset "my_dataset"
235
- # table = dataset.table "my_table"
236
- #
237
- # extract_job = table.extract "gs://my-bucket/file-name.json",
238
- # format: "json"
239
- # extract_job.wait_until_done!
240
- # extract_job.done? #=> true
241
- def wait_until_done!
242
- backoff = ->(retries) { sleep 2 * retries + 5 }
243
- retries = 0
244
- until done?
245
- backoff.call retries
246
- retries += 1
247
- reload!
248
- end
249
- end
250
-
251
- ##
252
- # @private New Job from a Google API Client object.
253
- def self.from_gapi gapi, conn
254
- klass = klass_for gapi
255
- klass.new.tap do |f|
256
- f.gapi = gapi
257
- f.service = conn
258
- end
259
- end
260
-
261
- protected
262
-
263
- ##
264
- # Raise an error unless an active connection is available.
265
- def ensure_service!
266
- fail "Must have active connection" unless service
267
- end
268
-
269
- ##
270
- # Get the subclass for a job type
271
- def self.klass_for gapi
272
- if gapi.configuration.copy
273
- return CopyJob
274
- elsif gapi.configuration.extract
275
- return ExtractJob
276
- elsif gapi.configuration.load
277
- return LoadJob
278
- elsif gapi.configuration.query
279
- return QueryJob
280
- end
281
- Job
282
- end
283
-
284
- def retrieve_table project_id, dataset_id, table_id
285
- ensure_service!
286
- gapi = service.get_project_table project_id, dataset_id, table_id
287
- Table.from_gapi gapi, service
288
- rescue Gcloud::NotFoundError
289
- nil
290
- end
291
- end
292
- end
293
- end
294
-
295
- # We need Job to be defined before loading these.
296
- require "gcloud/bigquery/copy_job"
297
- require "gcloud/bigquery/extract_job"
298
- require "gcloud/bigquery/load_job"
299
- require "gcloud/bigquery/query_job"