Class: Google::Cloud::ResourceManager::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/google/cloud/resource_manager/project.rb,
lib/google/cloud/resource_manager/project/list.rb,
lib/google/cloud/resource_manager/project/updater.rb

Overview

Project

Project is a high-level Google Cloud Platform entity. It is a container for ACLs, APIs, AppEngine Apps, VMs, and other Google Cloud Platform resources.

Examples:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"
project.update do |p|
  p.name = "My Project"
  p.labels["env"] = "production"
end

Direct Known Subclasses

Updater

Defined Under Namespace

Classes: List, Updater

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Checks if the state is ACTIVE.

Returns:

  • (Boolean)


241
242
243
244
# File 'lib/google/cloud/resource_manager/project.rb', line 241

def active?
  return false if state.nil?
  "ACTIVE".casecmp(state).zero?
end

#created_atObject

The time that this project was created.



216
217
218
219
220
# File 'lib/google/cloud/resource_manager/project.rb', line 216

def created_at
  Time.parse @gapi.create_time
rescue StandardError
  nil
end

#deleteObject

Marks the project for deletion. This method will only affect the project if the following criteria are met:

  • The project does not have a billing account associated with it.
  • The project has a lifecycle state of ACTIVE.
  • This method changes the project's lifecycle state from ACTIVE to DELETE_REQUESTED. The deletion starts at an unspecified time, at which point the lifecycle state changes to DELETE_IN_PROGRESS.

Until the deletion completes, you can check the lifecycle state by calling #reload!, or by retrieving the project with Manager#project. The project remains visible to Manager#project and Manager#projects, but cannot be updated.

After the deletion completes, the project is not retrievable by the Manager#project and Manager#projects methods.

The caller must have modify permissions for this project.

Examples:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"
project.active? #=> true
project.delete
project.active? #=> false
project.delete_requested? #=> true


341
342
343
344
345
# File 'lib/google/cloud/resource_manager/project.rb', line 341

def delete
  service.delete_project project_id
  reload!
  true
end

#delete_in_progress?Boolean

Checks if the state is DELETE_IN_PROGRESS.

Returns:

  • (Boolean)


262
263
264
265
# File 'lib/google/cloud/resource_manager/project.rb', line 262

def delete_in_progress?
  return false if state.nil?
  "DELETE_IN_PROGRESS".casecmp(state).zero?
end

#delete_requested?Boolean

Checks if the state is DELETE_REQUESTED.

Returns:

  • (Boolean)


255
256
257
258
# File 'lib/google/cloud/resource_manager/project.rb', line 255

def delete_requested?
  return false if state.nil?
  "DELETE_REQUESTED".casecmp(state).zero?
end

#labels {|labels| ... } ⇒ Object

The labels associated with this project.

Label keys must be between 1 and 63 characters long and must conform to the regular expression [a-z]([-a-z0-9]*[a-z0-9])?.

Label values must be between 0 and 63 characters long and must conform to the regular expression ([a-z]([-a-z0-9]*[a-z0-9])?)?.

No more than 256 labels can be associated with a given resource. (Hash)

Examples:

Labels are read-only and cannot be changed:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"
project.labels["env"] # read-only

Labels can be updated by passing a block, or with #labels=:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"
project.labels do |labels|
  labels["env"] = "production"
end

Yields:

  • (labels)

    a block for setting labels

Yield Parameters:

  • labels (Hash)

    the hash accepting labels



132
133
134
135
136
137
138
139
140
141
# File 'lib/google/cloud/resource_manager/project.rb', line 132

def labels
  labels = @gapi.labels.to_h
  if block_given?
    yielded_labels = labels.dup
    yield yielded_labels
    self.labels = yielded_labels if yielded_labels != labels # changed
  else
    labels.freeze
  end
end

#labels=(new_labels) ⇒ Object

Updates the labels associated with this project.

Label keys must be between 1 and 63 characters long and must conform to the regular expression [a-z]([-a-z0-9]*[a-z0-9])?.

