gcloud 0.4.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +8 -8
  2. data/CHANGELOG.md +15 -0
  3. data/OVERVIEW.md +38 -5
  4. data/lib/gcloud.rb +55 -4
  5. data/lib/gcloud/bigquery/data.rb +2 -0
  6. data/lib/gcloud/bigquery/dataset.rb +1 -1
  7. data/lib/gcloud/bigquery/dataset/list.rb +2 -0
  8. data/lib/gcloud/bigquery/job/list.rb +2 -0
  9. data/lib/gcloud/bigquery/project.rb +2 -9
  10. data/lib/gcloud/bigquery/table/list.rb +2 -0
  11. data/lib/gcloud/datastore.rb +23 -28
  12. data/lib/gcloud/datastore/connection.rb +3 -1
  13. data/lib/gcloud/datastore/dataset.rb +167 -22
  14. data/lib/gcloud/datastore/dataset/lookup_results.rb +2 -0
  15. data/lib/gcloud/datastore/dataset/query_results.rb +2 -0
  16. data/lib/gcloud/datastore/entity.rb +11 -11
  17. data/lib/gcloud/datastore/key.rb +33 -16
  18. data/lib/gcloud/dns/change/list.rb +2 -0
  19. data/lib/gcloud/dns/project.rb +1 -1
  20. data/lib/gcloud/dns/record/list.rb +2 -0
  21. data/lib/gcloud/dns/zone.rb +2 -2
  22. data/lib/gcloud/dns/zone/list.rb +2 -0
  23. data/lib/gcloud/gce.rb +0 -5
  24. data/lib/gcloud/pubsub.rb +65 -62
  25. data/lib/gcloud/pubsub/connection.rb +20 -2
  26. data/lib/gcloud/pubsub/project.rb +233 -72
  27. data/lib/gcloud/pubsub/subscription.rb +45 -13
  28. data/lib/gcloud/pubsub/subscription/list.rb +2 -0
  29. data/lib/gcloud/pubsub/topic.rb +66 -85
  30. data/lib/gcloud/pubsub/topic/list.rb +2 -0
  31. data/lib/gcloud/resource_manager.rb +244 -0
  32. data/lib/gcloud/resource_manager/connection.rb +124 -0
  33. data/lib/gcloud/resource_manager/credentials.rb +30 -0
  34. data/lib/gcloud/resource_manager/errors.rb +64 -0
  35. data/lib/gcloud/resource_manager/manager.rb +319 -0
  36. data/lib/gcloud/resource_manager/project.rb +529 -0
  37. data/lib/gcloud/resource_manager/project/list.rb +91 -0
  38. data/lib/gcloud/resource_manager/project/updater.rb +137 -0
  39. data/lib/gcloud/storage/bucket.rb +1 -1
  40. data/lib/gcloud/storage/bucket/cors.rb +2 -0
  41. data/lib/gcloud/storage/bucket/list.rb +2 -0
  42. data/lib/gcloud/storage/file/list.rb +2 -0
  43. data/lib/gcloud/storage/project.rb +1 -1
  44. data/lib/gcloud/version.rb +1 -1
  45. metadata +10 -2
