Class: Gcloud::ResourceManager::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/gcloud/resource_manager/project.rb,
lib/gcloud/resource_manager/project/list.rb,
lib/gcloud/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 "gcloud"

gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
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 Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProject

Returns a new instance of Project.



52
53
54
55
# File 'lib/gcloud/resource_manager/project.rb', line 52

def initialize
  @connection = nil
  @gapi = {}
end

Instance Attribute Details

#connectionObject



44
45
46
# File 'lib/gcloud/resource_manager/project.rb', line 44

def connection
  @connection
end

#gapiObject



48
49
50
# File 'lib/gcloud/resource_manager/project.rb', line 48

def gapi
  @gapi
end

Class Method Details

.from_gapi(gapi, connection) ⇒ Object



492
493
494
495
496
497
# File 'lib/gcloud/resource_manager/project.rb', line 492

def self.from_gapi gapi, connection
  new.tap do |p|
    p.gapi = gapi
    p.connection = connection
  end
end

Instance Method Details

#active?Boolean

Checks if the state is ‘ACTIVE`.

Returns:

  • (Boolean)


211
212
213
214
# File 'lib/gcloud/resource_manager/project.rb', line 211

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

#created_atObject

The time that this project was created.



186
187
188
189
190
# File 'lib/gcloud/resource_manager/project.rb', line 186

def created_at
  Time.parse @gapi["createTime"]
rescue
  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 "gcloud"

gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
project = resource_manager.project "tokyo-rain-123"
project.active? #=> true
project.delete
project.active? #=> false
project.delete_requested? #=> true


319
320
321
322
323
324
325
326
327
# File 'lib/gcloud/resource_manager/project.rb', line 319

def delete
  resp = connection.delete_project project_id
  if resp.success?
    reload!
    true
  else
    fail ApiError.from_response(resp)
  end
end

#delete_in_progress?Boolean

Checks if the state is ‘DELETE_IN_PROGRESS`.

Returns:

  • (Boolean)


232
233
234
235
# File 'lib/gcloud/resource_manager/project.rb', line 232

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)


225
226
227
228
# File 'lib/gcloud/resource_manager/project.rb', line 225

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 "gcloud"

gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
project = resource_manager.project "tokyo-rain-123"
project.labels["env"] #=> "dev" # read only
project.labels["env"] = "production" # raises error

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

require "gcloud"

gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
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



140
141
142
143
144
145
146
147
148
149
150
# File 'lib/gcloud/resource_manager/project.rb', line 140

def labels
  labels = @gapi["labels"]
  labels = labels.to_hash if labels.respond_to? :to_hash
  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 "gcloud"

gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
project = resource_manager.project "tokyo-rain-123"
project.labels = { "env" => "production" }


172
173
174
175
176
177
178
179
180
181
# File 'lib/gcloud/resource_manager/project.rb', line 172

def labels= new_labels
  ensure_connection!
  @gapi["labels"] = new_labels
  resp = connection.update_project @gapi
  if resp.success?
    @gapi = resp.data
  else
    fail ApiError.from_response(resp)
  end
end

#nameObject

The user-assigned name of the project.



76
77
78
# File 'lib/gcloud/resource_manager/project.rb', line 76

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 "gcloud"

gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
project = resource_manager.project "tokyo-rain-123"
project.name = "My Project"


95
96
97
98
99
100
101
102
103
104
# File 'lib/gcloud/resource_manager/project.rb', line 95

def name= new_name
  ensure_connection!
  @gapi["name"] = new_name
  resp = connection.update_project @gapi
  if resp.success?
    @gapi = resp.data
  else
    fail ApiError.from_response(resp)
  end
end

#policy(force: false) ⇒ Hash

Gets the [Cloud IAM](cloud.google.com/iam/) access control policy. Returns a hash that conforms to the following structure:

{
  "bindings" => [{
    "role" => "roles/viewer",
    "members" => ["serviceAccount:your-service-account"]
  }],
  "version" => 0,
  "etag" => "CAE="
}

Examples:

Policy values are memoized by default:

require "gcloud"

gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
project = resource_manager.project "tokyo-rain-123"
policy = project.policy

puts policy["bindings"]
puts policy["version"]
puts policy["etag"]

Use the ‘force` option to retrieve the latest policy:

require "gcloud"

gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
project = resource_manager.project "tokyo-rain-123"
policy = project.policy force: true

Parameters:

  • force (Boolean) (defaults to: false)

    Force load the latest policy when ‘true`. Otherwise the policy will be memoized to reduce the number of API calls made. The default is `false`.

Returns:

  • (Hash)

    See description

See Also:



