Class: OpenStackRouter::Region

Inherits:
Object
  • Object
show all
Defined in:
lib/openstack-router/region.rb

Overview

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

Author:

  • Roland Laurès

Constant Summary collapse

STATES =

The possible states of a Region

[
  # connected and fully operational
  STATE_OPERATING = :operating,
  # connected but some limits has been reached
  STATE_LIMITED = :limited,
  # some connection problems occured
  STATE_FAILED = :failed
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, connection_parameters) ⇒ Region

Returns a new instance of Region



27
28
29
30
31
32
33
# 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

Instance Attribute Details

#connection_parametersParameter::Connection (readonly)

The parameters to connect to OpenStack services.

Returns:



13
14
15
# File 'lib/openstack-router/region.rb', line 13

def connection_parameters
  @connection_parameters
end

#nameString (readonly)

The region name.

Returns:

  • (String)

    the current value of name



13
14
15
# File 'lib/openstack-router/region.rb', line 13

def name
  @name
end

#stateSymbol (readonly)

The region's state. @see STATES descriptions

Returns:

  • (Symbol)

    the current value of state



13
14
15
# File 'lib/openstack-router/region.rb', line 13

def state
  @state
end

Instance Method Details

#my_args(request_args, region_names) ⇒ Object



64
65
66
67
68
69
70
# 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) {|answer| ... } ⇒ NilClass | 'a | Excon::Response

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.

Parameters:

  • service (Symbol)

    The service to contact (:compute, :image…)

  • request_name (symbol)

    The request to do on that service. It must exist else an exception will be raised.

  • args (Array)

    All other arguments will be passed to the requested method.

Yields:

  • (answer)

    Block called with the answer for you to filter.

Yield Parameters:

  • answer (Excon::Response)

    The answer of the request

Yield Returns:

  • ('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.

Returns:

  • (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.



57
58
59
60
61
62
# 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