@@ -0,0 +1,124 @@
1
+ #--
2
+ # Copyright 2015 Google Inc. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require "gcloud/version"
17
+ require "google/api_client"
18
+
19
+ module Gcloud
20
+ module ResourceManager
21
+ ##
22
+ # Represents the connection to Resource Manager, as well as expose the API
23
+ # calls.
24
+ class Connection #:nodoc:
25
+ API_VERSION = "v1beta1"
26
+
27
+ attr_accessor :credentials #:nodoc:
28
+
29
+ ##
30
+ # Creates a new Connection instance.
31
+ def initialize credentials #:nodoc:
32
+ @credentials = credentials
33
+ @client = Google::APIClient.new application_name: "gcloud-ruby",
34
+ application_version: Gcloud::VERSION
35
+ @client.authorization = @credentials.client
36
+ @res_man = @client.discovered_api "cloudresourcemanager", API_VERSION
37
+ end
38
+
39
+ def list_project options = {}
40
+ params = { filter: options.delete(:filter),
41
+ pageToken: options.delete(:token),
42
+ maxResults: options.delete(:max)
43
+ }.delete_if { |_, v| v.nil? }
44
+
45
+ @client.execute(
46
+ api_method: @res_man.projects.list,
47
+ parameters: params
48
+ )
49
+ end
50
+
51
+ def get_project project_id
52
+ @client.execute(
53
+ api_method: @res_man.projects.get,
54
+ parameters: { projectId: project_id }
55
+ )
56
+ end
57
+
58
+ def create_project project_id, name, labels
59
+ project_gapi = { projectId: project_id, name: name,
60
+ labels: labels }.delete_if { |_, v| v.nil? }
61
+
62
+ @client.execute(
63
+ api_method: @res_man.projects.create,
64
+ body_object: project_gapi
65
+ )
66
+ end
67
+
68
+ ##
69
+ # Updated the project, given the project Google API Client object/hash.
70
+ # We try not to pass the gapi objects, but there is no PATCH, so we need
71
+ # to pass in a complete Project object.
72
+ def update_project project_gapi
73
+ project_id = project_gapi["projectId"]
74
+
75
+ @client.execute(
76
+ api_method: @res_man.projects.update,
77
+ parameters: { projectId: project_id },
78
+ body_object: project_gapi
79
+ )
80
+ end
81
+
82
+ def delete_project project_id
83
+ @client.execute(
84
+ api_method: @res_man.projects.delete,
85
+ parameters: { projectId: project_id }
86
+ )
87
+ end
88
+
89
+ def undelete_project project_id
90
+ @client.execute(
91
+ api_method: @res_man.projects.undelete,
92
+ parameters: { projectId: project_id }
93
+ )
94
+ end
95
+
96
+ def get_policy project_id
97
+ @client.execute(
98
+ api_method: @res_man.projects.get_iam_policy,
99
+ parameters: { resource: project_id }
100
+ )
101
+ end
102
+
103
+ def set_policy project_id, new_policy
104
+ @client.execute(
105
+ api_method: @res_man.projects.set_iam_policy,
106
+ parameters: { resource: project_id },
107
+ body_object: { policy: new_policy }
108
+ )
109
+ end
110
+
111
+ def test_permissions project_id, permissions
112
+ @client.execute(
113
+ api_method: @res_man.projects.test_iam_permissions,
114
+ parameters: { resource: project_id },
115
+ body_object: { permissions: permissions }
116
+ )
117
+ end
118
+
119
+ def inspect #:nodoc:
120
+ "#{self.class}(#{@project})"
121
+ end
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,30 @@
1
+ #--
2
+ # Copyright 2015 Google Inc. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require "gcloud/credentials"
17
+
18
+ module Gcloud
19
+ module ResourceManager
20
+ ##
21
+ # Represents the Oauth2 signing logic for Resource Manager.
22
+ class Credentials < Gcloud::Credentials #:nodoc:
23
+ SCOPE = ["https://www.googleapis.com/auth/cloud-platform"]
24
+ PATH_ENV_VARS = %w(RESOURCE_MANAGER_KEYFILE
25
+ GCLOUD_KEYFILE GOOGLE_CLOUD_KEYFILE)
26
+ JSON_ENV_VARS = %w(RESOURCE_MANAGER_KEYFILE_JSON GCLOUD_KEYFILE_JSON
27
+ GOOGLE_CLOUD_KEYFILE_JSON)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,64 @@
1
+ #--
2
+ # Copyright 2015 Google Inc. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
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
+ def initialize message, code, errors = [] #:nodoc:
37
+ super message
38
+ @code = code
39
+ @errors = errors
40
+ end
41
+
42
+ def self.from_response resp #:nodoc:
43
+ if resp.data? && resp.data["error"]
44
+ from_response_data resp.data["error"]
45
+ else
46
+ from_response_status resp
47
+ end
48
+ end
49
+
50
+ def self.from_response_data error #:nodoc:
51
+ new error["message"], error["code"], error["errors"]
52
+ end
53
+
54
+ def self.from_response_status resp #:nodoc:
55
+ if resp.status == 404
56
+ new "#{resp.error_message}: #{resp.request.uri.request_uri}",
57
+ resp.status
58
+ else
59
+ new resp.error_message, resp.status
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,319 @@
1
+ #--
2
+ # Copyright 2015 Google Inc. All rights reserved.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require "gcloud/resource_manager/credentials"
17
+ require "gcloud/resource_manager/connection"
18
+ require "gcloud/resource_manager/errors"
19
+ require "gcloud/resource_manager/project"
20
+
21
+ module Gcloud
22
+ module ResourceManager
23
+ ##
24
+ # = Manager
25
+ #
26
+ # Provides methods for creating, retrieving, and updating projects.
27
+ #
28
+ # require "gcloud"
29
+ #
30
+ # gcloud = Gcloud.new
31
+ # resource_manager = gcloud.resource_manager
32
+ # resource_manager.projects.each do |project|
33
+ # puts projects.project_id
34
+ # end
35
+ #
36
+ # See Gcloud#resource_manager
37
+ class Manager
38
+ ##
39
+ # The Connection object.
40
+ attr_accessor :connection #:nodoc:
41
+
42
+ ##
43
+ # Creates a new Connection instance.
44
+ #
45
+ # See Gcloud.resource_manager
46
+ def initialize credentials #:nodoc:
47
+ @connection = Connection.new credentials
48
+ end
49
+
50
+ ##
51
+ # Retrieves the projects that are visible to the user and satisfy the
52
+ # specified filter. This method returns projects in an unspecified order.
53
+ # New projects do not necessarily appear at the end of the list.
54
+ #
55
+ # === Parameters
56
+ #
57
+ # +options+::
58
+ # An optional Hash for controlling additional behavior. (+Hash+)
59
+ # <code>options[:filter]</code>::
60
+ # An expression for filtering the results of the request. Filter rules
61
+ # are case insensitive. (+String+)
62
+ #
63
+ # The fields eligible for filtering are:
64
+ # * +name+
65
+ # * +id+
66
+ # * +labels.key+ - where +key+ is the name of a label
67
+ #
68
+ # Some examples of using labels as filters:
69
+ # * +name:*+ - The project has a name.
70
+ # * +name:Howl+ - The project's name is Howl or howl.
71
+ # * +name:HOWL+ - Equivalent to above.
72
+ # * +NAME:howl+ - Equivalent to above.
73
+ # * +labels.color:*+ - The project has the label color.
74
+ # * +labels.color:red+ - The project's label color has the value red.
75
+ # * <code>labels.color:red labels.size:big</code> - The project's label
76
+ # color has the value red and its label size has the value big.
77
+ # <code>options[:token]</code>::
78
+ # A previously-returned page token representing part of the larger set
79
+ # of results to view. (+String+)
80
+ # <code>options[:max]</code>::
81
+ # Maximum number of projects to return. (+Integer+)
82
+ #
83
+ # === Returns
84
+ #
85
+ # Array of Gcloud::ResourceManager::Project
86
+ # (See Gcloud::ResourceManager::Project::List)
87
+ #
88
+ # === Examples
89
+ #
90
+ # require "gcloud"
91
+ #
92
+ # gcloud = Gcloud.new
93
+ # resource_manager = gcloud.resource_manager
94
+ # projects = resource_manager.projects
95
+ # projects.each do |project|
96
+ # puts project.project_id
97
+ # end
98
+ #
99
+ # Projects can be filtered using the +filter+ option:
100
+ #
101
+ # require "gcloud"
102
+ #
103
+ # gcloud = Gcloud.new
104
+ # resource_manager = gcloud.resource_manager
105
+ # projects = resource_manager.projects filter: "labels.env:production"
106
+ # projects.each do |project|
107
+ # puts project.project_id
108
+ # end
109
+ #
110
+ # If you have a significant number of projects, you may need to paginate
111
+ # through them: (See Gcloud::ResourceManager::Project::List)
112
+ #
113
+ # require "gcloud"
114
+ #
115
+ # gcloud = Gcloud.new
116
+ # resource_manager = gcloud.resource_manager
117
+ # projects = resource_manager.projects.all
118
+ # projects.each do |project|
119
+ # puts project.project_id
120
+ # end
121
+ #
122
+ def projects options = {}
123
+ resp = connection.list_project options
124
+ if resp.success?
125
+ Project::List.from_response resp, self
126
+ else
127
+ fail ApiError.from_response(resp)
128
+ end
129
+ end
130
+
131
+ ##
132
+ # Retrieves the project identified by the specified +project_id+.
133
+ #
134
+ # === Parameters
135
+ #
136
+ # +project_id+::
137
+ # The ID of the project. (+String+)
138
+ #
139
+ # === Returns
140
+ #
141
+ # Gcloud::ResourceManager::Project, or +nil+ if the project does not exist
142
+ #
143
+ # === Example
144
+ #
145
+ # require "gcloud"
146
+ #
147
+ # gcloud = Gcloud.new
148
+ # resource_manager = gcloud.resource_manager
149
+ # project = resource_manager.project "tokyo-rain-123"
150
+ # project.project_id #=> "tokyo-rain-123"
151
+ #
152
+ def project project_id
153
+ resp = connection.get_project project_id
154
+ if resp.success?
155
+ Project.from_gapi resp.data, connection
156
+ else
157
+ nil
158
+ end
159
+ end
160
+
161
+ ##
162
+ # Creates a project resource.
163
+ #
164
+ # Initially, the project resource is owned by its creator exclusively. The
165
+ # creator can later grant permission to others to read or update the
166
+ # project.
167
+ #
168
+ # Several APIs are activated automatically for the project, including
169
+ # Google Cloud Storage.
170
+ #
171
+ # === Parameters
172
+ #
173
+ # +project_id+::
174
+ # The unique, user-assigned ID of the project. It must be 6 to 30
175
+ # lowercase letters, digits, or hyphens. It must start with a letter.
176
+ # Trailing hyphens are prohibited. (+String+)
177
+ # +options+::
178
+ # An optional Hash for controlling additional behavior. (+Hash+)
179
+ # <code>options[:name]</code>::
180
+ # The user-assigned name of the project. This field is optional and can
181
+ # remain unset.
182
+ #
183
+ # Allowed characters are: lowercase and uppercase letters, numbers,
184
+ # hyphen, single-quote, double-quote, space, and exclamation point.
185
+ # (+String+)
186
+ # <code>options[:labels]</code>::
187
+ # The labels associated with this project.
188
+ #
189
+ # Label keys must be between 1 and 63 characters long and must conform
190
+ # to the following regular expression:
191
+ # <code>[a-z]([-a-z0-9]*[a-z0-9])?</code>.
192
+ #
193
+ # Label values must be between 0 and 63 characters long and must conform
194
+ # to the regular expression <code>([a-z]([-a-z0-9]*[a-z0-9])?)?</code>.
195
+ #
196
+ # No more than 256 labels can be associated with a given resource.
197
+ # (+Hash+)
198
+ #
199
+ # === Returns
200
+ #
201
+ # Gcloud::ResourceManager::Project
202
+ #
203
+ # === Example
204
+ #
205
+ # require "gcloud"
206
+ #
207
+ # gcloud = Gcloud.new
208
+ # resource_manager = gcloud.resource_manager
209
+ # project = resource_manager.create_project "tokyo-rain-123"
210
+ #
211
+ # A project can also be created with a +name+ and +labels+.
212
+ #
213
+ # require "gcloud"
214
+ #
215
+ # gcloud = Gcloud.new
216
+ # resource_manager = gcloud.resource_manager
217
+ # project = resource_manager.create_project "tokyo-rain-123",
218
+ # name: "Todos Development",
219
+ # labels: {env: :development}
220
+ #
221
+ def create_project project_id, options = {}
222
+ resp = connection.create_project project_id,
223
+ options[:name],
224
+ options[:labels]
225
+ if resp.success?
226
+ Project.from_gapi resp.data, connection
227
+ else
228
+ fail ApiError.from_response(resp)
229
+ end
230
+ end
231
+
232
+ ##
233
+ # Marks the project for deletion. This method will only affect the project
234
+ # if the following criteria are met:
235
+ #
236
+ # * The project does not have a billing account associated with it.
237
+ # * The project has a lifecycle state of +ACTIVE+.
238
+ # * This method changes the project's lifecycle state from +ACTIVE+ to
239
+ # +DELETE_REQUESTED+. The deletion starts at an unspecified time, at
240
+ # which point the lifecycle state changes to +DELETE_IN_PROGRESS+.
241
+ #
242
+ # Until the deletion completes, you can check the lifecycle state by
243
+ # retrieving the project with Manager#project. The project remains visible
244
+ # to Manager#project and Manager#projects, but cannot be updated.
245
+ #
246
+ # After the deletion completes, the project is not retrievable by the
247
+ # Manager#project and Manager#projects methods.
248
+ #
249
+ # The caller must have modify permissions for this project.
250
+ #
251
+ # === Parameters
252
+ #
253
+ # +project_id+::
254
+ # The ID of the project. (+String+)
255
+ #
256
+ # === Example
257
+ #
258
+ # require "gcloud"
259
+ #
260
+ # gcloud = Gcloud.new
261
+ # resource_manager = gcloud.resource_manager
262
+ # resource_manager.delete "tokyo-rain-123"
263
+ #
264
+ def delete project_id
265
+ resp = connection.delete_project project_id
266
+ if resp.success?
267
+ true
268
+ else
269
+ fail ApiError.from_response(resp)
270
+ end
271
+ end
272
+
273
+ ##
274
+ # Restores the project. You can only use this method for a project that
275
+ # has a lifecycle state of +DELETE_REQUESTED+. After deletion starts, as
276
+ # indicated by a lifecycle state of +DELETE_IN_PROGRESS+, the project
277
+ # cannot be restored.
278
+ #
279
+ # The caller must have modify permissions for this project.
280
+ #
281
+ # === Parameters
282
+ #
283
+ # +project_id+::
284
+ # The ID of the project. (+String+)
285
+ #
286
+ # === Example
287
+ #
288
+ # require "gcloud"
289
+ #
290
+ # gcloud = Gcloud.new
291
+ # resource_manager = gcloud.resource_manager
292
+ # resource_manager.undelete "tokyo-rain-123"
293
+ #
294
+ def undelete project_id
295
+ resp = connection.undelete_project project_id
296
+ if resp.success?
297
+ true
298
+ else
299
+ fail ApiError.from_response(resp)
300
+ end
301
+ end
302
+
303
+ protected
304
+
305
+ ##
306
+ # Create an options hash from the projects parameters.
307
+ def list_projects_options filter, options
308
+ # Handle only sending in options
309
+ if filter.is_a?(::Hash) && options.empty?
310
+ options = filter
311
+ filter = nil
312
+ end
313
+ # Give named parameter priority
314
+ options[:filter] = filter || options[:filter]
315
+ options
316
+ end
317
+ end
318
+ end
319
+ end