Module: Google::Cloud::ResourceManager

Defined in:
lib/google/cloud/resource_manager.rb,
lib/google/cloud/resource_manager/policy.rb,
lib/google/cloud/resource_manager/manager.rb,
lib/google/cloud/resource_manager/project.rb,
lib/google/cloud/resource_manager/service.rb,
lib/google/cloud/resource_manager/version.rb,
lib/google/cloud/resource_manager/credentials.rb,
lib/google/cloud/resource_manager/project/list.rb,
lib/google/cloud/resource_manager/project/updater.rb

Overview

Google Cloud Resource Manager

The Resource Manager API provides methods that you can use to programmatically manage your projects in the Google Cloud Platform. You may be familiar with managing projects in the Developers Console. With this API you can do the following:

  • Get a list of all projects associated with an account
  • Create new projects
  • Update existing projects
  • Delete projects
  • Undelete, or recover, projects that you don't want to delete

Authentication

The Resource Manager API currently requires authentication of a User Account, and cannot currently be accessed with a Service Account. To use a User Account install the Google Cloud SDK and authenticate with the following:

$ gcloud auth login

Also make sure all GCLOUD environment variables are cleared of any service accounts. Then google-cloud will be able to detect the user authentication and connect with those credentials.

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new

Listing Projects

Project is a collection of settings, credentials, and metadata about the application or applications you're working on. You can retrieve and inspect all projects that you have permissions to. (See Manager#projects)

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
resource_manager.projects.each do |project|
  puts projects.project_id
end

Managing Projects with Labels

Labels can be added to or removed from projects. (See Project#labels)

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.project "tokyo-rain-123"
# Label the project as production
project.update do |p|
  p.labels["env"] = "production"
end

Projects can then be filtered by labels. (See Manager#projects)

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
# Find only the productions projects
projects = resource_manager.projects filter: "labels.env:production"
projects.each do |project|
  puts project.project_id
end

Creating a Project

You can also use the API to create new projects. (See Manager#create_project)

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
project = resource_manager.create_project "tokyo-rain-123",
                                          name: "Todos Development",
                                          labels: {env: :development}

Deleting a Project

You can delete projects when they are no longer needed. (See Manager#delete and Project#delete)

require "google/cloud/resource_manager"

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

Undeleting a Project

You can also restore a deleted project within the waiting period that starts when the project was deleted. Restoring a project returns it to the state it was in prior to being deleted. (See Manager#undelete and Project#undelete)

require "google/cloud/resource_manager"

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

Configuring retries and timeout

You can configure how many times API requests may be automatically retried. When an API request fails, the response will be inspected to see if the request meets criteria indicating that it may succeed on retry, such as 500 and 503 status codes or a specific internal error code such as rateLimitExceeded. If it meets the criteria, the request will be retried after a delay. If another error occurs, the delay will be increased before a subsequent attempt, until the retries limit is reached.

You can also set the request timeout value in seconds.

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new retries: 10,
                                                      timeout: 120

See the Resource Manager error messages for a list of error conditions.

Managing IAM Policies

Google Cloud Identity and Access Management (Cloud IAM) access control policies can be managed on projects. These policies allow project owners to manage who (identity) has access to what (role). See Cloud IAM Overview for more information.

A project's access control policy can be retrieved. (See Project#policy and Policy.)

require "google/cloud/resource_manager"

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

A project's access control policy can also be updated:

require "google/cloud/resource_manager"

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

policy = project.policy do |p|
  p.add "roles/viewer", "serviceAccount:your-service-account"
end

And permissions can be tested on a project. (See Project#test_permissions)

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

For more information about using access control policies see Managing Policies.

Defined Under Namespace

Classes: Manager, Policy, Project

Constant Summary collapse

VERSION =
"0.24.1"

Class Method Summary collapse

Class Method Details

.new(keyfile: nil, scope: nil, retries: nil, timeout: nil) ⇒ Google::Cloud::ResourceManager::Manager

Creates a new Project instance connected to the Resource Manager service. Each call creates a new connection.

For more information on connecting to Google Cloud see the Authentication Guide.

Examples:

require "google/cloud/resource_manager"

resource_manager = Google::Cloud::ResourceManager.new
resource_manager.projects.each do |project|
  puts projects.project_id
end

Parameters:

  • keyfile (String, Hash)

    Keyfile downloaded from Google Cloud. If file path the file must be readable.

  • scope (String, Array<String>)

    The OAuth 2.0 scopes controlling the set of resources and operations that the connection can access. See Using OAuth 2.0 to Access Google APIs.

    The default scope is:

    • https://www.googleapis.com/auth/cloud-platform
  • retries (Integer)

    Number of times to retry requests on server error. The default value is 3. Optional.

  • timeout (Integer)

    Default timeout to use in requests. Optional.

Returns:



255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/google/cloud/resource_manager.rb', line 255

def self.new keyfile: nil, scope: nil, retries: nil, timeout: nil
  if keyfile.nil?
    credentials = Google::Cloud::ResourceManager::Credentials.default(
      scope: scope)
  else
    credentials = Google::Cloud::ResourceManager::Credentials.new(
      keyfile, scope: scope)
  end
  Google::Cloud::ResourceManager::Manager.new(
    Google::Cloud::ResourceManager::Service.new(
      credentials, retries: retries, timeout: timeout))
end