google-cloud-resource_manager 0.39.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,151 +0,0 @@
1
- # Copyright 2015 Google LLC
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
- # https://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
-
16
- require "delegate"
17
- require "time"
18
-
19
- module Google
20
- module Cloud
21
- module ResourceManager
22
- class Project
23
- ##
24
- # # Project Updater
25
- #
26
- # This object is used by Project#update when passed a block. These
27
- # methods are used to update the project data in a single API call.
28
- #
29
- # @example
30
- # require "google/cloud/resource_manager"
31
- #
32
- # resource_manager = Google::Cloud::ResourceManager.new
33
- # project = resource_manager.project "tokyo-rain-123"
34
- # project.update do |p|
35
- # p.name = "My Project"
36
- # p.labels["env"] = "production"
37
- # end
38
- #
39
- class Updater < DelegateClass(Project)
40
- ##
41
- # Updates the user-assigned name of the project. This field is
42
- # optional and can remain unset.
43
- #
44
- # Allowed characters are: lowercase and uppercase letters, numbers,
45
- # hyphen, single-quote, double-quote, space, and exclamation point.
46
- #
47
- # @example
48
- # require "google/cloud/resource_manager"
49
- #
50
- # resource_manager = Google::Cloud::ResourceManager.new
51
- # project = resource_manager.project "tokyo-rain-123"
52
- # project.update do |p|
53
- # p.name = "My Project"
54
- # end
55
- #
56
- def name= new_name
57
- gapi.name = new_name
58
- end
59
-
60
- ##
61
- # The labels associated with this project.
62
- #
63
- # Label keys must be between 1 and 63 characters long and must conform
64
- # to the following regular expression:
65
- # `[a-z]([-a-z0-9]*[a-z0-9])?`.
66
- #
67
- # Label values must be between 0 and 63 characters long and must
68
- # conform to the regular expression
69
- # `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
70
- #
71
- # No more than 256 labels can be associated with a given resource.
72
- # (`Hash`)
73
- #
74
- # @example
75
- # require "google/cloud/resource_manager"
76
- #
77
- # resource_manager = Google::Cloud::ResourceManager.new
78
- # project = resource_manager.project "tokyo-rain-123"
79
- # project.update do |p|
80
- # p.labels["env"] = "production"
81
- # end
82
- #
83
- def labels
84
- gapi.labels
85
- end
86
-
87
- ##
88
- # Updates the labels associated with this project.
89
- #
90
- # Label keys must be between 1 and 63 characters long and must conform
91
- # to the following regular expression:
92
- # `[a-z]([-a-z0-9]*[a-z0-9])?`.
93
- #
94
- # Label values must be between 0 and 63 characters long and must
95
- # conform to the regular expression
96
- # `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
97
- #
98
- # No more than 256 labels can be associated with a given resource.
99
- # (`Hash`)
100
- #
101
- # @example
102
- # require "google/cloud/resource_manager"
103
- #
104
- # resource_manager = Google::Cloud::ResourceManager.new
105
- # project = resource_manager.project "tokyo-rain-123"
106
- # project.update do |p|
107
- # p.labels = { "env" => "production" }
108
- # end
109
- #
110
- def labels= new_labels
111
- gapi.labels = new_labels
112
- end
113
-
114
- ##
115
- # Updates the reference to a parent with a new Resource.
116
- #
117
- # Supported parent types include "organization" and "folder". Once
118
- # set, the parent can be updated but cannot be cleared.
119
- #
120
- # The end user must have the `resourcemanager.projects.create`
121
- # permission on the parent.
122
- #
123
- # (See {Resource} and {Manager#resource}.)
124
- #
125
- # @param [Resource] new_parent A new parent Resource.
126
- #
127
- # @example
128
- # require "google/cloud/resource_manager"
129
- #
130
- # resource_manager = Google::Cloud::ResourceManager.new
131
- # project = resource_manager.project "tokyo-rain-123"
132
- # folder = resource_manager.resource "folder", "1234"
133
- # project.parent = folder
134
- #
135
- def parent= new_parent
136
- raise ArgumentError, "new_parent is required" if new_parent.nil?
137
- gapi.parent = new_parent.to_gapi
138
- end
139
-
140
- ##
141
- # @private Create an Updater object.
142
- def self.from_project project
143
- dupe_gapi = project.gapi.class.new(**project.gapi.to_h)
144
- dupe_project = Project.from_gapi dupe_gapi, nil # no way to update
145
- Updater.new dupe_project
146
- end
147
- end
148
- end
149
- end
150
- end
151
- end
@@ -1,502 +0,0 @@
1
- # Copyright 2015 Google LLC
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
- # https://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
- require "google/cloud/resource_manager/project/list"
18
- require "google/cloud/resource_manager/project/updater"
19
- require "google/cloud/resource_manager/policy"
20
- require "google/cloud/resource_manager/resource"
21
-
22
- module Google
23
- module Cloud
24
- module ResourceManager
25
- ##
26
- # # Project
27
- #
28
- # Project is a high-level Google Cloud Platform entity. It is a container
29
- # for ACLs, APIs, AppEngine Apps, VMs, and other Google Cloud Platform
30
- # resources.
31
- #
32
- # @example
33
- # require "google/cloud/resource_manager"
34
- #
35
- # resource_manager = Google::Cloud::ResourceManager.new
36
- # project = resource_manager.project "tokyo-rain-123"
37
- # project.update do |p|
38
- # p.name = "My Project"
39
- # p.labels["env"] = "production"
40
- # end
41
- #
42
- class Project
43
- ##
44
- # @private The Service object.
45
- attr_accessor :service
46
-
47
- ##
48
- # @private The Google API Client object.
49
- attr_accessor :gapi
50
-
51
- ##
52
- # @private Create an empty Project object.
53
- def initialize
54
- @service = nil
55
- @gapi = Google::Cloud::ResourceManager::Service::API::Project.new
56
- end
57
-
58
- ##
59
- # The unique, user-assigned ID of the project. It must be 6 to 30
60
- # lowercase letters, digits, or hyphens. It must start with a letter.
61
- # Trailing hyphens are prohibited. e.g. tokyo-rain-123
62
- #
63
- def project_id
64
- @gapi.project_id
65
- end
66
-
67
- ##
68
- # The number uniquely identifying the project. e.g. 415104041262
69
- #
70
- def project_number
71
- @gapi.project_number
72
- end
73
-
74
- ##
75
- # The user-assigned name of the project.
76
- #
77
- def name
78
- @gapi.name
79
- end
80
-
81
- ##
82
- # Updates the user-assigned name of the project. This field is optional
83
- # and can remain unset.
84
- #
85
- # Allowed characters are: lowercase and uppercase letters, numbers,
86
- # hyphen, single-quote, double-quote, space, and exclamation point.
87
- #
88
- # @example
89
- # require "google/cloud/resource_manager"
90
- #
91
- # resource_manager = Google::Cloud::ResourceManager.new
92
- # project = resource_manager.project "tokyo-rain-123"
93
- # project.name = "My Project"
94
- #
95
- def name= new_name
96
- ensure_service!
97
- @gapi.name = new_name
98
- @gapi = service.update_project @gapi
99
- end
100
-
101
- ##
102
- # The labels associated with this project.
103
- #
104
- # Label keys must be between 1 and 63 characters long and must conform
105
- # to the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?`.
106
- #
107
- # Label values must be between 0 and 63 characters long and must conform
108
- # to the regular expression `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
109
- #
110
- # No more than 256 labels can be associated with a given resource.
111
- # (`Hash`)
112
- #
113
- # @yield [labels] a block for setting labels
114
- # @yieldparam [Hash] labels the hash accepting labels
115
- #
116
- # @example Labels are read-only and cannot be changed:
117
- # require "google/cloud/resource_manager"
118
- #
119
- # resource_manager = Google::Cloud::ResourceManager.new
120
- # project = resource_manager.project "tokyo-rain-123"
121
- # project.labels["env"] # read-only
122
- #
123
- # @example Labels can be updated by passing a block, or with {#labels=}:
124
- # require "google/cloud/resource_manager"
125
- #
126
- # resource_manager = Google::Cloud::ResourceManager.new
127
- # project = resource_manager.project "tokyo-rain-123"
128
- # project.labels do |labels|
129
- # labels["env"] = "production"
130
- # end
131
- #
132
- def labels
133
- labels = @gapi.labels.to_h
134
- if block_given?
135
- yielded_labels = labels.dup
136
- yield yielded_labels
137
- self.labels = yielded_labels if yielded_labels != labels # changed
138
- else
139
- labels.freeze
140
- end
141
- end
142
-
143
- ##
144
- # Updates the labels associated with this project.
145
- #
146
- # Label keys must be between 1 and 63 characters long and must conform
147
- # to the regular expression `[a-z]([-a-z0-9]*[a-z0-9])?`.
148
- #
149
- # Label values must be between 0 and 63 characters long and must conform
150
- # to the regular expression `([a-z]([-a-z0-9]*[a-z0-9])?)?`.
151
- #
152
- # No more than 256 labels can be associated with a given resource.
153
- # (`Hash`)
154
- #
155
- # @example
156
- # require "google/cloud/resource_manager"
157
- #
158
- # resource_manager = Google::Cloud::ResourceManager.new
159
- # project = resource_manager.project "tokyo-rain-123"
160
- # project.labels = { "env" => "production" }
161
- #
162
- def labels= new_labels
163
- ensure_service!
164
- @gapi.labels = new_labels
165
- @gapi = service.update_project @gapi
166
- end
167
-
168
- ##
169
- # An optional reference to a parent Resource.
170
- #
171
- # Supported parent types include "organization" and "folder". Once set,
172
- # the parent can be updated but cannot be cleared.
173
- #
174
- # The resource object returned is read-only and cannot be changed. A new
175
- # resource object can be set using the `#parent=` or `#update` methods.
176
- # (See {Resource} and {Manager#resource}.)
177
- #
178
- # @return [nil, Resource] the reference to a parent Resource (read-only)
179
- #
180
- def parent
181
- return nil if @gapi.parent.nil?
182
- Resource.from_gapi(@gapi.parent).freeze
183
- end
184
-
185
- ##
186
- # Updates the reference to a parent with a new Resource.
187
- #
188
- # Supported parent types include "organization" and "folder". Once set,
189
- # the parent can be updated but cannot be cleared.
190
- #
191
- # The end user must have the `resourcemanager.projects.create`
192
- # permission on the parent.
193
- #
194
- # (See {Resource} and {Manager#resource}.)
195
- #
196
- # @param [Resource] new_parent A new parent Resource.
197
- #
198
- # @example
199
- # require "google/cloud/resource_manager"
200
- #
201
- # resource_manager = Google::Cloud::ResourceManager.new
202
- # project = resource_manager.project "tokyo-rain-123"
203
- # folder = resource_manager.resource "folder", "1234"
204
- # project.parent = folder
205
- #
206
- def parent= new_parent
207
- raise ArgumentError, "new_parent is required" if new_parent.nil?
208
- ensure_service!
209
- @gapi.parent = new_parent.to_gapi
210
- @gapi = service.update_project @gapi
211
- end
212
-
213
- ##
214
- # The time that this project was created.
215
- #
216
- def created_at
217
- Time.parse @gapi.create_time
218
- rescue StandardError
219
- nil
220
- end
221
-
222
- ##
223
- # The project lifecycle state.
224
- #
225
- # Possible values are:
226
- # * `ACTIVE` - The normal and active state.
227
- # * `DELETE_REQUESTED` - The project has been marked for deletion by the
228
- # user (by invoking ##delete) or by the system (Google Cloud
229
- # Platform). This can generally be reversed by invoking {#undelete}.
230
- # * `DELETE_IN_PROGRESS` - The process of deleting the project has
231
- # begun. Reversing the deletion is no longer possible.
232
- # * `LIFECYCLE_STATE_UNSPECIFIED` - Unspecified state. This is only
233
- # used/useful for distinguishing unset values.
234
- #
235
- def state
236
- @gapi.lifecycle_state
237
- end
238
-
239
- ##
240
- # Checks if the state is `ACTIVE`.
241
- def active?
242
- return false if state.nil?
243
- "ACTIVE".casecmp(state).zero?
244
- end
245
-
246
- ##
247
- # Checks if the state is `LIFECYCLE_STATE_UNSPECIFIED`.
248
- def unspecified?
249
- return false if state.nil?
250
- "LIFECYCLE_STATE_UNSPECIFIED".casecmp(state).zero?
251
- end
252
-
253
- ##
254
- # Checks if the state is `DELETE_REQUESTED`.
255
- def delete_requested?
256
- return false if state.nil?
257
- "DELETE_REQUESTED".casecmp(state).zero?
258
- end
259
-
260
- ##
261
- # Checks if the state is `DELETE_IN_PROGRESS`.
262
- def delete_in_progress?
263
- return false if state.nil?
264
- "DELETE_IN_PROGRESS".casecmp(state).zero?
265
- end
266
-
267
- ##
268
- # Updates the project in a single API call. See {Project::Updater}
269
- #
270
- # @yield [project] a block yielding a project delegate
271
- # @yieldparam [Project::Updater] project the delegate object for
272
- # updating the project
273
- #
274
- # @example
275
- # require "google/cloud/resource_manager"
276
- #
277
- # resource_manager = Google::Cloud::ResourceManager.new
278
- # project = resource_manager.project "tokyo-rain-123"
279
- # folder = resource_manager.resource "folder", "1234"
280
- # project.update do |p|
281
- # p.name = "My Project"
282
- # p.parent = folder
283
- # p.labels["env"] = "production"
284
- # end
285
- #
286
- def update
287
- updater = Updater.from_project self
288
- yield updater
289
- if updater.gapi.to_h != @gapi.to_h # changed
290
- @gapi = service.update_project updater.gapi
291
- end
292
- self
293
- end
294
-
295
- ##
296
- # Reloads the project (with updated state) from the Google Cloud
297
- # Resource Manager service.
298
- #
299
- # @example
300
- # require "google/cloud/resource_manager"
301
- #
302
- # resource_manager = Google::Cloud::ResourceManager.new
303
- # project = resource_manager.project "tokyo-rain-123"
304
- # project.reload!
305
- #
306
- def reload!
307
- @gapi = service.get_project project_id
308
- end
309
- alias refresh! reload!
310
-
311
- ##
312
- # Marks the project for deletion. This method will only affect the
313
- # project if the following criteria are met:
314
- #
315
- # * The project does not have a billing account associated with it.
316
- # * The project has a lifecycle state of `ACTIVE`.
317
- # * This method changes the project's lifecycle state from `ACTIVE` to
318
- # `DELETE_REQUESTED`. The deletion starts at an unspecified time, at
319
- # which point the lifecycle state changes to `DELETE_IN_PROGRESS`.
320
- #
321
- # Until the deletion completes, you can check the lifecycle state by
322
- # calling #reload!, or by retrieving the project with Manager#project.
323
- # The project remains visible to Manager#project and Manager#projects,
324
- # but cannot be updated.
325
- #
326
- # After the deletion completes, the project is not retrievable by the
327
- # Manager#project and Manager#projects methods.
328
- #
329
- # The caller must have modify permissions for this project.
330
- #
331
- # @example
332
- # require "google/cloud/resource_manager"
333
- #
334
- # resource_manager = Google::Cloud::ResourceManager.new
335
- # project = resource_manager.project "tokyo-rain-123"
336
- # project.active? #=> true
337
- # project.delete
338
- # project.active? #=> false
339
- # project.delete_requested? #=> true
340
- #
341
- def delete
342
- service.delete_project project_id
343
- reload!
344
- true
345
- end
346
-
347
- ##
348
- # Restores the project. You can only use this method for a project that
349
- # has a lifecycle state of `DELETE_REQUESTED`. After deletion starts, as
350
- # indicated by a lifecycle state of `DELETE_IN_PROGRESS`, the project
351
- # cannot be restored.
352
- #
353
- # The caller must have modify permissions for this project.
354
- #
355
- # @example
356
- # require "google/cloud/resource_manager"
357
- #
358
- # resource_manager = Google::Cloud::ResourceManager.new
359
- # project = resource_manager.project "tokyo-rain-123"
360
- # project.delete_requested? #=> true
361
- # project.undelete
362
- # project.delete_requested? #=> false
363
- # project.active? #=> true
364
- #
365
- def undelete
366
- service.undelete_project project_id
367
- reload!
368
- true
369
- end
370
-
371
- ##
372
- # Gets and updates the [Cloud IAM](https://cloud.google.com/iam/) access
373
- # control policy for this project.
374
- #
375
- # @see https://cloud.google.com/iam/docs/managing-policies Managing
376
- # Policies
377
- # @see https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/setIamPolicy
378
- # projects.setIamPolicy
379
- #
380
- # @yield [policy] A block for updating the policy. The latest policy
381
- # will be read from the service and passed to the block. After the
382
- # block completes, the modified policy will be written to the service.
383
- # @yieldparam [Policy] policy the current Cloud IAM Policy for this
384
- # project
385
- #
386
- # @return [Policy] the current Cloud IAM Policy for this project
387
- #
388
- # @example
389
- # require "google/cloud/resource_manager"
390
- #
391
- # resource_manager = Google::Cloud::ResourceManager.new
392
- # project = resource_manager.project "tokyo-rain-123"
393
- #
394
- # policy = project.policy
395
- #
396
- # @example Update the policy by passing a block:
397
- # require "google/cloud/resource_manager"
398
- #
399
- # resource_manager = Google::Cloud::ResourceManager.new
400
- # project = resource_manager.project "tokyo-rain-123"
401
- #
402
- # project.policy do |p|
403
- # p.add "roles/owner", "user:owner@example.com"
404
- # end
405
- #
406
- def policy
407
- ensure_service!
408
- gapi = service.get_policy project_id
409
- policy = Policy.from_gapi gapi
410
- return policy unless block_given?
411
- yield policy
412
- update_policy policy
413
- end
414
-
415
- ##
416
- # Updates the [Cloud IAM](https://cloud.google.com/iam/) access control
417
- # policy for this project. The policy should be read from {#policy}. See
418
- # {Google::Cloud::ResourceManager::Policy} for an explanation of the
419
- # policy `etag` property and how to modify policies.
420
- #
421
- # You can also update the policy by passing a block to {#policy}, which
422
- # will call this method internally after the block completes.
423
- #
424
- # @see https://cloud.google.com/iam/docs/managing-policies Managing
425
- # Policies
426
- # @see https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects/setIamPolicy
427
- # projects.setIamPolicy
428
- #
429
- # @param [Policy] new_policy a new or modified Cloud IAM Policy for this
430
- # project
431
- #
432
- # @return [Policy] the policy returned by the API update operation
433
- #
434
- # @example
435
- # require "google/cloud/resource_manager"
436
- #
437
- # resource_manager = Google::Cloud::ResourceManager.new
438
- # project = resource_manager.project "tokyo-rain-123"
439
- #
440
- # policy = project.policy # API call
441
- #
442
- # policy.add "roles/owner", "user:owner@example.com"
443
- #
444
- # project.update_policy policy # API call
445
- #
446
- def update_policy new_policy
447
- ensure_service!
448
- gapi = service.set_policy project_id, new_policy.to_gapi
449
- Policy.from_gapi gapi
450
- end
451
- alias policy= update_policy
452
-
453
- ##
454
- # Tests the specified permissions against the [Cloud
455
- # IAM](https://cloud.google.com/iam/) access control policy.
456
- #
457
- # @see https://cloud.google.com/iam/docs/managing-policies Managing
458
- # Policies
459
- #
460
- # @param [String, Array<String>] permissions The set of permissions to
461
- # check access for. Permissions with wildcards (such as `*` or
462
- # `storage.*`) are not allowed.
463
- #
464
- # @return [Array<String>] The permissions that have access
465
- #
466
- # @example
467
- # require "google/cloud/resource_manager"
468
- #
469
- # resource_manager = Google::Cloud::ResourceManager.new
470
- # project = resource_manager.project "tokyo-rain-123"
471
- # perms = project.test_permissions "resourcemanager.projects.get",
472
- # "resourcemanager.projects.delete"
473
- # perms.include? "resourcemanager.projects.get" #=> true
474
- # perms.include? "resourcemanager.projects.delete" #=> false
475
- #
476
- def test_permissions *permissions
477
- permissions = Array(permissions).flatten
478
- ensure_service!
479
- gapi = service.test_permissions project_id, permissions
480
- gapi.permissions
481
- end
482
-
483
- ##
484
- # @private New Change from a Google API Client object.
485
- def self.from_gapi gapi, service
486
- new.tap do |p|
487
- p.gapi = gapi
488
- p.service = service
489
- end
490
- end
491
-
492
- protected
493
-
494
- ##
495
- # Raise an error unless an active service is available.
496
- def ensure_service!
497
- raise "Must have active connection" unless service
498
- end
499
- end
500
- end
501
- end
502
- end