Class: Gcloud::ResourceManager::Manager
- Inherits:
-
Object
- Object
- Gcloud::ResourceManager::Manager
- Defined in:
- lib/gcloud/resource_manager/manager.rb
Overview
Manager
Provides methods for creating, retrieving, and updating projects.
require "gcloud"
gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
resource_manager.projects.each do |project|
puts projects.project_id
end
See Gcloud#resource_manager
Instance Attribute Summary collapse
-
#connection ⇒ Object
The Connection object.
Instance Method Summary collapse
-
#create_project(project_id, name: nil, labels: nil) ⇒ Object
Creates a project resource.
-
#delete(project_id) ⇒ Object
Marks the project for deletion.
-
#initialize(credentials) ⇒ Manager
constructor
Creates a new Connection instance.
-
#project(project_id) ⇒ Object
Retrieves the project identified by the specified
project_id. -
#projects(filter: nil, token: nil, max: nil) ⇒ Object
Retrieves the projects that are visible to the user and satisfy the specified filter.
-
#undelete(project_id) ⇒ Object
Restores the project.
Constructor Details
#initialize(credentials) ⇒ Manager
Creates a new Connection instance.
See Gcloud.resource_manager
46 47 48 |
# File 'lib/gcloud/resource_manager/manager.rb', line 46 def initialize credentials #:nodoc: @connection = Connection.new credentials end |
Instance Attribute Details
#connection ⇒ Object
The Connection object.
40 41 42 |
# File 'lib/gcloud/resource_manager/manager.rb', line 40 def connection @connection end |
Instance Method Details
#create_project(project_id, name: nil, labels: nil) ⇒ Object
Creates a project resource.
Initially, the project resource is owned by its creator exclusively. The creator can later grant permission to others to read or update the project.
Several APIs are activated automatically for the project, including Google Cloud Storage.
Parameters
project_id-
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. (
String) name-
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. (
String) labels-
The labels associated with this project.
Label keys must be between 1 and 63 characters long and must conform to the following 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)
Returns
Gcloud::ResourceManager::Project
Example
require "gcloud"
gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
project = resource_manager.create_project "tokyo-rain-123"
A project can also be created with a name and labels.
require "gcloud"
gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
project = resource_manager.create_project "tokyo-rain-123",
name: "Todos Development",
labels: {env: :development}
217 218 219 220 221 222 223 224 225 226 |
# File 'lib/gcloud/resource_manager/manager.rb', line 217 def create_project project_id, name: nil, labels: nil resp = connection.create_project project_id, name, labels if resp.success? Project.from_gapi resp.data, connection else fail ApiError.from_response(resp) end end |
#delete(project_id) ⇒ Object
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
ACTIVEtoDELETE_REQUESTED. The deletion starts at an unspecified time, at which point the lifecycle state changes toDELETE_IN_PROGRESS.
Until the deletion completes, you can check the lifecycle state 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.
Parameters
project_id-
The ID of the project. (
String)
Example
require "gcloud"
gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
resource_manager.delete "tokyo-rain-123"
260 261 262 263 264 265 266 267 |
# File 'lib/gcloud/resource_manager/manager.rb', line 260 def delete project_id resp = connection.delete_project project_id if resp.success? true else fail ApiError.from_response(resp) end end |
#project(project_id) ⇒ Object
Retrieves the project identified by the specified project_id.
Parameters
project_id-
The ID of the project. (
String)
Returns
Gcloud::ResourceManager::Project, or nil if the project does not exist
Example
require "gcloud"
gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
project = resource_manager.project "tokyo-rain-123"
project.project_id #=> "tokyo-rain-123"
150 151 152 153 154 155 156 157 |
# File 'lib/gcloud/resource_manager/manager.rb', line 150 def project project_id resp = connection.get_project project_id if resp.success? Project.from_gapi resp.data, connection else nil end end |
#projects(filter: nil, token: nil, max: nil) ⇒ Object
Retrieves the projects that are visible to the user and satisfy the specified filter. This method returns projects in an unspecified order. New projects do not necessarily appear at the end of the list.
Parameters
filter-
An expression for filtering the results of the request. Filter rules are case insensitive. (
String)The fields eligible for filtering are:
-
name -
id -
labels.key- wherekeyis the name of a label
Some examples of using labels as filters:
-
name:*- The project has a name. -
name:Howl- The project’s name is Howl or howl. -
name:HOWL- Equivalent to above. -
NAME:howl- Equivalent to above. -
labels.color:*- The project has the label color. -
labels.color:red- The project’s label color has the value red. -
labels.color:red labels.size:big- The project’s label color has the value red and its label size has the value big.
-
token-
A previously-returned page token representing part of the larger set of results to view. (
String) max-
Maximum number of projects to return. (
Integer)
Returns
Array of Gcloud::ResourceManager::Project (See Gcloud::ResourceManager::Project::List)
Examples
require "gcloud"
gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
projects = resource_manager.projects
projects.each do |project|
puts project.project_id
end
Projects can be filtered using the filter option:
require "gcloud"
gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
projects = resource_manager.projects filter: "labels.env:production"
projects.each do |project|
puts project.project_id
end
If you have a significant number of projects, you may need to paginate through them: (See Gcloud::ResourceManager::Project::List)
require "gcloud"
gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
projects = resource_manager.projects.all
projects.each do |project|
puts project.project_id
end
120 121 122 123 124 125 126 127 |
# File 'lib/gcloud/resource_manager/manager.rb', line 120 def projects filter: nil, token: nil, max: nil resp = connection.list_project filter: filter, token: token, max: max if resp.success? Project::List.from_response resp, self else fail ApiError.from_response(resp) end end |
#undelete(project_id) ⇒ Object
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.
Parameters
project_id-
The ID of the project. (
String)
Example
require "gcloud"
gcloud = Gcloud.new
resource_manager = gcloud.resource_manager
resource_manager.undelete "tokyo-rain-123"
290 291 292 293 294 295 296 297 |
# File 'lib/gcloud/resource_manager/manager.rb', line 290 def undelete project_id resp = connection.undelete_project project_id if resp.success? true else fail ApiError.from_response(resp) end end |