Label values must be between 0 and 63 characters long and must conform to the regular expression ([a-z]([-a-z0-9]*[a-z0-9])?)?.

No more than 256 labels can be associated with a given resource. (Hash)

Examples:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"
project.labels = { "env" => "production" }


162
163
164
165
166
# File 'lib/google/cloud/resource_manager/project.rb', line 162

def labels= new_labels
  ensure_service!
  @gapi.labels = new_labels
  @gapi = service.update_project @gapi
end

#nameObject

The user-assigned name of the project.



77
78
79
# File 'lib/google/cloud/resource_manager/project.rb', line 77

def name
  @gapi.name
end

#name=(new_name) ⇒ Object

Updates the user-assigned name of the project. This field is optional and can remain unset.

Allowed characters are: lowercase and uppercase letters, numbers, hyphen, single-quote, double-quote, space, and exclamation point.

Examples:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"
project.name = "My Project"


95
96
97
98
99
# File 'lib/google/cloud/resource_manager/project.rb', line 95

def name= new_name
  ensure_service!
  @gapi.name = new_name
  @gapi = service.update_project @gapi
end

#parentnil, Resource

An optional reference to a parent Resource.

Supported parent types include "organization" and "folder". Once set, the parent can be updated but cannot be cleared.

The resource object returned is read-only and cannot be changed. A new resource object can be set using the #parent= or #update methods. (See Resource and Manager#resource.)

Returns:

  • (nil, Resource)

    the reference to a parent Resource (read-only)



180
181
182
183
# File 'lib/google/cloud/resource_manager/project.rb', line 180

def parent
  return nil if @gapi.parent.nil?
  Resource.from_gapi(@gapi.parent).freeze
end

#parent=(new_parent) ⇒ Object

Updates the reference to a parent with a new Resource.

Supported parent types include "organization" and "folder". Once set, the parent can be updated but cannot be cleared.

The end user must have the resourcemanager.projects.create permission on the parent.

