Class: OpenStackRouter::Parameter::Connection

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

Overview

This class is used to parameter the connection to OpenStack instance and services. Its purpose is to encapsulate the connection parameters.

Constant Summary collapse

MANDATORY_OPTIONS =

The mandatory options needed to be pass in argument at initialization of that class.

%i[auth_url username api_key tenant project_id].freeze
OTHER_OPTIONS =

The options that are not mandatory for this object.

%i[region].freeze
ALL_OPTIONS =

Constant containing all the available options.

(MANDATORY_OPTIONS + OTHER_OPTIONS).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



34
35
36
37
# File 'lib/openstack-router/parameter/connection.rb', line 34

def initialize(options)
  mandatory_options(options)
  other_options(options)
end

Instance Attribute Details

#api_keyString

The api key that you have been given to connect to the OpenStack services

Returns:

  • (String)

    the current value of api_key



19
20
21
# File 'lib/openstack-router/parameter/connection.rb', line 19

def api_key
  @api_key
end

#auth_urlString

The OpenStack url of the auth system

Returns:

  • (String)

    the current value of auth_url



19
20
21
# File 'lib/openstack-router/parameter/connection.rb', line 19

def auth_url
  @auth_url
end

#project_idString

The Project ID that you want to limit your actions on the OpenStack services

Returns:

  • (String)

    the current value of project_id



19
20
21
# File 'lib/openstack-router/parameter/connection.rb', line 19

def project_id
  @project_id
end

#regionString | NilClass

The region on which to connect.

Returns:

  • (String | NilClass)

    the current value of region



19
20
21
# File 'lib/openstack-router/parameter/connection.rb', line 19

def region
  @region
end

#tenantString

The tenant that you have been given to connect to the OpenStack services

Returns:

  • (String)

    the current value of tenant



19
20
21
# File 'lib/openstack-router/parameter/connection.rb', line 19

def tenant
  @tenant
end

#usernameString

The username to use to connect to OpenStack

Returns:

  • (String)

    the current value of username



19
20
21
# File 'lib/openstack-router/parameter/connection.rb', line 19

def username
  @username
end

Instance Method Details

#present?(key) ⇒ Boolean

checks if an option is present. This method prevent testing nil? over the attribute.

Returns:

  • (Boolean)

    true if present, false if not

Raises:

  • (ArgumentError)

    if @key is not in the available options



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

def present?(key)
  raise ArgumentError, "unavailable option: #{key}" unless ALL_OPTIONS.include?(key)

  instance_variable_defined?("@#{key}".to_sym)
end

#to_hObject

convert the connection parameters to hash



45
46
47
# File 'lib/openstack-router/parameter/connection.rb', line 45

def to_h
  ALL_OPTIONS.map { |key| [key, send(key)] }.to_h
end

#to_os_hObject

get the corresponding connection parameter hash with openstack_ prefix.



40
41
42
# File 'lib/openstack-router/parameter/connection.rb', line 40

def to_os_h
  to_h.map { |key, value| ["openstack_#{key}", value] }.to_h
end

#to_sObject

convert to string the options (first convert to Hash, then the Hash to String)



50
51
52
# File 'lib/openstack-router/parameter/connection.rb', line 50

def to_s
  to_h.to_s
end