Class: OpenStackRouter::Router

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

Overview

The main class of the module. It aims to be the uniq entry point to access all the OpenStack services and all the regions.

Author:

  • Roland Laurès

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection_parameters, region_names) ⇒ Router

Returns a new instance of Router.



19
20
21
22
23
24
25
26
27
# File 'lib/openstack-router/router.rb', line 19

def initialize(connection_parameters, region_names)
  unless connection_parameters.class == Parameter::Connection
    raise ArgumentError, 'Invalid connection parameters. Please use OpenStackRouter::Parameter::Connection'
  end

  @connection_parameters = connection_parameters
  @regions = region_names.map { |region_name| Region.new(region_name, @connection_parameters) }
  @rr_circuits = {}
end

Instance Attribute Details

#connection_parametersParameter::Connection (readonly)

The connection parameters.

Returns:



15
16
17
# File 'lib/openstack-router/router.rb', line 15

def connection_parameters
  @connection_parameters
end

#regionsArray<Region> (readonly)

The regions object used to manage the by region stuffs.

Returns:

  • (Array<Region>)

    the current value of regions



15
16
17
# File 'lib/openstack-router/router.rb', line 15

def regions
  @regions
end

Instance Method Details

#region_namesArray<String>

Returns the region names.

Returns:

  • (Array<String>)

    the region names



30
31
32
# File 'lib/openstack-router/router.rb', line 30

def region_names
  @regions.map(&:name)
end

#request_each(service, request, *request_args, &block) ⇒ Hash

This method do a request on a service for each region in the router.

Parameters:

  • service (String | Symbol)

    The service you want to interogate Service name should be capitalized, but by precaution, it is converted to string and capitalized at each request. :compute will be converted to ‘Compute’.

  • request (Symbol)

    This is the request you want to launch on the regions’ service. This is a method call, and then must be a Symbol. @see github.com/fog/fog-openstack For more information on available services and requests name

Returns:

  • (Hash)

    The result of each region, the key being the region_name and the value the result of Region#request with the given argument. @see Region#request to understand how the block is managed.



46
47
48
49
50
51
# File 'lib/openstack-router/router.rb', line 46

def request_each(service, request, *request_args, &block)
  # TODO: make the requests to be parallele to no do them sequentialy...
  Hash[@regions.map do |region|
    [region.name, region.request(service, request, *request_args, &block)]
  end]
end

#rr_createString

Create an UUID for a round robin circuit. This allow to identify a round robin “group” that you can use for a given request or group of request.

Returns:

  • (String)

    A randomly generated UUID.



58
59
60
61
62
# File 'lib/openstack-router/router.rb', line 58

def rr_create
  key = SecureRandom.uuid
  @rr_circuits[key] = nil
  key
end

#rr_last_region(rr_uuid) ⇒ NilClass | Region

Returns the last used region for that round robin circuit. nil is return if the circuit hasn’t been used yet.

Returns:

  • (NilClass | Region)

    the last used region for that round robin circuit. nil is return if the circuit hasn’t been used yet.

Raises:

  • (ArgumentError)


66
67
68
69
70
# File 'lib/openstack-router/router.rb', line 66

def rr_last_region(rr_uuid)
  raise ArgumentError, 'Invalid UUID given' unless @rr_circuits.key?(rr_uuid)

  @rr_circuits[rr_uuid]
end

#rr_request(rr_uuid, service, request, *request_args, &block) ⇒ Object

Raises:

  • (ArgumentError)


72
73
74
75
76
77
# File 'lib/openstack-router/router.rb', line 72

def rr_request(rr_uuid, service, request, *request_args, &block)
  raise ArgumentError, 'Invalid UUID given' unless @rr_circuits.key?(rr_uuid)

  region = rr_next_region(rr_uuid)
  region.request(service, request, *region.my_args(request_args, region_names), &block)
end