gcloud 0.11.0 → 0.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +8 -8
  2. data/AUTHENTICATION.md +3 -3
  3. data/CHANGELOG.md +92 -0
  4. data/OVERVIEW.md +3 -3
  5. data/lib/gcloud.rb +75 -25
  6. data/lib/gcloud/backoff.rb +5 -1
  7. data/lib/gcloud/bigquery.rb +25 -43
  8. data/lib/gcloud/bigquery/copy_job.rb +13 -13
  9. data/lib/gcloud/bigquery/data.rb +20 -16
  10. data/lib/gcloud/bigquery/dataset.rb +202 -177
  11. data/lib/gcloud/bigquery/dataset/access.rb +118 -104
  12. data/lib/gcloud/bigquery/dataset/list.rb +14 -18
  13. data/lib/gcloud/bigquery/extract_job.rb +12 -12
  14. data/lib/gcloud/bigquery/insert_response.rb +12 -14
  15. data/lib/gcloud/bigquery/job.rb +45 -57
  16. data/lib/gcloud/bigquery/job/list.rb +18 -24
  17. data/lib/gcloud/bigquery/load_job.rb +35 -27
  18. data/lib/gcloud/bigquery/project.rb +53 -73
  19. data/lib/gcloud/bigquery/query_data.rb +28 -35
  20. data/lib/gcloud/bigquery/query_job.rb +18 -18
  21. data/lib/gcloud/bigquery/schema.rb +359 -0
  22. data/lib/gcloud/bigquery/service.rb +506 -0
  23. data/lib/gcloud/bigquery/table.rb +185 -266
  24. data/lib/gcloud/bigquery/table/list.rb +15 -19
  25. data/lib/gcloud/bigquery/view.rb +126 -81
  26. data/lib/gcloud/datastore.rb +39 -27
  27. data/lib/gcloud/datastore/commit.rb +2 -2
  28. data/lib/gcloud/datastore/dataset.rb +8 -19
  29. data/lib/gcloud/datastore/dataset/lookup_results.rb +2 -4
  30. data/lib/gcloud/datastore/dataset/query_results.rb +0 -2
  31. data/lib/gcloud/datastore/entity.rb +7 -1
  32. data/lib/gcloud/datastore/errors.rb +5 -27
  33. data/lib/gcloud/datastore/grpc_utils.rb +4 -3
  34. data/lib/gcloud/datastore/key.rb +6 -0
  35. data/lib/gcloud/datastore/service.rb +18 -12
  36. data/lib/gcloud/datastore/transaction.rb +0 -10
  37. data/lib/gcloud/dns.rb +29 -19
  38. data/lib/gcloud/dns/change.rb +10 -15
  39. data/lib/gcloud/dns/change/list.rb +4 -4
  40. data/lib/gcloud/dns/importer.rb +1 -1
  41. data/lib/gcloud/dns/project.rb +32 -49
  42. data/lib/gcloud/dns/record.rb +8 -2
  43. data/lib/gcloud/dns/record/list.rb +4 -4
  44. data/lib/gcloud/dns/service.rb +167 -0
  45. data/lib/gcloud/dns/zone.rb +33 -52
  46. data/lib/gcloud/dns/zone/list.rb +12 -16
  47. data/lib/gcloud/errors.rb +31 -19
  48. data/lib/gcloud/logging.rb +50 -39
  49. data/lib/gcloud/logging/entry.rb +197 -24
  50. data/lib/gcloud/logging/entry/list.rb +0 -2
  51. data/lib/gcloud/logging/logger.rb +1 -1
  52. data/lib/gcloud/logging/metric.rb +3 -9
  53. data/lib/gcloud/logging/metric/list.rb +0 -2
  54. data/lib/gcloud/logging/project.rb +58 -54
  55. data/lib/gcloud/logging/resource_descriptor.rb +2 -2
  56. data/lib/gcloud/logging/resource_descriptor/list.rb +0 -2
  57. data/lib/gcloud/logging/service.rb +32 -23
  58. data/lib/gcloud/logging/sink.rb +8 -14
  59. data/lib/gcloud/logging/sink/list.rb +0 -2
  60. data/lib/gcloud/pubsub.rb +21 -16
  61. data/lib/gcloud/pubsub/policy.rb +204 -0
  62. data/lib/gcloud/pubsub/project.rb +26 -38
  63. data/lib/gcloud/pubsub/service.rb +39 -31
  64. data/lib/gcloud/pubsub/subscription.rb +56 -59
  65. data/lib/gcloud/pubsub/subscription/list.rb +4 -4
  66. data/lib/gcloud/pubsub/topic.rb +69 -66
  67. data/lib/gcloud/pubsub/topic/list.rb +0 -2
  68. data/lib/gcloud/pubsub/topic/{batch.rb → publisher.rb} +15 -2
  69. data/lib/gcloud/resource_manager.rb +27 -26
  70. data/lib/gcloud/resource_manager/manager.rb +19 -39
  71. data/lib/gcloud/resource_manager/policy.rb +211 -0
  72. data/lib/gcloud/resource_manager/project.rb +97 -121
  73. data/lib/gcloud/resource_manager/project/list.rb +7 -7
  74. data/lib/gcloud/resource_manager/project/updater.rb +4 -9
  75. data/lib/gcloud/resource_manager/service.rb +127 -0
  76. data/lib/gcloud/storage.rb +24 -42
  77. data/lib/gcloud/storage/bucket.rb +104 -192
  78. data/lib/gcloud/storage/bucket/acl.rb +47 -143
  79. data/lib/gcloud/storage/bucket/cors.rb +55 -11
  80. data/lib/gcloud/storage/bucket/list.rb +14 -14
  81. data/lib/gcloud/storage/errors.rb +3 -43
  82. data/lib/gcloud/storage/file.rb +114 -111
  83. data/lib/gcloud/storage/file/acl.rb +27 -113
  84. data/lib/gcloud/storage/file/list.rb +21 -21
  85. data/lib/gcloud/storage/project.rb +49 -59
  86. data/lib/gcloud/storage/service.rb +347 -0
  87. data/lib/gcloud/translate.rb +24 -14
  88. data/lib/gcloud/translate/api.rb +12 -21
  89. data/lib/gcloud/translate/detection.rb +5 -5
  90. data/lib/gcloud/translate/language.rb +1 -1
  91. data/lib/gcloud/translate/service.rb +80 -0
  92. data/lib/gcloud/translate/translation.rb +6 -6
  93. data/lib/gcloud/version.rb +1 -1
  94. data/lib/gcloud/vision.rb +24 -15
  95. data/lib/gcloud/vision/annotate.rb +24 -21
  96. data/lib/gcloud/vision/annotation.rb +9 -9
  97. data/lib/gcloud/vision/annotation/entity.rb +11 -11
  98. data/lib/gcloud/vision/annotation/face.rb +25 -25
  99. data/lib/gcloud/vision/annotation/properties.rb +8 -8
  100. data/lib/gcloud/vision/annotation/safe_search.rb +4 -4
  101. data/lib/gcloud/vision/annotation/text.rb +7 -7
  102. data/lib/gcloud/vision/annotation/vertex.rb +1 -1
  103. data/lib/gcloud/vision/image.rb +11 -11
  104. data/lib/gcloud/vision/location.rb +5 -2
  105. data/lib/gcloud/vision/project.rb +14 -16
  106. data/lib/gcloud/vision/service.rb +66 -0
  107. data/lib/google/api_client.rb +0 -0
  108. metadata +27 -24
  109. data/lib/gcloud/bigquery/connection.rb +0 -624
  110. data/lib/gcloud/bigquery/errors.rb +0 -68
  111. data/lib/gcloud/bigquery/table/schema.rb +0 -234
  112. data/lib/gcloud/dns/connection.rb +0 -173
  113. data/lib/gcloud/dns/errors.rb +0 -68
  114. data/lib/gcloud/resource_manager/connection.rb +0 -134
  115. data/lib/gcloud/resource_manager/errors.rb +0 -68
  116. data/lib/gcloud/storage/connection.rb +0 -444
  117. data/lib/gcloud/translate/connection.rb +0 -85
  118. data/lib/gcloud/translate/errors.rb +0 -68
  119. data/lib/gcloud/upload.rb +0 -95
  120. data/lib/gcloud/vision/connection.rb +0 -63
  121. data/lib/gcloud/vision/errors.rb +0 -69
