Class: Tinet::Data

Inherits:
Object
  • Object
show all
Defined in:
lib/tinet/data.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes, switches, options) ⇒ Data

Returns a new instance of Data.

Parameters:



48
49
50
51
52
# File 'lib/tinet/data.rb', line 48

def initialize(nodes, switches, options)
  @nodes = nodes
  @switches = switches
  @options = options
end

Instance Attribute Details

#nodesObject (readonly)

Returns the value of attribute nodes.



43
44
45
# File 'lib/tinet/data.rb', line 43

def nodes
  @nodes
end

#optionsObject (readonly)

Returns the value of attribute options.



43
44
45
# File 'lib/tinet/data.rb', line 43

def options
  @options
end

#switchesObject (readonly)

Returns the value of attribute switches.



43
44
45
# File 'lib/tinet/data.rb', line 43

def switches
  @switches
end

Class Method Details

.parse(yaml_path) ⇒ Tinet::Data

Parameters:

  • yaml_path (String)

Returns:

Raises:



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/tinet/data.rb', line 10

def parse(yaml_path)
  yaml = YAML.load_file(yaml_path)
  raise InvalidYAMLError, "Nodes must be array" unless yaml['nodes'].is_a?(Array)
  raise InvalidYAMLError, "Switches must be array" unless yaml['switches'].is_a?(Array)

  namespace = yaml.fetch('meta', {}).fetch('namespace', nil)
  Tinet.namespace = namespace unless namespace.nil? || namespace.empty?

  nodes = yaml['nodes'].map { |node| Tinet::Node.parse(node) }
  switches = yaml['switches'].map { |switch| Tinet::Switch.parse(switch) }
  options = {
    pre_cmd: fetch(yaml, 'pre_cmd'),
    pre_init: fetch(yaml, 'pre_init'),
    post_init: fetch(yaml, 'post_init'),
    pre_conf: fetch(yaml, 'pre_conf'),
    post_conf: fetch(yaml, 'post_conf'),
    pre_down: fetch(yaml, 'pre_down'),
    post_down: fetch(yaml, 'post_down')
  }

  self.new(nodes, switches, options)
end