(See Resource and Manager#resource.)

Examples:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"
folder = resource_manager.resource "folder", "1234"
project.parent = folder

Parameters:

  • new_parent (Resource)

    A new parent Resource.

Raises:

  • (ArgumentError)


206
207
208
209
210
211
# File 'lib/google/cloud/resource_manager/project.rb', line 206

def parent= new_parent
  raise ArgumentError, "new_parent is required" if new_parent.nil?
  ensure_service!
  @gapi.parent = new_parent.to_gapi
  @gapi = service.update_project @gapi
end

#policy {|policy| ... } ⇒ Policy

Gets and updates the Cloud IAM access control policy for this project.

Examples:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"

policy = project.policy

Update the policy by passing a block:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"

project.policy do |p|
  p.add "roles/owner", "user:owner@example.com"
end

Yields:

  • (policy)

    A block for updating the policy. The latest policy will be read from the service and passed to the block. After the block completes, the modified policy will be written to the service.

Yield Parameters:

  • policy (Policy)

    the current Cloud IAM Policy for this project

Returns:

  • (Policy)

    the current Cloud IAM Policy for this project

See Also:



406
407
408
409
410
411
412
413
# File 'lib/google/cloud/resource_manager/project.rb', line 406

def policy
  ensure_service!
  gapi = service.get_policy project_id
  policy = Policy.from_gapi gapi
  return policy unless block_given?
  yield policy
  update_policy policy
end

#project_idObject

The unique, user-assigned ID of the project. It must be 6 to 30 lowercase letters, digits, or hyphens. It must start with a letter. Trailing hyphens are prohibited. e.g. tokyo-rain-123



63
64
65
# File 'lib/google/cloud/resource_manager/project.rb', line 63

def project_id
  @gapi.project_id
end

#project_numberObject

The number uniquely identifying the project. e.g. 415104041262



70
71
72
# File 'lib/google/cloud/resource_manager/project.rb', line 70

def project_number
  @gapi.project_number
end

#reload!Object Also known as: refresh!

Reloads the project (with updated state) from the Google Cloud Resource Manager service.

Examples:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"
project.reload!


306
307
308
# File 'lib/google/cloud/resource_manager/project.rb', line 306

def reload!
  @gapi = service.get_project project_id
end

#stateObject

The project lifecycle state.

Possible values are:

  • ACTIVE - The normal and active state.
  • DELETE_REQUESTED - The project has been marked for deletion by the user (by invoking ##delete) or by the system (Google Cloud Platform). This can generally be reversed by invoking #undelete.
  • DELETE_IN_PROGRESS - The process of deleting the project has begun. Reversing the deletion is no longer possible.
  • LIFECYCLE_STATE_UNSPECIFIED - Unspecified state. This is only used/useful for distinguishing unset values.


235
236
237
# File 'lib/google/cloud/resource_manager/project.rb', line 235

def state
  @gapi.lifecycle_state
end

#test_permissions(*permissions) ⇒ Array<String>

Tests the specified permissions against the Cloud IAM access control policy.

Examples:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"
perms = project.test_permissions "resourcemanager.projects.get",
                                 "resourcemanager.projects.delete"
perms.include? "resourcemanager.projects.get"    #=> true
perms.include? "resourcemanager.projects.delete" #=> false

Parameters:

  • permissions (String, Array<String>)

    The set of permissions to check access for. Permissions with wildcards (such as * or storage.*) are not allowed.

Returns:

  • (Array<String>)

    The permissions that have access

See Also:



476
477
478
479
480
481
# File 'lib/google/cloud/resource_manager/project.rb', line 476

def test_permissions *permissions
  permissions = Array(permissions).flatten
  ensure_service!
  gapi = service.test_permissions project_id, permissions
  gapi.permissions
end

#undeleteObject

Restores the project. You can only use this method for a project that has a lifecycle state of DELETE_REQUESTED. After deletion starts, as indicated by a lifecycle state of DELETE_IN_PROGRESS, the project cannot be restored.

The caller must have modify permissions for this project.

Examples:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"
project.delete_requested? #=> true
project.undelete
project.delete_requested? #=> false
project.active? #=> true


365
366
367
368
369
# File 'lib/google/cloud/resource_manager/project.rb', line 365

def undelete
  service.undelete_project project_id
  reload!
  true
end

#unspecified?Boolean

Checks if the state is LIFECYCLE_STATE_UNSPECIFIED.

Returns:

  • (Boolean)


248
249
250
251
# File 'lib/google/cloud/resource_manager/project.rb', line 248

def unspecified?
  return false if state.nil?
  "LIFECYCLE_STATE_UNSPECIFIED".casecmp(state).zero?
end

#update {|project| ... } ⇒ Object

Updates the project in a single API call. See Updater

Examples:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"
folder = resource_manager.resource "folder", "1234"
project.update do |p|
  p.name = "My Project"
  p.parent = folder
  p.labels["env"] = "production"
end

Yields:

  • (project)

    a block yielding a project delegate

Yield Parameters:



286
287
288
289
290
291
292
293
# File 'lib/google/cloud/resource_manager/project.rb', line 286

def update
  updater = Updater.from_project self
  yield updater
  if updater.gapi.to_h != @gapi.to_h # changed
    @gapi = service.update_project updater.gapi
  end
  self
end

#update_policy(new_policy) ⇒ Policy Also known as: policy=

Updates the Cloud IAM access control policy for this project. The policy should be read from #policy. See Google::Cloud::ResourceManager::Policy for an explanation of the policy etag property and how to modify policies.

You can also update the policy by passing a block to #policy, which will call this method internally after the block completes.

Examples:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"

policy = project.policy # API call

policy.add "roles/owner", "user:owner@example.com"

project.update_policy policy # API call

Parameters:

  • new_policy (Policy)

    a new or modified Cloud IAM Policy for this project

Returns:

  • (Policy)

    the policy returned by the API update operation

See Also:



446
447
448
449
450
# File 'lib/google/cloud/resource_manager/project.rb', line 446

def update_policy new_policy
  ensure_service!
  gapi = service.set_policy project_id, new_policy.to_gapi
  Policy.from_gapi gapi
end