@@ -1,68 +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
-
18
- module Gcloud
19
- module Dns
20
- ##
21
- # Base DNS exception class.
22
- class Error < Gcloud::Error
23
- end
24
-
25
- ##
26
- # Raised when an API call is not successful.
27
- class ApiError < Error
28
- ##
29
- # The code of the error.
30
- attr_reader :code
31
-
32
- ##
33
- # The errors encountered.
34
- attr_reader :errors
35
-
36
- # @private
37
- def initialize message, code, errors = []
38
- super message
39
- @code = code
40
- @errors = errors
41
- end
42
-
43
- # @private
44
- def self.from_response resp
45
- if resp.data? && resp.data["error"]
46
- from_response_data resp.data["error"]
47
- else
48
- from_response_status resp
49
- end
50
- end
51
-
52
- # @private
53
- def self.from_response_data error
54
- new error["message"], error["code"], error["errors"]
55
- end
56
-
57
- # @private
58
- def self.from_response_status resp
59
- if resp.status == 404
60
- new "#{resp.error_message}: #{resp.request.uri.request_uri}",
61
- resp.status
62
- else
63
- new resp.error_message, resp.status
64
- end
65
- end
66
- end
67
- end
68
- end
@@ -1,134 +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/version"
17
- require "gcloud/backoff"
18
- require "google/api_client"
19
-
20
- module Gcloud
21
- module ResourceManager
22
- ##
23
- # @private
24
- # Represents the connection to Resource Manager, as well as expose the API
25
- # calls.
26
- class Connection
27
- API_VERSION = "v1beta1"
28
-
29
- attr_accessor :credentials
30
-
31
- ##
32
- # Creates a new Connection instance.
33
- def initialize credentials
34
- @credentials = credentials
35
- @client = Google::APIClient.new application_name: "gcloud-ruby",
36
- application_version: Gcloud::VERSION
37
- @client.authorization = @credentials.client
38
- @res_man = @client.discovered_api "cloudresourcemanager", API_VERSION
39
- end
40
-
41
- def list_project filter: nil, token: nil, max: nil
42
- params = { filter: filter,
43
- pageToken: token,
44
- maxResults: max
45
- }.delete_if { |_, v| v.nil? }
46
-
47
- execute(
48
- api_method: @res_man.projects.list,
49
- parameters: params
50
- )
51
- end
52
-
53
- def get_project project_id
54
- execute(
55
- api_method: @res_man.projects.get,
56
- parameters: { projectId: project_id }
57
- )
58
- end
59
-
60
- def create_project project_id, name, labels
61
- project_gapi = { projectId: project_id, name: name,
62
- labels: labels }.delete_if { |_, v| v.nil? }
63
-
64
- execute(
65
- api_method: @res_man.projects.create,
66
- body_object: project_gapi
67
- )
68
- end
69
-
70
- ##
71
- # Updated the project, given the project Google API Client object/hash.
72
- # We try not to pass the gapi objects, but there is no PATCH, so we need
73
- # to pass in a complete Project object.
74
- def update_project project_gapi
75
- project_id = project_gapi["projectId"]
76
-
77
- execute(
78
- api_method: @res_man.projects.update,
79
- parameters: { projectId: project_id },
80
- body_object: project_gapi
81
- )
82
- end
83
-
84
- def delete_project project_id
85
- execute(
86
- api_method: @res_man.projects.delete,
87
- parameters: { projectId: project_id }
88
- )
89
- end
90
-
91
- def undelete_project project_id
92
- execute(
93
- api_method: @res_man.projects.undelete,
94
- parameters: { projectId: project_id }
95
- )
96
- end
97
-
98
- def get_policy project_id
99
- execute(
100
- api_method: @res_man.projects.get_iam_policy,
101
- parameters: { resource: project_id }
102
- )
103
- end
104
-
105
- def set_policy project_id, new_policy
106
- execute(
107
- api_method: @res_man.projects.set_iam_policy,
108
- parameters: { resource: project_id },
109
- body_object: { policy: new_policy }
110
- )
111
- end
112
-
113
- def test_permissions project_id, permissions
114
- execute(
115
- api_method: @res_man.projects.test_iam_permissions,
116
- parameters: { resource: project_id },
117
- body_object: { permissions: permissions }
118
- )
119
- end
120
-
121
- def inspect
122
- "#{self.class}(#{@project})"
123
- end
124
-
125
- protected
126
-
127
- def execute options
128
- Gcloud::Backoff.new.execute_gapi do
129
- @client.execute options
130
- end
131
- end
132
- end
133
- end
134
- end
@@ -1,68 +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
-
18
- module Gcloud
19
- module ResourceManager
20
- ##
21
- # Base Resource Manager exception class.
22
- class Error < Gcloud::Error
23
- end
24
-
25
- ##
26
- # Raised when an API call is not successful.
27
- class ApiError < Error
28
- ##
29
- # The code of the error.
30
- attr_reader :code
31
-
32
- ##
33
- # The errors encountered.
34
- attr_reader :errors
35
-
36
- # @private
37
- def initialize message, code, errors = []
38
- super message
39
- @code = code
40
- @errors = errors
41
- end
42
-
43
- # @private
44
- def self.from_response resp
45
- if resp.data? && resp.data["error"]
46
- from_response_data resp.data["error"]
47
- else
48
- from_response_status resp
49
- end
50
- end
51
-
52
- # @private
53
- def self.from_response_data error
54
- new error["message"], error["code"], error["errors"]
55
- end
56
-
57
- # @private
58
- def self.from_response_status resp
59
- if resp.status == 404
60
- new "#{resp.error_message}: #{resp.request.uri.request_uri}",
61
- resp.status
62
- else
63
- new resp.error_message, resp.status
64
- end
65
- end
66
- end
67
- end
68
- end
@@ -1,444 +0,0 @@
1
- # Copyright 2014 Google Inc. All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- require "pathname"
17
- require "gcloud/version"
18
- require "gcloud/backoff"
19
- require "gcloud/upload"
20
- require "google/api_client"
21
- require "mime/types"
22
- require "digest/sha2"
23
-
24
- module Gcloud
25
- module Storage
26
- ##
27
- # @private Represents the connection to Storage,
28
- # as well as expose the API calls.
29
- class Connection
30
- API_VERSION = "v1"
31
-
32
- attr_accessor :project
33
-
34
- # @private
35
- attr_accessor :credentials
36
-
37
- ##
38
- # Creates a new Connection instance.
39
- def initialize project, credentials
40
- @project = project
41
- @credentials = credentials
42
- @client = Google::APIClient.new application_name: "gcloud-ruby",
43
- application_version: Gcloud::VERSION
44
- @client.authorization = @credentials.client
45
- @storage = @client.discovered_api "storage", API_VERSION
46
- end
47
-
48
- ##
49
- # Retrieves a list of buckets for the given project.
50
- def list_buckets options = {}
51
- params = { project: @project }
52
- params["prefix"] = options[:prefix] if options[:prefix]
53
- params["pageToken"] = options[:token] if options[:token]
54
- params["maxResults"] = options[:max] if options[:max]
55
-
56
- execute(
57
- api_method: @storage.buckets.list,
58
- parameters: params
59
- )
60
- end
61
-
62
- ##
63
- # Retrieves bucket by name.
64
- def get_bucket bucket_name
65
- execute(
66
- api_method: @storage.buckets.get,
67
- parameters: { bucket: bucket_name }
68
- )
69
- end
70
-
71
- ##
72
- # Creates a new bucket.
73
- def insert_bucket bucket_name, options = {}
74
- params = { project: @project, predefinedAcl: options[:acl],
75
- predefinedDefaultObjectAcl: options[:default_acl]
76
- }.delete_if { |_, v| v.nil? }
77
-
78
- execute(
79
- api_method: @storage.buckets.insert,
80
- parameters: params,
81
- body_object: insert_bucket_request(bucket_name, options)
82
- )
83
- end
84
-
85
- ##
86
- # Updates a bucket, including its ACL metadata.
87
- def patch_bucket bucket_name, options = {}
88
- params = { bucket: bucket_name,
89
- predefinedAcl: options[:predefined_acl],
90
- predefinedDefaultObjectAcl: options[:predefined_default_acl]
91
- }.delete_if { |_, v| v.nil? }
92
-
93
- execute(
94
- api_method: @storage.buckets.patch,
95
- parameters: params,
96
- body_object: patch_bucket_request(options)
97
- )
98
- end
99
-
100
- ##
101
- # Permanently deletes an empty bucket.
102
- def delete_bucket bucket_name
103
- execute(
104
- api_method: @storage.buckets.delete,
105
- parameters: { bucket: bucket_name }
106
- )
107
- end
108
-
109
- ##
110
- # Retrieves a list of ACLs for the given bucket.
111
- def list_bucket_acls bucket_name
112
- execute(
113
- api_method: @storage.bucket_access_controls.list,
114
- parameters: { bucket: bucket_name }
115
- )
116
- end
117
-
118
- ##
119
- # Creates a new bucket ACL.
120
- def insert_bucket_acl bucket_name, entity, role
121
- execute(
122
- api_method: @storage.bucket_access_controls.insert,
123
- parameters: { bucket: bucket_name },
124
- body_object: { entity: entity, role: role }
125
- )
126
- end
127
-
128
- ##
129
- # Permanently deletes a bucket ACL.
130
- def delete_bucket_acl bucket_name, entity
131
- execute(
132
- api_method: @storage.bucket_access_controls.delete,
133
- parameters: { bucket: bucket_name, entity: entity }
134
- )
135
- end
136
-
137
- ##
138
- # Retrieves a list of default ACLs for the given bucket.
139
- def list_default_acls bucket_name
140
- execute(
141
- api_method: @storage.default_object_access_controls.list,
142
- parameters: { bucket: bucket_name }
143
- )
144
- end
145
-
146
- ##
147
- # Creates a new default ACL.
148
- def insert_default_acl bucket_name, entity, role
149
- execute(
150
- api_method: @storage.default_object_access_controls.insert,
151
- parameters: { bucket: bucket_name },
152
- body_object: { entity: entity, role: role }
153
- )
154
- end
155
-
156
- ##
157
- # Permanently deletes a default ACL.
158
- def delete_default_acl bucket_name, entity
159
- execute(
160
- api_method: @storage.default_object_access_controls.delete,
161
- parameters: { bucket: bucket_name, entity: entity }
162
- )
163
- end
164
-
165
- ##
166
- # Retrieves a list of files matching the criteria.
167
- def list_files bucket_name, options = {}
168
- params = {
169
- bucket: bucket_name,
170
- prefix: options[:prefix],
171
- delimiter: options[:delimiter],
172
- pageToken: options[:token],
173
- maxResults: options[:max],
174
- versions: options[:versions]
175
- }.delete_if { |_, v| v.nil? }
176
-
177
- execute(
178
- api_method: @storage.objects.list,
179
- parameters: params
180
- )
181
- end
182
-
183
- ##
184
- # Stores a new object and metadata. If resumable is true, a resumable
185
- # upload, otherwise uses a multipart form post.
186
- #
187
- # UploadIO comes from Faraday, which gets it from multipart-post
188
- # The initializer signature is:
189
- # filename_or_io, content_type, filename = nil, opts = {}
190
- def upload_file resumable, bucket_name, file, path = nil, options = {}
191
- local_path = Pathname(file).to_path
192
- options[:content_type] ||= mime_type_for(local_path)
193
- media = file_media local_path, options, resumable
194
- upload_path = Pathname(path || local_path).to_path
195
- result = insert_file resumable, bucket_name, upload_path, media, options
196
- return result unless resumable
197
- upload = result.resumable_upload
198
- result = execute upload while upload.resumable?
199
- result
200
- end
201
-
202
- ##
203
- # Retrieves an object or its metadata.
204
- def get_file bucket_name, file_path, options = {}
205
- query = { bucket: bucket_name, object: file_path }
206
- query[:generation] = options[:generation] if options[:generation]
207
-
208
- request_options = {
209
- api_method: @storage.objects.get,
210
- parameters: query
211
- }
212
-
213
- request_options = add_headers request_options, options
214
- execute request_options
215
- end
216
-
217
- ## Copy a file from source bucket/object to a
218
- # destination bucket/object.
219
- def copy_file source_bucket_name, source_file_path,
220
- destination_bucket_name, destination_file_path, options = {}
221
- request_options = {
222
- api_method: @storage.objects.copy,
223
- parameters: { sourceBucket: source_bucket_name,
224
- sourceObject: source_file_path,
225
- sourceGeneration: options[:generation],
226
- destinationBucket: destination_bucket_name,
227
- destinationObject: destination_file_path,
228
- predefinedAcl: options[:acl]
229
- }.delete_if { |_, v| v.nil? }
230
- }
231
- request_options = add_headers request_options, options
232
- execute request_options
233
- end
234
-
235
- ##
236
- # Download contents of a file.
237
-
238
- def download_file bucket_name, file_path, options = {}
239
- request_options = {
240
- api_method: @storage.objects.get,
241
- parameters: { bucket: bucket_name,
242
- object: file_path,
243
- alt: :media }
244
- }
245
- request_options = add_headers request_options, options
246
- execute request_options
247
- end
248
-
249
- ##
250
- # Updates a file's metadata.
251
- def patch_file bucket_name, file_path, options = {}
252
- params = { bucket: bucket_name,
253
- object: file_path,
254
- predefinedAcl: options[:predefined_acl]
255
- }.delete_if { |_, v| v.nil? }
256
-
257
- execute(
258
- api_method: @storage.objects.patch,
259
- parameters: params,
260
- body_object: patch_file_request(options)
261
- )
262
- end
263
-
264
- ##
265
- # Permanently deletes a file.
266
- def delete_file bucket_name, file_path
267
- execute(
268
- api_method: @storage.objects.delete,
269
- parameters: { bucket: bucket_name,
270
- object: file_path }
271
- )
272
- end
273
-
274
- ##
275
- # Retrieves a list of ACLs for the given file.
276
- def list_file_acls bucket_name, file_name
277
- execute(
278
- api_method: @storage.object_access_controls.list,
279
- parameters: { bucket: bucket_name, object: file_name }
280
- )
281
- end
282
-
283
- ##
284
- # Creates a new file ACL.
285
- def insert_file_acl bucket_name, file_name, entity, role, options = {}
286
- query = { bucket: bucket_name, object: file_name }
287
- query[:generation] = options[:generation] if options[:generation]
288
-
289
- execute(
290
- api_method: @storage.object_access_controls.insert,
291
- parameters: query,
292
- body_object: { entity: entity, role: role }
293
- )
294
- end
295
-
296
- ##
297
- # Permanently deletes a file ACL.
298
- def delete_file_acl bucket_name, file_name, entity, options = {}
299
- query = { bucket: bucket_name, object: file_name, entity: entity }
300
- query[:generation] = options[:generation] if options[:generation]
301
-
302
- execute(
303
- api_method: @storage.object_access_controls.delete,
304
- parameters: query
305
- )
306
- end
307
-
308
- ##
309
- # Retrieves the mime-type for a file path.
310
- # An empty string is returned if no mime-type can be found.
311
- def mime_type_for path
312
- MIME::Types.of(path).first.to_s
313
- end
314
-
315
- # @private
316
- def inspect
317
- "#{self.class}(#{@project})"
318
- end
319
-
320
- protected
321
-
322
- def insert_bucket_request name, options = {}
323
- {
324
- "name" => name,
325
- "location" => options[:location],
326
- "cors" => options[:cors],
327
- "logging" => logging_config(options),
328
- "storageClass" => storage_class(options[:storage_class]),
329
- "versioning" => versioning_config(options[:versioning]),
330
- "website" => website_config(options)
331
- }.delete_if { |_, v| v.nil? }
332
- end
333
-
334
- def patch_bucket_request options = {}
335
- {
336
- "cors" => options[:cors],
337
- "logging" => logging_config(options),
338
- "versioning" => versioning_config(options[:versioning]),
339
- "website" => website_config(options),
340
- "acl" => options[:acl],
341
- "defaultObjectAcl" => options[:default_acl]
342
- }.delete_if { |_, v| v.nil? }
343
- end
344
-
345
- def versioning_config enabled
346
- { "enabled" => enabled } unless enabled.nil?
347
- end
348
-
349
- def logging_config options
350
- bucket = options[:logging_bucket]
351
- prefix = options[:logging_prefix]
352
- {
353
- "logBucket" => bucket,
354
- "logObjectPrefix" => prefix
355
- }.delete_if { |_, v| v.nil? } if bucket || prefix
356
- end
357
-
358
- def website_config options
359
- website_main = options[:website_main]
360
- website_404 = options[:website_404]
361
- {
362
- "mainPageSuffix" => website_main,
363
- "notFoundPage" => website_404
364
- }.delete_if { |_, v| v.nil? } if website_main || website_404
365
- end
366
-
367
- # @private
368
- def storage_class str
369
- { "durable_reduced_availability" => "DURABLE_REDUCED_AVAILABILITY",
370
- "dra" => "DURABLE_REDUCED_AVAILABILITY",
371
- "durable" => "DURABLE_REDUCED_AVAILABILITY",
372
- "nearline" => "NEARLINE",
373
- "standard" => "STANDARD" }[str.to_s.downcase]
374
- end
375
-
376
- def insert_file resumable, bucket_name, path, media, options
377
- params = { uploadType: (resumable ? "resumable" : "multipart"),
378
- bucket: bucket_name,
379
- name: path,
380
- predefinedAcl: options[:acl]
381
- }.delete_if { |_, v| v.nil? }
382
-
383
- request_options = {
384
- api_method: @storage.objects.insert,
385
- media: media,
386
- parameters: params,
387
- body_object: insert_file_request(options)
388
- }
389
- request_options = add_headers request_options, options
390
- execute request_options
391
- end
392
-
393
- def file_media local_path, options, resumable
394
- media = Google::APIClient::UploadIO.new local_path,
395
- options[:content_type]
396
- return media unless resumable
397
- media.chunk_size = Gcloud::Upload.verify_chunk_size(
398
- options.delete(:chunk_size), media.length)
399
- media
400
- end
401
-
402
- def insert_file_request options = {}
403
- request = {
404
- "crc32c" => options[:crc32c],
405
- "md5Hash" => options[:md5],
406
- "metadata" => options[:metadata]
407
- }.delete_if { |_, v| v.nil? }
408
- request.merge patch_file_request(options)
409
- end
410
-
411
- def patch_file_request options = {}
412
- {
413
- "cacheControl" => options[:cache_control],
414
- "contentDisposition" => options[:content_disposition],
415
- "contentEncoding" => options[:content_encoding],
416
- "contentLanguage" => options[:content_language],
417
- "contentType" => options[:content_type],
418
- "metadata" => options[:metadata],
419
- "acl" => options[:acl]
420
- }.delete_if { |_, v| v.nil? }
421
- end
422
-
423
- def add_headers request_options, options
424
- headers = (request_options[:headers] ||= {})
425
- if options[:encryption_key] || options[:encryption_key_sha256]
426
- headers["x-goog-encryption-algorithm"] = "AES256"
427
- end
428
- if (key = options.delete(:encryption_key))
429
- headers["x-goog-encryption-key"] = Base64.encode64 key
430
- end
431
- if (key_sha256 = options.delete(:encryption_key_sha256))
432
- headers["x-goog-encryption-key-sha256"] = Base64.encode64 key_sha256
433
- end
434
- request_options
435
- end
436
-
437
- def execute options
438
- Gcloud::Backoff.new.execute_gapi do
439
- @client.execute options
440
- end
441
- end
442
- end
443
- end
444
- end