400
401
402
403
404
405
406
407
408
409
410
# File 'lib/gcloud/resource_manager/project.rb', line 400

def policy force: false
  @policy = nil if force
  @policy ||= begin
    ensure_connection!
    resp = connection.get_policy project_id
    fail ApiError.from_response(resp) unless resp.success?
    policy = resp.data
    policy = policy.to_hash if policy.respond_to? :to_hash
    policy
  end
end

#policy=(new_policy) ⇒ Object

Sets the [Cloud IAM](cloud.google.com/iam/) access control policy.

Examples:

require "gcloud"

gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
project = resource_manager.project "tokyo-rain-123"

viewer_policy = {
  "bindings" => [{
    "role" => "roles/viewer",
    "members" => ["serviceAccount:your-service-account"]
  }]
}
project.policy = viewer_policy

Parameters:

  • new_policy (String)

    A hash that conforms to the following structure:

    {
      "bindings" => [{
        "role" => "roles/viewer",
        "members" => ["serviceAccount:your-service-account"]
      }]
    }
    

See Also:



444
445
446
447
448
449
450
451
452
453
# File 'lib/gcloud/resource_manager/project.rb', line 444

def policy= new_policy
  ensure_connection!
  resp = connection.set_policy project_id, new_policy
  if resp.success?
    @policy = resp.data
    @policy = @policy.to_hash if @policy.respond_to? :to_hash
  else
    fail ApiError.from_response(resp)
  end
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



62
63
64
# File 'lib/gcloud/resource_manager/project.rb', line 62

def project_id
  @gapi["projectId"]
end

#project_numberObject

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



69
70
71
# File 'lib/gcloud/resource_manager/project.rb', line 69

def project_number
  @gapi["projectNumber"]
end

#reload!Object Also known as: refresh!

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

Examples:

require "gcloud"

gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
project = resource_manager.project "tokyo-rain-123"
project.reload!


278
279
280
281
282
283
284
285
# File 'lib/gcloud/resource_manager/project.rb', line 278

def reload!
  resp = connection.get_project project_id
  if resp.success?
    @gapi = resp.data
  else
    fail ApiError.from_response(resp)
  end
end

#stateObject

The project lifecycle state.

Possible values are:

  • ‘ACTIVE` - The normal and active state.

  • ‘LIFECYCLE_STATE_UNSPECIFIED` - Unspecified state. This is only used/useful for distinguishing unset values.

  • ‘DELETE_REQUESTED` - The project has been marked for deletion by the user (by invoking DeleteProject) or by the system (Google Cloud Platform). This can generally be reversed by invoking UndeleteProject.

  • ‘DELETE_IN_PROGRESS` - The process of deleting the project has begun. Reversing the deletion is no longer possible.



205
206
207
# File 'lib/gcloud/resource_manager/project.rb', line 205

def state
  @gapi["lifecycleState"]
end

#test_permissions(*permissions) ⇒ Array<String>

Tests the specified permissions against the [Cloud IAM](cloud.google.com/iam/) access control policy.

Examples:

require "gcloud"

gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
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:



479
480
481
482
483
484
485
486
487
488
# File 'lib/gcloud/resource_manager/project.rb', line 479

def test_permissions *permissions
  permissions = Array(permissions).flatten
  ensure_connection!
  resp = connection.test_permissions project_id, permissions
  if resp.success?
    Array(resp.data["permissions"])
  else
    fail ApiError.from_response(resp)
  end
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 "gcloud"

gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
project = resource_manager.project "tokyo-rain-123"
project.delete_requested? #=> true
project.undelete
project.delete_requested? #=> false
project.active? #=> true


348
349
350
351
352
353
354
355
356
# File 'lib/gcloud/resource_manager/project.rb', line 348

def undelete
  resp = connection.undelete_project project_id
  if resp.success?
    reload!
    true
  else
    fail ApiError.from_response(resp)
  end
end

#unspecified?Boolean

Checks if the state is ‘LIFECYCLE_STATE_UNSPECIFIED`.

Returns:

  • (Boolean)


218
219
220
221
# File 'lib/gcloud/resource_manager/project.rb', line 218

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 "gcloud"

gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
project = resource_manager.project "tokyo-rain-123"
project.update do |p|
  p.name = "My Project"
  p.labels["env"] = "production"
end

Yields:

  • (project)

    a block yielding a project delegate

Yield Parameters:



255
256
257
258
259
260
261
262
263
264
# File 'lib/gcloud/resource_manager/project.rb', line 255

def update
  updater = Updater.from_project self
  yield updater
  resp = connection.update_project updater.gapi
  if resp.success?
    @gapi = resp.data
  else
    fail ApiError.from_response(resp)
  end
end