class OpenStackRouter::Parameter::Connection
This class is used to parameter the connection to OpenStack instance and services. Its purpose is to encapsulate the connection parameters.
@author Roland Laurès @attr_reader [String] auth_url
The OpenStack url of the auth system @attr_reader [String | NilClass] username The username to use to connect to OpenStack
@note You can have username or userid set as you prefere, but need at least one of them set.
@attr_reader [String | NilClass] userid The user_id that you have been given to connect
to the OpenStack services @note You can have username or userid set as you prefere, but need at least one of them set.
@attr_reader [String] api_key
The api key that you have been given to connect
to the OpenStack services
@attr_reader [String | NilClass] region The region on which to connect. @attr_reader [String | NilClass] project_id
The Project ID that you want to limit your
actions on the OpenStack services
Constants
- ALL_OPTIONS
Constant containing all the available options.
- MANDATORY_OPTIONS
The mandatory options needed to be pass in argument at initialization of that class.
- OTHER_OPTIONS
The options that are not mandatory for this object.
- PAIRED_MANDATORY_OPTIONS
Paired mandatory options (means that one of one pair must me present, not the other).
Attributes
Public Class Methods
# File lib/openstack-router/parameter/connection.rb, line 38 def initialize(options) mandatory_options(options) paired_mandatory_options(options) other_options(options) end
Public Instance Methods
checks if an option is present. This method prevent testing nil? over the attribute.
@return [Boolean] true if present, false if not @raise [ArgumentError] if @key is not in the available options
# File lib/openstack-router/parameter/connection.rb, line 64 def present?(key) raise ArgumentError, "unavailable option: #{key}" unless ALL_OPTIONS.include?(key) instance_variable_defined?("@#{key}".to_sym) end
convert the connection parameters to hash
# File lib/openstack-router/parameter/connection.rb, line 50 def to_h ALL_OPTIONS.map { |key| [key, send(key)] }.to_h end
get the corresponding connection parameter hash with openstack_ prefix.
# File lib/openstack-router/parameter/connection.rb, line 45 def to_os_h to_h.map { |key, value| ["openstack_#{key}".to_sym, value] }.reject { |a| a.last.nil? }.to_h end
convert to string the options (first convert to Hash, then the Hash to String)
# File lib/openstack-router/parameter/connection.rb, line 55 def to_s to_h.to_s end
Private Instance Methods
# File lib/openstack-router/parameter/connection.rb, line 79 def mandatory_options(cfg) missing_keys = [] MANDATORY_OPTIONS.each do |key| if cfg.key? key send("#{key}=".to_sym, cfg[key]) else missing_keys.append(key) end end raise ArgumentError, "Missing keys: #{missing_keys}" unless missing_keys.empty? end
# File lib/openstack-router/parameter/connection.rb, line 105 def other_options(cfg) cfg.each do |key, value| send("#{key}=".to_sym, value) if OTHER_OPTIONS.include?(key) end end
# File lib/openstack-router/parameter/connection.rb, line 91 def paired_mandatory_options(cfg) missing_keys = [] PAIRED_MANDATORY_OPTIONS.each do |key1, key2| if cfg.key? key1 send("#{key1}=".to_sym, cfg[key1]) elsif cfg.key? key2 send("#{key2}=".to_sym, cfg[key2]) else missing_keys.append([key1, key2]) end end raise ArgumentError, "Missing paired keys: #{missing_keys}" unless missing_keys.empty? end