class OpenStackRouter::Region

The class abstracting a region. It aims to centralize a region state/status regarding VM spwning capacity and connectivity.

@author Roland Laurès @attr_reader [String] name The region name. @attr_reader [Symbol] state The region's state. @see {Region::STATES} descriptions @attr_reader [Parameter::Connection] connection_parameters The parameters to connect to OpenStack

services.

Constants

STATES

The possible states of a Region

Attributes

connection_parameters[R]
name[R]
state[R]

Public Class Methods

new(name, connection_parameters) click to toggle source
# File lib/openstack-router/region.rb, line 27
def initialize(name, connection_parameters)
  @name = name
  @connection_parameters = Parameter::Connection.new(connection_parameters.to_h.merge(region: name))

  @services = {}
  @state = STATE_OPERATING
end

Public Instance Methods

my_args(request_args, region_names) click to toggle source
# File lib/openstack-router/region.rb, line 64
def my_args(request_args, region_names)
  request_args.map do |arg|
    next arg unless arg.class == Hash && arg.keys == region_names

    arg[@name]
  end
end
request(service, request_name, *args, &block) click to toggle source

This method allow to call a request on a service for this region. If the service hasn't been already connected it will be connected.

@param [Symbol] service The service to contact (:compute, :image…) @param [symbol] request_name The request to do on that service.

It must exist else an exception will be raised.

@param [Array] args All other arguments will be passed to the requested

method.

@return [NilClass | 'a | Excon::Response]

* nil if a SocketError occurs and
  the region cannot be reached.
* +'a+ is the type returned by the block you give
* Excon::Response is return if no block is given and no error is raised.

@yield [answer] Block called with the answer for you to filter.

@yieldparam [Excon::Response] answer The answer of the request @yieldreturn ['a] You return what you want. It will be then return without

being touched by the method.
Since nothing is done after yielding, then you can use the +return+ keyword
from the block instead of +break+. But for safety, we recommand using +break+.
# File lib/openstack-router/region.rb, line 57
def request(service, request_name, *args, &block)
  service = service.to_s.capitalize
  return nil if @state == STATE_FAILED && setup(service) == STATE_FAILED

  do_request(service, request_name, *args, &block)
end

Private Instance Methods

do_request(service, request_name, *args) { |answer| ... } click to toggle source
# File lib/openstack-router/region.rb, line 82
def do_request(service, request_name, *args)
  setup(service) unless @services.key?(service)
  answer = @services[service].send(request_name, *args)
  block_given? ? yield(answer) : answer
rescue SocketError
  @services.delete(service)
  @state = STATE_FAILED if @services.empty?
  nil
end
setup(service) click to toggle source
# File lib/openstack-router/region.rb, line 74
def setup(service)
  @services[service] = Object.const_get(Fog::OpenStack.service_klass(service)).new(@connection_parameters.to_os_h)
  @state = STATE_OPERATING
rescue SocketError
  @state = STATE_FAILED if @services.empty?
  STATE_FAILED
end