gcloud 0.12.2 → 0.20.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -13
- data/lib/gcloud.rb +27 -456
- data/lib/gcloud/bigquery.rb +2 -382
- data/lib/gcloud/datastore.rb +2 -576
- data/lib/gcloud/dns.rb +2 -321
- data/lib/gcloud/logging.rb +1 -322
- data/lib/gcloud/pubsub.rb +2 -476
- data/lib/gcloud/resource_manager.rb +2 -273
- data/lib/gcloud/storage.rb +2 -440
- data/lib/gcloud/translate.rb +1 -250
- data/lib/gcloud/version.rb +2 -2
- data/lib/gcloud/vision.rb +1 -501
- metadata +36 -332
- data/AUTHENTICATION.md +0 -75
- data/CHANGELOG.md +0 -382
- data/OVERVIEW.md +0 -259
- data/lib/gcloud/backoff.rb +0 -150
- data/lib/gcloud/bigquery/copy_job.rb +0 -97
- data/lib/gcloud/bigquery/credentials.rb +0 -29
- data/lib/gcloud/bigquery/data.rb +0 -239
- data/lib/gcloud/bigquery/dataset.rb +0 -753
- data/lib/gcloud/bigquery/dataset/access.rb +0 -507
- data/lib/gcloud/bigquery/dataset/list.rb +0 -169
- data/lib/gcloud/bigquery/extract_job.rb +0 -117
- data/lib/gcloud/bigquery/insert_response.rb +0 -81
- data/lib/gcloud/bigquery/job.rb +0 -299
- data/lib/gcloud/bigquery/job/list.rb +0 -172
- data/lib/gcloud/bigquery/load_job.rb +0 -202
- data/lib/gcloud/bigquery/project.rb +0 -475
- data/lib/gcloud/bigquery/query_data.rb +0 -234
- data/lib/gcloud/bigquery/query_job.rb +0 -137
- data/lib/gcloud/bigquery/schema.rb +0 -359
- data/lib/gcloud/bigquery/service.rb +0 -506
- data/lib/gcloud/bigquery/table.rb +0 -1141
- data/lib/gcloud/bigquery/table/list.rb +0 -180
- data/lib/gcloud/bigquery/view.rb +0 -475
- data/lib/gcloud/credentials.rb +0 -129
- data/lib/gcloud/datastore/commit.rb +0 -148
- data/lib/gcloud/datastore/credentials.rb +0 -35
- data/lib/gcloud/datastore/cursor.rb +0 -76
- data/lib/gcloud/datastore/dataset.rb +0 -660
- data/lib/gcloud/datastore/dataset/lookup_results.rb +0 -219
- data/lib/gcloud/datastore/dataset/query_results.rb +0 -386
- data/lib/gcloud/datastore/entity.rb +0 -449
- data/lib/gcloud/datastore/errors.rb +0 -41
- data/lib/gcloud/datastore/gql_query.rb +0 -211
- data/lib/gcloud/datastore/grpc_utils.rb +0 -132
- data/lib/gcloud/datastore/key.rb +0 -281
- data/lib/gcloud/datastore/properties.rb +0 -128
- data/lib/gcloud/datastore/query.rb +0 -348
- data/lib/gcloud/datastore/service.rb +0 -167
- data/lib/gcloud/datastore/transaction.rb +0 -362
- data/lib/gcloud/dns/change.rb +0 -158
- data/lib/gcloud/dns/change/list.rb +0 -173
- data/lib/gcloud/dns/credentials.rb +0 -29
- data/lib/gcloud/dns/importer.rb +0 -183
- data/lib/gcloud/dns/project.rb +0 -247
- data/lib/gcloud/dns/record.rb +0 -170
- data/lib/gcloud/dns/record/list.rb +0 -174
- data/lib/gcloud/dns/service.rb +0 -167
- data/lib/gcloud/dns/zone.rb +0 -759
- data/lib/gcloud/dns/zone/list.rb +0 -168
- data/lib/gcloud/dns/zone/transaction.rb +0 -176
- data/lib/gcloud/errors.rb +0 -206
- data/lib/gcloud/gce.rb +0 -56
- data/lib/gcloud/grpc_utils.rb +0 -87
- data/lib/gcloud/logging/credentials.rb +0 -29
- data/lib/gcloud/logging/entry.rb +0 -465
- data/lib/gcloud/logging/entry/http_request.rb +0 -141
- data/lib/gcloud/logging/entry/list.rb +0 -177
- data/lib/gcloud/logging/entry/operation.rb +0 -90
- data/lib/gcloud/logging/logger.rb +0 -307
- data/lib/gcloud/logging/metric.rb +0 -169
- data/lib/gcloud/logging/metric/list.rb +0 -172
- data/lib/gcloud/logging/project.rb +0 -642
- data/lib/gcloud/logging/resource.rb +0 -84
- data/lib/gcloud/logging/resource_descriptor.rb +0 -137
- data/lib/gcloud/logging/resource_descriptor/list.rb +0 -174
- data/lib/gcloud/logging/service.rb +0 -267
- data/lib/gcloud/logging/sink.rb +0 -227
- data/lib/gcloud/logging/sink/list.rb +0 -171
- data/lib/gcloud/pubsub/credentials.rb +0 -29
- data/lib/gcloud/pubsub/message.rb +0 -94
- data/lib/gcloud/pubsub/policy.rb +0 -204
- data/lib/gcloud/pubsub/project.rb +0 -482
- data/lib/gcloud/pubsub/received_message.rb +0 -160
- data/lib/gcloud/pubsub/service.rb +0 -334
- data/lib/gcloud/pubsub/subscription.rb +0 -565
- data/lib/gcloud/pubsub/subscription/list.rb +0 -208
- data/lib/gcloud/pubsub/topic.rb +0 -511
- data/lib/gcloud/pubsub/topic/list.rb +0 -174
- data/lib/gcloud/pubsub/topic/publisher.rb +0 -85
- data/lib/gcloud/resource_manager/credentials.rb +0 -30
- data/lib/gcloud/resource_manager/manager.rb +0 -266
- data/lib/gcloud/resource_manager/policy.rb +0 -211
- data/lib/gcloud/resource_manager/project.rb +0 -484
- data/lib/gcloud/resource_manager/project/list.rb +0 -167
- data/lib/gcloud/resource_manager/project/updater.rb +0 -130
- data/lib/gcloud/resource_manager/service.rb +0 -127
- data/lib/gcloud/storage/bucket.rb +0 -775
- data/lib/gcloud/storage/bucket/acl.rb +0 -810
- data/lib/gcloud/storage/bucket/cors.rb +0 -153
- data/lib/gcloud/storage/bucket/list.rb +0 -172
- data/lib/gcloud/storage/credentials.rb +0 -29
- data/lib/gcloud/storage/errors.rb +0 -65
- data/lib/gcloud/storage/file.rb +0 -842
- data/lib/gcloud/storage/file/acl.rb +0 -425
- data/lib/gcloud/storage/file/list.rb +0 -191
- data/lib/gcloud/storage/file/verifier.rb +0 -67
- data/lib/gcloud/storage/project.rb +0 -316
- data/lib/gcloud/storage/service.rb +0 -347
- data/lib/gcloud/translate/api.rb +0 -241
- data/lib/gcloud/translate/detection.rb +0 -137
- data/lib/gcloud/translate/language.rb +0 -69
- data/lib/gcloud/translate/service.rb +0 -80
- data/lib/gcloud/translate/translation.rb +0 -112
- data/lib/gcloud/vision/annotate.rb +0 -224
- data/lib/gcloud/vision/annotation.rb +0 -455
- data/lib/gcloud/vision/annotation/entity.rb +0 -234
- data/lib/gcloud/vision/annotation/face.rb +0 -1750
- data/lib/gcloud/vision/annotation/properties.rb +0 -245
- data/lib/gcloud/vision/annotation/safe_search.rb +0 -161
- data/lib/gcloud/vision/annotation/text.rb +0 -236
- data/lib/gcloud/vision/annotation/vertex.rb +0 -108
- data/lib/gcloud/vision/credentials.rb +0 -29
- data/lib/gcloud/vision/image.rb +0 -590
- data/lib/gcloud/vision/location.rb +0 -115
- data/lib/gcloud/vision/project.rb +0 -278
- data/lib/gcloud/vision/service.rb +0 -66
- data/lib/google/api/annotations.rb +0 -14
- data/lib/google/api/http.rb +0 -30
- data/lib/google/api/label.rb +0 -24
- data/lib/google/api/monitored_resource.rb +0 -25
- data/lib/google/datastore/v1beta3/datastore.rb +0 -115
- data/lib/google/datastore/v1beta3/datastore_services.rb +0 -33
- data/lib/google/datastore/v1beta3/entity.rb +0 -63
- data/lib/google/datastore/v1beta3/query.rb +0 -128
- data/lib/google/devtools/cloudtrace/v1/trace.rb +0 -78
- data/lib/google/devtools/cloudtrace/v1/trace_services.rb +0 -32
- data/lib/google/example/library/v1/library.rb +0 -91
- data/lib/google/example/library/v1/library_services.rb +0 -40
- data/lib/google/iam/v1/iam_policy.rb +0 -33
- data/lib/google/iam/v1/iam_policy_services.rb +0 -30
- data/lib/google/iam/v1/policy.rb +0 -25
- data/lib/google/logging/type/http_request.rb +0 -28
- data/lib/google/logging/type/log_severity.rb +0 -27
- data/lib/google/logging/v2/log_entry.rb +0 -44
- data/lib/google/logging/v2/logging.rb +0 -56
- data/lib/google/logging/v2/logging_config.rb +0 -59
- data/lib/google/logging/v2/logging_config_services.rb +0 -32
- data/lib/google/logging/v2/logging_metrics.rb +0 -51
- data/lib/google/logging/v2/logging_metrics_services.rb +0 -32
- data/lib/google/logging/v2/logging_services.rb +0 -31
- data/lib/google/longrunning/operations.rb +0 -50
- data/lib/google/longrunning/operations_services.rb +0 -29
- data/lib/google/protobuf/descriptor.rb +0 -0
- data/lib/google/pubsub/v1/pubsub.rb +0 -129
- data/lib/google/pubsub/v1/pubsub_services.rb +0 -56
- data/lib/google/pubsub/v1beta2/pubsub.rb +0 -126
- data/lib/google/pubsub/v1beta2/pubsub_services.rb +0 -56
- data/lib/google/rpc/code.rb +0 -32
- data/lib/google/rpc/error_details.rb +0 -61
- data/lib/google/rpc/status.rb +0 -19
- data/lib/google/type/color.rb +0 -20
- data/lib/google/type/date.rb +0 -18
- data/lib/google/type/dayofweek.rb +0 -23
- data/lib/google/type/latlng.rb +0 -17
- data/lib/google/type/money.rb +0 -18
- data/lib/google/type/timeofday.rb +0 -19
@@ -1,167 +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 ResourceManager
|
18
|
-
class Project
|
19
|
-
##
|
20
|
-
# Project::List is a special case Array with additional values.
|
21
|
-
class List < DelegateClass(::Array)
|
22
|
-
##
|
23
|
-
# If not empty, indicates that there are more projects that match
|
24
|
-
# the request and this value should be passed to continue.
|
25
|
-
attr_accessor :token
|
26
|
-
|
27
|
-
##
|
28
|
-
# @private Create a new Project::List with an array of Project
|
29
|
-
# instances.
|
30
|
-
def initialize arr = []
|
31
|
-
super arr
|
32
|
-
end
|
33
|
-
|
34
|
-
##
|
35
|
-
# Whether there is a next page of projects.
|
36
|
-
#
|
37
|
-
# @return [Boolean]
|
38
|
-
#
|
39
|
-
# @example
|
40
|
-
# require "gcloud"
|
41
|
-
#
|
42
|
-
# gcloud = Gcloud.new
|
43
|
-
# resource_manager = gcloud.resource_manager
|
44
|
-
#
|
45
|
-
# projects = resource_manager.projects
|
46
|
-
# if projects.next?
|
47
|
-
# next_projects = projects.next
|
48
|
-
# end
|
49
|
-
#
|
50
|
-
def next?
|
51
|
-
!token.nil?
|
52
|
-
end
|
53
|
-
|
54
|
-
##
|
55
|
-
# Retrieve the next page of projects.
|
56
|
-
#
|
57
|
-
# @return [Project::List]
|
58
|
-
#
|
59
|
-
# @example
|
60
|
-
# require "gcloud"
|
61
|
-
#
|
62
|
-
# gcloud = Gcloud.new
|
63
|
-
# resource_manager = gcloud.resource_manager
|
64
|
-
#
|
65
|
-
# projects = resource_manager.projects
|
66
|
-
# if projects.next?
|
67
|
-
# next_projects = projects.next
|
68
|
-
# end
|
69
|
-
#
|
70
|
-
def next
|
71
|
-
return nil unless next?
|
72
|
-
ensure_manager!
|
73
|
-
@manager.projects token: token, filter: @filter, max: @max
|
74
|
-
end
|
75
|
-
|
76
|
-
##
|
77
|
-
# Retrieves all projects by repeatedly loading {#next} until {#next?}
|
78
|
-
# returns `false`. Calls the given block once for each project, which is
|
79
|
-
# passed as the parameter.
|
80
|
-
#
|
81
|
-
# An Enumerator is returned if no block is given.
|
82
|
-
#
|
83
|
-
# This method may make several API calls until all projects are
|
84
|
-
# retrieved. Be sure to use as narrow a search criteria as possible.
|
85
|
-
# Please use with caution.
|
86
|
-
#
|
87
|
-
# @param [Integer] request_limit The upper limit of API requests to make
|
88
|
-
# to load all projects. Default is no limit.
|
89
|
-
# @yield [project] The block for accessing each project.
|
90
|
-
# @yieldparam [Project] project The project object.
|
91
|
-
#
|
92
|
-
# @return [Enumerator]
|
93
|
-
#
|
94
|
-
# @example Iterating each project by passing a block:
|
95
|
-
# require "gcloud"
|
96
|
-
#
|
97
|
-
# gcloud = Gcloud.new
|
98
|
-
# resource_manager = gcloud.resource_manager
|
99
|
-
# projects = resource_manager.projects
|
100
|
-
#
|
101
|
-
# projects.all do |project|
|
102
|
-
# puts project.project_id
|
103
|
-
# end
|
104
|
-
#
|
105
|
-
# @example Using the enumerator by not passing a block:
|
106
|
-
# require "gcloud"
|
107
|
-
#
|
108
|
-
# gcloud = Gcloud.new
|
109
|
-
# resource_manager = gcloud.resource_manager
|
110
|
-
# projects = resource_manager.projects
|
111
|
-
#
|
112
|
-
# all_project_ids = projects.all.map do |project|
|
113
|
-
# project.project_id
|
114
|
-
# end
|
115
|
-
#
|
116
|
-
# @example Limit the number of API calls made:
|
117
|
-
# require "gcloud"
|
118
|
-
#
|
119
|
-
# gcloud = Gcloud.new
|
120
|
-
# resource_manager = gcloud.resource_manager
|
121
|
-
# projects = resource_manager.projects
|
122
|
-
#
|
123
|
-
# projects.all(request_limit: 10) do |project|
|
124
|
-
# puts project.project_id
|
125
|
-
# end
|
126
|
-
#
|
127
|
-
def all request_limit: nil
|
128
|
-
request_limit = request_limit.to_i if request_limit
|
129
|
-
unless block_given?
|
130
|
-
return enum_for(:all, request_limit: request_limit)
|
131
|
-
end
|
132
|
-
results = self
|
133
|
-
loop do
|
134
|
-
results.each { |r| yield r }
|
135
|
-
if request_limit
|
136
|
-
request_limit -= 1
|
137
|
-
break if request_limit < 0
|
138
|
-
end
|
139
|
-
break unless results.next?
|
140
|
-
results = results.next
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
##
|
145
|
-
# @private New Projects::List from a response object.
|
146
|
-
def self.from_gapi gapi_list, manager, filter = nil, max = nil
|
147
|
-
projects = new(Array(gapi_list.projects).map do |gapi_object|
|
148
|
-
Project.from_gapi gapi_object, manager.service
|
149
|
-
end)
|
150
|
-
projects.instance_variable_set :@token, gapi_list.next_page_token
|
151
|
-
projects.instance_variable_set :@manager, manager
|
152
|
-
projects.instance_variable_set :@filter, filter
|
153
|
-
projects.instance_variable_set :@max, max
|
154
|
-
projects
|
155
|
-
end
|
156
|
-
|
157
|
-
protected
|
158
|
-
|
159
|
-
##
|
160
|
-
# Raise an error unless an active connection is available.
|
161
|
-
def ensure_manager!
|
162
|
-
fail "Must have active connection" unless @manager
|
163
|
-
end
|
164
|
-
end
|
165
|
-
end
|
166
|
-
end
|
167
|
-
end
|
@@ -1,130 +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 "time"
|
17
|
-
|
18
|
-
module Gcloud
|
19
|
-
module ResourceManager
|
20
|
-
class Project
|
21
|
-
##
|
22
|
-
# # Project Updater
|
23
|
-
#
|
24
|
-
# This object is used by Project#update when passed a block. These methods
|
25
|
-
# are used to update the project data in a single API call.
|
26
|
-
#
|
27
|
-
# @example
|
28
|
-
# require "gcloud"
|
29
|
-
#
|
30
|
-
# gcloud = Gcloud.new
|
31
|
-
# resource_manager = gcloud.resource_manager
|
32
|
-
# project = resource_manager.project "tokyo-rain-123"
|
33
|
-
# project.update do |p|
|
34
|
-
# p.name = "My Project"
|
35
|
-
# p.labels["env"] = "production"
|
36
|
-
# end
|
37
|
-
#
|
38
|
-
class Updater < DelegateClass(Project)
|
39
|
-
##
|
40
|
-
# @private Create an Updater object.
|
41
|
-
def initialize project
|
42
|
-
super project
|
43
|
-
end
|
44
|
-
|
45
|
-
##
|
46
|
-
# Updates the user-assigned name of the project. This field is optional
|
47
|
-
# and can remain unset.
|
48
|
-
#
|
49
|
-
# Allowed characters are: lowercase and uppercase letters, numbers,
|
50
|
-
# hyphen, single-quote, double-quote, space, and exclamation point.
|
51
|
-
#
|
52
|
-
# @example
|
53
|
-
# require "gcloud"
|
54
|
-
#
|
55
|
-
# gcloud = Gcloud.new
|
56
|
-
# resource_manager = gcloud.resource_manager
|
57
|
-
# project = resource_manager.project "tokyo-rain-123"
|
58
|
-
# project.update do |p|
|
59
|
-
# p.name = "My Project"
|
60
|
-
# end
|
61
|
-
#
|
62
|
-
def name= new_name
|
63
|
-
gapi.name = new_name
|
64
|
-
end
|
65
|
-
|
66
|
-
##
|
67
|
-
# The labels associated with this project.
|
68
|
-
#
|
69
|
-
# Label keys must be between 1 and 63 characters long and must conform
|
70
|
-
# to the following regular expression:
|
71
|
-
# <code>[a-z]([-a-z0-9]*[a-z0-9])?</code>.
|
72
|
-
#
|
73
|
-
# Label values must be between 0 and 63 characters long and must conform
|
74
|
-
# to the regular expression <code>([a-z]([-a-z0-9]*[a-z0-9])?)?</code>.
|
75
|
-
#
|
76
|
-
# No more than 256 labels can be associated with a given resource.
|
77
|
-
# (`Hash`)
|
78
|
-
#
|
79
|
-
# @example
|
80
|
-
# require "gcloud"
|
81
|
-
#
|
82
|
-
# gcloud = Gcloud.new
|
83
|
-
# resource_manager = gcloud.resource_manager
|
84
|
-
# project = resource_manager.project "tokyo-rain-123"
|
85
|
-
# project.update do |p|
|
86
|
-
# p.labels["env"] = "production"
|
87
|
-
# end
|
88
|
-
#
|
89
|
-
def labels
|
90
|
-
gapi.labels
|
91
|
-
end
|
92
|
-
|
93
|
-
##
|
94
|
-
# Updates the labels associated with this project.
|
95
|
-
#
|
96
|
-
# Label keys must be between 1 and 63 characters long and must conform
|
97
|
-
# to the following regular expression:
|
98
|
-
# <code>[a-z]([-a-z0-9]*[a-z0-9])?</code>.
|
99
|
-
#
|
100
|
-
# Label values must be between 0 and 63 characters long and must conform
|
101
|
-
# to the regular expression <code>([a-z]([-a-z0-9]*[a-z0-9])?)?</code>.
|
102
|
-
#
|
103
|
-
# No more than 256 labels can be associated with a given resource.
|
104
|
-
# (`Hash`)
|
105
|
-
#
|
106
|
-
# @example
|
107
|
-
# require "gcloud"
|
108
|
-
#
|
109
|
-
# gcloud = Gcloud.new
|
110
|
-
# resource_manager = gcloud.resource_manager
|
111
|
-
# project = resource_manager.project "tokyo-rain-123"
|
112
|
-
# project.update do |p|
|
113
|
-
# p.labels = { "env" => "production" }
|
114
|
-
# end
|
115
|
-
#
|
116
|
-
def labels= new_labels
|
117
|
-
gapi.labels = new_labels
|
118
|
-
end
|
119
|
-
|
120
|
-
##
|
121
|
-
# @private Create an Updater object.
|
122
|
-
def self.from_project project
|
123
|
-
dupe_gapi = project.gapi.class.new project.gapi.to_h
|
124
|
-
dupe_project = Project.from_gapi dupe_gapi, nil # no way to update
|
125
|
-
Updater.new dupe_project
|
126
|
-
end
|
127
|
-
end
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
@@ -1,127 +0,0 @@
|
|
1
|
-
# Copyright 2016 Google Inc. All rights reserved.
|
2
|
-
#
|
3
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
-
# you may not use this file except in compliance with the License.
|
5
|
-
# You may obtain a copy of the License at
|
6
|
-
#
|
7
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
8
|
-
#
|
9
|
-
# Unless required by applicable law or agreed to in writing, software
|
10
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
-
# See the License for the specific language governing permissions and
|
13
|
-
# limitations under the License.
|
14
|
-
|
15
|
-
|
16
|
-
require "gcloud/version"
|
17
|
-
require "gcloud/errors"
|
18
|
-
require "google/apis/cloudresourcemanager_v1"
|
19
|
-
|
20
|
-
module Gcloud
|
21
|
-
module ResourceManager
|
22
|
-
##
|
23
|
-
# @private
|
24
|
-
# Represents the service to Resource Manager, as well as expose the API
|
25
|
-
# calls.
|
26
|
-
class Service
|
27
|
-
##
|
28
|
-
# Alias to the Google Client API module
|
29
|
-
API = Google::Apis::CloudresourcemanagerV1
|
30
|
-
|
31
|
-
attr_accessor :credentials
|
32
|
-
|
33
|
-
##
|
34
|
-
# Creates a new Service instance.
|
35
|
-
def initialize credentials, retries: nil, timeout: nil
|
36
|
-
@credentials = credentials
|
37
|
-
@service = API::CloudResourceManagerService.new
|
38
|
-
@service.client_options.application_name = "gcloud-ruby"
|
39
|
-
@service.client_options.application_version = Gcloud::VERSION
|
40
|
-
@service.request_options.retries = retries || 3
|
41
|
-
@service.request_options.timeout_sec = timeout if timeout
|
42
|
-
@service.authorization = @credentials.client
|
43
|
-
end
|
44
|
-
|
45
|
-
def service
|
46
|
-
return mocked_service if mocked_service
|
47
|
-
@service
|
48
|
-
end
|
49
|
-
attr_accessor :mocked_service
|
50
|
-
|
51
|
-
##
|
52
|
-
# Returns API::ListProjectsResponse
|
53
|
-
def list_project filter: nil, token: nil, max: nil
|
54
|
-
service.list_projects page_token: token, page_size: max, filter: filter
|
55
|
-
end
|
56
|
-
|
57
|
-
##
|
58
|
-
# Returns API::Project
|
59
|
-
def get_project project_id
|
60
|
-
service.get_project project_id
|
61
|
-
rescue Google::Apis::Error => e
|
62
|
-
raise Gcloud::Error.from_error(e)
|
63
|
-
end
|
64
|
-
|
65
|
-
##
|
66
|
-
# Returns API::Project
|
67
|
-
def create_project project_id, name, labels
|
68
|
-
project_attrs = { projectId: project_id, name: name,
|
69
|
-
labels: labels }.delete_if { |_, v| v.nil? }
|
70
|
-
service.create_project API::Project.new(project_attrs)
|
71
|
-
rescue Google::Apis::Error => e
|
72
|
-
raise Gcloud::Error.from_error(e)
|
73
|
-
end
|
74
|
-
|
75
|
-
##
|
76
|
-
# Updated the project, given a API::Project.
|
77
|
-
# Returns API::Project
|
78
|
-
def update_project project_gapi
|
79
|
-
service.update_project project_gapi.project_id, project_gapi
|
80
|
-
rescue Google::Apis::Error => e
|
81
|
-
raise Gcloud::Error.from_error(e)
|
82
|
-
end
|
83
|
-
|
84
|
-
def delete_project project_id
|
85
|
-
service.delete_project project_id
|
86
|
-
rescue Google::Apis::Error => e
|
87
|
-
raise Gcloud::Error.from_error(e)
|
88
|
-
end
|
89
|
-
|
90
|
-
def undelete_project project_id
|
91
|
-
service.undelete_project project_id
|
92
|
-
rescue Google::Apis::Error => e
|
93
|
-
raise Gcloud::Error.from_error(e)
|
94
|
-
end
|
95
|
-
|
96
|
-
##
|
97
|
-
# Returns API::Policy
|
98
|
-
def get_policy project_id
|
99
|
-
service.get_project_iam_policy "projects/#{project_id}"
|
100
|
-
rescue Google::Apis::Error => e
|
101
|
-
raise Gcloud::Error.from_error(e)
|
102
|
-
end
|
103
|
-
|
104
|
-
##
|
105
|
-
# Returns API::Policy
|
106
|
-
def set_policy project_id, new_policy
|
107
|
-
req = API::SetIamPolicyRequest.new policy: new_policy
|
108
|
-
service.set_project_iam_policy "projects/#{project_id}", req
|
109
|
-
rescue Google::Apis::Error => e
|
110
|
-
raise Gcloud::Error.from_error(e)
|
111
|
-
end
|
112
|
-
|
113
|
-
##
|
114
|
-
# Returns API::TestIamPermissionsResponse
|
115
|
-
def test_permissions project_id, permissions
|
116
|
-
req = API::TestIamPermissionsRequest.new permissions: permissions
|
117
|
-
service.test_project_iam_permissions "projects/#{project_id}", req
|
118
|
-
rescue Google::Apis::Error => e
|
119
|
-
raise Gcloud::Error.from_error(e)
|
120
|
-
end
|
121
|
-
|
122
|
-
def inspect
|
123
|
-
"#{self.class}"
|
124
|
-
end
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
@@ -1,775 +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 "gcloud/storage/bucket/acl"
|
17
|
-
require "gcloud/storage/bucket/list"
|
18
|
-
require "gcloud/storage/bucket/cors"
|
19
|
-
require "gcloud/storage/file"
|
20
|
-
require "pathname"
|
21
|
-
|
22
|
-
module Gcloud
|
23
|
-
module Storage
|
24
|
-
##
|
25
|
-
# # Bucket
|
26
|
-
#
|
27
|
-
# Represents a Storage bucket. Belongs to a Project and has many Files.
|
28
|
-
#
|
29
|
-
# @example
|
30
|
-
# require "gcloud"
|
31
|
-
#
|
32
|
-
# gcloud = Gcloud.new
|
33
|
-
# storage = gcloud.storage
|
34
|
-
#
|
35
|
-
# bucket = storage.bucket "my-bucket"
|
36
|
-
# file = bucket.file "path/to/my-file.ext"
|
37
|
-
#
|
38
|
-
class Bucket
|
39
|
-
##
|
40
|
-
# @private The Service object.
|
41
|
-
attr_accessor :service
|
42
|
-
|
43
|
-
##
|
44
|
-
# @private The Google API Client object.
|
45
|
-
attr_accessor :gapi
|
46
|
-
|
47
|
-
##
|
48
|
-
# @private Create an empty Bucket object.
|
49
|
-
def initialize
|
50
|
-
@service = nil
|
51
|
-
@gapi = Google::Apis::StorageV1::Bucket.new
|
52
|
-
end
|
53
|
-
|
54
|
-
##
|
55
|
-
# The kind of item this is.
|
56
|
-
# For buckets, this is always `storage#bucket`.
|
57
|
-
def kind
|
58
|
-
@gapi.kind
|
59
|
-
end
|
60
|
-
|
61
|
-
##
|
62
|
-
# The ID of the bucket.
|
63
|
-
def id
|
64
|
-
@gapi.id
|
65
|
-
end
|
66
|
-
|
67
|
-
##
|
68
|
-
# The name of the bucket.
|
69
|
-
def name
|
70
|
-
@gapi.name
|
71
|
-
end
|
72
|
-
|
73
|
-
##
|
74
|
-
# A URL that can be used to access the bucket using the REST API.
|
75
|
-
def api_url
|
76
|
-
@gapi.self_link
|
77
|
-
end
|
78
|
-
|
79
|
-
##
|
80
|
-
# Creation time of the bucket.
|
81
|
-
def created_at
|
82
|
-
@gapi.time_created
|
83
|
-
end
|
84
|
-
|
85
|
-
##
|
86
|
-
# Returns the current CORS configuration for a static website served from
|
87
|
-
# the bucket.
|
88
|
-
#
|
89
|
-
# The return value is a frozen (unmodifiable) array of hashes containing
|
90
|
-
# the attributes specified for the Bucket resource field
|
91
|
-
# [cors](https://cloud.google.com/storage/docs/json_api/v1/buckets#cors).
|
92
|
-
#
|
93
|
-
# This method also accepts a block for updating the bucket's CORS rules.
|
94
|
-
# See {Bucket::Cors} for details.
|
95
|
-
#
|
96
|
-
# @see https://cloud.google.com/storage/docs/cross-origin Cross-Origin
|
97
|
-
# Resource Sharing (CORS)
|
98
|
-
#
|
99
|
-
# @yield [cors] a block for setting CORS rules
|
100
|
-
# @yieldparam [Bucket::Cors] cors the object accepting CORS rules
|
101
|
-
#
|
102
|
-
# @example Retrieving the bucket's CORS rules.
|
103
|
-
# require "gcloud"
|
104
|
-
#
|
105
|
-
# gcloud = Gcloud.new
|
106
|
-
# storage = gcloud.storage
|
107
|
-
#
|
108
|
-
# bucket = storage.bucket "my-todo-app"
|
109
|
-
# bucket.cors #=> [{"origin"=>["http://example.org"],
|
110
|
-
# # "method"=>["GET","POST","DELETE"],
|
111
|
-
# # "responseHeader"=>["X-My-Custom-Header"],
|
112
|
-
# # "maxAgeSeconds"=>3600}]
|
113
|
-
#
|
114
|
-
# @example Updating the bucket's CORS rules inside a block.
|
115
|
-
# require "gcloud"
|
116
|
-
#
|
117
|
-
# gcloud = Gcloud.new
|
118
|
-
# storage = gcloud.storage
|
119
|
-
# bucket = storage.bucket "my-todo-app"
|
120
|
-
#
|
121
|
-
# bucket.update do |b|
|
122
|
-
# b.cors do |c|
|
123
|
-
# c.add_rule ["http://example.org", "https://example.org"],
|
124
|
-
# "*",
|
125
|
-
# response_headers: ["X-My-Custom-Header"],
|
126
|
-
# max_age: 3600
|
127
|
-
# end
|
128
|
-
# end
|
129
|
-
#
|
130
|
-
def cors
|
131
|
-
cors_builder = Bucket::Cors.from_gapi @gapi.cors_configurations
|
132
|
-
if block_given?
|
133
|
-
yield cors_builder
|
134
|
-
if cors_builder.changed?
|
135
|
-
@gapi.cors_configurations = cors_builder.to_gapi
|
136
|
-
patch_gapi! :cors_configurations
|
137
|
-
end
|
138
|
-
end
|
139
|
-
cors_builder.freeze # always return frozen objects
|
140
|
-
end
|
141
|
-
|
142
|
-
##
|
143
|
-
# The location of the bucket.
|
144
|
-
# Object data for objects in the bucket resides in physical
|
145
|
-
# storage within this region. Defaults to US.
|
146
|
-
# See the developer's guide for the authoritative list.
|
147
|
-
#
|
148
|
-
# @see https://cloud.google.com/storage/docs/concepts-techniques
|
149
|
-
def location
|
150
|
-
@gapi.location
|
151
|
-
end
|
152
|
-
|
153
|
-
##
|
154
|
-
# The destination bucket name for the bucket's logs.
|
155
|
-
#
|
156
|
-
# @see https://cloud.google.com/storage/docs/access-logs Access Logs
|
157
|
-
#
|
158
|
-
def logging_bucket
|
159
|
-
@gapi.logging.log_bucket if @gapi.logging
|
160
|
-
end
|
161
|
-
|
162
|
-
##
|
163
|
-
# Updates the destination bucket for the bucket's logs.
|
164
|
-
#
|
165
|
-
# @see https://cloud.google.com/storage/docs/access-logs Access Logs
|
166
|
-
#
|
167
|
-
# @param [String] logging_bucket The bucket to hold the logging output
|
168
|
-
#
|
169
|
-
def logging_bucket= logging_bucket
|
170
|
-
@gapi.logging ||= Google::Apis::StorageV1::Bucket::Logging.new
|
171
|
-
@gapi.logging.log_bucket = logging_bucket
|
172
|
-
patch_gapi! :logging
|
173
|
-
end
|
174
|
-
|
175
|
-
##
|
176
|
-
# The logging object prefix for the bucket's logs. For more information,
|
177
|
-
#
|
178
|
-
# @see https://cloud.google.com/storage/docs/access-logs Access Logs
|
179
|
-
#
|
180
|
-
def logging_prefix
|
181
|
-
@gapi.logging.log_object_prefix if @gapi.logging
|
182
|
-
end
|
183
|
-
|
184
|
-
##
|
185
|
-
# Updates the logging object prefix. This prefix will be used to create
|
186
|
-
# log object names for the bucket. It can be at most 900 characters and
|
187
|
-
# must be a [valid object
|
188
|
-
# name](https://cloud.google.com/storage/docs/bucket-naming#objectnames).
|
189
|
-
# By default, the object prefix is the name
|
190
|
-
# of the bucket for which the logs are enabled.
|
191
|
-
#
|
192
|
-
# @see https://cloud.google.com/storage/docs/access-logs Access Logs
|
193
|
-
#
|
194
|
-
def logging_prefix= logging_prefix
|
195
|
-
@gapi.logging ||= Google::Apis::StorageV1::Bucket::Logging.new
|
196
|
-
@gapi.logging.log_object_prefix = logging_prefix
|
197
|
-
patch_gapi! :logging
|
198
|
-
end
|
199
|
-
|
200
|
-
##
|
201
|
-
# The bucket's storage class. This defines how objects in the bucket are
|
202
|
-
# stored and determines the SLA and the cost of storage. Values include
|
203
|
-
# `STANDARD`, `NEARLINE`, and `DURABLE_REDUCED_AVAILABILITY`.
|
204
|
-
def storage_class
|
205
|
-
@gapi.storage_class
|
206
|
-
end
|
207
|
-
|
208
|
-
##
|
209
|
-
# Whether [Object
|
210
|
-
# Versioning](https://cloud.google.com/storage/docs/object-versioning) is
|
211
|
-
# enabled for the bucket.
|
212
|
-
def versioning?
|
213
|
-
@gapi.versioning.enabled? unless @gapi.versioning.nil?
|
214
|
-
end
|
215
|
-
|
216
|
-
##
|
217
|
-
# Updates whether [Object
|
218
|
-
# Versioning](https://cloud.google.com/storage/docs/object-versioning) is
|
219
|
-
# enabled for the bucket.
|
220
|
-
#
|
221
|
-
# @return [Boolean]
|
222
|
-
#
|
223
|
-
def versioning= new_versioning
|
224
|
-
@gapi.versioning ||= Google::Apis::StorageV1::Bucket::Versioning.new
|
225
|
-
@gapi.versioning.enabled = new_versioning
|
226
|
-
patch_gapi! :versioning
|
227
|
-
end
|
228
|
-
|
229
|
-
##
|
230
|
-
# The index page returned from a static website served from the bucket
|
231
|
-
# when a site visitor requests the top level directory.
|
232
|
-
#
|
233
|
-
# @see https://cloud.google.com/storage/docs/website-configuration#step4
|
234
|
-
# How to Host a Static Website
|
235
|
-
#
|
236
|
-
def website_main
|
237
|
-
@gapi.website.main_page_suffix if @gapi.website
|
238
|
-
end
|
239
|
-
|
240
|
-
##
|
241
|
-
# Updates the index page returned from a static website served from the
|
242
|
-
# bucket when a site visitor requests the top level directory.
|
243
|
-
#
|
244
|
-
# @see https://cloud.google.com/storage/docs/website-configuration#step4
|
245
|
-
# How to Host a Static Website
|
246
|
-
#
|
247
|
-
def website_main= website_main
|
248
|
-
@gapi.website ||= Google::Apis::StorageV1::Bucket::Website.new
|
249
|
-
@gapi.website.main_page_suffix = website_main
|
250
|
-
patch_gapi! :website
|
251
|
-
end
|
252
|
-
|
253
|
-
##
|
254
|
-
# The page returned from a static website served from the bucket when a
|
255
|
-
# site visitor requests a resource that does not exist.
|
256
|
-
#
|
257
|
-
# @see https://cloud.google.com/storage/docs/website-configuration#step4
|
258
|
-
# How to Host a Static Website
|
259
|
-
#
|
260
|
-
def website_404
|
261
|
-
@gapi.website.not_found_page if @gapi.website
|
262
|
-
end
|
263
|
-
|
264
|
-
##
|
265
|
-
# Updates the page returned from a static website served from the bucket
|
266
|
-
# when a site visitor requests a resource that does not exist.
|
267
|
-
#
|
268
|
-
# @see https://cloud.google.com/storage/docs/website-configuration#step4
|
269
|
-
# How to Host a Static Website
|
270
|
-
#
|
271
|
-
def website_404= website_404
|
272
|
-
@gapi.website ||= Google::Apis::StorageV1::Bucket::Website.new
|
273
|
-
@gapi.website.not_found_page = website_404
|
274
|
-
patch_gapi! :website
|
275
|
-
end
|
276
|
-
|
277
|
-
##
|
278
|
-
# Updates the bucket with changes made in the given block in a single
|
279
|
-
# PATCH request. The following attributes may be set: {#cors},
|
280
|
-
# {#logging_bucket=}, {#logging_prefix=}, {#versioning=},
|
281
|
-
# {#website_main=}, and {#website_404=}. In addition, the #cors
|
282
|
-
# configuration accessible in the block is completely mutable and will be
|
283
|
-
# included in the request. (See {Bucket::Cors})
|
284
|
-
#
|
285
|
-
# @yield [bucket] a block yielding a delegate object for updating the file
|
286
|
-
#
|
287
|
-
# @example
|
288
|
-
# require "gcloud"
|
289
|
-
#
|
290
|
-
# gcloud = Gcloud.new
|
291
|
-
# storage = gcloud.storage
|
292
|
-
#
|
293
|
-
# bucket = storage.bucket "my-bucket"
|
294
|
-
# bucket.update do |b|
|
295
|
-
# b.website_main = "index.html"
|
296
|
-
# b.website_404 = "not_found.html"
|
297
|
-
# b.cors[0]["method"] = ["GET","POST","DELETE"]
|
298
|
-
# b.cors[1]["responseHeader"] << "X-Another-Custom-Header"
|
299
|
-
# end
|
300
|
-
#
|
301
|
-
# @example New CORS rules can also be added in a nested block:
|
302
|
-
# require "gcloud"
|
303
|
-
#
|
304
|
-
# gcloud = Gcloud.new
|
305
|
-
# storage = gcloud.storage
|
306
|
-
# bucket = storage.bucket "my-todo-app"
|
307
|
-
#
|
308
|
-
# bucket.update do |b|
|
309
|
-
# b.cors do |c|
|
310
|
-
# c.add_rule ["http://example.org", "https://example.org"],
|
311
|
-
# "*",
|
312
|
-
# response_headers: ["X-My-Custom-Header"],
|
313
|
-
# max_age: 300
|
314
|
-
# end
|
315
|
-
# end
|
316
|
-
#
|
317
|
-
def update
|
318
|
-
updater = Updater.new @gapi
|
319
|
-
yield updater
|
320
|
-
# Add check for mutable cors
|
321
|
-
updater.check_for_mutable_cors!
|
322
|
-
patch_gapi! updater.updates unless updater.updates.empty?
|
323
|
-
end
|
324
|
-
|
325
|
-
##
|
326
|
-
# Permanently deletes the bucket.
|
327
|
-
# The bucket must be empty before it can be deleted.
|
328
|
-
#
|
329
|
-
# The API call to delete the bucket may be retried under certain
|
330
|
-
# conditions. See {Gcloud#storage} to control this behavior.
|
331
|
-
#
|
332
|
-
# @return [Boolean] Returns `true` if the bucket was deleted.
|
333
|
-
#
|
334
|
-
# @example
|
335
|
-
# require "gcloud"
|
336
|
-
#
|
337
|
-
# gcloud = Gcloud.new
|
338
|
-
# storage = gcloud.storage
|
339
|
-
#
|
340
|
-
# bucket = storage.bucket "my-bucket"
|
341
|
-
# bucket.delete
|
342
|
-
#
|
343
|
-
def delete
|
344
|
-
ensure_service!
|
345
|
-
service.delete_bucket name
|
346
|
-
true
|
347
|
-
end
|
348
|
-
|
349
|
-
##
|
350
|
-
# Retrieves a list of files matching the criteria.
|
351
|
-
#
|
352
|
-
# @param [String] prefix Filter results to files whose names begin with
|
353
|
-
# this prefix.
|
354
|
-
# @param [String] delimiter Returns results in a directory-like mode.
|
355
|
-
# `items` will contain only objects whose names, aside from the
|
356
|
-
# `prefix`, do not contain `delimiter`. Objects whose names, aside from
|
357
|
-
# the `prefix`, contain `delimiter` will have their name, truncated
|
358
|
-
# after the `delimiter`, returned in `prefixes`. Duplicate `prefixes`
|
359
|
-
# are omitted.
|
360
|
-
# @param [String] token A previously-returned page token representing part
|
361
|
-
# of the larger set of results to view.
|
362
|
-
# @param [Integer] max Maximum number of items plus prefixes to return. As
|
363
|
-
# duplicate prefixes are omitted, fewer total results may be returned
|
364
|
-
# than requested. The default value of this parameter is 1,000 items.
|
365
|
-
# @param [Boolean] versions If `true`, lists all versions of an object as
|
366
|
-
# distinct results. The default is `false`. For more information, see
|
367
|
-
# [Object Versioning
|
368
|
-
# ](https://cloud.google.com/storage/docs/object-versioning).
|
369
|
-
#
|
370
|
-
# @return [Array<Gcloud::Storage::File>] (See
|
371
|
-
# {Gcloud::Storage::File::List})
|
372
|
-
#
|
373
|
-
# @example
|
374
|
-
# require "gcloud"
|
375
|
-
#
|
376
|
-
# gcloud = Gcloud.new
|
377
|
-
# storage = gcloud.storage
|
378
|
-
#
|
379
|
-
# bucket = storage.bucket "my-bucket"
|
380
|
-
# files = bucket.files
|
381
|
-
# files.each do |file|
|
382
|
-
# puts file.name
|
383
|
-
# end
|
384
|
-
#
|
385
|
-
# @example Retrieve all files: (See {File::List#all})
|
386
|
-
# require "gcloud"
|
387
|
-
#
|
388
|
-
# gcloud = Gcloud.new
|
389
|
-
# storage = gcloud.storage
|
390
|
-
#
|
391
|
-
# bucket = storage.bucket "my-bucket"
|
392
|
-
# files = bucket.files
|
393
|
-
# files.all do |file|
|
394
|
-
# puts file.name
|
395
|
-
# end
|
396
|
-
#
|
397
|
-
def files prefix: nil, delimiter: nil, token: nil, max: nil, versions: nil
|
398
|
-
ensure_service!
|
399
|
-
options = {
|
400
|
-
prefix: prefix,
|
401
|
-
delimiter: delimiter,
|
402
|
-
token: token,
|
403
|
-
max: max,
|
404
|
-
versions: versions
|
405
|
-
}
|
406
|
-
gapi = service.list_files name, options
|
407
|
-
File::List.from_gapi gapi, service, name, prefix, delimiter, max,
|
408
|
-
versions
|
409
|
-
end
|
410
|
-
alias_method :find_files, :files
|
411
|
-
|
412
|
-
##
|
413
|
-
# Retrieves a file matching the path.
|
414
|
-
#
|
415
|
-
# If a [customer-supplied encryption
|
416
|
-
# key](https://cloud.google.com/storage/docs/encryption#customer-supplied)
|
417
|
-
# was used with {#create_file}, the `encryption_key` and
|
418
|
-
# `encryption_key_sha256` options must be provided or else the file's
|
419
|
-
# CRC32C checksum and MD5 hash will not be returned.
|
420
|
-
#
|
421
|
-
# @param [String] path Name (path) of the file.
|
422
|
-
# @param [Integer] generation When present, selects a specific revision of
|
423
|
-
# this object. Default is the latest version.
|
424
|
-
# @param [String] encryption_key Optional. The customer-supplied, AES-256
|
425
|
-
# encryption key used to encrypt the file, if one was provided to
|
426
|
-
# {#create_file}. Must be provided if `encryption_key_sha256` is
|
427
|
-
# provided.
|
428
|
-
# @param [String] encryption_key_sha256 Optional. The SHA256 hash of the
|
429
|
-
# customer-supplied, AES-256 encryption key used to encrypt the file, if
|
430
|
-
# one was provided to {#create_file}. Must be provided if
|
431
|
-
# `encryption_key` is provided.
|
432
|
-
#
|
433
|
-
# @return [Gcloud::Storage::File, nil] Returns nil if file does not exist
|
434
|
-
#
|
435
|
-
# @example
|
436
|
-
# require "gcloud"
|
437
|
-
#
|
438
|
-
# gcloud = Gcloud.new
|
439
|
-
# storage = gcloud.storage
|
440
|
-
#
|
441
|
-
# bucket = storage.bucket "my-bucket"
|
442
|
-
#
|
443
|
-
# file = bucket.file "path/to/my-file.ext"
|
444
|
-
# puts file.name
|
445
|
-
#
|
446
|
-
def file path, generation: nil, encryption_key: nil,
|
447
|
-
encryption_key_sha256: nil
|
448
|
-
ensure_service!
|
449
|
-
options = { generation: generation, key: encryption_key,
|
450
|
-
key_sha256: encryption_key_sha256 }
|
451
|
-
gapi = service.get_file name, path, options
|
452
|
-
File.from_gapi gapi, service
|
453
|
-
rescue Gcloud::NotFoundError
|
454
|
-
nil
|
455
|
-
end
|
456
|
-
alias_method :find_file, :file
|
457
|
-
|
458
|
-
##
|
459
|
-
# Creates a new {File} object by providing a path to a local file to
|
460
|
-
# upload and the path to store it with in the bucket.
|
461
|
-
#
|
462
|
-
# #### Customer-supplied encryption keys
|
463
|
-
#
|
464
|
-
# By default, Google Cloud Storage manages server-side encryption keys on
|
465
|
-
# your behalf. However, a [customer-supplied encryption
|
466
|
-
# key](https://cloud.google.com/storage/docs/encryption#customer-supplied)
|
467
|
-
# can be provided with the `encryption_key` and `encryption_key_sha256`
|
468
|
-
# options. If given, the same key and SHA256 hash also must be provided to
|
469
|
-
# subsequently download or copy the file. If you use customer-supplied
|
470
|
-
# encryption keys, you must securely manage your keys and ensure that they
|
471
|
-
# are not lost. Also, please note that file metadata is not encrypted,
|
472
|
-
# with the exception of the CRC32C checksum and MD5 hash. The names of
|
473
|
-
# files and buckets are also not encrypted, and you can read or update the
|
474
|
-
# metadata of an encrypted file without providing the encryption key.
|
475
|
-
#
|
476
|
-
# @param [String] file Path of the file on the filesystem to upload.
|
477
|
-
# @param [String] path Path to store the file in Google Cloud Storage.
|
478
|
-
# @param [String] acl A predefined set of access controls to apply to this
|
479
|
-
# file.
|
480
|
-
#
|
481
|
-
# Acceptable values are:
|
482
|
-
#
|
483
|
-
# * `auth`, `auth_read`, `authenticated`, `authenticated_read`,
|
484
|
-
# `authenticatedRead` - File owner gets OWNER access, and
|
485
|
-
# allAuthenticatedUsers get READER access.
|
486
|
-
# * `owner_full`, `bucketOwnerFullControl` - File owner gets OWNER
|
487
|
-
# access, and project team owners get OWNER access.
|
488
|
-
# * `owner_read`, `bucketOwnerRead` - File owner gets OWNER access, and
|
489
|
-
# project team owners get READER access.
|
490
|
-
# * `private` - File owner gets OWNER access.
|
491
|
-
# * `project_private`, `projectPrivate` - File owner gets OWNER access,
|
492
|
-
# and project team members get access according to their roles.
|
493
|
-
# * `public`, `public_read`, `publicRead` - File owner gets OWNER
|
494
|
-
# access, and allUsers get READER access.
|
495
|
-
# @param [String] cache_control The
|
496
|
-
# [Cache-Control](https://tools.ietf.org/html/rfc7234#section-5.2)
|
497
|
-
# response header to be returned when the file is downloaded.
|
498
|
-
# @param [String] content_disposition The
|
499
|
-
# [Content-Disposition](https://tools.ietf.org/html/rfc6266)
|
500
|
-
# response header to be returned when the file is downloaded.
|
501
|
-
# @param [String] content_encoding The [Content-Encoding
|
502
|
-
# ](https://tools.ietf.org/html/rfc7231#section-3.1.2.2) response header
|
503
|
-
# to be returned when the file is downloaded.
|
504
|
-
# @param [String] content_language The
|
505
|
-
# [Content-Language](http://tools.ietf.org/html/bcp47) response
|
506
|
-
# header to be returned when the file is downloaded.
|
507
|
-
# @param [String] content_type The
|
508
|
-
# [Content-Type](https://tools.ietf.org/html/rfc2616#section-14.17)
|
509
|
-
# response header to be returned when the file is downloaded.
|
510
|
-
# @param [String] crc32c The CRC32c checksum of the file data, as
|
511
|
-
# described in [RFC 4960, Appendix
|
512
|
-
# B](http://tools.ietf.org/html/rfc4960#appendix-B).
|
513
|
-
# If provided, Cloud Storage will only create the file if the value
|
514
|
-
# matches the value calculated by the service. See
|
515
|
-
# [Validation](https://cloud.google.com/storage/docs/hashes-etags)
|
516
|
-
# for more information.
|
517
|
-
# @param [String] md5 The MD5 hash of the file data. If provided, Cloud
|
518
|
-
# Storage will only create the file if the value matches the value
|
519
|
-
# calculated by the service. See
|
520
|
-
# [Validation](https://cloud.google.com/storage/docs/hashes-etags) for
|
521
|
-
# more information.
|
522
|
-
# @param [Hash] metadata A hash of custom, user-provided web-safe keys and
|
523
|
-
# arbitrary string values that will returned with requests for the file
|
524
|
-
# as "x-goog-meta-" response headers.
|
525
|
-
# @param [String] encryption_key Optional. A customer-supplied, AES-256
|
526
|
-
# encryption key that will be used to encrypt the file. Must be provided
|
527
|
-
# if `encryption_key_sha256` is provided.
|
528
|
-
# @param [String] encryption_key_sha256 Optional. The SHA256 hash of the
|
529
|
-
# customer-supplied, AES-256 encryption key that will be used to encrypt
|
530
|
-
# the file. Must be provided if `encryption_key` is provided.
|
531
|
-
#
|
532
|
-
# @return [Gcloud::Storage::File]
|
533
|
-
#
|
534
|
-
# @example
|
535
|
-
# require "gcloud"
|
536
|
-
#
|
537
|
-
# gcloud = Gcloud.new
|
538
|
-
# storage = gcloud.storage
|
539
|
-
#
|
540
|
-
# bucket = storage.bucket "my-bucket"
|
541
|
-
#
|
542
|
-
# bucket.create_file "path/to/local.file.ext"
|
543
|
-
#
|
544
|
-
# @example Specifying a destination path:
|
545
|
-
# require "gcloud"
|
546
|
-
#
|
547
|
-
# gcloud = Gcloud.new
|
548
|
-
# storage = gcloud.storage
|
549
|
-
#
|
550
|
-
# bucket = storage.bucket "my-bucket"
|
551
|
-
#
|
552
|
-
# bucket.create_file "path/to/local.file.ext",
|
553
|
-
# "destination/path/file.ext"
|
554
|
-
#
|
555
|
-
# @example Providing a customer-supplied encryption key:
|
556
|
-
# require "gcloud"
|
557
|
-
# require "digest/sha2"
|
558
|
-
#
|
559
|
-
# gcloud = Gcloud.new
|
560
|
-
# storage = gcloud.storage
|
561
|
-
# bucket = storage.bucket "my-bucket"
|
562
|
-
#
|
563
|
-
# # Key generation shown for example purposes only. Write your own.
|
564
|
-
# cipher = OpenSSL::Cipher.new "aes-256-cfb"
|
565
|
-
# cipher.encrypt
|
566
|
-
# key = cipher.random_key
|
567
|
-
# key_hash = Digest::SHA256.digest key
|
568
|
-
#
|
569
|
-
# bucket.create_file "path/to/local.file.ext",
|
570
|
-
# "destination/path/file.ext",
|
571
|
-
# encryption_key: key,
|
572
|
-
# encryption_key_sha256: key_hash
|
573
|
-
#
|
574
|
-
# # Store your key and hash securely for later use.
|
575
|
-
# file = bucket.file "destination/path/file.ext",
|
576
|
-
# encryption_key: key,
|
577
|
-
# encryption_key_sha256: key_hash
|
578
|
-
#
|
579
|
-
def create_file file, path = nil, acl: nil, cache_control: nil,
|
580
|
-
content_disposition: nil, content_encoding: nil,
|
581
|
-
content_language: nil, content_type: nil,
|
582
|
-
crc32c: nil, md5: nil, metadata: nil, encryption_key: nil,
|
583
|
-
encryption_key_sha256: nil
|
584
|
-
ensure_service!
|
585
|
-
options = { acl: File::Acl.predefined_rule_for(acl), md5: md5,
|
586
|
-
cache_control: cache_control, content_type: content_type,
|
587
|
-
content_disposition: content_disposition, crc32c: crc32c,
|
588
|
-
content_encoding: content_encoding,
|
589
|
-
content_language: content_language, metadata: metadata,
|
590
|
-
key: encryption_key, key_sha256: encryption_key_sha256 }
|
591
|
-
ensure_file_exists! file
|
592
|
-
# TODO: Handle file as an IO and path is missing more gracefully
|
593
|
-
path ||= Pathname(file).to_path
|
594
|
-
gapi = service.insert_file name, file, path, options
|
595
|
-
File.from_gapi gapi, service
|
596
|
-
end
|
597
|
-
alias_method :upload_file, :create_file
|
598
|
-
alias_method :new_file, :create_file
|
599
|
-
|
600
|
-
##
|
601
|
-
# The Bucket::Acl instance used to control access to the bucket.
|
602
|
-
#
|
603
|
-
# A bucket has owners, writers, and readers. Permissions can be granted to
|
604
|
-
# an individual user's email address, a group's email address, as well as
|
605
|
-
# many predefined lists.
|
606
|
-
#
|
607
|
-
# @see https://cloud.google.com/storage/docs/access-control Access Control
|
608
|
-
# guide
|
609
|
-
#
|
610
|
-
# @example Grant access to a user by pre-pending `"user-"` to an email:
|
611
|
-
# require "gcloud"
|
612
|
-
#
|
613
|
-
# gcloud = Gcloud.new
|
614
|
-
# storage = gcloud.storage
|
615
|
-
#
|
616
|
-
# bucket = storage.bucket "my-todo-app"
|
617
|
-
#
|
618
|
-
# email = "heidi@example.net"
|
619
|
-
# bucket.acl.add_reader "user-#{email}"
|
620
|
-
#
|
621
|
-
# @example Grant access to a group by pre-pending `"group-"` to an email:
|
622
|
-
# require "gcloud"
|
623
|
-
#
|
624
|
-
# gcloud = Gcloud.new
|
625
|
-
# storage = gcloud.storage
|
626
|
-
#
|
627
|
-
# bucket = storage.bucket "my-todo-app"
|
628
|
-
#
|
629
|
-
# email = "authors@example.net"
|
630
|
-
# bucket.acl.add_reader "group-#{email}"
|
631
|
-
#
|
632
|
-
# @example Or, grant access via a predefined permissions list:
|
633
|
-
# require "gcloud"
|
634
|
-
#
|
635
|
-
# gcloud = Gcloud.new
|
636
|
-
# storage = gcloud.storage
|
637
|
-
#
|
638
|
-
# bucket = storage.bucket "my-todo-app"
|
639
|
-
#
|
640
|
-
# bucket.acl.public!
|
641
|
-
#
|
642
|
-
def acl
|
643
|
-
@acl ||= Bucket::Acl.new self
|
644
|
-
end
|
645
|
-
|
646
|
-
##
|
647
|
-
# The Bucket::DefaultAcl instance used to control access to the bucket's
|
648
|
-
# files.
|
649
|
-
#
|
650
|
-
# A bucket's files have owners, writers, and readers. Permissions can be
|
651
|
-
# granted to an individual user's email address, a group's email address,
|
652
|
-
# as well as many predefined lists.
|
653
|
-
#
|
654
|
-
# @see https://cloud.google.com/storage/docs/access-control Access Control
|
655
|
-
# guide
|
656
|
-
#
|
657
|
-
# @example Grant access to a user by pre-pending `"user-"` to an email:
|
658
|
-
# require "gcloud"
|
659
|
-
#
|
660
|
-
# gcloud = Gcloud.new
|
661
|
-
# storage = gcloud.storage
|
662
|
-
#
|
663
|
-
# bucket = storage.bucket "my-todo-app"
|
664
|
-
#
|
665
|
-
# email = "heidi@example.net"
|
666
|
-
# bucket.default_acl.add_reader "user-#{email}"
|
667
|
-
#
|
668
|
-
# @example Grant access to a group by pre-pending `"group-"` to an email
|
669
|
-
# require "gcloud"
|
670
|
-
#
|
671
|
-
# gcloud = Gcloud.new
|
672
|
-
# storage = gcloud.storage
|
673
|
-
#
|
674
|
-
# bucket = storage.bucket "my-todo-app"
|
675
|
-
#
|
676
|
-
# email = "authors@example.net"
|
677
|
-
# bucket.default_acl.add_reader "group-#{email}"
|
678
|
-
#
|
679
|
-
# @example Or, grant access via a predefined permissions list:
|
680
|
-
# require "gcloud"
|
681
|
-
#
|
682
|
-
# gcloud = Gcloud.new
|
683
|
-
# storage = gcloud.storage
|
684
|
-
#
|
685
|
-
# bucket = storage.bucket "my-todo-app"
|
686
|
-
#
|
687
|
-
# bucket.default_acl.public!
|
688
|
-
#
|
689
|
-
def default_acl
|
690
|
-
@default_acl ||= Bucket::DefaultAcl.new self
|
691
|
-
end
|
692
|
-
|
693
|
-
##
|
694
|
-
# Reloads the bucket with current data from the Storage service.
|
695
|
-
def reload!
|
696
|
-
ensure_service!
|
697
|
-
@gapi = service.get_bucket name
|
698
|
-
end
|
699
|
-
alias_method :refresh!, :reload!
|
700
|
-
|
701
|
-
##
|
702
|
-
# @private New Bucket from a Google API Client object.
|
703
|
-
def self.from_gapi gapi, conn
|
704
|
-
new.tap do |f|
|
705
|
-
f.gapi = gapi
|
706
|
-
f.service = conn
|
707
|
-
end
|
708
|
-
end
|
709
|
-
|
710
|
-
protected
|
711
|
-
|
712
|
-
##
|
713
|
-
# Raise an error unless an active service is available.
|
714
|
-
def ensure_service!
|
715
|
-
fail "Must have active connection" unless service
|
716
|
-
end
|
717
|
-
|
718
|
-
def patch_gapi! *attributes
|
719
|
-
attributes.flatten!
|
720
|
-
return if attributes.empty?
|
721
|
-
ensure_service!
|
722
|
-
patch_args = Hash[attributes.map do |attr|
|
723
|
-
[attr, @gapi.send(attr)]
|
724
|
-
end]
|
725
|
-
patch_gapi = Google::Apis::StorageV1::Bucket.new patch_args
|
726
|
-
@gapi = service.patch_bucket name, patch_gapi
|
727
|
-
end
|
728
|
-
|
729
|
-
##
|
730
|
-
# Raise an error if the file is not found.
|
731
|
-
def ensure_file_exists! file
|
732
|
-
return if ::File.file? file
|
733
|
-
fail ArgumentError, "cannot find file #{file}"
|
734
|
-
end
|
735
|
-
|
736
|
-
##
|
737
|
-
# Yielded to a block to accumulate changes for a patch request.
|
738
|
-
class Updater < Bucket
|
739
|
-
attr_reader :updates
|
740
|
-
##
|
741
|
-
# Create an Updater object.
|
742
|
-
def initialize gapi
|
743
|
-
@updates = []
|
744
|
-
@gapi = gapi
|
745
|
-
@cors_builder = nil
|
746
|
-
end
|
747
|
-
|
748
|
-
def cors
|
749
|
-
# Same as Bucket#cors, but not frozen
|
750
|
-
@cors_builder ||= Bucket::Cors.from_gapi @gapi.cors_configurations
|
751
|
-
yield @cors_builder if block_given?
|
752
|
-
@cors_builder
|
753
|
-
end
|
754
|
-
|
755
|
-
##
|
756
|
-
# @private Make sure any cors changes are saved
|
757
|
-
def check_for_mutable_cors!
|
758
|
-
return if @cors_builder.nil?
|
759
|
-
return unless @cors_builder.changed?
|
760
|
-
@gapi.cors_configurations = @cors_builder.to_gapi
|
761
|
-
patch_gapi! :cors_configurations
|
762
|
-
end
|
763
|
-
|
764
|
-
protected
|
765
|
-
|
766
|
-
##
|
767
|
-
# Queue up all the updates instead of making them.
|
768
|
-
def patch_gapi! attribute
|
769
|
-
@updates << attribute
|
770
|
-
@updates.uniq!
|
771
|
-
end
|
772
|
-
end
|
773
|
-
end
|
774
|
-
end
|
775
|
-
end
|